Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

troika-3d

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

troika-3d - npm Package Compare versions

Comparing version 0.20.0 to 0.21.0

src/facade/instancing/InstancingDerivedMaterial.js

13

CHANGELOG.md

@@ -6,2 +6,15 @@ # Change Log

# [0.21.0](https://github.com/protectwise/troika/compare/v0.20.0...v0.21.0) (2020-03-27)
### Features
* **troika-3d:** instancing now supports custom derived materials ([bad5e02](https://github.com/protectwise/troika/commit/bad5e022e29e0b656258017f8697d1611eb9d2e9))
* **troika-3d:** reduce instancing batch size to 128 ([dc4bd8a](https://github.com/protectwise/troika/commit/dc4bd8aadd5826b4fe8247f981a03f43624a7bd7))
* **troika-3d:** update ThreeJS support up to r115 ([531ff6a](https://github.com/protectwise/troika/commit/531ff6a175b41d6fd273b1ca0fa91c5826360b22))
# [0.20.0](https://github.com/protectwise/troika/compare/v0.19.0...v0.20.0) (2020-03-16)

@@ -8,0 +21,0 @@

163

dist/troika-3d.umd.min.js

@@ -1,84 +0,83 @@

'use strict';(function(f,e){"object"===typeof exports&&"undefined"!==typeof module?e(exports,require("three"),require("troika-core"),require("troika-three-utils"),require("react"),require("prop-types")):"function"===typeof define&&define.amd?define("exports three troika-core troika-three-utils react prop-types".split(" "),e):(f=f||self,e(f.troika_3d={},f.THREE,f.troika_core,f.troika_three_utils,f.React,f.PropTypes))})(this,function(f,e,l,t,x,p){function ua(c,b){return c.distance-b.distance}function Q(c){return!1===
'use strict';(function(f,e){"object"===typeof exports&&"undefined"!==typeof module?e(exports,require("three"),require("troika-core"),require("troika-three-utils"),require("react"),require("prop-types")):"function"===typeof define&&define.amd?define("exports three troika-core troika-three-utils react prop-types".split(" "),e):(f=f||self,e(f.troika_3d={},f.THREE,f.troika_core,f.troika_three_utils,f.React,f.PropTypes))})(this,function(f,e,l,z,v,m){function ua(c,b){return c.distance-b.distance}function Q(c){return!1===
c.isRenderable&&(!c.children.length||c.children.every(Q))}function R(c,b,a){function d(a,d){Object.defineProperty(g.prototype,a,{set:function(b){b!==this.threeObject[a]&&(this.threeObject[a]=b,d&&(this._projectionChanged=!0))},get:function(){return this.threeObject[a]}})}var g=function(a){function d(d){var b=new c;a.call(this,d,b);this.lookAt=this.up=null;this._projectionChanged=!1;this._frustum=new e.Frustum;b.updateMatrixWorld=va}a&&(d.__proto__=a);d.prototype=Object.create(a&&a.prototype);d.prototype.constructor=
d;d.prototype.afterUpdate=function(){this.lookAt&&(S.copy(this.lookAt),T.copy(this.up||e.Object3D.DefaultUp),U.lookAt(this.threeObject.position,S,T),C.setFromRotationMatrix(U),this.quaternionX=C.x,this.quaternionY=C.y,this.quaternionZ=C.z,this.quaternionW=C.w);a.prototype.afterUpdate.call(this)};d.prototype.updateMatrices=function(){var d=this.threeObject;this._projectionChanged&&(d.updateProjectionMatrix(),this._projectionChanged=!1,this._projectionMatrixVersion=wa++);var b=this._worldMatrixVersion;
d;d.prototype.afterUpdate=function(){this.lookAt&&(S.copy(this.lookAt),T.copy(this.up||e.Object3D.DefaultUp),U.lookAt(this.threeObject.position,S,T),B.setFromRotationMatrix(U),this.quaternionX=B.x,this.quaternionY=B.y,this.quaternionZ=B.z,this.quaternionW=B.w);a.prototype.afterUpdate.call(this)};d.prototype.updateMatrices=function(){var d=this.threeObject;this._projectionChanged&&(d.updateProjectionMatrix(),this._projectionChanged=!1,this._projectionMatrixVersion=wa++);var b=this._worldMatrixVersion;
a.prototype.updateMatrices.call(this);b!==this._worldMatrixVersion&&d.matrixWorldInverse.getInverse(d.matrixWorld)};d.prototype.getFrustum=function(){this.updateMatrices();var a=this._frustum,d=this._worldMatrixVersion,b=this._projectionMatrixVersion;if(a._lastWorldMatrixVersion!==d||a._lastProjMatrixVersion!==b){var c=this.threeObject;c=(new e.Matrix4).multiplyMatrices(c.projectionMatrix,c.matrixWorldInverse);a.setFromMatrix(c);a._lastWorldMatrixVersion=d;a._lastProjMatrixVersion=b}return a};d.prototype.getRayAtProjectedCoords=
function(a,d){var b=V.ray=new e.Ray;V.setFromCamera(xa.set(a,d),this.threeObject);return b};return d}(n);b.forEach(function(a){d(a,!0)});a&&a.forEach(function(a){d(a,!1)});return g}function B(c,b,a){var d=function(a){function d(d){a.call(this,d,new c)}a&&(d.__proto__=a);d.prototype=Object.create(a&&a.prototype);d.prototype.constructor=d;var g={showHelper:{configurable:!0}};g.showHelper.set=function(a){var d=this._helper;!!a!==!!d&&(a?this.threeObject.add(this._helper=new b(this.threeObject)):d&&(d.dispose(),
this.threeObject.remove(d),this._helper=null))};d.prototype.afterUpdate=function(){a.prototype.afterUpdate.call(this);this._helper&&this._helper.update()};Object.defineProperties(d.prototype,g);return d}(W);a&&Object.defineProperties(d.prototype,a);return d}function J(c,b,a){void 0===a&&(a=[]);c=t.expandShaderIncludes(c);b=t.expandShaderIncludes(b);var d=X.test(c),g=Y.test(c),h=Z.test(c),u=t.getShaderUniformTypes(c),e=t.getShaderUniformTypes(b),k=["\nattribute vec4 troika_modelMatrixRow0;\nattribute vec4 troika_modelMatrixRow1;\nattribute vec4 troika_modelMatrixRow2;\n"],
f=[],v=[];if(d||g||h)c=c.replace(X,F),f.push(ya);if(g||h)c=c.replace(Y,F),f.push("\nmat4 troika_modelViewMatrix = viewMatrix * troika_modelMatrix;\n");h&&(c=c.replace(Z,F),f.push("\nmat3 troika_normalMatrix = transposeMat3(inverse(mat3(troika_modelViewMatrix)));\n"),/\binverse\s*\(/.test(c)||k.push("\n#if __VERSION__ < 300\n// matrix inversion utility for pre-ES3 - credit https://github.com/stackgl/glsl-inverse\nmat3 inverse(mat3 m) {\n float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];\n float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];\n float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];\n\n float b01 = a22 * a11 - a12 * a21;\n float b11 = -a22 * a10 + a12 * a20;\n float b21 = a21 * a10 - a11 * a20;\n\n float det = a00 * b01 + a01 * b11 + a02 * b21;\n\n return mat3(\n b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),\n b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),\n b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)\n ) / det;\n}\n#endif\n"));
a.forEach(function(a){var d=u[a],g=e[a];if(d||g){var h=new RegExp("\\b"+a+"\\b","g");k.push("attribute "+(d||g)+" troika_"+a+";");d&&(c=c.replace(h,F));g&&(b=b.replace(h,za),d="varying "+g+" troika_vary_"+a+";",k.push(d),v.push(d),f.push("troika_vary_"+a+" = troika_"+a+";"))}});c=c.replace(t.voidMainRegExp,"\n"+k.join("\n")+"\n$&\n"+f.join("\n")+"\n");v.length&&(b=b.replace(t.voidMainRegExp,"\n"+v.join("\n")+"\n$&\n "));return{vertexShader:c,fragmentShader:b}}function aa(c,b,a){var d=c.itemSize;
1===d?c.setX(b,a):2===d?c.setXY(b,a.x,a.y):3===d?a.isColor?c.setXYZ(b,a.r,a.g,a.b):c.setXYZ(b,a.x,a.y,a.z):4===d&&c.setXYZW(b,a.x,a.y,a.z,a.w)}x=x&&x.hasOwnProperty("default")?x["default"]:x;p=p&&p.hasOwnProperty("default")?p["default"]:p;var K=l.utils.assign,ba=l.utils.forOwn,ca=new e.Vector3,Aa=new e.Vector3,w=function(){var c={callback:function(b){c.value=b},value:null};return c}(),Ba={type:"removed"},D=[],Ca=0,Da=0,n=function(c){function b(a,d){c.call(this,a);d.matrixAutoUpdate=!1;this.threeObject=
d;d.$facade=this;var b=!1!==d.isRenderable;b||(d.layers.mask=0);for(;a;){if(a.isObject3DFacade){this._parentObject3DFacade=a;b&&this._addToThreeObjectTree();break}a=a.parent}this.notifyWorld("object3DAdded")}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){this.updateMatrices();this._checkBoundsChange();this._worldMatrixVersion>this._worldMatrixVersionAfterLastUpdate&&(this.shouldUpdateChildren()||this.traverse(function(a,d){a!==
d&&a.updateMatrices&&(a.updateMatrices(),a._checkBoundsChange())},this),this._worldMatrixVersionAfterLastUpdate=this._worldMatrixVersion);c.prototype.afterUpdate.call(this);if(this._removeChildIds){var a=this.threeObject,d=this._removeChildIds;a.children=a.children.filter(function(a){return a.id in d?(a.parent=null,a.dispatchEvent(Ba),!1):!0});var b=this._parentObject3DFacade;Q(a)&&b&&b.threeObject===a.parent&&b._queueRemoveChildObject3D(a.id);this._removeChildIds=null}};b.prototype.updateMatrices=
function(){var a=this.threeObject,d=this._parentObject3DFacade;if(this._matrixChanged){a.matrix.compose(a.position,a.quaternion,a.scale);this._matrixChanged=!1;var b=!0}else b=d&&d._worldMatrixVersion>this._worldMatrixVersion;if(b){d?a.matrixWorld.multiplyMatrices(d.threeObject.matrixWorld,a.matrix):a.matrixWorld.copy(a.matrix);a=a.children;d=0;for(b=a.length;d<b;d++)a[d].$facade||a[d].updateMatrixWorld(!0);this.markWorldMatrixDirty()}};b.prototype.markWorldMatrixDirty=function(){this._worldMatrixVersion=
++Ca;this._boundsChanged=!0};b.prototype._checkBoundsChange=function(){var a=this._boundsChanged;if(!a){var d=this._getGeometryBoundingSphere();d&&d.version!==this._lastGeometrySphereVersion&&(a=!0,this._lastGeometrySphereVersion=d.version)}a&&(this.notifyWorld("object3DBoundsChanged"),this._boundsChanged=!1)};b.prototype.getWorldPosition=function(a){this.updateMatrices();return(a||new e.Vector3).setFromMatrixPosition(this.threeObject.matrixWorld)};b.prototype.getCameraPosition=function(a){a=a||new e.Vector3;
this.notifyWorld("getCameraPosition",a);return a};b.prototype.getCameraFacade=function(){w.value=null;this.notifyWorld("getCameraFacade",w);return w.value};b.prototype.getCameraDistance=function(){var a=this.getCameraPosition(Aa),d=this.getWorldPosition(ca);return a.distanceTo(d)};b.prototype.getProjectedPosition=function(a,d,b){this.updateMatrices();w.value=null;w.worldPosition=ca.set(a||0,d||0,b||0).applyMatrix4(this.threeObject.matrixWorld);this.notifyWorld("projectWorldPosition",w);return w.value};
b.prototype.getSceneFacade=function(){w.value=null;this.notifyWorld("getSceneFacade",w);return w.value};b.prototype.getBoundingSphere=function(){var a=this._getGeometryBoundingSphere();if(!a)return null;this.updateMatrices();var d=this._boundingSphere;d||(d=this._boundingSphere=new e.Sphere);if(d._geometrySphereVersion!==a.version||d._worldMatrixVersion!==this._worldMatrixVersion)d.copy(a),d.applyMatrix4(this.threeObject.matrixWorld),d._worldMatrixVersion=this._worldMatrixVersion,d._geometrySphereVersion=
a.version;return d};b.prototype._getGeometryBoundingSphere=function(){var a=this.getGeometry();if(a){var d=a.boundingSphere,b=!1;if(d)if(a.isBufferGeometry){var c=a.attributes.position;c&&d._posAttrVersion!==c.version&&(a.computeBoundingSphere(),d._posAttrVersion=c.version,b=!0)}else a._lastBoundingSphere&&d.equals(a._lastBoundingSphere)||(a._lastBoundingSphere=d.clone(),b=!0);else a.computeBoundingSphere(),d=a.boundingSphere,b=!0;b&&(d.version=++Da);return d}return null};b.prototype.getGeometry=
function(){var a=this.threeObject;return a&&a.geometry};b.prototype.raycast=function(a){return this.threeObject?this._raycastObject(this.threeObject,a):null};b.prototype._raycastObject=function(a,d){if(a.visible){D.length=0;var b=null,c=this.raycastSide;null!=c&&(b=a.material.side,a.material.side=c);a.raycast(d,D);null!==b&&(a.material.side=b);if(D.length)return D.sort(ua),D.slice()}return null};b.prototype._addToThreeObjectTree=function(){var a=this._parentObject3DFacade;a&&this.threeObject.parent!==
a.threeObject&&(a.threeObject.add(this.threeObject),a._addToThreeObjectTree())};b.prototype._queueRemoveChildObject3D=function(a){(this._removeChildIds||(this._removeChildIds=Object.create(null)))[a]=!0};b.prototype.destructor=function(){this.notifyWorld("object3DRemoved");var a=this._parentObject3DFacade;a&&a._queueRemoveChildObject3D(this.threeObject.id);delete this.threeObject;c.prototype.destructor.call(this)};return b}(l.PointerEventTarget);["castShadow","receiveShadow","renderOrder","visible"].forEach(function(c){Object.defineProperty(n.prototype,
c,{get:function(){return this.threeObject[c]},set:function(b){this.threeObject[c]=b}})});n.prototype.raycastSide=null;ba({position:{x:"x",y:"y",z:"z"},scale:{x:"scaleX",y:"scaleY",z:"scaleZ"},rotation:{x:"rotateX",y:"rotateY",z:"rotateZ",order:"rotateOrder"},quaternion:{x:"quaternionX",y:"quaternionY",z:"quaternionZ",w:"quaternionW"}},function(c,b){ba(c,function(a,d){Object.defineProperty(n.prototype,a,{get:(new Function("return function "+a+"$get() {\n return this.threeObject."+b+"."+d+"\n}"))(),
set:(new Function("return function "+a+"$set(value) {\n //let obj = this.threeObject."+b+"\n if (this.threeObject."+b+"."+d+" !== value) {\n this.threeObject."+b+"."+d+" = value\n if (!this._matrixChanged) {\n this._matrixChanged = true\n }\n }\n}"))()})})});Object.defineProperty(n.prototype,"scale",{get:function(){return this.threeObject.scale.x},set:function(c){var b=this.threeObject.scale;if(c!==b.x||c!==b.y||c!==b.z)b.x=b.y=b.z=c,this._matrixChanged||(this._matrixChanged=!0)}});
Object.defineProperty(n.prototype,"isObject3DFacade",{value:!0});K(n.prototype,{threeObject:null,_parentObject3DFacade:null,_removeChildIds:null,_matrixChanged:!0,_worldMatrixVersion:-1,_worldMatrixVersionAfterLastUpdate:-1,_boundingSphereChanged:!1});l.Facade.defineEventProperty(n,"onBeforeRender","beforerender");l.Facade.defineEventProperty(n,"onAfterRender","afterrender");var va=function(){},V=new e.Raycaster,xa=new e.Vector2,S=new e.Vector3,U=new e.Matrix4,C=new e.Quaternion,T=new e.Vector3(0,
1,0),wa=0,da=R(e.PerspectiveCamera,["fov","aspect","near","far"],["focus","filmGauge","filmOffset"]);K=R(e.OrthographicCamera,"left right top bottom near far".split(" "));var ea=function(c){function b(a){var d=new e.Group;d.isRenderable=!1;c.call(this,a,d)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);return b.prototype.constructor=b}(n),Ea=function(c){function b(a){var d=new e.Object3D;d.isRenderable=!1;c.call(this,a,d);this.html=null;this.exact=!1;this.notifyWorld("addHtmlOverlay",
this)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.destructor=function(){this.notifyWorld("removeHtmlOverlay",this);c.prototype.destructor.call(this)};return b}(n),W=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={color:{configurable:!0},shadow:{configurable:!0}};a.color.set=function(a){this.threeObject.color.set(a)};a.color.get=function(){return this.threeObject.color.getHex()};
a.shadow.get=function(){return this.threeObject.shadow};a.shadow.set=function(a){l.utils.assignDeep(this.threeObject.shadow,a)};Object.defineProperties(b.prototype,a);return b}(n);"intensity distance angle penumbra decay castShadow width height".split(" ").forEach(function(c){Object.defineProperty(W.prototype,c,{get:function(){return this.threeObject[c]},set:function(b){this.threeObject[c]=b}})});var fa=B(e.AmbientLight),ha=B(e.DirectionalLight,e.DirectionalLightHelper),ia=B(e.SpotLight,e.SpotLightHelper),
ja=B(e.PointLight,e.PointLightHelper),ka=B(e.HemisphereLight,e.HemisphereLightHelper,{groundColor:{set:function(c){this.threeObject.groundColor.set(c)},get:function(){return this.threeObject.groundColor.getHex()}}}),Fa=B(e.RectAreaLight),ya="\nmat4 troika_modelMatrix = mat4(\n %0.x, %1.x, %2.x, 0.0,\n %0.y, %1.y, %2.y, 0.0,\n %0.z, %1.z, %2.z, 0.0,\n %0.w, %1.w, %2.w, 1.0\n);\n".replace(/%/g,"troika_modelMatrixRow"),X=/\bmodelMatrix\b/g,Y=/\bmodelViewMatrix\b/g,Z=/\bnormalMatrix\b/g,la=/\buniform\s+(int|float|vec[234])\s+$/,
F=function(c,b,a){return la.test(a.substr(0,b))?c:"troika_"+c},za=function(c,b,a){return la.test(a.substr(0,b))?c:"troika_vary_"+c},y=l.utils.assign,Ga=l.utils.assignIf,L=function(c){function b(a){c.call(this,a);this._instanceables=Object.create(null);this._batchGeometryPool=new G;this._needsRebatch=!0;this.addEventListener("beforerender",this._setupBatchObjects.bind(this));this.addEventListener("afterrender",this._teardownBatchObjects.bind(this))}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);
b.prototype.constructor=b;b.prototype.onNotifyWorld=function(a,d,b){var c=this._notifyWorldHandlers[d];if(c)c.call(this,a,b);else if(this.parent)this.parent.onNotifyWorld(a,d,b)};b.prototype._setupBatchObjects=function(a,d,b){b=this._instanceables;a=this._batchObjectsByKey;var c=this._needsRebatch;if(!c)for(var g in a)if(this._getBatchKey(a[g][0].$troikaBatchBaseObj)!==g){c=!0;break}if(c){a=this._batchObjectsByKey=Object.create(null);g=this._batchGeometryPool;for(var e in b){c=b[e];var k=c.threeObject,
f=c.instancedThreeObject;if(f&&k.visible){var v=this._getBatchKey(f),l=f.material.instanceUniforms,n=a[v]||(a[v]=[]),p=(v=n[n.length-1])&&v.geometry;if(!p||1024===p.maxInstancedCount){v=this._getBatchObject(f);p=v.geometry;for(var q=p._instanceAttrs.matrix,m=0;3>m;m++)q[m].version++;if(l)for(q=p._instanceAttrs.uniforms,m=l.length;m--;)q[l[m]].version++;n.push(v)}n=p.maxInstancedCount++;q=p._instanceAttrs.matrix;k=k.matrixWorld.elements;q[0].setXYZW(n,k[0],k[4],k[8],k[12]);q[1].setXYZW(n,k[1],k[5],
k[9],k[13]);q[2].setXYZW(n,k[2],k[6],k[10],k[14]);if(l)for(q=p._instanceAttrs.uniforms,k=l.length;k--;){m=l[k];p=q[m];var r=c._instanceUniforms;m=r&&m in r?r[m]:t.getShadersForMaterial(f.material).uniforms[m].value;aa(p,n,m)}c._instancingBatchObject=v;c._instancingBatchAttrOffset=n}else c._instancingBatchObject=c._instancingBatchAttrOffset=null}g.disposeUnused()}g=b=e=0;for(var w in a)for(c=a[w],d.children.push.apply(d.children,c),b++,f=c.length;f--;)e++,g+=c[f].geometry.maxInstancedCount;this.notifyWorld("statsUpdate",
{"Instancing Batch Groups":b,"Instancing Batches":e,"Instanced Objects":g});this._needsRebatch=!1};b.prototype._onInstanceAdded=function(a){this._instanceables[a.$facadeId]=a;this._needsRebatch=!0};b.prototype._onInstanceRemoved=function(a){delete this._instanceables[a.$facadeId];this._needsRebatch=!0};b.prototype._onInstanceChanged=function(a){this._needsRebatch=!0};b.prototype._onInstanceMatrixChanged=function(a){if(!this._needsRebatch){var d=a.instancedThreeObject,b=a._instancingBatchObject,c=
a._instancingBatchAttrOffset;d&&b&&this._getBatchKey(d)===this._getBatchKey(b)?(d=b.geometry._instanceAttrs.matrix,a=a.threeObject.matrixWorld.elements,d[0].setXYZW(c,a[0],a[4],a[8],a[12]).version++,d[1].setXYZW(c,a[1],a[5],a[9],a[13]).version++,d[2].setXYZW(c,a[2],a[6],a[10],a[14]).version++):(a._instancingBatchObject=a._instancingBatchAttrOffset=null,this._needsRebatch=!0)}};b.prototype._onInstanceUniformChanged=function(a,d){if(!this._needsRebatch){var b=a.instancedThreeObject,c=a._instancingBatchObject;
b&&c&&this._getBatchKey(b)===this._getBatchKey(c)?(b=c.geometry._instanceAttrs.uniforms[d],aa(b,a._instancingBatchAttrOffset,a._instanceUniforms[d]),b.version++):(a._instancingBatchObject=a._instancingBatchAttrOffset=null,this._needsRebatch=!0)}};b.prototype._getBatchKey=function(a){var d=this._batchKeysCache||(this._batchKeysCache=Object.create(null)),b=d&&d[a.id];if(!b){b=a.material;var c=t.getShadersForMaterial(b),e=b.instanceUniforms;b=a.geometry.id+"|"+b.id+"|"+c.vertexShader+"|"+c.fragmentShader+
"|"+(e?e.sort().join(","):"");d[a.id]=b}return b};b.prototype._getInstanceUniformsTypes=function(a){var b=this._uniformTypesCache||(this._uniformTypesCache=Object.create(null)),c=b[a.id];if(!c&&(c=b[a.id]=Object.create(null),(b=a.instanceUniforms)&&b.length)){a=t.getShadersForMaterial(a);var h=a.fragmentShader;a=y(t.getShaderUniformTypes(t.expandShaderIncludes(a.vertexShader)),t.getShaderUniformTypes(t.expandShaderIncludes(h)));for(h=b.length;h--;){var e=b[h];a[e]&&(c[e]=a[e])}}return c};b.prototype._getBatchObject=
function(a){var b,c=a.geometry,h=a.material;if(!c.isBufferGeometry)throw Error("Instanceable proto object must use a BufferGeometry");var u=this._getBatchKey(a),f=this._getInstanceUniformsTypes(h);c=this._batchGeometryPool.borrow(u,c,f);c.maxInstancedCount=0;u=Object.create(h);var k=h.instanceUniforms||[];k.sort();u.defines=Ga((b={},b["TROIKA_INSTANCED_"+k.join("_")]="",b),u.defines);u.onBeforeCompile=function(a){var b=h.$troikaUpgraded,d=a.vertexShader+"|"+a.fragmentShader+"|"+k.join(",");b&&b.upgradeKey===
d||(b=h.$troikaUpgraded=J(a.vertexShader,a.fragmentShader,k),b.upgradeKey=d);y(a,b)};var l=null;b=Object.create(a,{customDepthMaterial:{get:function(){return l=ma()}},customDistanceMaterial:{get:function(){return l=na()}},modelViewMatrix:{value:function(){var a=new e.Matrix4;a.multiplyMatrices=function(a,b){e.Matrix4.prototype.multiplyMatrices.call(this,a,b);l&&l._updateViewMatrix&&l._updateViewMatrix(a)};return a}()}});b.$troikaBatchBaseObj=a;b.$troikaInstancingManager=this;b.visible=!0;b.frustumCulled=
!1;b.geometry=c;b.material=u;return b};b.prototype._teardownBatchObjects=function(a,b,c){var d=this;this._batchGeometryPool.releaseAll();this._uniformTypesCache=this._batchKeysCache=null;b.children=b.children.filter(function(a){return a.$troikaInstancingManager!==d})};b.prototype.destructor=function(){var a=this._batchGeometryPool;a.releaseAll();a.disposeUnused();c.prototype.destructor.call(this)};return b}(ea),G=function(){this._poolsByKey=Object.create(null)};G.prototype.borrow=function(c,b,a){var d=
this._poolsByKey;c=d[c]||(d[c]={geometries:[],firstFree:0});d=c.geometries[c.firstFree++];if(!d){d=new e.InstancedBufferGeometry;y(d,b);d.attributes=y({},b.attributes);b=d._instanceAttrs={matrix:[],uniforms:Object.create(null)};for(var g=0;3>g;g++){var h=new e.InstancedBufferAttribute(new Float32Array(4096),4);h.setUsage?h.setUsage(35048):h.dynamic=!0;d.attributes["troika_modelMatrixRow"+g]=h;b.matrix[g]=h}for(var u in a)g=a[u],h=Ha[g],g=new e.InstancedBufferAttribute(new ("int"===g?Uint32Array:Float32Array)(1024*
h),h),g.setUsage?g.setUsage(35048):g.dynamic=!0,d.attributes["troika_"+u]=g,b.uniforms[u]=g;c.geometries.push(d)}return d};G.prototype.releaseAll=function(){var c=this._poolsByKey;if(c)for(var b in c)c[b].firstFree=0};G.prototype.disposeUnused=function(){var c=this._poolsByKey;if(c)for(var b in c){var a=c[b],d=a.firstFree;a=a.geometries;for(var g=d,h=a.length;g<h;g++){var e=a[g].attributes,f;for(f in e)e.hasOwnProperty(f)&&0!==f.indexOf("troika_")&&delete e[f];try{a[g].dispose()}catch(k){}a[g]._instanceAttrs=
null}a.length=d}};var r=L.prototype;r._notifyWorldHandlers={instanceableAdded:r._onInstanceAdded,instanceableRemoved:r._onInstanceRemoved,instanceableChanged:r._onInstanceChanged,instanceableMatrixChanged:r._onInstanceMatrixChanged,instanceableUniformChanged:r._onInstanceUniformChanged};var Ha={"int":1,"float":1,vec2:2,vec3:3,vec4:4},ma=function(){var c=y({},e.ShaderLib.depth);c.vertexShader=J(c.vertexShader,"",[]).vertexShader;var b=new e.ShaderMaterial(c);b.isMeshDepthMaterial=!0;b.depthPacking=
e.RGBADepthPacking;ma=function(){return b};return b},na=function(){var c=y({},e.ShaderLib.distanceRGBA),b=new e.Matrix4;c.vertexShader=J(c.vertexShader,"",[]).vertexShader;c.uniforms=y({viewMatrix:{value:b}},c.uniforms);var a=new e.ShaderMaterial(c);a.isMeshDistanceMaterial=!0;a.referencePosition=new e.Vector3;a._updateViewMatrix=function(d){b.copy(d);a.uniformsNeedUpdate=!0};na=function(){return a};return a},Ia={ambient:fa,directional:ha,spot:ia,point:ja,hemisphere:ka},Ja=[{distance:Infinity}],Ka=
new e.Sphere(void 0,Infinity),oa=[null],pa=function(c){function b(a){var b=new e.Scene;b.autoUpdate=!1;c.call(this,a,b)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={fog:{configurable:!0}};b.prototype.describeChildren=function(){var a={key:"instancingMgr",facade:L,children:this.objects},b=this.lights;b&&(a=[a],Array.isArray(b)||(oa[0]=b,b=oa),b.forEach(function(b,d){var c=b.facade||Ia[b.type];if("function"===typeof c){var g=l.utils.assign({},b);delete g.type;
g.key=b.key||"light"+d;g.facade=c;a.push(g)}}));return a};a.fog.set=function(a){var b=this._fogObj;if(a){var d="density"in a,c=d?e.FogExp2:e.Fog;b&&b instanceof c||(b=this._fogObj=new c);b.color.set(a.color);d?b.density=a.density:(b.near=a.near,b.far=a.far)}else b=this._fogObj=null;this.threeObject.fog=b};b.prototype.getBoundingSphere=function(){return Ka};b.prototype.raycast=function(a){return Ja};Object.defineProperties(b.prototype,a);return b}(n);r=l.utils.assign;var La=l.utils.forOwn,M=new e.Sphere,
Ma=Math.sqrt(3),m=function(){this.root=null;this.keysToLeaves=Object.create(null)};m.prototype.putSpheres=function(c){var b=this;La(c,function(a,d){b.putSphere(d,a)})};m.prototype.putSphere=function(c,b){var a=this,d=b.center,g=b.radius,h=this.root;if(!b||isNaN(g)||isNaN(d.x))console.warn("Invalid sphere",b);else{if(c in this.keysToLeaves)return this._updateSphere(c,b);if(h)if(h.isLeaf){g=this.root;var e=h.dataX,f=h.dataY,k=h.dataZ;if(e===d.x&&f===d.y&&k===d.z)this._insertIntoOctant(c,b,h);else{var l=
new q;h=l.cx=Math.round((e+d.x)/2);var m=l.cy=Math.round((f+d.y)/2);d=l.cz=Math.round((k+d.z)/2);l.cr=Math.ceil(Math.max(Math.abs(h-e),Math.abs(m-f),Math.abs(d-k))+1E-5);this.root=l;g.forEachLeafSphere(function(b,d){return a._insertIntoOctant(d,b,l)});this._insertIntoOctant(c,b,l)}}else this._expandToCoverPoint(d.x,d.y,d.z),this._insertIntoOctant(c,b,this.root);else d=new q,d.isLeaf=!0,d.addSphereData(c,b),this.root=d,this.keysToLeaves[c]=d}};m.prototype._expandToCoverPoint=function(c,b,a){for(;!this.root.containsPoint(c,
b,a);){var d=this.root,g=d.cx,h=d.cy,e=d.cz,f=d.cr,k=new q;k.maxRadius=d.maxRadius;k.sphereCount=d.sphereCount;k.leafCount=d.leafCount;k.cx=g+f*(c<g?-1:1);k.cy=h+f*(b<h?-1:1);k.cz=e+f*(a<e?-1:1);k.cr=2*f;g=k.getSubOctantIndexForPoint(g,h,e);d.parent=k;d.index=g;k[g]=d;this.root=k}};m.prototype._insertIntoOctant=function(c,b,a){var d=b.center,g=b.radius;if(a.isLeaf){var h=a.dataX,e=a.dataY,f=a.dataZ;if(d.x===h&&d.y===e&&d.z===f){a.addSphereData(c,b);for(b=a.parent;b;b=b.parent)g>b.maxRadius&&(b.maxRadius=
g);this.keysToLeaves[c]=a}else g=Na(a),a.parent[a.index]=g,g.addOctantForPoint(a,h,e,f),this._insertIntoOctant(c,b,g)}else{a.sphereCount++;h=a.getSubOctantIndexForPoint(d.x,d.y,d.z);if(h=a[h])return this._insertIntoOctant(c,b,h);h=new q;h.isLeaf=!0;a.addOctantForPoint(h,d.x,d.y,d.z);h.addSphereData(c,b);for(a=h.parent;a;a=a.parent)g>a.maxRadius&&(a.maxRadius=g),a.leafCount++;this.keysToLeaves[c]=h}};m.prototype.removeSphere=function(c){var b=this.keysToLeaves[c];if(b){for(var a=b.parent;a;)a.sphereCount--,
a=a.parent;if(1<b.sphereCount)b.removeSphereData(c),b.updateMaxRadii();else{a=b;do{var d=a.parent;(b=d)&&(d[a.index]=null);a=a.parent}while(a&&0===a.sphereCount);if(!b){this.root=null;return}d=null;for(a=b;a;)a.leafCount--,1===a.leafCount&&(d=a),a=a.parent;d?(a=this._findSingleLeaf(d),(b=d.parent)?(b.addOctantForPoint(a,a.cx,a.cy,a.cz),b.updateMaxRadii()):this.root=a):b.updateMaxRadii()}delete this.keysToLeaves[c]}};m.prototype._updateSphere=function(c,b){var a=this.keysToLeaves[c],d=b.center;if(a.containsPoint(d.x,
d.y,d.z)){var g=d.x!==a.dataX||d.y!==a.dataY||d.z!==a.dataZ;1<a.sphereCount&&g?(a.removeSphereData(c),a.updateMaxRadii(),this._insertIntoOctant(c,b,a)):(g&&(a.dataX=d.x,a.dataY=d.y,a.dataZ=d.z),b.radius!==a.maxRadius&&a.updateMaxRadii())}else this.removeSphere(c),this.putSphere(c,b)};m.prototype._findSingleLeaf=function(c){function b(a){a.isLeaf&&(d=a)}function a(a){d=null;this.walkBranch(a,b);return d}var d;this._findSingleLeaf=a;return a.call(this,c)};m.prototype.walkTree=function(c){this.root&&
this.walkBranch(this.root,c)};m.prototype.walkBranch=function(c,b){if(!1!==b(c)&&!c.isLeaf)for(var a=0;8>a;a++)null!==c[a]&&this.walkBranch(c[a],b)};m.prototype.forEachSphereOnRay=function(c,b,a){return this._forEachMatchingSphere(c.intersectsSphere.bind(c),b,a)};m.prototype.forEachIntersectingSphere=function(c,b,a){return this._forEachMatchingSphere(c.intersectsSphere.bind(c),b,a)};m.prototype._forEachMatchingSphere=function(c,b,a){function d(d,h){c(d)&&b.call(a,d,h)}this.walkTree(function(a){if(a.isLeaf)a.forEachLeafSphere(d);
else if(M.center.set(a.cx,a.cy,a.cz),M.radius=a.cr*Ma+a.maxRadius,!c(M))return!1;return!0})};var q=function(){};q.prototype.containsPoint=function(c,b,a){var d=this.cx,g=this.cy,h=this.cz,e=this.cr;return c>=d-e&&c<d+e&&b>=g-e&&b<g+e&&a>=h-e&&a<h+e};q.prototype.getSubOctantIndexForPoint=function(c,b,a){return(a<this.cz?0:4)+(b<this.cy?0:2)+(c<this.cx?0:1)};q.prototype.addOctantForPoint=function(c,b,a,d){var g=this.getSubOctantIndexForPoint(b,a,d),h=this.cr/2;c.parent=this;c.index=g;c.cx=this.cx+h*
(b<this.cx?-1:1);c.cy=this.cy+h*(a<this.cy?-1:1);c.cz=this.cz+h*(d<this.cz?-1:1);c.cr=h;return this[g]=c};q.prototype.findMaxSphereRadius=function(){var c=0;if(this.isLeaf){var b=this.data;if(1<this.sphereCount)for(var a in b){var d=b[a].radius;d>c&&(c=d)}else c=b.radius}else for(b=0;8>b;b++)null!==this[b]&&this[b].maxRadius>c&&(c=this[b].maxRadius);return c};q.prototype.updateMaxRadii=function(){var c=this.findMaxSphereRadius();if(c>this.maxRadius)for(var b=this;b;)c>b.maxRadius&&(b.maxRadius=c),
b=b.parent;else c<this.maxRadius&&(this.maxRadius=c,this.parent&&this.parent.updateMaxRadii())};q.prototype.addSphereData=function(c,b){var a=this.sphereCount++;if(0===a)this.leafCount=1,this.data=b,this.dataKey=c,c=b.center,this.dataX=c.x,this.dataY=c.y,this.dataZ=c.z;else if(1===a){a=this.data;var d=this.data=Object.create(null);d[this.dataKey]=a;d[c]=b;this.dataKey=null}else 1<a&&(this.data[c]=b);b.radius>this.maxRadius&&(this.maxRadius=b.radius)};q.prototype.removeSphereData=function(c){var b=
this.data;if(b){var a=this.sphereCount--;if(2<a)delete b[c];else if(2===a)for(var d in b){if(d!==c){this.dataKey=d;this.data=b[d];break}}else this.data=null}};q.prototype.forEachLeafSphere=function(c,b){var a=this.data;if(a)if(1<this.sphereCount)for(var d in a)c.call(b,a[d],d);else c.call(b,a,this.dataKey)};r(q.prototype,{parent:null,index:-1,cx:0,cy:0,cz:0,cr:0,0:null,1:null,2:null,3:null,4:null,5:null,6:null,7:null,isLeaf:!1,data:null,dataKey:null,dataX:0,dataY:0,dataZ:0,sphereCount:0,leafCount:0,
maxRadius:0});var Na=function(){var c="parent index cx cy cz cr sphereCount leafCount maxRadius".split(" ");return function(b){for(var a=new q,d=c.length;d--;)a[c[d]]=b[c[d]];return a}}(),E=l.utils.assign,N=new e.Vector2,z=new e.Vector3,qa=new e.Raycaster,H=function(c){function b(a){c.call(this,a);this._object3DFacadesById=Object.create(null);this._onBgClick=this._onBgClick.bind(this)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){var a=
this.width,b=this.height,g=this.antialias,h=this.backgroundColor,f=this.contextAttributes,l=this._element,k=this._threeRenderer,m=this.rendererClass||e.WebGLRenderer;k&&k instanceof m||(k&&k.dispose(),f=E({alpha:!0,antialias:g},f),(k=l.getContext("webgl2",f)||void 0)||console.info("webgl2 init failed, trying webgl"),k=this._threeRenderer=new m(E({canvas:l,context:k},f)));f=this.shadows;k.shadowMap.enabled=!!f;f&&"object"===typeof f&&E(k.shadowMap,f);h!==this._bgColor&&(this._threeRenderer.setClearColor(new e.Color(h||
0),null!=h?1:0),this._bgColor=h);this._updateDrawingBufferSize(a,b,this.pixelRatio||window.devicePixelRatio||1);c.prototype.afterUpdate.call(this)};b.prototype.describeChildren=function(){return[this._getCameraDef(),this._getSceneDef()]};b.prototype._getCameraDef=function(){return E({key:"camera",facade:da,aspect:this.width/this.height},this.camera)};b.prototype._getSceneDef=function(){return{key:"scene",facade:pa,lights:this.lights,objects:this.objects,fog:this.fog,onClick:this.onBackgroundClick?
this._onBgClick:null}};b.prototype._updateDrawingBufferSize=function(a,b,c){var d=this._threeRenderer;d.getSize(N);N.width===a&&N.height===b&&d.getPixelRatio()===c||d.setDrawingBufferSize(a,b,c)};b.prototype.doRender=function(){function a(a,d){a.call(this._object3DFacadesById[d],h,b,c)}var b=this.getChildByKey("scene").threeObject,c=this.getChildByKey("camera").threeObject,h=this._threeRenderer,e=this.eventRegistry;e.forEachListenerOfType("beforerender",a,this);h.render(b,c);e.forEachListenerOfType("afterrender",
a,this);if(e=this.onStatsUpdate){var f=h.info,k=f.memory;f=f.render;k={"WebGL Draw Calls":f.calls,"WebGL Geometries":k.geometries,"WebGL Textures":k.textures,"WebGL Triangles":f.triangles};f.points&&(k["WebGL Points"]=f.points);f.lines&&(k["WebGL Lines"]=f.lines);e(k)}};b.prototype.getFacadeUserSpaceXYZ=function(a){a=a.threeObject.matrixWorld.elements;return this.projectWorldPosition(a[12],a[13],a[14])};b.prototype.projectWorldPosition=function(a,b,c){z.set(a,b,c);a=this.getChildByKey("camera");a.updateMatrices();
a=a.threeObject;z.applyMatrix4(a.matrixWorldInverse);b=z.length()*(0<z.z?-1:1);z.applyMatrix4(a.projectionMatrix);return new e.Vector3((z.x+1)*this.width/2,(1-z.y)*this.height/2,b)};b.prototype._normalizePointerEvent=function(a){if(!a.ray){var b=a;if(a.touches){var g=/^touch(end|cancel)$/.test(a.type)?a.changedTouches:a.touches;1===g.length&&(b=g[0])}var h=a.target.getBoundingClientRect();g=((b.clientX||0)-(h.left||0))/(h.width||this.width)*2-1;b=((b.clientY||0)-(h.top||0))/(h.height||this.height)*
-2+1;h=this.getChildByKey("camera");h.updateMatrices();a.ray=h.getRayAtProjectedCoords(g,b)}c.prototype._normalizePointerEvent.call(this,a)};b.prototype.getFacadesAtEvent=function(a,b){return a.ray?this.getFacadesOnRay(a.ray,b):null};b.prototype.getFacadesOnRay=function(a,b){var c=this,d=this._updateOctree(),e=null;d&&(qa.ray=a,d.forEachSphereOnRay(a,function(a,d){(a=(d=(a=c._object3DFacadesById)&&a[d])&&(!b||b(d))&&d.raycast&&d.raycast(qa))&&a[0]&&(a[0].facade=d,(e||(e=[])).push(a[0]))}));return e};
b.prototype._updateOctree=function(){var a=this._boundingSphereOctree,b=this._octreeChangeset;if(b){a||(a=this._boundingSphereOctree=new m);var c=b.remove;b=b.put;if(c)for(var e in c)a.removeSphere(e);if(b)for(var f in b)e=this._object3DFacadesById[f],!e||e.isDestroying||c&&c[f]||((e=e.getBoundingSphere&&e.getBoundingSphere())?a.putSphere(f,e):a.removeSphere(f));this._octreeChangeset=null}return a};b.prototype._queueForOctreeChange=function(a,b){var c=this._octreeChangeset||(this._octreeChangeset=
{});(c[a]||(c[a]=Object.create(null)))[b.$facadeId]=b};b.prototype._onBgClick=function(a){if(a.target===a.currentTarget)this.onBackgroundClick(a)};b.prototype.destructor=function(){c.prototype.destructor.call(this);this._threeRenderer.dispose();this._threeRenderer.forceContextLoss()};return b}(l.WorldBaseFacade);H.prototype._notifyWorldHandlers=E(Object.create(l.WorldBaseFacade.prototype._notifyWorldHandlers),{getCameraPosition:function(c,b){b.setFromMatrixPosition(this.getChildByKey("camera").threeObject.matrixWorld)},
getCameraFacade:function(c,b){b.callback(this.getChildByKey("camera"))},getSceneFacade:function(c,b){b.callback(this.getChildByKey("scene"))},projectWorldPosition:function(c,b){c=b.worldPosition;b.callback(this.projectWorldPosition(c.x,c.y,c.z))},object3DAdded:function(c){this._object3DFacadesById[c.$facadeId]=c;this._queueForOctreeChange("put",c)},object3DBoundsChanged:function(c){this._queueForOctreeChange("put",c)},object3DRemoved:function(c){delete this._object3DFacadesById[c.$facadeId];this._queueForOctreeChange("remove",
c)},rayPointerMotion:function(c,b){var a=new MouseEvent("mousemove");a.isRayEvent=!0;a.ray=b;a.eventSource=c;this._onPointerMotionEvent(a)},rayPointerAction:function(c,b){var a=new ("wheel"===b.type?WheelEvent:MouseEvent)(b.type,b);a.isRayEvent=!0;a.ray=b.ray;a.eventSource=c;this._onPointerActionEvent(a)}});var Oa="onMouseOver onMouseOut onMouseMove onMouseDown onMouseUp onClick onDoubleClick".split(" ");r=function(c){function b(a){var b=new e.Object3D;b.isRenderable=!1;b.$troikaVisible=b.visible;
Object.defineProperty(b,"visible",Pa);c.call(this,a,b);this.notifyWorld("instanceableAdded")}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={instancedThreeObject:{configurable:!0}};a.instancedThreeObject.set=function(a){a!==this._instancedThreeObject&&(this._instancedThreeObject=a,this.notifyWorld("instanceableChanged"),this._boundsChanged=!0)};a.instancedThreeObject.get=function(){return this._instancedThreeObject};b.prototype.setInstanceUniform=function(a,
b){var c=this._instanceUniforms||(this._instanceUniforms=Object.create(null));c[a]!==b&&(c[a]=b,this.notifyWorld("instanceableUniformChanged",a))};b.prototype.updateMatrices=function(){var a=this._worldMatrixVersion;c.prototype.updateMatrices.call(this);this._worldMatrixVersion!==a&&this.threeObject.$troikaVisible&&this.notifyWorld("instanceableMatrixChanged")};b.prototype.destructor=function(){this.notifyWorld("instanceableRemoved");c.prototype.destructor.call(this)};b.prototype.getGeometry=function(){var a=
this.instancedThreeObject;return a&&a.geometry};b.prototype.raycast=function(a){var b=this.instancedThreeObject,c=this.threeObject;if(b&&c){var d=b.matrixWorld;b.matrixWorld=c.matrixWorld;a=this._raycastObject(b,a);b.matrixWorld=d;return a}return null};Object.defineProperties(b.prototype,a);return b}(n);var Pa={set:function(c){c!==this.$troikaVisible&&(this.$troikaVisible=c,this.$facade.notifyWorld("instanceableChanged"))},get:function(){return this.$troikaVisible}};l.utils.assign(r.prototype,{_lastInstancedMatrixVersion:-1,
_instancedThreeObject:null});var O=new e.BufferGeometry,Qa=new e.MeshBasicMaterial,P={basic:e.MeshBasicMaterial,depth:e.MeshDepthMaterial,distance:e.MeshDistanceMaterial,lambert:e.MeshLambertMaterial,matcap:e.MeshMatcapMaterial,normal:e.MeshNormalMaterial,phong:e.MeshPhongMaterial,physical:e.MeshPhysicalMaterial,standard:e.MeshStandardMaterial,toon:e.MeshToonMaterial},A=function(c){function b(a){c.call(this,a,new e.Mesh(O,Qa));this.material="standard";this.autoDisposeMaterial=this.autoDisposeGeometry=
!1;this._dirtyMtlProps=null}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){var a=this.geometry,b=this.material,g=this.threeObject;(a||O)!==g.geometry&&(this.autoDisposeGeometry&&g.geometry.dispose(),g.geometry=a||O);b!==this._lastMtl&&(this._lastMtl=b,"string"===typeof b?b=new (P[b]||e.MeshStandardMaterial):b&&b.isMaterial||(b="function"===typeof b?new b:new e.MeshStandardMaterial),g.material!==b&&(this.autoDisposeMaterial&&
g.material.dispose(),g.material=b));if(a=this._dirtyMtlProps)g.material.setValues(a),this._dirtyMtlProps=null;c.prototype.afterUpdate.call(this)};b.prototype.destructor=function(){this.autoDisposeGeometry&&this.threeObject.geometry.dispose();this.autoDisposeMaterial&&this.threeObject.material.dispose();c.prototype.destructor.call(this)};return b}(n),Ra={type:1,id:1,uuid:1,version:1};Object.keys(P).forEach(function(c){var b=new P[c];c=function(a){b.hasOwnProperty(a)&&!Ra.hasOwnProperty(a)&&Object.defineProperty(A.prototype,
"material."+a,{enumerable:!0,configurable:!0,get:function(){var b=this._dirtyMtlProps;return b&&a in b?b[a]:this.threeObject.material[a]},set:function(b){(this._dirtyMtlProps||(this._dirtyMtlProps=Object.create(null)))[a]=b}})};for(var a in b)c(a)});var ra=l.utils.memoize(function(){return new e.BoxBufferGeometry(1,1,1,1,1)}),Sa=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},
width:{configurable:!0},height:{configurable:!0},depth:{configurable:!0}};a.geometry.get=function(){return ra()};a.width.set=function(a){this.scaleX=a};a.width.get=function(){return this.scaleX};a.height.set=function(a){this.scaleY=a};a.height.get=function(){return this.scaleY};a.depth.set=function(a){this.scaleZ=a};a.depth.get=function(){return this.scaleZ};Object.defineProperties(b.prototype,a);return b}(A),sa=Object.create(null,[["low",32],["medium",64],["high",128]].reduce(function(c,b){var a=
b[1];c[b[0]]={get:l.utils.memoize(function(){return(new e.CircleBufferGeometry(1,a)).rotateX(-Math.PI/2)})};return c},{})),Ta=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},radius:{configurable:!0}};a.geometry.get=function(){return sa[this.detail]||sa.medium};a.radius.set=function(a){this.scaleX=this.scaleZ=a};a.radius.get=function(){return this.scaleX};Object.defineProperties(b.prototype,
a);return b}(A),Ua=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},size:{configurable:!0}};a.geometry.get=function(){return ra()};a.size.set=function(a){this.scale=a};a.size.get=function(){return this.scale};Object.defineProperties(b.prototype,a);return b}(A),Va=l.utils.memoize(function(){return(new e.PlaneBufferGeometry(1,1,1,1)).rotateX(-Math.PI/2)}),Wa=function(c){function b(){c.apply(this,
arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},width:{configurable:!0},depth:{configurable:!0}};a.geometry.get=function(){return Va()};a.width.set=function(a){this.scaleX=a};a.width.get=function(){return this.scaleX};a.depth.set=function(a){this.scaleZ=a};a.depth.get=function(){return this.scaleZ};Object.defineProperties(b.prototype,a);return b}(A),ta=Object.create(null,[["low",16,12],["medium",32,24],["high",64,
48]].reduce(function(c,b){var a=b[1],d=b[2];c[b[0]]={get:l.utils.memoize(function(){return new e.SphereBufferGeometry(1,a,d)})};return c},{})),Xa=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},radius:{configurable:!0}};a.geometry.get=function(){return ta[this.detail]||ta.medium};a.radius.set=function(a){this.scale=a};a.radius.get=function(){return this.scale};Object.defineProperties(b.prototype,
a);return b}(A),I=function(c){function b(a){var b=this;c.call(this,a);this._onCanvasRef=function(a){var c=b.context.onCanvasRef;c&&c(a);(c=b.props.onCanvasRef)&&c(a)}}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.render=function(){var a=this.props,b=this.context;return x.createElement(c,l.utils.assign({},a,{onCanvasRef:this._onCanvasRef,canvasStyle:a.canvasStyle||b.canvasStyle,worldFacade:a.worldFacade||b.worldFacade||H,worldProps:l.utils.assign({antialias:a.antialias,
rendererClass:a.rendererClass,backgroundColor:a.backgroundColor,shadows:a.shadows,camera:a.camera,lights:a.lights,objects:a.objects,fog:a.fog,onBackgroundClick:a.onBackgroundClick},b.worldProps,a.worldProps)}),a.children)};return b}(l.ReactCanvasBase);I.displayName="Canvas3D";I.propTypes=l.utils.assignIf({backgroundColor:p.any,lights:p.array,camera:p.object,objects:p.oneOfType([p.array,p.object]).isRequired,antialias:p.bool,onBackgroundClick:p.func,rendererClass:p.func},l.ReactCanvasBase.commonPropTypes);
I.contextType=x.createContext({worldFacade:H,worldProps:{},onCanvasRef:null,canvasStyle:null});Object.defineProperty(f,"Facade",{enumerable:!0,get:function(){return l.Facade}});Object.defineProperty(f,"ListFacade",{enumerable:!0,get:function(){return l.ListFacade}});Object.defineProperty(f,"ParentFacade",{enumerable:!0,get:function(){return l.ParentFacade}});Object.defineProperty(f,"createDerivedMaterial",{enumerable:!0,get:function(){return t.createDerivedMaterial}});f.AmbientLight3DFacade=fa;f.BoxFacade=
Sa;f.Canvas3D=I;f.CircleFacade=Ta;f.CubeFacade=Ua;f.DirectionalLight3DFacade=ha;f.Group3DFacade=ea;f.HemisphereLight3DFacade=ka;f.HtmlOverlay3DFacade=Ea;f.Instanceable3DFacade=r;f.InstancingManager=L;f.MeshFacade=A;f.Object3DFacade=n;f.OrthographicCamera3DFacade=K;f.PerspectiveCamera3DFacade=da;f.PlaneFacade=Wa;f.PointLight3DFacade=ja;f.RectAreaLight3DFacade=Fa;f.Scene3DFacade=pa;f.SphereFacade=Xa;f.SpotLight3DFacade=ia;f.World3DFacade=H;f.makeWorldTextureProvider=function(c){return function(b){function a(a){var c=
this,d=new e.CanvasTexture;b.call(this,a,d);this.worldTexture=d;var f=this._refireAsInnerEvent.bind(this);Oa.forEach(function(a){function b(a){f(a);d&&d.call(this,a)}var d;c[a]=b;Object.defineProperty(c,a,{set:function(a){d=a},get:function(){return b}})})}b&&(a.__proto__=b);a.prototype=Object.create(b&&b.prototype);a.prototype.constructor=a;a.prototype.afterUpdate=function(){var a=this,c=this._worldFacade,e=this.textureWorld;c&&e&&c instanceof e.facade||(c&&(c.onAfterRender=null,c.destructor()),e&&
(this.worldTexture.dispose(),c=document.createElement("canvas"),c.width=c.height=2,this.worldTexture.image=c,c=this._worldFacade=new e.facade(c),c.onAfterRender=function(){a.worldTexture.needsUpdate=!0;a.notifyWorld("needsRender")}));c&&(c.renderingScheduler=this._getOuterWorld().renderingScheduler,l.utils.assign(c,e),c.afterUpdate());b.prototype.afterUpdate.call(this)};a.prototype._refireAsInnerEvent=function(a){var b=this._worldFacade;if(b){var c=a.intersection&&a.intersection.uv,d=c?Math.round(c.x*
b.width):-1;b=c?Math.round((1-c.y)*b.height):-1;a=a.nativeEvent||a;c=document.createEvent("MouseEvents");c.initMouseEvent(a.type,!0,!0,window,a.detail,d,b,d,b,a.ctrlKey,a.altKey,a.shiftKey,a.metaKey,a.button,null);this.worldTexture.image.dispatchEvent(c)}};a.prototype._getOuterWorld=function(){for(var a=this;a&&!a.isWorld;)a=a.parent;return a};a.prototype.destructor=function(){var a=this._worldFacade;a&&(a.onAfterRender=null,a.destructor());this.worldTexture.dispose();b.prototype.destructor.call(this)};
return a}(c)};Object.defineProperty(f,"__esModule",{value:!0})});
function(a,d){var b=V.ray=new e.Ray;V.setFromCamera(xa.set(a,d),this.threeObject);return b};return d}(p);b.forEach(function(a){d(a,!0)});a&&a.forEach(function(a){d(a,!1)});return g}function A(c,b,a){var d=function(a){function d(d){a.call(this,d,new c)}a&&(d.__proto__=a);d.prototype=Object.create(a&&a.prototype);d.prototype.constructor=d;var g={showHelper:{configurable:!0}};g.showHelper.set=function(a){var d=this._helper;!!a!==!!d&&(a?this.threeObject.add(this._helper=new b(this.threeObject)):d&&(d.dispose(),
this.threeObject.remove(d),this._helper=null))};d.prototype.afterUpdate=function(){a.prototype.afterUpdate.call(this);this._helper&&this._helper.update()};Object.defineProperties(d.prototype,g);return d}(W);a&&Object.defineProperties(d.prototype,a);return d}function ya(c){var b=c.instanceUniforms,a=b?b.sort().join("|"):"",d=X.get(c);d&&d._instanceUniformsKey===a||(d=z.createDerivedMaterial(c,{defines:{TROIKA_INSTANCED_UNIFORMS:a},customRewriter:function(a){return za(a.vertexShader,a.fragmentShader,
b)}}),d._instanceUniformsKey=a,X.set(c,d));return d}function za(c,b,a){void 0===a&&(a=[]);var d=Y.test(c),g=Z.test(c),h=aa.test(c),w=z.getShaderUniformTypes(c),e=z.getShaderUniformTypes(b),k=["\nattribute vec4 troika_modelMatrixRow0;\nattribute vec4 troika_modelMatrixRow1;\nattribute vec4 troika_modelMatrixRow2;\nmat4 troika_modelMatrix;\nmat4 troika_modelViewMatrix;\nmat3 troika_normalMatrix;\n"],C=[],f=[];if(d||g||h)c=c.replace(Y,F),C.push(Aa);if(g||h)c=c.replace(Z,F),C.push("\ntroika_modelViewMatrix = viewMatrix * troika_modelMatrix;\n");
h&&(c=c.replace(aa,F),C.push("\ntroika_normalMatrix = transposeMat3(inverse(mat3(troika_modelViewMatrix)));\n"),/\binverse\s*\(/.test(c)||k.push("\n#if __VERSION__ < 300\n// matrix inversion utility for pre-ES3 - credit https://github.com/stackgl/glsl-inverse\nmat3 inverse(mat3 m) {\n float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];\n float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];\n float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];\n\n float b01 = a22 * a11 - a12 * a21;\n float b11 = -a22 * a10 + a12 * a20;\n float b21 = a21 * a10 - a11 * a20;\n\n float det = a00 * b01 + a01 * b11 + a02 * b21;\n\n return mat3(\n b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),\n b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),\n b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)\n ) / det;\n}\n#endif\n"));
a.forEach(function(a){var d=w[a],g=e[a];if(d||g){var h=new RegExp("\\b"+a+"\\b","g");k.push("attribute "+(d||g)+" troika_"+a+";");d&&(c=c.replace(h,F));g&&(b=b.replace(h,Ba),d="varying "+g+" troika_vary_"+a+";",k.push(d),f.push(d),C.push("troika_vary_"+a+" = troika_"+a+";"))}});c="\n"+k.join("\n")+"\n"+c.replace(z.voidMainRegExp,"\n $&\n "+C.join("\n")+"\n");f.length&&(b="\n"+f.join("\n")+"\n"+b);return{vertexShader:c,fragmentShader:b}}function ba(c,b,a){var d=c.itemSize;1===d?c.setX(b,a):2===d?
c.setXY(b,a.x,a.y):3===d?a.isColor?c.setXYZ(b,a.r,a.g,a.b):c.setXYZ(b,a.x,a.y,a.z):4===d&&c.setXYZW(b,a.x,a.y,a.z,a.w)}function ca(c,b){var a=c.uniforms;return a&&a[b]?a[b].value||null:(a=z.getShadersForMaterial(c).uniforms)&&a[b]?a[b].value||null:null}v=v&&Object.prototype.hasOwnProperty.call(v,"default")?v["default"]:v;m=m&&Object.prototype.hasOwnProperty.call(m,"default")?m["default"]:m;var J=l.utils.assign,da=l.utils.forOwn,ea=new e.Vector3,Ca=new e.Vector3,u=function(){var c={callback:function(b){c.value=
b},value:null};return c}(),Da={type:"removed"},D=[],Ea=0,Fa=0,p=function(c){function b(a,d){c.call(this,a);d.matrixAutoUpdate=!1;this.threeObject=d;d.$facade=this;var b=!1!==d.isRenderable;b||(d.layers.mask=0);for(;a;){if(a.isObject3DFacade){this._parentObject3DFacade=a;b&&this._addToThreeObjectTree();break}a=a.parent}this.notifyWorld("object3DAdded")}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){this.updateMatrices();this._checkBoundsChange();
this._worldMatrixVersion>this._worldMatrixVersionAfterLastUpdate&&(this.shouldUpdateChildren()||this.traverse(function(a,d){a!==d&&a.updateMatrices&&(a.updateMatrices(),a._checkBoundsChange())},this),this._worldMatrixVersionAfterLastUpdate=this._worldMatrixVersion);c.prototype.afterUpdate.call(this);if(this._removeChildIds){var a=this.threeObject,d=this._removeChildIds;a.children=a.children.filter(function(a){return a.id in d?(a.parent=null,a.dispatchEvent(Da),!1):!0});var b=this._parentObject3DFacade;
Q(a)&&b&&b.threeObject===a.parent&&b._queueRemoveChildObject3D(a.id);this._removeChildIds=null}};b.prototype.updateMatrices=function(){var a=this.threeObject,d=this._parentObject3DFacade;if(this._matrixChanged){a.matrix.compose(a.position,a.quaternion,a.scale);this._matrixChanged=!1;var b=!0}else b=d&&d._worldMatrixVersion>this._worldMatrixVersion;if(b){d?a.matrixWorld.multiplyMatrices(d.threeObject.matrixWorld,a.matrix):a.matrixWorld.copy(a.matrix);a=a.children;d=0;for(b=a.length;d<b;d++)a[d].$facade||
a[d].updateMatrixWorld(!0);this.markWorldMatrixDirty()}};b.prototype.markWorldMatrixDirty=function(){this._worldMatrixVersion=++Ea;this._boundsChanged=!0};b.prototype._checkBoundsChange=function(){var a=this._boundsChanged;if(!a){var d=this._getGeometryBoundingSphere();d&&d.version!==this._lastGeometrySphereVersion&&(a=!0,this._lastGeometrySphereVersion=d.version)}a&&(this.notifyWorld("object3DBoundsChanged"),this._boundsChanged=!1)};b.prototype.getWorldPosition=function(a){this.updateMatrices();
return(a||new e.Vector3).setFromMatrixPosition(this.threeObject.matrixWorld)};b.prototype.getCameraPosition=function(a){a=a||new e.Vector3;this.notifyWorld("getCameraPosition",a);return a};b.prototype.getCameraFacade=function(){u.value=null;this.notifyWorld("getCameraFacade",u);return u.value};b.prototype.getCameraDistance=function(){var a=this.getCameraPosition(Ca),d=this.getWorldPosition(ea);return a.distanceTo(d)};b.prototype.getProjectedPosition=function(a,d,b){this.updateMatrices();u.value=null;
u.worldPosition=ea.set(a||0,d||0,b||0).applyMatrix4(this.threeObject.matrixWorld);this.notifyWorld("projectWorldPosition",u);return u.value};b.prototype.getSceneFacade=function(){u.value=null;this.notifyWorld("getSceneFacade",u);return u.value};b.prototype.getBoundingSphere=function(){var a=this._getGeometryBoundingSphere();if(!a)return null;this.updateMatrices();var d=this._boundingSphere;d||(d=this._boundingSphere=new e.Sphere);if(d._geometrySphereVersion!==a.version||d._worldMatrixVersion!==this._worldMatrixVersion)d.copy(a),
d.applyMatrix4(this.threeObject.matrixWorld),d._worldMatrixVersion=this._worldMatrixVersion,d._geometrySphereVersion=a.version;return d};b.prototype._getGeometryBoundingSphere=function(){var a=this.getGeometry();if(a){var d=a.boundingSphere,b=!1;if(d)if(a.isBufferGeometry){var c=a.attributes.position;c&&d._posAttrVersion!==c.version&&(a.computeBoundingSphere(),d._posAttrVersion=c.version,b=!0)}else a._lastBoundingSphere&&d.equals(a._lastBoundingSphere)||(a._lastBoundingSphere=d.clone(),b=!0);else a.computeBoundingSphere(),
d=a.boundingSphere,b=!0;b&&(d.version=++Fa);return d}return null};b.prototype.getGeometry=function(){var a=this.threeObject;return a&&a.geometry};b.prototype.raycast=function(a){return this.threeObject?this._raycastObject(this.threeObject,a):null};b.prototype._raycastObject=function(a,d){if(a.visible){D.length=0;var b=null,c=this.raycastSide;null!=c&&(b=a.material.side,a.material.side=c);a.raycast(d,D);null!==b&&(a.material.side=b);if(D.length)return D.sort(ua),D.slice()}return null};b.prototype._addToThreeObjectTree=
function(){var a=this._parentObject3DFacade;a&&this.threeObject.parent!==a.threeObject&&(a.threeObject.add(this.threeObject),a._addToThreeObjectTree())};b.prototype._queueRemoveChildObject3D=function(a){(this._removeChildIds||(this._removeChildIds=Object.create(null)))[a]=!0};b.prototype.destructor=function(){this.notifyWorld("object3DRemoved");var a=this._parentObject3DFacade;a&&a._queueRemoveChildObject3D(this.threeObject.id);delete this.threeObject;c.prototype.destructor.call(this)};return b}(l.PointerEventTarget);
["castShadow","receiveShadow","renderOrder","visible"].forEach(function(c){Object.defineProperty(p.prototype,c,{get:function(){return this.threeObject[c]},set:function(b){this.threeObject[c]=b}})});p.prototype.raycastSide=null;da({position:{x:"x",y:"y",z:"z"},scale:{x:"scaleX",y:"scaleY",z:"scaleZ"},rotation:{x:"rotateX",y:"rotateY",z:"rotateZ",order:"rotateOrder"},quaternion:{x:"quaternionX",y:"quaternionY",z:"quaternionZ",w:"quaternionW"}},function(c,b){da(c,function(a,d){Object.defineProperty(p.prototype,
a,{get:(new Function("return function "+a+"$get() {\n return this.threeObject."+b+"."+d+"\n}"))(),set:(new Function("return function "+a+"$set(value) {\n //let obj = this.threeObject."+b+"\n if (this.threeObject."+b+"."+d+" !== value) {\n this.threeObject."+b+"."+d+" = value\n if (!this._matrixChanged) {\n this._matrixChanged = true\n }\n }\n}"))()})})});Object.defineProperty(p.prototype,"scale",{get:function(){return this.threeObject.scale.x},set:function(c){var b=this.threeObject.scale;
if(c!==b.x||c!==b.y||c!==b.z)b.x=b.y=b.z=c,this._matrixChanged||(this._matrixChanged=!0)}});Object.defineProperty(p.prototype,"isObject3DFacade",{value:!0});J(p.prototype,{threeObject:null,_parentObject3DFacade:null,_removeChildIds:null,_matrixChanged:!0,_worldMatrixVersion:-1,_worldMatrixVersionAfterLastUpdate:-1,_boundingSphereChanged:!1});l.Facade.defineEventProperty(p,"onBeforeRender","beforerender");l.Facade.defineEventProperty(p,"onAfterRender","afterrender");var va=function(){},V=new e.Raycaster,
xa=new e.Vector2,S=new e.Vector3,U=new e.Matrix4,B=new e.Quaternion,T=new e.Vector3(0,1,0),wa=0,fa=R(e.PerspectiveCamera,["fov","aspect","near","far"],["focus","filmGauge","filmOffset"]);J=R(e.OrthographicCamera,"left right top bottom near far".split(" "));var ha=function(c){function b(a){var d=new e.Group;d.isRenderable=!1;c.call(this,a,d)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);return b.prototype.constructor=b}(p),Ga=function(c){function b(a){var d=new e.Object3D;d.isRenderable=
!1;c.call(this,a,d);this.html=null;this.exact=!1;this.notifyWorld("addHtmlOverlay",this)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.destructor=function(){this.notifyWorld("removeHtmlOverlay",this);c.prototype.destructor.call(this)};return b}(p),W=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={color:{configurable:!0},shadow:{configurable:!0}};a.color.set=
function(a){this.threeObject.color.set(a)};a.color.get=function(){return this.threeObject.color.getHex()};a.shadow.get=function(){return this.threeObject.shadow};a.shadow.set=function(a){l.utils.assignDeep(this.threeObject.shadow,a)};Object.defineProperties(b.prototype,a);return b}(p);"intensity distance angle penumbra decay castShadow width height".split(" ").forEach(function(c){Object.defineProperty(W.prototype,c,{get:function(){return this.threeObject[c]},set:function(b){this.threeObject[c]=b}})});
var ia=A(e.AmbientLight),ja=A(e.DirectionalLight,e.DirectionalLightHelper),ka=A(e.SpotLight,e.SpotLightHelper),la=A(e.PointLight,e.PointLightHelper),ma=A(e.HemisphereLight,e.HemisphereLightHelper,{groundColor:{set:function(c){this.threeObject.groundColor.set(c)},get:function(){return this.threeObject.groundColor.getHex()}}}),Ha=A(e.RectAreaLight),Aa="\ntroika_modelMatrix = mat4(\n %0.x, %1.x, %2.x, 0.0,\n %0.y, %1.y, %2.y, 0.0,\n %0.z, %1.z, %2.z, 0.0,\n %0.w, %1.w, %2.w, 1.0\n);\n".replace(/%/g,
"troika_modelMatrixRow"),Y=/\bmodelMatrix\b/g,Z=/\bmodelViewMatrix\b/g,aa=/\bnormalMatrix\b/g,na=/\buniform\s+(int|float|vec[234])\s+$/,F=function(c,b,a){return na.test(a.substr(0,b))?c:"troika_"+c},Ba=function(c,b,a){return na.test(a.substr(0,b))?c:"troika_vary_"+c},X=new WeakMap,K=l.utils.assign,L=function(c){function b(a){c.call(this,a);this._instanceables=Object.create(null);this._batchGeometryPool=new G;this._needsRebatch=!0;this.addEventListener("beforerender",this._setupBatchObjects.bind(this));
this.addEventListener("afterrender",this._teardownBatchObjects.bind(this))}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.onNotifyWorld=function(a,d,b){var c=this._notifyWorldHandlers[d];if(c)c.call(this,a,b);else if(this.parent)this.parent.onNotifyWorld(a,d,b)};b.prototype._setupBatchObjects=function(a,d,b){b=this._instanceables;a=this._batchObjectsByKey;var c=this._needsRebatch;if(!c)for(var g in a)if(this._getBatchKey(a[g][0].$troikaBatchBaseObj)!==
g){c=!0;break}if(c){a=this._batchObjectsByKey=Object.create(null);g=this._batchGeometryPool;for(var e in b){c=b[e];var k=c.threeObject,f=c.instancedThreeObject;if(f&&k.visible){var l=this._getBatchKey(f),p=f.material.instanceUniforms,m=a[l]||(a[l]=[]),n=(l=m[m.length-1])&&l.geometry;if(!n||128===n.maxInstancedCount){l=this._getBatchObject(f);n=l.geometry;for(var q=n._instanceAttrs.matrix,t=0;3>t;t++)q[t].version++;if(p)for(q=n._instanceAttrs.uniforms,t=p.length;t--;)q[p[t]].version++;m.push(l)}m=
n.maxInstancedCount++;q=n._instanceAttrs.matrix;k=k.matrixWorld.elements;q[0].setXYZW(m,k[0],k[4],k[8],k[12]);q[1].setXYZW(m,k[1],k[5],k[9],k[13]);q[2].setXYZW(m,k[2],k[6],k[10],k[14]);if(p)for(q=n._instanceAttrs.uniforms,k=p.length;k--;){t=p[k];n=q[t];var r=c._instanceUniforms;t=r&&t in r?r[t]:ca(f.material,t);ba(n,m,t)}c._instancingBatchObject=l;c._instancingBatchAttrOffset=m}else c._instancingBatchObject=c._instancingBatchAttrOffset=null}g.disposeUnused()}g=b=e=0;for(var u in a)for(c=a[u],d.children.push.apply(d.children,
c),b++,f=c.length;f--;)e++,g+=c[f].geometry.maxInstancedCount;this.notifyWorld("statsUpdate",{"Instancing Batch Groups":b,"Instancing Batches":e,"Instanced Objects":g});this._needsRebatch=!1};b.prototype._onInstanceAdded=function(a){this._instanceables[a.$facadeId]=a;this._needsRebatch=!0};b.prototype._onInstanceRemoved=function(a){delete this._instanceables[a.$facadeId];this._needsRebatch=!0};b.prototype._onInstanceChanged=function(a){this._needsRebatch=!0};b.prototype._onInstanceMatrixChanged=function(a){if(!this._needsRebatch){var d=
a.instancedThreeObject,b=a._instancingBatchObject,c=a._instancingBatchAttrOffset;d&&b&&this._getBatchKey(d)===this._getBatchKey(b)?(d=b.geometry._instanceAttrs.matrix,a=a.threeObject.matrixWorld.elements,d[0].setXYZW(c,a[0],a[4],a[8],a[12]).version++,d[1].setXYZW(c,a[1],a[5],a[9],a[13]).version++,d[2].setXYZW(c,a[2],a[6],a[10],a[14]).version++):(a._instancingBatchObject=a._instancingBatchAttrOffset=null,this._needsRebatch=!0)}};b.prototype._onInstanceUniformChanged=function(a,d){if(!this._needsRebatch){var b=
a.instancedThreeObject,c=a._instancingBatchObject;b&&c&&this._getBatchKey(b)===this._getBatchKey(c)?(b=c.geometry._instanceAttrs.uniforms[d],ba(b,a._instancingBatchAttrOffset,a._instanceUniforms[d]),b.version++):(a._instancingBatchObject=a._instancingBatchAttrOffset=null,this._needsRebatch=!0)}};b.prototype._getBatchKey=function(a){var b=this._batchKeysCache||(this._batchKeysCache=Object.create(null)),c=b&&b[a.id];if(!c){c=a.material;var h=c.instanceUniforms;c=a.geometry.id+"|"+c.id+"|"+(h?h.sort().join(","):
"");b[a.id]=c}return c};b.prototype._getInstanceUniformSizes=function(a){var b=this._uniformSizesCache||(this._uniformSizesCache=new Map),c=b.get(a);c||(c=Object.create(null),a.instanceUniforms&&a.instanceUniforms.forEach(function(b){var d=ca(a,b);d=null==d?0:"number"===typeof d?1:d.isVector2?2:d.isVector3||d.isColor?3:d.isVector4?4:Array.isArray(d)?d.length:0;0<d?c[b]=d:console.warn("Could not determine item size for uniform "+b)}),b.set(a,c));return c};b.prototype._getBatchObject=function(a){var b=
a.geometry,c=a.material;if(!b.isBufferGeometry)throw Error("Instanceable proto object must use a BufferGeometry");var h=this._getBatchKey(a),w=this._getInstanceUniformSizes(c);b=this._batchGeometryPool.borrow(h,b,w);b.maxInstancedCount=0;var f=ya(c),k,l;c=Object.create(a,{geometry:{value:b},material:{value:f},visible:{value:!0},frustumCulled:{value:!1},customDepthMaterial:{get:function(){k||(k=f.getDepthMaterial(),k.isShaderMaterial=!0);return k}},customDistanceMaterial:{get:function(){l||(l=f.getDistanceMaterial(),
l.isShaderMaterial=!0,l.uniforms=K({viewMatrix:{value:new e.Matrix4}},l.uniforms));return l}},modelViewMatrix:{value:function(){var a=new e.Matrix4;a.multiplyMatrices=function(a,b){l&&(l.uniforms.viewMatrix.value.copy(a),l.uniformsNeedUpdate=!0);return e.Matrix4.prototype.multiplyMatrices.call(this,a,b)};return a}()}});c.$troikaBatchBaseObj=a;c.$troikaInstancingManager=this;return c};b.prototype._teardownBatchObjects=function(a,b,c){var d=this;this._batchGeometryPool.releaseAll();this._uniformSizesCache=
this._batchKeysCache=null;b.children=b.children.filter(function(a){return a.$troikaInstancingManager!==d})};b.prototype.destructor=function(){var a=this._batchGeometryPool;a.releaseAll();a.disposeUnused();c.prototype.destructor.call(this)};return b}(ha),G=function(){this._poolsByKey=Object.create(null)};G.prototype.borrow=function(c,b,a){var d=this._poolsByKey;c=d[c]||(d[c]={geometries:[],firstFree:0});d=c.geometries[c.firstFree++];if(!d){d=new e.InstancedBufferGeometry;K(d,b);d.attributes=K({},b.attributes);
b=d._instanceAttrs={matrix:[],uniforms:Object.create(null)};for(var g=0;3>g;g++){var h=new e.InstancedBufferAttribute(new Float32Array(512),4);h.setUsage?h.setUsage(35048):h.dynamic=!0;d.attributes["troika_modelMatrixRow"+g]=h;b.matrix[g]=h}for(var w in a)g=a[w],g=new e.InstancedBufferAttribute(new Float32Array(128*g),g),g.setUsage?g.setUsage(35048):g.dynamic=!0,d.attributes["troika_"+w]=g,b.uniforms[w]=g;c.geometries.push(d)}return d};G.prototype.releaseAll=function(){var c=this._poolsByKey;if(c)for(var b in c)c[b].firstFree=
0};G.prototype.disposeUnused=function(){var c=this._poolsByKey;if(c)for(var b in c){var a=c[b],d=a.firstFree;a=a.geometries;for(var g=d,h=a.length;g<h;g++){var e=a[g].attributes,f;for(f in e)e.hasOwnProperty(f)&&0!==f.indexOf("troika_")&&delete e[f];try{a[g].dispose()}catch(k){}a[g]._instanceAttrs=null}a.length=d}};var r=L.prototype;r._notifyWorldHandlers={instanceableAdded:r._onInstanceAdded,instanceableRemoved:r._onInstanceRemoved,instanceableChanged:r._onInstanceChanged,instanceableMatrixChanged:r._onInstanceMatrixChanged,
instanceableUniformChanged:r._onInstanceUniformChanged};var Ia={ambient:ia,directional:ja,spot:ka,point:la,hemisphere:ma},Ja=[{distance:Infinity}],Ka=new e.Sphere(void 0,Infinity),oa=[null],pa=function(c){function b(a){var b=new e.Scene;b.autoUpdate=!1;c.call(this,a,b)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={fog:{configurable:!0}};b.prototype.describeChildren=function(){var a={key:"instancingMgr",facade:L,children:this.objects},b=this.lights;b&&
(a=[a],Array.isArray(b)||(oa[0]=b,b=oa),b.forEach(function(b,d){var c=b.facade||Ia[b.type];if("function"===typeof c){var g=l.utils.assign({},b);delete g.type;g.key=b.key||"light"+d;g.facade=c;a.push(g)}}));return a};a.fog.set=function(a){var b=this._fogObj;if(a){var d="density"in a,c=d?e.FogExp2:e.Fog;b&&b instanceof c||(b=this._fogObj=new c);b.color.set(a.color);d?b.density=a.density:(b.near=a.near,b.far=a.far)}else b=this._fogObj=null;this.threeObject.fog=b};b.prototype.getBoundingSphere=function(){return Ka};
b.prototype.raycast=function(a){return Ja};Object.defineProperties(b.prototype,a);return b}(p);r=l.utils.assign;var La=l.utils.forOwn,M=new e.Sphere,Ma=Math.sqrt(3),q=function(){this.root=null;this.keysToLeaves=Object.create(null)};q.prototype.putSpheres=function(c){var b=this;La(c,function(a,d){b.putSphere(d,a)})};q.prototype.putSphere=function(c,b){var a=this,d=b.center,g=b.radius,h=this.root;if(!b||isNaN(g)||isNaN(d.x))console.warn("Invalid sphere",b);else{if(c in this.keysToLeaves)return this._updateSphere(c,
b);if(h)if(h.isLeaf){g=this.root;var e=h.dataX,f=h.dataY,k=h.dataZ;if(e===d.x&&f===d.y&&k===d.z)this._insertIntoOctant(c,b,h);else{var l=new n;h=l.cx=Math.round((e+d.x)/2);var m=l.cy=Math.round((f+d.y)/2);d=l.cz=Math.round((k+d.z)/2);l.cr=Math.ceil(Math.max(Math.abs(h-e),Math.abs(m-f),Math.abs(d-k))+1E-5);this.root=l;g.forEachLeafSphere(function(b,d){return a._insertIntoOctant(d,b,l)});this._insertIntoOctant(c,b,l)}}else this._expandToCoverPoint(d.x,d.y,d.z),this._insertIntoOctant(c,b,this.root);
else d=new n,d.isLeaf=!0,d.addSphereData(c,b),this.root=d,this.keysToLeaves[c]=d}};q.prototype._expandToCoverPoint=function(c,b,a){for(;!this.root.containsPoint(c,b,a);){var d=this.root,g=d.cx,h=d.cy,e=d.cz,f=d.cr,k=new n;k.maxRadius=d.maxRadius;k.sphereCount=d.sphereCount;k.leafCount=d.leafCount;k.cx=g+f*(c<g?-1:1);k.cy=h+f*(b<h?-1:1);k.cz=e+f*(a<e?-1:1);k.cr=2*f;g=k.getSubOctantIndexForPoint(g,h,e);d.parent=k;d.index=g;k[g]=d;this.root=k}};q.prototype._insertIntoOctant=function(c,b,a){var d=b.center,
g=b.radius;if(a.isLeaf){var h=a.dataX,e=a.dataY,f=a.dataZ;if(d.x===h&&d.y===e&&d.z===f){a.addSphereData(c,b);for(b=a.parent;b;b=b.parent)g>b.maxRadius&&(b.maxRadius=g);this.keysToLeaves[c]=a}else g=Na(a),a.parent[a.index]=g,g.addOctantForPoint(a,h,e,f),this._insertIntoOctant(c,b,g)}else{a.sphereCount++;h=a.getSubOctantIndexForPoint(d.x,d.y,d.z);if(h=a[h])return this._insertIntoOctant(c,b,h);h=new n;h.isLeaf=!0;a.addOctantForPoint(h,d.x,d.y,d.z);h.addSphereData(c,b);for(a=h.parent;a;a=a.parent)g>a.maxRadius&&
(a.maxRadius=g),a.leafCount++;this.keysToLeaves[c]=h}};q.prototype.removeSphere=function(c){var b=this.keysToLeaves[c];if(b){for(var a=b.parent;a;)a.sphereCount--,a=a.parent;if(1<b.sphereCount)b.removeSphereData(c),b.updateMaxRadii();else{a=b;do{var d=a.parent;(b=d)&&(d[a.index]=null);a=a.parent}while(a&&0===a.sphereCount);if(!b){this.root=null;return}d=null;for(a=b;a;)a.leafCount--,1===a.leafCount&&(d=a),a=a.parent;d?(a=this._findSingleLeaf(d),(b=d.parent)?(b.addOctantForPoint(a,a.cx,a.cy,a.cz),
b.updateMaxRadii()):this.root=a):b.updateMaxRadii()}delete this.keysToLeaves[c]}};q.prototype._updateSphere=function(c,b){var a=this.keysToLeaves[c],d=b.center;if(a.containsPoint(d.x,d.y,d.z)){var g=d.x!==a.dataX||d.y!==a.dataY||d.z!==a.dataZ;1<a.sphereCount&&g?(a.removeSphereData(c),a.updateMaxRadii(),this._insertIntoOctant(c,b,a)):(g&&(a.dataX=d.x,a.dataY=d.y,a.dataZ=d.z),b.radius!==a.maxRadius&&a.updateMaxRadii())}else this.removeSphere(c),this.putSphere(c,b)};q.prototype._findSingleLeaf=function(c){function b(a){a.isLeaf&&
(d=a)}function a(a){d=null;this.walkBranch(a,b);return d}var d;this._findSingleLeaf=a;return a.call(this,c)};q.prototype.walkTree=function(c){this.root&&this.walkBranch(this.root,c)};q.prototype.walkBranch=function(c,b){if(!1!==b(c)&&!c.isLeaf)for(var a=0;8>a;a++)null!==c[a]&&this.walkBranch(c[a],b)};q.prototype.forEachSphereOnRay=function(c,b,a){return this._forEachMatchingSphere(c.intersectsSphere.bind(c),b,a)};q.prototype.forEachIntersectingSphere=function(c,b,a){return this._forEachMatchingSphere(c.intersectsSphere.bind(c),
b,a)};q.prototype._forEachMatchingSphere=function(c,b,a){function d(d,h){c(d)&&b.call(a,d,h)}this.walkTree(function(a){if(a.isLeaf)a.forEachLeafSphere(d);else if(M.center.set(a.cx,a.cy,a.cz),M.radius=a.cr*Ma+a.maxRadius,!c(M))return!1;return!0})};var n=function(){};n.prototype.containsPoint=function(c,b,a){var d=this.cx,g=this.cy,h=this.cz,e=this.cr;return c>=d-e&&c<d+e&&b>=g-e&&b<g+e&&a>=h-e&&a<h+e};n.prototype.getSubOctantIndexForPoint=function(c,b,a){return(a<this.cz?0:4)+(b<this.cy?0:2)+(c<this.cx?
0:1)};n.prototype.addOctantForPoint=function(c,b,a,d){var g=this.getSubOctantIndexForPoint(b,a,d),h=this.cr/2;c.parent=this;c.index=g;c.cx=this.cx+h*(b<this.cx?-1:1);c.cy=this.cy+h*(a<this.cy?-1:1);c.cz=this.cz+h*(d<this.cz?-1:1);c.cr=h;return this[g]=c};n.prototype.findMaxSphereRadius=function(){var c=0;if(this.isLeaf){var b=this.data;if(1<this.sphereCount)for(var a in b){var d=b[a].radius;d>c&&(c=d)}else c=b.radius}else for(b=0;8>b;b++)null!==this[b]&&this[b].maxRadius>c&&(c=this[b].maxRadius);
return c};n.prototype.updateMaxRadii=function(){var c=this.findMaxSphereRadius();if(c>this.maxRadius)for(var b=this;b;)c>b.maxRadius&&(b.maxRadius=c),b=b.parent;else c<this.maxRadius&&(this.maxRadius=c,this.parent&&this.parent.updateMaxRadii())};n.prototype.addSphereData=function(c,b){var a=this.sphereCount++;if(0===a)this.leafCount=1,this.data=b,this.dataKey=c,c=b.center,this.dataX=c.x,this.dataY=c.y,this.dataZ=c.z;else if(1===a){a=this.data;var d=this.data=Object.create(null);d[this.dataKey]=a;
d[c]=b;this.dataKey=null}else 1<a&&(this.data[c]=b);b.radius>this.maxRadius&&(this.maxRadius=b.radius)};n.prototype.removeSphereData=function(c){var b=this.data;if(b){var a=this.sphereCount--;if(2<a)delete b[c];else if(2===a)for(var d in b){if(d!==c){this.dataKey=d;this.data=b[d];break}}else this.data=null}};n.prototype.forEachLeafSphere=function(c,b){var a=this.data;if(a)if(1<this.sphereCount)for(var d in a)c.call(b,a[d],d);else c.call(b,a,this.dataKey)};r(n.prototype,{parent:null,index:-1,cx:0,
cy:0,cz:0,cr:0,0:null,1:null,2:null,3:null,4:null,5:null,6:null,7:null,isLeaf:!1,data:null,dataKey:null,dataX:0,dataY:0,dataZ:0,sphereCount:0,leafCount:0,maxRadius:0});var Na=function(){var c="parent index cx cy cz cr sphereCount leafCount maxRadius".split(" ");return function(b){for(var a=new n,d=c.length;d--;)a[c[d]]=b[c[d]];return a}}(),E=l.utils.assign,N=new e.Vector2,x=new e.Vector3,qa=new e.Raycaster,H=function(c){function b(a){c.call(this,a);this._object3DFacadesById=Object.create(null);this._onBgClick=
this._onBgClick.bind(this)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){var a=this.width,b=this.height,g=this.antialias,h=this.backgroundColor,f=this.contextAttributes,l=this._element,k=this._threeRenderer,m=this.rendererClass||e.WebGLRenderer;k&&k instanceof m||(k&&k.dispose(),f=E({alpha:!0,antialias:g},f),(k=l.getContext("webgl2",f)||void 0)||console.info("webgl2 init failed, trying webgl"),k=this._threeRenderer=new m(E({canvas:l,
context:k},f)));f=this.shadows;k.shadowMap.enabled=!!f;f&&"object"===typeof f&&E(k.shadowMap,f);h!==this._bgColor&&(this._threeRenderer.setClearColor(new e.Color(h||0),null!=h?1:0),this._bgColor=h);this._updateDrawingBufferSize(a,b,this.pixelRatio||window.devicePixelRatio||1);c.prototype.afterUpdate.call(this)};b.prototype.describeChildren=function(){return[this._getCameraDef(),this._getSceneDef()]};b.prototype._getCameraDef=function(){return E({key:"camera",facade:fa,aspect:this.width/this.height},
this.camera)};b.prototype._getSceneDef=function(){return{key:"scene",facade:pa,lights:this.lights,objects:this.objects,fog:this.fog,onClick:this.onBackgroundClick?this._onBgClick:null}};b.prototype._updateDrawingBufferSize=function(a,b,c){var d=this._threeRenderer;d.getSize(N);N.width===a&&N.height===b&&d.getPixelRatio()===c||d.setDrawingBufferSize(a,b,c)};b.prototype.doRender=function(){function a(a,d){a.call(this._object3DFacadesById[d],h,b,c)}var b=this.getChildByKey("scene").threeObject,c=this.getChildByKey("camera").threeObject,
h=this._threeRenderer,e=this.eventRegistry;e.forEachListenerOfType("beforerender",a,this);h.render(b,c);e.forEachListenerOfType("afterrender",a,this);if(e=this.onStatsUpdate){var f=h.info,k=f.memory;f=f.render;k={"WebGL Draw Calls":f.calls,"WebGL Geometries":k.geometries,"WebGL Textures":k.textures,"WebGL Triangles":f.triangles};f.points&&(k["WebGL Points"]=f.points);f.lines&&(k["WebGL Lines"]=f.lines);e(k)}};b.prototype.getFacadeUserSpaceXYZ=function(a){a=a.threeObject.matrixWorld.elements;return this.projectWorldPosition(a[12],
a[13],a[14])};b.prototype.projectWorldPosition=function(a,b,c){x.set(a,b,c);a=this.getChildByKey("camera");a.updateMatrices();a=a.threeObject;x.applyMatrix4(a.matrixWorldInverse);b=x.length()*(0<x.z?-1:1);x.applyMatrix4(a.projectionMatrix);return new e.Vector3((x.x+1)*this.width/2,(1-x.y)*this.height/2,b)};b.prototype._normalizePointerEvent=function(a){if(!a.ray){var b=a;if(a.touches){var g=/^touch(end|cancel)$/.test(a.type)?a.changedTouches:a.touches;1===g.length&&(b=g[0])}var h=a.target.getBoundingClientRect();
g=((b.clientX||0)-(h.left||0))/(h.width||this.width)*2-1;b=((b.clientY||0)-(h.top||0))/(h.height||this.height)*-2+1;h=this.getChildByKey("camera");h.updateMatrices();a.ray=h.getRayAtProjectedCoords(g,b)}c.prototype._normalizePointerEvent.call(this,a)};b.prototype.getFacadesAtEvent=function(a,b){return a.ray?this.getFacadesOnRay(a.ray,b):null};b.prototype.getFacadesOnRay=function(a,b){var c=this,d=this._updateOctree(),e=null;d&&(qa.ray=a,d.forEachSphereOnRay(a,function(a,d){(a=(d=(a=c._object3DFacadesById)&&
a[d])&&(!b||b(d))&&d.raycast&&d.raycast(qa))&&a[0]&&(a[0].facade=d,(e||(e=[])).push(a[0]))}));return e};b.prototype._updateOctree=function(){var a=this._boundingSphereOctree,b=this._octreeChangeset;if(b){a||(a=this._boundingSphereOctree=new q);var c=b.remove;b=b.put;if(c)for(var e in c)a.removeSphere(e);if(b)for(var f in b)e=this._object3DFacadesById[f],!e||e.isDestroying||c&&c[f]||((e=e.getBoundingSphere&&e.getBoundingSphere())?a.putSphere(f,e):a.removeSphere(f));this._octreeChangeset=null}return a};
b.prototype._queueForOctreeChange=function(a,b){var c=this._octreeChangeset||(this._octreeChangeset={});(c[a]||(c[a]=Object.create(null)))[b.$facadeId]=b};b.prototype._onBgClick=function(a){if(a.target===a.currentTarget)this.onBackgroundClick(a)};b.prototype.destructor=function(){c.prototype.destructor.call(this);this._threeRenderer.dispose()};return b}(l.WorldBaseFacade);H.prototype._notifyWorldHandlers=E(Object.create(l.WorldBaseFacade.prototype._notifyWorldHandlers),{getCameraPosition:function(c,
b){b.setFromMatrixPosition(this.getChildByKey("camera").threeObject.matrixWorld)},getCameraFacade:function(c,b){b.callback(this.getChildByKey("camera"))},getSceneFacade:function(c,b){b.callback(this.getChildByKey("scene"))},projectWorldPosition:function(c,b){c=b.worldPosition;b.callback(this.projectWorldPosition(c.x,c.y,c.z))},object3DAdded:function(c){this._object3DFacadesById[c.$facadeId]=c;this._queueForOctreeChange("put",c)},object3DBoundsChanged:function(c){this._queueForOctreeChange("put",c)},
object3DRemoved:function(c){delete this._object3DFacadesById[c.$facadeId];this._queueForOctreeChange("remove",c)},rayPointerMotion:function(c,b){var a=new MouseEvent("mousemove");a.isRayEvent=!0;a.ray=b;a.eventSource=c;this._onPointerMotionEvent(a)},rayPointerAction:function(c,b){var a=new ("wheel"===b.type?WheelEvent:MouseEvent)(b.type,b);a.isRayEvent=!0;a.ray=b.ray;a.eventSource=c;this._onPointerActionEvent(a)}});var Oa="onMouseOver onMouseOut onMouseMove onMouseDown onMouseUp onClick onDoubleClick".split(" ");
r=function(c){function b(a){var b=new e.Object3D;b.isRenderable=!1;b.$troikaVisible=b.visible;Object.defineProperty(b,"visible",Pa);c.call(this,a,b);this.notifyWorld("instanceableAdded")}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={instancedThreeObject:{configurable:!0}};a.instancedThreeObject.set=function(a){a!==this._instancedThreeObject&&(this._instancedThreeObject=a,this.notifyWorld("instanceableChanged"),this._boundsChanged=!0)};a.instancedThreeObject.get=
function(){return this._instancedThreeObject};b.prototype.setInstanceUniform=function(a,b){var c=this._instanceUniforms||(this._instanceUniforms=Object.create(null));c[a]!==b&&(c[a]=b,this.notifyWorld("instanceableUniformChanged",a))};b.prototype.updateMatrices=function(){var a=this._worldMatrixVersion;c.prototype.updateMatrices.call(this);this._worldMatrixVersion!==a&&this.threeObject.$troikaVisible&&this.notifyWorld("instanceableMatrixChanged")};b.prototype.destructor=function(){this.notifyWorld("instanceableRemoved");
c.prototype.destructor.call(this)};b.prototype.getGeometry=function(){var a=this.instancedThreeObject;return a&&a.geometry};b.prototype.raycast=function(a){var b=this.instancedThreeObject,c=this.threeObject;if(b&&c){var d=b.matrixWorld;b.matrixWorld=c.matrixWorld;a=this._raycastObject(b,a);b.matrixWorld=d;return a}return null};Object.defineProperties(b.prototype,a);return b}(p);var Pa={set:function(c){c!==this.$troikaVisible&&(this.$troikaVisible=c,this.$facade.notifyWorld("instanceableChanged"))},
get:function(){return this.$troikaVisible}};l.utils.assign(r.prototype,{_lastInstancedMatrixVersion:-1,_instancedThreeObject:null});var O=new e.BufferGeometry,Qa=new e.MeshBasicMaterial,P={basic:e.MeshBasicMaterial,depth:e.MeshDepthMaterial,distance:e.MeshDistanceMaterial,lambert:e.MeshLambertMaterial,matcap:e.MeshMatcapMaterial,normal:e.MeshNormalMaterial,phong:e.MeshPhongMaterial,physical:e.MeshPhysicalMaterial,standard:e.MeshStandardMaterial,toon:e.MeshToonMaterial},y=function(c){function b(a){c.call(this,
a,new e.Mesh(O,Qa));this.material="standard";this.autoDisposeMaterial=this.autoDisposeGeometry=!1;this._dirtyMtlProps=null}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.afterUpdate=function(){var a=this.geometry,b=this.material,g=this.threeObject;(a||O)!==g.geometry&&(this.autoDisposeGeometry&&g.geometry.dispose(),g.geometry=a||O);b!==this._lastMtl&&(this._lastMtl=b,"string"===typeof b?b=new (P[b]||e.MeshStandardMaterial):b&&b.isMaterial||(b="function"===
typeof b?new b:new e.MeshStandardMaterial),g.material!==b&&(this.autoDisposeMaterial&&g.material.dispose(),g.material=b));if(a=this._dirtyMtlProps)g.material.setValues(a),this._dirtyMtlProps=null;c.prototype.afterUpdate.call(this)};b.prototype.destructor=function(){this.autoDisposeGeometry&&this.threeObject.geometry.dispose();this.autoDisposeMaterial&&this.threeObject.material.dispose();c.prototype.destructor.call(this)};return b}(p),Ra={type:1,id:1,uuid:1,version:1};Object.keys(P).forEach(function(c){var b=
new P[c];c=function(a){b.hasOwnProperty(a)&&!Ra.hasOwnProperty(a)&&Object.defineProperty(y.prototype,"material."+a,{enumerable:!0,configurable:!0,get:function(){var b=this._dirtyMtlProps;return b&&a in b?b[a]:this.threeObject.material[a]},set:function(b){(this._dirtyMtlProps||(this._dirtyMtlProps=Object.create(null)))[a]=b}})};for(var a in b)c(a)});var ra=l.utils.memoize(function(){return new e.BoxBufferGeometry(1,1,1,1,1)}),Sa=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=
Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},width:{configurable:!0},height:{configurable:!0},depth:{configurable:!0}};a.geometry.get=function(){return ra()};a.width.set=function(a){this.scaleX=a};a.width.get=function(){return this.scaleX};a.height.set=function(a){this.scaleY=a};a.height.get=function(){return this.scaleY};a.depth.set=function(a){this.scaleZ=a};a.depth.get=function(){return this.scaleZ};Object.defineProperties(b.prototype,a);return b}(y),
sa=Object.create(null,[["low",32],["medium",64],["high",128]].reduce(function(c,b){var a=b[1];c[b[0]]={get:l.utils.memoize(function(){return(new e.CircleBufferGeometry(1,a)).rotateX(-Math.PI/2)})};return c},{})),Ta=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},radius:{configurable:!0}};a.geometry.get=function(){return sa[this.detail]||sa.medium};a.radius.set=function(a){this.scaleX=
this.scaleZ=a};a.radius.get=function(){return this.scaleX};Object.defineProperties(b.prototype,a);return b}(y),Ua=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},size:{configurable:!0}};a.geometry.get=function(){return ra()};a.size.set=function(a){this.scale=a};a.size.get=function(){return this.scale};Object.defineProperties(b.prototype,a);return b}(y),Va=l.utils.memoize(function(){return(new e.PlaneBufferGeometry(1,
1,1,1)).rotateX(-Math.PI/2)}),Wa=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},width:{configurable:!0},depth:{configurable:!0}};a.geometry.get=function(){return Va()};a.width.set=function(a){this.scaleX=a};a.width.get=function(){return this.scaleX};a.depth.set=function(a){this.scaleZ=a};a.depth.get=function(){return this.scaleZ};Object.defineProperties(b.prototype,a);return b}(y),
ta=Object.create(null,[["low",16,12],["medium",32,24],["high",64,48]].reduce(function(c,b){var a=b[1],d=b[2];c[b[0]]={get:l.utils.memoize(function(){return new e.SphereBufferGeometry(1,a,d)})};return c},{})),Xa=function(c){function b(){c.apply(this,arguments)}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;var a={geometry:{configurable:!0},radius:{configurable:!0}};a.geometry.get=function(){return ta[this.detail]||ta.medium};a.radius.set=function(a){this.scale=
a};a.radius.get=function(){return this.scale};Object.defineProperties(b.prototype,a);return b}(y),I=function(c){function b(a){var b=this;c.call(this,a);this._onCanvasRef=function(a){var c=b.context.onCanvasRef;c&&c(a);(c=b.props.onCanvasRef)&&c(a)}}c&&(b.__proto__=c);b.prototype=Object.create(c&&c.prototype);b.prototype.constructor=b;b.prototype.render=function(){var a=this.props,b=this.context;return v.createElement(c,l.utils.assign({},a,{onCanvasRef:this._onCanvasRef,canvasStyle:a.canvasStyle||
b.canvasStyle,worldFacade:a.worldFacade||b.worldFacade||H,worldProps:l.utils.assign({antialias:a.antialias,rendererClass:a.rendererClass,backgroundColor:a.backgroundColor,shadows:a.shadows,camera:a.camera,lights:a.lights,objects:a.objects,fog:a.fog,onBackgroundClick:a.onBackgroundClick},b.worldProps,a.worldProps)}),a.children)};return b}(l.ReactCanvasBase);I.displayName="Canvas3D";I.propTypes=l.utils.assignIf({backgroundColor:m.any,lights:m.array,camera:m.object,objects:m.oneOfType([m.array,m.object]).isRequired,
antialias:m.bool,onBackgroundClick:m.func,rendererClass:m.func},l.ReactCanvasBase.commonPropTypes);I.contextType=v.createContext({worldFacade:H,worldProps:{},onCanvasRef:null,canvasStyle:null});Object.defineProperty(f,"Facade",{enumerable:!0,get:function(){return l.Facade}});Object.defineProperty(f,"ListFacade",{enumerable:!0,get:function(){return l.ListFacade}});Object.defineProperty(f,"ParentFacade",{enumerable:!0,get:function(){return l.ParentFacade}});Object.defineProperty(f,"createDerivedMaterial",
{enumerable:!0,get:function(){return z.createDerivedMaterial}});f.AmbientLight3DFacade=ia;f.BoxFacade=Sa;f.Canvas3D=I;f.CircleFacade=Ta;f.CubeFacade=Ua;f.DirectionalLight3DFacade=ja;f.Group3DFacade=ha;f.HemisphereLight3DFacade=ma;f.HtmlOverlay3DFacade=Ga;f.Instanceable3DFacade=r;f.InstancingManager=L;f.MeshFacade=y;f.Object3DFacade=p;f.OrthographicCamera3DFacade=J;f.PerspectiveCamera3DFacade=fa;f.PlaneFacade=Wa;f.PointLight3DFacade=la;f.RectAreaLight3DFacade=Ha;f.Scene3DFacade=pa;f.SphereFacade=Xa;
f.SpotLight3DFacade=ka;f.World3DFacade=H;f.makeWorldTextureProvider=function(c){return function(b){function a(a){var c=this,d=new e.CanvasTexture;b.call(this,a,d);this.worldTexture=d;var f=this._refireAsInnerEvent.bind(this);Oa.forEach(function(a){function b(a){f(a);d&&d.call(this,a)}var d;c[a]=b;Object.defineProperty(c,a,{set:function(a){d=a},get:function(){return b}})})}b&&(a.__proto__=b);a.prototype=Object.create(b&&b.prototype);a.prototype.constructor=a;a.prototype.afterUpdate=function(){var a=
this,c=this._worldFacade,e=this.textureWorld;c&&e&&c instanceof e.facade||(c&&(c.onAfterRender=null,c.destructor()),e&&(this.worldTexture.dispose(),c=document.createElement("canvas"),c.width=c.height=2,this.worldTexture.image=c,c=this._worldFacade=new e.facade(c),c.onAfterRender=function(){a.worldTexture.needsUpdate=!0;a.notifyWorld("needsRender")}));c&&(c.renderingScheduler=this._getOuterWorld().renderingScheduler,l.utils.assign(c,e),c.afterUpdate());b.prototype.afterUpdate.call(this)};a.prototype._refireAsInnerEvent=
function(a){var b=this._worldFacade;if(b){var c=a.intersection&&a.intersection.uv,d=c?Math.round(c.x*b.width):-1;b=c?Math.round((1-c.y)*b.height):-1;a=a.nativeEvent||a;c=document.createEvent("MouseEvents");c.initMouseEvent(a.type,!0,!0,window,a.detail,d,b,d,b,a.ctrlKey,a.altKey,a.shiftKey,a.metaKey,a.button,null);this.worldTexture.image.dispatchEvent(c)}};a.prototype._getOuterWorld=function(){for(var a=this;a&&!a.isWorld;)a=a.parent;return a};a.prototype.destructor=function(){var a=this._worldFacade;
a&&(a.onAfterRender=null,a.destructor());this.worldTexture.dispose();b.prototype.destructor.call(this)};return a}(c)};Object.defineProperty(f,"__esModule",{value:!0})})
{
"name": "troika-3d",
"version": "0.20.0",
"version": "0.21.0",
"description": "Troika 3D",

@@ -18,9 +18,9 @@ "author": "Jason Johnston <jason.johnston@protectwise.com>",

"dependencies": {
"troika-core": "^0.20.0",
"troika-three-utils": "^0.20.0"
"troika-core": "^0.21.0",
"troika-three-utils": "^0.21.0"
},
"peerDependencies": {
"three": "0.103.x - 0.112.x"
"three": "0.103.x - 0.115.x"
},
"gitHead": "c97a9f97c30cd4ddfba068430d02d7c0a73c2e08"
"gitHead": "0a7527c90b55f0cd09192ad2f71c62f287702186"
}

@@ -5,15 +5,11 @@ import { utils } from 'troika-core'

InstancedBufferGeometry,
Matrix4,
RGBADepthPacking,
ShaderMaterial,
ShaderLib,
Vector3
Matrix4
} from 'three'
import Group3DFacade from '../Group3DFacade.js'
import { upgradeShaders } from './InstancingShaderUpgrades.js'
import { getShadersForMaterial, getShaderUniformTypes, expandShaderIncludes } from 'troika-three-utils'
import { getInstancingDerivedMaterial } from './InstancingDerivedMaterial.js'
import { getShadersForMaterial } from 'troika-three-utils'
const { assign, assignIf } = utils
const { assign } = utils
const INSTANCE_BATCH_SIZE = 1024 //TODO make this an option?
const INSTANCE_BATCH_SIZE = 128 //TODO make this an option?
const DYNAMIC_DRAW = 0x88E8 //can't import DynamicDrawUsage from three without breaking older versions

@@ -126,3 +122,4 @@

let facadeUniforms = facade._instanceUniforms
let value = facadeUniforms && uniform in facadeUniforms ? facadeUniforms[uniform] : getShadersForMaterial(protoObject.material).uniforms[uniform].value //TODO clean up
let value = facadeUniforms && (uniform in facadeUniforms) ?
facadeUniforms[uniform] : getDefaultUniformValue(protoObject.material, uniform)
setAttributeValue(attr, attrOffset, value)

@@ -226,5 +223,4 @@ }

let mat = object.material
let shaders = getShadersForMaterial(mat)
let uniforms = mat.instanceUniforms
key = `${object.geometry.id}|${mat.id}|${shaders.vertexShader}|${shaders.fragmentShader}|${uniforms ? uniforms.sort().join(',') : ''}`
key = `${object.geometry.id}|${mat.id}|${uniforms ? uniforms.sort().join(',') : ''}`
cache[object.id] = key

@@ -235,21 +231,19 @@ }

_getInstanceUniformsTypes(material) {
let cache = this._uniformTypesCache || (this._uniformTypesCache = Object.create(null)) //cache results for duration of this frame
let result = cache[material.id]
_getInstanceUniformSizes(material) {
// Cache results per material for duration of this frame
let cache = this._uniformSizesCache || (this._uniformSizesCache = new Map())
let result = cache.get(material)
if (!result) {
result = cache[material.id] = Object.create(null)
let {instanceUniforms} = material
if (instanceUniforms && instanceUniforms.length) {
let {vertexShader, fragmentShader} = getShadersForMaterial(material)
let allTypes = assign(
getShaderUniformTypes(expandShaderIncludes(vertexShader)),
getShaderUniformTypes(expandShaderIncludes(fragmentShader))
) //TODO handle type mismatches?
for (let i = instanceUniforms.length; i--;) {
let uniform = instanceUniforms[i]
if (allTypes[uniform]) {
result[uniform] = allTypes[uniform]
result = Object.create(null)
if (material.instanceUniforms) {
material.instanceUniforms.forEach(name => {
let size = getUniformItemSize(material, name)
if (size > 0) {
result[name] = size
} else {
console.warn(`Could not determine item size for uniform ${name}`)
}
}
})
}
cache.set(material, result)
}

@@ -267,38 +261,57 @@ return result

let batchKey = this._getBatchKey(instancedObject)
let uniformsTypes = this._getInstanceUniformsTypes(material)
let batchGeometry = this._batchGeometryPool.borrow(batchKey, geometry, uniformsTypes)
let uniformSizes = this._getInstanceUniformSizes(material)
let batchGeometry = this._batchGeometryPool.borrow(batchKey, geometry, uniformSizes)
batchGeometry.maxInstancedCount = 0
// Upgrade the material to one with the shader modifications and defines to trigger instancing
let batchMaterial = Object.create(material)
let instUniforms = material.instanceUniforms || []
instUniforms.sort()
batchMaterial.defines = assignIf({
[`TROIKA_INSTANCED_${instUniforms.join('_')}`]: '' //unique define value to trigger compilation for different sets of instanceUniforms
}, batchMaterial.defines)
batchMaterial.onBeforeCompile = function(shaderInfo) {
// Upgrade the material's shaders to support instanced matrices and other uniforms
// This will be called on program change even after first compile, so cache after first run
let upgraded = material.$troikaUpgraded
let upgradeKey = `${shaderInfo.vertexShader}|${shaderInfo.fragmentShader}|${instUniforms.join(',')}`
if (!upgraded || upgraded.upgradeKey !== upgradeKey) {
upgraded = material.$troikaUpgraded = upgradeShaders(shaderInfo.vertexShader, shaderInfo.fragmentShader, instUniforms)
upgraded.upgradeKey = upgradeKey
}
assign(shaderInfo, upgraded)
}
// Upgrade the material to one with the shader modifications for instancing
let batchMaterial = getInstancingDerivedMaterial(material)
let depthMaterial, distanceMaterial
// Create a new mesh object to hold it all
let shadowMaterial = null
let batchObject = Object.create(instancedObject, {
// Redefine properties rather than setting them so we don't inadvertently trigger setters on
// the base object:
geometry: { value: batchGeometry },
material: { value: batchMaterial },
visible: { value: true },
frustumCulled: { value: false },
// Lazy getters for shadow materials:
customDepthMaterial: {
get() { return shadowMaterial = getBatchDepthMaterial() }
get() {
if (!depthMaterial) {
depthMaterial = batchMaterial.getDepthMaterial()
// We need to trick WebGLRenderer into setting the `viewMatrix` uniform, which it doesn't
// normally do for MeshDepthMaterial but it's needed by the instancing shader code. It does
// for ShaderMaterials so we pretend to be one.
depthMaterial.isShaderMaterial = true
}
return depthMaterial
}
},
customDistanceMaterial: {
get() { return shadowMaterial = getBatchDistanceMaterial() }
get() {
if (!distanceMaterial) {
distanceMaterial = batchMaterial.getDistanceMaterial()
// We need to trick WebGLRenderer into setting the `viewMatrix` uniform, which it doesn't
// normally do for MeshDistanceMaterial but it's needed by the instancing shader code. It does
// for ShaderMaterials so we pretend to be one.
distanceMaterial.isShaderMaterial = true
// Additionally, WebGLShadowMap.render() rotates a single camera 6 times per object, which fails
// to trigger the code in WebGLRenderer.setProgram() that updates the viewMatrix uniform for
// directions 2 through 6. Since we need a correct viewMatrix in the instancing shader code,
// we hack it by defining our own viewMatrix uniform on the distance material and manually
// updating it before each view of the distance cube is rendered. Unfortunately intercepting the
// view changes in a way that has access to the shadow camera's viewMatrix has proven quite
// difficult; the least-awful way I've found is to monkeypatch the `modelViewMatrix.multiplyMatrices()`
// function which is always called - see (*!) below.
distanceMaterial.uniforms = assign({
viewMatrix: { value: new Matrix4() }
}, distanceMaterial.uniforms)
}
return distanceMaterial
}
},
// Hack to update viewMatrix for each face of the distance cube - see explanation
// in getBatchDistanceMaterial() comments below. Would be cleaner to use an onBeforeRender
// hook but that isn't called during shadowmap rendering.
// (*!) Hack for updating viewMatrix uniform on the distance material - see explanation above.
modelViewMatrix: {

@@ -308,6 +321,7 @@ value: function() {

modelViewMatrix.multiplyMatrices = function(viewMatrix, matrixWorld) {
Matrix4.prototype.multiplyMatrices.call(this, viewMatrix, matrixWorld)
if (shadowMaterial && shadowMaterial._updateViewMatrix) {
shadowMaterial._updateViewMatrix(viewMatrix)
if (distanceMaterial) {
distanceMaterial.uniforms.viewMatrix.value.copy(viewMatrix)
distanceMaterial.uniformsNeedUpdate = true //undocumented flag for ShaderMaterial
}
return Matrix4.prototype.multiplyMatrices.call(this, viewMatrix, matrixWorld)
}

@@ -320,6 +334,2 @@ return modelViewMatrix

batchObject.$troikaInstancingManager = this
batchObject.visible = true
batchObject.frustumCulled = false
batchObject.geometry = batchGeometry
batchObject.material = batchMaterial
// NOTE other props are inherited so don't need to copy them

@@ -335,3 +345,3 @@ return batchObject

this._batchKeysCache = null
this._uniformTypesCache = null
this._uniformSizesCache = null

@@ -359,3 +369,3 @@ // Remove batch objects from scene

borrow(key, baseGeometry, uniformsTypes) {
borrow(key, baseGeometry, instanceUniformSizes) {
let poolsByKey = this._poolsByKey

@@ -384,7 +394,5 @@ let pool = poolsByKey[key] || (poolsByKey[key] = {geometries: [], firstFree: 0})

// Create instancing attributes for the instanceUniforms
for (let name in uniformsTypes) {
let type = uniformsTypes[name]
let itemSize = ATTR_ITEM_SIZES[type]
let ArrayType = type === 'int' ? Uint32Array : Float32Array
let attr = new InstancedBufferAttribute(new ArrayType(INSTANCE_BATCH_SIZE * itemSize), itemSize)
for (let name in instanceUniformSizes) {
let itemSize = instanceUniformSizes[name]
let attr = new InstancedBufferAttribute(new Float32Array(INSTANCE_BATCH_SIZE * itemSize), itemSize)
if (attr.setUsage) {

@@ -451,12 +459,2 @@ attr.setUsage(DYNAMIC_DRAW)

const ATTR_ITEM_SIZES = {
'int': 1,
'float': 1,
'vec2': 2,
'vec3': 3,
'vec4': 4
}
function setAttributeValue(attr, offset, value) {

@@ -481,43 +479,29 @@ let size = attr.itemSize

function getDefaultUniformValue(material, name) {
// Try uniforms on the material itself, then try the builtin material shaders
let uniforms = material.uniforms
if (uniforms && uniforms[name]) {
return uniforms[name].value || null
}
uniforms = getShadersForMaterial(material).uniforms
if (uniforms && uniforms[name]) {
return uniforms[name].value || null
}
return null
}
let getBatchDepthMaterial = function() {
// We have to use a ShaderMaterial here instead of just deriving from MeshDepthMaterial,
// due to a quirk in WebGLRenderer where it doesn't set the viewMatrix uniform
// for MeshDepthMaterial, which is needed by the instancing upgrades.
const shaderInfo = assign({}, ShaderLib.depth)
shaderInfo.vertexShader = upgradeShaders(shaderInfo.vertexShader, '', []).vertexShader
const material = new ShaderMaterial(shaderInfo)
material.isMeshDepthMaterial = true
material.depthPacking = RGBADepthPacking
getBatchDepthMaterial = () => material
return material
function getUniformItemSize(material, name) {
return getItemSizeForValue(getDefaultUniformValue(material, name))
}
let getBatchDistanceMaterial = function() {
// We have to use a ShaderMaterial here instead of just deriving from MeshDistanceMaterial,
// due to a quirk in WebGLRenderer where it doesn't set the viewMatrix uniform
// for MeshDistanceMaterial, which is needed by the instancing upgrades.
// Additionally, the way WebGLShadowMap rotates a single camera 6 times per object prevents
// WebGLRenderer.setProgram() from updating the viewMatrix uniform for directions 2-6. To
// get around this we define a ShaderMaterial uniform for it and monkeypatch in way to
// intercept view changes and manually update the uniform to match (see modelViewMatrix
// override above when constructing the batchObject).
const shaderInfo = assign({}, ShaderLib.distanceRGBA)
const viewMatrix = new Matrix4()
shaderInfo.vertexShader = upgradeShaders(shaderInfo.vertexShader, '', []).vertexShader
shaderInfo.uniforms = assign({viewMatrix: {value: viewMatrix}}, shaderInfo.uniforms)
const material = new ShaderMaterial(shaderInfo)
material.isMeshDistanceMaterial = true
material.referencePosition = new Vector3() //mutated during shadowmap setup
material._updateViewMatrix = source => {
viewMatrix.copy(source)
material.uniformsNeedUpdate = true //undocumented flag for ShaderMaterial
}
getBatchDistanceMaterial = () => material
return material
function getItemSizeForValue(value) {
return value == null ? 0
: typeof value === 'number' ? 1
: value.isVector2 ? 2
: (value.isVector3 || value.isColor) ? 3
: value.isVector4 ? 4
: Array.isArray(value) ? value.length
: 0
}
export default InstancingManager

@@ -292,3 +292,2 @@ import { WorldBaseFacade, utils } from 'troika-core'

this._threeRenderer.dispose()
this._threeRenderer.forceContextLoss()
}

@@ -295,0 +294,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc