diff --git a/dist/xeokit-sdk.cjs.js b/dist/xeokit-sdk.cjs.js index c03eeda1d..aee76ec80 100644 --- a/dist/xeokit-sdk.cjs.js +++ b/dist/xeokit-sdk.cjs.js @@ -77670,10 +77670,8 @@ class TrianglesDataTextureOcclusionRenderer { const scene = this._scene; const clipping = scene._sectionPlanesState.sectionPlanes.length > 0; const src = []; + src.push("#version 300 es"); src.push("// TrianglesDataTextureColorRenderer vertex shader"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("#extension GL_EXT_frag_depth : enable"); - } src.push("uniform int renderPass;"); src.push("attribute vec3 position;"); if (scene.entityOffsetsEnabled) { @@ -77723,12 +77721,7 @@ class TrianglesDataTextureOcclusionRenderer { } src.push("vec4 clipPos = projMatrix * viewPosition;"); if (scene.logarithmicDepthBufferEnabled) { - if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("vFragDepth = 1.0 + clipPos.w;"); - } else { - src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"); - src.push("clipPos.z *= clipPos.w;"); - } + src.push("vFragDepth = 1.0 + clipPos.w;"); src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));"); } src.push("gl_Position = clipPos;"); @@ -77742,10 +77735,8 @@ class TrianglesDataTextureOcclusionRenderer { const sectionPlanesState = scene._sectionPlanesState; const clipping = sectionPlanesState.sectionPlanes.length > 0; const src = []; + src.push('#version 300 es'); src.push("// TrianglesDataTextureColorRenderer fragment shader"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("#extension GL_EXT_frag_depth : enable"); - } src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"); src.push("precision highp float;"); src.push("precision highp int;"); @@ -77753,7 +77744,7 @@ class TrianglesDataTextureOcclusionRenderer { src.push("precision mediump float;"); src.push("precision mediump int;"); src.push("#endif"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { + if (scene.logarithmicDepthBufferEnabled) { src.push("in float isPerspective;"); src.push("uniform float logDepthBufFC;"); src.push("in float vFragDepth;"); @@ -77780,7 +77771,7 @@ class TrianglesDataTextureOcclusionRenderer { src.push(" if (dist > 0.0) { discard; }"); src.push(" }"); } - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { + if (scene.logarithmicDepthBufferEnabled) { src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"); } src.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "); // Occluders are blue @@ -78540,6 +78531,7 @@ class TrianglesDataTextureNormalsRenderer { const sectionPlanesState = scene._sectionPlanesState; const clipping = (sectionPlanesState.sectionPlanes.length > 0); const src = []; + src.push("#version 300 es"); src.push("// Batched geometry normals fragment shader"); if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { diff --git a/dist/xeokit-sdk.es.js b/dist/xeokit-sdk.es.js index 740e15994..653b0c98a 100644 --- a/dist/xeokit-sdk.es.js +++ b/dist/xeokit-sdk.es.js @@ -77666,10 +77666,8 @@ class TrianglesDataTextureOcclusionRenderer { const scene = this._scene; const clipping = scene._sectionPlanesState.sectionPlanes.length > 0; const src = []; + src.push("#version 300 es"); src.push("// TrianglesDataTextureColorRenderer vertex shader"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("#extension GL_EXT_frag_depth : enable"); - } src.push("uniform int renderPass;"); src.push("attribute vec3 position;"); if (scene.entityOffsetsEnabled) { @@ -77719,12 +77717,7 @@ class TrianglesDataTextureOcclusionRenderer { } src.push("vec4 clipPos = projMatrix * viewPosition;"); if (scene.logarithmicDepthBufferEnabled) { - if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("vFragDepth = 1.0 + clipPos.w;"); - } else { - src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"); - src.push("clipPos.z *= clipPos.w;"); - } + src.push("vFragDepth = 1.0 + clipPos.w;"); src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));"); } src.push("gl_Position = clipPos;"); @@ -77738,10 +77731,8 @@ class TrianglesDataTextureOcclusionRenderer { const sectionPlanesState = scene._sectionPlanesState; const clipping = sectionPlanesState.sectionPlanes.length > 0; const src = []; + src.push('#version 300 es'); src.push("// TrianglesDataTextureColorRenderer fragment shader"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { - src.push("#extension GL_EXT_frag_depth : enable"); - } src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"); src.push("precision highp float;"); src.push("precision highp int;"); @@ -77749,7 +77740,7 @@ class TrianglesDataTextureOcclusionRenderer { src.push("precision mediump float;"); src.push("precision mediump int;"); src.push("#endif"); - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { + if (scene.logarithmicDepthBufferEnabled) { src.push("in float isPerspective;"); src.push("uniform float logDepthBufFC;"); src.push("in float vFragDepth;"); @@ -77776,7 +77767,7 @@ class TrianglesDataTextureOcclusionRenderer { src.push(" if (dist > 0.0) { discard; }"); src.push(" }"); } - if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { + if (scene.logarithmicDepthBufferEnabled) { src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"); } src.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "); // Occluders are blue @@ -78536,6 +78527,7 @@ class TrianglesDataTextureNormalsRenderer { const sectionPlanesState = scene._sectionPlanesState; const clipping = (sectionPlanesState.sectionPlanes.length > 0); const src = []; + src.push("#version 300 es"); src.push("// Batched geometry normals fragment shader"); if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) { diff --git a/dist/xeokit-sdk.es5.js b/dist/xeokit-sdk.es5.js index 666655336..8deb81a83 100644 --- a/dist/xeokit-sdk.es5.js +++ b/dist/xeokit-sdk.es5.js @@ -17420,11 +17420,11 @@ src.push("if (solid != 1u) {");src.push("if (isPerspectiveMatrix(projMatrix)) {" src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);");src.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;");src.push("vec4 viewPosition = viewMatrix * worldPosition; ");src.push("vWorldNormal = normal.xyz;");src.push("vec4 clipPos = projMatrix * viewPosition;");if(scene.logarithmicDepthBufferEnabled){src.push("vFragDepth = 1.0 + clipPos.w;");src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");}if(clipping){src.push(" vWorldPosition = worldPosition;");src.push(" vFlags2 = flags2.w;");}src.push("gl_Position = clipPos;");src.push("}");src.push("}");return src;}},{key:"_buildFragmentShader",value:function _buildFragmentShader(){var scene=this._scene;var sectionPlanesState=scene._sectionPlanesState;var clipping=sectionPlanesState.sectionPlanes.length>0;var src=[];src.push('#version 300 es');src.push("// Triangles dataTexture pick normals fragment shader");src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");src.push("precision highp float;");src.push("precision highp int;");src.push("#else");src.push("precision mediump float;");src.push("precision mediump int;");src.push("#endif");if(scene.logarithmicDepthBufferEnabled){src.push("in float isPerspective;");src.push("uniform float logDepthBufFC;");src.push("in float vFragDepth;");}if(clipping){src.push("in vec4 vWorldPosition;");src.push("flat in uint vFlags2;");for(var i=0;i 0u;");src.push(" if (clippable) {");src.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }");src.push(" }");}if(scene.logarithmicDepthBufferEnabled){// src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"); src.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;");}src.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);");src.push("}");return src;}},{key:"webglContextRestored",value:function webglContextRestored(){this._program=null;}},{key:"destroy",value:function destroy(){if(this._program){this._program.destroy();}this._program=null;}}]);return TrianglesDataTexturePickNormalsRenderer;}();math.vec3();math.vec3();math.vec3();math.vec3();math.mat4();/** * @private - */var TrianglesDataTextureOcclusionRenderer=/*#__PURE__*/function(){function TrianglesDataTextureOcclusionRenderer(scene){_classCallCheck(this,TrianglesDataTextureOcclusionRenderer);this._scene=scene;this._hash=this._getHash();this._allocate();}_createClass(TrianglesDataTextureOcclusionRenderer,[{key:"getValid",value:function getValid(){return this._hash===this._getHash();}},{key:"_getHash",value:function _getHash(){return this._scene._sectionPlanesState.getHash();}},{key:"drawLayer",value:function drawLayer(frameCtx,dataTextureLayer,renderPass){return;}},{key:"_allocate",value:function _allocate(){var scene=this._scene;var gl=scene.canvas.gl;this._program=new Program(gl,this._buildShader());if(this._program.errors){this.errors=this._program.errors;return;}var program=this._program;this._uRenderPass=program.getLocation("renderPass");this._uPositionsDecodeMatrix=program.getLocation("positionsDecodeMatrix");this._uWorldMatrix=program.getLocation("worldMatrix");this._uViewMatrix=program.getLocation("viewMatrix");this._uProjMatrix=program.getLocation("projMatrix");this._uSectionPlanes=[];for(var _i370=0,len=scene._sectionPlanesState.sectionPlanes.length;_i3700;var src=[];src.push("// TrianglesDataTextureColorRenderer vertex shader");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("#extension GL_EXT_frag_depth : enable");}src.push("uniform int renderPass;");src.push("attribute vec3 position;");if(scene.entityOffsetsEnabled){src.push("attribute vec3 offset;");}src.push("attribute vec4 color;");src.push("attribute vec4 flags;");src.push("attribute vec4 flags2;");src.push("uniform mat4 worldMatrix;");src.push("uniform mat4 viewMatrix;");src.push("uniform mat4 projMatrix;");src.push("uniform mat4 positionsDecodeMatrix;");if(scene.logarithmicDepthBufferEnabled){src.push("uniform float logDepthBufFC;");if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("out float vFragDepth;");}src.push("bool isPerspectiveMatrix(mat4 m) {");src.push(" return (m[2][3] == - 1.0);");src.push("}");src.push("out float isPerspective;");}if(clipping){src.push("out vec4 vWorldPosition;");src.push("out vec4 vFlags2;");}src.push("void main(void) {");// flags.x = NOT_RENDERED | COLOR_OPAQUE | COLOR_TRANSPARENT + */var TrianglesDataTextureOcclusionRenderer=/*#__PURE__*/function(){function TrianglesDataTextureOcclusionRenderer(scene){_classCallCheck(this,TrianglesDataTextureOcclusionRenderer);this._scene=scene;this._hash=this._getHash();this._allocate();}_createClass(TrianglesDataTextureOcclusionRenderer,[{key:"getValid",value:function getValid(){return this._hash===this._getHash();}},{key:"_getHash",value:function _getHash(){return this._scene._sectionPlanesState.getHash();}},{key:"drawLayer",value:function drawLayer(frameCtx,dataTextureLayer,renderPass){return;}},{key:"_allocate",value:function _allocate(){var scene=this._scene;var gl=scene.canvas.gl;this._program=new Program(gl,this._buildShader());if(this._program.errors){this.errors=this._program.errors;return;}var program=this._program;this._uRenderPass=program.getLocation("renderPass");this._uPositionsDecodeMatrix=program.getLocation("positionsDecodeMatrix");this._uWorldMatrix=program.getLocation("worldMatrix");this._uViewMatrix=program.getLocation("viewMatrix");this._uProjMatrix=program.getLocation("projMatrix");this._uSectionPlanes=[];for(var _i370=0,len=scene._sectionPlanesState.sectionPlanes.length;_i3700;var src=[];src.push("#version 300 es");src.push("// TrianglesDataTextureColorRenderer vertex shader");src.push("uniform int renderPass;");src.push("attribute vec3 position;");if(scene.entityOffsetsEnabled){src.push("attribute vec3 offset;");}src.push("attribute vec4 color;");src.push("attribute vec4 flags;");src.push("attribute vec4 flags2;");src.push("uniform mat4 worldMatrix;");src.push("uniform mat4 viewMatrix;");src.push("uniform mat4 projMatrix;");src.push("uniform mat4 positionsDecodeMatrix;");if(scene.logarithmicDepthBufferEnabled){src.push("uniform float logDepthBufFC;");if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("out float vFragDepth;");}src.push("bool isPerspectiveMatrix(mat4 m) {");src.push(" return (m[2][3] == - 1.0);");src.push("}");src.push("out float isPerspective;");}if(clipping){src.push("out vec4 vWorldPosition;");src.push("out vec4 vFlags2;");}src.push("void main(void) {");// flags.x = NOT_RENDERED | COLOR_OPAQUE | COLOR_TRANSPARENT // renderPass = COLOR_OPAQUE // Only opaque objects can be occluders src.push("if (int(flags.x) != renderPass) {");src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);");// Cull vertex -src.push(" } else {");src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");if(scene.entityOffsetsEnabled){src.push(" worldPosition.xyz = worldPosition.xyz + offset;");}src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");if(clipping){src.push(" vWorldPosition = worldPosition;");src.push(" vFlags2 = flags2;");}src.push("vec4 clipPos = projMatrix * viewPosition;");if(scene.logarithmicDepthBufferEnabled){if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("vFragDepth = 1.0 + clipPos.w;");}else{src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;");src.push("clipPos.z *= clipPos.w;");}src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");}src.push("gl_Position = clipPos;");src.push(" }");src.push("}");return src;}},{key:"_buildFragmentShader",value:function _buildFragmentShader(){var scene=this._scene;var sectionPlanesState=scene._sectionPlanesState;var clipping=sectionPlanesState.sectionPlanes.length>0;var src=[];src.push("// TrianglesDataTextureColorRenderer fragment shader");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("#extension GL_EXT_frag_depth : enable");}src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");src.push("precision highp float;");src.push("precision highp int;");src.push("#else");src.push("precision mediump float;");src.push("precision mediump int;");src.push("#endif");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("in float isPerspective;");src.push("uniform float logDepthBufFC;");src.push("in float vFragDepth;");}if(clipping){src.push("in vec4 vWorldPosition;");src.push("in vec4 vFlags2;");for(var _i371=0;_i371 0.0);");src.push(" if (clippable) {");src.push(" float dist = 0.0;");for(var _i372=0;_i372 0.0) { discard; }");src.push(" }");}if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");}src.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); ");// Occluders are blue +src.push(" } else {");src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");if(scene.entityOffsetsEnabled){src.push(" worldPosition.xyz = worldPosition.xyz + offset;");}src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");if(clipping){src.push(" vWorldPosition = worldPosition;");src.push(" vFlags2 = flags2;");}src.push("vec4 clipPos = projMatrix * viewPosition;");if(scene.logarithmicDepthBufferEnabled){src.push("vFragDepth = 1.0 + clipPos.w;");src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");}src.push("gl_Position = clipPos;");src.push(" }");src.push("}");return src;}},{key:"_buildFragmentShader",value:function _buildFragmentShader(){var scene=this._scene;var sectionPlanesState=scene._sectionPlanesState;var clipping=sectionPlanesState.sectionPlanes.length>0;var src=[];src.push('#version 300 es');src.push("// TrianglesDataTextureColorRenderer fragment shader");src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");src.push("precision highp float;");src.push("precision highp int;");src.push("#else");src.push("precision mediump float;");src.push("precision mediump int;");src.push("#endif");if(scene.logarithmicDepthBufferEnabled){src.push("in float isPerspective;");src.push("uniform float logDepthBufFC;");src.push("in float vFragDepth;");}if(clipping){src.push("in vec4 vWorldPosition;");src.push("in vec4 vFlags2;");for(var _i371=0;_i371 0.0);");src.push(" if (clippable) {");src.push(" float dist = 0.0;");for(var _i372=0;_i372 0.0) { discard; }");src.push(" }");}if(scene.logarithmicDepthBufferEnabled){src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");}src.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); ");// Occluders are blue src.push("}");return src;}},{key:"webglContextRestored",value:function webglContextRestored(){this._program=null;}},{key:"destroy",value:function destroy(){if(this._program){this._program.destroy();}this._program=null;}}]);return TrianglesDataTextureOcclusionRenderer;}();var tempVec3a$a=math.vec3();var tempVec3b$6=math.vec3();var tempVec3c$5=math.vec3();var tempVec3d$2=math.vec3();var tempMat4a$1=math.mat4();/** * @private */var TrianglesDataTextureDepthRenderer=/*#__PURE__*/function(){function TrianglesDataTextureDepthRenderer(scene){_classCallCheck(this,TrianglesDataTextureDepthRenderer);this._scene=scene;this._allocate();this._hash=this._getHash();}_createClass(TrianglesDataTextureDepthRenderer,[{key:"getValid",value:function getValid(){return this._hash===this._getHash();}},{key:"_getHash",value:function _getHash(){return this._scene._sectionPlanesState.getHash();}},{key:"drawLayer",value:function drawLayer(frameCtx,dataTextureLayer,renderPass){var scene=this._scene;var camera=scene.camera;var model=dataTextureLayer.model;var gl=scene.canvas.gl;var state=dataTextureLayer._state;var textureState=state.textureState;var origin=dataTextureLayer._state.origin;var position=model.position,rotationMatrix=model.rotationMatrix,rotationMatrixConjugate=model.rotationMatrixConjugate;if(!this._program){this._allocate();if(this.errors){return;}}if(frameCtx.lastProgramId!==this._program.id){frameCtx.lastProgramId=this._program.id;this._bindProgram(frameCtx,state);}textureState.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var rtcViewMatrix;var rtcCameraEye;var gotOrigin=origin[0]!==0||origin[1]!==0||origin[2]!==0;var gotPosition=position[0]!==0||position[1]!==0||position[2]!==0;if(gotOrigin||gotPosition){var rtcOrigin=tempVec3a$a;if(gotOrigin){var rotatedOrigin=math.transformPoint3(rotationMatrix,origin,tempVec3b$6);rtcOrigin[0]=rotatedOrigin[0];rtcOrigin[1]=rotatedOrigin[1];rtcOrigin[2]=rotatedOrigin[2];}else{rtcOrigin[0]=0;rtcOrigin[1]=0;rtcOrigin[2]=0;}rtcOrigin[0]+=position[0];rtcOrigin[1]+=position[1];rtcOrigin[2]+=position[2];rtcViewMatrix=createRTCViewMat(camera.viewMatrix,rtcOrigin,tempMat4a$1);rtcCameraEye=tempVec3c$5;rtcCameraEye[0]=camera.eye[0]-rtcOrigin[0];rtcCameraEye[1]=camera.eye[1]-rtcOrigin[1];rtcCameraEye[2]=camera.eye[2]-rtcOrigin[2];}else{rtcViewMatrix=camera.viewMatrix;rtcCameraEye=camera.eye;}gl.uniformMatrix4fv(this._uSceneModelWorldMatrix,false,rotationMatrixConjugate);gl.uniformMatrix4fv(this._uViewMatrix,false,rtcViewMatrix);gl.uniformMatrix4fv(this._uProjMatrix,false,camera.projMatrix);gl.uniform3fv(this._uCameraEyeRtc,rtcCameraEye);gl.uniform1i(this._uRenderPass,renderPass);if(scene.logarithmicDepthBufferEnabled){var logDepthBufFC=2.0/(Math.log(frameCtx.pickZFar+1.0)/Math.LN2);gl.uniform1f(this._uLogDepthBufFC,logDepthBufFC);}var numSectionPlanes=scene._sectionPlanesState.sectionPlanes.length;if(numSectionPlanes>0){var sectionPlanes=scene._sectionPlanesState.sectionPlanes;var baseIndex=dataTextureLayer.layerIndex*numSectionPlanes;var renderFlags=model.renderFlags;for(var sectionPlaneIndex=0;sectionPlaneIndex0){textureState.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8// 8 bits indices @@ -17452,7 +17452,7 @@ src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndex this._aFlags.bindArrayBuffer(state.flagsBuf);if(this._aFlags2){this._aFlags2.bindArrayBuffer(state.flags2Buf);}state.indicesBuf.bind();gl.drawElements(gl.TRIANGLES,state.indicesBuf.numItems,state.indicesBuf.itemType,0);}},{key:"_allocate",value:function _allocate(){var scene=this._scene;var gl=scene.canvas.gl;this._program=new Program(gl,this._buildShader());if(this._program.errors){this.errors=this._program.errors;return;}var program=this._program;this._uRenderPass=program.getLocation("renderPass");this._uPositionsDecodeMatrix=program.getLocation("positionsDecodeMatrix");this._uWorldMatrix=program.getLocation("worldMatrix");this._uWorldNormalMatrix=program.getLocation("worldNormalMatrix");this._uViewMatrix=program.getLocation("viewMatrix");this._uViewNormalMatrix=program.getLocation("viewNormalMatrix");this._uProjMatrix=program.getLocation("projMatrix");this._uSectionPlanes=[];for(var _i376=0,len=scene._sectionPlanesState.sectionPlanes.length;_i3760;var src=[];src.push("// Batched geometry normals vertex shader");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("#extension GL_EXT_frag_depth : enable");}src.push("uniform int renderPass;");src.push("attribute vec3 position;");if(scene.entityOffsetsEnabled){src.push("attribute vec3 offset;");}src.push("attribute vec3 normal;");src.push("attribute vec4 color;");src.push("attribute vec4 flags;");src.push("attribute vec4 flags2;");src.push("uniform mat4 worldMatrix;");src.push("uniform mat4 worldNormalMatrix;");src.push("uniform mat4 viewMatrix;");src.push("uniform mat4 projMatrix;");src.push("uniform mat4 viewNormalMatrix;");src.push("uniform mat4 positionsDecodeMatrix;");if(scene.logarithmicDepthBufferEnabled){src.push("uniform float logDepthBufFC;");if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("out float vFragDepth;");}src.push("bool isPerspectiveMatrix(mat4 m) {");src.push(" return (m[2][3] == - 1.0);");src.push("}");src.push("varying float isPerspective;");}src.push("vec3 octDecode(vec2 oct) {");src.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));");src.push(" if (v.z < 0.0) {");src.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);");src.push(" }");src.push(" return normalize(v);");src.push("}");if(clipping){src.push("out vec4 vWorldPosition;");src.push("out vec4 vFlags2;");}src.push("out vec3 vViewNormal;");src.push("void main(void) {");// flags.x = NOT_RENDERED | COLOR_OPAQUE | COLOR_TRANSPARENT // renderPass = COLOR_OPAQUE -src.push("if (int(flags.x) != renderPass) {");src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);");src.push(" } else {");src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");if(scene.entityOffsetsEnabled){src.push(" worldPosition.xyz = worldPosition.xyz + offset;");}src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");src.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); ");src.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);");if(clipping){src.push(" vWorldPosition = worldPosition;");src.push(" vFlags2 = flags2;");}src.push(" vViewNormal = viewNormal;");src.push("vec4 clipPos = projMatrix * viewPosition;");if(scene.logarithmicDepthBufferEnabled){if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("vFragDepth = 1.0 + clipPos.w;");}else{src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;");src.push("clipPos.z *= clipPos.w;");}src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");}src.push("gl_Position = clipPos;");src.push(" }");src.push("}");return src;}},{key:"_buildFragmentShader",value:function _buildFragmentShader(){var scene=this._scene;var sectionPlanesState=scene._sectionPlanesState;var clipping=sectionPlanesState.sectionPlanes.length>0;var src=[];src.push("// Batched geometry normals fragment shader");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("#extension GL_EXT_frag_depth : enable");}src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");src.push("precision highp float;");src.push("precision highp int;");src.push("#else");src.push("precision mediump float;");src.push("precision mediump int;");src.push("#endif");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("in float isPerspective;");src.push("uniform float logDepthBufFC;");src.push("in float vFragDepth;");}if(clipping){src.push("in vec4 vWorldPosition;");src.push("in vec4 vFlags2;");for(var _i377=0;_i377 0.0);");src.push(" if (clippable) {");src.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }");src.push(" }");}if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");}src.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); ");src.push("}");return src;}},{key:"webglContextRestored",value:function webglContextRestored(){this._program=null;}},{key:"destroy",value:function destroy(){if(this._program){this._program.destroy();}this._program=null;}}]);return TrianglesDataTextureNormalsRenderer;}();var tempVec3a$8=math.vec3();/** +src.push("if (int(flags.x) != renderPass) {");src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);");src.push(" } else {");src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");if(scene.entityOffsetsEnabled){src.push(" worldPosition.xyz = worldPosition.xyz + offset;");}src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");src.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); ");src.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);");if(clipping){src.push(" vWorldPosition = worldPosition;");src.push(" vFlags2 = flags2;");}src.push(" vViewNormal = viewNormal;");src.push("vec4 clipPos = projMatrix * viewPosition;");if(scene.logarithmicDepthBufferEnabled){if(WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("vFragDepth = 1.0 + clipPos.w;");}else{src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;");src.push("clipPos.z *= clipPos.w;");}src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");}src.push("gl_Position = clipPos;");src.push(" }");src.push("}");return src;}},{key:"_buildFragmentShader",value:function _buildFragmentShader(){var scene=this._scene;var sectionPlanesState=scene._sectionPlanesState;var clipping=sectionPlanesState.sectionPlanes.length>0;var src=[];src.push("#version 300 es");src.push("// Batched geometry normals fragment shader");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("#extension GL_EXT_frag_depth : enable");}src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");src.push("precision highp float;");src.push("precision highp int;");src.push("#else");src.push("precision mediump float;");src.push("precision mediump int;");src.push("#endif");if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push("in float isPerspective;");src.push("uniform float logDepthBufFC;");src.push("in float vFragDepth;");}if(clipping){src.push("in vec4 vWorldPosition;");src.push("in vec4 vFlags2;");for(var _i377=0;_i377 0.0);");src.push(" if (clippable) {");src.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }");src.push(" }");}if(scene.logarithmicDepthBufferEnabled&&WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]){src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");}src.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); ");src.push("}");return src;}},{key:"webglContextRestored",value:function webglContextRestored(){this._program=null;}},{key:"destroy",value:function destroy(){if(this._program){this._program.destroy();}this._program=null;}}]);return TrianglesDataTextureNormalsRenderer;}();var tempVec3a$8=math.vec3();/** * @private */var TrianglesDataTexturePickNormalsFlatRenderer=/*#__PURE__*/function(){function TrianglesDataTexturePickNormalsFlatRenderer(scene){_classCallCheck(this,TrianglesDataTexturePickNormalsFlatRenderer);this._scene=scene;this._hash=this._getHash();this._allocate();}_createClass(TrianglesDataTexturePickNormalsFlatRenderer,[{key:"getValid",value:function getValid(){return this._hash===this._getHash();}},{key:"_getHash",value:function _getHash(){return this._scene._sectionPlanesState.getHash();}},{key:"drawLayer",value:function drawLayer(frameCtx,dataTextureLayer,renderPass){var model=dataTextureLayer.model;var scene=model.scene;var camera=scene.camera;var gl=scene.canvas.gl;var state=dataTextureLayer._state;var origin=dataTextureLayer._state.origin;if(!this._program){this._allocate(dataTextureLayer);}if(frameCtx.lastProgramId!==this._program.id){frameCtx.lastProgramId=this._program.id;this._bindProgram();}this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function bind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,state.texturePerObjectIdPositionsDecodeMatrix);return true;},unbind:function unbind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,null);}},1);this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function bind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,state.texturePerVertexIdCoordinates);return true;},unbind:function unbind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,null);}},2);this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function bind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,state.texturePerObjectIdColorsAndFlags);return true;},unbind:function unbind(unit){gl.activeTexture(gl["TEXTURE"+unit]);gl.bindTexture(gl.TEXTURE_2D,null);}},3);gl.uniform1i(this._uRenderPass,renderPass);gl.uniform1i(this._uPickInvisible,frameCtx.pickInvisible);gl.uniformMatrix4fv(this._uWorldMatrix,false,model.worldMatrix);var pickViewMatrix=frameCtx.pickViewMatrix||camera.viewMatrix;var viewMatrix=origin?createRTCViewMat(pickViewMatrix,origin):pickViewMatrix;gl.uniformMatrix4fv(this._uViewMatrix,false,viewMatrix);gl.uniformMatrix4fv(this._uProjMatrix,false,frameCtx.pickProjMatrix);if(scene.logarithmicDepthBufferEnabled){var logDepthBufFC=2.0/(Math.log(camera.project.far+1.0)/Math.LN2);// TODO: Far should be from projection matrix? gl.uniform1f(this._uLogDepthBufFC,logDepthBufFC);}var numSectionPlanes=scene._sectionPlanesState.sectionPlanes.length;if(numSectionPlanes>0){var sectionPlanes=scene._sectionPlanesState.sectionPlanes;var baseIndex=dataTextureLayer.layerIndex*numSectionPlanes;var renderFlags=model.renderFlags;for(var sectionPlaneIndex=0;sectionPlaneIndex{for(let s=0,n=e.meshI /** * @author https://github.com/tmarti, with support from https://tribia.com/ * @license MIT - **/let $l=null;function eo(e,t){const s=3*e,n=3*t;let i,a,r,l,o,c;const u=Math.min(i=$l[s],a=$l[s+1],r=$l[s+2]),h=Math.min(l=$l[n],o=$l[n+1],c=$l[n+2]);if(u!==h)return u-h;const p=Math.max(i,a,r),A=Math.max(l,o,c);return p!==A?p-A:0}let to=null;function so(e,t){let s=to[2*e]-to[2*t];return 0!==s?s:to[2*e+1]-to[2*t+1]}function no(e,t,s=!1){const n=e.positionsCompressed||[],i=function(e,t){const s=new Int32Array(e.length/3);for(let e=0,t=s.length;e>t;s.sort(eo);const n=new Int32Array(e.length);for(let t=0,i=s.length;te[t+1]){let s=e[t];e[t]=e[t+1],e[t+1]=s}to=new Int32Array(e),t.sort(so);const s=new Int32Array(e.length);for(let n=0,i=t.length;nt){let s=e;e=t,t=s}function s(s,n){return s!==e?e-s:n!==t?t-n:0}let n=0,i=(a.length>>1)-1;for(;n<=i;){const e=i+n>>1,t=s(a[2*e],a[2*e+1]);if(t>0)n=e+1;else{if(!(t<0))return e;i=e-1}}return-n-1}const l=new Int32Array(a.length/2);l.fill(0);const o=n.length/3;if(o>8*(1<p.maxNumPositions&&(p=h()),p.bucketNumber>8)return[e];let d;-1===c[o]&&(c[o]=p.numPositions++,p.positionsCompressed.push(n[3*o]),p.positionsCompressed.push(n[3*o+1]),p.positionsCompressed.push(n[3*o+2])),-1===c[u]&&(c[u]=p.numPositions++,p.positionsCompressed.push(n[3*u]),p.positionsCompressed.push(n[3*u+1]),p.positionsCompressed.push(n[3*u+2])),-1===c[A]&&(c[A]=p.numPositions++,p.positionsCompressed.push(n[3*A]),p.positionsCompressed.push(n[3*A+1]),p.positionsCompressed.push(n[3*A+2])),p.indices.push(c[o]),p.indices.push(c[u]),p.indices.push(c[A]),(d=r(o,u))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(o,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(u,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]]))}const A=t/8*2,d=t/8,f=2*n.length+(i.length+a.length)*A;let I=0,y=-n.length/3;return u.forEach((e=>{I+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*d,y+=e.positionsCompressed.length/3})),I>f?[e]:(s&&function(e,t){const s={},n={};let i=0;e.forEach((e=>{const t=e.indices,a=e.edgeIndices,r=e.positionsCompressed;for(let e=0,n=t.length;e0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl,s=e._lightsState;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uLightAmbient=n.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];const i=s.lights;let a;for(let e=0,t=i.length;e0;let i;const a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),e.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(let e=0,t=s.lights.length;e> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(let e=0,t=s.lights.length;e0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),this._withSAO&&(n.push("uniform sampler2D uOcclusionTexture;"),n.push("uniform vec4 uSAOParams;"),n.push("const float packUpscale = 256. / 255.;"),n.push("const float unpackDownScale = 255. / 256.;"),n.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),n.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),n.push("float unpackRGBToFloat( const in vec4 v ) {"),n.push(" return dot( v, unPackFactors );"),n.push("}")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(n.push(" float viewportWidth = uSAOParams[0];"),n.push(" float viewportHeight = uSAOParams[1];"),n.push(" float blendCutoff = uSAOParams[2];"),n.push(" float blendFactor = uSAOParams[3];"),n.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),n.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),n.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const ho=new Float32Array([1,1,1]),po=h.vec3(),Ao=h.vec3(),fo=h.vec3(),Io=h.vec3(),yo=h.mat4();class mo{constructor(e,t){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=a,d=i.viewMatrix;if(!this._program&&(this._allocate(),this.errors))return;let f,I;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),c||0!==u[0]||0!==u[1]||0!==u[2]){const e=po;if(c){const t=Ao;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,yo),I=fo,I[0]=i.eye[0]-e[0],I[1]=i.eye[1]-e[1],I[2]=i.eye[2]-e[2]}else f=d,I=i.eye;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s===Ri.SILHOUETTE_XRAYED){const e=n.xrayMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.SILHOUETTE_HIGHLIGHTED){const e=n.highlightMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.SILHOUETTE_SELECTED){const e=n.selectedMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,ho);if(n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=n._sectionPlanesState.sectionPlanes.length;if(y>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture silhouette vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.y) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("} else {"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = color;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const vo=new Float32Array([0,0,0,1]),wo=h.vec3(),go=h.vec3();h.vec3();const To=h.vec3(),Eo=h.mat4();class bo{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=wo;if(I){const t=go;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,Eo)}else f=d;if(r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s===Ri.EDGES_XRAYED){const e=i.xrayMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.EDGES_HIGHLIGHTED){const e=i.highlightMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.EDGES_SELECTED){const e=i.selectedMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,vo);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vColor = vec4(color.r, color.g, color.b, color.a);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Do=h.vec3(),Po=h.vec3(),Ro=h.vec3(),Co=h.mat4();class _o{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Do;if(I){const t=Po;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,Co)}else f=d;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vec4 rgb = vec4(color.rgba);"),s.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Bo=h.vec3(),Oo=h.vec3(),So=h.vec3(),No=h.mat4();class xo{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(t),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n;let d,f;o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Bo;if(I){const t=h.transformPoint3(p,c,Oo);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=B(a.viewMatrix,e,No),f=So,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=a.viewMatrix,f=a.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry picking vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("smooth out vec4 vWorldPosition;"),s.push("flat out uvec4 vFlags2;")),s.push("out vec4 vPickColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry picking fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outPickColor = vPickColor; "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Lo=h.vec3(),Mo=h.vec3(),Fo=h.vec3(),Ho=h.vec3(),Uo=h.mat4();class Go{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=e.pickViewMatrix||a.viewMatrix;let f,I;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),a.eye,c||0!==u[0]||0!==u[1]||0!==u[2]){const t=Lo;if(c){const e=Mo;h.transformPoint3(p,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],f=B(d,t,Uo),I=Fo,I[0]=a.eye[0]-t[0],I[1]=a.eye[1]-t[1],I[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else f=d,I=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniform1f(this._uPickZNear,e.pickZNear),r.uniform1f(this._uPickZFar,e.pickZFar),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=i._sectionPlanesState.sectionPlanes.length;if(y>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick depth vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform float pickZNear;"),n.push("uniform float pickZFar;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),n.push(" outPackedDepth = packDepth(zNormalizedDepth); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Vo=h.vec3(),jo=h.vec3(),ko=h.vec3(),Qo=h.vec3(),Wo=h.vec3(),zo=h.mat4();class Ko{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Vo;let y,m;I[0]=h.safeInv(d[3]-d[0])*h.MAX_INT,I[1]=h.safeInv(d[4]-d[1])*h.MAX_INT,I[2]=h.safeInv(d[5]-d[2])*h.MAX_INT,e.snapPickCoordinateScale[0]=h.safeInv(I[0]),e.snapPickCoordinateScale[1]=h.safeInv(I[1]),e.snapPickCoordinateScale[2]=h.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=jo;if(v){const e=h.transformPoint3(p,c,ko);t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=B(f,t,zo),m=Qo,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this.uVectorA,e.snapVectorA),r.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(T,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(T,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(T,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uSnapVectorA;"),s.push("uniform vec2 uSnapInvVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),s.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.xyzw *= tmp;"),s.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("gl_PointSize = 1.0;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Yo=h.vec3(),Xo=h.vec3(),qo=h.vec3(),Jo=h.vec3(),Zo=h.vec3(),$o=h.mat4();class ec{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Yo;let y,m;I[0]=h.safeInv(d[3]-d[0])*h.MAX_INT,I[1]=h.safeInv(d[4]-d[1])*h.MAX_INT,I[2]=h.safeInv(d[5]-d[2])*h.MAX_INT,e.snapPickCoordinateScale[0]=h.safeInv(I[0]),e.snapPickCoordinateScale[1]=h.safeInv(I[1]),e.snapPickCoordinateScale[2]=h.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=Xo;if(v){const e=qo;h.transformPoint3(p,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=B(f,t,$o),m=Jo,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this._uVectorA,e.snapVectorA),r.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uVectorAB;"),s.push("uniform vec2 uInverseVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),s.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push(" if (isPerspectiveMatrix(projMatrix)) {"),s.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" } else {"),s.push(" if (viewNormal.z < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" }"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.z += 0.0001;"),s.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const tc=h.vec3();class sc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);let u=a.eye;e.pickViewMatrix&&(o.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),u=e.pickOrigin||u);const h=[u[0]-c[0],u[1]-c[1],u[2]-c[2]];if(r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uRenderPass,s),r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const p=i._sectionPlanesState.sectionPlanes.length;if(p>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*p,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureCameraMatrices;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec3 vWorldNormal;"),s.push("void main(void) {"),s.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),s.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),s.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("}"),s.push("}"),s.push("normal = -normal;"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vWorldNormal = normal.xyz;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.w;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}h.vec3(),h.vec3(),h.vec3(),h.vec3(),h.mat4();class nc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("// TrianglesDataTextureColorRenderer vertex shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(ce.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("// TrianglesDataTextureColorRenderer fragment shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0;e 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const ic=h.vec3(),ac=h.vec3(),rc=h.vec3(),lc=h.vec3(),oc=h.mat4();class cc{constructor(e){this._scene=e,this._allocate(),this._hash=this._getHash()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=a;if(!this._program&&(this._allocate(),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=ic;if(I){const t=h.transformPoint3(p,c,ac);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=B(i.viewMatrix,e,oc),f=rc,f[0]=i.eye[0]-e[0],f[1]=i.eye[1]-e[1],f[2]=i.eye[2]-e[2]}else d=i.viewMatrix,f=i.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const m=n._sectionPlanesState.sectionPlanes.length;if(m>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture draw vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out highp vec2 vHighPrecisionZW;"),s&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("vHighPrecisionZW = gl_Position.zw;"),n.push("}"),n.push("}"),n}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),n.push("in highp vec2 vHighPrecisionZW;"),n.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),n.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const uc=h.vec3(),hc=h.vec3(),pc=h.vec3(),Ac=h.vec3(),dc=h.mat4();class fc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin,{position:c,rotationMatrix:u,rotationMatrixConjugate:p}=n,A=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));const I=0!==o[0]||0!==o[1]||0!==o[2],y=0!==c[0]||0!==c[1]||0!==c[2];if(I||y){const e=uc;if(I){const t=hc;h.transformPoint3(u,o,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=c[0],e[1]+=c[1],e[2]+=c[2],d=B(A,e,dc),f=pc,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=A,f=a.eye;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,p),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),r.uniformMatrix4fv(this._uWorldNormalMatrix,!1,n.worldNormalMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0,s=[];return s.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec3 normal;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 worldNormalMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 viewNormalMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("varying float isPerspective;")),s.push("vec3 octDecode(vec2 oct) {"),s.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),s.push(" if (v.z < 0.0) {"),s.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),s.push(" }"),s.push(" return normalize(v);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("out vec3 vViewNormal;"),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),s.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push(" vViewNormal = viewNormal;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(ce.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Ic=h.vec3();class yc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerVertexIdCoordinates),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},3),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uWorldMatrix,!1,n.worldMatrix);const c=e.pickViewMatrix||a.viewMatrix,u=o?B(c,o):c;if(r.uniformMatrix4fv(this._uViewMatrix,!1,u),r.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const h=i._sectionPlanesState.sectionPlanes.length;if(h>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*h,a=n.renderFlags;for(let t=0;t0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick flat normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),s.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform isampler2D uTexturePerPolygonIdNormals;"),s.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("out vec4 vWorldPosition;"),t&&s.push("out int vFlags2;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_normal_index = polygonIndex & 4095;"),s.push("int v_normal_index = polygonIndex >> 12;"),s.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),s.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),s.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),s.push("int objectIndex;"),s.push("if (((polygonIndex >> 3) % 2) == 0) {"),s.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),s.push("} else {"),s.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),s.push("}"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("int h_index = polygonIndex & 4095;"),s.push("int v_index = polygonIndex >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),s.push("int vertexNumber = gl_VertexID % 3;"),s.push("vec3 position;"),s.push("if (vertexNumber == 0) position = position1;"),s.push("else if (vertexNumber == 1) position = position2;"),s.push("else position = position3;"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vWorldPosition = worldPosition;"),t&&s.push(" vFlags2 = flags2.r;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals fragment shader"),n.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("in vec4 vWorldPosition;"),s){n.push("in int vFlags2;");for(var i=0;i 0;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),n.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),n.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),n.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}class mc{constructor(e){this._scene=e}_compile(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}get colorRenderer(){return this._colorRenderer||(this._colorRenderer=new uo(this._scene,!1)),this._colorRenderer}get colorRendererWithSAO(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new uo(this._scene,!0)),this._colorRendererWithSAO}get colorQualityRendererWithSAO(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}get silhouetteRenderer(){return this._silhouetteRenderer||(this._silhouetteRenderer=new mo(this._scene)),this._silhouetteRenderer}get depthRenderer(){return this._depthRenderer||(this._depthRenderer=new cc(this._scene)),this._depthRenderer}get normalsRenderer(){return this._normalsRenderer||(this._normalsRenderer=new fc(this._scene)),this._normalsRenderer}get edgesRenderer(){return this._edgesRenderer||(this._edgesRenderer=new bo(this._scene)),this._edgesRenderer}get edgesColorRenderer(){return this._edgesColorRenderer||(this._edgesColorRenderer=new _o(this._scene)),this._edgesColorRenderer}get pickMeshRenderer(){return this._pickMeshRenderer||(this._pickMeshRenderer=new xo(this._scene)),this._pickMeshRenderer}get pickNormalsRenderer(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new sc(this._scene)),this._pickNormalsRenderer}get pickNormalsFlatRenderer(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new yc(this._scene)),this._pickNormalsFlatRenderer}get pickDepthRenderer(){return this._pickDepthRenderer||(this._pickDepthRenderer=new Go(this._scene)),this._pickDepthRenderer}get vertexDepthRenderer(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new Ko(this._scene)),this._vertexDepthRenderer}get snapDepthBufInitRenderer(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new ec(this._scene)),this._snapDepthBufInitRenderer}get occlusionRenderer(){return this._occlusionRenderer||(this._occlusionRenderer=new nc(this._scene)),this._occlusionRenderer}_destroy(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}const vc={};class wc{constructor(){this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]}}class gc{constructor(){this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}finalize(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}bindCommonTextures(e,t,s,n,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,s,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,n,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}bindPickCameraTexture(e,t){}bindTriangleIndicesTextures(e,t,s,n){this.indicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.indicesPerBitnessTextures[n].bindTexture(e,s,7)}bindEdgeIndicesTextures(e,t,s,n){this.edgeIndicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[n].bindTexture(e,s,7)}}class Tc{constructor(e,t,s,n,i=null){this._gl=e,this._texture=t,this._textureWidth=s,this._textureHeight=n,this._textureData=i}bindTexture(e,t,s){return e.bindTexture(t,this,s)}bind(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}unbind(e){}}const Ec={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(Ec,null,4));let e=0;Object.keys(Ec).forEach((t=>{t.startsWith("size")&&(e+=Ec[t])})),console.log(`Total size ${e} bytes (${(e/1e3/1e3).toFixed(2)} MB)`),console.log(`Avg bytes / triangle: ${(e/Ec.totalPolygons).toFixed(2)}`);let t={};Object.keys(Ec).forEach((s=>{s.startsWith("size")&&(t[s]=`${(Ec[s]/e*100).toFixed(2)} % of total`)})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};class bc{disableBindedTextureFiltering(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}generateCameraDataTexture(e,t,s,n){const i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const a=new Tc(e,i,4,3);let r=!0;a.updateViewMatrix=(s,i)=>{e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?B(s,n):s)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};const l=()=>{r&&(r=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(()=>r=!0)),s.on("rendering",l),l(),a}generatePickCameraDataTexture(e,t,s){const n=e.createTexture();e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const i=new Tc(e,n,4,3);return i.updateViewMatrix=(n,a)=>{e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(s?B(n,s):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}generateModelTexture(e,t){const s=e.createTexture();return e.bindTexture(e.TEXTURE_2D,s),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,s,4,2)}generateTextureForColorsAndFlags(e,t,s,n,i,a,r){const l=t.length;this.numPortions=l;const o=4096,c=Math.ceil(l/512);if(0===c)throw"texture height===0";const u=new Uint8Array(16384*c);Ec.sizeDataColorsAndFlags+=u.byteLength,Ec.numberOfTextures++;for(let e=0;e>24&255,n[e]>>16&255,n[e]>>8&255,255&n[e]],32*e+16),u.set([i[e]>>24&255,i[e]>>16&255,i[e]>>8&255,255&i[e]],32*e+20),u.set([a[e]>>24&255,a[e]>>16&255,a[e]>>8&255,255&a[e]],32*e+24),u.set([r[e]?1:0,0,0,0],32*e+28);const h=e.createTexture();return e.bindTexture(e.TEXTURE_2D,h),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,o,c),e.texSubImage2D(e.TEXTURE_2D,0,0,0,o,c,e.RGBA_INTEGER,e.UNSIGNED_BYTE,u,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,h,o,c,u)}generateTextureForObjectOffsets(e,t){const s=512,n=Math.ceil(t/s);if(0===n)throw"texture height===0";const i=new Float32Array(1536*n).fill(0);Ec.sizeDataTextureOffsets+=i.byteLength,Ec.numberOfTextures++;const a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,s,n),e.texSubImage2D(e.TEXTURE_2D,0,0,0,s,n,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,a,s,n,i)}generateTextureForPositionsDecodeMatrices(e,t,s){const n=t.length;if(0===n)throw"num decode+entity matrices===0";const i=2048,a=Math.ceil(n/512),r=new Float32Array(8192*a);Ec.sizeDataPositionDecodeMatrices+=r.byteLength,Ec.numberOfTextures++;const l=h.mat4();for(let e=0;e{s._compile()})),e.on("destroyed",(()=>{delete vc[t],s._destroy()}))),s}(e.scene),this.model=e,this._buffer=new wc,this._dataTextureState=new gc,this._dataTextureGenerator=new bc,this._state=new je({origin:h.vec3(t.origin),metallicRoughnessBuf:null,positionsDecodeMatrix:h.mat4(),textureState:this._dataTextureState,numIndices8Bits:0,numIndices16Bits:0,numIndices32Bits:0,numEdgeIndices8Bits:0,numEdgeIndices16Bits:0,numEdgeIndices32Bits:0,numVertices:0}),this._numPortions=0,this._numVisibleLayerPortions=0,this._numTransparentLayerPortions=0,this._numXRayedLayerPortions=0,this._numSelectedLayerPortions=0,this._numHighlightedLayerPortions=0,this._numClippableLayerPortions=0,this._numEdgesLayerPortions=0,this._numPickableLayerPortions=0,this._numCulledLayerPortions=0,this._subPortions=[],this._portionToSubPortionsMap=[],this._bucketGeometries={},this.aabb=h.collapseAABB3(),this._numUpdatesInFrame=0,this._finalized=!1}canCreatePortion(e){if(this._finalized)throw"Already finalized";const t=e.buckets.length;this._numPortions+t>65536&&Ec.cannotCreatePortion.because10BitsObjectId++;let s=this._numPortions+t<=65536;const n=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#0`:`${e.id}#0`;if(!this._bucketGeometries[n]){const t=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits);let n=0,i=0;e.buckets.forEach((e=>{n+=e.positionsCompressed.length/3,i+=e.indices.length/3})),(this._state.numVertices+n>16777216||t+i>16777216)&&Ec.cannotCreatePortion.becauseTextureSize++,s&&=this._state.numVertices+n<=16777216&&t+i<=16777216}return s}createPortion(e){if(this._finalized)throw"Already finalized";const t=[],s=e.worldAABB;e.buckets.forEach(((n,i)=>{const a=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#${i}`:`${e.id}#${i}`;let r=this._bucketGeometries[a];r||(r=this._createBucketGeometry(e,n),this._bucketGeometries[a]=r);const l=h.collapseAABB3(Bc),o=this._createSubPortion(e,r,n,l);h.expandAABB3(s,l),t.push(o)}));const n=this._state.origin;0===n[0]&&0===n[1]&&0===n[2]||(s[0]+=n[0],s[1]+=n[1],s[2]+=n[2],s[3]+=n[0],s[4]+=n[1],s[5]+=n[2]),h.expandAABB3(this.aabb,s);const i=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(t),this.model.numPortions++,i}_createBucketGeometry(e,t){if(t.indices){const e=8*Math.ceil(t.indices.length/3/8)*3;Ec.overheadSizeAlignementIndices+=2*(e-t.indices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.indices),t.indices=s}if(t.edgeIndices){const e=8*Math.ceil(t.edgeIndices.length/2/8)*2;Ec.overheadSizeAlignementEdgeIndices+=2*(e-t.edgeIndices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.edgeIndices),t.edgeIndices=s}const s=t.positionsCompressed,n=t.indices,i=t.edgeIndices,a=this._buffer,r=a.positionsCompressed.length/3,l=s.length/3;for(let e=0,t=s.length;e0){let e,s=3*t.numTriangles;t.numVertices<=256?(e=u.perTriangleNumberPortionId8Bits,p.numIndices8Bits+=s,Ec.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(e=u.perTriangleNumberPortionId16Bits,p.numIndices16Bits+=s,Ec.totalPolygons16Bits+=t.numTriangles):(e=u.perTriangleNumberPortionId32Bits,p.numIndices32Bits+=s,Ec.totalPolygons32Bits+=t.numTriangles),Ec.totalPolygons+=t.numTriangles;for(let s=0;s0){let e,s=2*t.numEdges;t.numVertices<=256?(e=u.perEdgeNumberPortionId8Bits,p.numEdgeIndices8Bits+=s,Ec.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(e=u.perEdgeNumberPortionId16Bits,p.numEdgeIndices16Bits+=s,Ec.totalEdges16Bits+=t.numEdges):(e=u.perEdgeNumberPortionId32Bits,p.numEdgeIndices32Bits+=s,Ec.totalEdges32Bits+=t.numEdges),Ec.totalEdges+=t.numEdges;for(let s=0;s0&&(t.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId8Bits)),n.perEdgeNumberPortionId16Bits.length>0&&(t.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId16Bits)),n.perEdgeNumberPortionId32Bits.length>0&&(t.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId32Bits)),n.indices8Bits.length>0&&(t.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(s,n.indices8Bits)),n.indices16Bits.length>0&&(t.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(s,n.indices16Bits)),n.indices32Bits.length>0&&(t.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(s,n.indices32Bits)),n.edgeIndices8Bits.length>0&&(t.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(s,n.edgeIndices8Bits)),n.edgeIndices16Bits.length>0&&(t.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(s,n.edgeIndices16Bits)),n.edgeIndices32Bits.length>0&&(t.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(s,n.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(s,this.model)),t.textureModelMatrices=this.model._modelMatricesTexture,t.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),t.textureCameraMatrices=t.cameraTexture,t.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),t.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(()=>{this._deferredSetFlagsDirty&&this._uploadDeferredFlags(),this._numUpdatesInFrame=0}))}isEmpty(){return 0===this._numPortions}initFlags(e,t,s){t&x&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&G&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&U&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&V&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&F&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&j&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&M&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&L&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),s&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,s,true),this._setFlags2(e,t,true)}flushInitFlags(){this._setDeferredFlags(),this._setDeferredFlags2()}setVisible(e,t,s){if(!this._finalized)throw"Not finalized";t&x?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,s)}setHighlighted(e,t,s){if(!this._finalized)throw"Not finalized";t&G?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,s)}setXRayed(e,t,s){if(!this._finalized)throw"Not finalized";t&U?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,s)}setSelected(e,t,s){if(!this._finalized)throw"Not finalized";t&V?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,s)}setEdges(e,t,s){if(!this._finalized)throw"Not finalized";t&j?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,s)}setClippable(e,t){if(!this._finalized)throw"Not finalized";t&F?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}_beginDeferredFlags(){this._deferredSetFlagsActive=!0}_uploadDeferredFlags(){if(this._deferredSetFlagsActive=!1,!this._deferredSetFlagsDirty)return;this._deferredSetFlagsDirty=!1;const e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}setCulled(e,t,s){if(!this._finalized)throw"Not finalized";t&L?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,s)}setCollidable(e,t){if(!this._finalized)throw"Not finalized"}setPickable(e,t,s){if(!this._finalized)throw"Not finalized";t&M?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,s)}setColor(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdColorsAndFlags._texture),n.texSubImage2D(n.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,n.RGBA_INTEGER,n.UNSIGNED_BYTE,Cc))}setTransparent(e,t,s){s?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,s)}_setFlags(e,t,s,n=!1){const i=this._portionToSubPortionsMap[e];for(let e=0,a=i.length;e=10&&this._beginDeferredFlags(),d.bindTexture(d.TEXTURE_2D,A.texturePerObjectIdColorsAndFlags._texture),d.texSubImage2D(d.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,d.RGBA_INTEGER,d.UNSIGNED_BYTE,Cc))}_setDeferredFlags(){}_setFlags2(e,t,s=!1){const n=this._portionToSubPortionsMap[e];for(let e=0,i=n.length;e=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,Cc))}_setDeferredFlags2(){}setOffset(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdOffsets._texture),n.texSubImage2D(n.TEXTURE_2D,0,0,e,1,1,n.RGB,n.FLOAT,_c))}drawColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,Ri.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}_updateBackfaceCull(e,t){const s=this.model.backfaces||e.sectioned;if(t.backfaces!==s){const e=t.gl;s?e.disable(e.CULL_FACE):e.enable(e.CULL_FACE),t.backfaces=s}}drawColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,Ri.COLOR_TRANSPARENT))}drawDepth(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawSilhouetteXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_XRAYED))}drawSilhouetteHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_HIGHLIGHTED))}drawSilhouetteSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_SELECTED))}drawEdgesColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,Ri.EDGES_COLOR_OPAQUE)}drawEdgesColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,Ri.EDGES_COLOR_TRANSPARENT)}drawEdgesHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_HIGHLIGHTED)}drawEdgesSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_SELECTED)}drawEdgesXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_XRAYED)}drawOcclusion(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawShadow(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}setPickMatrices(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}drawPickMesh(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,Ri.PICK))}drawPickDepths(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,Ri.PICK))}drawSnapInitDepthBuf(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,Ri.PICK))}drawSnapDepths(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,Ri.PICK))}drawPickNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,Ri.PICK))}destroy(){if(this._destroyed)return;const e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}const xc=h.vec3();h.mat4();const Lc=h.vec3([1,1,1]),Mc=h.vec3([0,0,0]),Fc=h.vec3([0,0,0]),Hc=h.identityQuaternion(),Uc=h.identityMat4(),Gc=new Uint8Array([255,255,255]);class Vc extends C{constructor(e,t={}){super(e,t),this._dtxEnabled=this.scene.dtxEnabled&&!1!==t.dtxEnabled,this._enableVertexWelding=!1,this._enableIndexBucketing=!0,this._vboBatchingLayerScratchMemory=(Pi++,Di),this._textureTranscoder=t.textureTranscoder||Yl(this.scene.viewer),this._maxGeometryBatchSize=t.maxGeometryBatchSize,this._aabb=h.collapseAABB3(),this._aabbDirty=!0,this._quantizationRanges={},this._vboInstancingLayers={},this._vboBatchingLayers={},this._dtxLayers={},this._meshList=[],this.layerList=[],this._entityList=[],this._geometries={},this._dtxBuckets={},this._textures={},this._textureSets={},this._meshes={},this._entities={},this._scheduledMeshes={},this.renderFlags=new Qs,this.numGeometries=0,this.numPortions=0,this.numVisibleLayerPortions=0,this.numTransparentLayerPortions=0,this.numXRayedLayerPortions=0,this.numHighlightedLayerPortions=0,this.numSelectedLayerPortions=0,this.numEdgesLayerPortions=0,this.numPickableLayerPortions=0,this.numClippableLayerPortions=0,this.numCulledLayerPortions=0,this.numEntities=0,this._numTriangles=0,this._numLines=0,this._numPoints=0,this._edgeThreshold=t.edgeThreshold||10,this._origin=h.vec3(t.origin||[0,0,0]),this._position=h.vec3(t.position||[0,0,0]),this._rotation=h.vec3(t.rotation||[0,0,0]),this._quaternion=h.vec4(t.quaternion||[0,0,0,1]),this._conjugateQuaternion=h.vec4(t.quaternion||[0,0,0,1]),t.rotation&&h.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._scale=h.vec3(t.scale||[1,1,1]),this._worldRotationMatrix=h.mat4(),this._worldRotationMatrixConjugate=h.mat4(),this._matrix=h.mat4(),this._matrixDirty=!0,this._rebuildMatrices(),this._worldNormalMatrix=h.mat4(),h.inverseMat4(this._matrix,this._worldNormalMatrix),h.transposeMat4(this._worldNormalMatrix),(t.matrix||t.position||t.rotation||t.scale||t.quaternion)&&(this._viewMatrix=h.mat4(),this._viewNormalMatrix=h.mat4(),this._viewMatrixDirty=!0,this._matrixNonIdentity=!0),this._opacity=1,this._colorize=[1,1,1],this._saoEnabled=!1!==t.saoEnabled,this._pbrEnabled=!1!==t.pbrEnabled,this._colorTextureEnabled=!1!==t.colorTextureEnabled,this._isModel=t.isModel,this._isModel&&this.scene._registerModel(this),this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewMatrixDirty=!0})),this.scene.vfc.enabled&&(this._vfcManager=this.scene.vfc.getVFCManager(this)),this._createDefaultTextureSet(),this.visible=t.visible,this.culled=t.culled,this.pickable=t.pickable,this.clippable=t.clippable,this.collidable=t.collidable,this.castsShadow=t.castsShadow,this.receivesShadow=t.receivesShadow,this.xrayed=t.xrayed,this.highlighted=t.highlighted,this.selected=t.selected,this.edges=t.edges,this.colorize=t.colorize,this.opacity=t.opacity,this.backfaces=t.backfaces}_createDefaultTextureSet(){const e=new Fl({id:"defaultColorTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new Fl({id:"defaultMetalRoughTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),s=new Fl({id:"defaultNormalsTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),n=new Fl({id:"defaultEmissiveTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new Fl({id:"defaultOcclusionTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=s,this._textures.defaultEmissiveTexture=n,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new Ml({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:s,emissiveTexture:n,occlusionTexture:i})}get isPerformanceModel(){return!0}get objects(){return this._entities}get origin(){return this._origin}set position(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get position(){return this._position}set rotation(e){this._rotation.set(e||[0,0,0]),h.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get rotation(){return this._rotation}set quaternion(e){this._quaternion.set(e||[0,0,0,1]),h.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get quaternion(){return this._quaternion}set scale(e){}get scale(){return this._scale}set matrix(e){this._matrix.set(e||Uc),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),h.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),h.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get matrix(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix}get rotationMatrix(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}_rebuildMatrices(){this._matrixDirty&&(h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),h.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),h.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}get rotationMatrixConjugate(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}_setWorldMatrixDirty(){this._matrixDirty=!0}_setLocalAABBDirty(){for(let e=0,t=this._entityList.length;e0}set visible(e){e=!1!==e,this._visible=e;for(let t=0,s=this._entityList.length;t0}set xrayed(e){e=!!e,this._xrayed=e;for(let t=0,s=this._entityList.length;t0}set highlighted(e){e=!!e,this._highlighted=e;for(let t=0,s=this._entityList.length;t0}set selected(e){e=!!e,this._selected=e;for(let t=0,s=this._entityList.length;t0}set edges(e){e=!!e,this._edges=e;for(let t=0,s=this._entityList.length;t0}set pickable(e){e=!1!==e,this._pickable=e;for(let t=0,s=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){const t=e.colors,s=new Uint8Array(t.length);for(let e=0,n=t.length;e{o.setImage(c,{minFilter:s,magFilter:n,wrapS:i,wrapT:a,wrapR:r,flipY:e.flipY,encoding:l}),this.glRedraw()},c.src=e.src;break;default:this._textureTranscoder?y.loadArraybuffer(e.src,(e=>{e.byteLength?this._textureTranscoder.transcode([e],o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'src': file data is zero length")}),(function(e){this.error(`[createTexture] Can't create texture from 'src': ${e}`)})):this.error(`[createTexture] Can't create texture from 'src' - SceneModel needs to be configured with a TextureTranscoder for this file type ('${t}')`)}}else e.buffers&&(this._textureTranscoder?this._textureTranscoder.transcode(e.buffers,o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'buffers' - SceneModel needs to be configured with a TextureTranscoder for this option"));this._textures[t]=new Fl({id:t,texture:o})}createTextureSet(e){const t=e.id;if(null==t)return void this.error("[createTextureSet] Config missing: id");if(this._textureSets[t])return void this.error(`[createTextureSet] Texture set already created: ${t}`);let s,n,i,a,r;if(void 0!==e.colorTextureId&&null!==e.colorTextureId){if(s=this._textures[e.colorTextureId],!s)return void this.error(`[createTextureSet] Texture not found: ${e.colorTextureId} - ensure that you create it first with createTexture()`)}else s=this._textures.defaultColorTexture;if(void 0!==e.metallicRoughnessTextureId&&null!==e.metallicRoughnessTextureId){if(n=this._textures[e.metallicRoughnessTextureId],!n)return void this.error(`[createTextureSet] Texture not found: ${e.metallicRoughnessTextureId} - ensure that you create it first with createTexture()`)}else n=this._textures.defaultMetalRoughTexture;if(void 0!==e.normalsTextureId&&null!==e.normalsTextureId){if(i=this._textures[e.normalsTextureId],!i)return void this.error(`[createTextureSet] Texture not found: ${e.normalsTextureId} - ensure that you create it first with createTexture()`)}else i=this._textures.defaultNormalsTexture;if(void 0!==e.emissiveTextureId&&null!==e.emissiveTextureId){if(a=this._textures[e.emissiveTextureId],!a)return void this.error(`[createTextureSet] Texture not found: ${e.emissiveTextureId} - ensure that you create it first with createTexture()`)}else a=this._textures.defaultEmissiveTexture;if(void 0!==e.occlusionTextureId&&null!==e.occlusionTextureId){if(r=this._textures[e.occlusionTextureId],!r)return void this.error(`[createTextureSet] Texture not found: ${e.occlusionTextureId} - ensure that you create it first with createTexture()`)}else r=this._textures.defaultOcclusionTexture;const l=new Ml({id:t,model:this,colorTexture:s,metallicRoughnessTexture:n,normalsTexture:i,emissiveTexture:a,occlusionTexture:r});this._textureSets[t]=l}createMesh(e){if(void 0===e.id||null===e.id)return void this.error("[createMesh] SceneModel.createMesh() config missing: id");if(this._scheduledMeshes[e.id])return void this.error(`[createMesh] SceneModel already has a mesh with this ID: ${e.id}`);const t=!(void 0!==e.geometryId);if(e.sceneModelMatrix=this._matrixNonIdentity?this._matrix:null,t){const t=!!this._dtxEnabled;if(void 0!==e.primitive&&null!==e.primitive||(e.primitive="triangles"),"points"!==e.primitive&&"lines"!==e.primitive&&"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive)return void this.error(`Unsupported value for 'primitive': '${primitive}' ('geometryId' is absent) - supported values are 'points', 'lines', 'triangles', 'solid' and 'surface'.`);if(!e.positions&&!e.positionsCompressed&&!e.buckets)return this.error("Param expected: 'positions', 'positionsCompressed' or `buckets` ('geometryId' is absent)"),null;if(e.positions&&(e.positionsDecodeMatrix||e.positionsDecodeBoundary))return this.error("Illegal params: 'positions' not expected with 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.positionsCompressed&&!e.positionsDecodeMatrix&&!e.positionsDecodeBoundary)return this.error("Param expected: 'positionsCompressed' should be accompanied by 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.uvCompressed&&!e.uvDecodeMatrix)return this.error("Param expected: 'uvCompressed' should be accompanied by `uvDecodeMatrix` ('geometryId' is absent)"),null;if(!e.buckets&&!e.indices&&"points"!==e.primitive)return this.error(`Param expected: indices (required for '${e.primitive}' primitive type)`),null;if((e.matrix||e.position||e.rotation||e.scale)&&(e.positionsCompressed||e.positionsDecodeBoundary))return this.error("Unexpected params: 'matrix', 'rotation', 'scale', 'position' not allowed with 'positionsCompressed'"),null;if(e.origin=e.origin?h.addVec3(this._origin,e.origin,h.vec3()):this._origin,e.matrix)e.meshMatrix=e.matrix;else if(e.scale||e.rotation||e.position){const t=e.scale||Lc,s=e.position||Mc,n=e.rotation||Fc;h.eulerToQuaternion(n,"XYZ",Hc),e.meshMatrix=h.composeMat4(s,Hc,t,h.mat4())}if(e.positionsDecodeBoundary&&(e.positionsDecodeMatrix=ha(e.positionsDecodeBoundary,h.mat4())),t){if(e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.positions){const t=h.vec3(),s=[];S(e.positions,s,t)&&(e.positions=s,e.origin=h.addVec3(e.origin,t,t))}if(e.positions){const t=h.collapseAABB3();e.positionsDecodeMatrix=h.mat4(),h.expandAABB3Points3(t,e.positions),e.positionsCompressed=ua(e.positions,t,e.positionsDecodeMatrix)}e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=dt(e.positions,e.indices,null,2):e.edgeIndices=dt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.buckets||(e.buckets=jc(e,this._enableVertexWelding&&this._enableIndexBucketing))}else{if(e.type=1,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):[255,255,255],e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.positions){const t=[];S(e.positions,t,xc)&&(e.positions=t,e.origin=h.addVec3(e.origin,xc,h.vec3()))}if(e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=dt(e.positions,e.indices,null,2):e.edgeIndices=dt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId],!e.textureSet))return void this.error(`[createMesh] Texture set not found: ${e.textureSetId} - ensure that you create it first with createTextureSet()`)}}else{if(e.positions||e.positionsCompressed||e.indices||e.edgeIndices||e.normals||e.normalsCompressed||e.uv||e.uvCompressed||e.positionsDecodeMatrix)return void this.error("Mesh geometry parameters not expected when instancing a geometry (not expected: positions, positionsCompressed, indices, edgeIndices, normals, normalsCompressed, uv, uvCompressed, positionsDecodeMatrix)");if(e.geometry=this._geometries[e.geometryId],!e.geometry)return void this.error(`[createMesh] Geometry not found: ${e.geometryId} - ensure that you create it first with createGeometry()`);if(e.origin=e.origin?h.addVec3(this._origin,e.origin,h.vec3()):this._origin,e.positionsDecodeMatrix=e.geometry.positionsDecodeMatrix,e.matrix)e.meshMatrix=e.matrix.slice();else{const t=e.scale||Lc,s=e.position||Mc,n=e.rotation||Fc;h.eulerToQuaternion(n,"XYZ",Hc),e.meshMatrix=h.composeMat4(s,Hc,t,h.mat4())}if(!!this._dtxEnabled){e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255;let t=this._dtxBuckets[e.geometryId];t||(t=jc(e.geometry,this._enableVertexWelding,this._enableIndexBucketing),this._dtxBuckets[e.geometryId]=t),e.buckets=t}else e.type=0,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId]),function(e){if(e.obb=h.OBB3(),e.positionsCompressed&&e.positionsCompressed.length>0){const t=h.collapseAABB3();h.expandAABB3Points3(t,e.positionsCompressed),gt.decompressAABB(t,e.positionsDecodeMatrix),h.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){const t=h.collapseAABB3();h.expandAABB3Points3(t,e.positions),h.AABB3ToOBB3(t,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}_createMesh(e){const t=new bi(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);const s=t.pickId,n=s>>24&255,i=s>>16&255,a=s>>8&255,r=255&s;switch(e.pickColor=new Uint8Array([r,a,i,n]),e.worldAABB=h.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=h.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,h.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}_getNumPrimitives(e){let t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(let s=0,n=e.buckets.length;s>>0).toString(16)}_getVBOInstancingLayer(e){const t=this,s=e.origin,n=e.textureSetId||"-",i=e.geometryId,a=`${Math.round(s[0])}.${Math.round(s[1])}.${Math.round(s[2])}.${n}.${i}`;let r=this._vboInstancingLayers[a];if(r)return r;let l=e.textureSet;const o=e.geometry;for(;!r;)switch(o.primitive){case"triangles":case"surface":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new Fr({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!1});break;case"solid":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new Fr({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!0});break;case"lines":console.log(`[SceneModel ${this.id}]: creating LinesInstancingLayer`),r=new il({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0});break;case"points":console.log(`[SceneModel ${this.id}]: creating PointsInstancingLayer`),r=new Ll({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0})}return this._vboInstancingLayers[a]=r,this.layerList.push(r),r}createEntity(e){if(void 0===e.id?e.id=h.createUUID():this.scene.components[e.id]&&(this.error(`Scene already has a Component with this ID: ${e.id} - will assign random ID`),e.id=h.createUUID()),void 0===e.meshIds)return void this.error("Config missing: meshIds");let t=0;if(this._visible&&!1!==e.visible&&(t|=x),this._pickable&&!1!==e.pickable&&(t|=M),this._culled&&!1!==e.culled&&(t|=L),this._clippable&&!1!==e.clippable&&(t|=F),this._collidable&&!1!==e.collidable&&(t|=H),this._edges&&!1!==e.edges&&(t|=j),this._xrayed&&!1!==e.xrayed&&(t|=U),this._highlighted&&!1!==e.highlighted&&(t|=G),this._selected&&!1!==e.selected&&(t|=V),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(let t=0,s=e.meshIds.length;t{}));for(let e=0,t=this.layerList.length;ee.sortIdt.sortId?1:0));for(let e=0,t=this.layerList.length;e0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}_updateRenderFlagsVisibleLayers(){const e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(let t=0,s=this.layerList.length;t0)for(let e=0;e0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){const t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0){this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0))}if(this.numSelectedLayerPortions>0){const t=this.scene.selectedMaterial._state;t.fill&&(t.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){const t=this.scene.highlightMaterial._state;t.fill&&(t.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}drawColorOpaque(e){const t=this.renderFlags;for(let s=0,n=t.visibleLayers.length;s65536?16:8)}else r=[{positionsCompressed:n,indices:i,edgeIndices:a}];return r}class kc extends C{constructor(e,t={}){super(e,t),this._skyboxMesh=new tn(this,{geometry:new bt(this,{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new _t(this,{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new xn(this,{src:t.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:t.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),this.size=t.size,this.active=t.active}set size(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}get size(){return this._size}set active(e){this._skyboxMesh.visible=e}get active(){return this._skyboxMesh.visible}}const Qc=h.vec4(),Wc=h.vec4(),zc=h.vec3(),Kc=h.vec3(),Yc=h.vec3(),Xc=h.vec4(),qc=h.vec4(),Jc=h.vec4();class Zc{constructor(e){this._scene=e}dollyToCanvasPos(e,t,s){let n=!1;const i=this._scene.camera;if(e){const t=h.subVec3(e,i.eye,zc);n=h.lenVec3(t){this._cameraDirty=!0})),this._onProjMatrix=this._scene.camera.on("projMatrix",(()=>{this._cameraDirty=!0})),this._onTick=this._scene.on("tick",(()=>{this.updatePivotElement()}))}updatePivotElement(){const e=this._scene.camera,t=this._scene.canvas;if(this._pivoting&&this._cameraDirty){h.transformPoint3(e.viewMatrix,this.getPivotPos(),this._pivotViewPos),this._pivotViewPos[3]=1,h.transformPoint4(e.projMatrix,this._pivotViewPos,this._pivotProjPos);const s=t.boundary,n=s[2],i=s[3];this._pivotCanvasPos[0]=Math.floor((1+this._pivotProjPos[0]/this._pivotProjPos[3])*n/2),this._pivotCanvasPos[1]=Math.floor((1-this._pivotProjPos[1]/this._pivotProjPos[3])*i/2);let a=t._lastBoundingClientRect;if(!a||t._canvasSizeChanged){const e=t.canvas;a=t._lastBoundingClientRect=e.getBoundingClientRect()}this._pivotElement&&(this._pivotElement.style.left=Math.floor(a.left+this._pivotCanvasPos[0])-this._pivotElement.clientWidth/2+window.scrollX+"px",this._pivotElement.style.top=Math.floor(a.top+this._pivotCanvasPos[1])-this._pivotElement.clientHeight/2+window.scrollY+"px"),this._cameraDirty=!1}}setPivotElement(e){this._pivotElement=e}startPivot(){if(this._cameraLookingDownwards())return this._pivoting=!1,!1;const e=this._scene.camera;let t=h.lookAtMat4v(e.eye,e.look,e.worldUp);h.transformPoint3(t,this.getPivotPos(),this._cameraOffset);const s=this.getPivotPos();this._cameraOffset[2]+=h.distVec3(e.eye,s),t=h.inverseMat4(t);const n=h.transformVec3(t,this._cameraOffset),i=h.vec3();if(h.subVec3(e.eye,s,i),h.addVec3(i,n),e.zUp){const e=i[1];i[1]=i[2],i[2]=e}this._radius=h.lenVec3(i),this._polar=Math.acos(i[1]/this._radius),this._azimuth=Math.atan2(i[0],i[2]),this._pivoting=!0}_cameraLookingDownwards(){const e=this._scene.camera,t=h.normalizeVec3(h.subVec3(e.look,e.eye,$c)),s=h.cross3Vec3(t,e.worldUp,eu);return h.sqLenVec3(s)<=1e-4}getPivoting(){return this._pivoting}setPivotPos(e){this._pivotWorldPos.set(e),this._pivotPosSet=!0}setCanvasPivotPos(e){const t=this._scene.camera,s=Math.abs(h.distVec3(this._scene.center,t.eye)),n=t.project.transposedMatrix,i=n.subarray(8,12),a=n.subarray(12),r=[0,0,-1,1],l=h.dotVec4(r,i)/h.dotVec4(r,a),o=su;t.project.unproject(e,l,nu,iu,o);const c=h.normalizeVec3(h.subVec3(o,t.eye,$c)),u=h.addVec3(t.eye,h.mulVec3Scalar(c,s,eu),tu);this.setPivotPos(u)}getPivotPos(){return this._pivotPosSet?this._pivotWorldPos:this._scene.camera.look}continuePivot(e,t){if(!this._pivoting)return;if(0===e&&0===t)return;const s=this._scene.camera;var n=-e;const i=-t;1===s.worldUp[2]&&(n=-n),this._azimuth+=.01*-n,this._polar+=.01*i,this._polar=h.clamp(this._polar,.001,Math.PI-.001);const a=[this._radius*Math.sin(this._polar)*Math.sin(this._azimuth),this._radius*Math.cos(this._polar),this._radius*Math.sin(this._polar)*Math.cos(this._azimuth)];if(1===s.worldUp[2]){const e=a[1];a[1]=a[2],a[2]=e}const r=h.lenVec3(h.subVec3(s.look,s.eye,h.vec3())),l=this.getPivotPos();h.addVec3(a,l);let o=h.lookAtMat4v(a,l,s.worldUp);o=h.inverseMat4(o);const c=h.transformVec3(o,this._cameraOffset);o[12]-=c[0],o[13]-=c[1],o[14]-=c[2];const u=[o[8],o[9],o[10]];s.eye=[o[12],o[13],o[14]],h.subVec3(s.eye,h.mulVec3Scalar(u,r),s.look),s.up=[o[4],o[5],o[6]],this.showPivot()}showPivot(){this._shown||(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this.updatePivotElement(),this._pivotElement.style.visibility="visible",this._shown=!0,this._hideTimeout=window.setTimeout((()=>{this.hidePivot()}),1e3)))}hidePivot(){this._shown&&(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this._pivotElement.style.visibility="hidden"),this._shown=!1)}endPivot(){this._pivoting=!1}destroy(){this._scene.camera.off(this._onViewMatrix),this._scene.camera.off(this._onProjMatrix),this._scene.off(this._onTick)}}class ru{constructor(e,t){this._scene=e.scene,this._cameraControl=e,this._scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},this._configs=t,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick=!1,this.pickCursorPos=h.vec2(),this.picked=!1,this.pickedSurface=!1,this.pickResult=null,this._lastPickedEntityId=null,this._needFireEvents=!1}update(){if(!this._configs.pointerEnabled)return;if(!this.schedulePickEntity&&!this.schedulePickSurface)return;this.picked=!1,this.pickedSurface=!1,this.snappedOrPicked=!1,this.hoveredSnappedOrSurfaceOff=!1,this._needFireEvents=!1;const e=this._cameraControl.hasSubs("hoverSurface");if(this.scheduleSnapOrPick){const e=this._scene.snapPick({canvasPos:this.pickCursorPos,snapRadius:this._configs.snapRadius,snapMode:this._configs.snapMode});e&&e.snappedWorldPos?(this.snapPickResult=e,this.snappedOrPicked=!0,this._needFireEvents=!0):(this.schedulePickSurface=!0,this.snapPickResult=null)}if(this.schedulePickSurface&&this.pickResult&&this.pickResult.worldPos){const t=this.pickResult.canvasPos;if(t[0]===this.pickCursorPos[0]&&t[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!0,this._needFireEvents=e,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.hoveredSnappedOrSurfaceOff=!0,void(this.scheduleSnapOrPick=!1)}if(this.schedulePickEntity&&this.pickResult&&(this.pickResult.canvasPos||this.pickResult.snappedCanvasPos)){const e=this.pickResult.canvasPos||this.pickResult.snappedCanvasPos;if(e[0]===this.pickCursorPos[0]&&e[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!1,this.schedulePickEntity=!1,void(this.schedulePickSurface=!1)}this.schedulePickSurface||this.scheduleSnapOrPick&&!this.snapPickResult?(this.pickResult=this._scene.pick({pickSurface:!0,pickSurfaceNormal:!1,canvasPos:this.pickCursorPos}),this.pickResult?(this.picked=!0,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.pickedSurface=!0,this._needFireEvents=!0):this.scheduleSnapOrPick&&(this.hoveredSnappedOrSurfaceOff=!0,this._needFireEvents=!0)):(this.pickResult=this._scene.pick({canvasPos:this.pickCursorPos}),this.pickResult&&(this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!0)),this.scheduleSnapOrPick=!1,this.schedulePickEntity=!1,this.schedulePickSurface=!1}fireEvents(){if(this._needFireEvents){if(this.hoveredSnappedOrSurfaceOff&&this._cameraControl.fire("hoverSnapOrSurfaceOff",{canvasPos:this.pickCursorPos},!0),this.snappedOrPicked)if(this.snapPickResult){const e=new de;e.worldPos=this.snapPickResult.snappedWorldPos,e.canvasPos=this.snapPickResult.snappedCanvasPos,this._cameraControl.fire("hoverSnapOrSurface",e,!0),this.snapPickResult=null}else this._cameraControl.fire("hoverSnapOrSurface",this.pickResult,!0);if(this.picked&&this.pickResult&&(this.pickResult.entity||this.pickResult.worldPos)){if(this.pickResult.entity){const e=this.pickResult.entity.id;this._lastPickedEntityId!==e&&(void 0!==this._lastPickedEntityId&&this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._cameraControl.fire("hoverEnter",this.pickResult,!0),this._lastPickedEntityId=e)}this._cameraControl.fire("hover",this.pickResult,!0),this.pickResult.worldPos&&(this.pickedSurface=!0,this._cameraControl.fire("hoverSurface",this.pickResult,!0))}else void 0!==this._lastPickedEntityId&&(this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),this._cameraControl.fire("hoverOff",{canvasPos:this.pickCursorPos},!0);this.pickResult=null,this._needFireEvents=!1}}destroy(){}}const lu=h.vec2();class ou{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController;let r,l,o,c=0,u=0,p=0,A=0,d=!1;const f=h.vec3();let I=!0;const y=this._scene.canvas.canvas,m=[];function v(e=!0){y.style.cursor="move",c=n.pointerCanvasPos[0],u=n.pointerCanvasPos[1],p=n.pointerCanvasPos[0],A=n.pointerCanvasPos[1],e&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(d=!0,f.set(a.pickResult.worldPos)):d=!1)}document.addEventListener("keydown",this._documentKeyDownHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!0}),document.addEventListener("keyup",this._documentKeyUpHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!1}),y.addEventListener("mousedown",this._mouseDownHandler=t=>{if(s.active&&s.pointerEnabled)switch(t.which){case 1:m[e.input.KEY_SHIFT]||s.planView?(r=!0,v()):(r=!0,v(!1));break;case 2:l=!0,v();break;case 3:o=!0,s.panRightClick&&v()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=()=>{if(!s.active||!s.pointerEnabled)return;if(!r&&!l&&!o)return;const t=e.canvas.boundary,a=t[2],p=t[3],A=n.pointerCanvasPos[0],I=n.pointerCanvasPos[1];if(m[e.input.KEY_SHIFT]||s.planView||!s.panRightClick&&l||s.panRightClick&&o){const t=A-c,s=I-u,n=e.camera;if("perspective"===n.projection){const a=Math.abs(d?h.lenVec3(h.subVec3(f,e.camera.eye,[])):e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=1.5*t*a/p,i.panDeltaY+=1.5*s*a/p}else i.panDeltaX+=.5*n.ortho.scale*(t/p),i.panDeltaY+=.5*n.ortho.scale*(s/p)}else!r||l||o||s.planView||(s.firstPerson?(i.rotateDeltaY-=(A-c)/a*s.dragRotationRate/2,i.rotateDeltaX+=(I-u)/p*(s.dragRotationRate/4)):(i.rotateDeltaY-=(A-c)/a*(1.5*s.dragRotationRate),i.rotateDeltaX+=(I-u)/p*(1.5*s.dragRotationRate)));c=A,u=I}),y.addEventListener("mousemove",this._canvasMouseMoveHandler=e=>{s.active&&s.pointerEnabled&&n.mouseover&&(I=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{if(s.active&&s.pointerEnabled)switch(e.which){case 1:case 2:case 3:r=!1,l=!1,o=!1}}),y.addEventListener("mouseup",this._mouseUpHandler=e=>{if(s.active&&s.pointerEnabled){if(3===e.which){!function(e,t){if(e){let s=e.target,n=0,i=0,a=0,r=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,a+=s.scrollLeft,r+=s.scrollTop,s=s.offsetParent;t[0]=e.pageX+a-n,t[1]=e.pageY+r-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,lu);const s=lu[0],n=lu[1];Math.abs(s-p)<3&&Math.abs(n-A)<3&&t.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:lu,event:e},!0)}y.style.removeProperty("cursor")}}),y.addEventListener("mouseenter",this._mouseEnterHandler=()=>{s.active&&s.pointerEnabled});const w=1/60;let g=null;y.addEventListener("wheel",this._mouseWheelHandler=e=>{if(!s.active||!s.pointerEnabled)return;const t=performance.now()/1e3;var a=null!==g?t-g:0;g=t,a>.05&&(a=.05),a{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const r=i._isKeyDownForAction(i.AXIS_VIEW_RIGHT),l=i._isKeyDownForAction(i.AXIS_VIEW_BACK),o=i._isKeyDownForAction(i.AXIS_VIEW_LEFT),c=i._isKeyDownForAction(i.AXIS_VIEW_FRONT),u=i._isKeyDownForAction(i.AXIS_VIEW_TOP),p=i._isKeyDownForAction(i.AXIS_VIEW_BOTTOM);if(!(r||l||o||c||u||p))return;const A=e.aabb,d=h.getAABB3Diag(A);h.getAABB3Center(A,cu);const f=Math.abs(d/Math.tan(t.cameraFlight.fitFOV*h.DEGTORAD)),I=1.1*d;du.orthoScale=I,r?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldRight,f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):l?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldForward,f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):o?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldRight,-f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):c?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldForward,-f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):u?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldUp,f,uu),Au)),du.look.set(cu),du.up.set(h.normalizeVec3(h.mulVec3Scalar(a.worldForward,1,hu),pu))):p&&(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldUp,-f,uu),Au)),du.look.set(cu),du.up.set(h.normalizeVec3(h.mulVec3Scalar(a.worldForward,-1,hu)))),!s.firstPerson&&s.followPointer&&t.pivotController.setPivotPos(cu),t.cameraFlight.duration>0?t.cameraFlight.flyTo(du,(()=>{t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot()})):(t.cameraFlight.jumpTo(du),t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot())}))}reset(){}destroy(){this._scene.input.off(this._onSceneKeyDown)}}class Iu{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=t.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;let o=!1,c=!1;const u=this._scene.canvas.canvas,p=s=>{let n;s&&s.worldPos&&(n=s.worldPos);const i=s&&s.entity?s.entity.aabb:e.aabb;if(n){const s=e.camera;h.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};u.addEventListener("mousemove",this._canvasMouseMoveHandler=t=>{if(!s.active||!s.pointerEnabled)return;if(o||c)return;const i=l.hasSubs("hover"),r=l.hasSubs("hoverOut"),u=l.hasSubs("hoverOff"),h=l.hasSubs("hoverSurface"),p=l.hasSubs("hoverSnapOrSurface");if(i||r||u||h||p)if(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=h,a.scheduleSnapOrPick=p,a.update(),a.pickResult){if(a.pickResult.entity){const t=a.pickResult.entity.id;this._lastPickedEntityId!==t&&(void 0!==this._lastPickedEntityId&&l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),l.fire("hoverEnter",a.pickResult,!0),this._lastPickedEntityId=t)}l.fire("hover",a.pickResult,!0),(a.pickResult.worldPos||a.pickResult.snappedWorldPos)&&l.fire("hoverSurface",a.pickResult,!0)}else void 0!==this._lastPickedEntityId&&(l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),l.fire("hoverOff",{canvasPos:a.pickCursorPos},!0)}),u.addEventListener("mousedown",this._canvasMouseDownHandler=t=>{1===t.which&&(o=!0),3===t.which&&(c=!0);if(1===t.which&&s.active&&s.pointerEnabled&&(n.mouseDownClientX=t.clientX,n.mouseDownClientY=t.clientY,n.mouseDownCursorX=n.pointerCanvasPos[0],n.mouseDownCursorY=n.pointerCanvasPos[1],!s.firstPerson&&s.followPointer&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),1===t.which))){const t=a.pickResult;t&&t.worldPos?(r.setPivotPos(t.worldPos),r.startPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),r.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{1===e.which&&(o=!1),3===e.which&&(c=!1)}),u.addEventListener("mouseup",this._canvasMouseUpHandler=i=>{if(!s.active||!s.pointerEnabled)return;if(!(1===i.which))return;if(r.hidePivot(),Math.abs(i.clientX-n.mouseDownClientX)>3||Math.abs(i.clientY-n.mouseDownClientY)>3)return;const o=l.hasSubs("picked"),c=l.hasSubs("pickedNothing"),u=l.hasSubs("pickedSurface"),A=l.hasSubs("doublePicked"),d=l.hasSubs("doublePickedSurface"),f=l.hasSubs("doublePickedNothing");if(!(s.doublePickFlyTo||A||d||f))return(o||c||u)&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=u,a.update(),a.pickResult?(l.fire("picked",a.pickResult,!0),a.pickedSurface&&l.fire("pickedSurface",a.pickResult,!0)):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0)),void(this._clicks=0);if(this._clicks++,1===this._clicks){a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo,a.schedulePickSurface=u,a.update();const e=a.pickResult,i=a.pickedSurface;this._timeout=setTimeout((()=>{e?(l.fire("picked",e,!0),i&&(l.fire("pickedSurface",e,!0),!s.firstPerson&&s.followPointer&&(t.pivotController.setPivotPos(e.worldPos),t.pivotController.startPivot()&&t.pivotController.showPivot()))):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0),this._clicks=0}),s.doubleClickTimeFrame)}else{if(null!==this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null),a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo||A||d,a.schedulePickSurface=a.schedulePickEntity&&d,a.update(),a.pickResult){if(l.fire("doublePicked",a.pickResult,!0),a.pickedSurface&&l.fire("doublePickedSurface",a.pickResult,!0),s.doublePickFlyTo&&(p(a.pickResult),!s.firstPerson&&s.followPointer)){const e=a.pickResult.entity.aabb,s=h.getAABB3Center(e);t.pivotController.setPivotPos(s),t.pivotController.startPivot()&&t.pivotController.showPivot()}}else if(l.fire("doublePickedNothing",{canvasPos:n.pointerCanvasPos},!0),s.doublePickFlyTo&&(p(),!s.firstPerson&&s.followPointer)){const s=e.aabb,n=h.getAABB3Center(s);t.pivotController.setPivotPos(n),t.pivotController.startPivot()&&t.pivotController.showPivot()}this._clicks=0}},!1)}reset(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}destroy(){const e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}class yu{constructor(e,t,s,n,i){this._scene=e;const a=e.input,r=[],l=e.canvas.canvas;let o=!0;this._onSceneMouseMove=a.on("mousemove",(()=>{o=!0})),this._onSceneKeyDown=a.on("keydown",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&n.mouseover&&(r[t]=!0,t===a.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=a.on("keyup",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&(r[t]=!1,t===a.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=e.on("tick",(l=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const c=t.cameraControl,u=l.deltaTime/1e3;if(!s.planView){const e=c._isKeyDownForAction(c.ROTATE_Y_POS,r),n=c._isKeyDownForAction(c.ROTATE_Y_NEG,r),a=c._isKeyDownForAction(c.ROTATE_X_POS,r),l=c._isKeyDownForAction(c.ROTATE_X_NEG,r),o=u*s.keyboardRotationRate;(e||n||a||l)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),e?i.rotateDeltaY+=o:n&&(i.rotateDeltaY-=o),a?i.rotateDeltaX+=o:l&&(i.rotateDeltaX-=o),!s.firstPerson&&s.followPointer&&t.pivotController.startPivot())}if(!r[a.KEY_CTRL]&&!r[a.KEY_ALT]){const e=c._isKeyDownForAction(c.DOLLY_BACKWARDS,r),a=c._isKeyDownForAction(c.DOLLY_FORWARDS,r);if(e||a){const r=u*s.keyboardDollyRate;!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),a?i.dollyDelta-=r:e&&(i.dollyDelta+=r),o&&(n.followPointerDirty=!0,o=!1)}}const h=c._isKeyDownForAction(c.PAN_FORWARDS,r),p=c._isKeyDownForAction(c.PAN_BACKWARDS,r),A=c._isKeyDownForAction(c.PAN_LEFT,r),d=c._isKeyDownForAction(c.PAN_RIGHT,r),f=c._isKeyDownForAction(c.PAN_UP,r),I=c._isKeyDownForAction(c.PAN_DOWN,r),y=(r[a.KEY_ALT]?.3:1)*u*s.keyboardPanRate;(h||p||A||d||f||I)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),I?i.panDeltaY+=y:f&&(i.panDeltaY+=-y),d?i.panDeltaX+=-y:A&&(i.panDeltaX+=y),p?i.panDeltaZ+=y:h&&(i.panDeltaZ+=-y))}))}reset(){}destroy(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}const mu=h.vec3();class vu{constructor(e,t,s,n,i){this._scene=e;const a=e.camera,r=t.pickController,l=t.pivotController,o=t.panController;let c=1,u=1,p=null;this._onTick=e.on("tick",(()=>{if(!s.active||!s.pointerEnabled)return;let t="default";if(Math.abs(i.dollyDelta)<.001&&(i.dollyDelta=0),Math.abs(i.rotateDeltaX)<.001&&(i.rotateDeltaX=0),Math.abs(i.rotateDeltaY)<.001&&(i.rotateDeltaY=0),0===i.rotateDeltaX&&0===i.rotateDeltaY||(i.dollyDelta=0),s.followPointer&&--c<=0&&(c=1,0!==i.dollyDelta)){if(0===i.rotateDeltaY&&0===i.rotateDeltaX&&s.followPointer&&n.followPointerDirty&&(r.pickCursorPos=n.pointerCanvasPos,r.schedulePickSurface=!0,r.update(),r.pickResult&&r.pickResult.worldPos?p=r.pickResult.worldPos:(u=1,p=null),n.followPointerDirty=!1),p){const t=Math.abs(h.lenVec3(h.subVec3(p,e.camera.eye,mu)));u=t/s.dollyProximityThreshold}u{n.mouseover=!0}),a.addEventListener("mouseleave",this._mouseLeaveHandler=()=>{n.mouseover=!1,a.style.cursor=null}),document.addEventListener("mousemove",this._mouseMoveHandler=e=>{gu(e,a,n.pointerCanvasPos)}),a.addEventListener("mousedown",this._mouseDownHandler=e=>{s.active&&s.pointerEnabled&&(gu(e,a,n.pointerCanvasPos),n.mouseover=!0)}),a.addEventListener("mouseup",this._mouseUpHandler=e=>{s.active&&s.pointerEnabled})}reset(){}destroy(){const e=this._scene.canvas.canvas;document.removeEventListener("mousemove",this._mouseMoveHandler),e.removeEventListener("mouseenter",this._mouseEnterHandler),e.removeEventListener("mouseleave",this._mouseLeaveHandler),e.removeEventListener("mousedown",this._mouseDownHandler),e.removeEventListener("mouseup",this._mouseUpHandler)}}function gu(e,t,s){if(e){const{x:n,y:i}=t.getBoundingClientRect();s[0]=e.clientX-n,s[1]=e.clientY-i}else e=window.event,s[0]=e.x,s[1]=e.y;return s}const Tu=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class Eu{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=h.vec2(),o=h.vec2(),c=h.vec2(),u=h.vec2(),p=[],A=this._scene.canvas.canvas;let d=0,f=!1;this._onTick=e.on("tick",(()=>{f=!1})),A.addEventListener("touchstart",this._canvasTouchStartHandler=t=>{if(!s.active||!s.pointerEnabled)return;t.preventDefault();const i=t.touches,o=t.changedTouches;for(n.touchStartTime=Date.now(),1===i.length&&1===o.length&&(Tu(i[0],l),s.followPointer&&(a.pickCursorPos=l,a.schedulePickSurface=!0,a.update(),s.planView||(a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(r.setPivotPos(a.pickResult.worldPos),!s.firstPerson&&r.startPivot()&&r.showPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),!s.firstPerson&&r.startPivot()&&r.showPivot()))));p.length{if(!s.active||!s.pointerEnabled)return;if(t.stopPropagation(),t.preventDefault(),f)return;f=!0;const r=e.canvas.boundary,l=r[2],A=r[3],I=t.touches;if(t.touches.length===d){if(1===d){Tu(I[0],o),h.subVec2(o,p[0],u);const t=u[0],a=u[1];if(null!==n.longTouchTimeout&&(Math.abs(t)>s.longTapRadius||Math.abs(a)>s.longTapRadius)&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),s.planView){const n=e.camera;if("perspective"===n.projection){const r=Math.abs(e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=t*r/A*s.touchPanRate,i.panDeltaY+=a*r/A*s.touchPanRate}else i.panDeltaX+=.5*n.ortho.scale*(t/A)*s.touchPanRate,i.panDeltaY+=.5*n.ortho.scale*(a/A)*s.touchPanRate}else i.rotateDeltaY-=t/l*(1*s.dragRotationRate),i.rotateDeltaX+=a/A*(1.5*s.dragRotationRate)}else if(2===d){const t=I[0],r=I[1];Tu(t,o),Tu(r,c);const l=h.geometricMeanVec2(p[0],p[1]),u=h.geometricMeanVec2(o,c),d=h.vec2();h.subVec2(l,u,d);const f=d[0],y=d[1],m=e.camera,v=h.distVec2([t.pageX,t.pageY],[r.pageX,r.pageY]),w=(h.distVec2(p[0],p[1])-v)*s.touchDollyRate;if(i.dollyDelta=w,Math.abs(w)<1)if("perspective"===m.projection){const t=a.pickResult?a.pickResult.worldPos:e.center,n=Math.abs(h.lenVec3(h.subVec3(t,e.camera.eye,[])))*Math.tan(m.perspective.fov/2*Math.PI/180);i.panDeltaX-=f*n/A*s.touchPanRate,i.panDeltaY-=y*n/A*s.touchPanRate}else i.panDeltaX-=.5*m.ortho.scale*(f/A)*s.touchPanRate,i.panDeltaY-=.5*m.ortho.scale*(y/A)*s.touchPanRate;n.pointerCanvasPos=u}for(let e=0;e{let n;s&&s.worldPos&&(n=s.worldPos);const i=s?s.entity.aabb:e.aabb;if(n){const s=e.camera;h.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};A.addEventListener("touchstart",this._canvasTouchStartHandler=e=>{if(!s.active||!s.pointerEnabled)return;null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null);const i=e.touches,a=e.changedTouches;if(l=Date.now(),1===i.length&&1===a.length){u=l,bu(i[0],c);const a=c[0],r=c[1],o=i[0].pageX,h=i[0].pageY;n.longTouchTimeout=setTimeout((()=>{t.cameraControl.fire("rightClick",{pagePos:[Math.round(o),Math.round(h)],canvasPos:[Math.round(a),Math.round(r)],event:e},!0),n.longTouchTimeout=null}),s.longTapTimeout)}else u=-1;for(;o.length{if(!s.active||!s.pointerEnabled)return;const t=Date.now(),i=e.touches,l=e.changedTouches,A=r.hasSubs("pickedSurface");null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),0===i.length&&1===l.length&&u>-1&&t-u<150&&(p>-1&&u-p<325?(bu(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("doublePicked",a.pickResult),a.pickedSurface&&r.fire("doublePickedSurface",a.pickResult),s.doublePickFlyTo&&d(a.pickResult)):(r.fire("doublePickedNothing"),s.doublePickFlyTo&&d()),p=-1):h.distVec2(o[0],c)<4&&(bu(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("picked",a.pickResult),a.pickedSurface&&r.fire("pickedSurface",a.pickResult)):r.fire("pickedNothing"),p=t),u=-1),o.length=i.length;for(let e=0,t=i.length;e{e.preventDefault()},this._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},this._states={pointerCanvasPos:h.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:h.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},this._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};const s=this.scene;this._controllers={cameraControl:this,pickController:new ru(this,this._configs),pivotController:new au(s,this._configs),panController:new Zc(s),cameraFlight:new pi(this,{duration:.5})},this._handlers=[new wu(this.scene,this._controllers,this._configs,this._states,this._updates),new Eu(this.scene,this._controllers,this._configs,this._states,this._updates),new ou(this.scene,this._controllers,this._configs,this._states,this._updates),new fu(this.scene,this._controllers,this._configs,this._states,this._updates),new Iu(this.scene,this._controllers,this._configs,this._states,this._updates),new Du(this.scene,this._controllers,this._configs,this._states,this._updates),new yu(this.scene,this._controllers,this._configs,this._states,this._updates)],this._cameraUpdater=new vu(this.scene,this._controllers,this._configs,this._states,this._updates),this.navMode=t.navMode,t.planView&&(this.planView=t.planView),this.constrainVertical=t.constrainVertical,t.keyboardLayout?this.keyboardLayout=t.keyboardLayout:this.keyMap=t.keyMap,this.doublePickFlyTo=t.doublePickFlyTo,this.panRightClick=t.panRightClick,this.active=t.active,this.followPointer=t.followPointer,this.rotationInertia=t.rotationInertia,this.keyboardPanRate=t.keyboardPanRate,this.touchPanRate=t.touchPanRate,this.keyboardRotationRate=t.keyboardRotationRate,this.dragRotationRate=t.dragRotationRate,this.touchDollyRate=t.touchDollyRate,this.dollyInertia=t.dollyInertia,this.dollyProximityThreshold=t.dollyProximityThreshold,this.dollyMinSpeed=t.dollyMinSpeed,this.panInertia=t.panInertia,this.pointerEnabled=!0,this.keyboardDollyRate=t.keyboardDollyRate,this.mouseWheelDollyRate=t.mouseWheelDollyRate}set keyMap(e){if(e=e||"qwerty",y.isString(e)){const t=this.scene.input,s={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":s[this.PAN_LEFT]=[t.KEY_A],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_Z],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":s[this.PAN_LEFT]=[t.KEY_Q],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_W],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=s}else{const t=e;this._keyMap=t}}get keyMap(){return this._keyMap}_isKeyDownForAction(e,t){const s=this._keyMap[e];if(!s)return!1;t||(t=this.scene.input.keyDown);for(let e=0,n=s.length;e0?Su(t):null,r=s&&s.length>0?Su(s):null,l=e=>{if(!e)return;var t=!0;(r&&r[e.type]||a&&!a[e.type])&&(t=!1),t&&n.push(e.id);const s=e.children;if(s)for(var i=0,o=s.length;i>t;s.sort(eo);const n=new Int32Array(e.length);for(let t=0,i=s.length;te[t+1]){let s=e[t];e[t]=e[t+1],e[t+1]=s}to=new Int32Array(e),t.sort(so);const s=new Int32Array(e.length);for(let n=0,i=t.length;nt){let s=e;e=t,t=s}function s(s,n){return s!==e?e-s:n!==t?t-n:0}let n=0,i=(a.length>>1)-1;for(;n<=i;){const e=i+n>>1,t=s(a[2*e],a[2*e+1]);if(t>0)n=e+1;else{if(!(t<0))return e;i=e-1}}return-n-1}const l=new Int32Array(a.length/2);l.fill(0);const o=n.length/3;if(o>8*(1<p.maxNumPositions&&(p=h()),p.bucketNumber>8)return[e];let d;-1===c[o]&&(c[o]=p.numPositions++,p.positionsCompressed.push(n[3*o]),p.positionsCompressed.push(n[3*o+1]),p.positionsCompressed.push(n[3*o+2])),-1===c[u]&&(c[u]=p.numPositions++,p.positionsCompressed.push(n[3*u]),p.positionsCompressed.push(n[3*u+1]),p.positionsCompressed.push(n[3*u+2])),-1===c[A]&&(c[A]=p.numPositions++,p.positionsCompressed.push(n[3*A]),p.positionsCompressed.push(n[3*A+1]),p.positionsCompressed.push(n[3*A+2])),p.indices.push(c[o]),p.indices.push(c[u]),p.indices.push(c[A]),(d=r(o,u))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(o,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(u,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]]))}const A=t/8*2,d=t/8,f=2*n.length+(i.length+a.length)*A;let I=0,y=-n.length/3;return u.forEach((e=>{I+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*d,y+=e.positionsCompressed.length/3})),I>f?[e]:(s&&function(e,t){const s={},n={};let i=0;e.forEach((e=>{const t=e.indices,a=e.edgeIndices,r=e.positionsCompressed;for(let e=0,n=t.length;e0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl,s=e._lightsState;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uLightAmbient=n.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];const i=s.lights;let a;for(let e=0,t=i.length;e0;let i;const a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),e.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(let e=0,t=s.lights.length;e> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(let e=0,t=s.lights.length;e0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),this._withSAO&&(n.push("uniform sampler2D uOcclusionTexture;"),n.push("uniform vec4 uSAOParams;"),n.push("const float packUpscale = 256. / 255.;"),n.push("const float unpackDownScale = 255. / 256.;"),n.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),n.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),n.push("float unpackRGBToFloat( const in vec4 v ) {"),n.push(" return dot( v, unPackFactors );"),n.push("}")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(n.push(" float viewportWidth = uSAOParams[0];"),n.push(" float viewportHeight = uSAOParams[1];"),n.push(" float blendCutoff = uSAOParams[2];"),n.push(" float blendFactor = uSAOParams[3];"),n.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),n.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),n.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const ho=new Float32Array([1,1,1]),po=h.vec3(),Ao=h.vec3(),fo=h.vec3(),Io=h.vec3(),yo=h.mat4();class mo{constructor(e,t){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=a,d=i.viewMatrix;if(!this._program&&(this._allocate(),this.errors))return;let f,I;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),c||0!==u[0]||0!==u[1]||0!==u[2]){const e=po;if(c){const t=Ao;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,yo),I=fo,I[0]=i.eye[0]-e[0],I[1]=i.eye[1]-e[1],I[2]=i.eye[2]-e[2]}else f=d,I=i.eye;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s===Ri.SILHOUETTE_XRAYED){const e=n.xrayMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.SILHOUETTE_HIGHLIGHTED){const e=n.highlightMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.SILHOUETTE_SELECTED){const e=n.selectedMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,ho);if(n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=n._sectionPlanesState.sectionPlanes.length;if(y>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture silhouette vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.y) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("} else {"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = color;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const vo=new Float32Array([0,0,0,1]),wo=h.vec3(),go=h.vec3();h.vec3();const To=h.vec3(),Eo=h.mat4();class bo{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=wo;if(I){const t=go;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,Eo)}else f=d;if(r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s===Ri.EDGES_XRAYED){const e=i.xrayMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.EDGES_HIGHLIGHTED){const e=i.highlightMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===Ri.EDGES_SELECTED){const e=i.selectedMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,vo);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vColor = vec4(color.r, color.g, color.b, color.a);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Do=h.vec3(),Po=h.vec3(),Ro=h.vec3(),Co=h.mat4();class _o{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Do;if(I){const t=Po;h.transformPoint3(p,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=B(d,e,Co)}else f=d;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vec4 rgb = vec4(color.rgba);"),s.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Bo=h.vec3(),Oo=h.vec3(),So=h.vec3(),No=h.mat4();class xo{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(t),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n;let d,f;o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Bo;if(I){const t=h.transformPoint3(p,c,Oo);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=B(a.viewMatrix,e,No),f=So,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=a.viewMatrix,f=a.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry picking vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("smooth out vec4 vWorldPosition;"),s.push("flat out uvec4 vFlags2;")),s.push("out vec4 vPickColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry picking fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outPickColor = vPickColor; "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Lo=h.vec3(),Mo=h.vec3(),Fo=h.vec3(),Ho=h.vec3(),Uo=h.mat4();class Go{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=e.pickViewMatrix||a.viewMatrix;let f,I;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),a.eye,c||0!==u[0]||0!==u[1]||0!==u[2]){const t=Lo;if(c){const e=Mo;h.transformPoint3(p,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],f=B(d,t,Uo),I=Fo,I[0]=a.eye[0]-t[0],I[1]=a.eye[1]-t[1],I[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else f=d,I=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniform1f(this._uPickZNear,e.pickZNear),r.uniform1f(this._uPickZFar,e.pickZFar),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=i._sectionPlanesState.sectionPlanes.length;if(y>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick depth vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform float pickZNear;"),n.push("uniform float pickZFar;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),n.push(" outPackedDepth = packDepth(zNormalizedDepth); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Vo=h.vec3(),jo=h.vec3(),ko=h.vec3(),Qo=h.vec3(),Wo=h.vec3(),zo=h.mat4();class Ko{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Vo;let y,m;I[0]=h.safeInv(d[3]-d[0])*h.MAX_INT,I[1]=h.safeInv(d[4]-d[1])*h.MAX_INT,I[2]=h.safeInv(d[5]-d[2])*h.MAX_INT,e.snapPickCoordinateScale[0]=h.safeInv(I[0]),e.snapPickCoordinateScale[1]=h.safeInv(I[1]),e.snapPickCoordinateScale[2]=h.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=jo;if(v){const e=h.transformPoint3(p,c,ko);t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=B(f,t,zo),m=Qo,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this.uVectorA,e.snapVectorA),r.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(T,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(T,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(T,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uSnapVectorA;"),s.push("uniform vec2 uSnapInvVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),s.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.xyzw *= tmp;"),s.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("gl_PointSize = 1.0;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Yo=h.vec3(),Xo=h.vec3(),qo=h.vec3(),Jo=h.vec3(),Zo=h.vec3(),$o=h.mat4();class ec{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Yo;let y,m;I[0]=h.safeInv(d[3]-d[0])*h.MAX_INT,I[1]=h.safeInv(d[4]-d[1])*h.MAX_INT,I[2]=h.safeInv(d[5]-d[2])*h.MAX_INT,e.snapPickCoordinateScale[0]=h.safeInv(I[0]),e.snapPickCoordinateScale[1]=h.safeInv(I[1]),e.snapPickCoordinateScale[2]=h.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=Xo;if(v){const e=qo;h.transformPoint3(p,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=B(f,t,$o),m=Jo,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this._uVectorA,e.snapVectorA),r.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uVectorAB;"),s.push("uniform vec2 uInverseVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),s.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push(" if (isPerspectiveMatrix(projMatrix)) {"),s.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" } else {"),s.push(" if (viewNormal.z < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" }"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.z += 0.0001;"),s.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const tc=h.vec3();class sc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);let u=a.eye;e.pickViewMatrix&&(o.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),u=e.pickOrigin||u);const h=[u[0]-c[0],u[1]-c[1],u[2]-c[2]];if(r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uRenderPass,s),r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const p=i._sectionPlanesState.sectionPlanes.length;if(p>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*p,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureCameraMatrices;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec3 vWorldNormal;"),s.push("void main(void) {"),s.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),s.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),s.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("}"),s.push("}"),s.push("normal = -normal;"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vWorldNormal = normal.xyz;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.w;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}h.vec3(),h.vec3(),h.vec3(),h.vec3(),h.mat4();class nc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureColorRenderer vertex shader"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0;e 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const ic=h.vec3(),ac=h.vec3(),rc=h.vec3(),lc=h.vec3(),oc=h.mat4();class cc{constructor(e){this._scene=e,this._allocate(),this._hash=this._getHash()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:p,rotationMatrixConjugate:A}=a;if(!this._program&&(this._allocate(),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=ic;if(I){const t=h.transformPoint3(p,c,ac);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=B(i.viewMatrix,e,oc),f=rc,f[0]=i.eye[0]-e[0],f[1]=i.eye[1]-e[1],f[2]=i.eye[2]-e[2]}else d=i.viewMatrix,f=i.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const m=n._sectionPlanesState.sectionPlanes.length;if(m>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture draw vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out highp vec2 vHighPrecisionZW;"),s&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("vHighPrecisionZW = gl_Position.zw;"),n.push("}"),n.push("}"),n}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),n.push("in highp vec2 vHighPrecisionZW;"),n.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),n.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const uc=h.vec3(),hc=h.vec3(),pc=h.vec3(),Ac=h.vec3(),dc=h.mat4();class fc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin,{position:c,rotationMatrix:u,rotationMatrixConjugate:p}=n,A=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));const I=0!==o[0]||0!==o[1]||0!==o[2],y=0!==c[0]||0!==c[1]||0!==c[2];if(I||y){const e=uc;if(I){const t=hc;h.transformPoint3(u,o,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=c[0],e[1]+=c[1],e[2]+=c[2],d=B(A,e,dc),f=pc,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=A,f=a.eye;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,p),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),r.uniformMatrix4fv(this._uWorldNormalMatrix,!1,n.worldNormalMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0,s=[];return s.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec3 normal;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 worldNormalMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 viewNormalMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("varying float isPerspective;")),s.push("vec3 octDecode(vec2 oct) {"),s.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),s.push(" if (v.z < 0.0) {"),s.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),s.push(" }"),s.push(" return normalize(v);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("out vec3 vViewNormal;"),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),s.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push(" vViewNormal = viewNormal;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(ce.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&ce.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Ic=h.vec3();class yc{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerVertexIdCoordinates),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},3),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uWorldMatrix,!1,n.worldMatrix);const c=e.pickViewMatrix||a.viewMatrix,u=o?B(c,o):c;if(r.uniformMatrix4fv(this._uViewMatrix,!1,u),r.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const h=i._sectionPlanesState.sectionPlanes.length;if(h>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*h,a=n.renderFlags;for(let t=0;t0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new ge(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick flat normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),s.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform isampler2D uTexturePerPolygonIdNormals;"),s.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("out vec4 vWorldPosition;"),t&&s.push("out int vFlags2;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_normal_index = polygonIndex & 4095;"),s.push("int v_normal_index = polygonIndex >> 12;"),s.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),s.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),s.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),s.push("int objectIndex;"),s.push("if (((polygonIndex >> 3) % 2) == 0) {"),s.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),s.push("} else {"),s.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),s.push("}"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("int h_index = polygonIndex & 4095;"),s.push("int v_index = polygonIndex >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),s.push("int vertexNumber = gl_VertexID % 3;"),s.push("vec3 position;"),s.push("if (vertexNumber == 0) position = position1;"),s.push("else if (vertexNumber == 1) position = position2;"),s.push("else position = position3;"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vWorldPosition = worldPosition;"),t&&s.push(" vFlags2 = flags2.r;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals fragment shader"),n.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("in vec4 vWorldPosition;"),s){n.push("in int vFlags2;");for(var i=0;i 0;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),n.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),n.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),n.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}class mc{constructor(e){this._scene=e}_compile(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}get colorRenderer(){return this._colorRenderer||(this._colorRenderer=new uo(this._scene,!1)),this._colorRenderer}get colorRendererWithSAO(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new uo(this._scene,!0)),this._colorRendererWithSAO}get colorQualityRendererWithSAO(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}get silhouetteRenderer(){return this._silhouetteRenderer||(this._silhouetteRenderer=new mo(this._scene)),this._silhouetteRenderer}get depthRenderer(){return this._depthRenderer||(this._depthRenderer=new cc(this._scene)),this._depthRenderer}get normalsRenderer(){return this._normalsRenderer||(this._normalsRenderer=new fc(this._scene)),this._normalsRenderer}get edgesRenderer(){return this._edgesRenderer||(this._edgesRenderer=new bo(this._scene)),this._edgesRenderer}get edgesColorRenderer(){return this._edgesColorRenderer||(this._edgesColorRenderer=new _o(this._scene)),this._edgesColorRenderer}get pickMeshRenderer(){return this._pickMeshRenderer||(this._pickMeshRenderer=new xo(this._scene)),this._pickMeshRenderer}get pickNormalsRenderer(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new sc(this._scene)),this._pickNormalsRenderer}get pickNormalsFlatRenderer(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new yc(this._scene)),this._pickNormalsFlatRenderer}get pickDepthRenderer(){return this._pickDepthRenderer||(this._pickDepthRenderer=new Go(this._scene)),this._pickDepthRenderer}get vertexDepthRenderer(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new Ko(this._scene)),this._vertexDepthRenderer}get snapDepthBufInitRenderer(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new ec(this._scene)),this._snapDepthBufInitRenderer}get occlusionRenderer(){return this._occlusionRenderer||(this._occlusionRenderer=new nc(this._scene)),this._occlusionRenderer}_destroy(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}const vc={};class wc{constructor(){this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]}}class gc{constructor(){this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}finalize(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}bindCommonTextures(e,t,s,n,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,s,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,n,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}bindPickCameraTexture(e,t){}bindTriangleIndicesTextures(e,t,s,n){this.indicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.indicesPerBitnessTextures[n].bindTexture(e,s,7)}bindEdgeIndicesTextures(e,t,s,n){this.edgeIndicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[n].bindTexture(e,s,7)}}class Tc{constructor(e,t,s,n,i=null){this._gl=e,this._texture=t,this._textureWidth=s,this._textureHeight=n,this._textureData=i}bindTexture(e,t,s){return e.bindTexture(t,this,s)}bind(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}unbind(e){}}const Ec={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(Ec,null,4));let e=0;Object.keys(Ec).forEach((t=>{t.startsWith("size")&&(e+=Ec[t])})),console.log(`Total size ${e} bytes (${(e/1e3/1e3).toFixed(2)} MB)`),console.log(`Avg bytes / triangle: ${(e/Ec.totalPolygons).toFixed(2)}`);let t={};Object.keys(Ec).forEach((s=>{s.startsWith("size")&&(t[s]=`${(Ec[s]/e*100).toFixed(2)} % of total`)})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};class bc{disableBindedTextureFiltering(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}generateCameraDataTexture(e,t,s,n){const i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const a=new Tc(e,i,4,3);let r=!0;a.updateViewMatrix=(s,i)=>{e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?B(s,n):s)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};const l=()=>{r&&(r=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(()=>r=!0)),s.on("rendering",l),l(),a}generatePickCameraDataTexture(e,t,s){const n=e.createTexture();e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const i=new Tc(e,n,4,3);return i.updateViewMatrix=(n,a)=>{e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(s?B(n,s):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}generateModelTexture(e,t){const s=e.createTexture();return e.bindTexture(e.TEXTURE_2D,s),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,s,4,2)}generateTextureForColorsAndFlags(e,t,s,n,i,a,r){const l=t.length;this.numPortions=l;const o=4096,c=Math.ceil(l/512);if(0===c)throw"texture height===0";const u=new Uint8Array(16384*c);Ec.sizeDataColorsAndFlags+=u.byteLength,Ec.numberOfTextures++;for(let e=0;e>24&255,n[e]>>16&255,n[e]>>8&255,255&n[e]],32*e+16),u.set([i[e]>>24&255,i[e]>>16&255,i[e]>>8&255,255&i[e]],32*e+20),u.set([a[e]>>24&255,a[e]>>16&255,a[e]>>8&255,255&a[e]],32*e+24),u.set([r[e]?1:0,0,0,0],32*e+28);const h=e.createTexture();return e.bindTexture(e.TEXTURE_2D,h),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,o,c),e.texSubImage2D(e.TEXTURE_2D,0,0,0,o,c,e.RGBA_INTEGER,e.UNSIGNED_BYTE,u,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,h,o,c,u)}generateTextureForObjectOffsets(e,t){const s=512,n=Math.ceil(t/s);if(0===n)throw"texture height===0";const i=new Float32Array(1536*n).fill(0);Ec.sizeDataTextureOffsets+=i.byteLength,Ec.numberOfTextures++;const a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,s,n),e.texSubImage2D(e.TEXTURE_2D,0,0,0,s,n,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new Tc(e,a,s,n,i)}generateTextureForPositionsDecodeMatrices(e,t,s){const n=t.length;if(0===n)throw"num decode+entity matrices===0";const i=2048,a=Math.ceil(n/512),r=new Float32Array(8192*a);Ec.sizeDataPositionDecodeMatrices+=r.byteLength,Ec.numberOfTextures++;const l=h.mat4();for(let e=0;e{s._compile()})),e.on("destroyed",(()=>{delete vc[t],s._destroy()}))),s}(e.scene),this.model=e,this._buffer=new wc,this._dataTextureState=new gc,this._dataTextureGenerator=new bc,this._state=new je({origin:h.vec3(t.origin),metallicRoughnessBuf:null,positionsDecodeMatrix:h.mat4(),textureState:this._dataTextureState,numIndices8Bits:0,numIndices16Bits:0,numIndices32Bits:0,numEdgeIndices8Bits:0,numEdgeIndices16Bits:0,numEdgeIndices32Bits:0,numVertices:0}),this._numPortions=0,this._numVisibleLayerPortions=0,this._numTransparentLayerPortions=0,this._numXRayedLayerPortions=0,this._numSelectedLayerPortions=0,this._numHighlightedLayerPortions=0,this._numClippableLayerPortions=0,this._numEdgesLayerPortions=0,this._numPickableLayerPortions=0,this._numCulledLayerPortions=0,this._subPortions=[],this._portionToSubPortionsMap=[],this._bucketGeometries={},this.aabb=h.collapseAABB3(),this._numUpdatesInFrame=0,this._finalized=!1}canCreatePortion(e){if(this._finalized)throw"Already finalized";const t=e.buckets.length;this._numPortions+t>65536&&Ec.cannotCreatePortion.because10BitsObjectId++;let s=this._numPortions+t<=65536;const n=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#0`:`${e.id}#0`;if(!this._bucketGeometries[n]){const t=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits);let n=0,i=0;e.buckets.forEach((e=>{n+=e.positionsCompressed.length/3,i+=e.indices.length/3})),(this._state.numVertices+n>16777216||t+i>16777216)&&Ec.cannotCreatePortion.becauseTextureSize++,s&&=this._state.numVertices+n<=16777216&&t+i<=16777216}return s}createPortion(e){if(this._finalized)throw"Already finalized";const t=[],s=e.worldAABB;e.buckets.forEach(((n,i)=>{const a=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#${i}`:`${e.id}#${i}`;let r=this._bucketGeometries[a];r||(r=this._createBucketGeometry(e,n),this._bucketGeometries[a]=r);const l=h.collapseAABB3(Bc),o=this._createSubPortion(e,r,n,l);h.expandAABB3(s,l),t.push(o)}));const n=this._state.origin;0===n[0]&&0===n[1]&&0===n[2]||(s[0]+=n[0],s[1]+=n[1],s[2]+=n[2],s[3]+=n[0],s[4]+=n[1],s[5]+=n[2]),h.expandAABB3(this.aabb,s);const i=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(t),this.model.numPortions++,i}_createBucketGeometry(e,t){if(t.indices){const e=8*Math.ceil(t.indices.length/3/8)*3;Ec.overheadSizeAlignementIndices+=2*(e-t.indices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.indices),t.indices=s}if(t.edgeIndices){const e=8*Math.ceil(t.edgeIndices.length/2/8)*2;Ec.overheadSizeAlignementEdgeIndices+=2*(e-t.edgeIndices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.edgeIndices),t.edgeIndices=s}const s=t.positionsCompressed,n=t.indices,i=t.edgeIndices,a=this._buffer,r=a.positionsCompressed.length/3,l=s.length/3;for(let e=0,t=s.length;e0){let e,s=3*t.numTriangles;t.numVertices<=256?(e=u.perTriangleNumberPortionId8Bits,p.numIndices8Bits+=s,Ec.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(e=u.perTriangleNumberPortionId16Bits,p.numIndices16Bits+=s,Ec.totalPolygons16Bits+=t.numTriangles):(e=u.perTriangleNumberPortionId32Bits,p.numIndices32Bits+=s,Ec.totalPolygons32Bits+=t.numTriangles),Ec.totalPolygons+=t.numTriangles;for(let s=0;s0){let e,s=2*t.numEdges;t.numVertices<=256?(e=u.perEdgeNumberPortionId8Bits,p.numEdgeIndices8Bits+=s,Ec.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(e=u.perEdgeNumberPortionId16Bits,p.numEdgeIndices16Bits+=s,Ec.totalEdges16Bits+=t.numEdges):(e=u.perEdgeNumberPortionId32Bits,p.numEdgeIndices32Bits+=s,Ec.totalEdges32Bits+=t.numEdges),Ec.totalEdges+=t.numEdges;for(let s=0;s0&&(t.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId8Bits)),n.perEdgeNumberPortionId16Bits.length>0&&(t.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId16Bits)),n.perEdgeNumberPortionId32Bits.length>0&&(t.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId32Bits)),n.indices8Bits.length>0&&(t.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(s,n.indices8Bits)),n.indices16Bits.length>0&&(t.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(s,n.indices16Bits)),n.indices32Bits.length>0&&(t.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(s,n.indices32Bits)),n.edgeIndices8Bits.length>0&&(t.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(s,n.edgeIndices8Bits)),n.edgeIndices16Bits.length>0&&(t.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(s,n.edgeIndices16Bits)),n.edgeIndices32Bits.length>0&&(t.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(s,n.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(s,this.model)),t.textureModelMatrices=this.model._modelMatricesTexture,t.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),t.textureCameraMatrices=t.cameraTexture,t.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),t.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(()=>{this._deferredSetFlagsDirty&&this._uploadDeferredFlags(),this._numUpdatesInFrame=0}))}isEmpty(){return 0===this._numPortions}initFlags(e,t,s){t&x&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&G&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&U&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&V&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&F&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&j&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&M&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&L&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),s&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,s,true),this._setFlags2(e,t,true)}flushInitFlags(){this._setDeferredFlags(),this._setDeferredFlags2()}setVisible(e,t,s){if(!this._finalized)throw"Not finalized";t&x?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,s)}setHighlighted(e,t,s){if(!this._finalized)throw"Not finalized";t&G?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,s)}setXRayed(e,t,s){if(!this._finalized)throw"Not finalized";t&U?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,s)}setSelected(e,t,s){if(!this._finalized)throw"Not finalized";t&V?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,s)}setEdges(e,t,s){if(!this._finalized)throw"Not finalized";t&j?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,s)}setClippable(e,t){if(!this._finalized)throw"Not finalized";t&F?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}_beginDeferredFlags(){this._deferredSetFlagsActive=!0}_uploadDeferredFlags(){if(this._deferredSetFlagsActive=!1,!this._deferredSetFlagsDirty)return;this._deferredSetFlagsDirty=!1;const e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}setCulled(e,t,s){if(!this._finalized)throw"Not finalized";t&L?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,s)}setCollidable(e,t){if(!this._finalized)throw"Not finalized"}setPickable(e,t,s){if(!this._finalized)throw"Not finalized";t&M?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,s)}setColor(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdColorsAndFlags._texture),n.texSubImage2D(n.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,n.RGBA_INTEGER,n.UNSIGNED_BYTE,Cc))}setTransparent(e,t,s){s?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,s)}_setFlags(e,t,s,n=!1){const i=this._portionToSubPortionsMap[e];for(let e=0,a=i.length;e=10&&this._beginDeferredFlags(),d.bindTexture(d.TEXTURE_2D,A.texturePerObjectIdColorsAndFlags._texture),d.texSubImage2D(d.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,d.RGBA_INTEGER,d.UNSIGNED_BYTE,Cc))}_setDeferredFlags(){}_setFlags2(e,t,s=!1){const n=this._portionToSubPortionsMap[e];for(let e=0,i=n.length;e=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,Cc))}_setDeferredFlags2(){}setOffset(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdOffsets._texture),n.texSubImage2D(n.TEXTURE_2D,0,0,e,1,1,n.RGB,n.FLOAT,_c))}drawColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,Ri.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}_updateBackfaceCull(e,t){const s=this.model.backfaces||e.sectioned;if(t.backfaces!==s){const e=t.gl;s?e.disable(e.CULL_FACE):e.enable(e.CULL_FACE),t.backfaces=s}}drawColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,Ri.COLOR_TRANSPARENT))}drawDepth(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawSilhouetteXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_XRAYED))}drawSilhouetteHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_HIGHLIGHTED))}drawSilhouetteSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,Ri.SILHOUETTE_SELECTED))}drawEdgesColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,Ri.EDGES_COLOR_OPAQUE)}drawEdgesColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,Ri.EDGES_COLOR_TRANSPARENT)}drawEdgesHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_HIGHLIGHTED)}drawEdgesSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_SELECTED)}drawEdgesXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,Ri.EDGES_XRAYED)}drawOcclusion(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}drawShadow(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,Ri.COLOR_OPAQUE))}setPickMatrices(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}drawPickMesh(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,Ri.PICK))}drawPickDepths(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,Ri.PICK))}drawSnapInitDepthBuf(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,Ri.PICK))}drawSnapDepths(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,Ri.PICK))}drawPickNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,Ri.PICK))}destroy(){if(this._destroyed)return;const e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}const xc=h.vec3();h.mat4();const Lc=h.vec3([1,1,1]),Mc=h.vec3([0,0,0]),Fc=h.vec3([0,0,0]),Hc=h.identityQuaternion(),Uc=h.identityMat4(),Gc=new Uint8Array([255,255,255]);class Vc extends C{constructor(e,t={}){super(e,t),this._dtxEnabled=this.scene.dtxEnabled&&!1!==t.dtxEnabled,this._enableVertexWelding=!1,this._enableIndexBucketing=!0,this._vboBatchingLayerScratchMemory=(Pi++,Di),this._textureTranscoder=t.textureTranscoder||Yl(this.scene.viewer),this._maxGeometryBatchSize=t.maxGeometryBatchSize,this._aabb=h.collapseAABB3(),this._aabbDirty=!0,this._quantizationRanges={},this._vboInstancingLayers={},this._vboBatchingLayers={},this._dtxLayers={},this._meshList=[],this.layerList=[],this._entityList=[],this._geometries={},this._dtxBuckets={},this._textures={},this._textureSets={},this._meshes={},this._entities={},this._scheduledMeshes={},this.renderFlags=new Qs,this.numGeometries=0,this.numPortions=0,this.numVisibleLayerPortions=0,this.numTransparentLayerPortions=0,this.numXRayedLayerPortions=0,this.numHighlightedLayerPortions=0,this.numSelectedLayerPortions=0,this.numEdgesLayerPortions=0,this.numPickableLayerPortions=0,this.numClippableLayerPortions=0,this.numCulledLayerPortions=0,this.numEntities=0,this._numTriangles=0,this._numLines=0,this._numPoints=0,this._edgeThreshold=t.edgeThreshold||10,this._origin=h.vec3(t.origin||[0,0,0]),this._position=h.vec3(t.position||[0,0,0]),this._rotation=h.vec3(t.rotation||[0,0,0]),this._quaternion=h.vec4(t.quaternion||[0,0,0,1]),this._conjugateQuaternion=h.vec4(t.quaternion||[0,0,0,1]),t.rotation&&h.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._scale=h.vec3(t.scale||[1,1,1]),this._worldRotationMatrix=h.mat4(),this._worldRotationMatrixConjugate=h.mat4(),this._matrix=h.mat4(),this._matrixDirty=!0,this._rebuildMatrices(),this._worldNormalMatrix=h.mat4(),h.inverseMat4(this._matrix,this._worldNormalMatrix),h.transposeMat4(this._worldNormalMatrix),(t.matrix||t.position||t.rotation||t.scale||t.quaternion)&&(this._viewMatrix=h.mat4(),this._viewNormalMatrix=h.mat4(),this._viewMatrixDirty=!0,this._matrixNonIdentity=!0),this._opacity=1,this._colorize=[1,1,1],this._saoEnabled=!1!==t.saoEnabled,this._pbrEnabled=!1!==t.pbrEnabled,this._colorTextureEnabled=!1!==t.colorTextureEnabled,this._isModel=t.isModel,this._isModel&&this.scene._registerModel(this),this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewMatrixDirty=!0})),this.scene.vfc.enabled&&(this._vfcManager=this.scene.vfc.getVFCManager(this)),this._createDefaultTextureSet(),this.visible=t.visible,this.culled=t.culled,this.pickable=t.pickable,this.clippable=t.clippable,this.collidable=t.collidable,this.castsShadow=t.castsShadow,this.receivesShadow=t.receivesShadow,this.xrayed=t.xrayed,this.highlighted=t.highlighted,this.selected=t.selected,this.edges=t.edges,this.colorize=t.colorize,this.opacity=t.opacity,this.backfaces=t.backfaces}_createDefaultTextureSet(){const e=new Fl({id:"defaultColorTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new Fl({id:"defaultMetalRoughTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),s=new Fl({id:"defaultNormalsTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),n=new Fl({id:"defaultEmissiveTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new Fl({id:"defaultOcclusionTexture",texture:new Cn({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=s,this._textures.defaultEmissiveTexture=n,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new Ml({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:s,emissiveTexture:n,occlusionTexture:i})}get isPerformanceModel(){return!0}get objects(){return this._entities}get origin(){return this._origin}set position(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get position(){return this._position}set rotation(e){this._rotation.set(e||[0,0,0]),h.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get rotation(){return this._rotation}set quaternion(e){this._quaternion.set(e||[0,0,0,1]),h.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get quaternion(){return this._quaternion}set scale(e){}get scale(){return this._scale}set matrix(e){this._matrix.set(e||Uc),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),h.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),h.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get matrix(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix}get rotationMatrix(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}_rebuildMatrices(){this._matrixDirty&&(h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),h.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),h.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),h.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}get rotationMatrixConjugate(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}_setWorldMatrixDirty(){this._matrixDirty=!0}_setLocalAABBDirty(){for(let e=0,t=this._entityList.length;e0}set visible(e){e=!1!==e,this._visible=e;for(let t=0,s=this._entityList.length;t0}set xrayed(e){e=!!e,this._xrayed=e;for(let t=0,s=this._entityList.length;t0}set highlighted(e){e=!!e,this._highlighted=e;for(let t=0,s=this._entityList.length;t0}set selected(e){e=!!e,this._selected=e;for(let t=0,s=this._entityList.length;t0}set edges(e){e=!!e,this._edges=e;for(let t=0,s=this._entityList.length;t0}set pickable(e){e=!1!==e,this._pickable=e;for(let t=0,s=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){const t=e.colors,s=new Uint8Array(t.length);for(let e=0,n=t.length;e{o.setImage(c,{minFilter:s,magFilter:n,wrapS:i,wrapT:a,wrapR:r,flipY:e.flipY,encoding:l}),this.glRedraw()},c.src=e.src;break;default:this._textureTranscoder?y.loadArraybuffer(e.src,(e=>{e.byteLength?this._textureTranscoder.transcode([e],o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'src': file data is zero length")}),(function(e){this.error(`[createTexture] Can't create texture from 'src': ${e}`)})):this.error(`[createTexture] Can't create texture from 'src' - SceneModel needs to be configured with a TextureTranscoder for this file type ('${t}')`)}}else e.buffers&&(this._textureTranscoder?this._textureTranscoder.transcode(e.buffers,o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'buffers' - SceneModel needs to be configured with a TextureTranscoder for this option"));this._textures[t]=new Fl({id:t,texture:o})}createTextureSet(e){const t=e.id;if(null==t)return void this.error("[createTextureSet] Config missing: id");if(this._textureSets[t])return void this.error(`[createTextureSet] Texture set already created: ${t}`);let s,n,i,a,r;if(void 0!==e.colorTextureId&&null!==e.colorTextureId){if(s=this._textures[e.colorTextureId],!s)return void this.error(`[createTextureSet] Texture not found: ${e.colorTextureId} - ensure that you create it first with createTexture()`)}else s=this._textures.defaultColorTexture;if(void 0!==e.metallicRoughnessTextureId&&null!==e.metallicRoughnessTextureId){if(n=this._textures[e.metallicRoughnessTextureId],!n)return void this.error(`[createTextureSet] Texture not found: ${e.metallicRoughnessTextureId} - ensure that you create it first with createTexture()`)}else n=this._textures.defaultMetalRoughTexture;if(void 0!==e.normalsTextureId&&null!==e.normalsTextureId){if(i=this._textures[e.normalsTextureId],!i)return void this.error(`[createTextureSet] Texture not found: ${e.normalsTextureId} - ensure that you create it first with createTexture()`)}else i=this._textures.defaultNormalsTexture;if(void 0!==e.emissiveTextureId&&null!==e.emissiveTextureId){if(a=this._textures[e.emissiveTextureId],!a)return void this.error(`[createTextureSet] Texture not found: ${e.emissiveTextureId} - ensure that you create it first with createTexture()`)}else a=this._textures.defaultEmissiveTexture;if(void 0!==e.occlusionTextureId&&null!==e.occlusionTextureId){if(r=this._textures[e.occlusionTextureId],!r)return void this.error(`[createTextureSet] Texture not found: ${e.occlusionTextureId} - ensure that you create it first with createTexture()`)}else r=this._textures.defaultOcclusionTexture;const l=new Ml({id:t,model:this,colorTexture:s,metallicRoughnessTexture:n,normalsTexture:i,emissiveTexture:a,occlusionTexture:r});this._textureSets[t]=l}createMesh(e){if(void 0===e.id||null===e.id)return void this.error("[createMesh] SceneModel.createMesh() config missing: id");if(this._scheduledMeshes[e.id])return void this.error(`[createMesh] SceneModel already has a mesh with this ID: ${e.id}`);const t=!(void 0!==e.geometryId);if(e.sceneModelMatrix=this._matrixNonIdentity?this._matrix:null,t){const t=!!this._dtxEnabled;if(void 0!==e.primitive&&null!==e.primitive||(e.primitive="triangles"),"points"!==e.primitive&&"lines"!==e.primitive&&"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive)return void this.error(`Unsupported value for 'primitive': '${primitive}' ('geometryId' is absent) - supported values are 'points', 'lines', 'triangles', 'solid' and 'surface'.`);if(!e.positions&&!e.positionsCompressed&&!e.buckets)return this.error("Param expected: 'positions', 'positionsCompressed' or `buckets` ('geometryId' is absent)"),null;if(e.positions&&(e.positionsDecodeMatrix||e.positionsDecodeBoundary))return this.error("Illegal params: 'positions' not expected with 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.positionsCompressed&&!e.positionsDecodeMatrix&&!e.positionsDecodeBoundary)return this.error("Param expected: 'positionsCompressed' should be accompanied by 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.uvCompressed&&!e.uvDecodeMatrix)return this.error("Param expected: 'uvCompressed' should be accompanied by `uvDecodeMatrix` ('geometryId' is absent)"),null;if(!e.buckets&&!e.indices&&"points"!==e.primitive)return this.error(`Param expected: indices (required for '${e.primitive}' primitive type)`),null;if((e.matrix||e.position||e.rotation||e.scale)&&(e.positionsCompressed||e.positionsDecodeBoundary))return this.error("Unexpected params: 'matrix', 'rotation', 'scale', 'position' not allowed with 'positionsCompressed'"),null;if(e.origin=e.origin?h.addVec3(this._origin,e.origin,h.vec3()):this._origin,e.matrix)e.meshMatrix=e.matrix;else if(e.scale||e.rotation||e.position){const t=e.scale||Lc,s=e.position||Mc,n=e.rotation||Fc;h.eulerToQuaternion(n,"XYZ",Hc),e.meshMatrix=h.composeMat4(s,Hc,t,h.mat4())}if(e.positionsDecodeBoundary&&(e.positionsDecodeMatrix=ha(e.positionsDecodeBoundary,h.mat4())),t){if(e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.positions){const t=h.vec3(),s=[];S(e.positions,s,t)&&(e.positions=s,e.origin=h.addVec3(e.origin,t,t))}if(e.positions){const t=h.collapseAABB3();e.positionsDecodeMatrix=h.mat4(),h.expandAABB3Points3(t,e.positions),e.positionsCompressed=ua(e.positions,t,e.positionsDecodeMatrix)}e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=dt(e.positions,e.indices,null,2):e.edgeIndices=dt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.buckets||(e.buckets=jc(e,this._enableVertexWelding&&this._enableIndexBucketing))}else{if(e.type=1,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):[255,255,255],e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.positions){const t=[];S(e.positions,t,xc)&&(e.positions=t,e.origin=h.addVec3(e.origin,xc,h.vec3()))}if(e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=dt(e.positions,e.indices,null,2):e.edgeIndices=dt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId],!e.textureSet))return void this.error(`[createMesh] Texture set not found: ${e.textureSetId} - ensure that you create it first with createTextureSet()`)}}else{if(e.positions||e.positionsCompressed||e.indices||e.edgeIndices||e.normals||e.normalsCompressed||e.uv||e.uvCompressed||e.positionsDecodeMatrix)return void this.error("Mesh geometry parameters not expected when instancing a geometry (not expected: positions, positionsCompressed, indices, edgeIndices, normals, normalsCompressed, uv, uvCompressed, positionsDecodeMatrix)");if(e.geometry=this._geometries[e.geometryId],!e.geometry)return void this.error(`[createMesh] Geometry not found: ${e.geometryId} - ensure that you create it first with createGeometry()`);if(e.origin=e.origin?h.addVec3(this._origin,e.origin,h.vec3()):this._origin,e.positionsDecodeMatrix=e.geometry.positionsDecodeMatrix,e.matrix)e.meshMatrix=e.matrix.slice();else{const t=e.scale||Lc,s=e.position||Mc,n=e.rotation||Fc;h.eulerToQuaternion(n,"XYZ",Hc),e.meshMatrix=h.composeMat4(s,Hc,t,h.mat4())}if(!!this._dtxEnabled){e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255;let t=this._dtxBuckets[e.geometryId];t||(t=jc(e.geometry,this._enableVertexWelding,this._enableIndexBucketing),this._dtxBuckets[e.geometryId]=t),e.buckets=t}else e.type=0,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Gc,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId]),function(e){if(e.obb=h.OBB3(),e.positionsCompressed&&e.positionsCompressed.length>0){const t=h.collapseAABB3();h.expandAABB3Points3(t,e.positionsCompressed),gt.decompressAABB(t,e.positionsDecodeMatrix),h.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){const t=h.collapseAABB3();h.expandAABB3Points3(t,e.positions),h.AABB3ToOBB3(t,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}_createMesh(e){const t=new bi(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);const s=t.pickId,n=s>>24&255,i=s>>16&255,a=s>>8&255,r=255&s;switch(e.pickColor=new Uint8Array([r,a,i,n]),e.worldAABB=h.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=h.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,h.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}_getNumPrimitives(e){let t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(let s=0,n=e.buckets.length;s>>0).toString(16)}_getVBOInstancingLayer(e){const t=this,s=e.origin,n=e.textureSetId||"-",i=e.geometryId,a=`${Math.round(s[0])}.${Math.round(s[1])}.${Math.round(s[2])}.${n}.${i}`;let r=this._vboInstancingLayers[a];if(r)return r;let l=e.textureSet;const o=e.geometry;for(;!r;)switch(o.primitive){case"triangles":case"surface":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new Fr({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!1});break;case"solid":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new Fr({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!0});break;case"lines":console.log(`[SceneModel ${this.id}]: creating LinesInstancingLayer`),r=new il({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0});break;case"points":console.log(`[SceneModel ${this.id}]: creating PointsInstancingLayer`),r=new Ll({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0})}return this._vboInstancingLayers[a]=r,this.layerList.push(r),r}createEntity(e){if(void 0===e.id?e.id=h.createUUID():this.scene.components[e.id]&&(this.error(`Scene already has a Component with this ID: ${e.id} - will assign random ID`),e.id=h.createUUID()),void 0===e.meshIds)return void this.error("Config missing: meshIds");let t=0;if(this._visible&&!1!==e.visible&&(t|=x),this._pickable&&!1!==e.pickable&&(t|=M),this._culled&&!1!==e.culled&&(t|=L),this._clippable&&!1!==e.clippable&&(t|=F),this._collidable&&!1!==e.collidable&&(t|=H),this._edges&&!1!==e.edges&&(t|=j),this._xrayed&&!1!==e.xrayed&&(t|=U),this._highlighted&&!1!==e.highlighted&&(t|=G),this._selected&&!1!==e.selected&&(t|=V),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(let t=0,s=e.meshIds.length;t{}));for(let e=0,t=this.layerList.length;ee.sortIdt.sortId?1:0));for(let e=0,t=this.layerList.length;e0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}_updateRenderFlagsVisibleLayers(){const e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(let t=0,s=this.layerList.length;t0)for(let e=0;e0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){const t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0){this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0))}if(this.numSelectedLayerPortions>0){const t=this.scene.selectedMaterial._state;t.fill&&(t.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){const t=this.scene.highlightMaterial._state;t.fill&&(t.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}drawColorOpaque(e){const t=this.renderFlags;for(let s=0,n=t.visibleLayers.length;s65536?16:8)}else r=[{positionsCompressed:n,indices:i,edgeIndices:a}];return r}class kc extends C{constructor(e,t={}){super(e,t),this._skyboxMesh=new tn(this,{geometry:new bt(this,{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new _t(this,{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new xn(this,{src:t.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:t.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),this.size=t.size,this.active=t.active}set size(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}get size(){return this._size}set active(e){this._skyboxMesh.visible=e}get active(){return this._skyboxMesh.visible}}const Qc=h.vec4(),Wc=h.vec4(),zc=h.vec3(),Kc=h.vec3(),Yc=h.vec3(),Xc=h.vec4(),qc=h.vec4(),Jc=h.vec4();class Zc{constructor(e){this._scene=e}dollyToCanvasPos(e,t,s){let n=!1;const i=this._scene.camera;if(e){const t=h.subVec3(e,i.eye,zc);n=h.lenVec3(t){this._cameraDirty=!0})),this._onProjMatrix=this._scene.camera.on("projMatrix",(()=>{this._cameraDirty=!0})),this._onTick=this._scene.on("tick",(()=>{this.updatePivotElement()}))}updatePivotElement(){const e=this._scene.camera,t=this._scene.canvas;if(this._pivoting&&this._cameraDirty){h.transformPoint3(e.viewMatrix,this.getPivotPos(),this._pivotViewPos),this._pivotViewPos[3]=1,h.transformPoint4(e.projMatrix,this._pivotViewPos,this._pivotProjPos);const s=t.boundary,n=s[2],i=s[3];this._pivotCanvasPos[0]=Math.floor((1+this._pivotProjPos[0]/this._pivotProjPos[3])*n/2),this._pivotCanvasPos[1]=Math.floor((1-this._pivotProjPos[1]/this._pivotProjPos[3])*i/2);let a=t._lastBoundingClientRect;if(!a||t._canvasSizeChanged){const e=t.canvas;a=t._lastBoundingClientRect=e.getBoundingClientRect()}this._pivotElement&&(this._pivotElement.style.left=Math.floor(a.left+this._pivotCanvasPos[0])-this._pivotElement.clientWidth/2+window.scrollX+"px",this._pivotElement.style.top=Math.floor(a.top+this._pivotCanvasPos[1])-this._pivotElement.clientHeight/2+window.scrollY+"px"),this._cameraDirty=!1}}setPivotElement(e){this._pivotElement=e}startPivot(){if(this._cameraLookingDownwards())return this._pivoting=!1,!1;const e=this._scene.camera;let t=h.lookAtMat4v(e.eye,e.look,e.worldUp);h.transformPoint3(t,this.getPivotPos(),this._cameraOffset);const s=this.getPivotPos();this._cameraOffset[2]+=h.distVec3(e.eye,s),t=h.inverseMat4(t);const n=h.transformVec3(t,this._cameraOffset),i=h.vec3();if(h.subVec3(e.eye,s,i),h.addVec3(i,n),e.zUp){const e=i[1];i[1]=i[2],i[2]=e}this._radius=h.lenVec3(i),this._polar=Math.acos(i[1]/this._radius),this._azimuth=Math.atan2(i[0],i[2]),this._pivoting=!0}_cameraLookingDownwards(){const e=this._scene.camera,t=h.normalizeVec3(h.subVec3(e.look,e.eye,$c)),s=h.cross3Vec3(t,e.worldUp,eu);return h.sqLenVec3(s)<=1e-4}getPivoting(){return this._pivoting}setPivotPos(e){this._pivotWorldPos.set(e),this._pivotPosSet=!0}setCanvasPivotPos(e){const t=this._scene.camera,s=Math.abs(h.distVec3(this._scene.center,t.eye)),n=t.project.transposedMatrix,i=n.subarray(8,12),a=n.subarray(12),r=[0,0,-1,1],l=h.dotVec4(r,i)/h.dotVec4(r,a),o=su;t.project.unproject(e,l,nu,iu,o);const c=h.normalizeVec3(h.subVec3(o,t.eye,$c)),u=h.addVec3(t.eye,h.mulVec3Scalar(c,s,eu),tu);this.setPivotPos(u)}getPivotPos(){return this._pivotPosSet?this._pivotWorldPos:this._scene.camera.look}continuePivot(e,t){if(!this._pivoting)return;if(0===e&&0===t)return;const s=this._scene.camera;var n=-e;const i=-t;1===s.worldUp[2]&&(n=-n),this._azimuth+=.01*-n,this._polar+=.01*i,this._polar=h.clamp(this._polar,.001,Math.PI-.001);const a=[this._radius*Math.sin(this._polar)*Math.sin(this._azimuth),this._radius*Math.cos(this._polar),this._radius*Math.sin(this._polar)*Math.cos(this._azimuth)];if(1===s.worldUp[2]){const e=a[1];a[1]=a[2],a[2]=e}const r=h.lenVec3(h.subVec3(s.look,s.eye,h.vec3())),l=this.getPivotPos();h.addVec3(a,l);let o=h.lookAtMat4v(a,l,s.worldUp);o=h.inverseMat4(o);const c=h.transformVec3(o,this._cameraOffset);o[12]-=c[0],o[13]-=c[1],o[14]-=c[2];const u=[o[8],o[9],o[10]];s.eye=[o[12],o[13],o[14]],h.subVec3(s.eye,h.mulVec3Scalar(u,r),s.look),s.up=[o[4],o[5],o[6]],this.showPivot()}showPivot(){this._shown||(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this.updatePivotElement(),this._pivotElement.style.visibility="visible",this._shown=!0,this._hideTimeout=window.setTimeout((()=>{this.hidePivot()}),1e3)))}hidePivot(){this._shown&&(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this._pivotElement.style.visibility="hidden"),this._shown=!1)}endPivot(){this._pivoting=!1}destroy(){this._scene.camera.off(this._onViewMatrix),this._scene.camera.off(this._onProjMatrix),this._scene.off(this._onTick)}}class ru{constructor(e,t){this._scene=e.scene,this._cameraControl=e,this._scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},this._configs=t,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick=!1,this.pickCursorPos=h.vec2(),this.picked=!1,this.pickedSurface=!1,this.pickResult=null,this._lastPickedEntityId=null,this._needFireEvents=!1}update(){if(!this._configs.pointerEnabled)return;if(!this.schedulePickEntity&&!this.schedulePickSurface)return;this.picked=!1,this.pickedSurface=!1,this.snappedOrPicked=!1,this.hoveredSnappedOrSurfaceOff=!1,this._needFireEvents=!1;const e=this._cameraControl.hasSubs("hoverSurface");if(this.scheduleSnapOrPick){const e=this._scene.snapPick({canvasPos:this.pickCursorPos,snapRadius:this._configs.snapRadius,snapMode:this._configs.snapMode});e&&e.snappedWorldPos?(this.snapPickResult=e,this.snappedOrPicked=!0,this._needFireEvents=!0):(this.schedulePickSurface=!0,this.snapPickResult=null)}if(this.schedulePickSurface&&this.pickResult&&this.pickResult.worldPos){const t=this.pickResult.canvasPos;if(t[0]===this.pickCursorPos[0]&&t[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!0,this._needFireEvents=e,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.hoveredSnappedOrSurfaceOff=!0,void(this.scheduleSnapOrPick=!1)}if(this.schedulePickEntity&&this.pickResult&&(this.pickResult.canvasPos||this.pickResult.snappedCanvasPos)){const e=this.pickResult.canvasPos||this.pickResult.snappedCanvasPos;if(e[0]===this.pickCursorPos[0]&&e[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!1,this.schedulePickEntity=!1,void(this.schedulePickSurface=!1)}this.schedulePickSurface||this.scheduleSnapOrPick&&!this.snapPickResult?(this.pickResult=this._scene.pick({pickSurface:!0,pickSurfaceNormal:!1,canvasPos:this.pickCursorPos}),this.pickResult?(this.picked=!0,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.pickedSurface=!0,this._needFireEvents=!0):this.scheduleSnapOrPick&&(this.hoveredSnappedOrSurfaceOff=!0,this._needFireEvents=!0)):(this.pickResult=this._scene.pick({canvasPos:this.pickCursorPos}),this.pickResult&&(this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!0)),this.scheduleSnapOrPick=!1,this.schedulePickEntity=!1,this.schedulePickSurface=!1}fireEvents(){if(this._needFireEvents){if(this.hoveredSnappedOrSurfaceOff&&this._cameraControl.fire("hoverSnapOrSurfaceOff",{canvasPos:this.pickCursorPos},!0),this.snappedOrPicked)if(this.snapPickResult){const e=new de;e.worldPos=this.snapPickResult.snappedWorldPos,e.canvasPos=this.snapPickResult.snappedCanvasPos,this._cameraControl.fire("hoverSnapOrSurface",e,!0),this.snapPickResult=null}else this._cameraControl.fire("hoverSnapOrSurface",this.pickResult,!0);if(this.picked&&this.pickResult&&(this.pickResult.entity||this.pickResult.worldPos)){if(this.pickResult.entity){const e=this.pickResult.entity.id;this._lastPickedEntityId!==e&&(void 0!==this._lastPickedEntityId&&this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._cameraControl.fire("hoverEnter",this.pickResult,!0),this._lastPickedEntityId=e)}this._cameraControl.fire("hover",this.pickResult,!0),this.pickResult.worldPos&&(this.pickedSurface=!0,this._cameraControl.fire("hoverSurface",this.pickResult,!0))}else void 0!==this._lastPickedEntityId&&(this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),this._cameraControl.fire("hoverOff",{canvasPos:this.pickCursorPos},!0);this.pickResult=null,this._needFireEvents=!1}}destroy(){}}const lu=h.vec2();class ou{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController;let r,l,o,c=0,u=0,p=0,A=0,d=!1;const f=h.vec3();let I=!0;const y=this._scene.canvas.canvas,m=[];function v(e=!0){y.style.cursor="move",c=n.pointerCanvasPos[0],u=n.pointerCanvasPos[1],p=n.pointerCanvasPos[0],A=n.pointerCanvasPos[1],e&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(d=!0,f.set(a.pickResult.worldPos)):d=!1)}document.addEventListener("keydown",this._documentKeyDownHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!0}),document.addEventListener("keyup",this._documentKeyUpHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!1}),y.addEventListener("mousedown",this._mouseDownHandler=t=>{if(s.active&&s.pointerEnabled)switch(t.which){case 1:m[e.input.KEY_SHIFT]||s.planView?(r=!0,v()):(r=!0,v(!1));break;case 2:l=!0,v();break;case 3:o=!0,s.panRightClick&&v()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=()=>{if(!s.active||!s.pointerEnabled)return;if(!r&&!l&&!o)return;const t=e.canvas.boundary,a=t[2],p=t[3],A=n.pointerCanvasPos[0],I=n.pointerCanvasPos[1];if(m[e.input.KEY_SHIFT]||s.planView||!s.panRightClick&&l||s.panRightClick&&o){const t=A-c,s=I-u,n=e.camera;if("perspective"===n.projection){const a=Math.abs(d?h.lenVec3(h.subVec3(f,e.camera.eye,[])):e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=1.5*t*a/p,i.panDeltaY+=1.5*s*a/p}else i.panDeltaX+=.5*n.ortho.scale*(t/p),i.panDeltaY+=.5*n.ortho.scale*(s/p)}else!r||l||o||s.planView||(s.firstPerson?(i.rotateDeltaY-=(A-c)/a*s.dragRotationRate/2,i.rotateDeltaX+=(I-u)/p*(s.dragRotationRate/4)):(i.rotateDeltaY-=(A-c)/a*(1.5*s.dragRotationRate),i.rotateDeltaX+=(I-u)/p*(1.5*s.dragRotationRate)));c=A,u=I}),y.addEventListener("mousemove",this._canvasMouseMoveHandler=e=>{s.active&&s.pointerEnabled&&n.mouseover&&(I=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{if(s.active&&s.pointerEnabled)switch(e.which){case 1:case 2:case 3:r=!1,l=!1,o=!1}}),y.addEventListener("mouseup",this._mouseUpHandler=e=>{if(s.active&&s.pointerEnabled){if(3===e.which){!function(e,t){if(e){let s=e.target,n=0,i=0,a=0,r=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,a+=s.scrollLeft,r+=s.scrollTop,s=s.offsetParent;t[0]=e.pageX+a-n,t[1]=e.pageY+r-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,lu);const s=lu[0],n=lu[1];Math.abs(s-p)<3&&Math.abs(n-A)<3&&t.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:lu,event:e},!0)}y.style.removeProperty("cursor")}}),y.addEventListener("mouseenter",this._mouseEnterHandler=()=>{s.active&&s.pointerEnabled});const w=1/60;let g=null;y.addEventListener("wheel",this._mouseWheelHandler=e=>{if(!s.active||!s.pointerEnabled)return;const t=performance.now()/1e3;var a=null!==g?t-g:0;g=t,a>.05&&(a=.05),a{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const r=i._isKeyDownForAction(i.AXIS_VIEW_RIGHT),l=i._isKeyDownForAction(i.AXIS_VIEW_BACK),o=i._isKeyDownForAction(i.AXIS_VIEW_LEFT),c=i._isKeyDownForAction(i.AXIS_VIEW_FRONT),u=i._isKeyDownForAction(i.AXIS_VIEW_TOP),p=i._isKeyDownForAction(i.AXIS_VIEW_BOTTOM);if(!(r||l||o||c||u||p))return;const A=e.aabb,d=h.getAABB3Diag(A);h.getAABB3Center(A,cu);const f=Math.abs(d/Math.tan(t.cameraFlight.fitFOV*h.DEGTORAD)),I=1.1*d;du.orthoScale=I,r?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldRight,f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):l?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldForward,f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):o?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldRight,-f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):c?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldForward,-f,uu),Au)),du.look.set(cu),du.up.set(a.worldUp)):u?(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldUp,f,uu),Au)),du.look.set(cu),du.up.set(h.normalizeVec3(h.mulVec3Scalar(a.worldForward,1,hu),pu))):p&&(du.eye.set(h.addVec3(cu,h.mulVec3Scalar(a.worldUp,-f,uu),Au)),du.look.set(cu),du.up.set(h.normalizeVec3(h.mulVec3Scalar(a.worldForward,-1,hu)))),!s.firstPerson&&s.followPointer&&t.pivotController.setPivotPos(cu),t.cameraFlight.duration>0?t.cameraFlight.flyTo(du,(()=>{t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot()})):(t.cameraFlight.jumpTo(du),t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot())}))}reset(){}destroy(){this._scene.input.off(this._onSceneKeyDown)}}class Iu{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=t.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;let o=!1,c=!1;const u=this._scene.canvas.canvas,p=s=>{let n;s&&s.worldPos&&(n=s.worldPos);const i=s&&s.entity?s.entity.aabb:e.aabb;if(n){const s=e.camera;h.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};u.addEventListener("mousemove",this._canvasMouseMoveHandler=t=>{if(!s.active||!s.pointerEnabled)return;if(o||c)return;const i=l.hasSubs("hover"),r=l.hasSubs("hoverOut"),u=l.hasSubs("hoverOff"),h=l.hasSubs("hoverSurface"),p=l.hasSubs("hoverSnapOrSurface");if(i||r||u||h||p)if(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=h,a.scheduleSnapOrPick=p,a.update(),a.pickResult){if(a.pickResult.entity){const t=a.pickResult.entity.id;this._lastPickedEntityId!==t&&(void 0!==this._lastPickedEntityId&&l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),l.fire("hoverEnter",a.pickResult,!0),this._lastPickedEntityId=t)}l.fire("hover",a.pickResult,!0),(a.pickResult.worldPos||a.pickResult.snappedWorldPos)&&l.fire("hoverSurface",a.pickResult,!0)}else void 0!==this._lastPickedEntityId&&(l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),l.fire("hoverOff",{canvasPos:a.pickCursorPos},!0)}),u.addEventListener("mousedown",this._canvasMouseDownHandler=t=>{1===t.which&&(o=!0),3===t.which&&(c=!0);if(1===t.which&&s.active&&s.pointerEnabled&&(n.mouseDownClientX=t.clientX,n.mouseDownClientY=t.clientY,n.mouseDownCursorX=n.pointerCanvasPos[0],n.mouseDownCursorY=n.pointerCanvasPos[1],!s.firstPerson&&s.followPointer&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),1===t.which))){const t=a.pickResult;t&&t.worldPos?(r.setPivotPos(t.worldPos),r.startPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),r.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{1===e.which&&(o=!1),3===e.which&&(c=!1)}),u.addEventListener("mouseup",this._canvasMouseUpHandler=i=>{if(!s.active||!s.pointerEnabled)return;if(!(1===i.which))return;if(r.hidePivot(),Math.abs(i.clientX-n.mouseDownClientX)>3||Math.abs(i.clientY-n.mouseDownClientY)>3)return;const o=l.hasSubs("picked"),c=l.hasSubs("pickedNothing"),u=l.hasSubs("pickedSurface"),A=l.hasSubs("doublePicked"),d=l.hasSubs("doublePickedSurface"),f=l.hasSubs("doublePickedNothing");if(!(s.doublePickFlyTo||A||d||f))return(o||c||u)&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=u,a.update(),a.pickResult?(l.fire("picked",a.pickResult,!0),a.pickedSurface&&l.fire("pickedSurface",a.pickResult,!0)):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0)),void(this._clicks=0);if(this._clicks++,1===this._clicks){a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo,a.schedulePickSurface=u,a.update();const e=a.pickResult,i=a.pickedSurface;this._timeout=setTimeout((()=>{e?(l.fire("picked",e,!0),i&&(l.fire("pickedSurface",e,!0),!s.firstPerson&&s.followPointer&&(t.pivotController.setPivotPos(e.worldPos),t.pivotController.startPivot()&&t.pivotController.showPivot()))):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0),this._clicks=0}),s.doubleClickTimeFrame)}else{if(null!==this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null),a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo||A||d,a.schedulePickSurface=a.schedulePickEntity&&d,a.update(),a.pickResult){if(l.fire("doublePicked",a.pickResult,!0),a.pickedSurface&&l.fire("doublePickedSurface",a.pickResult,!0),s.doublePickFlyTo&&(p(a.pickResult),!s.firstPerson&&s.followPointer)){const e=a.pickResult.entity.aabb,s=h.getAABB3Center(e);t.pivotController.setPivotPos(s),t.pivotController.startPivot()&&t.pivotController.showPivot()}}else if(l.fire("doublePickedNothing",{canvasPos:n.pointerCanvasPos},!0),s.doublePickFlyTo&&(p(),!s.firstPerson&&s.followPointer)){const s=e.aabb,n=h.getAABB3Center(s);t.pivotController.setPivotPos(n),t.pivotController.startPivot()&&t.pivotController.showPivot()}this._clicks=0}},!1)}reset(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}destroy(){const e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}class yu{constructor(e,t,s,n,i){this._scene=e;const a=e.input,r=[],l=e.canvas.canvas;let o=!0;this._onSceneMouseMove=a.on("mousemove",(()=>{o=!0})),this._onSceneKeyDown=a.on("keydown",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&n.mouseover&&(r[t]=!0,t===a.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=a.on("keyup",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&(r[t]=!1,t===a.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=e.on("tick",(l=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const c=t.cameraControl,u=l.deltaTime/1e3;if(!s.planView){const e=c._isKeyDownForAction(c.ROTATE_Y_POS,r),n=c._isKeyDownForAction(c.ROTATE_Y_NEG,r),a=c._isKeyDownForAction(c.ROTATE_X_POS,r),l=c._isKeyDownForAction(c.ROTATE_X_NEG,r),o=u*s.keyboardRotationRate;(e||n||a||l)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),e?i.rotateDeltaY+=o:n&&(i.rotateDeltaY-=o),a?i.rotateDeltaX+=o:l&&(i.rotateDeltaX-=o),!s.firstPerson&&s.followPointer&&t.pivotController.startPivot())}if(!r[a.KEY_CTRL]&&!r[a.KEY_ALT]){const e=c._isKeyDownForAction(c.DOLLY_BACKWARDS,r),a=c._isKeyDownForAction(c.DOLLY_FORWARDS,r);if(e||a){const r=u*s.keyboardDollyRate;!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),a?i.dollyDelta-=r:e&&(i.dollyDelta+=r),o&&(n.followPointerDirty=!0,o=!1)}}const h=c._isKeyDownForAction(c.PAN_FORWARDS,r),p=c._isKeyDownForAction(c.PAN_BACKWARDS,r),A=c._isKeyDownForAction(c.PAN_LEFT,r),d=c._isKeyDownForAction(c.PAN_RIGHT,r),f=c._isKeyDownForAction(c.PAN_UP,r),I=c._isKeyDownForAction(c.PAN_DOWN,r),y=(r[a.KEY_ALT]?.3:1)*u*s.keyboardPanRate;(h||p||A||d||f||I)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),I?i.panDeltaY+=y:f&&(i.panDeltaY+=-y),d?i.panDeltaX+=-y:A&&(i.panDeltaX+=y),p?i.panDeltaZ+=y:h&&(i.panDeltaZ+=-y))}))}reset(){}destroy(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}const mu=h.vec3();class vu{constructor(e,t,s,n,i){this._scene=e;const a=e.camera,r=t.pickController,l=t.pivotController,o=t.panController;let c=1,u=1,p=null;this._onTick=e.on("tick",(()=>{if(!s.active||!s.pointerEnabled)return;let t="default";if(Math.abs(i.dollyDelta)<.001&&(i.dollyDelta=0),Math.abs(i.rotateDeltaX)<.001&&(i.rotateDeltaX=0),Math.abs(i.rotateDeltaY)<.001&&(i.rotateDeltaY=0),0===i.rotateDeltaX&&0===i.rotateDeltaY||(i.dollyDelta=0),s.followPointer&&--c<=0&&(c=1,0!==i.dollyDelta)){if(0===i.rotateDeltaY&&0===i.rotateDeltaX&&s.followPointer&&n.followPointerDirty&&(r.pickCursorPos=n.pointerCanvasPos,r.schedulePickSurface=!0,r.update(),r.pickResult&&r.pickResult.worldPos?p=r.pickResult.worldPos:(u=1,p=null),n.followPointerDirty=!1),p){const t=Math.abs(h.lenVec3(h.subVec3(p,e.camera.eye,mu)));u=t/s.dollyProximityThreshold}u{n.mouseover=!0}),a.addEventListener("mouseleave",this._mouseLeaveHandler=()=>{n.mouseover=!1,a.style.cursor=null}),document.addEventListener("mousemove",this._mouseMoveHandler=e=>{gu(e,a,n.pointerCanvasPos)}),a.addEventListener("mousedown",this._mouseDownHandler=e=>{s.active&&s.pointerEnabled&&(gu(e,a,n.pointerCanvasPos),n.mouseover=!0)}),a.addEventListener("mouseup",this._mouseUpHandler=e=>{s.active&&s.pointerEnabled})}reset(){}destroy(){const e=this._scene.canvas.canvas;document.removeEventListener("mousemove",this._mouseMoveHandler),e.removeEventListener("mouseenter",this._mouseEnterHandler),e.removeEventListener("mouseleave",this._mouseLeaveHandler),e.removeEventListener("mousedown",this._mouseDownHandler),e.removeEventListener("mouseup",this._mouseUpHandler)}}function gu(e,t,s){if(e){const{x:n,y:i}=t.getBoundingClientRect();s[0]=e.clientX-n,s[1]=e.clientY-i}else e=window.event,s[0]=e.x,s[1]=e.y;return s}const Tu=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class Eu{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=h.vec2(),o=h.vec2(),c=h.vec2(),u=h.vec2(),p=[],A=this._scene.canvas.canvas;let d=0,f=!1;this._onTick=e.on("tick",(()=>{f=!1})),A.addEventListener("touchstart",this._canvasTouchStartHandler=t=>{if(!s.active||!s.pointerEnabled)return;t.preventDefault();const i=t.touches,o=t.changedTouches;for(n.touchStartTime=Date.now(),1===i.length&&1===o.length&&(Tu(i[0],l),s.followPointer&&(a.pickCursorPos=l,a.schedulePickSurface=!0,a.update(),s.planView||(a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(r.setPivotPos(a.pickResult.worldPos),!s.firstPerson&&r.startPivot()&&r.showPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),!s.firstPerson&&r.startPivot()&&r.showPivot()))));p.length{if(!s.active||!s.pointerEnabled)return;if(t.stopPropagation(),t.preventDefault(),f)return;f=!0;const r=e.canvas.boundary,l=r[2],A=r[3],I=t.touches;if(t.touches.length===d){if(1===d){Tu(I[0],o),h.subVec2(o,p[0],u);const t=u[0],a=u[1];if(null!==n.longTouchTimeout&&(Math.abs(t)>s.longTapRadius||Math.abs(a)>s.longTapRadius)&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),s.planView){const n=e.camera;if("perspective"===n.projection){const r=Math.abs(e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=t*r/A*s.touchPanRate,i.panDeltaY+=a*r/A*s.touchPanRate}else i.panDeltaX+=.5*n.ortho.scale*(t/A)*s.touchPanRate,i.panDeltaY+=.5*n.ortho.scale*(a/A)*s.touchPanRate}else i.rotateDeltaY-=t/l*(1*s.dragRotationRate),i.rotateDeltaX+=a/A*(1.5*s.dragRotationRate)}else if(2===d){const t=I[0],r=I[1];Tu(t,o),Tu(r,c);const l=h.geometricMeanVec2(p[0],p[1]),u=h.geometricMeanVec2(o,c),d=h.vec2();h.subVec2(l,u,d);const f=d[0],y=d[1],m=e.camera,v=h.distVec2([t.pageX,t.pageY],[r.pageX,r.pageY]),w=(h.distVec2(p[0],p[1])-v)*s.touchDollyRate;if(i.dollyDelta=w,Math.abs(w)<1)if("perspective"===m.projection){const t=a.pickResult?a.pickResult.worldPos:e.center,n=Math.abs(h.lenVec3(h.subVec3(t,e.camera.eye,[])))*Math.tan(m.perspective.fov/2*Math.PI/180);i.panDeltaX-=f*n/A*s.touchPanRate,i.panDeltaY-=y*n/A*s.touchPanRate}else i.panDeltaX-=.5*m.ortho.scale*(f/A)*s.touchPanRate,i.panDeltaY-=.5*m.ortho.scale*(y/A)*s.touchPanRate;n.pointerCanvasPos=u}for(let e=0;e{let n;s&&s.worldPos&&(n=s.worldPos);const i=s?s.entity.aabb:e.aabb;if(n){const s=e.camera;h.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};A.addEventListener("touchstart",this._canvasTouchStartHandler=e=>{if(!s.active||!s.pointerEnabled)return;null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null);const i=e.touches,a=e.changedTouches;if(l=Date.now(),1===i.length&&1===a.length){u=l,bu(i[0],c);const a=c[0],r=c[1],o=i[0].pageX,h=i[0].pageY;n.longTouchTimeout=setTimeout((()=>{t.cameraControl.fire("rightClick",{pagePos:[Math.round(o),Math.round(h)],canvasPos:[Math.round(a),Math.round(r)],event:e},!0),n.longTouchTimeout=null}),s.longTapTimeout)}else u=-1;for(;o.length{if(!s.active||!s.pointerEnabled)return;const t=Date.now(),i=e.touches,l=e.changedTouches,A=r.hasSubs("pickedSurface");null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),0===i.length&&1===l.length&&u>-1&&t-u<150&&(p>-1&&u-p<325?(bu(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("doublePicked",a.pickResult),a.pickedSurface&&r.fire("doublePickedSurface",a.pickResult),s.doublePickFlyTo&&d(a.pickResult)):(r.fire("doublePickedNothing"),s.doublePickFlyTo&&d()),p=-1):h.distVec2(o[0],c)<4&&(bu(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("picked",a.pickResult),a.pickedSurface&&r.fire("pickedSurface",a.pickResult)):r.fire("pickedNothing"),p=t),u=-1),o.length=i.length;for(let e=0,t=i.length;e{e.preventDefault()},this._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},this._states={pointerCanvasPos:h.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:h.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},this._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};const s=this.scene;this._controllers={cameraControl:this,pickController:new ru(this,this._configs),pivotController:new au(s,this._configs),panController:new Zc(s),cameraFlight:new pi(this,{duration:.5})},this._handlers=[new wu(this.scene,this._controllers,this._configs,this._states,this._updates),new Eu(this.scene,this._controllers,this._configs,this._states,this._updates),new ou(this.scene,this._controllers,this._configs,this._states,this._updates),new fu(this.scene,this._controllers,this._configs,this._states,this._updates),new Iu(this.scene,this._controllers,this._configs,this._states,this._updates),new Du(this.scene,this._controllers,this._configs,this._states,this._updates),new yu(this.scene,this._controllers,this._configs,this._states,this._updates)],this._cameraUpdater=new vu(this.scene,this._controllers,this._configs,this._states,this._updates),this.navMode=t.navMode,t.planView&&(this.planView=t.planView),this.constrainVertical=t.constrainVertical,t.keyboardLayout?this.keyboardLayout=t.keyboardLayout:this.keyMap=t.keyMap,this.doublePickFlyTo=t.doublePickFlyTo,this.panRightClick=t.panRightClick,this.active=t.active,this.followPointer=t.followPointer,this.rotationInertia=t.rotationInertia,this.keyboardPanRate=t.keyboardPanRate,this.touchPanRate=t.touchPanRate,this.keyboardRotationRate=t.keyboardRotationRate,this.dragRotationRate=t.dragRotationRate,this.touchDollyRate=t.touchDollyRate,this.dollyInertia=t.dollyInertia,this.dollyProximityThreshold=t.dollyProximityThreshold,this.dollyMinSpeed=t.dollyMinSpeed,this.panInertia=t.panInertia,this.pointerEnabled=!0,this.keyboardDollyRate=t.keyboardDollyRate,this.mouseWheelDollyRate=t.mouseWheelDollyRate}set keyMap(e){if(e=e||"qwerty",y.isString(e)){const t=this.scene.input,s={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":s[this.PAN_LEFT]=[t.KEY_A],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_Z],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":s[this.PAN_LEFT]=[t.KEY_Q],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_W],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=s}else{const t=e;this._keyMap=t}}get keyMap(){return this._keyMap}_isKeyDownForAction(e,t){const s=this._keyMap[e];if(!s)return!1;t||(t=this.scene.input.keyDown);for(let e=0,n=s.length;e0?Su(t):null,r=s&&s.length>0?Su(s):null,l=e=>{if(!e)return;var t=!0;(r&&r[e.type]||a&&!a[e.type])&&(t=!1),t&&n.push(e.id);const s=e.children;if(s)for(var i=0,o=s.length;i * Copyright (c) 2022 Niklas von Hertzen diff --git a/dist/xeokit-sdk.min.es.js b/dist/xeokit-sdk.min.es.js index ba0b4ce45..b23bdff6c 100644 --- a/dist/xeokit-sdk.min.es.js +++ b/dist/xeokit-sdk.min.es.js @@ -23,7 +23,7 @@ function as(e,t){t.length;const s=e.length,n=[],i=[],a=e=>{for(let s=0,n=e.meshI /** * @author https://github.com/tmarti, with support from https://tribia.com/ * @license MIT - **/let Mc=null;function Fc(e,t){const s=3*e,n=3*t;let i,a,r,l,o,c;const u=Math.min(i=Mc[s],a=Mc[s+1],r=Mc[s+2]),h=Math.min(l=Mc[n],o=Mc[n+1],c=Mc[n+2]);if(u!==h)return u-h;const p=Math.max(i,a,r),A=Math.max(l,o,c);return p!==A?p-A:0}let Hc=null;function Uc(e,t){let s=Hc[2*e]-Hc[2*t];return 0!==s?s:Hc[2*e+1]-Hc[2*t+1]}function Gc(e,t,s=!1){const n=e.positionsCompressed||[],i=function(e,t){const s=new Int32Array(e.length/3);for(let e=0,t=s.length;e>t;s.sort(Fc);const n=new Int32Array(e.length);for(let t=0,i=s.length;te[t+1]){let s=e[t];e[t]=e[t+1],e[t+1]=s}Hc=new Int32Array(e),t.sort(Uc);const s=new Int32Array(e.length);for(let n=0,i=t.length;nt){let s=e;e=t,t=s}function s(s,n){return s!==e?e-s:n!==t?t-n:0}let n=0,i=(a.length>>1)-1;for(;n<=i;){const e=i+n>>1,t=s(a[2*e],a[2*e+1]);if(t>0)n=e+1;else{if(!(t<0))return e;i=e-1}}return-n-1}const l=new Int32Array(a.length/2);l.fill(0);const o=n.length/3;if(o>8*(1<p.maxNumPositions&&(p=h()),p.bucketNumber>8)return[e];let d;-1===c[o]&&(c[o]=p.numPositions++,p.positionsCompressed.push(n[3*o]),p.positionsCompressed.push(n[3*o+1]),p.positionsCompressed.push(n[3*o+2])),-1===c[u]&&(c[u]=p.numPositions++,p.positionsCompressed.push(n[3*u]),p.positionsCompressed.push(n[3*u+1]),p.positionsCompressed.push(n[3*u+2])),-1===c[A]&&(c[A]=p.numPositions++,p.positionsCompressed.push(n[3*A]),p.positionsCompressed.push(n[3*A+1]),p.positionsCompressed.push(n[3*A+2])),p.indices.push(c[o]),p.indices.push(c[u]),p.indices.push(c[A]),(d=r(o,u))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(o,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(u,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]]))}const A=t/8*2,d=t/8,f=2*n.length+(i.length+a.length)*A;let I=0,y=-n.length/3;return u.forEach((e=>{I+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*d,y+=e.positionsCompressed.length/3})),I>f?[e]:(s&&function(e,t){const s={},n={};let i=0;e.forEach((e=>{const t=e.indices,a=e.edgeIndices,r=e.positionsCompressed;for(let e=0,n=t.length;e0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl,s=e._lightsState;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uLightAmbient=n.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];const i=s.lights;let a;for(let e=0,t=i.length;e0;let i;const a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),e.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(let e=0,t=s.lights.length;e> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(let e=0,t=s.lights.length;e0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),this._withSAO&&(n.push("uniform sampler2D uOcclusionTexture;"),n.push("uniform vec4 uSAOParams;"),n.push("const float packUpscale = 256. / 255.;"),n.push("const float unpackDownScale = 255. / 256.;"),n.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),n.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),n.push("float unpackRGBToFloat( const in vec4 v ) {"),n.push(" return dot( v, unPackFactors );"),n.push("}")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(n.push(" float viewportWidth = uSAOParams[0];"),n.push(" float viewportHeight = uSAOParams[1];"),n.push(" float blendCutoff = uSAOParams[2];"),n.push(" float blendFactor = uSAOParams[3];"),n.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),n.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),n.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Yc=new Float32Array([1,1,1]),Xc=p.vec3(),qc=p.vec3(),Jc=p.vec3(),Zc=p.vec3(),$c=p.mat4();class eu{constructor(e,t){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=a,d=i.viewMatrix;if(!this._program&&(this._allocate(),this.errors))return;let f,I;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),c||0!==u[0]||0!==u[1]||0!==u[2]){const e=Xc;if(c){const t=qc;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,$c),I=Jc,I[0]=i.eye[0]-e[0],I[1]=i.eye[1]-e[1],I[2]=i.eye[2]-e[2]}else f=d,I=i.eye;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s===cr.SILHOUETTE_XRAYED){const e=n.xrayMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.SILHOUETTE_HIGHLIGHTED){const e=n.highlightMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.SILHOUETTE_SELECTED){const e=n.selectedMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,Yc);if(n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=n._sectionPlanesState.sectionPlanes.length;if(y>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture silhouette vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.y) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("} else {"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = color;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const tu=new Float32Array([0,0,0,1]),su=p.vec3(),nu=p.vec3();p.vec3();const iu=p.vec3(),au=p.mat4();class ru{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=su;if(I){const t=nu;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,au)}else f=d;if(r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s===cr.EDGES_XRAYED){const e=i.xrayMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.EDGES_HIGHLIGHTED){const e=i.highlightMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.EDGES_SELECTED){const e=i.selectedMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,tu);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vColor = vec4(color.r, color.g, color.b, color.a);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const lu=p.vec3(),ou=p.vec3(),cu=p.vec3(),uu=p.mat4();class hu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=lu;if(I){const t=ou;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,uu)}else f=d;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vec4 rgb = vec4(color.rgba);"),s.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const pu=p.vec3(),Au=p.vec3(),du=p.vec3(),fu=p.mat4();class Iu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(t),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n;let d,f;o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=pu;if(I){const t=p.transformPoint3(h,c,Au);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=O(a.viewMatrix,e,fu),f=du,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=a.viewMatrix,f=a.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry picking vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("smooth out vec4 vWorldPosition;"),s.push("flat out uvec4 vFlags2;")),s.push("out vec4 vPickColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry picking fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outPickColor = vPickColor; "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const yu=p.vec3(),mu=p.vec3(),vu=p.vec3(),wu=p.vec3(),gu=p.mat4();class Tu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=e.pickViewMatrix||a.viewMatrix;let f,I;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),a.eye,c||0!==u[0]||0!==u[1]||0!==u[2]){const t=yu;if(c){const e=mu;p.transformPoint3(h,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],f=O(d,t,gu),I=vu,I[0]=a.eye[0]-t[0],I[1]=a.eye[1]-t[1],I[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else f=d,I=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniform1f(this._uPickZNear,e.pickZNear),r.uniform1f(this._uPickZFar,e.pickZFar),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=i._sectionPlanesState.sectionPlanes.length;if(y>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick depth vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform float pickZNear;"),n.push("uniform float pickZFar;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),n.push(" outPackedDepth = packDepth(zNormalizedDepth); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Eu=p.vec3(),bu=p.vec3(),Du=p.vec3(),Pu=p.vec3(),Ru=p.vec3(),Cu=p.mat4();class _u{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Eu;let y,m;I[0]=p.safeInv(d[3]-d[0])*p.MAX_INT,I[1]=p.safeInv(d[4]-d[1])*p.MAX_INT,I[2]=p.safeInv(d[5]-d[2])*p.MAX_INT,e.snapPickCoordinateScale[0]=p.safeInv(I[0]),e.snapPickCoordinateScale[1]=p.safeInv(I[1]),e.snapPickCoordinateScale[2]=p.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=bu;if(v){const e=p.transformPoint3(h,c,Du);t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=O(f,t,Cu),m=Pu,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this.uVectorA,e.snapVectorA),r.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(T,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(T,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(T,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uSnapVectorA;"),s.push("uniform vec2 uSnapInvVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),s.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.xyzw *= tmp;"),s.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("gl_PointSize = 1.0;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Bu=p.vec3(),Ou=p.vec3(),Su=p.vec3(),Nu=p.vec3(),xu=p.vec3(),Lu=p.mat4();class Mu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Bu;let y,m;I[0]=p.safeInv(d[3]-d[0])*p.MAX_INT,I[1]=p.safeInv(d[4]-d[1])*p.MAX_INT,I[2]=p.safeInv(d[5]-d[2])*p.MAX_INT,e.snapPickCoordinateScale[0]=p.safeInv(I[0]),e.snapPickCoordinateScale[1]=p.safeInv(I[1]),e.snapPickCoordinateScale[2]=p.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=Ou;if(v){const e=Su;p.transformPoint3(h,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=O(f,t,Lu),m=Nu,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this._uVectorA,e.snapVectorA),r.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uVectorAB;"),s.push("uniform vec2 uInverseVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),s.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push(" if (isPerspectiveMatrix(projMatrix)) {"),s.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" } else {"),s.push(" if (viewNormal.z < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" }"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.z += 0.0001;"),s.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Fu=p.vec3();class Hu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);let u=a.eye;e.pickViewMatrix&&(o.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),u=e.pickOrigin||u);const h=[u[0]-c[0],u[1]-c[1],u[2]-c[2]];if(r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uRenderPass,s),r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const p=i._sectionPlanesState.sectionPlanes.length;if(p>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*p,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureCameraMatrices;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec3 vWorldNormal;"),s.push("void main(void) {"),s.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),s.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),s.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("}"),s.push("}"),s.push("normal = -normal;"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vWorldNormal = normal.xyz;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.w;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}p.vec3(),p.vec3(),p.vec3(),p.vec3(),p.mat4();class Uu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("// TrianglesDataTextureColorRenderer vertex shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("// TrianglesDataTextureColorRenderer fragment shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0;e 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Gu=p.vec3(),Vu=p.vec3(),ju=p.vec3(),ku=p.vec3(),Qu=p.mat4();class Wu{constructor(e){this._scene=e,this._allocate(),this._hash=this._getHash()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=a;if(!this._program&&(this._allocate(),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Gu;if(I){const t=p.transformPoint3(h,c,Vu);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=O(i.viewMatrix,e,Qu),f=ju,f[0]=i.eye[0]-e[0],f[1]=i.eye[1]-e[1],f[2]=i.eye[2]-e[2]}else d=i.viewMatrix,f=i.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const m=n._sectionPlanesState.sectionPlanes.length;if(m>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture draw vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out highp vec2 vHighPrecisionZW;"),s&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("vHighPrecisionZW = gl_Position.zw;"),n.push("}"),n.push("}"),n}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),n.push("in highp vec2 vHighPrecisionZW;"),n.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),n.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const zu=p.vec3(),Ku=p.vec3(),Yu=p.vec3(),Xu=p.vec3(),qu=p.mat4();class Ju{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin,{position:c,rotationMatrix:u,rotationMatrixConjugate:h}=n,A=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));const I=0!==o[0]||0!==o[1]||0!==o[2],y=0!==c[0]||0!==c[1]||0!==c[2];if(I||y){const e=zu;if(I){const t=Ku;p.transformPoint3(u,o,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=c[0],e[1]+=c[1],e[2]+=c[2],d=O(A,e,qu),f=Yu,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=A,f=a.eye;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,h),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),r.uniformMatrix4fv(this._uWorldNormalMatrix,!1,n.worldNormalMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0,s=[];return s.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec3 normal;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 worldNormalMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 viewNormalMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("varying float isPerspective;")),s.push("vec3 octDecode(vec2 oct) {"),s.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),s.push(" if (v.z < 0.0) {"),s.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),s.push(" }"),s.push(" return normalize(v);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("out vec3 vViewNormal;"),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),s.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push(" vViewNormal = viewNormal;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Zu=p.vec3();class $u{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerVertexIdCoordinates),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},3),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uWorldMatrix,!1,n.worldMatrix);const c=e.pickViewMatrix||a.viewMatrix,u=o?O(c,o):c;if(r.uniformMatrix4fv(this._uViewMatrix,!1,u),r.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const h=i._sectionPlanesState.sectionPlanes.length;if(h>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*h,a=n.renderFlags;for(let t=0;t0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick flat normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),s.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform isampler2D uTexturePerPolygonIdNormals;"),s.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("out vec4 vWorldPosition;"),t&&s.push("out int vFlags2;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_normal_index = polygonIndex & 4095;"),s.push("int v_normal_index = polygonIndex >> 12;"),s.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),s.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),s.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),s.push("int objectIndex;"),s.push("if (((polygonIndex >> 3) % 2) == 0) {"),s.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),s.push("} else {"),s.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),s.push("}"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("int h_index = polygonIndex & 4095;"),s.push("int v_index = polygonIndex >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),s.push("int vertexNumber = gl_VertexID % 3;"),s.push("vec3 position;"),s.push("if (vertexNumber == 0) position = position1;"),s.push("else if (vertexNumber == 1) position = position2;"),s.push("else position = position3;"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vWorldPosition = worldPosition;"),t&&s.push(" vFlags2 = flags2.r;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals fragment shader"),n.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("in vec4 vWorldPosition;"),s){n.push("in int vFlags2;");for(var i=0;i 0;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),n.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),n.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),n.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}class eh{constructor(e){this._scene=e}_compile(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}get colorRenderer(){return this._colorRenderer||(this._colorRenderer=new Kc(this._scene,!1)),this._colorRenderer}get colorRendererWithSAO(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new Kc(this._scene,!0)),this._colorRendererWithSAO}get colorQualityRendererWithSAO(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}get silhouetteRenderer(){return this._silhouetteRenderer||(this._silhouetteRenderer=new eu(this._scene)),this._silhouetteRenderer}get depthRenderer(){return this._depthRenderer||(this._depthRenderer=new Wu(this._scene)),this._depthRenderer}get normalsRenderer(){return this._normalsRenderer||(this._normalsRenderer=new Ju(this._scene)),this._normalsRenderer}get edgesRenderer(){return this._edgesRenderer||(this._edgesRenderer=new ru(this._scene)),this._edgesRenderer}get edgesColorRenderer(){return this._edgesColorRenderer||(this._edgesColorRenderer=new hu(this._scene)),this._edgesColorRenderer}get pickMeshRenderer(){return this._pickMeshRenderer||(this._pickMeshRenderer=new Iu(this._scene)),this._pickMeshRenderer}get pickNormalsRenderer(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new Hu(this._scene)),this._pickNormalsRenderer}get pickNormalsFlatRenderer(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new $u(this._scene)),this._pickNormalsFlatRenderer}get pickDepthRenderer(){return this._pickDepthRenderer||(this._pickDepthRenderer=new Tu(this._scene)),this._pickDepthRenderer}get vertexDepthRenderer(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new _u(this._scene)),this._vertexDepthRenderer}get snapDepthBufInitRenderer(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new Mu(this._scene)),this._snapDepthBufInitRenderer}get occlusionRenderer(){return this._occlusionRenderer||(this._occlusionRenderer=new Uu(this._scene)),this._occlusionRenderer}_destroy(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}const th={};class sh{constructor(){this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]}}class nh{constructor(){this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}finalize(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}bindCommonTextures(e,t,s,n,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,s,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,n,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}bindPickCameraTexture(e,t){}bindTriangleIndicesTextures(e,t,s,n){this.indicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.indicesPerBitnessTextures[n].bindTexture(e,s,7)}bindEdgeIndicesTextures(e,t,s,n){this.edgeIndicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[n].bindTexture(e,s,7)}}class ih{constructor(e,t,s,n,i=null){this._gl=e,this._texture=t,this._textureWidth=s,this._textureHeight=n,this._textureData=i}bindTexture(e,t,s){return e.bindTexture(t,this,s)}bind(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}unbind(e){}}const ah={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(ah,null,4));let e=0;Object.keys(ah).forEach((t=>{t.startsWith("size")&&(e+=ah[t])})),console.log(`Total size ${e} bytes (${(e/1e3/1e3).toFixed(2)} MB)`),console.log(`Avg bytes / triangle: ${(e/ah.totalPolygons).toFixed(2)}`);let t={};Object.keys(ah).forEach((s=>{s.startsWith("size")&&(t[s]=`${(ah[s]/e*100).toFixed(2)} % of total`)})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};class rh{disableBindedTextureFiltering(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}generateCameraDataTexture(e,t,s,n){const i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const a=new ih(e,i,4,3);let r=!0;a.updateViewMatrix=(s,i)=>{e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?O(s,n):s)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};const l=()=>{r&&(r=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(()=>r=!0)),s.on("rendering",l),l(),a}generatePickCameraDataTexture(e,t,s){const n=e.createTexture();e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const i=new ih(e,n,4,3);return i.updateViewMatrix=(n,a)=>{e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(s?O(n,s):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}generateModelTexture(e,t){const s=e.createTexture();return e.bindTexture(e.TEXTURE_2D,s),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new ih(e,s,4,2)}generateTextureForColorsAndFlags(e,t,s,n,i,a,r){const l=t.length;this.numPortions=l;const o=4096,c=Math.ceil(l/512);if(0===c)throw"texture height===0";const u=new Uint8Array(16384*c);ah.sizeDataColorsAndFlags+=u.byteLength,ah.numberOfTextures++;for(let e=0;e>24&255,n[e]>>16&255,n[e]>>8&255,255&n[e]],32*e+16),u.set([i[e]>>24&255,i[e]>>16&255,i[e]>>8&255,255&i[e]],32*e+20),u.set([a[e]>>24&255,a[e]>>16&255,a[e]>>8&255,255&a[e]],32*e+24),u.set([r[e]?1:0,0,0,0],32*e+28);const h=e.createTexture();return e.bindTexture(e.TEXTURE_2D,h),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,o,c),e.texSubImage2D(e.TEXTURE_2D,0,0,0,o,c,e.RGBA_INTEGER,e.UNSIGNED_BYTE,u,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new ih(e,h,o,c,u)}generateTextureForObjectOffsets(e,t){const s=512,n=Math.ceil(t/s);if(0===n)throw"texture height===0";const i=new Float32Array(1536*n).fill(0);ah.sizeDataTextureOffsets+=i.byteLength,ah.numberOfTextures++;const a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,s,n),e.texSubImage2D(e.TEXTURE_2D,0,0,0,s,n,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new ih(e,a,s,n,i)}generateTextureForPositionsDecodeMatrices(e,t,s){const n=t.length;if(0===n)throw"num decode+entity matrices===0";const i=2048,a=Math.ceil(n/512),r=new Float32Array(8192*a);ah.sizeDataPositionDecodeMatrices+=r.byteLength,ah.numberOfTextures++;const l=p.mat4();for(let e=0;e{s._compile()})),e.on("destroyed",(()=>{delete th[t],s._destroy()}))),s}(e.scene),this.model=e,this._buffer=new sh,this._dataTextureState=new nh,this._dataTextureGenerator=new rh,this._state=new ze({origin:p.vec3(t.origin),metallicRoughnessBuf:null,positionsDecodeMatrix:p.mat4(),textureState:this._dataTextureState,numIndices8Bits:0,numIndices16Bits:0,numIndices32Bits:0,numEdgeIndices8Bits:0,numEdgeIndices16Bits:0,numEdgeIndices32Bits:0,numVertices:0}),this._numPortions=0,this._numVisibleLayerPortions=0,this._numTransparentLayerPortions=0,this._numXRayedLayerPortions=0,this._numSelectedLayerPortions=0,this._numHighlightedLayerPortions=0,this._numClippableLayerPortions=0,this._numEdgesLayerPortions=0,this._numPickableLayerPortions=0,this._numCulledLayerPortions=0,this._subPortions=[],this._portionToSubPortionsMap=[],this._bucketGeometries={},this.aabb=p.collapseAABB3(),this._numUpdatesInFrame=0,this._finalized=!1}canCreatePortion(e){if(this._finalized)throw"Already finalized";const t=e.buckets.length;this._numPortions+t>65536&&ah.cannotCreatePortion.because10BitsObjectId++;let s=this._numPortions+t<=65536;const n=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#0`:`${e.id}#0`;if(!this._bucketGeometries[n]){const t=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits);let n=0,i=0;e.buckets.forEach((e=>{n+=e.positionsCompressed.length/3,i+=e.indices.length/3})),(this._state.numVertices+n>16777216||t+i>16777216)&&ah.cannotCreatePortion.becauseTextureSize++,s&&=this._state.numVertices+n<=16777216&&t+i<=16777216}return s}createPortion(e){if(this._finalized)throw"Already finalized";const t=[],s=e.worldAABB;e.buckets.forEach(((n,i)=>{const a=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#${i}`:`${e.id}#${i}`;let r=this._bucketGeometries[a];r||(r=this._createBucketGeometry(e,n),this._bucketGeometries[a]=r);const l=p.collapseAABB3(ph),o=this._createSubPortion(e,r,n,l);p.expandAABB3(s,l),t.push(o)}));const n=this._state.origin;0===n[0]&&0===n[1]&&0===n[2]||(s[0]+=n[0],s[1]+=n[1],s[2]+=n[2],s[3]+=n[0],s[4]+=n[1],s[5]+=n[2]),p.expandAABB3(this.aabb,s);const i=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(t),this.model.numPortions++,i}_createBucketGeometry(e,t){if(t.indices){const e=8*Math.ceil(t.indices.length/3/8)*3;ah.overheadSizeAlignementIndices+=2*(e-t.indices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.indices),t.indices=s}if(t.edgeIndices){const e=8*Math.ceil(t.edgeIndices.length/2/8)*2;ah.overheadSizeAlignementEdgeIndices+=2*(e-t.edgeIndices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.edgeIndices),t.edgeIndices=s}const s=t.positionsCompressed,n=t.indices,i=t.edgeIndices,a=this._buffer,r=a.positionsCompressed.length/3,l=s.length/3;for(let e=0,t=s.length;e0){let e,s=3*t.numTriangles;t.numVertices<=256?(e=u.perTriangleNumberPortionId8Bits,h.numIndices8Bits+=s,ah.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(e=u.perTriangleNumberPortionId16Bits,h.numIndices16Bits+=s,ah.totalPolygons16Bits+=t.numTriangles):(e=u.perTriangleNumberPortionId32Bits,h.numIndices32Bits+=s,ah.totalPolygons32Bits+=t.numTriangles),ah.totalPolygons+=t.numTriangles;for(let s=0;s0){let e,s=2*t.numEdges;t.numVertices<=256?(e=u.perEdgeNumberPortionId8Bits,h.numEdgeIndices8Bits+=s,ah.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(e=u.perEdgeNumberPortionId16Bits,h.numEdgeIndices16Bits+=s,ah.totalEdges16Bits+=t.numEdges):(e=u.perEdgeNumberPortionId32Bits,h.numEdgeIndices32Bits+=s,ah.totalEdges32Bits+=t.numEdges),ah.totalEdges+=t.numEdges;for(let s=0;s0&&(t.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId8Bits)),n.perEdgeNumberPortionId16Bits.length>0&&(t.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId16Bits)),n.perEdgeNumberPortionId32Bits.length>0&&(t.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId32Bits)),n.indices8Bits.length>0&&(t.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(s,n.indices8Bits)),n.indices16Bits.length>0&&(t.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(s,n.indices16Bits)),n.indices32Bits.length>0&&(t.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(s,n.indices32Bits)),n.edgeIndices8Bits.length>0&&(t.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(s,n.edgeIndices8Bits)),n.edgeIndices16Bits.length>0&&(t.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(s,n.edgeIndices16Bits)),n.edgeIndices32Bits.length>0&&(t.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(s,n.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(s,this.model)),t.textureModelMatrices=this.model._modelMatricesTexture,t.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),t.textureCameraMatrices=t.cameraTexture,t.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),t.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(()=>{this._deferredSetFlagsDirty&&this._uploadDeferredFlags(),this._numUpdatesInFrame=0}))}isEmpty(){return 0===this._numPortions}initFlags(e,t,s){t&M&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&j&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&V&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&k&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&U&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&Q&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&H&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&F&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),s&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,s,true),this._setFlags2(e,t,true)}flushInitFlags(){this._setDeferredFlags(),this._setDeferredFlags2()}setVisible(e,t,s){if(!this._finalized)throw"Not finalized";t&M?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,s)}setHighlighted(e,t,s){if(!this._finalized)throw"Not finalized";t&j?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,s)}setXRayed(e,t,s){if(!this._finalized)throw"Not finalized";t&V?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,s)}setSelected(e,t,s){if(!this._finalized)throw"Not finalized";t&k?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,s)}setEdges(e,t,s){if(!this._finalized)throw"Not finalized";t&Q?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,s)}setClippable(e,t){if(!this._finalized)throw"Not finalized";t&U?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}_beginDeferredFlags(){this._deferredSetFlagsActive=!0}_uploadDeferredFlags(){if(this._deferredSetFlagsActive=!1,!this._deferredSetFlagsDirty)return;this._deferredSetFlagsDirty=!1;const e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}setCulled(e,t,s){if(!this._finalized)throw"Not finalized";t&F?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,s)}setCollidable(e,t){if(!this._finalized)throw"Not finalized"}setPickable(e,t,s){if(!this._finalized)throw"Not finalized";t&H?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,s)}setColor(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdColorsAndFlags._texture),n.texSubImage2D(n.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,n.RGBA_INTEGER,n.UNSIGNED_BYTE,uh))}setTransparent(e,t,s){s?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,s)}_setFlags(e,t,s,n=!1){const i=this._portionToSubPortionsMap[e];for(let e=0,a=i.length;e=10&&this._beginDeferredFlags(),d.bindTexture(d.TEXTURE_2D,A.texturePerObjectIdColorsAndFlags._texture),d.texSubImage2D(d.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,d.RGBA_INTEGER,d.UNSIGNED_BYTE,uh))}_setDeferredFlags(){}_setFlags2(e,t,s=!1){const n=this._portionToSubPortionsMap[e];for(let e=0,i=n.length;e=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,uh))}_setDeferredFlags2(){}setOffset(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdOffsets._texture),n.texSubImage2D(n.TEXTURE_2D,0,0,e,1,1,n.RGB,n.FLOAT,hh))}drawColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,cr.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}_updateBackfaceCull(e,t){const s=this.model.backfaces||e.sectioned;if(t.backfaces!==s){const e=t.gl;s?e.disable(e.CULL_FACE):e.enable(e.CULL_FACE),t.backfaces=s}}drawColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,cr.COLOR_TRANSPARENT))}drawDepth(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawSilhouetteXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_XRAYED))}drawSilhouetteHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_HIGHLIGHTED))}drawSilhouetteSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_SELECTED))}drawEdgesColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,cr.EDGES_COLOR_OPAQUE)}drawEdgesColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,cr.EDGES_COLOR_TRANSPARENT)}drawEdgesHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_HIGHLIGHTED)}drawEdgesSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_SELECTED)}drawEdgesXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_XRAYED)}drawOcclusion(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawShadow(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}setPickMatrices(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}drawPickMesh(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,cr.PICK))}drawPickDepths(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,cr.PICK))}drawSnapInitDepthBuf(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,cr.PICK))}drawSnapDepths(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,cr.PICK))}drawPickNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,cr.PICK))}destroy(){if(this._destroyed)return;const e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}const Ih=p.vec3();p.mat4();const yh=p.vec3([1,1,1]),mh=p.vec3([0,0,0]),vh=p.vec3([0,0,0]),wh=p.identityQuaternion(),gh=p.identityMat4(),Th=new Uint8Array([255,255,255]);class Eh extends _{constructor(e,t={}){super(e,t),this._dtxEnabled=this.scene.dtxEnabled&&!1!==t.dtxEnabled,this._enableVertexWelding=!1,this._enableIndexBucketing=!0,this._vboBatchingLayerScratchMemory=(or++,lr),this._textureTranscoder=t.textureTranscoder||Oc(this.scene.viewer),this._maxGeometryBatchSize=t.maxGeometryBatchSize,this._aabb=p.collapseAABB3(),this._aabbDirty=!0,this._quantizationRanges={},this._vboInstancingLayers={},this._vboBatchingLayers={},this._dtxLayers={},this._meshList=[],this.layerList=[],this._entityList=[],this._geometries={},this._dtxBuckets={},this._textures={},this._textureSets={},this._meshes={},this._entities={},this._scheduledMeshes={},this.renderFlags=new ci,this.numGeometries=0,this.numPortions=0,this.numVisibleLayerPortions=0,this.numTransparentLayerPortions=0,this.numXRayedLayerPortions=0,this.numHighlightedLayerPortions=0,this.numSelectedLayerPortions=0,this.numEdgesLayerPortions=0,this.numPickableLayerPortions=0,this.numClippableLayerPortions=0,this.numCulledLayerPortions=0,this.numEntities=0,this._numTriangles=0,this._numLines=0,this._numPoints=0,this._edgeThreshold=t.edgeThreshold||10,this._origin=p.vec3(t.origin||[0,0,0]),this._position=p.vec3(t.position||[0,0,0]),this._rotation=p.vec3(t.rotation||[0,0,0]),this._quaternion=p.vec4(t.quaternion||[0,0,0,1]),this._conjugateQuaternion=p.vec4(t.quaternion||[0,0,0,1]),t.rotation&&p.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._scale=p.vec3(t.scale||[1,1,1]),this._worldRotationMatrix=p.mat4(),this._worldRotationMatrixConjugate=p.mat4(),this._matrix=p.mat4(),this._matrixDirty=!0,this._rebuildMatrices(),this._worldNormalMatrix=p.mat4(),p.inverseMat4(this._matrix,this._worldNormalMatrix),p.transposeMat4(this._worldNormalMatrix),(t.matrix||t.position||t.rotation||t.scale||t.quaternion)&&(this._viewMatrix=p.mat4(),this._viewNormalMatrix=p.mat4(),this._viewMatrixDirty=!0,this._matrixNonIdentity=!0),this._opacity=1,this._colorize=[1,1,1],this._saoEnabled=!1!==t.saoEnabled,this._pbrEnabled=!1!==t.pbrEnabled,this._colorTextureEnabled=!1!==t.colorTextureEnabled,this._isModel=t.isModel,this._isModel&&this.scene._registerModel(this),this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewMatrixDirty=!0})),this.scene.vfc.enabled&&(this._vfcManager=this.scene.vfc.getVFCManager(this)),this._createDefaultTextureSet(),this.visible=t.visible,this.culled=t.culled,this.pickable=t.pickable,this.clippable=t.clippable,this.collidable=t.collidable,this.castsShadow=t.castsShadow,this.receivesShadow=t.receivesShadow,this.xrayed=t.xrayed,this.highlighted=t.highlighted,this.selected=t.selected,this.edges=t.edges,this.colorize=t.colorize,this.opacity=t.opacity,this.backfaces=t.backfaces}_createDefaultTextureSet(){const e=new wc({id:"defaultColorTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new wc({id:"defaultMetalRoughTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),s=new wc({id:"defaultNormalsTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),n=new wc({id:"defaultEmissiveTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new wc({id:"defaultOcclusionTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=s,this._textures.defaultEmissiveTexture=n,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new vc({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:s,emissiveTexture:n,occlusionTexture:i})}get isPerformanceModel(){return!0}get objects(){return this._entities}get origin(){return this._origin}set position(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get position(){return this._position}set rotation(e){this._rotation.set(e||[0,0,0]),p.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get rotation(){return this._rotation}set quaternion(e){this._quaternion.set(e||[0,0,0,1]),p.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get quaternion(){return this._quaternion}set scale(e){}get scale(){return this._scale}set matrix(e){this._matrix.set(e||gh),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),p.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),p.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get matrix(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix}get rotationMatrix(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}_rebuildMatrices(){this._matrixDirty&&(p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),p.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),p.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}get rotationMatrixConjugate(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}_setWorldMatrixDirty(){this._matrixDirty=!0}_setLocalAABBDirty(){for(let e=0,t=this._entityList.length;e0}set visible(e){e=!1!==e,this._visible=e;for(let t=0,s=this._entityList.length;t0}set xrayed(e){e=!!e,this._xrayed=e;for(let t=0,s=this._entityList.length;t0}set highlighted(e){e=!!e,this._highlighted=e;for(let t=0,s=this._entityList.length;t0}set selected(e){e=!!e,this._selected=e;for(let t=0,s=this._entityList.length;t0}set edges(e){e=!!e,this._edges=e;for(let t=0,s=this._entityList.length;t0}set pickable(e){e=!1!==e,this._pickable=e;for(let t=0,s=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){const t=e.colors,s=new Uint8Array(t.length);for(let e=0,n=t.length;e{o.setImage(c,{minFilter:s,magFilter:n,wrapS:i,wrapT:a,wrapR:r,flipY:e.flipY,encoding:l}),this.glRedraw()},c.src=e.src;break;default:this._textureTranscoder?m.loadArraybuffer(e.src,(e=>{e.byteLength?this._textureTranscoder.transcode([e],o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'src': file data is zero length")}),(function(e){this.error(`[createTexture] Can't create texture from 'src': ${e}`)})):this.error(`[createTexture] Can't create texture from 'src' - SceneModel needs to be configured with a TextureTranscoder for this file type ('${t}')`)}}else e.buffers&&(this._textureTranscoder?this._textureTranscoder.transcode(e.buffers,o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'buffers' - SceneModel needs to be configured with a TextureTranscoder for this option"));this._textures[t]=new wc({id:t,texture:o})}createTextureSet(e){const t=e.id;if(null==t)return void this.error("[createTextureSet] Config missing: id");if(this._textureSets[t])return void this.error(`[createTextureSet] Texture set already created: ${t}`);let s,n,i,a,r;if(void 0!==e.colorTextureId&&null!==e.colorTextureId){if(s=this._textures[e.colorTextureId],!s)return void this.error(`[createTextureSet] Texture not found: ${e.colorTextureId} - ensure that you create it first with createTexture()`)}else s=this._textures.defaultColorTexture;if(void 0!==e.metallicRoughnessTextureId&&null!==e.metallicRoughnessTextureId){if(n=this._textures[e.metallicRoughnessTextureId],!n)return void this.error(`[createTextureSet] Texture not found: ${e.metallicRoughnessTextureId} - ensure that you create it first with createTexture()`)}else n=this._textures.defaultMetalRoughTexture;if(void 0!==e.normalsTextureId&&null!==e.normalsTextureId){if(i=this._textures[e.normalsTextureId],!i)return void this.error(`[createTextureSet] Texture not found: ${e.normalsTextureId} - ensure that you create it first with createTexture()`)}else i=this._textures.defaultNormalsTexture;if(void 0!==e.emissiveTextureId&&null!==e.emissiveTextureId){if(a=this._textures[e.emissiveTextureId],!a)return void this.error(`[createTextureSet] Texture not found: ${e.emissiveTextureId} - ensure that you create it first with createTexture()`)}else a=this._textures.defaultEmissiveTexture;if(void 0!==e.occlusionTextureId&&null!==e.occlusionTextureId){if(r=this._textures[e.occlusionTextureId],!r)return void this.error(`[createTextureSet] Texture not found: ${e.occlusionTextureId} - ensure that you create it first with createTexture()`)}else r=this._textures.defaultOcclusionTexture;const l=new vc({id:t,model:this,colorTexture:s,metallicRoughnessTexture:n,normalsTexture:i,emissiveTexture:a,occlusionTexture:r});this._textureSets[t]=l}createMesh(e){if(void 0===e.id||null===e.id)return void this.error("[createMesh] SceneModel.createMesh() config missing: id");if(this._scheduledMeshes[e.id])return void this.error(`[createMesh] SceneModel already has a mesh with this ID: ${e.id}`);const t=!(void 0!==e.geometryId);if(e.sceneModelMatrix=this._matrixNonIdentity?this._matrix:null,t){const t=!!this._dtxEnabled;if(void 0!==e.primitive&&null!==e.primitive||(e.primitive="triangles"),"points"!==e.primitive&&"lines"!==e.primitive&&"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive)return void this.error(`Unsupported value for 'primitive': '${primitive}' ('geometryId' is absent) - supported values are 'points', 'lines', 'triangles', 'solid' and 'surface'.`);if(!e.positions&&!e.positionsCompressed&&!e.buckets)return this.error("Param expected: 'positions', 'positionsCompressed' or `buckets` ('geometryId' is absent)"),null;if(e.positions&&(e.positionsDecodeMatrix||e.positionsDecodeBoundary))return this.error("Illegal params: 'positions' not expected with 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.positionsCompressed&&!e.positionsDecodeMatrix&&!e.positionsDecodeBoundary)return this.error("Param expected: 'positionsCompressed' should be accompanied by 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.uvCompressed&&!e.uvDecodeMatrix)return this.error("Param expected: 'uvCompressed' should be accompanied by `uvDecodeMatrix` ('geometryId' is absent)"),null;if(!e.buckets&&!e.indices&&"points"!==e.primitive)return this.error(`Param expected: indices (required for '${e.primitive}' primitive type)`),null;if((e.matrix||e.position||e.rotation||e.scale)&&(e.positionsCompressed||e.positionsDecodeBoundary))return this.error("Unexpected params: 'matrix', 'rotation', 'scale', 'position' not allowed with 'positionsCompressed'"),null;if(e.origin=e.origin?p.addVec3(this._origin,e.origin,p.vec3()):this._origin,e.matrix)e.meshMatrix=e.matrix;else if(e.scale||e.rotation||e.position){const t=e.scale||yh,s=e.position||mh,n=e.rotation||vh;p.eulerToQuaternion(n,"XYZ",wh),e.meshMatrix=p.composeMat4(s,wh,t,p.mat4())}if(e.positionsDecodeBoundary&&(e.positionsDecodeMatrix=Kr(e.positionsDecodeBoundary,p.mat4())),t){if(e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.positions){const t=p.vec3(),s=[];N(e.positions,s,t)&&(e.positions=s,e.origin=p.addVec3(e.origin,t,t))}if(e.positions){const t=p.collapseAABB3();e.positionsDecodeMatrix=p.mat4(),p.expandAABB3Points3(t,e.positions),e.positionsCompressed=zr(e.positions,t,e.positionsDecodeMatrix)}e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=mt(e.positions,e.indices,null,2):e.edgeIndices=mt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.buckets||(e.buckets=bh(e,this._enableVertexWelding&&this._enableIndexBucketing))}else{if(e.type=1,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):[255,255,255],e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.positions){const t=[];N(e.positions,t,Ih)&&(e.positions=t,e.origin=p.addVec3(e.origin,Ih,p.vec3()))}if(e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=mt(e.positions,e.indices,null,2):e.edgeIndices=mt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId],!e.textureSet))return void this.error(`[createMesh] Texture set not found: ${e.textureSetId} - ensure that you create it first with createTextureSet()`)}}else{if(e.positions||e.positionsCompressed||e.indices||e.edgeIndices||e.normals||e.normalsCompressed||e.uv||e.uvCompressed||e.positionsDecodeMatrix)return void this.error("Mesh geometry parameters not expected when instancing a geometry (not expected: positions, positionsCompressed, indices, edgeIndices, normals, normalsCompressed, uv, uvCompressed, positionsDecodeMatrix)");if(e.geometry=this._geometries[e.geometryId],!e.geometry)return void this.error(`[createMesh] Geometry not found: ${e.geometryId} - ensure that you create it first with createGeometry()`);if(e.origin=e.origin?p.addVec3(this._origin,e.origin,p.vec3()):this._origin,e.positionsDecodeMatrix=e.geometry.positionsDecodeMatrix,e.matrix)e.meshMatrix=e.matrix.slice();else{const t=e.scale||yh,s=e.position||mh,n=e.rotation||vh;p.eulerToQuaternion(n,"XYZ",wh),e.meshMatrix=p.composeMat4(s,wh,t,p.mat4())}if(!!this._dtxEnabled){e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255;let t=this._dtxBuckets[e.geometryId];t||(t=bh(e.geometry,this._enableVertexWelding,this._enableIndexBucketing),this._dtxBuckets[e.geometryId]=t),e.buckets=t}else e.type=0,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId]),function(e){if(e.obb=p.OBB3(),e.positionsCompressed&&e.positionsCompressed.length>0){const t=p.collapseAABB3();p.expandAABB3Points3(t,e.positionsCompressed),Dt.decompressAABB(t,e.positionsDecodeMatrix),p.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){const t=p.collapseAABB3();p.expandAABB3Points3(t,e.positions),p.AABB3ToOBB3(t,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}_createMesh(e){const t=new rr(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);const s=t.pickId,n=s>>24&255,i=s>>16&255,a=s>>8&255,r=255&s;switch(e.pickColor=new Uint8Array([r,a,i,n]),e.worldAABB=p.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=p.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,p.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}_getNumPrimitives(e){let t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(let s=0,n=e.buckets.length;s>>0).toString(16)}_getVBOInstancingLayer(e){const t=this,s=e.origin,n=e.textureSetId||"-",i=e.geometryId,a=`${Math.round(s[0])}.${Math.round(s[1])}.${Math.round(s[2])}.${n}.${i}`;let r=this._vboInstancingLayers[a];if(r)return r;let l=e.textureSet;const o=e.geometry;for(;!r;)switch(o.primitive){case"triangles":case"surface":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new wo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!1});break;case"solid":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new wo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!0});break;case"lines":console.log(`[SceneModel ${this.id}]: creating LinesInstancingLayer`),r=new Vo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0});break;case"points":console.log(`[SceneModel ${this.id}]: creating PointsInstancingLayer`),r=new mc({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0})}return this._vboInstancingLayers[a]=r,this.layerList.push(r),r}createEntity(e){if(void 0===e.id?e.id=p.createUUID():this.scene.components[e.id]&&(this.error(`Scene already has a Component with this ID: ${e.id} - will assign random ID`),e.id=p.createUUID()),void 0===e.meshIds)return void this.error("Config missing: meshIds");let t=0;if(this._visible&&!1!==e.visible&&(t|=M),this._pickable&&!1!==e.pickable&&(t|=H),this._culled&&!1!==e.culled&&(t|=F),this._clippable&&!1!==e.clippable&&(t|=U),this._collidable&&!1!==e.collidable&&(t|=G),this._edges&&!1!==e.edges&&(t|=Q),this._xrayed&&!1!==e.xrayed&&(t|=V),this._highlighted&&!1!==e.highlighted&&(t|=j),this._selected&&!1!==e.selected&&(t|=k),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(let t=0,s=e.meshIds.length;t{}));for(let e=0,t=this.layerList.length;ee.sortIdt.sortId?1:0));for(let e=0,t=this.layerList.length;e0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}_updateRenderFlagsVisibleLayers(){const e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(let t=0,s=this.layerList.length;t0)for(let e=0;e0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){const t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0){this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0))}if(this.numSelectedLayerPortions>0){const t=this.scene.selectedMaterial._state;t.fill&&(t.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){const t=this.scene.highlightMaterial._state;t.fill&&(t.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}drawColorOpaque(e){const t=this.renderFlags;for(let s=0,n=t.visibleLayers.length;s65536?16:8)}else r=[{positionsCompressed:n,indices:i,edgeIndices:a}];return r}class Dh extends Eh{constructor(e,t={}){super(e,t)}}class Ph extends _{constructor(e,t={}){super(e,t),this._skyboxMesh=new wi(this,{geometry:new Ct(this,{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new Nt(this,{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new $i(this,{src:t.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:t.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),this.size=t.size,this.active=t.active}set size(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}get size(){return this._size}set active(e){this._skyboxMesh.visible=e}get active(){return this._skyboxMesh.visible}}class Rh{transcode(e,t,s={}){}destroy(){}}const Ch=p.vec4(),_h=p.vec4(),Bh=p.vec3(),Oh=p.vec3(),Sh=p.vec3(),Nh=p.vec4(),xh=p.vec4(),Lh=p.vec4();class Mh{constructor(e){this._scene=e}dollyToCanvasPos(e,t,s){let n=!1;const i=this._scene.camera;if(e){const t=p.subVec3(e,i.eye,Bh);n=p.lenVec3(t){this._cameraDirty=!0})),this._onProjMatrix=this._scene.camera.on("projMatrix",(()=>{this._cameraDirty=!0})),this._onTick=this._scene.on("tick",(()=>{this.updatePivotElement()}))}updatePivotElement(){const e=this._scene.camera,t=this._scene.canvas;if(this._pivoting&&this._cameraDirty){p.transformPoint3(e.viewMatrix,this.getPivotPos(),this._pivotViewPos),this._pivotViewPos[3]=1,p.transformPoint4(e.projMatrix,this._pivotViewPos,this._pivotProjPos);const s=t.boundary,n=s[2],i=s[3];this._pivotCanvasPos[0]=Math.floor((1+this._pivotProjPos[0]/this._pivotProjPos[3])*n/2),this._pivotCanvasPos[1]=Math.floor((1-this._pivotProjPos[1]/this._pivotProjPos[3])*i/2);let a=t._lastBoundingClientRect;if(!a||t._canvasSizeChanged){const e=t.canvas;a=t._lastBoundingClientRect=e.getBoundingClientRect()}this._pivotElement&&(this._pivotElement.style.left=Math.floor(a.left+this._pivotCanvasPos[0])-this._pivotElement.clientWidth/2+window.scrollX+"px",this._pivotElement.style.top=Math.floor(a.top+this._pivotCanvasPos[1])-this._pivotElement.clientHeight/2+window.scrollY+"px"),this._cameraDirty=!1}}setPivotElement(e){this._pivotElement=e}startPivot(){if(this._cameraLookingDownwards())return this._pivoting=!1,!1;const e=this._scene.camera;let t=p.lookAtMat4v(e.eye,e.look,e.worldUp);p.transformPoint3(t,this.getPivotPos(),this._cameraOffset);const s=this.getPivotPos();this._cameraOffset[2]+=p.distVec3(e.eye,s),t=p.inverseMat4(t);const n=p.transformVec3(t,this._cameraOffset),i=p.vec3();if(p.subVec3(e.eye,s,i),p.addVec3(i,n),e.zUp){const e=i[1];i[1]=i[2],i[2]=e}this._radius=p.lenVec3(i),this._polar=Math.acos(i[1]/this._radius),this._azimuth=Math.atan2(i[0],i[2]),this._pivoting=!0}_cameraLookingDownwards(){const e=this._scene.camera,t=p.normalizeVec3(p.subVec3(e.look,e.eye,Fh)),s=p.cross3Vec3(t,e.worldUp,Hh);return p.sqLenVec3(s)<=1e-4}getPivoting(){return this._pivoting}setPivotPos(e){this._pivotWorldPos.set(e),this._pivotPosSet=!0}setCanvasPivotPos(e){const t=this._scene.camera,s=Math.abs(p.distVec3(this._scene.center,t.eye)),n=t.project.transposedMatrix,i=n.subarray(8,12),a=n.subarray(12),r=[0,0,-1,1],l=p.dotVec4(r,i)/p.dotVec4(r,a),o=Gh;t.project.unproject(e,l,Vh,jh,o);const c=p.normalizeVec3(p.subVec3(o,t.eye,Fh)),u=p.addVec3(t.eye,p.mulVec3Scalar(c,s,Hh),Uh);this.setPivotPos(u)}getPivotPos(){return this._pivotPosSet?this._pivotWorldPos:this._scene.camera.look}continuePivot(e,t){if(!this._pivoting)return;if(0===e&&0===t)return;const s=this._scene.camera;var n=-e;const i=-t;1===s.worldUp[2]&&(n=-n),this._azimuth+=.01*-n,this._polar+=.01*i,this._polar=p.clamp(this._polar,.001,Math.PI-.001);const a=[this._radius*Math.sin(this._polar)*Math.sin(this._azimuth),this._radius*Math.cos(this._polar),this._radius*Math.sin(this._polar)*Math.cos(this._azimuth)];if(1===s.worldUp[2]){const e=a[1];a[1]=a[2],a[2]=e}const r=p.lenVec3(p.subVec3(s.look,s.eye,p.vec3())),l=this.getPivotPos();p.addVec3(a,l);let o=p.lookAtMat4v(a,l,s.worldUp);o=p.inverseMat4(o);const c=p.transformVec3(o,this._cameraOffset);o[12]-=c[0],o[13]-=c[1],o[14]-=c[2];const u=[o[8],o[9],o[10]];s.eye=[o[12],o[13],o[14]],p.subVec3(s.eye,p.mulVec3Scalar(u,r),s.look),s.up=[o[4],o[5],o[6]],this.showPivot()}showPivot(){this._shown||(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this.updatePivotElement(),this._pivotElement.style.visibility="visible",this._shown=!0,this._hideTimeout=window.setTimeout((()=>{this.hidePivot()}),1e3)))}hidePivot(){this._shown&&(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this._pivotElement.style.visibility="hidden"),this._shown=!1)}endPivot(){this._pivoting=!1}destroy(){this._scene.camera.off(this._onViewMatrix),this._scene.camera.off(this._onProjMatrix),this._scene.off(this._onTick)}}class Qh{constructor(e,t){this._scene=e.scene,this._cameraControl=e,this._scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},this._configs=t,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick=!1,this.pickCursorPos=p.vec2(),this.picked=!1,this.pickedSurface=!1,this.pickResult=null,this._lastPickedEntityId=null,this._needFireEvents=!1}update(){if(!this._configs.pointerEnabled)return;if(!this.schedulePickEntity&&!this.schedulePickSurface)return;this.picked=!1,this.pickedSurface=!1,this.snappedOrPicked=!1,this.hoveredSnappedOrSurfaceOff=!1,this._needFireEvents=!1;const e=this._cameraControl.hasSubs("hoverSurface");if(this.scheduleSnapOrPick){const e=this._scene.snapPick({canvasPos:this.pickCursorPos,snapRadius:this._configs.snapRadius,snapMode:this._configs.snapMode});e&&e.snappedWorldPos?(this.snapPickResult=e,this.snappedOrPicked=!0,this._needFireEvents=!0):(this.schedulePickSurface=!0,this.snapPickResult=null)}if(this.schedulePickSurface&&this.pickResult&&this.pickResult.worldPos){const t=this.pickResult.canvasPos;if(t[0]===this.pickCursorPos[0]&&t[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!0,this._needFireEvents=e,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.hoveredSnappedOrSurfaceOff=!0,void(this.scheduleSnapOrPick=!1)}if(this.schedulePickEntity&&this.pickResult&&(this.pickResult.canvasPos||this.pickResult.snappedCanvasPos)){const e=this.pickResult.canvasPos||this.pickResult.snappedCanvasPos;if(e[0]===this.pickCursorPos[0]&&e[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!1,this.schedulePickEntity=!1,void(this.schedulePickSurface=!1)}this.schedulePickSurface||this.scheduleSnapOrPick&&!this.snapPickResult?(this.pickResult=this._scene.pick({pickSurface:!0,pickSurfaceNormal:!1,canvasPos:this.pickCursorPos}),this.pickResult?(this.picked=!0,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.pickedSurface=!0,this._needFireEvents=!0):this.scheduleSnapOrPick&&(this.hoveredSnappedOrSurfaceOff=!0,this._needFireEvents=!0)):(this.pickResult=this._scene.pick({canvasPos:this.pickCursorPos}),this.pickResult&&(this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!0)),this.scheduleSnapOrPick=!1,this.schedulePickEntity=!1,this.schedulePickSurface=!1}fireEvents(){if(this._needFireEvents){if(this.hoveredSnappedOrSurfaceOff&&this._cameraControl.fire("hoverSnapOrSurfaceOff",{canvasPos:this.pickCursorPos},!0),this.snappedOrPicked)if(this.snapPickResult){const e=new me;e.worldPos=this.snapPickResult.snappedWorldPos,e.canvasPos=this.snapPickResult.snappedCanvasPos,this._cameraControl.fire("hoverSnapOrSurface",e,!0),this.snapPickResult=null}else this._cameraControl.fire("hoverSnapOrSurface",this.pickResult,!0);if(this.picked&&this.pickResult&&(this.pickResult.entity||this.pickResult.worldPos)){if(this.pickResult.entity){const e=this.pickResult.entity.id;this._lastPickedEntityId!==e&&(void 0!==this._lastPickedEntityId&&this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._cameraControl.fire("hoverEnter",this.pickResult,!0),this._lastPickedEntityId=e)}this._cameraControl.fire("hover",this.pickResult,!0),this.pickResult.worldPos&&(this.pickedSurface=!0,this._cameraControl.fire("hoverSurface",this.pickResult,!0))}else void 0!==this._lastPickedEntityId&&(this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),this._cameraControl.fire("hoverOff",{canvasPos:this.pickCursorPos},!0);this.pickResult=null,this._needFireEvents=!1}}destroy(){}}const Wh=p.vec2();class zh{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController;let r,l,o,c=0,u=0,h=0,A=0,d=!1;const f=p.vec3();let I=!0;const y=this._scene.canvas.canvas,m=[];function v(e=!0){y.style.cursor="move",c=n.pointerCanvasPos[0],u=n.pointerCanvasPos[1],h=n.pointerCanvasPos[0],A=n.pointerCanvasPos[1],e&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(d=!0,f.set(a.pickResult.worldPos)):d=!1)}document.addEventListener("keydown",this._documentKeyDownHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!0}),document.addEventListener("keyup",this._documentKeyUpHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!1}),y.addEventListener("mousedown",this._mouseDownHandler=t=>{if(s.active&&s.pointerEnabled)switch(t.which){case 1:m[e.input.KEY_SHIFT]||s.planView?(r=!0,v()):(r=!0,v(!1));break;case 2:l=!0,v();break;case 3:o=!0,s.panRightClick&&v()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=()=>{if(!s.active||!s.pointerEnabled)return;if(!r&&!l&&!o)return;const t=e.canvas.boundary,a=t[2],h=t[3],A=n.pointerCanvasPos[0],I=n.pointerCanvasPos[1];if(m[e.input.KEY_SHIFT]||s.planView||!s.panRightClick&&l||s.panRightClick&&o){const t=A-c,s=I-u,n=e.camera;if("perspective"===n.projection){const a=Math.abs(d?p.lenVec3(p.subVec3(f,e.camera.eye,[])):e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=1.5*t*a/h,i.panDeltaY+=1.5*s*a/h}else i.panDeltaX+=.5*n.ortho.scale*(t/h),i.panDeltaY+=.5*n.ortho.scale*(s/h)}else!r||l||o||s.planView||(s.firstPerson?(i.rotateDeltaY-=(A-c)/a*s.dragRotationRate/2,i.rotateDeltaX+=(I-u)/h*(s.dragRotationRate/4)):(i.rotateDeltaY-=(A-c)/a*(1.5*s.dragRotationRate),i.rotateDeltaX+=(I-u)/h*(1.5*s.dragRotationRate)));c=A,u=I}),y.addEventListener("mousemove",this._canvasMouseMoveHandler=e=>{s.active&&s.pointerEnabled&&n.mouseover&&(I=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{if(s.active&&s.pointerEnabled)switch(e.which){case 1:case 2:case 3:r=!1,l=!1,o=!1}}),y.addEventListener("mouseup",this._mouseUpHandler=e=>{if(s.active&&s.pointerEnabled){if(3===e.which){!function(e,t){if(e){let s=e.target,n=0,i=0,a=0,r=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,a+=s.scrollLeft,r+=s.scrollTop,s=s.offsetParent;t[0]=e.pageX+a-n,t[1]=e.pageY+r-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,Wh);const s=Wh[0],n=Wh[1];Math.abs(s-h)<3&&Math.abs(n-A)<3&&t.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:Wh,event:e},!0)}y.style.removeProperty("cursor")}}),y.addEventListener("mouseenter",this._mouseEnterHandler=()=>{s.active&&s.pointerEnabled});const w=1/60;let g=null;y.addEventListener("wheel",this._mouseWheelHandler=e=>{if(!s.active||!s.pointerEnabled)return;const t=performance.now()/1e3;var a=null!==g?t-g:0;g=t,a>.05&&(a=.05),a{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const r=i._isKeyDownForAction(i.AXIS_VIEW_RIGHT),l=i._isKeyDownForAction(i.AXIS_VIEW_BACK),o=i._isKeyDownForAction(i.AXIS_VIEW_LEFT),c=i._isKeyDownForAction(i.AXIS_VIEW_FRONT),u=i._isKeyDownForAction(i.AXIS_VIEW_TOP),h=i._isKeyDownForAction(i.AXIS_VIEW_BOTTOM);if(!(r||l||o||c||u||h))return;const A=e.aabb,d=p.getAABB3Diag(A);p.getAABB3Center(A,Kh);const f=Math.abs(d/Math.tan(t.cameraFlight.fitFOV*p.DEGTORAD)),I=1.1*d;Zh.orthoScale=I,r?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldRight,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):l?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldForward,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):o?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldRight,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):c?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldForward,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):u?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldUp,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(p.normalizeVec3(p.mulVec3Scalar(a.worldForward,1,Xh),qh))):h&&(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldUp,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(p.normalizeVec3(p.mulVec3Scalar(a.worldForward,-1,Xh)))),!s.firstPerson&&s.followPointer&&t.pivotController.setPivotPos(Kh),t.cameraFlight.duration>0?t.cameraFlight.flyTo(Zh,(()=>{t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot()})):(t.cameraFlight.jumpTo(Zh),t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot())}))}reset(){}destroy(){this._scene.input.off(this._onSceneKeyDown)}}class ep{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=t.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;let o=!1,c=!1;const u=this._scene.canvas.canvas,h=s=>{let n;s&&s.worldPos&&(n=s.worldPos);const i=s&&s.entity?s.entity.aabb:e.aabb;if(n){const s=e.camera;p.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};u.addEventListener("mousemove",this._canvasMouseMoveHandler=t=>{if(!s.active||!s.pointerEnabled)return;if(o||c)return;const i=l.hasSubs("hover"),r=l.hasSubs("hoverOut"),u=l.hasSubs("hoverOff"),h=l.hasSubs("hoverSurface"),p=l.hasSubs("hoverSnapOrSurface");if(i||r||u||h||p)if(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=h,a.scheduleSnapOrPick=p,a.update(),a.pickResult){if(a.pickResult.entity){const t=a.pickResult.entity.id;this._lastPickedEntityId!==t&&(void 0!==this._lastPickedEntityId&&l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),l.fire("hoverEnter",a.pickResult,!0),this._lastPickedEntityId=t)}l.fire("hover",a.pickResult,!0),(a.pickResult.worldPos||a.pickResult.snappedWorldPos)&&l.fire("hoverSurface",a.pickResult,!0)}else void 0!==this._lastPickedEntityId&&(l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),l.fire("hoverOff",{canvasPos:a.pickCursorPos},!0)}),u.addEventListener("mousedown",this._canvasMouseDownHandler=t=>{1===t.which&&(o=!0),3===t.which&&(c=!0);if(1===t.which&&s.active&&s.pointerEnabled&&(n.mouseDownClientX=t.clientX,n.mouseDownClientY=t.clientY,n.mouseDownCursorX=n.pointerCanvasPos[0],n.mouseDownCursorY=n.pointerCanvasPos[1],!s.firstPerson&&s.followPointer&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),1===t.which))){const t=a.pickResult;t&&t.worldPos?(r.setPivotPos(t.worldPos),r.startPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),r.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{1===e.which&&(o=!1),3===e.which&&(c=!1)}),u.addEventListener("mouseup",this._canvasMouseUpHandler=i=>{if(!s.active||!s.pointerEnabled)return;if(!(1===i.which))return;if(r.hidePivot(),Math.abs(i.clientX-n.mouseDownClientX)>3||Math.abs(i.clientY-n.mouseDownClientY)>3)return;const o=l.hasSubs("picked"),c=l.hasSubs("pickedNothing"),u=l.hasSubs("pickedSurface"),A=l.hasSubs("doublePicked"),d=l.hasSubs("doublePickedSurface"),f=l.hasSubs("doublePickedNothing");if(!(s.doublePickFlyTo||A||d||f))return(o||c||u)&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=u,a.update(),a.pickResult?(l.fire("picked",a.pickResult,!0),a.pickedSurface&&l.fire("pickedSurface",a.pickResult,!0)):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0)),void(this._clicks=0);if(this._clicks++,1===this._clicks){a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo,a.schedulePickSurface=u,a.update();const e=a.pickResult,i=a.pickedSurface;this._timeout=setTimeout((()=>{e?(l.fire("picked",e,!0),i&&(l.fire("pickedSurface",e,!0),!s.firstPerson&&s.followPointer&&(t.pivotController.setPivotPos(e.worldPos),t.pivotController.startPivot()&&t.pivotController.showPivot()))):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0),this._clicks=0}),s.doubleClickTimeFrame)}else{if(null!==this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null),a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo||A||d,a.schedulePickSurface=a.schedulePickEntity&&d,a.update(),a.pickResult){if(l.fire("doublePicked",a.pickResult,!0),a.pickedSurface&&l.fire("doublePickedSurface",a.pickResult,!0),s.doublePickFlyTo&&(h(a.pickResult),!s.firstPerson&&s.followPointer)){const e=a.pickResult.entity.aabb,s=p.getAABB3Center(e);t.pivotController.setPivotPos(s),t.pivotController.startPivot()&&t.pivotController.showPivot()}}else if(l.fire("doublePickedNothing",{canvasPos:n.pointerCanvasPos},!0),s.doublePickFlyTo&&(h(),!s.firstPerson&&s.followPointer)){const s=e.aabb,n=p.getAABB3Center(s);t.pivotController.setPivotPos(n),t.pivotController.startPivot()&&t.pivotController.showPivot()}this._clicks=0}},!1)}reset(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}destroy(){const e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}class tp{constructor(e,t,s,n,i){this._scene=e;const a=e.input,r=[],l=e.canvas.canvas;let o=!0;this._onSceneMouseMove=a.on("mousemove",(()=>{o=!0})),this._onSceneKeyDown=a.on("keydown",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&n.mouseover&&(r[t]=!0,t===a.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=a.on("keyup",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&(r[t]=!1,t===a.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=e.on("tick",(l=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const c=t.cameraControl,u=l.deltaTime/1e3;if(!s.planView){const e=c._isKeyDownForAction(c.ROTATE_Y_POS,r),n=c._isKeyDownForAction(c.ROTATE_Y_NEG,r),a=c._isKeyDownForAction(c.ROTATE_X_POS,r),l=c._isKeyDownForAction(c.ROTATE_X_NEG,r),o=u*s.keyboardRotationRate;(e||n||a||l)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),e?i.rotateDeltaY+=o:n&&(i.rotateDeltaY-=o),a?i.rotateDeltaX+=o:l&&(i.rotateDeltaX-=o),!s.firstPerson&&s.followPointer&&t.pivotController.startPivot())}if(!r[a.KEY_CTRL]&&!r[a.KEY_ALT]){const e=c._isKeyDownForAction(c.DOLLY_BACKWARDS,r),a=c._isKeyDownForAction(c.DOLLY_FORWARDS,r);if(e||a){const r=u*s.keyboardDollyRate;!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),a?i.dollyDelta-=r:e&&(i.dollyDelta+=r),o&&(n.followPointerDirty=!0,o=!1)}}const h=c._isKeyDownForAction(c.PAN_FORWARDS,r),p=c._isKeyDownForAction(c.PAN_BACKWARDS,r),A=c._isKeyDownForAction(c.PAN_LEFT,r),d=c._isKeyDownForAction(c.PAN_RIGHT,r),f=c._isKeyDownForAction(c.PAN_UP,r),I=c._isKeyDownForAction(c.PAN_DOWN,r),y=(r[a.KEY_ALT]?.3:1)*u*s.keyboardPanRate;(h||p||A||d||f||I)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),I?i.panDeltaY+=y:f&&(i.panDeltaY+=-y),d?i.panDeltaX+=-y:A&&(i.panDeltaX+=y),p?i.panDeltaZ+=y:h&&(i.panDeltaZ+=-y))}))}reset(){}destroy(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}const sp=p.vec3();class np{constructor(e,t,s,n,i){this._scene=e;const a=e.camera,r=t.pickController,l=t.pivotController,o=t.panController;let c=1,u=1,h=null;this._onTick=e.on("tick",(()=>{if(!s.active||!s.pointerEnabled)return;let t="default";if(Math.abs(i.dollyDelta)<.001&&(i.dollyDelta=0),Math.abs(i.rotateDeltaX)<.001&&(i.rotateDeltaX=0),Math.abs(i.rotateDeltaY)<.001&&(i.rotateDeltaY=0),0===i.rotateDeltaX&&0===i.rotateDeltaY||(i.dollyDelta=0),s.followPointer&&--c<=0&&(c=1,0!==i.dollyDelta)){if(0===i.rotateDeltaY&&0===i.rotateDeltaX&&s.followPointer&&n.followPointerDirty&&(r.pickCursorPos=n.pointerCanvasPos,r.schedulePickSurface=!0,r.update(),r.pickResult&&r.pickResult.worldPos?h=r.pickResult.worldPos:(u=1,h=null),n.followPointerDirty=!1),h){const t=Math.abs(p.lenVec3(p.subVec3(h,e.camera.eye,sp)));u=t/s.dollyProximityThreshold}u{n.mouseover=!0}),a.addEventListener("mouseleave",this._mouseLeaveHandler=()=>{n.mouseover=!1,a.style.cursor=null}),document.addEventListener("mousemove",this._mouseMoveHandler=e=>{ap(e,a,n.pointerCanvasPos)}),a.addEventListener("mousedown",this._mouseDownHandler=e=>{s.active&&s.pointerEnabled&&(ap(e,a,n.pointerCanvasPos),n.mouseover=!0)}),a.addEventListener("mouseup",this._mouseUpHandler=e=>{s.active&&s.pointerEnabled})}reset(){}destroy(){const e=this._scene.canvas.canvas;document.removeEventListener("mousemove",this._mouseMoveHandler),e.removeEventListener("mouseenter",this._mouseEnterHandler),e.removeEventListener("mouseleave",this._mouseLeaveHandler),e.removeEventListener("mousedown",this._mouseDownHandler),e.removeEventListener("mouseup",this._mouseUpHandler)}}function ap(e,t,s){if(e){const{x:n,y:i}=t.getBoundingClientRect();s[0]=e.clientX-n,s[1]=e.clientY-i}else e=window.event,s[0]=e.x,s[1]=e.y;return s}const rp=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class lp{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=p.vec2(),o=p.vec2(),c=p.vec2(),u=p.vec2(),h=[],A=this._scene.canvas.canvas;let d=0,f=!1;this._onTick=e.on("tick",(()=>{f=!1})),A.addEventListener("touchstart",this._canvasTouchStartHandler=t=>{if(!s.active||!s.pointerEnabled)return;t.preventDefault();const i=t.touches,o=t.changedTouches;for(n.touchStartTime=Date.now(),1===i.length&&1===o.length&&(rp(i[0],l),s.followPointer&&(a.pickCursorPos=l,a.schedulePickSurface=!0,a.update(),s.planView||(a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(r.setPivotPos(a.pickResult.worldPos),!s.firstPerson&&r.startPivot()&&r.showPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),!s.firstPerson&&r.startPivot()&&r.showPivot()))));h.length{if(!s.active||!s.pointerEnabled)return;if(t.stopPropagation(),t.preventDefault(),f)return;f=!0;const r=e.canvas.boundary,l=r[2],A=r[3],I=t.touches;if(t.touches.length===d){if(1===d){rp(I[0],o),p.subVec2(o,h[0],u);const t=u[0],a=u[1];if(null!==n.longTouchTimeout&&(Math.abs(t)>s.longTapRadius||Math.abs(a)>s.longTapRadius)&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),s.planView){const n=e.camera;if("perspective"===n.projection){const r=Math.abs(e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=t*r/A*s.touchPanRate,i.panDeltaY+=a*r/A*s.touchPanRate}else i.panDeltaX+=.5*n.ortho.scale*(t/A)*s.touchPanRate,i.panDeltaY+=.5*n.ortho.scale*(a/A)*s.touchPanRate}else i.rotateDeltaY-=t/l*(1*s.dragRotationRate),i.rotateDeltaX+=a/A*(1.5*s.dragRotationRate)}else if(2===d){const t=I[0],r=I[1];rp(t,o),rp(r,c);const l=p.geometricMeanVec2(h[0],h[1]),u=p.geometricMeanVec2(o,c),d=p.vec2();p.subVec2(l,u,d);const f=d[0],y=d[1],m=e.camera,v=p.distVec2([t.pageX,t.pageY],[r.pageX,r.pageY]),w=(p.distVec2(h[0],h[1])-v)*s.touchDollyRate;if(i.dollyDelta=w,Math.abs(w)<1)if("perspective"===m.projection){const t=a.pickResult?a.pickResult.worldPos:e.center,n=Math.abs(p.lenVec3(p.subVec3(t,e.camera.eye,[])))*Math.tan(m.perspective.fov/2*Math.PI/180);i.panDeltaX-=f*n/A*s.touchPanRate,i.panDeltaY-=y*n/A*s.touchPanRate}else i.panDeltaX-=.5*m.ortho.scale*(f/A)*s.touchPanRate,i.panDeltaY-=.5*m.ortho.scale*(y/A)*s.touchPanRate;n.pointerCanvasPos=u}for(let e=0;e{let n;s&&s.worldPos&&(n=s.worldPos);const i=s?s.entity.aabb:e.aabb;if(n){const s=e.camera;p.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};A.addEventListener("touchstart",this._canvasTouchStartHandler=e=>{if(!s.active||!s.pointerEnabled)return;null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null);const i=e.touches,a=e.changedTouches;if(l=Date.now(),1===i.length&&1===a.length){u=l,op(i[0],c);const a=c[0],r=c[1],o=i[0].pageX,h=i[0].pageY;n.longTouchTimeout=setTimeout((()=>{t.cameraControl.fire("rightClick",{pagePos:[Math.round(o),Math.round(h)],canvasPos:[Math.round(a),Math.round(r)],event:e},!0),n.longTouchTimeout=null}),s.longTapTimeout)}else u=-1;for(;o.length{if(!s.active||!s.pointerEnabled)return;const t=Date.now(),i=e.touches,l=e.changedTouches,A=r.hasSubs("pickedSurface");null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),0===i.length&&1===l.length&&u>-1&&t-u<150&&(h>-1&&u-h<325?(op(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("doublePicked",a.pickResult),a.pickedSurface&&r.fire("doublePickedSurface",a.pickResult),s.doublePickFlyTo&&d(a.pickResult)):(r.fire("doublePickedNothing"),s.doublePickFlyTo&&d()),h=-1):p.distVec2(o[0],c)<4&&(op(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("picked",a.pickResult),a.pickedSurface&&r.fire("pickedSurface",a.pickResult)):r.fire("pickedNothing"),h=t),u=-1),o.length=i.length;for(let e=0,t=i.length;e{e.preventDefault()},this._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},this._states={pointerCanvasPos:p.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:p.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},this._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};const s=this.scene;this._controllers={cameraControl:this,pickController:new Qh(this,this._configs),pivotController:new kh(s,this._configs),panController:new Mh(s),cameraFlight:new Fa(this,{duration:.5})},this._handlers=[new ip(this.scene,this._controllers,this._configs,this._states,this._updates),new lp(this.scene,this._controllers,this._configs,this._states,this._updates),new zh(this.scene,this._controllers,this._configs,this._states,this._updates),new $h(this.scene,this._controllers,this._configs,this._states,this._updates),new ep(this.scene,this._controllers,this._configs,this._states,this._updates),new cp(this.scene,this._controllers,this._configs,this._states,this._updates),new tp(this.scene,this._controllers,this._configs,this._states,this._updates)],this._cameraUpdater=new np(this.scene,this._controllers,this._configs,this._states,this._updates),this.navMode=t.navMode,t.planView&&(this.planView=t.planView),this.constrainVertical=t.constrainVertical,t.keyboardLayout?this.keyboardLayout=t.keyboardLayout:this.keyMap=t.keyMap,this.doublePickFlyTo=t.doublePickFlyTo,this.panRightClick=t.panRightClick,this.active=t.active,this.followPointer=t.followPointer,this.rotationInertia=t.rotationInertia,this.keyboardPanRate=t.keyboardPanRate,this.touchPanRate=t.touchPanRate,this.keyboardRotationRate=t.keyboardRotationRate,this.dragRotationRate=t.dragRotationRate,this.touchDollyRate=t.touchDollyRate,this.dollyInertia=t.dollyInertia,this.dollyProximityThreshold=t.dollyProximityThreshold,this.dollyMinSpeed=t.dollyMinSpeed,this.panInertia=t.panInertia,this.pointerEnabled=!0,this.keyboardDollyRate=t.keyboardDollyRate,this.mouseWheelDollyRate=t.mouseWheelDollyRate}set keyMap(e){if(e=e||"qwerty",m.isString(e)){const t=this.scene.input,s={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":s[this.PAN_LEFT]=[t.KEY_A],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_Z],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":s[this.PAN_LEFT]=[t.KEY_Q],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_W],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=s}else{const t=e;this._keyMap=t}}get keyMap(){return this._keyMap}_isKeyDownForAction(e,t){const s=this._keyMap[e];if(!s)return!1;t||(t=this.scene.input.keyDown);for(let e=0,n=s.length;e0?Ip(t):null,r=s&&s.length>0?Ip(s):null,l=e=>{if(!e)return;var t=!0;(r&&r[e.type]||a&&!a[e.type])&&(t=!1),t&&n.push(e.id);const s=e.children;if(s)for(var i=0,o=s.length;i>t;s.sort(Fc);const n=new Int32Array(e.length);for(let t=0,i=s.length;te[t+1]){let s=e[t];e[t]=e[t+1],e[t+1]=s}Hc=new Int32Array(e),t.sort(Uc);const s=new Int32Array(e.length);for(let n=0,i=t.length;nt){let s=e;e=t,t=s}function s(s,n){return s!==e?e-s:n!==t?t-n:0}let n=0,i=(a.length>>1)-1;for(;n<=i;){const e=i+n>>1,t=s(a[2*e],a[2*e+1]);if(t>0)n=e+1;else{if(!(t<0))return e;i=e-1}}return-n-1}const l=new Int32Array(a.length/2);l.fill(0);const o=n.length/3;if(o>8*(1<p.maxNumPositions&&(p=h()),p.bucketNumber>8)return[e];let d;-1===c[o]&&(c[o]=p.numPositions++,p.positionsCompressed.push(n[3*o]),p.positionsCompressed.push(n[3*o+1]),p.positionsCompressed.push(n[3*o+2])),-1===c[u]&&(c[u]=p.numPositions++,p.positionsCompressed.push(n[3*u]),p.positionsCompressed.push(n[3*u+1]),p.positionsCompressed.push(n[3*u+2])),-1===c[A]&&(c[A]=p.numPositions++,p.positionsCompressed.push(n[3*A]),p.positionsCompressed.push(n[3*A+1]),p.positionsCompressed.push(n[3*A+2])),p.indices.push(c[o]),p.indices.push(c[u]),p.indices.push(c[A]),(d=r(o,u))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(o,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]])),(d=r(u,A))>=0&&0===l[d]&&(l[d]=1,p.edgeIndices.push(c[a[2*d]]),p.edgeIndices.push(c[a[2*d+1]]))}const A=t/8*2,d=t/8,f=2*n.length+(i.length+a.length)*A;let I=0,y=-n.length/3;return u.forEach((e=>{I+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*d,y+=e.positionsCompressed.length/3})),I>f?[e]:(s&&function(e,t){const s={},n={};let i=0;e.forEach((e=>{const t=e.indices,a=e.edgeIndices,r=e.positionsCompressed;for(let e=0,n=t.length;e0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl,s=e._lightsState;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uLightAmbient=n.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];const i=s.lights;let a;for(let e=0,t=i.length;e0;let i;const a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),e.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(let e=0,t=s.lights.length;e> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(let e=0,t=s.lights.length;e0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),this._withSAO&&(n.push("uniform sampler2D uOcclusionTexture;"),n.push("uniform vec4 uSAOParams;"),n.push("const float packUpscale = 256. / 255.;"),n.push("const float unpackDownScale = 255. / 256.;"),n.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),n.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),n.push("float unpackRGBToFloat( const in vec4 v ) {"),n.push(" return dot( v, unPackFactors );"),n.push("}")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(n.push(" float viewportWidth = uSAOParams[0];"),n.push(" float viewportHeight = uSAOParams[1];"),n.push(" float blendCutoff = uSAOParams[2];"),n.push(" float blendFactor = uSAOParams[3];"),n.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),n.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),n.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Yc=new Float32Array([1,1,1]),Xc=p.vec3(),qc=p.vec3(),Jc=p.vec3(),Zc=p.vec3(),$c=p.mat4();class eu{constructor(e,t){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=a,d=i.viewMatrix;if(!this._program&&(this._allocate(),this.errors))return;let f,I;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),c||0!==u[0]||0!==u[1]||0!==u[2]){const e=Xc;if(c){const t=qc;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,$c),I=Jc,I[0]=i.eye[0]-e[0],I[1]=i.eye[1]-e[1],I[2]=i.eye[2]-e[2]}else f=d,I=i.eye;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s===cr.SILHOUETTE_XRAYED){const e=n.xrayMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.SILHOUETTE_HIGHLIGHTED){const e=n.highlightMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.SILHOUETTE_SELECTED){const e=n.selectedMaterial._state,t=e.fillColor,s=e.fillAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,Yc);if(n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=n._sectionPlanesState.sectionPlanes.length;if(y>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture silhouette vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.y) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("} else {"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("viewNormal = -viewNormal;"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = color;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const tu=new Float32Array([0,0,0,1]),su=p.vec3(),nu=p.vec3();p.vec3();const iu=p.vec3(),au=p.mat4();class ru{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=su;if(I){const t=nu;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,au)}else f=d;if(r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s===cr.EDGES_XRAYED){const e=i.xrayMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.EDGES_HIGHLIGHTED){const e=i.highlightMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else if(s===cr.EDGES_SELECTED){const e=i.selectedMaterial._state,t=e.edgeColor,s=e.edgeAlpha;r.uniform4f(this._uColor,t[0],t[1],t[2],s)}else r.uniform4fv(this._uColor,tu);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uColor=s.getLocation("color"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vColor = vec4(color.r, color.g, color.b, color.a);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const lu=p.vec3(),ou=p.vec3(),cu=p.vec3(),uu=p.mat4();class hu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=lu;if(I){const t=ou;p.transformPoint3(h,c,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],f=O(d,e,uu)}else f=d;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(r.LINES,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(r.LINES,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(r.LINES,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.z) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("vec4 rgb = vec4(color.rgba);"),s.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { discard; }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vColor;"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const pu=p.vec3(),Au=p.vec3(),du=p.vec3(),fu=p.mat4();class Iu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(t),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n;let d,f;o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=pu;if(I){const t=p.transformPoint3(h,c,Au);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=O(a.viewMatrix,e,fu),f=du,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=a.viewMatrix,f=a.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry picking vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("smooth out vec4 vWorldPosition;"),s.push("flat out uvec4 vFlags2;")),s.push("out vec4 vPickColor;"),s.push("void main(void) {"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry picking fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outPickColor = vPickColor; "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const yu=p.vec3(),mu=p.vec3(),vu=p.vec3(),wu=p.vec3(),gu=p.mat4();class Tu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=e.pickViewMatrix||a.viewMatrix;let f,I;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),a.eye,c||0!==u[0]||0!==u[1]||0!==u[2]){const t=yu;if(c){const e=mu;p.transformPoint3(h,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],f=O(d,t,gu),I=vu,I[0]=a.eye[0]-t[0],I[1]=a.eye[1]-t[1],I[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else f=d,I=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniform1f(this._uPickZNear,e.pickZNear),r.uniform1f(this._uPickZFar,e.pickZFar),r.uniformMatrix4fv(this._uWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,f),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const y=i._sectionPlanesState.sectionPlanes.length;if(y>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*y,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick depth vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("}"),s.push("}"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform float pickZNear;"),n.push("uniform float pickZFar;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),n.push(" outPackedDepth = packDepth(zNormalizedDepth); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Eu=p.vec3(),bu=p.vec3(),Du=p.vec3(),Pu=p.vec3(),Ru=p.vec3(),Cu=p.mat4();class _u{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){if(!this._program&&(this._allocate(),this.errors))return;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Eu;let y,m;I[0]=p.safeInv(d[3]-d[0])*p.MAX_INT,I[1]=p.safeInv(d[4]-d[1])*p.MAX_INT,I[2]=p.safeInv(d[5]-d[2])*p.MAX_INT,e.snapPickCoordinateScale[0]=p.safeInv(I[0]),e.snapPickCoordinateScale[1]=p.safeInv(I[1]),e.snapPickCoordinateScale[2]=p.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=bu;if(v){const e=p.transformPoint3(h,c,Du);t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=O(f,t,Cu),m=Pu,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this.uVectorA,e.snapVectorA),r.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),r.drawArrays(T,0,l.numEdgeIndices8Bits)),l.numEdgeIndices16Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),r.drawArrays(T,0,l.numEdgeIndices16Bits)),l.numEdgeIndices32Bits>0&&(o.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),r.drawArrays(T,0,l.numEdgeIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Batched geometry edges drawing vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),s.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uSnapVectorA;"),s.push("uniform vec2 uSnapInvVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),s.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec4 vViewPosition;"),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int edgeIndex = gl_VertexID / 2;"),s.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),s.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),s.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),s.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),s.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.r;")),s.push("vViewPosition = viewPosition;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.xyzw *= tmp;"),s.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push("gl_PointSize = 1.0;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Bu=p.vec3(),Ou=p.vec3(),Su=p.vec3(),Nu=p.vec3(),xu=p.vec3(),Lu=p.mat4();class Mu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=n,d=t.aabb,f=e.pickViewMatrix||a.viewMatrix,I=Bu;let y,m;I[0]=p.safeInv(d[3]-d[0])*p.MAX_INT,I[1]=p.safeInv(d[4]-d[1])*p.MAX_INT,I[2]=p.safeInv(d[5]-d[2])*p.MAX_INT,e.snapPickCoordinateScale[0]=p.safeInv(I[0]),e.snapPickCoordinateScale[1]=p.safeInv(I[1]),e.snapPickCoordinateScale[2]=p.safeInv(I[2]),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const v=0!==c[0]||0!==c[1]||0!==c[2],w=0!==u[0]||0!==u[1]||0!==u[2];if(v||w){const t=Ou;if(v){const e=Su;p.transformPoint3(h,c,e),t[0]=e[0],t[1]=e[1],t[2]=e[2]}else t[0]=0,t[1]=0,t[2]=0;t[0]+=u[0],t[1]+=u[1],t[2]+=u[2],y=O(f,t,Lu),m=Nu,m[0]=a.eye[0]-t[0],m[1]=a.eye[1]-t[1],m[2]=a.eye[2]-t[2],e.snapPickOrigin[0]=t[0],e.snapPickOrigin[1]=t[1],e.snapPickOrigin[2]=t[2]}else y=f,m=a.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(r.uniform3fv(this._uCameraEyeRtc,m),r.uniform2fv(this._uVectorA,e.snapVectorA),r.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),r.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),r.uniform3fv(this._uCoordinateScaler,I),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,y),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),i.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const g=i._sectionPlanesState.sectionPlanes.length;if(g>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*g,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=s.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform mat4 sceneModelWorldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("uniform vec2 uVectorAB;"),s.push("uniform vec2 uInverseVectorAB;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("vec2 remapClipPos(vec2 clipPos) {"),s.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),s.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),s.push(" return vec2(x, y);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out highp vec3 relativeToOriginPosition;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("{"),s.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),s.push("if (color.a == 0u) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("};"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (solid != 1u) {"),s.push(" if (isPerspectiveMatrix(projMatrix)) {"),s.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" } else {"),s.push(" if (viewNormal.z < 0.0) {"),s.push(" position = positions[2 - (gl_VertexID % 3)];"),s.push(" viewNormal = -viewNormal;"),s.push(" }"),s.push(" }"),s.push("}"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("relativeToOriginPosition = worldPosition.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push("vWorldPosition = worldPosition;"),s.push("vFlags2 = flags2.r;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),s.push("float tmp = clipPos.w;"),s.push("clipPos.xyzw /= tmp;"),s.push("clipPos.xy = remapClipPos(clipPos.xy);"),s.push("clipPos.z += 0.0001;"),s.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("uniform int uLayerNumber;"),n.push("uniform vec3 uCoordinateScaler;"),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Fu=p.vec3();class Hu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);let u=a.eye;e.pickViewMatrix&&(o.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),u=e.pickOrigin||u);const h=[u[0]-c[0],u[1]-c[1],u[2]-c[2]];if(r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uRenderPass,s),r.uniform3fv(this._uCameraEyeRtc,h),r.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const p=i._sectionPlanesState.sectionPlanes.length;if(p>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*p,a=n.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),s.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),s.push("uniform highp sampler2D uTextureCameraMatrices;"),s.push("uniform highp sampler2D uTextureModelMatrices;"),s.push("uniform vec3 uCameraEyeRtc;"),s.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("out float isPerspective;")),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("flat out uint vFlags2;")),s.push("out vec3 vWorldNormal;"),s.push("void main(void) {"),s.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),s.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),s.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),s.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),s.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),s.push(" return;"),s.push("} else {"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),s.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),s.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),s.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),s.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),s.push("vec3 position;"),s.push("position = positions[gl_VertexID % 3];"),s.push("if (solid != 1u) {"),s.push("if (isPerspectiveMatrix(projMatrix)) {"),s.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),s.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("} else {"),s.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),s.push("if (viewNormal.z < 0.0) {"),s.push("position = positions[2 - (gl_VertexID % 3)];"),s.push("normal = -normal;"),s.push("}"),s.push("}"),s.push("}"),s.push("normal = -normal;"),s.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),s.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),s.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),s.push("vec4 viewPosition = viewMatrix * worldPosition; "),s.push("vWorldNormal = normal.xyz;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2.w;")),s.push("gl_Position = clipPos;"),s.push("}"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(var i=0;i 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}p.vec3(),p.vec3(),p.vec3(),p.vec3(),p.mat4();class Uu{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// TrianglesDataTextureColorRenderer vertex shader"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0;e 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Gu=p.vec3(),Vu=p.vec3(),ju=p.vec3(),ku=p.vec3(),Qu=p.mat4();class Wu{constructor(e){this._scene=e,this._allocate(),this._hash=this._getHash()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=this._scene,i=n.camera,a=t.model,r=n.canvas.gl,l=t._state,o=l.textureState,c=t._state.origin,{position:u,rotationMatrix:h,rotationMatrixConjugate:A}=a;if(!this._program&&(this._allocate(),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,l)),o.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);const I=0!==c[0]||0!==c[1]||0!==c[2],y=0!==u[0]||0!==u[1]||0!==u[2];if(I||y){const e=Gu;if(I){const t=p.transformPoint3(h,c,Vu);e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=u[0],e[1]+=u[1],e[2]+=u[2],d=O(i.viewMatrix,e,Qu),f=ju,f[0]=i.eye[0]-e[0],f[1]=i.eye[1]-e[1],f[2]=i.eye[2]-e[2]}else d=i.viewMatrix,f=i.eye;if(r.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,A),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),r.uniform3fv(this._uCameraEyeRtc,f),r.uniform1i(this._uRenderPass,s),n.logarithmicDepthBufferEnabled){const t=2/(Math.log(e.pickZFar+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,t)}const m=n._sectionPlanesState.sectionPlanes.length;if(m>0){const e=n._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,i=a.renderFlags;for(let t=0;t0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(o.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=s.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture draw vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out highp vec2 vHighPrecisionZW;"),s&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("vHighPrecisionZW = gl_Position.zw;"),n.push("}"),n.push("}"),n}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture draw fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),n.push("in highp vec2 vHighPrecisionZW;"),n.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("flat in uint vFlags2;");for(let e=0,s=t.sectionPlanes.length;e 0u;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(let e=0,s=t.sectionPlanes.length;e 0.0) { "),n.push(" discard;"),n.push(" }"),n.push("}")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),n.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const zu=p.vec3(),Ku=p.vec3(),Yu=p.vec3(),Xu=p.vec3(),qu=p.mat4();class Ju{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin,{position:c,rotationMatrix:u,rotationMatrixConjugate:h}=n,A=a.viewMatrix;if(!this._program&&(this._allocate(t),this.errors))return;let d,f;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));const I=0!==o[0]||0!==o[1]||0!==o[2],y=0!==c[0]||0!==c[1]||0!==c[2];if(I||y){const e=zu;if(I){const t=Ku;p.transformPoint3(u,o,t),e[0]=t[0],e[1]=t[1],e[2]=t[2]}else e[0]=0,e[1]=0,e[2]=0;e[0]+=c[0],e[1]+=c[1],e[2]+=c[2],d=O(A,e,qu),f=Yu,f[0]=a.eye[0]-e[0],f[1]=a.eye[1]-e[1],f[2]=a.eye[2]-e[2]}else d=A,f=a.eye;r.uniform1i(this._uRenderPass,s),r.uniformMatrix4fv(this._uWorldMatrix,!1,h),r.uniformMatrix4fv(this._uViewMatrix,!1,d),r.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),r.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),r.uniformMatrix4fv(this._uWorldNormalMatrix,!1,n.worldNormalMatrix);const m=i._sectionPlanesState.sectionPlanes.length;if(m>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*m,a=n.renderFlags;for(let t=0;t0,s=[];return s.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("#extension GL_EXT_frag_depth : enable"),s.push("uniform int renderPass;"),s.push("attribute vec3 position;"),e.entityOffsetsEnabled&&s.push("attribute vec3 offset;"),s.push("attribute vec3 normal;"),s.push("attribute vec4 color;"),s.push("attribute vec4 flags;"),s.push("attribute vec4 flags2;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 worldNormalMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform mat4 viewNormalMatrix;"),s.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("varying float isPerspective;")),s.push("vec3 octDecode(vec2 oct) {"),s.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),s.push(" if (v.z < 0.0) {"),s.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),s.push(" }"),s.push(" return normalize(v);"),s.push("}"),t&&(s.push("out vec4 vWorldPosition;"),s.push("out vec4 vFlags2;")),s.push("out vec3 vViewNormal;"),s.push("void main(void) {"),s.push("if (int(flags.x) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),s.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(s.push(" vWorldPosition = worldPosition;"),s.push(" vFlags2 = flags2;")),s.push(" vViewNormal = viewNormal;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth?s.push("vFragDepth = 1.0 + clipPos.w;"):(s.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),s.push("clipPos.z *= clipPos.w;")),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;"),n.push("in vec4 vFlags2;");for(let e=0;e 0.0);"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(var i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&Ae.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}const Zu=p.vec3();class $u{constructor(e){this._scene=e,this._hash=this._getHash(),this._allocate()}getValid(){return this._hash===this._getHash()}_getHash(){return this._scene._sectionPlanesState.getHash()}drawLayer(e,t,s){const n=t.model,i=n.scene,a=i.camera,r=i.canvas.gl,l=t._state,o=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerVertexIdCoordinates),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},3),r.uniform1i(this._uRenderPass,s),r.uniform1i(this._uPickInvisible,e.pickInvisible),r.uniformMatrix4fv(this._uWorldMatrix,!1,n.worldMatrix);const c=e.pickViewMatrix||a.viewMatrix,u=o?O(c,o):c;if(r.uniformMatrix4fv(this._uViewMatrix,!1,u),r.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.project.far+1)/Math.LN2);r.uniform1f(this._uLogDepthBufFC,e)}const h=i._sectionPlanesState.sectionPlanes.length;if(h>0){const e=i._sectionPlanesState.sectionPlanes,s=t.layerIndex*h,a=n.renderFlags;for(let t=0;t0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices8Bits)),l.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices16Bits)),l.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,l.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){r.activeTexture(r["TEXTURE"+e]),r.bindTexture(r.TEXTURE_2D,null)}},5),r.drawArrays(r.TRIANGLES,0,l.numIndices32Bits)),e.drawElements++}_allocate(){const e=this._scene,t=e.canvas.gl;if(this._program=new De(t,this._buildShader()),this._program.errors)return void(this.errors=this._program.errors);const s=this._program;this._uRenderPass=s.getLocation("renderPass"),this._uPickInvisible=s.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=s.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=s.getLocation("worldMatrix"),this._uViewMatrix=s.getLocation("viewMatrix"),this._uProjMatrix=s.getLocation("projMatrix"),this._uSectionPlanes=[];for(let t=0,n=e._sectionPlanesState.sectionPlanes.length;t0,s=[];return s.push("#version 300 es"),s.push("// Triangles dataTexture pick flat normals vertex shader"),s.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),s.push("precision highp float;"),s.push("precision highp int;"),s.push("precision highp usampler2D;"),s.push("precision highp isampler2D;"),s.push("precision highp sampler2D;"),s.push("#else"),s.push("precision mediump float;"),s.push("precision mediump int;"),s.push("precision mediump usampler2D;"),s.push("precision mediump isampler2D;"),s.push("precision mediump sampler2D;"),s.push("#endif"),s.push("uniform int renderPass;"),s.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&s.push("in vec3 offset;"),s.push("uniform bool pickInvisible;"),s.push("uniform mat4 worldMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),s.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),s.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),s.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),s.push("uniform usampler2D uTexturePerPolygonIdIndices;"),s.push("uniform isampler2D uTexturePerPolygonIdNormals;"),s.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;"),s.push("bool isPerspectiveMatrix(mat4 m) {"),s.push(" return (m[2][3] == - 1.0);"),s.push("}"),s.push("out float isPerspective;")),s.push("out vec4 vWorldPosition;"),t&&s.push("out int vFlags2;"),s.push("void main(void) {"),s.push("int polygonIndex = gl_VertexID / 3;"),s.push("int h_normal_index = polygonIndex & 4095;"),s.push("int v_normal_index = polygonIndex >> 12;"),s.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),s.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),s.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),s.push("int objectIndex;"),s.push("if (((polygonIndex >> 3) % 2) == 0) {"),s.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),s.push("} else {"),s.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),s.push("}"),s.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),s.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),s.push("int h_index = polygonIndex & 4095;"),s.push("int v_index = polygonIndex >> 12;"),s.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),s.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),s.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),s.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),s.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),s.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),s.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),s.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),s.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),s.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),s.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),s.push("int vertexNumber = gl_VertexID % 3;"),s.push("vec3 position;"),s.push("if (vertexNumber == 0) position = position1;"),s.push("else if (vertexNumber == 1) position = position2;"),s.push("else position = position3;"),s.push("if (int(flags.w) != renderPass) {"),s.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),s.push(" } else {"),s.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&s.push(" worldPosition.xyz = worldPosition.xyz + offset;"),s.push(" vec4 viewPosition = viewMatrix * worldPosition; "),s.push(" vWorldPosition = worldPosition;"),t&&s.push(" vFlags2 = flags2.r;"),s.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(s.push("vFragDepth = 1.0 + clipPos.w;"),s.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),s.push("gl_Position = clipPos;"),s.push(" }"),s.push("}"),s}_buildFragmentShader(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals fragment shader"),n.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("in float isPerspective;"),n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),n.push("in vec4 vWorldPosition;"),s){n.push("in int vFlags2;");for(var i=0;i 0;"),n.push(" if (clippable) {"),n.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),n.push(" }")}return e.logarithmicDepthBufferEnabled&&n.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),n.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),n.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),n.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),n.push("}"),n}webglContextRestored(){this._program=null}destroy(){this._program&&this._program.destroy(),this._program=null}}class eh{constructor(e){this._scene=e}_compile(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}get colorRenderer(){return this._colorRenderer||(this._colorRenderer=new Kc(this._scene,!1)),this._colorRenderer}get colorRendererWithSAO(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new Kc(this._scene,!0)),this._colorRendererWithSAO}get colorQualityRendererWithSAO(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}get silhouetteRenderer(){return this._silhouetteRenderer||(this._silhouetteRenderer=new eu(this._scene)),this._silhouetteRenderer}get depthRenderer(){return this._depthRenderer||(this._depthRenderer=new Wu(this._scene)),this._depthRenderer}get normalsRenderer(){return this._normalsRenderer||(this._normalsRenderer=new Ju(this._scene)),this._normalsRenderer}get edgesRenderer(){return this._edgesRenderer||(this._edgesRenderer=new ru(this._scene)),this._edgesRenderer}get edgesColorRenderer(){return this._edgesColorRenderer||(this._edgesColorRenderer=new hu(this._scene)),this._edgesColorRenderer}get pickMeshRenderer(){return this._pickMeshRenderer||(this._pickMeshRenderer=new Iu(this._scene)),this._pickMeshRenderer}get pickNormalsRenderer(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new Hu(this._scene)),this._pickNormalsRenderer}get pickNormalsFlatRenderer(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new $u(this._scene)),this._pickNormalsFlatRenderer}get pickDepthRenderer(){return this._pickDepthRenderer||(this._pickDepthRenderer=new Tu(this._scene)),this._pickDepthRenderer}get vertexDepthRenderer(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new _u(this._scene)),this._vertexDepthRenderer}get snapDepthBufInitRenderer(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new Mu(this._scene)),this._snapDepthBufInitRenderer}get occlusionRenderer(){return this._occlusionRenderer||(this._occlusionRenderer=new Uu(this._scene)),this._occlusionRenderer}_destroy(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}const th={};class sh{constructor(){this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]}}class nh{constructor(){this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}finalize(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}bindCommonTextures(e,t,s,n,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,s,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,n,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}bindPickCameraTexture(e,t){}bindTriangleIndicesTextures(e,t,s,n){this.indicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.indicesPerBitnessTextures[n].bindTexture(e,s,7)}bindEdgeIndicesTextures(e,t,s,n){this.edgeIndicesPortionIdsPerBitnessTextures[n].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[n].bindTexture(e,s,7)}}class ih{constructor(e,t,s,n,i=null){this._gl=e,this._texture=t,this._textureWidth=s,this._textureHeight=n,this._textureData=i}bindTexture(e,t,s){return e.bindTexture(t,this,s)}bind(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}unbind(e){}}const ah={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(ah,null,4));let e=0;Object.keys(ah).forEach((t=>{t.startsWith("size")&&(e+=ah[t])})),console.log(`Total size ${e} bytes (${(e/1e3/1e3).toFixed(2)} MB)`),console.log(`Avg bytes / triangle: ${(e/ah.totalPolygons).toFixed(2)}`);let t={};Object.keys(ah).forEach((s=>{s.startsWith("size")&&(t[s]=`${(ah[s]/e*100).toFixed(2)} % of total`)})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};class rh{disableBindedTextureFiltering(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}generateCameraDataTexture(e,t,s,n){const i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const a=new ih(e,i,4,3);let r=!0;a.updateViewMatrix=(s,i)=>{e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?O(s,n):s)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};const l=()=>{r&&(r=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(()=>r=!0)),s.on("rendering",l),l(),a}generatePickCameraDataTexture(e,t,s){const n=e.createTexture();e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);const i=new ih(e,n,4,3);return i.updateViewMatrix=(n,a)=>{e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(s?O(n,s):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}generateModelTexture(e,t){const s=e.createTexture();return e.bindTexture(e.TEXTURE_2D,s),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new ih(e,s,4,2)}generateTextureForColorsAndFlags(e,t,s,n,i,a,r){const l=t.length;this.numPortions=l;const o=4096,c=Math.ceil(l/512);if(0===c)throw"texture height===0";const u=new Uint8Array(16384*c);ah.sizeDataColorsAndFlags+=u.byteLength,ah.numberOfTextures++;for(let e=0;e>24&255,n[e]>>16&255,n[e]>>8&255,255&n[e]],32*e+16),u.set([i[e]>>24&255,i[e]>>16&255,i[e]>>8&255,255&i[e]],32*e+20),u.set([a[e]>>24&255,a[e]>>16&255,a[e]>>8&255,255&a[e]],32*e+24),u.set([r[e]?1:0,0,0,0],32*e+28);const h=e.createTexture();return e.bindTexture(e.TEXTURE_2D,h),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,o,c),e.texSubImage2D(e.TEXTURE_2D,0,0,0,o,c,e.RGBA_INTEGER,e.UNSIGNED_BYTE,u,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new ih(e,h,o,c,u)}generateTextureForObjectOffsets(e,t){const s=512,n=Math.ceil(t/s);if(0===n)throw"texture height===0";const i=new Float32Array(1536*n).fill(0);ah.sizeDataTextureOffsets+=i.byteLength,ah.numberOfTextures++;const a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,s,n),e.texSubImage2D(e.TEXTURE_2D,0,0,0,s,n,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new ih(e,a,s,n,i)}generateTextureForPositionsDecodeMatrices(e,t,s){const n=t.length;if(0===n)throw"num decode+entity matrices===0";const i=2048,a=Math.ceil(n/512),r=new Float32Array(8192*a);ah.sizeDataPositionDecodeMatrices+=r.byteLength,ah.numberOfTextures++;const l=p.mat4();for(let e=0;e{s._compile()})),e.on("destroyed",(()=>{delete th[t],s._destroy()}))),s}(e.scene),this.model=e,this._buffer=new sh,this._dataTextureState=new nh,this._dataTextureGenerator=new rh,this._state=new ze({origin:p.vec3(t.origin),metallicRoughnessBuf:null,positionsDecodeMatrix:p.mat4(),textureState:this._dataTextureState,numIndices8Bits:0,numIndices16Bits:0,numIndices32Bits:0,numEdgeIndices8Bits:0,numEdgeIndices16Bits:0,numEdgeIndices32Bits:0,numVertices:0}),this._numPortions=0,this._numVisibleLayerPortions=0,this._numTransparentLayerPortions=0,this._numXRayedLayerPortions=0,this._numSelectedLayerPortions=0,this._numHighlightedLayerPortions=0,this._numClippableLayerPortions=0,this._numEdgesLayerPortions=0,this._numPickableLayerPortions=0,this._numCulledLayerPortions=0,this._subPortions=[],this._portionToSubPortionsMap=[],this._bucketGeometries={},this.aabb=p.collapseAABB3(),this._numUpdatesInFrame=0,this._finalized=!1}canCreatePortion(e){if(this._finalized)throw"Already finalized";const t=e.buckets.length;this._numPortions+t>65536&&ah.cannotCreatePortion.because10BitsObjectId++;let s=this._numPortions+t<=65536;const n=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#0`:`${e.id}#0`;if(!this._bucketGeometries[n]){const t=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits);let n=0,i=0;e.buckets.forEach((e=>{n+=e.positionsCompressed.length/3,i+=e.indices.length/3})),(this._state.numVertices+n>16777216||t+i>16777216)&&ah.cannotCreatePortion.becauseTextureSize++,s&&=this._state.numVertices+n<=16777216&&t+i<=16777216}return s}createPortion(e){if(this._finalized)throw"Already finalized";const t=[],s=e.worldAABB;e.buckets.forEach(((n,i)=>{const a=void 0!==e.geometryId&&null!==e.geometryId?`${e.geometryId}#${i}`:`${e.id}#${i}`;let r=this._bucketGeometries[a];r||(r=this._createBucketGeometry(e,n),this._bucketGeometries[a]=r);const l=p.collapseAABB3(ph),o=this._createSubPortion(e,r,n,l);p.expandAABB3(s,l),t.push(o)}));const n=this._state.origin;0===n[0]&&0===n[1]&&0===n[2]||(s[0]+=n[0],s[1]+=n[1],s[2]+=n[2],s[3]+=n[0],s[4]+=n[1],s[5]+=n[2]),p.expandAABB3(this.aabb,s);const i=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(t),this.model.numPortions++,i}_createBucketGeometry(e,t){if(t.indices){const e=8*Math.ceil(t.indices.length/3/8)*3;ah.overheadSizeAlignementIndices+=2*(e-t.indices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.indices),t.indices=s}if(t.edgeIndices){const e=8*Math.ceil(t.edgeIndices.length/2/8)*2;ah.overheadSizeAlignementEdgeIndices+=2*(e-t.edgeIndices.length);const s=new Uint32Array(e);s.fill(0),s.set(t.edgeIndices),t.edgeIndices=s}const s=t.positionsCompressed,n=t.indices,i=t.edgeIndices,a=this._buffer,r=a.positionsCompressed.length/3,l=s.length/3;for(let e=0,t=s.length;e0){let e,s=3*t.numTriangles;t.numVertices<=256?(e=u.perTriangleNumberPortionId8Bits,h.numIndices8Bits+=s,ah.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(e=u.perTriangleNumberPortionId16Bits,h.numIndices16Bits+=s,ah.totalPolygons16Bits+=t.numTriangles):(e=u.perTriangleNumberPortionId32Bits,h.numIndices32Bits+=s,ah.totalPolygons32Bits+=t.numTriangles),ah.totalPolygons+=t.numTriangles;for(let s=0;s0){let e,s=2*t.numEdges;t.numVertices<=256?(e=u.perEdgeNumberPortionId8Bits,h.numEdgeIndices8Bits+=s,ah.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(e=u.perEdgeNumberPortionId16Bits,h.numEdgeIndices16Bits+=s,ah.totalEdges16Bits+=t.numEdges):(e=u.perEdgeNumberPortionId32Bits,h.numEdgeIndices32Bits+=s,ah.totalEdges32Bits+=t.numEdges),ah.totalEdges+=t.numEdges;for(let s=0;s0&&(t.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId8Bits)),n.perEdgeNumberPortionId16Bits.length>0&&(t.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId16Bits)),n.perEdgeNumberPortionId32Bits.length>0&&(t.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(s,n.perEdgeNumberPortionId32Bits)),n.indices8Bits.length>0&&(t.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(s,n.indices8Bits)),n.indices16Bits.length>0&&(t.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(s,n.indices16Bits)),n.indices32Bits.length>0&&(t.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(s,n.indices32Bits)),n.edgeIndices8Bits.length>0&&(t.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(s,n.edgeIndices8Bits)),n.edgeIndices16Bits.length>0&&(t.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(s,n.edgeIndices16Bits)),n.edgeIndices32Bits.length>0&&(t.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(s,n.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(s,this.model)),t.textureModelMatrices=this.model._modelMatricesTexture,t.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),t.textureCameraMatrices=t.cameraTexture,t.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),t.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(()=>{this._deferredSetFlagsDirty&&this._uploadDeferredFlags(),this._numUpdatesInFrame=0}))}isEmpty(){return 0===this._numPortions}initFlags(e,t,s){t&M&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&j&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&V&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&k&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&U&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&Q&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&H&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&F&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),s&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,s,true),this._setFlags2(e,t,true)}flushInitFlags(){this._setDeferredFlags(),this._setDeferredFlags2()}setVisible(e,t,s){if(!this._finalized)throw"Not finalized";t&M?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,s)}setHighlighted(e,t,s){if(!this._finalized)throw"Not finalized";t&j?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,s)}setXRayed(e,t,s){if(!this._finalized)throw"Not finalized";t&V?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,s)}setSelected(e,t,s){if(!this._finalized)throw"Not finalized";t&k?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,s)}setEdges(e,t,s){if(!this._finalized)throw"Not finalized";t&Q?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,s)}setClippable(e,t){if(!this._finalized)throw"Not finalized";t&U?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}_beginDeferredFlags(){this._deferredSetFlagsActive=!0}_uploadDeferredFlags(){if(this._deferredSetFlagsActive=!1,!this._deferredSetFlagsDirty)return;this._deferredSetFlagsDirty=!1;const e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}setCulled(e,t,s){if(!this._finalized)throw"Not finalized";t&F?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,s)}setCollidable(e,t){if(!this._finalized)throw"Not finalized"}setPickable(e,t,s){if(!this._finalized)throw"Not finalized";t&H?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,s)}setColor(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdColorsAndFlags._texture),n.texSubImage2D(n.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,n.RGBA_INTEGER,n.UNSIGNED_BYTE,uh))}setTransparent(e,t,s){s?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,s)}_setFlags(e,t,s,n=!1){const i=this._portionToSubPortionsMap[e];for(let e=0,a=i.length;e=10&&this._beginDeferredFlags(),d.bindTexture(d.TEXTURE_2D,A.texturePerObjectIdColorsAndFlags._texture),d.texSubImage2D(d.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,d.RGBA_INTEGER,d.UNSIGNED_BYTE,uh))}_setDeferredFlags(){}_setFlags2(e,t,s=!1){const n=this._portionToSubPortionsMap[e];for(let e=0,i=n.length;e=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,uh))}_setDeferredFlags2(){}setOffset(e,t){const s=this._portionToSubPortionsMap[e];for(let e=0,n=s.length;e=10&&this._beginDeferredFlags(),n.bindTexture(n.TEXTURE_2D,s.texturePerObjectIdOffsets._texture),n.texSubImage2D(n.TEXTURE_2D,0,0,e,1,1,n.RGB,n.FLOAT,hh))}drawColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,cr.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}_updateBackfaceCull(e,t){const s=this.model.backfaces||e.sectioned;if(t.backfaces!==s){const e=t.gl;s?e.disable(e.CULL_FACE):e.enable(e.CULL_FACE),t.backfaces=s}}drawColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,cr.COLOR_TRANSPARENT))}drawDepth(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawSilhouetteXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_XRAYED))}drawSilhouetteHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_HIGHLIGHTED))}drawSilhouetteSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,cr.SILHOUETTE_SELECTED))}drawEdgesColorOpaque(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,cr.EDGES_COLOR_OPAQUE)}drawEdgesColorTransparent(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,cr.EDGES_COLOR_TRANSPARENT)}drawEdgesHighlighted(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_HIGHLIGHTED)}drawEdgesSelected(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_SELECTED)}drawEdgesXRayed(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,cr.EDGES_XRAYED)}drawOcclusion(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}drawShadow(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,cr.COLOR_OPAQUE))}setPickMatrices(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}drawPickMesh(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,cr.PICK))}drawPickDepths(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,cr.PICK))}drawSnapInitDepthBuf(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,cr.PICK))}drawSnapDepths(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,cr.PICK))}drawPickNormals(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,cr.PICK))}destroy(){if(this._destroyed)return;const e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}const Ih=p.vec3();p.mat4();const yh=p.vec3([1,1,1]),mh=p.vec3([0,0,0]),vh=p.vec3([0,0,0]),wh=p.identityQuaternion(),gh=p.identityMat4(),Th=new Uint8Array([255,255,255]);class Eh extends _{constructor(e,t={}){super(e,t),this._dtxEnabled=this.scene.dtxEnabled&&!1!==t.dtxEnabled,this._enableVertexWelding=!1,this._enableIndexBucketing=!0,this._vboBatchingLayerScratchMemory=(or++,lr),this._textureTranscoder=t.textureTranscoder||Oc(this.scene.viewer),this._maxGeometryBatchSize=t.maxGeometryBatchSize,this._aabb=p.collapseAABB3(),this._aabbDirty=!0,this._quantizationRanges={},this._vboInstancingLayers={},this._vboBatchingLayers={},this._dtxLayers={},this._meshList=[],this.layerList=[],this._entityList=[],this._geometries={},this._dtxBuckets={},this._textures={},this._textureSets={},this._meshes={},this._entities={},this._scheduledMeshes={},this.renderFlags=new ci,this.numGeometries=0,this.numPortions=0,this.numVisibleLayerPortions=0,this.numTransparentLayerPortions=0,this.numXRayedLayerPortions=0,this.numHighlightedLayerPortions=0,this.numSelectedLayerPortions=0,this.numEdgesLayerPortions=0,this.numPickableLayerPortions=0,this.numClippableLayerPortions=0,this.numCulledLayerPortions=0,this.numEntities=0,this._numTriangles=0,this._numLines=0,this._numPoints=0,this._edgeThreshold=t.edgeThreshold||10,this._origin=p.vec3(t.origin||[0,0,0]),this._position=p.vec3(t.position||[0,0,0]),this._rotation=p.vec3(t.rotation||[0,0,0]),this._quaternion=p.vec4(t.quaternion||[0,0,0,1]),this._conjugateQuaternion=p.vec4(t.quaternion||[0,0,0,1]),t.rotation&&p.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._scale=p.vec3(t.scale||[1,1,1]),this._worldRotationMatrix=p.mat4(),this._worldRotationMatrixConjugate=p.mat4(),this._matrix=p.mat4(),this._matrixDirty=!0,this._rebuildMatrices(),this._worldNormalMatrix=p.mat4(),p.inverseMat4(this._matrix,this._worldNormalMatrix),p.transposeMat4(this._worldNormalMatrix),(t.matrix||t.position||t.rotation||t.scale||t.quaternion)&&(this._viewMatrix=p.mat4(),this._viewNormalMatrix=p.mat4(),this._viewMatrixDirty=!0,this._matrixNonIdentity=!0),this._opacity=1,this._colorize=[1,1,1],this._saoEnabled=!1!==t.saoEnabled,this._pbrEnabled=!1!==t.pbrEnabled,this._colorTextureEnabled=!1!==t.colorTextureEnabled,this._isModel=t.isModel,this._isModel&&this.scene._registerModel(this),this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewMatrixDirty=!0})),this.scene.vfc.enabled&&(this._vfcManager=this.scene.vfc.getVFCManager(this)),this._createDefaultTextureSet(),this.visible=t.visible,this.culled=t.culled,this.pickable=t.pickable,this.clippable=t.clippable,this.collidable=t.collidable,this.castsShadow=t.castsShadow,this.receivesShadow=t.receivesShadow,this.xrayed=t.xrayed,this.highlighted=t.highlighted,this.selected=t.selected,this.edges=t.edges,this.colorize=t.colorize,this.opacity=t.opacity,this.backfaces=t.backfaces}_createDefaultTextureSet(){const e=new wc({id:"defaultColorTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new wc({id:"defaultMetalRoughTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),s=new wc({id:"defaultNormalsTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),n=new wc({id:"defaultEmissiveTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new wc({id:"defaultOcclusionTexture",texture:new Ki({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=s,this._textures.defaultEmissiveTexture=n,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new vc({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:s,emissiveTexture:n,occlusionTexture:i})}get isPerformanceModel(){return!0}get objects(){return this._entities}get origin(){return this._origin}set position(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get position(){return this._position}set rotation(e){this._rotation.set(e||[0,0,0]),p.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get rotation(){return this._rotation}set quaternion(e){this._quaternion.set(e||[0,0,0,1]),p.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get quaternion(){return this._quaternion}set scale(e){}get scale(){return this._scale}set matrix(e){this._matrix.set(e||gh),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),p.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),p.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}get matrix(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix}get rotationMatrix(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}_rebuildMatrices(){this._matrixDirty&&(p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),p.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),p.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),p.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}get rotationMatrixConjugate(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}_setWorldMatrixDirty(){this._matrixDirty=!0}_setLocalAABBDirty(){for(let e=0,t=this._entityList.length;e0}set visible(e){e=!1!==e,this._visible=e;for(let t=0,s=this._entityList.length;t0}set xrayed(e){e=!!e,this._xrayed=e;for(let t=0,s=this._entityList.length;t0}set highlighted(e){e=!!e,this._highlighted=e;for(let t=0,s=this._entityList.length;t0}set selected(e){e=!!e,this._selected=e;for(let t=0,s=this._entityList.length;t0}set edges(e){e=!!e,this._edges=e;for(let t=0,s=this._entityList.length;t0}set pickable(e){e=!1!==e,this._pickable=e;for(let t=0,s=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){const t=e.colors,s=new Uint8Array(t.length);for(let e=0,n=t.length;e{o.setImage(c,{minFilter:s,magFilter:n,wrapS:i,wrapT:a,wrapR:r,flipY:e.flipY,encoding:l}),this.glRedraw()},c.src=e.src;break;default:this._textureTranscoder?m.loadArraybuffer(e.src,(e=>{e.byteLength?this._textureTranscoder.transcode([e],o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'src': file data is zero length")}),(function(e){this.error(`[createTexture] Can't create texture from 'src': ${e}`)})):this.error(`[createTexture] Can't create texture from 'src' - SceneModel needs to be configured with a TextureTranscoder for this file type ('${t}')`)}}else e.buffers&&(this._textureTranscoder?this._textureTranscoder.transcode(e.buffers,o).then((()=>{this.glRedraw()})):this.error("[createTexture] Can't create texture from 'buffers' - SceneModel needs to be configured with a TextureTranscoder for this option"));this._textures[t]=new wc({id:t,texture:o})}createTextureSet(e){const t=e.id;if(null==t)return void this.error("[createTextureSet] Config missing: id");if(this._textureSets[t])return void this.error(`[createTextureSet] Texture set already created: ${t}`);let s,n,i,a,r;if(void 0!==e.colorTextureId&&null!==e.colorTextureId){if(s=this._textures[e.colorTextureId],!s)return void this.error(`[createTextureSet] Texture not found: ${e.colorTextureId} - ensure that you create it first with createTexture()`)}else s=this._textures.defaultColorTexture;if(void 0!==e.metallicRoughnessTextureId&&null!==e.metallicRoughnessTextureId){if(n=this._textures[e.metallicRoughnessTextureId],!n)return void this.error(`[createTextureSet] Texture not found: ${e.metallicRoughnessTextureId} - ensure that you create it first with createTexture()`)}else n=this._textures.defaultMetalRoughTexture;if(void 0!==e.normalsTextureId&&null!==e.normalsTextureId){if(i=this._textures[e.normalsTextureId],!i)return void this.error(`[createTextureSet] Texture not found: ${e.normalsTextureId} - ensure that you create it first with createTexture()`)}else i=this._textures.defaultNormalsTexture;if(void 0!==e.emissiveTextureId&&null!==e.emissiveTextureId){if(a=this._textures[e.emissiveTextureId],!a)return void this.error(`[createTextureSet] Texture not found: ${e.emissiveTextureId} - ensure that you create it first with createTexture()`)}else a=this._textures.defaultEmissiveTexture;if(void 0!==e.occlusionTextureId&&null!==e.occlusionTextureId){if(r=this._textures[e.occlusionTextureId],!r)return void this.error(`[createTextureSet] Texture not found: ${e.occlusionTextureId} - ensure that you create it first with createTexture()`)}else r=this._textures.defaultOcclusionTexture;const l=new vc({id:t,model:this,colorTexture:s,metallicRoughnessTexture:n,normalsTexture:i,emissiveTexture:a,occlusionTexture:r});this._textureSets[t]=l}createMesh(e){if(void 0===e.id||null===e.id)return void this.error("[createMesh] SceneModel.createMesh() config missing: id");if(this._scheduledMeshes[e.id])return void this.error(`[createMesh] SceneModel already has a mesh with this ID: ${e.id}`);const t=!(void 0!==e.geometryId);if(e.sceneModelMatrix=this._matrixNonIdentity?this._matrix:null,t){const t=!!this._dtxEnabled;if(void 0!==e.primitive&&null!==e.primitive||(e.primitive="triangles"),"points"!==e.primitive&&"lines"!==e.primitive&&"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive)return void this.error(`Unsupported value for 'primitive': '${primitive}' ('geometryId' is absent) - supported values are 'points', 'lines', 'triangles', 'solid' and 'surface'.`);if(!e.positions&&!e.positionsCompressed&&!e.buckets)return this.error("Param expected: 'positions', 'positionsCompressed' or `buckets` ('geometryId' is absent)"),null;if(e.positions&&(e.positionsDecodeMatrix||e.positionsDecodeBoundary))return this.error("Illegal params: 'positions' not expected with 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.positionsCompressed&&!e.positionsDecodeMatrix&&!e.positionsDecodeBoundary)return this.error("Param expected: 'positionsCompressed' should be accompanied by 'positionsDecodeMatrix'/'positionsDecodeBoundary' ('geometryId' is absent)"),null;if(e.uvCompressed&&!e.uvDecodeMatrix)return this.error("Param expected: 'uvCompressed' should be accompanied by `uvDecodeMatrix` ('geometryId' is absent)"),null;if(!e.buckets&&!e.indices&&"points"!==e.primitive)return this.error(`Param expected: indices (required for '${e.primitive}' primitive type)`),null;if((e.matrix||e.position||e.rotation||e.scale)&&(e.positionsCompressed||e.positionsDecodeBoundary))return this.error("Unexpected params: 'matrix', 'rotation', 'scale', 'position' not allowed with 'positionsCompressed'"),null;if(e.origin=e.origin?p.addVec3(this._origin,e.origin,p.vec3()):this._origin,e.matrix)e.meshMatrix=e.matrix;else if(e.scale||e.rotation||e.position){const t=e.scale||yh,s=e.position||mh,n=e.rotation||vh;p.eulerToQuaternion(n,"XYZ",wh),e.meshMatrix=p.composeMat4(s,wh,t,p.mat4())}if(e.positionsDecodeBoundary&&(e.positionsDecodeMatrix=Kr(e.positionsDecodeBoundary,p.mat4())),t){if(e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.positions){const t=p.vec3(),s=[];N(e.positions,s,t)&&(e.positions=s,e.origin=p.addVec3(e.origin,t,t))}if(e.positions){const t=p.collapseAABB3();e.positionsDecodeMatrix=p.mat4(),p.expandAABB3Points3(t,e.positions),e.positionsCompressed=zr(e.positions,t,e.positionsDecodeMatrix)}e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=mt(e.positions,e.indices,null,2):e.edgeIndices=mt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.buckets||(e.buckets=bh(e,this._enableVertexWelding&&this._enableIndexBucketing))}else{if(e.type=1,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):[255,255,255],e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.positions){const t=[];N(e.positions,t,Ih)&&(e.positions=t,e.origin=p.addVec3(e.origin,Ih,p.vec3()))}if(e.buckets||e.edgeIndices||"triangles"!==e.primitive&&"solid"!==e.primitive&&"surface"!==e.primitive||(e.positions?e.edgeIndices=mt(e.positions,e.indices,null,2):e.edgeIndices=mt(e.positionsCompressed,e.indices,e.positionsDecodeMatrix,2)),e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId],!e.textureSet))return void this.error(`[createMesh] Texture set not found: ${e.textureSetId} - ensure that you create it first with createTextureSet()`)}}else{if(e.positions||e.positionsCompressed||e.indices||e.edgeIndices||e.normals||e.normalsCompressed||e.uv||e.uvCompressed||e.positionsDecodeMatrix)return void this.error("Mesh geometry parameters not expected when instancing a geometry (not expected: positions, positionsCompressed, indices, edgeIndices, normals, normalsCompressed, uv, uvCompressed, positionsDecodeMatrix)");if(e.geometry=this._geometries[e.geometryId],!e.geometry)return void this.error(`[createMesh] Geometry not found: ${e.geometryId} - ensure that you create it first with createGeometry()`);if(e.origin=e.origin?p.addVec3(this._origin,e.origin,p.vec3()):this._origin,e.positionsDecodeMatrix=e.geometry.positionsDecodeMatrix,e.matrix)e.meshMatrix=e.matrix.slice();else{const t=e.scale||yh,s=e.position||mh,n=e.rotation||vh;p.eulerToQuaternion(n,"XYZ",wh),e.meshMatrix=p.composeMat4(s,wh,t,p.mat4())}if(!!this._dtxEnabled){e.type=2,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255;let t=this._dtxBuckets[e.geometryId];t||(t=bh(e.geometry,this._enableVertexWelding,this._enableIndexBucketing),this._dtxBuckets[e.geometryId]=t),e.buckets=t}else e.type=0,e.color=e.color?new Uint8Array([Math.floor(255*e.color[0]),Math.floor(255*e.color[1]),Math.floor(255*e.color[2])]):Th,e.opacity=void 0!==e.opacity&&null!==e.opacity?Math.floor(255*e.opacity):255,e.metallic=void 0!==e.metallic&&null!==e.metallic?Math.floor(255*e.metallic):0,e.roughness=void 0!==e.roughness&&null!==e.roughness?Math.floor(255*e.roughness):255,e.textureSetId&&(e.textureSet=this._textureSets[e.textureSetId]),function(e){if(e.obb=p.OBB3(),e.positionsCompressed&&e.positionsCompressed.length>0){const t=p.collapseAABB3();p.expandAABB3Points3(t,e.positionsCompressed),Dt.decompressAABB(t,e.positionsDecodeMatrix),p.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){const t=p.collapseAABB3();p.expandAABB3Points3(t,e.positions),p.AABB3ToOBB3(t,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}_createMesh(e){const t=new rr(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);const s=t.pickId,n=s>>24&255,i=s>>16&255,a=s>>8&255,r=255&s;switch(e.pickColor=new Uint8Array([r,a,i,n]),e.worldAABB=p.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=p.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,p.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}_getNumPrimitives(e){let t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(let s=0,n=e.buckets.length;s>>0).toString(16)}_getVBOInstancingLayer(e){const t=this,s=e.origin,n=e.textureSetId||"-",i=e.geometryId,a=`${Math.round(s[0])}.${Math.round(s[1])}.${Math.round(s[2])}.${n}.${i}`;let r=this._vboInstancingLayers[a];if(r)return r;let l=e.textureSet;const o=e.geometry;for(;!r;)switch(o.primitive){case"triangles":case"surface":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new wo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!1});break;case"solid":console.log(`[SceneModel ${this.id}]: creating TrianglesInstancingLayer`),r=new wo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0,solid:!0});break;case"lines":console.log(`[SceneModel ${this.id}]: creating LinesInstancingLayer`),r=new Vo({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0});break;case"points":console.log(`[SceneModel ${this.id}]: creating PointsInstancingLayer`),r=new mc({model:t,textureSet:l,geometry:o,origin:s,layerIndex:0})}return this._vboInstancingLayers[a]=r,this.layerList.push(r),r}createEntity(e){if(void 0===e.id?e.id=p.createUUID():this.scene.components[e.id]&&(this.error(`Scene already has a Component with this ID: ${e.id} - will assign random ID`),e.id=p.createUUID()),void 0===e.meshIds)return void this.error("Config missing: meshIds");let t=0;if(this._visible&&!1!==e.visible&&(t|=M),this._pickable&&!1!==e.pickable&&(t|=H),this._culled&&!1!==e.culled&&(t|=F),this._clippable&&!1!==e.clippable&&(t|=U),this._collidable&&!1!==e.collidable&&(t|=G),this._edges&&!1!==e.edges&&(t|=Q),this._xrayed&&!1!==e.xrayed&&(t|=V),this._highlighted&&!1!==e.highlighted&&(t|=j),this._selected&&!1!==e.selected&&(t|=k),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(let t=0,s=e.meshIds.length;t{}));for(let e=0,t=this.layerList.length;ee.sortIdt.sortId?1:0));for(let e=0,t=this.layerList.length;e0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}_updateRenderFlagsVisibleLayers(){const e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(let t=0,s=this.layerList.length;t0)for(let e=0;e0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){const t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0){this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0))}if(this.numSelectedLayerPortions>0){const t=this.scene.selectedMaterial._state;t.fill&&(t.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){const t=this.scene.highlightMaterial._state;t.fill&&(t.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}drawColorOpaque(e){const t=this.renderFlags;for(let s=0,n=t.visibleLayers.length;s65536?16:8)}else r=[{positionsCompressed:n,indices:i,edgeIndices:a}];return r}class Dh extends Eh{constructor(e,t={}){super(e,t)}}class Ph extends _{constructor(e,t={}){super(e,t),this._skyboxMesh=new wi(this,{geometry:new Ct(this,{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new Nt(this,{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new $i(this,{src:t.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:t.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),this.size=t.size,this.active=t.active}set size(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}get size(){return this._size}set active(e){this._skyboxMesh.visible=e}get active(){return this._skyboxMesh.visible}}class Rh{transcode(e,t,s={}){}destroy(){}}const Ch=p.vec4(),_h=p.vec4(),Bh=p.vec3(),Oh=p.vec3(),Sh=p.vec3(),Nh=p.vec4(),xh=p.vec4(),Lh=p.vec4();class Mh{constructor(e){this._scene=e}dollyToCanvasPos(e,t,s){let n=!1;const i=this._scene.camera;if(e){const t=p.subVec3(e,i.eye,Bh);n=p.lenVec3(t){this._cameraDirty=!0})),this._onProjMatrix=this._scene.camera.on("projMatrix",(()=>{this._cameraDirty=!0})),this._onTick=this._scene.on("tick",(()=>{this.updatePivotElement()}))}updatePivotElement(){const e=this._scene.camera,t=this._scene.canvas;if(this._pivoting&&this._cameraDirty){p.transformPoint3(e.viewMatrix,this.getPivotPos(),this._pivotViewPos),this._pivotViewPos[3]=1,p.transformPoint4(e.projMatrix,this._pivotViewPos,this._pivotProjPos);const s=t.boundary,n=s[2],i=s[3];this._pivotCanvasPos[0]=Math.floor((1+this._pivotProjPos[0]/this._pivotProjPos[3])*n/2),this._pivotCanvasPos[1]=Math.floor((1-this._pivotProjPos[1]/this._pivotProjPos[3])*i/2);let a=t._lastBoundingClientRect;if(!a||t._canvasSizeChanged){const e=t.canvas;a=t._lastBoundingClientRect=e.getBoundingClientRect()}this._pivotElement&&(this._pivotElement.style.left=Math.floor(a.left+this._pivotCanvasPos[0])-this._pivotElement.clientWidth/2+window.scrollX+"px",this._pivotElement.style.top=Math.floor(a.top+this._pivotCanvasPos[1])-this._pivotElement.clientHeight/2+window.scrollY+"px"),this._cameraDirty=!1}}setPivotElement(e){this._pivotElement=e}startPivot(){if(this._cameraLookingDownwards())return this._pivoting=!1,!1;const e=this._scene.camera;let t=p.lookAtMat4v(e.eye,e.look,e.worldUp);p.transformPoint3(t,this.getPivotPos(),this._cameraOffset);const s=this.getPivotPos();this._cameraOffset[2]+=p.distVec3(e.eye,s),t=p.inverseMat4(t);const n=p.transformVec3(t,this._cameraOffset),i=p.vec3();if(p.subVec3(e.eye,s,i),p.addVec3(i,n),e.zUp){const e=i[1];i[1]=i[2],i[2]=e}this._radius=p.lenVec3(i),this._polar=Math.acos(i[1]/this._radius),this._azimuth=Math.atan2(i[0],i[2]),this._pivoting=!0}_cameraLookingDownwards(){const e=this._scene.camera,t=p.normalizeVec3(p.subVec3(e.look,e.eye,Fh)),s=p.cross3Vec3(t,e.worldUp,Hh);return p.sqLenVec3(s)<=1e-4}getPivoting(){return this._pivoting}setPivotPos(e){this._pivotWorldPos.set(e),this._pivotPosSet=!0}setCanvasPivotPos(e){const t=this._scene.camera,s=Math.abs(p.distVec3(this._scene.center,t.eye)),n=t.project.transposedMatrix,i=n.subarray(8,12),a=n.subarray(12),r=[0,0,-1,1],l=p.dotVec4(r,i)/p.dotVec4(r,a),o=Gh;t.project.unproject(e,l,Vh,jh,o);const c=p.normalizeVec3(p.subVec3(o,t.eye,Fh)),u=p.addVec3(t.eye,p.mulVec3Scalar(c,s,Hh),Uh);this.setPivotPos(u)}getPivotPos(){return this._pivotPosSet?this._pivotWorldPos:this._scene.camera.look}continuePivot(e,t){if(!this._pivoting)return;if(0===e&&0===t)return;const s=this._scene.camera;var n=-e;const i=-t;1===s.worldUp[2]&&(n=-n),this._azimuth+=.01*-n,this._polar+=.01*i,this._polar=p.clamp(this._polar,.001,Math.PI-.001);const a=[this._radius*Math.sin(this._polar)*Math.sin(this._azimuth),this._radius*Math.cos(this._polar),this._radius*Math.sin(this._polar)*Math.cos(this._azimuth)];if(1===s.worldUp[2]){const e=a[1];a[1]=a[2],a[2]=e}const r=p.lenVec3(p.subVec3(s.look,s.eye,p.vec3())),l=this.getPivotPos();p.addVec3(a,l);let o=p.lookAtMat4v(a,l,s.worldUp);o=p.inverseMat4(o);const c=p.transformVec3(o,this._cameraOffset);o[12]-=c[0],o[13]-=c[1],o[14]-=c[2];const u=[o[8],o[9],o[10]];s.eye=[o[12],o[13],o[14]],p.subVec3(s.eye,p.mulVec3Scalar(u,r),s.look),s.up=[o[4],o[5],o[6]],this.showPivot()}showPivot(){this._shown||(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this.updatePivotElement(),this._pivotElement.style.visibility="visible",this._shown=!0,this._hideTimeout=window.setTimeout((()=>{this.hidePivot()}),1e3)))}hidePivot(){this._shown&&(null!==this._hideTimeout&&(window.clearTimeout(this._hideTimeout),this._hideTimeout=null),this._pivotElement&&(this._pivotElement.style.visibility="hidden"),this._shown=!1)}endPivot(){this._pivoting=!1}destroy(){this._scene.camera.off(this._onViewMatrix),this._scene.camera.off(this._onProjMatrix),this._scene.off(this._onTick)}}class Qh{constructor(e,t){this._scene=e.scene,this._cameraControl=e,this._scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},this._configs=t,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick=!1,this.pickCursorPos=p.vec2(),this.picked=!1,this.pickedSurface=!1,this.pickResult=null,this._lastPickedEntityId=null,this._needFireEvents=!1}update(){if(!this._configs.pointerEnabled)return;if(!this.schedulePickEntity&&!this.schedulePickSurface)return;this.picked=!1,this.pickedSurface=!1,this.snappedOrPicked=!1,this.hoveredSnappedOrSurfaceOff=!1,this._needFireEvents=!1;const e=this._cameraControl.hasSubs("hoverSurface");if(this.scheduleSnapOrPick){const e=this._scene.snapPick({canvasPos:this.pickCursorPos,snapRadius:this._configs.snapRadius,snapMode:this._configs.snapMode});e&&e.snappedWorldPos?(this.snapPickResult=e,this.snappedOrPicked=!0,this._needFireEvents=!0):(this.schedulePickSurface=!0,this.snapPickResult=null)}if(this.schedulePickSurface&&this.pickResult&&this.pickResult.worldPos){const t=this.pickResult.canvasPos;if(t[0]===this.pickCursorPos[0]&&t[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!0,this._needFireEvents=e,this.schedulePickEntity=!1,this.schedulePickSurface=!1,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.hoveredSnappedOrSurfaceOff=!0,void(this.scheduleSnapOrPick=!1)}if(this.schedulePickEntity&&this.pickResult&&(this.pickResult.canvasPos||this.pickResult.snappedCanvasPos)){const e=this.pickResult.canvasPos||this.pickResult.snappedCanvasPos;if(e[0]===this.pickCursorPos[0]&&e[1]===this.pickCursorPos[1])return this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!1,this.schedulePickEntity=!1,void(this.schedulePickSurface=!1)}this.schedulePickSurface||this.scheduleSnapOrPick&&!this.snapPickResult?(this.pickResult=this._scene.pick({pickSurface:!0,pickSurfaceNormal:!1,canvasPos:this.pickCursorPos}),this.pickResult?(this.picked=!0,this.scheduleSnapOrPick?this.snappedOrPicked=!0:this.pickedSurface=!0,this._needFireEvents=!0):this.scheduleSnapOrPick&&(this.hoveredSnappedOrSurfaceOff=!0,this._needFireEvents=!0)):(this.pickResult=this._scene.pick({canvasPos:this.pickCursorPos}),this.pickResult&&(this.picked=!0,this.pickedSurface=!1,this._needFireEvents=!0)),this.scheduleSnapOrPick=!1,this.schedulePickEntity=!1,this.schedulePickSurface=!1}fireEvents(){if(this._needFireEvents){if(this.hoveredSnappedOrSurfaceOff&&this._cameraControl.fire("hoverSnapOrSurfaceOff",{canvasPos:this.pickCursorPos},!0),this.snappedOrPicked)if(this.snapPickResult){const e=new me;e.worldPos=this.snapPickResult.snappedWorldPos,e.canvasPos=this.snapPickResult.snappedCanvasPos,this._cameraControl.fire("hoverSnapOrSurface",e,!0),this.snapPickResult=null}else this._cameraControl.fire("hoverSnapOrSurface",this.pickResult,!0);if(this.picked&&this.pickResult&&(this.pickResult.entity||this.pickResult.worldPos)){if(this.pickResult.entity){const e=this.pickResult.entity.id;this._lastPickedEntityId!==e&&(void 0!==this._lastPickedEntityId&&this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._cameraControl.fire("hoverEnter",this.pickResult,!0),this._lastPickedEntityId=e)}this._cameraControl.fire("hover",this.pickResult,!0),this.pickResult.worldPos&&(this.pickedSurface=!0,this._cameraControl.fire("hoverSurface",this.pickResult,!0))}else void 0!==this._lastPickedEntityId&&(this._cameraControl.fire("hoverOut",{entity:this._scene.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),this._cameraControl.fire("hoverOff",{canvasPos:this.pickCursorPos},!0);this.pickResult=null,this._needFireEvents=!1}}destroy(){}}const Wh=p.vec2();class zh{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController;let r,l,o,c=0,u=0,h=0,A=0,d=!1;const f=p.vec3();let I=!0;const y=this._scene.canvas.canvas,m=[];function v(e=!0){y.style.cursor="move",c=n.pointerCanvasPos[0],u=n.pointerCanvasPos[1],h=n.pointerCanvasPos[0],A=n.pointerCanvasPos[1],e&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(d=!0,f.set(a.pickResult.worldPos)):d=!1)}document.addEventListener("keydown",this._documentKeyDownHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!0}),document.addEventListener("keyup",this._documentKeyUpHandler=t=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;const n=t.keyCode;m[n]=!1}),y.addEventListener("mousedown",this._mouseDownHandler=t=>{if(s.active&&s.pointerEnabled)switch(t.which){case 1:m[e.input.KEY_SHIFT]||s.planView?(r=!0,v()):(r=!0,v(!1));break;case 2:l=!0,v();break;case 3:o=!0,s.panRightClick&&v()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=()=>{if(!s.active||!s.pointerEnabled)return;if(!r&&!l&&!o)return;const t=e.canvas.boundary,a=t[2],h=t[3],A=n.pointerCanvasPos[0],I=n.pointerCanvasPos[1];if(m[e.input.KEY_SHIFT]||s.planView||!s.panRightClick&&l||s.panRightClick&&o){const t=A-c,s=I-u,n=e.camera;if("perspective"===n.projection){const a=Math.abs(d?p.lenVec3(p.subVec3(f,e.camera.eye,[])):e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=1.5*t*a/h,i.panDeltaY+=1.5*s*a/h}else i.panDeltaX+=.5*n.ortho.scale*(t/h),i.panDeltaY+=.5*n.ortho.scale*(s/h)}else!r||l||o||s.planView||(s.firstPerson?(i.rotateDeltaY-=(A-c)/a*s.dragRotationRate/2,i.rotateDeltaX+=(I-u)/h*(s.dragRotationRate/4)):(i.rotateDeltaY-=(A-c)/a*(1.5*s.dragRotationRate),i.rotateDeltaX+=(I-u)/h*(1.5*s.dragRotationRate)));c=A,u=I}),y.addEventListener("mousemove",this._canvasMouseMoveHandler=e=>{s.active&&s.pointerEnabled&&n.mouseover&&(I=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{if(s.active&&s.pointerEnabled)switch(e.which){case 1:case 2:case 3:r=!1,l=!1,o=!1}}),y.addEventListener("mouseup",this._mouseUpHandler=e=>{if(s.active&&s.pointerEnabled){if(3===e.which){!function(e,t){if(e){let s=e.target,n=0,i=0,a=0,r=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,a+=s.scrollLeft,r+=s.scrollTop,s=s.offsetParent;t[0]=e.pageX+a-n,t[1]=e.pageY+r-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,Wh);const s=Wh[0],n=Wh[1];Math.abs(s-h)<3&&Math.abs(n-A)<3&&t.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:Wh,event:e},!0)}y.style.removeProperty("cursor")}}),y.addEventListener("mouseenter",this._mouseEnterHandler=()=>{s.active&&s.pointerEnabled});const w=1/60;let g=null;y.addEventListener("wheel",this._mouseWheelHandler=e=>{if(!s.active||!s.pointerEnabled)return;const t=performance.now()/1e3;var a=null!==g?t-g:0;g=t,a>.05&&(a=.05),a{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const r=i._isKeyDownForAction(i.AXIS_VIEW_RIGHT),l=i._isKeyDownForAction(i.AXIS_VIEW_BACK),o=i._isKeyDownForAction(i.AXIS_VIEW_LEFT),c=i._isKeyDownForAction(i.AXIS_VIEW_FRONT),u=i._isKeyDownForAction(i.AXIS_VIEW_TOP),h=i._isKeyDownForAction(i.AXIS_VIEW_BOTTOM);if(!(r||l||o||c||u||h))return;const A=e.aabb,d=p.getAABB3Diag(A);p.getAABB3Center(A,Kh);const f=Math.abs(d/Math.tan(t.cameraFlight.fitFOV*p.DEGTORAD)),I=1.1*d;Zh.orthoScale=I,r?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldRight,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):l?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldForward,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):o?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldRight,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):c?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldForward,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(a.worldUp)):u?(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldUp,f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(p.normalizeVec3(p.mulVec3Scalar(a.worldForward,1,Xh),qh))):h&&(Zh.eye.set(p.addVec3(Kh,p.mulVec3Scalar(a.worldUp,-f,Yh),Jh)),Zh.look.set(Kh),Zh.up.set(p.normalizeVec3(p.mulVec3Scalar(a.worldForward,-1,Xh)))),!s.firstPerson&&s.followPointer&&t.pivotController.setPivotPos(Kh),t.cameraFlight.duration>0?t.cameraFlight.flyTo(Zh,(()=>{t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot()})):(t.cameraFlight.jumpTo(Zh),t.pivotController.getPivoting()&&s.followPointer&&t.pivotController.showPivot())}))}reset(){}destroy(){this._scene.input.off(this._onSceneKeyDown)}}class ep{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=t.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;let o=!1,c=!1;const u=this._scene.canvas.canvas,h=s=>{let n;s&&s.worldPos&&(n=s.worldPos);const i=s&&s.entity?s.entity.aabb:e.aabb;if(n){const s=e.camera;p.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};u.addEventListener("mousemove",this._canvasMouseMoveHandler=t=>{if(!s.active||!s.pointerEnabled)return;if(o||c)return;const i=l.hasSubs("hover"),r=l.hasSubs("hoverOut"),u=l.hasSubs("hoverOff"),h=l.hasSubs("hoverSurface"),p=l.hasSubs("hoverSnapOrSurface");if(i||r||u||h||p)if(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=h,a.scheduleSnapOrPick=p,a.update(),a.pickResult){if(a.pickResult.entity){const t=a.pickResult.entity.id;this._lastPickedEntityId!==t&&(void 0!==this._lastPickedEntityId&&l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),l.fire("hoverEnter",a.pickResult,!0),this._lastPickedEntityId=t)}l.fire("hover",a.pickResult,!0),(a.pickResult.worldPos||a.pickResult.snappedWorldPos)&&l.fire("hoverSurface",a.pickResult,!0)}else void 0!==this._lastPickedEntityId&&(l.fire("hoverOut",{entity:e.objects[this._lastPickedEntityId]},!0),this._lastPickedEntityId=void 0),l.fire("hoverOff",{canvasPos:a.pickCursorPos},!0)}),u.addEventListener("mousedown",this._canvasMouseDownHandler=t=>{1===t.which&&(o=!0),3===t.which&&(c=!0);if(1===t.which&&s.active&&s.pointerEnabled&&(n.mouseDownClientX=t.clientX,n.mouseDownClientY=t.clientY,n.mouseDownCursorX=n.pointerCanvasPos[0],n.mouseDownCursorY=n.pointerCanvasPos[1],!s.firstPerson&&s.followPointer&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickSurface=!0,a.update(),1===t.which))){const t=a.pickResult;t&&t.worldPos?(r.setPivotPos(t.worldPos),r.startPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),r.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=e=>{1===e.which&&(o=!1),3===e.which&&(c=!1)}),u.addEventListener("mouseup",this._canvasMouseUpHandler=i=>{if(!s.active||!s.pointerEnabled)return;if(!(1===i.which))return;if(r.hidePivot(),Math.abs(i.clientX-n.mouseDownClientX)>3||Math.abs(i.clientY-n.mouseDownClientY)>3)return;const o=l.hasSubs("picked"),c=l.hasSubs("pickedNothing"),u=l.hasSubs("pickedSurface"),A=l.hasSubs("doublePicked"),d=l.hasSubs("doublePickedSurface"),f=l.hasSubs("doublePickedNothing");if(!(s.doublePickFlyTo||A||d||f))return(o||c||u)&&(a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=!0,a.schedulePickSurface=u,a.update(),a.pickResult?(l.fire("picked",a.pickResult,!0),a.pickedSurface&&l.fire("pickedSurface",a.pickResult,!0)):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0)),void(this._clicks=0);if(this._clicks++,1===this._clicks){a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo,a.schedulePickSurface=u,a.update();const e=a.pickResult,i=a.pickedSurface;this._timeout=setTimeout((()=>{e?(l.fire("picked",e,!0),i&&(l.fire("pickedSurface",e,!0),!s.firstPerson&&s.followPointer&&(t.pivotController.setPivotPos(e.worldPos),t.pivotController.startPivot()&&t.pivotController.showPivot()))):l.fire("pickedNothing",{canvasPos:n.pointerCanvasPos},!0),this._clicks=0}),s.doubleClickTimeFrame)}else{if(null!==this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null),a.pickCursorPos=n.pointerCanvasPos,a.schedulePickEntity=s.doublePickFlyTo||A||d,a.schedulePickSurface=a.schedulePickEntity&&d,a.update(),a.pickResult){if(l.fire("doublePicked",a.pickResult,!0),a.pickedSurface&&l.fire("doublePickedSurface",a.pickResult,!0),s.doublePickFlyTo&&(h(a.pickResult),!s.firstPerson&&s.followPointer)){const e=a.pickResult.entity.aabb,s=p.getAABB3Center(e);t.pivotController.setPivotPos(s),t.pivotController.startPivot()&&t.pivotController.showPivot()}}else if(l.fire("doublePickedNothing",{canvasPos:n.pointerCanvasPos},!0),s.doublePickFlyTo&&(h(),!s.firstPerson&&s.followPointer)){const s=e.aabb,n=p.getAABB3Center(s);t.pivotController.setPivotPos(n),t.pivotController.startPivot()&&t.pivotController.showPivot()}this._clicks=0}},!1)}reset(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}destroy(){const e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}class tp{constructor(e,t,s,n,i){this._scene=e;const a=e.input,r=[],l=e.canvas.canvas;let o=!0;this._onSceneMouseMove=a.on("mousemove",(()=>{o=!0})),this._onSceneKeyDown=a.on("keydown",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&n.mouseover&&(r[t]=!0,t===a.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=a.on("keyup",(t=>{s.active&&s.pointerEnabled&&e.input.keyboardEnabled&&(r[t]=!1,t===a.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=e.on("tick",(l=>{if(!s.active||!s.pointerEnabled||!e.input.keyboardEnabled)return;if(!n.mouseover)return;const c=t.cameraControl,u=l.deltaTime/1e3;if(!s.planView){const e=c._isKeyDownForAction(c.ROTATE_Y_POS,r),n=c._isKeyDownForAction(c.ROTATE_Y_NEG,r),a=c._isKeyDownForAction(c.ROTATE_X_POS,r),l=c._isKeyDownForAction(c.ROTATE_X_NEG,r),o=u*s.keyboardRotationRate;(e||n||a||l)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),e?i.rotateDeltaY+=o:n&&(i.rotateDeltaY-=o),a?i.rotateDeltaX+=o:l&&(i.rotateDeltaX-=o),!s.firstPerson&&s.followPointer&&t.pivotController.startPivot())}if(!r[a.KEY_CTRL]&&!r[a.KEY_ALT]){const e=c._isKeyDownForAction(c.DOLLY_BACKWARDS,r),a=c._isKeyDownForAction(c.DOLLY_FORWARDS,r);if(e||a){const r=u*s.keyboardDollyRate;!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),a?i.dollyDelta-=r:e&&(i.dollyDelta+=r),o&&(n.followPointerDirty=!0,o=!1)}}const h=c._isKeyDownForAction(c.PAN_FORWARDS,r),p=c._isKeyDownForAction(c.PAN_BACKWARDS,r),A=c._isKeyDownForAction(c.PAN_LEFT,r),d=c._isKeyDownForAction(c.PAN_RIGHT,r),f=c._isKeyDownForAction(c.PAN_UP,r),I=c._isKeyDownForAction(c.PAN_DOWN,r),y=(r[a.KEY_ALT]?.3:1)*u*s.keyboardPanRate;(h||p||A||d||f||I)&&(!s.firstPerson&&s.followPointer&&t.pivotController.startPivot(),I?i.panDeltaY+=y:f&&(i.panDeltaY+=-y),d?i.panDeltaX+=-y:A&&(i.panDeltaX+=y),p?i.panDeltaZ+=y:h&&(i.panDeltaZ+=-y))}))}reset(){}destroy(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}const sp=p.vec3();class np{constructor(e,t,s,n,i){this._scene=e;const a=e.camera,r=t.pickController,l=t.pivotController,o=t.panController;let c=1,u=1,h=null;this._onTick=e.on("tick",(()=>{if(!s.active||!s.pointerEnabled)return;let t="default";if(Math.abs(i.dollyDelta)<.001&&(i.dollyDelta=0),Math.abs(i.rotateDeltaX)<.001&&(i.rotateDeltaX=0),Math.abs(i.rotateDeltaY)<.001&&(i.rotateDeltaY=0),0===i.rotateDeltaX&&0===i.rotateDeltaY||(i.dollyDelta=0),s.followPointer&&--c<=0&&(c=1,0!==i.dollyDelta)){if(0===i.rotateDeltaY&&0===i.rotateDeltaX&&s.followPointer&&n.followPointerDirty&&(r.pickCursorPos=n.pointerCanvasPos,r.schedulePickSurface=!0,r.update(),r.pickResult&&r.pickResult.worldPos?h=r.pickResult.worldPos:(u=1,h=null),n.followPointerDirty=!1),h){const t=Math.abs(p.lenVec3(p.subVec3(h,e.camera.eye,sp)));u=t/s.dollyProximityThreshold}u{n.mouseover=!0}),a.addEventListener("mouseleave",this._mouseLeaveHandler=()=>{n.mouseover=!1,a.style.cursor=null}),document.addEventListener("mousemove",this._mouseMoveHandler=e=>{ap(e,a,n.pointerCanvasPos)}),a.addEventListener("mousedown",this._mouseDownHandler=e=>{s.active&&s.pointerEnabled&&(ap(e,a,n.pointerCanvasPos),n.mouseover=!0)}),a.addEventListener("mouseup",this._mouseUpHandler=e=>{s.active&&s.pointerEnabled})}reset(){}destroy(){const e=this._scene.canvas.canvas;document.removeEventListener("mousemove",this._mouseMoveHandler),e.removeEventListener("mouseenter",this._mouseEnterHandler),e.removeEventListener("mouseleave",this._mouseLeaveHandler),e.removeEventListener("mousedown",this._mouseDownHandler),e.removeEventListener("mouseup",this._mouseUpHandler)}}function ap(e,t,s){if(e){const{x:n,y:i}=t.getBoundingClientRect();s[0]=e.clientX-n,s[1]=e.clientY-i}else e=window.event,s[0]=e.x,s[1]=e.y;return s}const rp=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class lp{constructor(e,t,s,n,i){this._scene=e;const a=t.pickController,r=t.pivotController,l=p.vec2(),o=p.vec2(),c=p.vec2(),u=p.vec2(),h=[],A=this._scene.canvas.canvas;let d=0,f=!1;this._onTick=e.on("tick",(()=>{f=!1})),A.addEventListener("touchstart",this._canvasTouchStartHandler=t=>{if(!s.active||!s.pointerEnabled)return;t.preventDefault();const i=t.touches,o=t.changedTouches;for(n.touchStartTime=Date.now(),1===i.length&&1===o.length&&(rp(i[0],l),s.followPointer&&(a.pickCursorPos=l,a.schedulePickSurface=!0,a.update(),s.planView||(a.picked&&a.pickedSurface&&a.pickResult&&a.pickResult.worldPos?(r.setPivotPos(a.pickResult.worldPos),!s.firstPerson&&r.startPivot()&&r.showPivot()):(s.smartPivot?r.setCanvasPivotPos(n.pointerCanvasPos):r.setPivotPos(e.camera.look),!s.firstPerson&&r.startPivot()&&r.showPivot()))));h.length{if(!s.active||!s.pointerEnabled)return;if(t.stopPropagation(),t.preventDefault(),f)return;f=!0;const r=e.canvas.boundary,l=r[2],A=r[3],I=t.touches;if(t.touches.length===d){if(1===d){rp(I[0],o),p.subVec2(o,h[0],u);const t=u[0],a=u[1];if(null!==n.longTouchTimeout&&(Math.abs(t)>s.longTapRadius||Math.abs(a)>s.longTapRadius)&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),s.planView){const n=e.camera;if("perspective"===n.projection){const r=Math.abs(e.camera.eyeLookDist)*Math.tan(n.perspective.fov/2*Math.PI/180);i.panDeltaX+=t*r/A*s.touchPanRate,i.panDeltaY+=a*r/A*s.touchPanRate}else i.panDeltaX+=.5*n.ortho.scale*(t/A)*s.touchPanRate,i.panDeltaY+=.5*n.ortho.scale*(a/A)*s.touchPanRate}else i.rotateDeltaY-=t/l*(1*s.dragRotationRate),i.rotateDeltaX+=a/A*(1.5*s.dragRotationRate)}else if(2===d){const t=I[0],r=I[1];rp(t,o),rp(r,c);const l=p.geometricMeanVec2(h[0],h[1]),u=p.geometricMeanVec2(o,c),d=p.vec2();p.subVec2(l,u,d);const f=d[0],y=d[1],m=e.camera,v=p.distVec2([t.pageX,t.pageY],[r.pageX,r.pageY]),w=(p.distVec2(h[0],h[1])-v)*s.touchDollyRate;if(i.dollyDelta=w,Math.abs(w)<1)if("perspective"===m.projection){const t=a.pickResult?a.pickResult.worldPos:e.center,n=Math.abs(p.lenVec3(p.subVec3(t,e.camera.eye,[])))*Math.tan(m.perspective.fov/2*Math.PI/180);i.panDeltaX-=f*n/A*s.touchPanRate,i.panDeltaY-=y*n/A*s.touchPanRate}else i.panDeltaX-=.5*m.ortho.scale*(f/A)*s.touchPanRate,i.panDeltaY-=.5*m.ortho.scale*(y/A)*s.touchPanRate;n.pointerCanvasPos=u}for(let e=0;e{let n;s&&s.worldPos&&(n=s.worldPos);const i=s?s.entity.aabb:e.aabb;if(n){const s=e.camera;p.subVec3(s.eye,s.look,[]),t.cameraFlight.flyTo({aabb:i})}else t.cameraFlight.flyTo({aabb:i})};A.addEventListener("touchstart",this._canvasTouchStartHandler=e=>{if(!s.active||!s.pointerEnabled)return;null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null);const i=e.touches,a=e.changedTouches;if(l=Date.now(),1===i.length&&1===a.length){u=l,op(i[0],c);const a=c[0],r=c[1],o=i[0].pageX,h=i[0].pageY;n.longTouchTimeout=setTimeout((()=>{t.cameraControl.fire("rightClick",{pagePos:[Math.round(o),Math.round(h)],canvasPos:[Math.round(a),Math.round(r)],event:e},!0),n.longTouchTimeout=null}),s.longTapTimeout)}else u=-1;for(;o.length{if(!s.active||!s.pointerEnabled)return;const t=Date.now(),i=e.touches,l=e.changedTouches,A=r.hasSubs("pickedSurface");null!==n.longTouchTimeout&&(clearTimeout(n.longTouchTimeout),n.longTouchTimeout=null),0===i.length&&1===l.length&&u>-1&&t-u<150&&(h>-1&&u-h<325?(op(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("doublePicked",a.pickResult),a.pickedSurface&&r.fire("doublePickedSurface",a.pickResult),s.doublePickFlyTo&&d(a.pickResult)):(r.fire("doublePickedNothing"),s.doublePickFlyTo&&d()),h=-1):p.distVec2(o[0],c)<4&&(op(l[0],a.pickCursorPos),a.schedulePickEntity=!0,a.schedulePickSurface=A,a.update(),a.pickResult?(a.pickResult.touchInput=!0,r.fire("picked",a.pickResult),a.pickedSurface&&r.fire("pickedSurface",a.pickResult)):r.fire("pickedNothing"),h=t),u=-1),o.length=i.length;for(let e=0,t=i.length;e{e.preventDefault()},this._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},this._states={pointerCanvasPos:p.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:p.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},this._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};const s=this.scene;this._controllers={cameraControl:this,pickController:new Qh(this,this._configs),pivotController:new kh(s,this._configs),panController:new Mh(s),cameraFlight:new Fa(this,{duration:.5})},this._handlers=[new ip(this.scene,this._controllers,this._configs,this._states,this._updates),new lp(this.scene,this._controllers,this._configs,this._states,this._updates),new zh(this.scene,this._controllers,this._configs,this._states,this._updates),new $h(this.scene,this._controllers,this._configs,this._states,this._updates),new ep(this.scene,this._controllers,this._configs,this._states,this._updates),new cp(this.scene,this._controllers,this._configs,this._states,this._updates),new tp(this.scene,this._controllers,this._configs,this._states,this._updates)],this._cameraUpdater=new np(this.scene,this._controllers,this._configs,this._states,this._updates),this.navMode=t.navMode,t.planView&&(this.planView=t.planView),this.constrainVertical=t.constrainVertical,t.keyboardLayout?this.keyboardLayout=t.keyboardLayout:this.keyMap=t.keyMap,this.doublePickFlyTo=t.doublePickFlyTo,this.panRightClick=t.panRightClick,this.active=t.active,this.followPointer=t.followPointer,this.rotationInertia=t.rotationInertia,this.keyboardPanRate=t.keyboardPanRate,this.touchPanRate=t.touchPanRate,this.keyboardRotationRate=t.keyboardRotationRate,this.dragRotationRate=t.dragRotationRate,this.touchDollyRate=t.touchDollyRate,this.dollyInertia=t.dollyInertia,this.dollyProximityThreshold=t.dollyProximityThreshold,this.dollyMinSpeed=t.dollyMinSpeed,this.panInertia=t.panInertia,this.pointerEnabled=!0,this.keyboardDollyRate=t.keyboardDollyRate,this.mouseWheelDollyRate=t.mouseWheelDollyRate}set keyMap(e){if(e=e||"qwerty",m.isString(e)){const t=this.scene.input,s={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":s[this.PAN_LEFT]=[t.KEY_A],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_Z],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":s[this.PAN_LEFT]=[t.KEY_Q],s[this.PAN_RIGHT]=[t.KEY_D],s[this.PAN_UP]=[t.KEY_W],s[this.PAN_DOWN]=[t.KEY_X],s[this.PAN_BACKWARDS]=[],s[this.PAN_FORWARDS]=[],s[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],s[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],s[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],s[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],s[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],s[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],s[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],s[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],s[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],s[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],s[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],s[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=s}else{const t=e;this._keyMap=t}}get keyMap(){return this._keyMap}_isKeyDownForAction(e,t){const s=this._keyMap[e];if(!s)return!1;t||(t=this.scene.input.keyDown);for(let e=0,n=s.length;e0?Ip(t):null,r=s&&s.length>0?Ip(s):null,l=e=>{if(!e)return;var t=!0;(r&&r[e.type]||a&&!a[e.type])&&(t=!1),t&&n.push(e.id);const s=e.children;if(s)for(var i=0,o=s.length;i * Copyright (c) 2022 Niklas von Hertzen diff --git a/dist/xeokit-sdk.min.es5.js b/dist/xeokit-sdk.min.es5.js index ede33cfe3..9a58d5471 100644 --- a/dist/xeokit-sdk.min.es5.js +++ b/dist/xeokit-sdk.min.es5.js @@ -23,7 +23,7 @@ function Kn(e,t){t.length;for(var n=e.length,r=[],i=[],a=function(n){var a=e[n]; /** * @author https://github.com/tmarti, with support from https://tribia.com/ * @license MIT - **/var Cc=null;function _c(e,t){var n,r,i,a,s,o,l=3*e,u=3*t,c=Math.min(n=Cc[l],r=Cc[l+1],i=Cc[l+2]),f=Math.min(a=Cc[u],s=Cc[u+1],o=Cc[u+2]);if(c!==f)return c-f;var p=Math.max(n,r,i),A=Math.max(a,s,o);return p!==A?p-A:0}function Bc(e,t){for(var n=new Int32Array(e.length/3),r=0,i=n.length;r>t;n.sort(_c);for(var o=new Int32Array(e.length),l=0,u=n.length;le[i+1]){var s=e[i];e[i]=e[i+1],e[i+1]=s}Oc=new Int32Array(e),t.sort(Sc);for(var o=new Int32Array(e.length),l=0,u=t.length;l0)for(var g=r._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl,n=e._lightsState;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var r=this._program;this._uRenderPass=r.getLocation("renderPass"),this._uLightAmbient=r.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];for(var i=n.lights,a=0,s=i.length;a0,a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),t.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),t.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(var s=0,o=r.lights.length;s> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(var l=0,u=r.lights.length;l0,r=[];if(r.push("#version 300 es"),r.push("// TrianglesDataTextureColorRenderer fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),this._withSAO&&(r.push("uniform sampler2D uOcclusionTexture;"),r.push("uniform vec4 uSAOParams;"),r.push("const float packUpscale = 256. / 255.;"),r.push("const float unpackDownScale = 255. / 256.;"),r.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),r.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),r.push("float unpackRGBToFloat( const in vec4 v ) {"),r.push(" return dot( v, unPackFactors );"),r.push("}")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(r.push(" float viewportWidth = uSAOParams[0];"),r.push(" float viewportHeight = uSAOParams[1];"),r.push(" float blendCutoff = uSAOParams[2];"),r.push(" float blendFactor = uSAOParams[3];"),r.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),r.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),r.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),jc=new Float32Array([1,1,1]),Vc=Z.vec3(),Qc=Z.vec3(),Wc=Z.vec3(),zc=Z.vec3(),Kc=Z.mat4(),Yc=function(){function e(t,n){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=this._scene,i=r.camera,a=t.model,s=r.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=a.position,f=a.rotationMatrix,p=a.rotationMatrixConjugate,A=i.viewMatrix;if(this._program||(this._allocate(),!this.errors)){var d,v;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,o)),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),u||0!==c[0]||0!==c[1]||0!==c[2]){var h=Vc;if(u){var I=Qc;Z.transformPoint3(f,u,I),h[0]=I[0],h[1]=I[1],h[2]=I[2]}else h[0]=0,h[1]=0,h[2]=0;h[0]+=c[0],h[1]+=c[1],h[2]+=c[2],d=Ie(A,h,Kc),(v=Wc)[0]=i.eye[0]-h[0],v[1]=i.eye[1]-h[1],v[2]=i.eye[2]-h[2]}else d=A,v=i.eye;if(s.uniform3fv(this._uCameraEyeRtc,v),s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),n===to.SILHOUETTE_XRAYED){var y=r.xrayMaterial._state,m=y.fillColor,w=y.fillAlpha;s.uniform4f(this._uColor,m[0],m[1],m[2],w)}else if(n===to.SILHOUETTE_HIGHLIGHTED){var g=r.highlightMaterial._state,T=g.fillColor,E=g.fillAlpha;s.uniform4f(this._uColor,T[0],T[1],T[2],E)}else if(n===to.SILHOUETTE_SELECTED){var b=r.selectedMaterial._state,D=b.fillColor,P=b.fillAlpha;s.uniform4f(this._uColor,D[0],D[1],D[2],P)}else s.uniform4fv(this._uColor,jc);if(r.logarithmicDepthBufferEnabled){var R=2/(Math.log(e.pickZFar+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,R)}var C=r._sectionPlanesState.sectionPlanes.length;if(C>0)for(var _=r._sectionPlanesState.sectionPlanes,B=t.layerIndex*C,O=a.renderFlags,S=0;S0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uColor=n.getLocation("color"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture silhouette vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.y) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture draw fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = color;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Xc=new Float32Array([0,0,0,1]),qc=Z.vec3(),Jc=Z.vec3();Z.vec3();var Zc=Z.vec3(),$c=Z.mat4(),ef=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=r.position,f=r.rotationMatrix,p=r.rotationMatrixConjugate,A=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=qc;if(v){var y=Jc;Z.transformPoint3(f,u,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],d=Ie(A,I,$c)}else d=A;if(s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),n===to.EDGES_XRAYED){var m=i.xrayMaterial._state,w=m.edgeColor,g=m.edgeAlpha;s.uniform4f(this._uColor,w[0],w[1],w[2],g)}else if(n===to.EDGES_HIGHLIGHTED){var T=i.highlightMaterial._state,E=T.edgeColor,b=T.edgeAlpha;s.uniform4f(this._uColor,E[0],E[1],E[2],b)}else if(n===to.EDGES_SELECTED){var D=i.selectedMaterial._state,P=D.edgeColor,R=D.edgeAlpha;s.uniform4f(this._uColor,P[0],P[1],P[2],R)}else s.uniform4fv(this._uColor,Xc);var C=i._sectionPlanesState.sectionPlanes.length;if(C>0)for(var _=i._sectionPlanesState.sectionPlanes,B=t.layerIndex*C,O=r.renderFlags,S=0;S0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),s.drawArrays(s.LINES,0,o.numEdgeIndices8Bits)),o.numEdgeIndices16Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),s.drawArrays(s.LINES,0,o.numEdgeIndices16Bits)),o.numEdgeIndices32Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),s.drawArrays(s.LINES,0,o.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uColor=n.getLocation("color"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vColor;"),n.push("void main(void) {"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.z) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("vColor = vec4(color.r, color.g, color.b, color.a);"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { discard; }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),tf=Z.vec3(),nf=Z.vec3(),rf=Z.vec3(),af=Z.mat4(),sf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=r.position,f=r.rotationMatrix,p=r.rotationMatrixConjugate,A=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=tf;if(v){var y=nf;Z.transformPoint3(f,u,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],d=Ie(A,I,af)}else d=A;s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);var m=i._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=i._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=r.renderFlags,E=0;E0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),s.drawArrays(s.LINES,0,o.numEdgeIndices8Bits)),o.numEdgeIndices16Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),s.drawArrays(s.LINES,0,o.numEdgeIndices16Bits)),o.numEdgeIndices32Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),s.drawArrays(s.LINES,0,o.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vColor;"),n.push("void main(void) {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.z) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("vec4 rgb = vec4(color.rgba);"),n.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { discard; }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),of=Z.vec3(),lf=Z.vec3(),uf=Z.vec3(),cf=Z.mat4(),ff=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){if(this._program||(this._allocate(t),!this.errors)){e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate;c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==f[0]||0!==f[1]||0!==f[2],h=0!==p[0]||0!==p[1]||0!==p[2];if(v||h){var I=of;if(v){var y=Z.transformPoint3(A,f,lf);I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=p[0],I[1]+=p[1],I[2]+=p[2],r=Ie(o.viewMatrix,I,cf),(i=uf)[0]=o.eye[0]-I[0],i[1]=o.eye[1]-I[1],i[2]=o.eye[2]-I[2]}else r=o.viewMatrix,i=o.eye;if(l.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),l.uniform3fv(this._uCameraEyeRtc,i),l.uniform1i(this._uRenderPass,n),s.logarithmicDepthBufferEnabled){var m=2/(Math.log(o.project.far+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,m)}var w=s._sectionPlanesState.sectionPlanes.length;if(w>0)for(var g=s._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry picking vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("smooth out vec4 vWorldPosition;"),n.push("flat out uvec4 vFlags2;")),n.push("out vec4 vPickColor;"),n.push("void main(void) {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry picking fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outPickColor = vPickColor; "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),pf=Z.vec3(),Af=Z.vec3(),df=Z.vec3(),vf=Z.vec3(),hf=Z.mat4(),If=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=e.pickViewMatrix||o.viewMatrix;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),o.eye,f||0!==p[0]||0!==p[1]||0!==p[2]){var h=pf;if(f){var I=Af;Z.transformPoint3(A,f,I),h[0]=I[0],h[1]=I[1],h[2]=I[2]}else h[0]=0,h[1]=0,h[2]=0;h[0]+=p[0],h[1]+=p[1],h[2]+=p[2],r=Ie(v,h,hf),(i=df)[0]=o.eye[0]-h[0],i[1]=o.eye[1]-h[1],i[2]=o.eye[2]-h[2],e.snapPickOrigin[0]=h[0],e.snapPickOrigin[1]=h[1],e.snapPickOrigin[2]=h[2]}else r=v,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniform1f(this._uPickZNear,e.pickZNear),l.uniform1f(this._uPickZFar,e.pickZFar),l.uniformMatrix4fv(this._uWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var y=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,y)}var m=s._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=s._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=a.renderFlags,E=0;E0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vViewPosition;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vViewPosition = viewPosition;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick depth fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform float pickZNear;"),r.push("uniform float pickZFar;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),r.push(" outPackedDepth = packDepth(zNormalizedDepth); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),yf=Z.vec3(),mf=Z.vec3(),wf=Z.vec3(),gf=Z.vec3(),Tf=Z.vec3(),Ef=Z.mat4(),bf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){if(this._program||(this._allocate(),!this.errors)){e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=t.aabb,h=e.pickViewMatrix||o.viewMatrix,I=yf;I[0]=Z.safeInv(v[3]-v[0])*Z.MAX_INT,I[1]=Z.safeInv(v[4]-v[1])*Z.MAX_INT,I[2]=Z.safeInv(v[5]-v[2])*Z.MAX_INT,e.snapPickCoordinateScale[0]=Z.safeInv(I[0]),e.snapPickCoordinateScale[1]=Z.safeInv(I[1]),e.snapPickCoordinateScale[2]=Z.safeInv(I[2]),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var y=0!==f[0]||0!==f[1]||0!==f[2],m=0!==p[0]||0!==p[1]||0!==p[2];if(y||m){var w=mf;if(y){var g=Z.transformPoint3(A,f,wf);w[0]=g[0],w[1]=g[1],w[2]=g[2]}else w[0]=0,w[1]=0,w[2]=0;w[0]+=p[0],w[1]+=p[1],w[2]+=p[2],r=Ie(h,w,Ef),(i=gf)[0]=o.eye[0]-w[0],i[1]=o.eye[1]-w[1],i[2]=o.eye[2]-w[2],e.snapPickOrigin[0]=w[0],e.snapPickOrigin[1]=w[1],e.snapPickOrigin[2]=w[2]}else r=h,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform2fv(this.uVectorA,e.snapVectorA),l.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),l.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),l.uniform3fv(this._uCoordinateScaler,I),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var T=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,T)}var E=s._sectionPlanesState.sectionPlanes.length;if(E>0)for(var b=s._sectionPlanesState.sectionPlanes,D=t.layerIndex*E,P=a.renderFlags,R=0;R0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),l.drawArrays(S,0,u.numEdgeIndices8Bits)),u.numEdgeIndices16Bits>0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),l.drawArrays(S,0,u.numEdgeIndices16Bits)),u.numEdgeIndices32Bits>0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),l.drawArrays(S,0,u.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("uniform vec2 uSnapVectorA;"),n.push("uniform vec2 uSnapInvVectorAB;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),n.push("vec2 remapClipPos(vec2 clipPos) {"),n.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),n.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),n.push(" return vec2(x, y);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vViewPosition;"),n.push("out highp vec3 relativeToOriginPosition;"),n.push("void main(void) {"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("{"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("relativeToOriginPosition = worldPosition.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vViewPosition = viewPosition;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),n.push("float tmp = clipPos.w;"),n.push("clipPos.xyzw /= tmp;"),n.push("clipPos.xy = remapClipPos(clipPos.xy);"),n.push("clipPos.xyzw *= tmp;"),n.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("gl_PointSize = 1.0;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick depth fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform int uLayerNumber;"),r.push("uniform vec3 uCoordinateScaler;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0;s 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Df=Z.vec3(),Pf=Z.vec3(),Rf=Z.vec3(),Cf=Z.vec3(),_f=Z.vec3(),Bf=Z.mat4(),Of=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=t.aabb,h=e.pickViewMatrix||o.viewMatrix,I=Df;I[0]=Z.safeInv(v[3]-v[0])*Z.MAX_INT,I[1]=Z.safeInv(v[4]-v[1])*Z.MAX_INT,I[2]=Z.safeInv(v[5]-v[2])*Z.MAX_INT,e.snapPickCoordinateScale[0]=Z.safeInv(I[0]),e.snapPickCoordinateScale[1]=Z.safeInv(I[1]),e.snapPickCoordinateScale[2]=Z.safeInv(I[2]),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var y=0!==f[0]||0!==f[1]||0!==f[2],m=0!==p[0]||0!==p[1]||0!==p[2];if(y||m){var w=Pf;if(y){var g=Rf;Z.transformPoint3(A,f,g),w[0]=g[0],w[1]=g[1],w[2]=g[2]}else w[0]=0,w[1]=0,w[2]=0;w[0]+=p[0],w[1]+=p[1],w[2]+=p[2],r=Ie(h,w,Bf),(i=Cf)[0]=o.eye[0]-w[0],i[1]=o.eye[1]-w[1],i[2]=o.eye[2]-w[2],e.snapPickOrigin[0]=w[0],e.snapPickOrigin[1]=w[1],e.snapPickOrigin[2]=w[2]}else r=h,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform2fv(this._uVectorA,e.snapVectorA),l.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),l.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),l.uniform3fv(this._uCoordinateScaler,I),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var T=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,T)}var E=s._sectionPlanesState.sectionPlanes.length;if(E>0)for(var b=s._sectionPlanesState.sectionPlanes,D=t.layerIndex*E,P=a.renderFlags,R=0;R0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("uniform vec2 uVectorAB;"),n.push("uniform vec2 uInverseVectorAB;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("vec2 remapClipPos(vec2 clipPos) {"),n.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),n.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),n.push(" return vec2(x, y);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out highp vec3 relativeToOriginPosition;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("{"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push(" if (isPerspectiveMatrix(projMatrix)) {"),n.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push(" position = positions[2 - (gl_VertexID % 3)];"),n.push(" viewNormal = -viewNormal;"),n.push(" }"),n.push(" } else {"),n.push(" if (viewNormal.z < 0.0) {"),n.push(" position = positions[2 - (gl_VertexID % 3)];"),n.push(" viewNormal = -viewNormal;"),n.push(" }"),n.push(" }"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("relativeToOriginPosition = worldPosition.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),n.push("float tmp = clipPos.w;"),n.push("clipPos.xyzw /= tmp;"),n.push("clipPos.xy = remapClipPos(clipPos.xy);"),n.push("clipPos.z += 0.0001;"),n.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform int uLayerNumber;"),r.push("uniform vec3 uCoordinateScaler;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0;s 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Sf=Z.vec3(),Nf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var c=a.eye;e.pickViewMatrix&&(l.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),c=e.pickOrigin||c);var f=[c[0]-u[0],c[1]-u[1],c[2]-u[2]];if(s.uniform3fv(this._uCameraEyeRtc,f),s.uniform1i(this._uRenderPass,n),s.uniform3fv(this._uCameraEyeRtc,f),s.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){var p=2/(Math.log(a.project.far+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,p)}var A=i._sectionPlanesState.sectionPlanes.length;if(A>0)for(var d=i._sectionPlanesState.sectionPlanes,v=t.layerIndex*A,h=r.renderFlags,I=0;I0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureCameraMatrices;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec3 vWorldNormal;"),n.push("void main(void) {"),n.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),n.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),n.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("normal = -normal;"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("normal = -normal;"),n.push("}"),n.push("}"),n.push("}"),n.push("normal = -normal;"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vWorldNormal = normal.xyz;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.w;")),n.push("gl_Position = clipPos;"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick normals fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}();Z.vec3(),Z.vec3(),Z.vec3(),Z.vec3(),Z.mat4();var Lf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("// TrianglesDataTextureColorRenderer vertex shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("uniform int renderPass;"),n.push("attribute vec3 position;"),e.entityOffsetsEnabled&&n.push("attribute vec3 offset;"),n.push("attribute vec4 color;"),n.push("attribute vec4 flags;"),n.push("attribute vec4 flags2;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("out vec4 vFlags2;")),n.push("void main(void) {"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&($e.SUPPORTED_EXTENSIONS.EXT_frag_depth?n.push("vFragDepth = 1.0 + clipPos.w;"):(n.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),n.push("clipPos.z *= clipPos.w;")),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("// TrianglesDataTextureColorRenderer fragment shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("in vec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var a=0;a 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),xf=Z.vec3(),Mf=Z.vec3(),Ff=Z.vec3(),Hf=Z.vec3(),Uf=Z.mat4(),Gf=function(){function e(t){b(this,e),this._scene=t,this._allocate(),this._hash=this._getHash()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=this._scene,i=r.camera,a=t.model,s=r.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=a.position,f=a.rotationMatrix,p=a.rotationMatrixConjugate;if(this._program||(this._allocate(),!this.errors)){var A,d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,o)),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=xf;if(v){var y=Z.transformPoint3(f,u,Mf);I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],A=Ie(i.viewMatrix,I,Uf),(d=Ff)[0]=i.eye[0]-I[0],d[1]=i.eye[1]-I[1],d[2]=i.eye[2]-I[2]}else A=i.viewMatrix,d=i.eye;if(s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,A),s.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s.uniform3fv(this._uCameraEyeRtc,d),s.uniform1i(this._uRenderPass,n),r.logarithmicDepthBufferEnabled){var m=2/(Math.log(e.pickZFar+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,m)}var w=r._sectionPlanesState.sectionPlanes.length;if(w>0)for(var g=r._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,r=[];return r.push("#version 300 es"),r.push("// Triangles dataTexture draw vertex shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("precision highp usampler2D;"),r.push("precision highp isampler2D;"),r.push("precision highp sampler2D;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("precision mediump usampler2D;"),r.push("precision mediump isampler2D;"),r.push("precision mediump sampler2D;"),r.push("#endif"),r.push("uniform int renderPass;"),e.entityOffsetsEnabled&&r.push("in vec3 offset;"),r.push("uniform mat4 sceneModelWorldMatrix;"),r.push("uniform mat4 viewMatrix;"),r.push("uniform mat4 projMatrix;"),r.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),r.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),r.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),r.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),r.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),r.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),r.push("uniform highp sampler2D uTextureModelMatrices;"),r.push("uniform vec3 uCameraEyeRtc;"),r.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(r.push("uniform float logDepthBufFC;"),r.push("out float vFragDepth;"),r.push("out float isPerspective;")),r.push("bool isPerspectiveMatrix(mat4 m) {"),r.push(" return (m[2][3] == - 1.0);"),r.push("}"),r.push("out highp vec2 vHighPrecisionZW;"),n&&(r.push("out vec4 vWorldPosition;"),r.push("flat out uint vFlags2;")),r.push("void main(void) {"),r.push("int polygonIndex = gl_VertexID / 3;"),r.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),r.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),r.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),r.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),r.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),r.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),r.push("if (int(flags.x) != renderPass) {"),r.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),r.push(" return;"),r.push("} else {"),r.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),r.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),r.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),r.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),r.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),r.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),r.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),r.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),r.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),r.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),r.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),r.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),r.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),r.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),r.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),r.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),r.push("if (color.a == 0u) {"),r.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),r.push(" return;"),r.push("};"),r.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),r.push("vec3 position;"),r.push("position = positions[gl_VertexID % 3];"),r.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),r.push("if (solid != 1u) {"),r.push("if (isPerspectiveMatrix(projMatrix)) {"),r.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),r.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),r.push("position = positions[2 - (gl_VertexID % 3)];"),r.push("viewNormal = -viewNormal;"),r.push("}"),r.push("} else {"),r.push("if (viewNormal.z < 0.0) {"),r.push("position = positions[2 - (gl_VertexID % 3)];"),r.push("viewNormal = -viewNormal;"),r.push("}"),r.push("}"),r.push("}"),r.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),r.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),r.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),r.push("vec4 viewPosition = viewMatrix * worldPosition; "),r.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(r.push("vFragDepth = 1.0 + clipPos.w;"),r.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n&&(r.push("vWorldPosition = worldPosition;"),r.push("vFlags2 = flags2.r;")),r.push("gl_Position = clipPos;"),r.push("vHighPrecisionZW = gl_Position.zw;"),r.push("}"),r.push("}"),r}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture draw fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),r.push("in highp vec2 vHighPrecisionZW;"),r.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),r.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),kf=Z.vec3(),jf=Z.vec3(),Vf=Z.vec3(),Qf=Z.vec3(),Wf=Z.mat4(),zf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=t._state.origin,u=r.position,c=r.rotationMatrix,f=r.rotationMatrixConjugate,p=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var A,d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));var v=0!==l[0]||0!==l[1]||0!==l[2],h=0!==u[0]||0!==u[1]||0!==u[2];if(v||h){var I=kf;if(v){var y=jf;Z.transformPoint3(c,l,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=u[0],I[1]+=u[1],I[2]+=u[2],A=Ie(p,I,Wf),(d=Vf)[0]=a.eye[0]-I[0],d[1]=a.eye[1]-I[1],d[2]=a.eye[2]-I[2]}else A=p,d=a.eye;s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uWorldMatrix,!1,f),s.uniformMatrix4fv(this._uViewMatrix,!1,A),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),s.uniformMatrix4fv(this._uWorldNormalMatrix,!1,r.worldNormalMatrix);var m=i._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=i._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=r.renderFlags,E=0;E0,n=[];return n.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("uniform int renderPass;"),n.push("attribute vec3 position;"),e.entityOffsetsEnabled&&n.push("attribute vec3 offset;"),n.push("attribute vec3 normal;"),n.push("attribute vec4 color;"),n.push("attribute vec4 flags;"),n.push("attribute vec4 flags2;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 worldNormalMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform mat4 viewNormalMatrix;"),n.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("varying float isPerspective;")),n.push("vec3 octDecode(vec2 oct) {"),n.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),n.push(" if (v.z < 0.0) {"),n.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),n.push(" }"),n.push(" return normalize(v);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("out vec4 vFlags2;")),n.push("out vec3 vViewNormal;"),n.push("void main(void) {"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),n.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),n.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push(" vViewNormal = viewNormal;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&($e.SUPPORTED_EXTENSIONS.EXT_frag_depth?n.push("vFragDepth = 1.0 + clipPos.w;"):(n.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),n.push("clipPos.z *= clipPos.w;")),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("in vec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var a=0;a 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Kf=Z.vec3(),Yf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerVertexIdCoordinates),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},3),s.uniform1i(this._uRenderPass,n),s.uniform1i(this._uPickInvisible,e.pickInvisible),s.uniformMatrix4fv(this._uWorldMatrix,!1,r.worldMatrix);var u=e.pickViewMatrix||a.viewMatrix,c=l?Ie(u,l):u;if(s.uniformMatrix4fv(this._uViewMatrix,!1,c),s.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){var f=2/(Math.log(a.project.far+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,f)}var p=i._sectionPlanesState.sectionPlanes.length;if(p>0)for(var A=i._sectionPlanesState.sectionPlanes,d=t.layerIndex*p,v=r.renderFlags,h=0;h0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),n.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform bool pickInvisible;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform isampler2D uTexturePerPolygonIdNormals;"),n.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),n.push("out vec4 vWorldPosition;"),t&&n.push("out int vFlags2;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_normal_index = polygonIndex & 4095;"),n.push("int v_normal_index = polygonIndex >> 12;"),n.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),n.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),n.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),n.push("int objectIndex;"),n.push("if (((polygonIndex >> 3) % 2) == 0) {"),n.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),n.push("} else {"),n.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),n.push("}"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("int h_index = polygonIndex & 4095;"),n.push("int v_index = polygonIndex >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),n.push("int vertexNumber = gl_VertexID % 3;"),n.push("vec3 position;"),n.push("if (vertexNumber == 0) position = position1;"),n.push("else if (vertexNumber == 1) position = position2;"),n.push("else position = position3;"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),n.push(" vWorldPosition = worldPosition;"),t&&n.push(" vFlags2 = flags2.r;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick flat normals fragment shader"),r.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("in vec4 vWorldPosition;"),n){r.push("in int vFlags2;");for(var i=0;i 0;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),r.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),r.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),r.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Xf=function(){function e(t){b(this,e),this._scene=t}return P(e,[{key:"_compile",value:function(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}},{key:"colorRenderer",get:function(){return this._colorRenderer||(this._colorRenderer=new kc(this._scene,!1)),this._colorRenderer}},{key:"colorRendererWithSAO",get:function(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new kc(this._scene,!0)),this._colorRendererWithSAO}},{key:"colorQualityRendererWithSAO",get:function(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}},{key:"silhouetteRenderer",get:function(){return this._silhouetteRenderer||(this._silhouetteRenderer=new Yc(this._scene)),this._silhouetteRenderer}},{key:"depthRenderer",get:function(){return this._depthRenderer||(this._depthRenderer=new Gf(this._scene)),this._depthRenderer}},{key:"normalsRenderer",get:function(){return this._normalsRenderer||(this._normalsRenderer=new zf(this._scene)),this._normalsRenderer}},{key:"edgesRenderer",get:function(){return this._edgesRenderer||(this._edgesRenderer=new ef(this._scene)),this._edgesRenderer}},{key:"edgesColorRenderer",get:function(){return this._edgesColorRenderer||(this._edgesColorRenderer=new sf(this._scene)),this._edgesColorRenderer}},{key:"pickMeshRenderer",get:function(){return this._pickMeshRenderer||(this._pickMeshRenderer=new ff(this._scene)),this._pickMeshRenderer}},{key:"pickNormalsRenderer",get:function(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new Nf(this._scene)),this._pickNormalsRenderer}},{key:"pickNormalsFlatRenderer",get:function(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new Yf(this._scene)),this._pickNormalsFlatRenderer}},{key:"pickDepthRenderer",get:function(){return this._pickDepthRenderer||(this._pickDepthRenderer=new If(this._scene)),this._pickDepthRenderer}},{key:"vertexDepthRenderer",get:function(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new bf(this._scene)),this._vertexDepthRenderer}},{key:"snapDepthBufInitRenderer",get:function(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new Of(this._scene)),this._snapDepthBufInitRenderer}},{key:"occlusionRenderer",get:function(){return this._occlusionRenderer||(this._occlusionRenderer=new Lf(this._scene)),this._occlusionRenderer}},{key:"_destroy",value:function(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}]),e}(),qf={};var Jf=P((function e(){b(this,e),this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]})),Zf=function(){function e(){b(this,e),this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}return P(e,[{key:"finalize",value:function(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}},{key:"bindCommonTextures",value:function(e,t,n,r,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,n,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,r,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}},{key:"bindPickCameraTexture",value:function(e,t){}},{key:"bindTriangleIndicesTextures",value:function(e,t,n,r){this.indicesPortionIdsPerBitnessTextures[r].bindTexture(e,t,6),this.indicesPerBitnessTextures[r].bindTexture(e,n,7)}},{key:"bindEdgeIndicesTextures",value:function(e,t,n,r){this.edgeIndicesPortionIdsPerBitnessTextures[r].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[r].bindTexture(e,n,7)}}]),e}(),$f=function(){function e(t,n,r,i){var a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null;b(this,e),this._gl=t,this._texture=n,this._textureWidth=r,this._textureHeight=i,this._textureData=a}return P(e,[{key:"bindTexture",value:function(e,t,n){return e.bindTexture(t,this,n)}},{key:"bind",value:function(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}},{key:"unbind",value:function(e){}}]),e}(),ep={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(ep,null,4));var e=0;Object.keys(ep).forEach((function(t){t.startsWith("size")&&(e+=ep[t])})),console.log("Total size ".concat(e," bytes (").concat((e/1e3/1e3).toFixed(2)," MB)")),console.log("Avg bytes / triangle: ".concat((e/ep.totalPolygons).toFixed(2)));var t={};Object.keys(ep).forEach((function(n){n.startsWith("size")&&(t[n]="".concat((ep[n]/e*100).toFixed(2)," % of total"))})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};var tp=function(){function e(){b(this,e)}return P(e,[{key:"disableBindedTextureFiltering",value:function(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}},{key:"generateCameraDataTexture",value:function(e,t,n,r){var i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);var a=new $f(e,i,4,3),s=!0;a.updateViewMatrix=function(n,i){e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(r?Ie(n,r):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};var o=function(){s&&(s=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(function(){return s=!0})),n.on("rendering",o),o(),a}},{key:"generatePickCameraDataTexture",value:function(e,t,n){var r=e.createTexture();e.bindTexture(e.TEXTURE_2D,r),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);var i=new $f(e,r,4,3);return i.updateViewMatrix=function(r,a){e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?Ie(r,n):r)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}},{key:"generateModelTexture",value:function(e,t){var n=e.createTexture();return e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new $f(e,n,4,2)}},{key:"generateTextureForColorsAndFlags",value:function(e,t,n,r,i,a,s){var o=t.length;this.numPortions=o;var l=4096,u=Math.ceil(o/512);if(0===u)throw"texture height===0";var c=new Uint8Array(16384*u);ep.sizeDataColorsAndFlags+=c.byteLength,ep.numberOfTextures++;for(var f=0;f>24&255,r[f]>>16&255,r[f]>>8&255,255&r[f]],32*f+16),c.set([i[f]>>24&255,i[f]>>16&255,i[f]>>8&255,255&i[f]],32*f+20),c.set([a[f]>>24&255,a[f]>>16&255,a[f]>>8&255,255&a[f]],32*f+24),c.set([s[f]?1:0,0,0,0],32*f+28);var p=e.createTexture();return e.bindTexture(e.TEXTURE_2D,p),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,l,u),e.texSubImage2D(e.TEXTURE_2D,0,0,0,l,u,e.RGBA_INTEGER,e.UNSIGNED_BYTE,c,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new $f(e,p,l,u,c)}},{key:"generateTextureForObjectOffsets",value:function(e,t){var n=512,r=Math.ceil(t/n);if(0===r)throw"texture height===0";var i=new Float32Array(1536*r).fill(0);ep.sizeDataTextureOffsets+=i.byteLength,ep.numberOfTextures++;var a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,n,r),e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new $f(e,a,n,r,i)}},{key:"generateTextureForPositionsDecodeMatrices",value:function(e,t,n){var r=t.length;if(0===r)throw"num decode+entity matrices===0";var i=2048,a=Math.ceil(r/512),s=new Float32Array(8192*a);ep.sizeDataPositionDecodeMatrices+=s.byteLength,ep.numberOfTextures++;for(var o=Z.mat4(),l=0;l65536&&ep.cannotCreatePortion.because10BitsObjectId++;var n=this._numPortions+t<=65536,r=void 0!==e.geometryId&&null!==e.geometryId?"".concat(e.geometryId,"#").concat(0):"".concat(e.id,"#").concat(0);if(!this._bucketGeometries[r]){var i=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits),a=0,s=0;e.buckets.forEach((function(e){a+=e.positionsCompressed.length/3,s+=e.indices.length/3})),(this._state.numVertices+a>16777216||i+s>16777216)&&ep.cannotCreatePortion.becauseTextureSize++,n&&(n=this._state.numVertices+a<=16777216&&i+s<=16777216)}return n}},{key:"createPortion",value:function(e){var t=this;if(this._finalized)throw"Already finalized";var n=[],r=e.worldAABB;e.buckets.forEach((function(i,a){var s=void 0!==e.geometryId&&null!==e.geometryId?"".concat(e.geometryId,"#").concat(a):"".concat(e.id,"#").concat(a),o=t._bucketGeometries[s];o||(o=t._createBucketGeometry(e,i),t._bucketGeometries[s]=o);var l=Z.collapseAABB3(op),u=t._createSubPortion(e,o,i,l);Z.expandAABB3(r,l),n.push(u)}));var i=this._state.origin;0===i[0]&&0===i[1]&&0===i[2]||(r[0]+=i[0],r[1]+=i[1],r[2]+=i[2],r[3]+=i[0],r[4]+=i[1],r[5]+=i[2]),Z.expandAABB3(this.aabb,r);var a=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(n),this.model.numPortions++,a}},{key:"_createBucketGeometry",value:function(e,t){if(t.indices){var n=8*Math.ceil(t.indices.length/3/8)*3;ep.overheadSizeAlignementIndices+=2*(n-t.indices.length);var r=new Uint32Array(n);r.fill(0),r.set(t.indices),t.indices=r}if(t.edgeIndices){var i=8*Math.ceil(t.edgeIndices.length/2/8)*2;ep.overheadSizeAlignementEdgeIndices+=2*(i-t.edgeIndices.length);var a=new Uint32Array(i);a.fill(0),a.set(t.edgeIndices),t.edgeIndices=a}for(var s,o=t.positionsCompressed,l=t.indices,u=t.edgeIndices,c=this._buffer,f=c.positionsCompressed.length/3,p=o.length/3,A=0,d=o.length;A0){var y,m=3*t.numTriangles;t.numVertices<=256?(y=p.perTriangleNumberPortionId8Bits,A.numIndices8Bits+=m,ep.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(y=p.perTriangleNumberPortionId16Bits,A.numIndices16Bits+=m,ep.totalPolygons16Bits+=t.numTriangles):(y=p.perTriangleNumberPortionId32Bits,A.numIndices32Bits+=m,ep.totalPolygons32Bits+=t.numTriangles),ep.totalPolygons+=t.numTriangles;for(var w=0;w0){var g,T=2*t.numEdges;t.numVertices<=256?(g=p.perEdgeNumberPortionId8Bits,A.numEdgeIndices8Bits+=T,ep.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(g=p.perEdgeNumberPortionId16Bits,A.numEdgeIndices16Bits+=T,ep.totalEdges16Bits+=t.numEdges):(g=p.perEdgeNumberPortionId32Bits,A.numEdgeIndices32Bits+=T,ep.totalEdges32Bits+=t.numEdges),ep.totalEdges+=t.numEdges;for(var E=0;E0&&(n.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId8Bits)),i.perEdgeNumberPortionId16Bits.length>0&&(n.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId16Bits)),i.perEdgeNumberPortionId32Bits.length>0&&(n.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId32Bits)),i.indices8Bits.length>0&&(n.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(r,i.indices8Bits)),i.indices16Bits.length>0&&(n.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(r,i.indices16Bits)),i.indices32Bits.length>0&&(n.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(r,i.indices32Bits)),i.edgeIndices8Bits.length>0&&(n.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(r,i.edgeIndices8Bits)),i.edgeIndices16Bits.length>0&&(n.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(r,i.edgeIndices16Bits)),i.edgeIndices32Bits.length>0&&(n.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(r,i.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(r,this.model)),n.textureModelMatrices=this.model._modelMatricesTexture,n.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),n.textureCameraMatrices=n.cameraTexture,n.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),n.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(function(){e._deferredSetFlagsDirty&&e._uploadDeferredFlags(),e._numUpdatesInFrame=0}))}}},{key:"isEmpty",value:function(){return 0===this._numPortions}},{key:"initFlags",value:function(e,t,n){t&Te&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&Ce&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&Re&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&_e&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&De&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&Be&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&be&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&Ee&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),n&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,n,true),this._setFlags2(e,t,true)}},{key:"flushInitFlags",value:function(){this._setDeferredFlags(),this._setDeferredFlags2()}},{key:"setVisible",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Te?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,n)}},{key:"setHighlighted",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Ce?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,n)}},{key:"setXRayed",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Re?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,n)}},{key:"setSelected",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&_e?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,n)}},{key:"setEdges",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Be?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,n)}},{key:"setClippable",value:function(e,t){if(!this._finalized)throw"Not finalized";t&De?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}},{key:"_beginDeferredFlags",value:function(){this._deferredSetFlagsActive=!0}},{key:"_uploadDeferredFlags",value:function(){if(this._deferredSetFlagsActive=!1,this._deferredSetFlagsDirty){this._deferredSetFlagsDirty=!1;var e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}}},{key:"setCulled",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Ee?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,n)}},{key:"setCollidable",value:function(e,t){if(!this._finalized)throw"Not finalized"}},{key:"setPickable",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&be?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,n)}},{key:"setColor",value:function(e,t){for(var n=this._portionToSubPortionsMap[e],r=0,i=n.length;r=10&&this._beginDeferredFlags(),r.bindTexture(r.TEXTURE_2D,n.texturePerObjectIdColorsAndFlags._texture),r.texSubImage2D(r.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,r.RGBA_INTEGER,r.UNSIGNED_BYTE,ap))}},{key:"setTransparent",value:function(e,t,n){n?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,n)}},{key:"_setFlags",value:function(e,t,n){for(var r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=this._portionToSubPortionsMap[e],a=0,s=i.length;a3&&void 0!==arguments[3]&&arguments[3];if(!this._finalized)throw"Not finalized";var i,a,s=!!(t&Te),o=!!(t&Re),l=!!(t&Ce),u=!!(t&_e),c=!!(t&Be),f=!!(t&be),p=!!(t&Ee);i=!s||p||o?to.NOT_RENDERED:n?to.COLOR_TRANSPARENT:to.COLOR_OPAQUE,a=!s||p?to.NOT_RENDERED:u?to.SILHOUETTE_SELECTED:l?to.SILHOUETTE_HIGHLIGHTED:o?to.SILHOUETTE_XRAYED:to.NOT_RENDERED;var A=0;A=!s||p?to.NOT_RENDERED:u?to.EDGES_SELECTED:l?to.EDGES_HIGHLIGHTED:o?to.EDGES_XRAYED:c?n?to.EDGES_COLOR_TRANSPARENT:to.EDGES_COLOR_OPAQUE:to.NOT_RENDERED;var d=s&&f?to.PICK:to.NOT_RENDERED,v=this._dataTextureState,h=this.model.scene.canvas.gl;ap[0]=i,ap[1]=a,ap[2]=A,ap[3]=d,v.texturePerObjectIdColorsAndFlags._textureData.set(ap,32*e+8),this._deferredSetFlagsActive||r?this._deferredSetFlagsDirty=!0:(++this._numUpdatesInFrame>=10&&this._beginDeferredFlags(),h.bindTexture(h.TEXTURE_2D,v.texturePerObjectIdColorsAndFlags._texture),h.texSubImage2D(h.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,h.RGBA_INTEGER,h.UNSIGNED_BYTE,ap))}},{key:"_setDeferredFlags",value:function(){}},{key:"_setFlags2",value:function(e,t){for(var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=this._portionToSubPortionsMap[e],i=0,a=r.length;i2&&void 0!==arguments[2]&&arguments[2];if(!this._finalized)throw"Not finalized";var r=t&De?255:0,i=this._dataTextureState,a=this.model.scene.canvas.gl;ap[0]=r,ap[1]=0,ap[2]=1,ap[3]=2,i.texturePerObjectIdColorsAndFlags._textureData.set(ap,32*e+12),this._deferredSetFlagsActive||n?this._deferredSetFlagsDirty=!0:(++this._numUpdatesInFrame>=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,ap))}},{key:"_setDeferredFlags2",value:function(){}},{key:"setOffset",value:function(e,t){for(var n=this._portionToSubPortionsMap[e],r=0,i=n.length;r=10&&this._beginDeferredFlags(),r.bindTexture(r.TEXTURE_2D,n.texturePerObjectIdOffsets._texture),r.texSubImage2D(r.TEXTURE_2D,0,0,e,1,1,r.RGB,r.FLOAT,sp))}},{key:"drawColorOpaque",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,to.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"_updateBackfaceCull",value:function(e,t){var n=this.model.backfaces||e.sectioned;if(t.backfaces!==n){var r=t.gl;n?r.disable(r.CULL_FACE):r.enable(r.CULL_FACE),t.backfaces=n}}},{key:"drawColorTransparent",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,to.COLOR_TRANSPARENT))}},{key:"drawDepth",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawNormals",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawSilhouetteXRayed",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_XRAYED))}},{key:"drawSilhouetteHighlighted",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_HIGHLIGHTED))}},{key:"drawSilhouetteSelected",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_SELECTED))}},{key:"drawEdgesColorOpaque",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,to.EDGES_COLOR_OPAQUE)}},{key:"drawEdgesColorTransparent",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,to.EDGES_COLOR_TRANSPARENT)}},{key:"drawEdgesHighlighted",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_HIGHLIGHTED)}},{key:"drawEdgesSelected",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_SELECTED)}},{key:"drawEdgesXRayed",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_XRAYED)}},{key:"drawOcclusion",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawShadow",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"setPickMatrices",value:function(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}},{key:"drawPickMesh",value:function(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,to.PICK))}},{key:"drawPickDepths",value:function(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,to.PICK))}},{key:"drawSnapInitDepthBuf",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,to.PICK))}},{key:"drawSnapDepths",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,to.PICK))}},{key:"drawPickNormals",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,to.PICK))}},{key:"destroy",value:function(){if(!this._destroyed){var e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}}]),e}(),fp=Z.vec3();Z.mat4();var pp=Z.vec3([1,1,1]),Ap=Z.vec3([0,0,0]),dp=Z.vec3([0,0,0]),vp=Z.identityQuaternion(),hp=Z.identityMat4(),Ip=new Uint8Array([255,255,255]),yp=function(e){I(n,ve);var t=m(n);function n(e){var r,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),(r=t.call(this,e,i))._dtxEnabled=r.scene.dtxEnabled&&!1!==i.dtxEnabled,r._enableVertexWelding=!1,r._enableIndexBucketing=!0,r._vboBatchingLayerScratchMemory=eo(),r._textureTranscoder=i.textureTranscoder||Ec(r.scene.viewer),r._maxGeometryBatchSize=i.maxGeometryBatchSize,r._aabb=Z.collapseAABB3(),r._aabbDirty=!0,r._quantizationRanges={},r._vboInstancingLayers={},r._vboBatchingLayers={},r._dtxLayers={},r._meshList=[],r.layerList=[],r._entityList=[],r._geometries={},r._dtxBuckets={},r._textures={},r._textureSets={},r._meshes={},r._entities={},r._scheduledMeshes={},r.renderFlags=new ea,r.numGeometries=0,r.numPortions=0,r.numVisibleLayerPortions=0,r.numTransparentLayerPortions=0,r.numXRayedLayerPortions=0,r.numHighlightedLayerPortions=0,r.numSelectedLayerPortions=0,r.numEdgesLayerPortions=0,r.numPickableLayerPortions=0,r.numClippableLayerPortions=0,r.numCulledLayerPortions=0,r.numEntities=0,r._numTriangles=0,r._numLines=0,r._numPoints=0,r._edgeThreshold=i.edgeThreshold||10,r._origin=Z.vec3(i.origin||[0,0,0]),r._position=Z.vec3(i.position||[0,0,0]),r._rotation=Z.vec3(i.rotation||[0,0,0]),r._quaternion=Z.vec4(i.quaternion||[0,0,0,1]),r._conjugateQuaternion=Z.vec4(i.quaternion||[0,0,0,1]),i.rotation&&Z.eulerToQuaternion(r._rotation,"XYZ",r._quaternion),r._scale=Z.vec3(i.scale||[1,1,1]),r._worldRotationMatrix=Z.mat4(),r._worldRotationMatrixConjugate=Z.mat4(),r._matrix=Z.mat4(),r._matrixDirty=!0,r._rebuildMatrices(),r._worldNormalMatrix=Z.mat4(),Z.inverseMat4(r._matrix,r._worldNormalMatrix),Z.transposeMat4(r._worldNormalMatrix),(i.matrix||i.position||i.rotation||i.scale||i.quaternion)&&(r._viewMatrix=Z.mat4(),r._viewNormalMatrix=Z.mat4(),r._viewMatrixDirty=!0,r._matrixNonIdentity=!0),r._opacity=1,r._colorize=[1,1,1],r._saoEnabled=!1!==i.saoEnabled,r._pbrEnabled=!1!==i.pbrEnabled,r._colorTextureEnabled=!1!==i.colorTextureEnabled,r._isModel=i.isModel,r._isModel&&r.scene._registerModel(g(r)),r._onCameraViewMatrix=r.scene.camera.on("matrix",(function(){r._viewMatrixDirty=!0})),r.scene.vfc.enabled&&(r._vfcManager=r.scene.vfc.getVFCManager(g(r))),r._createDefaultTextureSet(),r.visible=i.visible,r.culled=i.culled,r.pickable=i.pickable,r.clippable=i.clippable,r.collidable=i.collidable,r.castsShadow=i.castsShadow,r.receivesShadow=i.receivesShadow,r.xrayed=i.xrayed,r.highlighted=i.highlighted,r.selected=i.selected,r.edges=i.edges,r.colorize=i.colorize,r.opacity=i.opacity,r.backfaces=i.backfaces,r}return P(n,[{key:"_createDefaultTextureSet",value:function(){var e=new pc({id:"defaultColorTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new pc({id:"defaultMetalRoughTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),n=new pc({id:"defaultNormalsTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),r=new pc({id:"defaultEmissiveTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new pc({id:"defaultOcclusionTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=n,this._textures.defaultEmissiveTexture=r,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new fc({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:n,emissiveTexture:r,occlusionTexture:i})}},{key:"isPerformanceModel",get:function(){return!0}},{key:"objects",get:function(){return this._entities}},{key:"origin",get:function(){return this._origin}},{key:"position",get:function(){return this._position},set:function(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"rotation",get:function(){return this._rotation},set:function(e){this._rotation.set(e||[0,0,0]),Z.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"quaternion",get:function(){return this._quaternion},set:function(e){this._quaternion.set(e||[0,0,0,1]),Z.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"scale",get:function(){return this._scale},set:function(e){}},{key:"matrix",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix},set:function(e){this._matrix.set(e||hp),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),Z.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),Z.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"rotationMatrix",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}},{key:"_rebuildMatrices",value:function(){this._matrixDirty&&(Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),Z.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),Z.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}},{key:"rotationMatrixConjugate",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}},{key:"_setWorldMatrixDirty",value:function(){this._matrixDirty=!0}},{key:"_setLocalAABBDirty",value:function(){for(var e=0,t=this._entityList.length;e0},set:function(e){e=!1!==e,this._visible=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._xrayed=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._highlighted=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._selected=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._edges=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!1!==e,this._pickable=e;for(var t=0,n=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){for(var n=e.colors,r=new Uint8Array(n.length),i=0,a=n.length;i0){var t=Z.collapseAABB3();Z.expandAABB3Points3(t,e.positionsCompressed),An.decompressAABB(t,e.positionsDecodeMatrix),Z.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){var n=Z.collapseAABB3();Z.expandAABB3Points3(n,e.positions),Z.AABB3ToOBB3(n,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}else this.error("[createMesh] SceneModel.createMesh() config missing: id")}},{key:"_createMesh",value:function(e){var t=new Js(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);var n=t.pickId,r=n>>24&255,i=n>>16&255,a=n>>8&255,s=255&n;switch(e.pickColor=new Uint8Array([s,a,i,r]),e.worldAABB=Z.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=Z.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,Z.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}},{key:"_getNumPrimitives",value:function(e){var t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(var n=0,r=e.buckets.length;n>>0).toString(16)}},{key:"_getVBOInstancingLayer",value:function(e){var t=this,n=e.origin,r=e.textureSetId||"-",i=e.geometryId,a="".concat(Math.round(n[0]),".").concat(Math.round(n[1]),".").concat(Math.round(n[2]),".").concat(r,".").concat(i),s=this._vboInstancingLayers[a];if(s)return s;for(var o=e.textureSet,l=e.geometry;!s;)switch(l.primitive){case"triangles":case"surface":console.log("[SceneModel ".concat(this.id,"]: creating TrianglesInstancingLayer")),s=new pu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0,solid:!1});break;case"solid":console.log("[SceneModel ".concat(this.id,"]: creating TrianglesInstancingLayer")),s=new pu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0,solid:!0});break;case"lines":console.log("[SceneModel ".concat(this.id,"]: creating LinesInstancingLayer")),s=new Nu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0});break;case"points":console.log("[SceneModel ".concat(this.id,"]: creating PointsInstancingLayer")),s=new cc({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0})}return this._vboInstancingLayers[a]=s,this.layerList.push(s),s}},{key:"createEntity",value:function(e){if(void 0===e.id?e.id=Z.createUUID():this.scene.components[e.id]&&(this.error("Scene already has a Component with this ID: ".concat(e.id," - will assign random ID")),e.id=Z.createUUID()),void 0!==e.meshIds){var t=0;if(this._visible&&!1!==e.visible&&(t|=Te),this._pickable&&!1!==e.pickable&&(t|=be),this._culled&&!1!==e.culled&&(t|=Ee),this._clippable&&!1!==e.clippable&&(t|=De),this._collidable&&!1!==e.collidable&&(t|=Pe),this._edges&&!1!==e.edges&&(t|=Be),this._xrayed&&!1!==e.xrayed&&(t|=Re),this._highlighted&&!1!==e.highlighted&&(t|=Ce),this._selected&&!1!==e.selected&&(t|=_e),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(var n=0,r=e.meshIds.length;nt.sortId?1:0}));for(var s=0,o=this.layerList.length;s0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}},{key:"_updateRenderFlagsVisibleLayers",value:function(){var e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(var t=0,n=this.layerList.length;t0)for(var a=0;a0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){var t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0)this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0));if(this.numSelectedLayerPortions>0){var n=this.scene.selectedMaterial._state;n.fill&&(n.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),n.edges&&(n.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){var r=this.scene.highlightMaterial._state;r.fill&&(r.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),r.edges&&(r.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}}},{key:"drawColorOpaque",value:function(e){for(var t=this.renderFlags,n=0,r=t.visibleLayers.length;n2&&void 0!==arguments[2]&&arguments[2],r=e.positionsCompressed||[],i=Bc(e.indices||[],t),a=Nc(e.edgeIndices||[]);function s(e,t){if(e>t){var n=e;e=t,t=n}function r(n,r){return n!==e?e-n:r!==t?t-r:0}for(var i=0,s=(a.length>>1)-1;i<=s;){var o=s+i>>1,l=r(a[2*o],a[2*o+1]);if(l>0)i=o+1;else{if(!(l<0))return o;s=o-1}}return-i-1}var o=new Int32Array(a.length/2);o.fill(0);var l=r.length/3;if(l>8*(1<p.maxNumPositions&&(p=f()),p.bucketNumber>8)return[e];-1===u[h]&&(u[h]=p.numPositions++,p.positionsCompressed.push(r[3*h]),p.positionsCompressed.push(r[3*h+1]),p.positionsCompressed.push(r[3*h+2])),-1===u[I]&&(u[I]=p.numPositions++,p.positionsCompressed.push(r[3*I]),p.positionsCompressed.push(r[3*I+1]),p.positionsCompressed.push(r[3*I+2])),-1===u[y]&&(u[y]=p.numPositions++,p.positionsCompressed.push(r[3*y]),p.positionsCompressed.push(r[3*y+1]),p.positionsCompressed.push(r[3*y+2])),p.indices.push(u[h]),p.indices.push(u[I]),p.indices.push(u[y]);var m=void 0;(m=s(h,I))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]])),(m=s(h,y))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]])),(m=s(I,y))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]]))}var w=t/8*2,g=t/8,T=2*r.length+(i.length+a.length)*w,E=0;return r.length,c.forEach((function(e){E+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*g,e.positionsCompressed.length})),E>T?[e]:(n&&Lc(c,e),c)}({positionsCompressed:r,indices:i,edgeIndices:a},r.length/3>65536?16:8):s=[{positionsCompressed:r,indices:i,edgeIndices:a}];return s}var wp=function(e){I(n,yp);var t=m(n);function n(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),t.call(this,e,r)}return P(n)}(),gp=function(e){I(n,ve);var t=m(n);function n(e){var r,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),(r=t.call(this,e,i))._skyboxMesh=new fa(g(r),{geometry:new hn(g(r),{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new gn(g(r),{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new Qa(g(r),{src:i.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:i.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),r.size=i.size,r.active=i.active,r}return P(n,[{key:"size",get:function(){return this._size},set:function(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}},{key:"active",get:function(){return this._skyboxMesh.visible},set:function(e){this._skyboxMesh.visible=e}}]),n}(),Tp=function(){function e(){b(this,e)}return P(e,[{key:"transcode",value:function(e,t){}},{key:"destroy",value:function(){}}]),e}(),Ep=Z.vec4(),bp=Z.vec4(),Dp=Z.vec3(),Pp=Z.vec3(),Rp=Z.vec3(),Cp=Z.vec4(),_p=Z.vec4(),Bp=Z.vec4(),Op=function(){function e(t){b(this,e),this._scene=t}return P(e,[{key:"dollyToCanvasPos",value:function(e,t,n){var r=!1,i=this._scene.camera;if(e){var a=Z.subVec3(e,i.eye,Dp);r=Z.lenVec3(a)0&&void 0!==arguments[0])||arguments[0];I.style.cursor="move",w(),e&&g()}function w(){c=i.pointerCanvasPos[0],f=i.pointerCanvasPos[1],p=i.pointerCanvasPos[0],A=i.pointerCanvasPos[1]}function g(){u.pickCursorPos=i.pointerCanvasPos,u.schedulePickSurface=!0,u.update(),u.picked&&u.pickedSurface&&u.pickResult&&u.pickResult.worldPos?(d=!0,v.set(u.pickResult.worldPos)):d=!1}document.addEventListener("keydown",this._documentKeyDownHandler=function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled){var n=e.keyCode;y[n]=!0}}),document.addEventListener("keyup",this._documentKeyUpHandler=function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled){var n=e.keyCode;y[n]=!1}}),I.addEventListener("mousedown",this._mouseDownHandler=function(e){if(r.active&&r.pointerEnabled)switch(e.which){case 1:y[t.input.KEY_SHIFT]||r.planView?(s=!0,m()):(s=!0,m(!1));break;case 2:o=!0,m();break;case 3:l=!0,r.panRightClick&&m()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=function(){if(r.active&&r.pointerEnabled&&(s||o||l)){var e=t.canvas.boundary,n=e[2],u=e[3],p=i.pointerCanvasPos[0],A=i.pointerCanvasPos[1];if(y[t.input.KEY_SHIFT]||r.planView||!r.panRightClick&&o||r.panRightClick&&l){var h=p-c,I=A-f,m=t.camera;if("perspective"===m.projection){var w=Math.abs(d?Z.lenVec3(Z.subVec3(v,t.camera.eye,[])):t.camera.eyeLookDist)*Math.tan(m.perspective.fov/2*Math.PI/180);a.panDeltaX+=1.5*h*w/u,a.panDeltaY+=1.5*I*w/u}else a.panDeltaX+=.5*m.ortho.scale*(h/u),a.panDeltaY+=.5*m.ortho.scale*(I/u)}else!s||o||l||r.planView||(r.firstPerson?(a.rotateDeltaY-=(p-c)/n*r.dragRotationRate/2,a.rotateDeltaX+=(A-f)/u*(r.dragRotationRate/4)):(a.rotateDeltaY-=(p-c)/n*(1.5*r.dragRotationRate),a.rotateDeltaX+=(A-f)/u*(1.5*r.dragRotationRate)));c=p,f=A}}),I.addEventListener("mousemove",this._canvasMouseMoveHandler=function(e){r.active&&r.pointerEnabled&&i.mouseover&&(h=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=function(e){if(r.active&&r.pointerEnabled)switch(e.which){case 1:case 2:case 3:s=!1,o=!1,l=!1}}),I.addEventListener("mouseup",this._mouseUpHandler=function(e){if(r.active&&r.pointerEnabled){if(3===e.which){!function(e,t){if(e){for(var n=e.target,r=0,i=0,a=0,s=0;n.offsetParent;)r+=n.offsetLeft,i+=n.offsetTop,a+=n.scrollLeft,s+=n.scrollTop,n=n.offsetParent;t[0]=e.pageX+a-r,t[1]=e.pageY+s-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,Gp);var t=Gp[0],i=Gp[1];Math.abs(t-p)<3&&Math.abs(i-A)<3&&n.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:Gp,event:e},!0)}I.style.removeProperty("cursor")}}),I.addEventListener("mouseenter",this._mouseEnterHandler=function(){r.active&&r.pointerEnabled});var T=1/60,E=null;I.addEventListener("wheel",this._mouseWheelHandler=function(e){if(r.active&&r.pointerEnabled){var t=performance.now()/1e3,n=null!==E?t-E:0;E=t,n>.05&&(n=.05),n0?n.cameraFlight.flyTo(Kp,(function(){n.pivotController.getPivoting()&&r.followPointer&&n.pivotController.showPivot()})):(n.cameraFlight.jumpTo(Kp),n.pivotController.getPivoting()&&r.followPointer&&n.pivotController.showPivot())}}}))}return P(e,[{key:"reset",value:function(){}},{key:"destroy",value:function(){this._scene.input.off(this._onSceneKeyDown)}}]),e}(),Xp=function(){function e(t,n,r,i,a){var s=this;b(this,e),this._scene=t;var o=n.pickController,l=n.pivotController,u=n.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;var c=!1,f=!1,p=this._scene.canvas.canvas,A=function(e){var r;e&&e.worldPos&&(r=e.worldPos);var i=e&&e.entity?e.entity.aabb:t.aabb;if(r){var a=t.camera;Z.subVec3(a.eye,a.look,[]),n.cameraFlight.flyTo({aabb:i})}else n.cameraFlight.flyTo({aabb:i})};p.addEventListener("mousemove",this._canvasMouseMoveHandler=function(e){if(r.active&&r.pointerEnabled&&!c&&!f){var n=u.hasSubs("hover"),a=u.hasSubs("hoverOut"),l=u.hasSubs("hoverOff"),p=u.hasSubs("hoverSurface"),A=u.hasSubs("hoverSnapOrSurface");if(n||a||l||p||A)if(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=!0,o.schedulePickSurface=p,o.scheduleSnapOrPick=A,o.update(),o.pickResult){if(o.pickResult.entity){var d=o.pickResult.entity.id;s._lastPickedEntityId!==d&&(void 0!==s._lastPickedEntityId&&u.fire("hoverOut",{entity:t.objects[s._lastPickedEntityId]},!0),u.fire("hoverEnter",o.pickResult,!0),s._lastPickedEntityId=d)}u.fire("hover",o.pickResult,!0),(o.pickResult.worldPos||o.pickResult.snappedWorldPos)&&u.fire("hoverSurface",o.pickResult,!0)}else void 0!==s._lastPickedEntityId&&(u.fire("hoverOut",{entity:t.objects[s._lastPickedEntityId]},!0),s._lastPickedEntityId=void 0),u.fire("hoverOff",{canvasPos:o.pickCursorPos},!0)}}),p.addEventListener("mousedown",this._canvasMouseDownHandler=function(e){if(1===e.which&&(c=!0),3===e.which&&(f=!0),1===e.which&&r.active&&r.pointerEnabled&&(i.mouseDownClientX=e.clientX,i.mouseDownClientY=e.clientY,i.mouseDownCursorX=i.pointerCanvasPos[0],i.mouseDownCursorY=i.pointerCanvasPos[1],!r.firstPerson&&r.followPointer&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickSurface=!0,o.update(),1===e.which))){var n=o.pickResult;n&&n.worldPos?(l.setPivotPos(n.worldPos),l.startPivot()):(r.smartPivot?l.setCanvasPivotPos(i.pointerCanvasPos):l.setPivotPos(t.camera.look),l.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=function(e){1===e.which&&(c=!1),3===e.which&&(f=!1)}),p.addEventListener("mouseup",this._canvasMouseUpHandler=function(e){if(r.active&&r.pointerEnabled&&(1===e.which&&(l.hidePivot(),!(Math.abs(e.clientX-i.mouseDownClientX)>3||Math.abs(e.clientY-i.mouseDownClientY)>3)))){var a=u.hasSubs("picked"),c=u.hasSubs("pickedNothing"),f=u.hasSubs("pickedSurface"),p=u.hasSubs("doublePicked"),d=u.hasSubs("doublePickedSurface"),v=u.hasSubs("doublePickedNothing");if(!(r.doublePickFlyTo||p||d||v))return(a||c||f)&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=!0,o.schedulePickSurface=f,o.update(),o.pickResult?(u.fire("picked",o.pickResult,!0),o.pickedSurface&&u.fire("pickedSurface",o.pickResult,!0)):u.fire("pickedNothing",{canvasPos:i.pointerCanvasPos},!0)),void(s._clicks=0);if(s._clicks++,1===s._clicks){o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=r.doublePickFlyTo,o.schedulePickSurface=f,o.update();var h=o.pickResult,I=o.pickedSurface;s._timeout=setTimeout((function(){h?(u.fire("picked",h,!0),I&&(u.fire("pickedSurface",h,!0),!r.firstPerson&&r.followPointer&&(n.pivotController.setPivotPos(h.worldPos),n.pivotController.startPivot()&&n.pivotController.showPivot()))):u.fire("pickedNothing",{canvasPos:i.pointerCanvasPos},!0),s._clicks=0}),r.doubleClickTimeFrame)}else{if(null!==s._timeout&&(window.clearTimeout(s._timeout),s._timeout=null),o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=r.doublePickFlyTo||p||d,o.schedulePickSurface=o.schedulePickEntity&&d,o.update(),o.pickResult){if(u.fire("doublePicked",o.pickResult,!0),o.pickedSurface&&u.fire("doublePickedSurface",o.pickResult,!0),r.doublePickFlyTo&&(A(o.pickResult),!r.firstPerson&&r.followPointer)){var y=o.pickResult.entity.aabb,m=Z.getAABB3Center(y);n.pivotController.setPivotPos(m),n.pivotController.startPivot()&&n.pivotController.showPivot()}}else if(u.fire("doublePickedNothing",{canvasPos:i.pointerCanvasPos},!0),r.doublePickFlyTo&&(A(),!r.firstPerson&&r.followPointer)){var w=t.aabb,g=Z.getAABB3Center(w);n.pivotController.setPivotPos(g),n.pivotController.startPivot()&&n.pivotController.showPivot()}s._clicks=0}}},!1)}return P(e,[{key:"reset",value:function(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}},{key:"destroy",value:function(){var e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}]),e}(),qp=function(){function e(t,n,r,i,a){b(this,e),this._scene=t;var s=t.input,o=[],l=t.canvas.canvas,u=!0;this._onSceneMouseMove=s.on("mousemove",(function(){u=!0})),this._onSceneKeyDown=s.on("keydown",(function(e){r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&i.mouseover&&(o[e]=!0,e===s.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=s.on("keyup",(function(e){r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&(o[e]=!1,e===s.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=t.on("tick",(function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&i.mouseover){var l=n.cameraControl,c=e.deltaTime/1e3;if(!r.planView){var f=l._isKeyDownForAction(l.ROTATE_Y_POS,o),p=l._isKeyDownForAction(l.ROTATE_Y_NEG,o),A=l._isKeyDownForAction(l.ROTATE_X_POS,o),d=l._isKeyDownForAction(l.ROTATE_X_NEG,o),v=c*r.keyboardRotationRate;(f||p||A||d)&&(!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),f?a.rotateDeltaY+=v:p&&(a.rotateDeltaY-=v),A?a.rotateDeltaX+=v:d&&(a.rotateDeltaX-=v),!r.firstPerson&&r.followPointer&&n.pivotController.startPivot())}if(!o[s.KEY_CTRL]&&!o[s.KEY_ALT]){var h=l._isKeyDownForAction(l.DOLLY_BACKWARDS,o),I=l._isKeyDownForAction(l.DOLLY_FORWARDS,o);if(h||I){var y=c*r.keyboardDollyRate;!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),I?a.dollyDelta-=y:h&&(a.dollyDelta+=y),u&&(i.followPointerDirty=!0,u=!1)}}var m=l._isKeyDownForAction(l.PAN_FORWARDS,o),w=l._isKeyDownForAction(l.PAN_BACKWARDS,o),g=l._isKeyDownForAction(l.PAN_LEFT,o),T=l._isKeyDownForAction(l.PAN_RIGHT,o),E=l._isKeyDownForAction(l.PAN_UP,o),b=l._isKeyDownForAction(l.PAN_DOWN,o),D=(o[s.KEY_ALT]?.3:1)*c*r.keyboardPanRate;(m||w||g||T||E||b)&&(!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),b?a.panDeltaY+=D:E&&(a.panDeltaY+=-D),T?a.panDeltaX+=-D:g&&(a.panDeltaX+=D),w?a.panDeltaZ+=D:m&&(a.panDeltaZ+=-D))}}))}return P(e,[{key:"reset",value:function(){}},{key:"destroy",value:function(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}]),e}(),Jp=Z.vec3(),Zp=function(){function e(t,n,r,i,a){b(this,e),this._scene=t;var s=t.camera,o=n.pickController,l=n.pivotController,u=n.panController,c=1,f=1,p=null;this._onTick=t.on("tick",(function(){if(r.active&&r.pointerEnabled){var e="default";if(Math.abs(a.dollyDelta)<.001&&(a.dollyDelta=0),Math.abs(a.rotateDeltaX)<.001&&(a.rotateDeltaX=0),Math.abs(a.rotateDeltaY)<.001&&(a.rotateDeltaY=0),0===a.rotateDeltaX&&0===a.rotateDeltaY||(a.dollyDelta=0),r.followPointer&&--c<=0&&(c=1,0!==a.dollyDelta)){if(0===a.rotateDeltaY&&0===a.rotateDeltaX&&r.followPointer&&i.followPointerDirty&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickSurface=!0,o.update(),o.pickResult&&o.pickResult.worldPos?p=o.pickResult.worldPos:(f=1,p=null),i.followPointerDirty=!1),p){var n=Math.abs(Z.lenVec3(Z.subVec3(p,t.camera.eye,Jp)));f=n/r.dollyProximityThreshold}fr.longTapRadius||Math.abs(I)>r.longTapRadius)&&(clearTimeout(i.longTouchTimeout),i.longTouchTimeout=null),r.planView){var y=t.camera;if("perspective"===y.projection){var m=Math.abs(t.camera.eyeLookDist)*Math.tan(y.perspective.fov/2*Math.PI/180);a.panDeltaX+=h*m/l*r.touchPanRate,a.panDeltaY+=I*m/l*r.touchPanRate}else a.panDeltaX+=.5*y.ortho.scale*(h/l)*r.touchPanRate,a.panDeltaY+=.5*y.ortho.scale*(I/l)*r.touchPanRate}else a.rotateDeltaY-=h/o*(1*r.dragRotationRate),a.rotateDeltaX+=I/l*(1.5*r.dragRotationRate)}else if(2===d){var w=A[0],g=A[1];tA(w,u),tA(g,c);var T=Z.geometricMeanVec2(p[0],p[1]),E=Z.geometricMeanVec2(u,c),b=Z.vec2();Z.subVec2(T,E,b);var D=b[0],P=b[1],R=t.camera,C=Z.distVec2([w.pageX,w.pageY],[g.pageX,g.pageY]),_=(Z.distVec2(p[0],p[1])-C)*r.touchDollyRate;if(a.dollyDelta=_,Math.abs(_)<1)if("perspective"===R.projection){var B=s.pickResult?s.pickResult.worldPos:t.center,O=Math.abs(Z.lenVec3(Z.subVec3(B,t.camera.eye,[])))*Math.tan(R.perspective.fov/2*Math.PI/180);a.panDeltaX-=D*O/l*r.touchPanRate,a.panDeltaY-=P*O/l*r.touchPanRate}else a.panDeltaX-=.5*R.ortho.scale*(D/l)*r.touchPanRate,a.panDeltaY-=.5*R.ortho.scale*(P/l)*r.touchPanRate;i.pointerCanvasPos=E}for(var S=0;S-1&&t-f<150&&(p>-1&&f-p<325?(rA(a[0],o.pickCursorPos),o.schedulePickEntity=!0,o.schedulePickSurface=s,o.update(),o.pickResult?(o.pickResult.touchInput=!0,l.fire("doublePicked",o.pickResult),o.pickedSurface&&l.fire("doublePickedSurface",o.pickResult),r.doublePickFlyTo&&d(o.pickResult)):(l.fire("doublePickedNothing"),r.doublePickFlyTo&&d()),p=-1):Z.distVec2(u[0],c)<4&&(rA(a[0],o.pickCursorPos),o.schedulePickEntity=!0,o.schedulePickSurface=s,o.update(),o.pickResult?(o.pickResult.touchInput=!0,l.fire("picked",o.pickResult),o.pickedSurface&&l.fire("pickedSurface",o.pickResult)):l.fire("pickedNothing"),p=t),f=-1),u.length=n.length;for(var A=0,v=n.length;A1&&void 0!==arguments[1]?arguments[1]:{};b(this,n),(r=t.call(this,e,i)).PAN_LEFT=0,r.PAN_RIGHT=1,r.PAN_UP=2,r.PAN_DOWN=3,r.PAN_FORWARDS=4,r.PAN_BACKWARDS=5,r.ROTATE_X_POS=6,r.ROTATE_X_NEG=7,r.ROTATE_Y_POS=8,r.ROTATE_Y_NEG=9,r.DOLLY_FORWARDS=10,r.DOLLY_BACKWARDS=11,r.AXIS_VIEW_RIGHT=12,r.AXIS_VIEW_BACK=13,r.AXIS_VIEW_LEFT=14,r.AXIS_VIEW_FRONT=15,r.AXIS_VIEW_TOP=16,r.AXIS_VIEW_BOTTOM=17,r._keyMap={},r.scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},r._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},r._states={pointerCanvasPos:Z.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:Z.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},r._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};var a=r.scene;return r._controllers={cameraControl:g(r),pickController:new Up(g(r),r._configs),pivotController:new Hp(a,r._configs),panController:new Op(a),cameraFlight:new _s(g(r),{duration:.5})},r._handlers=[new $p(r.scene,r._controllers,r._configs,r._states,r._updates),new nA(r.scene,r._controllers,r._configs,r._states,r._updates),new kp(r.scene,r._controllers,r._configs,r._states,r._updates),new Yp(r.scene,r._controllers,r._configs,r._states,r._updates),new Xp(r.scene,r._controllers,r._configs,r._states,r._updates),new iA(r.scene,r._controllers,r._configs,r._states,r._updates),new qp(r.scene,r._controllers,r._configs,r._states,r._updates)],r._cameraUpdater=new Zp(r.scene,r._controllers,r._configs,r._states,r._updates),r.navMode=i.navMode,i.planView&&(r.planView=i.planView),r.constrainVertical=i.constrainVertical,i.keyboardLayout?r.keyboardLayout=i.keyboardLayout:r.keyMap=i.keyMap,r.doublePickFlyTo=i.doublePickFlyTo,r.panRightClick=i.panRightClick,r.active=i.active,r.followPointer=i.followPointer,r.rotationInertia=i.rotationInertia,r.keyboardPanRate=i.keyboardPanRate,r.touchPanRate=i.touchPanRate,r.keyboardRotationRate=i.keyboardRotationRate,r.dragRotationRate=i.dragRotationRate,r.touchDollyRate=i.touchDollyRate,r.dollyInertia=i.dollyInertia,r.dollyProximityThreshold=i.dollyProximityThreshold,r.dollyMinSpeed=i.dollyMinSpeed,r.panInertia=i.panInertia,r.pointerEnabled=!0,r.keyboardDollyRate=i.keyboardDollyRate,r.mouseWheelDollyRate=i.mouseWheelDollyRate,r}return P(n,[{key:"keyMap",get:function(){return this._keyMap},set:function(e){if(e=e||"qwerty",ae.isString(e)){var t=this.scene.input,n={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":n[this.PAN_LEFT]=[t.KEY_A],n[this.PAN_RIGHT]=[t.KEY_D],n[this.PAN_UP]=[t.KEY_Z],n[this.PAN_DOWN]=[t.KEY_X],n[this.PAN_BACKWARDS]=[],n[this.PAN_FORWARDS]=[],n[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],n[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],n[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],n[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],n[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],n[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],n[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],n[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],n[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],n[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],n[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],n[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":n[this.PAN_LEFT]=[t.KEY_Q],n[this.PAN_RIGHT]=[t.KEY_D],n[this.PAN_UP]=[t.KEY_W],n[this.PAN_DOWN]=[t.KEY_X],n[this.PAN_BACKWARDS]=[],n[this.PAN_FORWARDS]=[],n[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],n[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],n[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],n[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],n[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],n[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],n[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],n[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],n[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],n[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],n[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],n[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=n}else{var r=e;this._keyMap=r}}},{key:"_isKeyDownForAction",value:function(e,t){var n=this._keyMap[e];if(!n)return!1;t||(t=this.scene.input.keyDown);for(var r=0,i=n.length;r1&&void 0!==arguments[1]?arguments[1]:{};if(this.finalized)throw"MetaScene already finalized - can't add more data";this._globalizeIDs(e,t);var n=this.metaScene;if(e.propertySets)for(var r=0,i=e.propertySets.length;r0?fA(t):null,s=n&&n.length>0?fA(n):null;return function e(t){if(t){var n=!0;(s&&s[t.type]||a&&!a[t.type])&&(n=!1),n&&r.push(t.id);var i=t.children;if(i)for(var o=0,l=i.length;o>t;n.sort(_c);for(var o=new Int32Array(e.length),l=0,u=n.length;le[i+1]){var s=e[i];e[i]=e[i+1],e[i+1]=s}Oc=new Int32Array(e),t.sort(Sc);for(var o=new Int32Array(e.length),l=0,u=t.length;l0)for(var g=r._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl,n=e._lightsState;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var r=this._program;this._uRenderPass=r.getLocation("renderPass"),this._uLightAmbient=r.getLocation("lightAmbient"),this._uLightColor=[],this._uLightDir=[],this._uLightPos=[],this._uLightAttenuation=[];for(var i=n.lights,a=0,s=i.length;a0,a=[];a.push("#version 300 es"),a.push("// TrianglesDataTextureColorRenderer vertex shader"),a.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),a.push("precision highp float;"),a.push("precision highp int;"),a.push("precision highp usampler2D;"),a.push("precision highp isampler2D;"),a.push("precision highp sampler2D;"),a.push("#else"),a.push("precision mediump float;"),a.push("precision mediump int;"),a.push("precision mediump usampler2D;"),a.push("precision mediump isampler2D;"),a.push("precision mediump sampler2D;"),a.push("#endif"),a.push("uniform int renderPass;"),t.entityOffsetsEnabled&&a.push("in vec3 offset;"),a.push("uniform mat4 sceneModelWorldMatrix;"),a.push("uniform mat4 viewMatrix;"),a.push("uniform mat4 projMatrix;"),a.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),a.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),a.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),a.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),a.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),a.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),a.push("uniform highp sampler2D uTextureModelMatrices;"),a.push("uniform vec3 uCameraEyeRtc;"),a.push("vec3 positions[3];"),t.logarithmicDepthBufferEnabled&&(a.push("uniform float logDepthBufFC;"),a.push("out float vFragDepth;"),a.push("out float isPerspective;")),a.push("bool isPerspectiveMatrix(mat4 m) {"),a.push(" return (m[2][3] == - 1.0);"),a.push("}"),a.push("uniform vec4 lightAmbient;");for(var s=0,o=r.lights.length;s> 3) & 4095;"),a.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),a.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),a.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),a.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),a.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),a.push("if (int(flags.x) != renderPass) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("} else {"),a.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),a.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),a.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),a.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),a.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),a.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),a.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),a.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),a.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),a.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),a.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),a.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),a.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),a.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),a.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),a.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),a.push("if (color.a == 0u) {"),a.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),a.push(" return;"),a.push("};"),a.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),a.push("vec3 position;"),a.push("position = positions[gl_VertexID % 3];"),a.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),a.push("if (solid != 1u) {"),a.push("if (isPerspectiveMatrix(projMatrix)) {"),a.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),a.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("} else {"),a.push("if (viewNormal.z < 0.0) {"),a.push("position = positions[2 - (gl_VertexID % 3)];"),a.push("viewNormal = -viewNormal;"),a.push("}"),a.push("}"),a.push("}"),a.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),a.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),a.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),a.push("vec4 viewPosition = viewMatrix * worldPosition; "),a.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);"),a.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);"),a.push("float lambertian = 1.0;");for(var l=0,u=r.lights.length;l0,r=[];if(r.push("#version 300 es"),r.push("// TrianglesDataTextureColorRenderer fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),this._withSAO&&(r.push("uniform sampler2D uOcclusionTexture;"),r.push("uniform vec4 uSAOParams;"),r.push("const float packUpscale = 256. / 255.;"),r.push("const float unpackDownScale = 255. / 256.;"),r.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );"),r.push("const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. );"),r.push("float unpackRGBToFloat( const in vec4 v ) {"),r.push(" return dot( v, unPackFactors );"),r.push("}")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),this._withSAO?(r.push(" float viewportWidth = uSAOParams[0];"),r.push(" float viewportHeight = uSAOParams[1];"),r.push(" float blendCutoff = uSAOParams[2];"),r.push(" float blendFactor = uSAOParams[3];"),r.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);"),r.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;"),r.push(" outColor = vec4(vColor.rgb * ambient, 1.0);")):r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),jc=new Float32Array([1,1,1]),Vc=Z.vec3(),Qc=Z.vec3(),Wc=Z.vec3(),zc=Z.vec3(),Kc=Z.mat4(),Yc=function(){function e(t,n){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=this._scene,i=r.camera,a=t.model,s=r.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=a.position,f=a.rotationMatrix,p=a.rotationMatrixConjugate,A=i.viewMatrix;if(this._program||(this._allocate(),!this.errors)){var d,v;if(e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,o)),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),u||0!==c[0]||0!==c[1]||0!==c[2]){var h=Vc;if(u){var I=Qc;Z.transformPoint3(f,u,I),h[0]=I[0],h[1]=I[1],h[2]=I[2]}else h[0]=0,h[1]=0,h[2]=0;h[0]+=c[0],h[1]+=c[1],h[2]+=c[2],d=Ie(A,h,Kc),(v=Wc)[0]=i.eye[0]-h[0],v[1]=i.eye[1]-h[1],v[2]=i.eye[2]-h[2]}else d=A,v=i.eye;if(s.uniform3fv(this._uCameraEyeRtc,v),s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),n===to.SILHOUETTE_XRAYED){var y=r.xrayMaterial._state,m=y.fillColor,w=y.fillAlpha;s.uniform4f(this._uColor,m[0],m[1],m[2],w)}else if(n===to.SILHOUETTE_HIGHLIGHTED){var g=r.highlightMaterial._state,T=g.fillColor,E=g.fillAlpha;s.uniform4f(this._uColor,T[0],T[1],T[2],E)}else if(n===to.SILHOUETTE_SELECTED){var b=r.selectedMaterial._state,D=b.fillColor,P=b.fillAlpha;s.uniform4f(this._uColor,D[0],D[1],D[2],P)}else s.uniform4fv(this._uColor,jc);if(r.logarithmicDepthBufferEnabled){var R=2/(Math.log(e.pickZFar+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,R)}var C=r._sectionPlanesState.sectionPlanes.length;if(C>0)for(var _=r._sectionPlanesState.sectionPlanes,B=t.layerIndex*C,O=a.renderFlags,S=0;S0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uColor=n.getLocation("color"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture silhouette vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.y) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("} else {"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("viewNormal = -viewNormal;"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("gl_Position = clipPos;"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture draw fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = color;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Xc=new Float32Array([0,0,0,1]),qc=Z.vec3(),Jc=Z.vec3();Z.vec3();var Zc=Z.vec3(),$c=Z.mat4(),ef=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=r.position,f=r.rotationMatrix,p=r.rotationMatrixConjugate,A=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=qc;if(v){var y=Jc;Z.transformPoint3(f,u,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],d=Ie(A,I,$c)}else d=A;if(s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),n===to.EDGES_XRAYED){var m=i.xrayMaterial._state,w=m.edgeColor,g=m.edgeAlpha;s.uniform4f(this._uColor,w[0],w[1],w[2],g)}else if(n===to.EDGES_HIGHLIGHTED){var T=i.highlightMaterial._state,E=T.edgeColor,b=T.edgeAlpha;s.uniform4f(this._uColor,E[0],E[1],E[2],b)}else if(n===to.EDGES_SELECTED){var D=i.selectedMaterial._state,P=D.edgeColor,R=D.edgeAlpha;s.uniform4f(this._uColor,P[0],P[1],P[2],R)}else s.uniform4fv(this._uColor,Xc);var C=i._sectionPlanesState.sectionPlanes.length;if(C>0)for(var _=i._sectionPlanesState.sectionPlanes,B=t.layerIndex*C,O=r.renderFlags,S=0;S0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),s.drawArrays(s.LINES,0,o.numEdgeIndices8Bits)),o.numEdgeIndices16Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),s.drawArrays(s.LINES,0,o.numEdgeIndices16Bits)),o.numEdgeIndices32Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),s.drawArrays(s.LINES,0,o.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uColor=n.getLocation("color"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec4 color;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vColor;"),n.push("void main(void) {"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.z) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("vColor = vec4(color.r, color.g, color.b, color.a);"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { discard; }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),tf=Z.vec3(),nf=Z.vec3(),rf=Z.vec3(),af=Z.mat4(),sf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=r.position,f=r.rotationMatrix,p=r.rotationMatrixConjugate,A=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=tf;if(v){var y=nf;Z.transformPoint3(f,u,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],d=Ie(A,I,af)}else d=A;s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,d),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix);var m=i._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=i._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=r.renderFlags,E=0;E0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),s.drawArrays(s.LINES,0,o.numEdgeIndices8Bits)),o.numEdgeIndices16Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),s.drawArrays(s.LINES,0,o.numEdgeIndices16Bits)),o.numEdgeIndices32Bits>0&&(l.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),s.drawArrays(s.LINES,0,o.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vColor;"),n.push("void main(void) {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.z) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("vec4 rgb = vec4(color.rgba);"),n.push("vColor = vec4(float(rgb.r*0.5) / 255.0, float(rgb.g*0.5) / 255.0, float(rgb.b*0.5) / 255.0, float(rgb.a) / 255.0);"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry edges drawing fragment shader"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { discard; }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outColor = vColor;"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),of=Z.vec3(),lf=Z.vec3(),uf=Z.vec3(),cf=Z.mat4(),ff=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){if(this._program||(this._allocate(t),!this.errors)){e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e));var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate;c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==f[0]||0!==f[1]||0!==f[2],h=0!==p[0]||0!==p[1]||0!==p[2];if(v||h){var I=of;if(v){var y=Z.transformPoint3(A,f,lf);I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=p[0],I[1]+=p[1],I[2]+=p[2],r=Ie(o.viewMatrix,I,cf),(i=uf)[0]=o.eye[0]-I[0],i[1]=o.eye[1]-I[1],i[2]=o.eye[2]-I[2]}else r=o.viewMatrix,i=o.eye;if(l.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),l.uniform3fv(this._uCameraEyeRtc,i),l.uniform1i(this._uRenderPass,n),s.logarithmicDepthBufferEnabled){var m=2/(Math.log(o.project.far+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,m)}var w=s._sectionPlanesState.sectionPlanes.length;if(w>0)for(var g=s._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry picking vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("smooth out vec4 vWorldPosition;"),n.push("flat out uvec4 vFlags2;")),n.push("out vec4 vPickColor;"),n.push("void main(void) {"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vPickColor = vec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0)) / 255.0;"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry picking fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uvec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outPickColor = vPickColor; "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),pf=Z.vec3(),Af=Z.vec3(),df=Z.vec3(),vf=Z.vec3(),hf=Z.mat4(),If=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=e.pickViewMatrix||o.viewMatrix;if(this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets),o.eye,f||0!==p[0]||0!==p[1]||0!==p[2]){var h=pf;if(f){var I=Af;Z.transformPoint3(A,f,I),h[0]=I[0],h[1]=I[1],h[2]=I[2]}else h[0]=0,h[1]=0,h[2]=0;h[0]+=p[0],h[1]+=p[1],h[2]+=p[2],r=Ie(v,h,hf),(i=df)[0]=o.eye[0]-h[0],i[1]=o.eye[1]-h[1],i[2]=o.eye[2]-h[2],e.snapPickOrigin[0]=h[0],e.snapPickOrigin[1]=h[1],e.snapPickOrigin[2]=h[2]}else r=v,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniform1f(this._uPickZNear,e.pickZNear),l.uniform1f(this._uPickZFar,e.pickZFar),l.uniformMatrix4fv(this._uWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var y=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,y)}var m=s._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=s._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=a.renderFlags,E=0;E0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick depth vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vViewPosition;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("}"),n.push("}"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vViewPosition = viewPosition;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick depth fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform float pickZNear;"),r.push("uniform float pickZFar;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));"),r.push(" outPackedDepth = packDepth(zNormalizedDepth); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),yf=Z.vec3(),mf=Z.vec3(),wf=Z.vec3(),gf=Z.vec3(),Tf=Z.vec3(),Ef=Z.mat4(),bf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){if(this._program||(this._allocate(),!this.errors)){e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=t.aabb,h=e.pickViewMatrix||o.viewMatrix,I=yf;I[0]=Z.safeInv(v[3]-v[0])*Z.MAX_INT,I[1]=Z.safeInv(v[4]-v[1])*Z.MAX_INT,I[2]=Z.safeInv(v[5]-v[2])*Z.MAX_INT,e.snapPickCoordinateScale[0]=Z.safeInv(I[0]),e.snapPickCoordinateScale[1]=Z.safeInv(I[1]),e.snapPickCoordinateScale[2]=Z.safeInv(I[2]),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var y=0!==f[0]||0!==f[1]||0!==f[2],m=0!==p[0]||0!==p[1]||0!==p[2];if(y||m){var w=mf;if(y){var g=Z.transformPoint3(A,f,wf);w[0]=g[0],w[1]=g[1],w[2]=g[2]}else w[0]=0,w[1]=0,w[2]=0;w[0]+=p[0],w[1]+=p[1],w[2]+=p[2],r=Ie(h,w,Ef),(i=gf)[0]=o.eye[0]-w[0],i[1]=o.eye[1]-w[1],i[2]=o.eye[2]-w[2],e.snapPickOrigin[0]=w[0],e.snapPickOrigin[1]=w[1],e.snapPickOrigin[2]=w[2]}else r=h,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform2fv(this.uVectorA,e.snapVectorA),l.uniform2fv(this.uInverseVectorAB,e.snapInvVectorAB),l.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),l.uniform3fv(this._uCoordinateScaler,I),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var T=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,T)}var E=s._sectionPlanesState.sectionPlanes.length;if(E>0)for(var b=s._sectionPlanesState.sectionPlanes,D=t.layerIndex*E,P=a.renderFlags,R=0;R0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,8),l.drawArrays(S,0,u.numEdgeIndices8Bits)),u.numEdgeIndices16Bits>0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,16),l.drawArrays(S,0,u.numEdgeIndices16Bits)),u.numEdgeIndices32Bits>0&&(c.bindEdgeIndicesTextures(this._program,this._uTexturePerEdgeIdPortionIds,this._uTexturePerPolygonIdEdgeIndices,32),l.drawArrays(S,0,u.numEdgeIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Batched geometry edges drawing vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdEdgeIndices;"),n.push("uniform mediump usampler2D uTexturePerEdgeIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("uniform vec2 uSnapVectorA;"),n.push("uniform vec2 uSnapInvVectorAB;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),n.push("vec2 remapClipPos(vec2 clipPos) {"),n.push(" float x = (clipPos.x - uSnapVectorA.x) * uSnapInvVectorAB.x;"),n.push(" float y = (clipPos.y - uSnapVectorA.y) * uSnapInvVectorAB.y;"),n.push(" return vec2(x, y);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec4 vViewPosition;"),n.push("out highp vec3 relativeToOriginPosition;"),n.push("void main(void) {"),n.push("int edgeIndex = gl_VertexID / 2;"),n.push("int h_packed_object_id_index = (edgeIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (edgeIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerEdgeIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("{"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedEdgeIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0));"),n.push("int edgeIndexBaseOffset = (packedEdgeIndexBaseOffset.r << 24) + (packedEdgeIndexBaseOffset.g << 16) + (packedEdgeIndexBaseOffset.b << 8) + packedEdgeIndexBaseOffset.a;"),n.push("int h_index = (edgeIndex - edgeIndexBaseOffset) & 4095;"),n.push("int v_index = (edgeIndex - edgeIndexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdEdgeIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("int indexPositionH = uniqueVertexIndexes[gl_VertexID % 2] & 4095;"),n.push("int indexPositionV = uniqueVertexIndexes[gl_VertexID % 2] >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH, indexPositionV), 0));"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("relativeToOriginPosition = worldPosition.xyz;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.r;")),n.push("vViewPosition = viewPosition;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),n.push("float tmp = clipPos.w;"),n.push("clipPos.xyzw /= tmp;"),n.push("clipPos.xy = remapClipPos(clipPos.xy);"),n.push("clipPos.xyzw *= tmp;"),n.push("vViewPosition = clipPos;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push("gl_PointSize = 1.0;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick depth fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform int uLayerNumber;"),r.push("uniform vec3 uCoordinateScaler;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0;s 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, uLayerNumber);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Df=Z.vec3(),Pf=Z.vec3(),Rf=Z.vec3(),Cf=Z.vec3(),_f=Z.vec3(),Bf=Z.mat4(),Of=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){this._program||this._allocate(),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram());var r,i,a=t.model,s=a.scene,o=s.camera,l=s.canvas.gl,u=t._state,c=u.textureState,f=t._state.origin,p=a.position,A=a.rotationMatrix,d=a.rotationMatrixConjugate,v=t.aabb,h=e.pickViewMatrix||o.viewMatrix,I=Df;I[0]=Z.safeInv(v[3]-v[0])*Z.MAX_INT,I[1]=Z.safeInv(v[4]-v[1])*Z.MAX_INT,I[2]=Z.safeInv(v[5]-v[2])*Z.MAX_INT,e.snapPickCoordinateScale[0]=Z.safeInv(I[0]),e.snapPickCoordinateScale[1]=Z.safeInv(I[1]),e.snapPickCoordinateScale[2]=Z.safeInv(I[2]),c.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var y=0!==f[0]||0!==f[1]||0!==f[2],m=0!==p[0]||0!==p[1]||0!==p[2];if(y||m){var w=Pf;if(y){var g=Rf;Z.transformPoint3(A,f,g),w[0]=g[0],w[1]=g[1],w[2]=g[2]}else w[0]=0,w[1]=0,w[2]=0;w[0]+=p[0],w[1]+=p[1],w[2]+=p[2],r=Ie(h,w,Bf),(i=Cf)[0]=o.eye[0]-w[0],i[1]=o.eye[1]-w[1],i[2]=o.eye[2]-w[2],e.snapPickOrigin[0]=w[0],e.snapPickOrigin[1]=w[1],e.snapPickOrigin[2]=w[2]}else r=h,i=o.eye,e.snapPickOrigin[0]=0,e.snapPickOrigin[1]=0,e.snapPickOrigin[2]=0;if(l.uniform3fv(this._uCameraEyeRtc,i),l.uniform2fv(this._uVectorA,e.snapVectorA),l.uniform2fv(this._uInverseVectorAB,e.snapInvVectorAB),l.uniform1i(this._uLayerNumber,e.snapPickLayerNumber),l.uniform3fv(this._uCoordinateScaler,I),l.uniform1i(this._uRenderPass,n),l.uniform1i(this._uPickInvisible,e.pickInvisible),l.uniformMatrix4fv(this._uSceneWorldModelMatrix,!1,d),l.uniformMatrix4fv(this._uViewMatrix,!1,r),l.uniformMatrix4fv(this._uProjMatrix,!1,o.projMatrix),s.logarithmicDepthBufferEnabled){var T=2/(Math.log(e.pickZFar+1)/Math.LN2);l.uniform1f(this._uLogDepthBufFC,T)}var E=s._sectionPlanesState.sectionPlanes.length;if(E>0)for(var b=s._sectionPlanesState.sectionPlanes,D=t.layerIndex*E,P=a.renderFlags,R=0;R0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),l.drawArrays(l.TRIANGLES,0,u.numIndices8Bits)),u.numIndices16Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),l.drawArrays(l.TRIANGLES,0,u.numIndices16Bits)),u.numIndices32Bits>0&&(c.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),l.drawArrays(l.TRIANGLES,0,u.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSceneWorldModelMatrix=n.getLocation("sceneModelWorldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// TrianglesDataTextureSnapDepthBufInitRenderer vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform mat4 sceneModelWorldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("uniform vec2 uVectorAB;"),n.push("uniform vec2 uInverseVectorAB;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("vec2 remapClipPos(vec2 clipPos) {"),n.push(" float x = (clipPos.x - uVectorAB.x) * uInverseVectorAB.x;"),n.push(" float y = (clipPos.y - uVectorAB.y) * uInverseVectorAB.y;"),n.push(" return vec2(x, y);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out highp vec3 relativeToOriginPosition;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("{"),n.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),n.push("if (color.a == 0u) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("};"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (solid != 1u) {"),n.push(" if (isPerspectiveMatrix(projMatrix)) {"),n.push(" vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push(" if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push(" position = positions[2 - (gl_VertexID % 3)];"),n.push(" viewNormal = -viewNormal;"),n.push(" }"),n.push(" } else {"),n.push(" if (viewNormal.z < 0.0) {"),n.push(" position = positions[2 - (gl_VertexID % 3)];"),n.push(" viewNormal = -viewNormal;"),n.push(" }"),n.push(" }"),n.push("}"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("relativeToOriginPosition = worldPosition.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push("vWorldPosition = worldPosition;"),n.push("vFlags2 = flags2.r;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),n.push("float tmp = clipPos.w;"),n.push("clipPos.xyzw /= tmp;"),n.push("clipPos.xy = remapClipPos(clipPos.xy);"),n.push("clipPos.z += 0.0001;"),n.push("clipPos.xyzw *= tmp;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// TrianglesDataTextureSnapDepthBufInitRenderer fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("uniform int uLayerNumber;"),r.push("uniform vec3 uCoordinateScaler;"),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0;s 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("outCoords = ivec4(relativeToOriginPosition.xyz * uCoordinateScaler.xyz, - uLayerNumber);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Sf=Z.vec3(),Nf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureCameraMatrices,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var c=a.eye;e.pickViewMatrix&&(l.bindPickCameraTexture(this._program,this._uTextureCameraMatrices),c=e.pickOrigin||c);var f=[c[0]-u[0],c[1]-u[1],c[2]-u[2]];if(s.uniform3fv(this._uCameraEyeRtc,f),s.uniform1i(this._uRenderPass,n),s.uniform3fv(this._uCameraEyeRtc,f),s.uniform1i(this._uPickInvisible,e.pickInvisible),i.logarithmicDepthBufferEnabled){var p=2/(Math.log(a.project.far+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,p)}var A=i._sectionPlanesState.sectionPlanes.length;if(A>0)for(var d=i._sectionPlanesState.sectionPlanes,v=t.layerIndex*A,h=r.renderFlags,I=0;I0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick normals vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform bool pickInvisible;"),n.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),n.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),n.push("uniform highp sampler2D uTextureCameraMatrices;"),n.push("uniform highp sampler2D uTextureModelMatrices;"),n.push("uniform vec3 uCameraEyeRtc;"),n.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("out float isPerspective;")),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("flat out uint vFlags2;")),n.push("out vec3 vWorldNormal;"),n.push("void main(void) {"),n.push("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));"),n.push("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));"),n.push("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),n.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),n.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),n.push(" return;"),n.push("} else {"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),n.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),n.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),n.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),n.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),n.push("vec3 position;"),n.push("position = positions[gl_VertexID % 3];"),n.push("if (solid != 1u) {"),n.push("if (isPerspectiveMatrix(projMatrix)) {"),n.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),n.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("normal = -normal;"),n.push("}"),n.push("} else {"),n.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),n.push("if (viewNormal.z < 0.0) {"),n.push("position = positions[2 - (gl_VertexID % 3)];"),n.push("normal = -normal;"),n.push("}"),n.push("}"),n.push("}"),n.push("normal = -normal;"),n.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),n.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),n.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),n.push("vec4 viewPosition = viewMatrix * worldPosition; "),n.push("vWorldNormal = normal.xyz;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2.w;")),n.push("gl_Position = clipPos;"),n.push("}"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick normals fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}();Z.vec3(),Z.vec3(),Z.vec3(),Z.vec3(),Z.mat4();var Lf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// TrianglesDataTextureColorRenderer vertex shader"),n.push("uniform int renderPass;"),n.push("attribute vec3 position;"),e.entityOffsetsEnabled&&n.push("attribute vec3 offset;"),n.push("attribute vec4 color;"),n.push("attribute vec4 flags;"),n.push("attribute vec4 flags2;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),t&&(n.push("out vec4 vWorldPosition;"),n.push("out vec4 vFlags2;")),n.push("void main(void) {"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// TrianglesDataTextureColorRenderer fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("in vec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var a=0;a 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),xf=Z.vec3(),Mf=Z.vec3(),Ff=Z.vec3(),Hf=Z.vec3(),Uf=Z.mat4(),Gf=function(){function e(t){b(this,e),this._scene=t,this._allocate(),this._hash=this._getHash()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=this._scene,i=r.camera,a=t.model,s=r.canvas.gl,o=t._state,l=o.textureState,u=t._state.origin,c=a.position,f=a.rotationMatrix,p=a.rotationMatrixConjugate;if(this._program||(this._allocate(),!this.errors)){var A,d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(e,o)),l.bindCommonTextures(this._program,this._uTexturePerObjectIdPositionsDecodeMatrix,this._uTexturePerVertexIdCoordinates,this._uTexturePerObjectIdColorsAndFlags,this._uTextureModelMatrices,this._uTexturePerObjectIdOffsets);var v=0!==u[0]||0!==u[1]||0!==u[2],h=0!==c[0]||0!==c[1]||0!==c[2];if(v||h){var I=xf;if(v){var y=Z.transformPoint3(f,u,Mf);I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=c[0],I[1]+=c[1],I[2]+=c[2],A=Ie(i.viewMatrix,I,Uf),(d=Ff)[0]=i.eye[0]-I[0],d[1]=i.eye[1]-I[1],d[2]=i.eye[2]-I[2]}else A=i.viewMatrix,d=i.eye;if(s.uniformMatrix4fv(this._uSceneModelWorldMatrix,!1,p),s.uniformMatrix4fv(this._uViewMatrix,!1,A),s.uniformMatrix4fv(this._uProjMatrix,!1,i.projMatrix),s.uniform3fv(this._uCameraEyeRtc,d),s.uniform1i(this._uRenderPass,n),r.logarithmicDepthBufferEnabled){var m=2/(Math.log(e.pickZFar+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,m)}var w=r._sectionPlanesState.sectionPlanes.length;if(w>0)for(var g=r._sectionPlanesState.sectionPlanes,T=t.layerIndex*w,E=a.renderFlags,b=0;b0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,8),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,16),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(l.bindTriangleIndicesTextures(this._program,this._uTexturePerPolygonIdPortionIds,this._uTexturePerPolygonIdIndices,32),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uSceneModelWorldMatrix=n.getLocation("sceneModelWorldMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,r=[];return r.push("#version 300 es"),r.push("// Triangles dataTexture draw vertex shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("precision highp usampler2D;"),r.push("precision highp isampler2D;"),r.push("precision highp sampler2D;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("precision mediump usampler2D;"),r.push("precision mediump isampler2D;"),r.push("precision mediump sampler2D;"),r.push("#endif"),r.push("uniform int renderPass;"),e.entityOffsetsEnabled&&r.push("in vec3 offset;"),r.push("uniform mat4 sceneModelWorldMatrix;"),r.push("uniform mat4 viewMatrix;"),r.push("uniform mat4 projMatrix;"),r.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),r.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"),r.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"),r.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"),r.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"),r.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"),r.push("uniform highp sampler2D uTextureModelMatrices;"),r.push("uniform vec3 uCameraEyeRtc;"),r.push("vec3 positions[3];"),e.logarithmicDepthBufferEnabled&&(r.push("uniform float logDepthBufFC;"),r.push("out float vFragDepth;"),r.push("out float isPerspective;")),r.push("bool isPerspectiveMatrix(mat4 m) {"),r.push(" return (m[2][3] == - 1.0);"),r.push("}"),r.push("out highp vec2 vHighPrecisionZW;"),n&&(r.push("out vec4 vWorldPosition;"),r.push("flat out uint vFlags2;")),r.push("void main(void) {"),r.push("int polygonIndex = gl_VertexID / 3;"),r.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;"),r.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;"),r.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"),r.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),r.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),r.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),r.push("if (int(flags.x) != renderPass) {"),r.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),r.push(" return;"),r.push("} else {"),r.push("mat4 worldMatrix = sceneModelWorldMatrix * mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));"),r.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),r.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"),r.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"),r.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;"),r.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;"),r.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),r.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),r.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),r.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),r.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),r.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"),r.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),r.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),r.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),r.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"),r.push("if (color.a == 0u) {"),r.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"),r.push(" return;"),r.push("};"),r.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"),r.push("vec3 position;"),r.push("position = positions[gl_VertexID % 3];"),r.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"),r.push("if (solid != 1u) {"),r.push("if (isPerspectiveMatrix(projMatrix)) {"),r.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;"),r.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"),r.push("position = positions[2 - (gl_VertexID % 3)];"),r.push("viewNormal = -viewNormal;"),r.push("}"),r.push("} else {"),r.push("if (viewNormal.z < 0.0) {"),r.push("position = positions[2 - (gl_VertexID % 3)];"),r.push("viewNormal = -viewNormal;"),r.push("}"),r.push("}"),r.push("}"),r.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),r.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);"),r.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;"),r.push("vec4 viewPosition = viewMatrix * worldPosition; "),r.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(r.push("vFragDepth = 1.0 + clipPos.w;"),r.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n&&(r.push("vWorldPosition = worldPosition;"),r.push("vFlags2 = flags2.r;")),r.push("gl_Position = clipPos;"),r.push("vHighPrecisionZW = gl_Position.zw;"),r.push("}"),r.push("}"),r}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture draw fragment shader"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),r.push("in highp vec2 vHighPrecisionZW;"),r.push("out vec4 outColor;"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("flat in uint vFlags2;");for(var i=0,a=t.sectionPlanes.length;i 0u;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var s=0,o=t.sectionPlanes.length;s 0.0) { "),r.push(" discard;"),r.push(" }"),r.push("}")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;"),r.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),kf=Z.vec3(),jf=Z.vec3(),Vf=Z.vec3(),Qf=Z.vec3(),Wf=Z.mat4(),zf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=t._state.origin,u=r.position,c=r.rotationMatrix,f=r.rotationMatrixConjugate,p=a.viewMatrix;if(this._program||(this._allocate(t),!this.errors)){var A,d;e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram(t));var v=0!==l[0]||0!==l[1]||0!==l[2],h=0!==u[0]||0!==u[1]||0!==u[2];if(v||h){var I=kf;if(v){var y=jf;Z.transformPoint3(c,l,y),I[0]=y[0],I[1]=y[1],I[2]=y[2]}else I[0]=0,I[1]=0,I[2]=0;I[0]+=u[0],I[1]+=u[1],I[2]+=u[2],A=Ie(p,I,Wf),(d=Vf)[0]=a.eye[0]-I[0],d[1]=a.eye[1]-I[1],d[2]=a.eye[2]-I[2]}else A=p,d=a.eye;s.uniform1i(this._uRenderPass,n),s.uniformMatrix4fv(this._uWorldMatrix,!1,f),s.uniformMatrix4fv(this._uViewMatrix,!1,A),s.uniformMatrix4fv(this._uProjMatrix,!1,a.projMatrix),s.uniformMatrix4fv(this._uViewNormalMatrix,!1,a.viewNormalMatrix),s.uniformMatrix4fv(this._uWorldNormalMatrix,!1,r.worldNormalMatrix);var m=i._sectionPlanesState.sectionPlanes.length;if(m>0)for(var w=i._sectionPlanesState.sectionPlanes,g=t.layerIndex*m,T=r.renderFlags,E=0;E0,n=[];return n.push("// Batched geometry normals vertex shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("#extension GL_EXT_frag_depth : enable"),n.push("uniform int renderPass;"),n.push("attribute vec3 position;"),e.entityOffsetsEnabled&&n.push("attribute vec3 offset;"),n.push("attribute vec3 normal;"),n.push("attribute vec4 color;"),n.push("attribute vec4 flags;"),n.push("attribute vec4 flags2;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 worldNormalMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform mat4 viewNormalMatrix;"),n.push("uniform mat4 positionsDecodeMatrix;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("varying float isPerspective;")),n.push("vec3 octDecode(vec2 oct) {"),n.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));"),n.push(" if (v.z < 0.0) {"),n.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);"),n.push(" }"),n.push(" return normalize(v);"),n.push("}"),t&&(n.push("out vec4 vWorldPosition;"),n.push("out vec4 vFlags2;")),n.push("out vec3 vViewNormal;"),n.push("void main(void) {"),n.push("if (int(flags.x) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),n.push(" vec4 worldNormal = worldNormalMatrix * vec4(octDecode(normal.xy), 0.0); "),n.push(" vec3 viewNormal = normalize((viewNormalMatrix * worldNormal).xyz);"),t&&(n.push(" vWorldPosition = worldPosition;"),n.push(" vFlags2 = flags2;")),n.push(" vViewNormal = viewNormal;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&($e.SUPPORTED_EXTENSIONS.EXT_frag_depth?n.push("vFragDepth = 1.0 + clipPos.w;"):(n.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;"),n.push("clipPos.z *= clipPos.w;")),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Batched geometry normals fragment shader"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),n){r.push("in vec4 vWorldPosition;"),r.push("in vec4 vFlags2;");for(var i=0;i 0.0);"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(var a=0;a 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&$e.SUPPORTED_EXTENSIONS.EXT_frag_depth&&r.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" gl_FragColor = vec4(packNormalToRGB(vViewNormal), 1.0); "),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Kf=Z.vec3(),Yf=function(){function e(t){b(this,e),this._scene=t,this._hash=this._getHash(),this._allocate()}return P(e,[{key:"getValid",value:function(){return this._hash===this._getHash()}},{key:"_getHash",value:function(){return this._scene._sectionPlanesState.getHash()}},{key:"drawLayer",value:function(e,t,n){var r=t.model,i=r.scene,a=i.camera,s=i.canvas.gl,o=t._state,l=t._state.origin;this._program||this._allocate(t),e.lastProgramId!==this._program.id&&(e.lastProgramId=this._program.id,this._bindProgram()),this._program.bindTexture(this._uTexturePerObjectIdPositionsDecodeMatrix,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerObjectIdPositionsDecodeMatrix),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},1),this._program.bindTexture(this._uTexturePerVertexIdCoordinates,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerVertexIdCoordinates),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},2),this._program.bindTexture(this._uTexturePerObjectIdColorsAndFlags,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerObjectIdColorsAndFlags),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},3),s.uniform1i(this._uRenderPass,n),s.uniform1i(this._uPickInvisible,e.pickInvisible),s.uniformMatrix4fv(this._uWorldMatrix,!1,r.worldMatrix);var u=e.pickViewMatrix||a.viewMatrix,c=l?Ie(u,l):u;if(s.uniformMatrix4fv(this._uViewMatrix,!1,c),s.uniformMatrix4fv(this._uProjMatrix,!1,e.pickProjMatrix),i.logarithmicDepthBufferEnabled){var f=2/(Math.log(a.project.far+1)/Math.LN2);s.uniform1f(this._uLogDepthBufFC,f)}var p=i._sectionPlanesState.sectionPlanes.length;if(p>0)for(var A=i._sectionPlanesState.sectionPlanes,d=t.layerIndex*p,v=r.renderFlags,h=0;h0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds8Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices8Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices8Bits)),o.numIndices16Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds16Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices16Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices16Bits)),o.numIndices32Bits>0&&(this._program.bindTexture(this._uTexturePerPolygonIdPortionIds,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdPortionIds32Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},4),this._program.bindTexture(this._uTexturePerPolygonIdIndices,{bind:function(e){return s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,o.texturePerPolygonIdIndices32Bits),!0},unbind:function(e){s.activeTexture(s["TEXTURE"+e]),s.bindTexture(s.TEXTURE_2D,null)}},5),s.drawArrays(s.TRIANGLES,0,o.numIndices32Bits)),e.drawElements++}},{key:"_allocate",value:function(){var e=this._scene,t=e.canvas.gl;if(this._program=new pt(t,this._buildShader()),this._program.errors)this.errors=this._program.errors;else{var n=this._program;this._uRenderPass=n.getLocation("renderPass"),this._uPickInvisible=n.getLocation("pickInvisible"),this._uPositionsDecodeMatrix=n.getLocation("positionsDecodeMatrix"),this._uWorldMatrix=n.getLocation("worldMatrix"),this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(var r=0,i=e._sectionPlanesState.sectionPlanes.length;r0,n=[];return n.push("#version 300 es"),n.push("// Triangles dataTexture pick flat normals vertex shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("precision highp usampler2D;"),n.push("precision highp isampler2D;"),n.push("precision highp sampler2D;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("precision mediump usampler2D;"),n.push("precision mediump isampler2D;"),n.push("precision mediump sampler2D;"),n.push("#endif"),n.push("uniform int renderPass;"),n.push("in uvec3 packedVertexId;"),e.entityOffsetsEnabled&&n.push("in vec3 offset;"),n.push("uniform bool pickInvisible;"),n.push("uniform mat4 worldMatrix;"),n.push("uniform mat4 viewMatrix;"),n.push("uniform mat4 projMatrix;"),n.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"),n.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"),n.push("uniform usampler2D uTexturePerVertexIdCoordinates;"),n.push("uniform usampler2D uTexturePerPolygonIdIndices;"),n.push("uniform isampler2D uTexturePerPolygonIdNormals;"),n.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("out float vFragDepth;"),n.push("bool isPerspectiveMatrix(mat4 m) {"),n.push(" return (m[2][3] == - 1.0);"),n.push("}"),n.push("out float isPerspective;")),n.push("out vec4 vWorldPosition;"),t&&n.push("out int vFlags2;"),n.push("void main(void) {"),n.push("int polygonIndex = gl_VertexID / 3;"),n.push("int h_normal_index = polygonIndex & 4095;"),n.push("int v_normal_index = polygonIndex >> 12;"),n.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;"),n.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;"),n.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"),n.push("int objectIndex;"),n.push("if (((polygonIndex >> 3) % 2) == 0) {"),n.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);"),n.push("} else {"),n.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;"),n.push("}"),n.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"),n.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"),n.push("int h_index = polygonIndex & 4095;"),n.push("int v_index = polygonIndex >> 12;"),n.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"),n.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;"),n.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;"),n.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;"),n.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*4+3, objectIndexCoords.y), 0));"),n.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"),n.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"),n.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));"),n.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));"),n.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));"),n.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"),n.push("int vertexNumber = gl_VertexID % 3;"),n.push("vec3 position;"),n.push("if (vertexNumber == 0) position = position1;"),n.push("else if (vertexNumber == 1) position = position2;"),n.push("else position = position3;"),n.push("if (int(flags.w) != renderPass) {"),n.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"),n.push(" } else {"),n.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "),e.entityOffsetsEnabled&&n.push(" worldPosition.xyz = worldPosition.xyz + offset;"),n.push(" vec4 viewPosition = viewMatrix * worldPosition; "),n.push(" vWorldPosition = worldPosition;"),t&&n.push(" vFlags2 = flags2.r;"),n.push("vec4 clipPos = projMatrix * viewPosition;"),e.logarithmicDepthBufferEnabled&&(n.push("vFragDepth = 1.0 + clipPos.w;"),n.push("isPerspective = float (isPerspectiveMatrix(projMatrix));")),n.push("gl_Position = clipPos;"),n.push(" }"),n.push("}"),n}},{key:"_buildFragmentShader",value:function(){var e=this._scene,t=e._sectionPlanesState,n=t.sectionPlanes.length>0,r=[];if(r.push("#version 300 es"),r.push("// Triangles dataTexture pick flat normals fragment shader"),r.push("#extension GL_OES_standard_derivatives : enable"),e.logarithmicDepthBufferEnabled&&r.push("#extension GL_EXT_frag_depth : enable"),r.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),r.push("precision highp float;"),r.push("precision highp int;"),r.push("#else"),r.push("precision mediump float;"),r.push("precision mediump int;"),r.push("#endif"),e.logarithmicDepthBufferEnabled&&(r.push("in float isPerspective;"),r.push("uniform float logDepthBufFC;"),r.push("in float vFragDepth;")),r.push("in vec4 vWorldPosition;"),n){r.push("in int vFlags2;");for(var i=0;i 0;"),r.push(" if (clippable) {"),r.push(" float dist = 0.0;");for(i=0;i 0.0) { discard; }"),r.push(" }")}return e.logarithmicDepthBufferEnabled&&r.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"),r.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );"),r.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );"),r.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );"),r.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);"),r.push("}"),r}},{key:"webglContextRestored",value:function(){this._program=null}},{key:"destroy",value:function(){this._program&&this._program.destroy(),this._program=null}}]),e}(),Xf=function(){function e(t){b(this,e),this._scene=t}return P(e,[{key:"_compile",value:function(){this._colorRenderer&&!this._colorRenderer.getValid()&&(this._colorRenderer.destroy(),this._colorRenderer=null),this._colorRendererWithSAO&&!this._colorRendererWithSAO.getValid()&&(this._colorRendererWithSAO.destroy(),this._colorRendererWithSAO=null),this._flatColorRenderer&&!this._flatColorRenderer.getValid()&&(this._flatColorRenderer.destroy(),this._flatColorRenderer=null),this._flatColorRendererWithSAO&&!this._flatColorRendererWithSAO.getValid()&&(this._flatColorRendererWithSAO.destroy(),this._flatColorRendererWithSAO=null),this._colorQualityRendererWithSAO&&!this._colorQualityRendererWithSAO.getValid()&&(this._colorQualityRendererWithSAO.destroy(),this._colorQualityRendererWithSAO=null),this._depthRenderer&&!this._depthRenderer.getValid()&&(this._depthRenderer.destroy(),this._depthRenderer=null),this._normalsRenderer&&!this._normalsRenderer.getValid()&&(this._normalsRenderer.destroy(),this._normalsRenderer=null),this._silhouetteRenderer&&!this._silhouetteRenderer.getValid()&&(this._silhouetteRenderer.destroy(),this._silhouetteRenderer=null),this._edgesRenderer&&!this._edgesRenderer.getValid()&&(this._edgesRenderer.destroy(),this._edgesRenderer=null),this._edgesColorRenderer&&!this._edgesColorRenderer.getValid()&&(this._edgesColorRenderer.destroy(),this._edgesColorRenderer=null),this._pickMeshRenderer&&!this._pickMeshRenderer.getValid()&&(this._pickMeshRenderer.destroy(),this._pickMeshRenderer=null),this._pickDepthRenderer&&!this._pickDepthRenderer.getValid()&&(this._pickDepthRenderer.destroy(),this._pickDepthRenderer=null),this._vertexDepthRenderer&&!this._vertexDepthRenderer.getValid()&&(this._vertexDepthRenderer.destroy(),this._vertexDepthRenderer=null),this._snapDepthBufInitRenderer&&!this._snapDepthBufInitRenderer.getValid()&&(this._snapDepthBufInitRenderer.destroy(),this._snapDepthBufInitRenderer=null),this._pickNormalsRenderer&&!1===this._pickNormalsRenderer.getValid()&&(this._pickNormalsRenderer.destroy(),this._pickNormalsRenderer=null),this._pickNormalsFlatRenderer&&!1===this._pickNormalsFlatRenderer.getValid()&&(this._pickNormalsFlatRenderer.destroy(),this._pickNormalsFlatRenderer=null),this._occlusionRenderer&&!1===this._occlusionRenderer.getValid()&&(this._occlusionRenderer.destroy(),this._occlusionRenderer=null)}},{key:"colorRenderer",get:function(){return this._colorRenderer||(this._colorRenderer=new kc(this._scene,!1)),this._colorRenderer}},{key:"colorRendererWithSAO",get:function(){return this._colorRendererWithSAO||(this._colorRendererWithSAO=new kc(this._scene,!0)),this._colorRendererWithSAO}},{key:"colorQualityRendererWithSAO",get:function(){return this._colorQualityRendererWithSAO||(this._colorQualityRendererWithSAO=new TrianglesDataTextureColorQualityRenderer(this._scene,!0)),this._colorQualityRendererWithSAO}},{key:"silhouetteRenderer",get:function(){return this._silhouetteRenderer||(this._silhouetteRenderer=new Yc(this._scene)),this._silhouetteRenderer}},{key:"depthRenderer",get:function(){return this._depthRenderer||(this._depthRenderer=new Gf(this._scene)),this._depthRenderer}},{key:"normalsRenderer",get:function(){return this._normalsRenderer||(this._normalsRenderer=new zf(this._scene)),this._normalsRenderer}},{key:"edgesRenderer",get:function(){return this._edgesRenderer||(this._edgesRenderer=new ef(this._scene)),this._edgesRenderer}},{key:"edgesColorRenderer",get:function(){return this._edgesColorRenderer||(this._edgesColorRenderer=new sf(this._scene)),this._edgesColorRenderer}},{key:"pickMeshRenderer",get:function(){return this._pickMeshRenderer||(this._pickMeshRenderer=new ff(this._scene)),this._pickMeshRenderer}},{key:"pickNormalsRenderer",get:function(){return this._pickNormalsRenderer||(this._pickNormalsRenderer=new Nf(this._scene)),this._pickNormalsRenderer}},{key:"pickNormalsFlatRenderer",get:function(){return this._pickNormalsFlatRenderer||(this._pickNormalsFlatRenderer=new Yf(this._scene)),this._pickNormalsFlatRenderer}},{key:"pickDepthRenderer",get:function(){return this._pickDepthRenderer||(this._pickDepthRenderer=new If(this._scene)),this._pickDepthRenderer}},{key:"vertexDepthRenderer",get:function(){return this._vertexDepthRenderer||(this._vertexDepthRenderer=new bf(this._scene)),this._vertexDepthRenderer}},{key:"snapDepthBufInitRenderer",get:function(){return this._snapDepthBufInitRenderer||(this._snapDepthBufInitRenderer=new Of(this._scene)),this._snapDepthBufInitRenderer}},{key:"occlusionRenderer",get:function(){return this._occlusionRenderer||(this._occlusionRenderer=new Lf(this._scene)),this._occlusionRenderer}},{key:"_destroy",value:function(){this._colorRenderer&&this._colorRenderer.destroy(),this._colorRendererWithSAO&&this._colorRendererWithSAO.destroy(),this._flatColorRenderer&&this._flatColorRenderer.destroy(),this._flatColorRendererWithSAO&&this._flatColorRendererWithSAO.destroy(),this._colorQualityRendererWithSAO&&this._colorQualityRendererWithSAO.destroy(),this._depthRenderer&&this._depthRenderer.destroy(),this._normalsRenderer&&this._normalsRenderer.destroy(),this._silhouetteRenderer&&this._silhouetteRenderer.destroy(),this._edgesRenderer&&this._edgesRenderer.destroy(),this._edgesColorRenderer&&this._edgesColorRenderer.destroy(),this._pickMeshRenderer&&this._pickMeshRenderer.destroy(),this._pickDepthRenderer&&this._pickDepthRenderer.destroy(),this._vertexDepthRenderer&&this._vertexDepthRenderer.destroy(),this._snapDepthBufInitRenderer&&this._snapDepthBufInitRenderer.destroy(),this._pickNormalsRenderer&&this._pickNormalsRenderer.destroy(),this._pickNormalsFlatRenderer&&this._pickNormalsFlatRenderer.destroy(),this._occlusionRenderer&&this._occlusionRenderer.destroy()}}]),e}(),qf={};var Jf=P((function e(){b(this,e),this.positionsCompressed=[],this.metallicRoughness=[],this.indices8Bits=[],this.indices16Bits=[],this.indices32Bits=[],this.edgeIndices8Bits=[],this.edgeIndices16Bits=[],this.edgeIndices32Bits=[],this.perObjectColors=[],this.perObjectPickColors=[],this.perObjectSolid=[],this.perObjectOffsets=[],this.perObjectPositionsDecodeMatrices=[],this.perObjectInstancePositioningMatrices=[],this.perObjectVertexBases=[],this.perObjectIndexBaseOffsets=[],this.perObjectEdgeIndexBaseOffsets=[],this.perTriangleNumberPortionId8Bits=[],this.perTriangleNumberPortionId16Bits=[],this.perTriangleNumberPortionId32Bits=[],this.perEdgeNumberPortionId8Bits=[],this.perEdgeNumberPortionId16Bits=[],this.perEdgeNumberPortionId32Bits=[]})),Zf=function(){function e(){b(this,e),this.texturePerObjectIdColorsAndFlags=null,this.texturePerObjectIdOffsets=null,this.texturePerObjectIdPositionsDecodeMatrix=null,this.texturePerVertexIdCoordinates=null,this.texturePerPolygonIdPortionIds8Bits=null,this.texturePerPolygonIdPortionIds16Bits=null,this.texturePerPolygonIdPortionIds32Bits=null,this.texturePerEdgeIdPortionIds8Bits=null,this.texturePerEdgeIdPortionIds16Bits=null,this.texturePerEdgeIdPortionIds32Bits=null,this.texturePerPolygonIdIndices8Bits=null,this.texturePerPolygonIdIndices16Bits=null,this.texturePerPolygonIdIndices32Bits=null,this.texturePerPolygonIdEdgeIndices8Bits=null,this.texturePerPolygonIdEdgeIndices16Bits=null,this.texturePerPolygonIdEdgeIndices32Bits=null,this.textureModelMatrices=null}return P(e,[{key:"finalize",value:function(){this.indicesPerBitnessTextures={8:this.texturePerPolygonIdIndices8Bits,16:this.texturePerPolygonIdIndices16Bits,32:this.texturePerPolygonIdIndices32Bits},this.indicesPortionIdsPerBitnessTextures={8:this.texturePerPolygonIdPortionIds8Bits,16:this.texturePerPolygonIdPortionIds16Bits,32:this.texturePerPolygonIdPortionIds32Bits},this.edgeIndicesPerBitnessTextures={8:this.texturePerPolygonIdEdgeIndices8Bits,16:this.texturePerPolygonIdEdgeIndices16Bits,32:this.texturePerPolygonIdEdgeIndices32Bits},this.edgeIndicesPortionIdsPerBitnessTextures={8:this.texturePerEdgeIdPortionIds8Bits,16:this.texturePerEdgeIdPortionIds16Bits,32:this.texturePerEdgeIdPortionIds32Bits}}},{key:"bindCommonTextures",value:function(e,t,n,r,i,a){this.texturePerObjectIdPositionsDecodeMatrix.bindTexture(e,t,1),this.texturePerVertexIdCoordinates.bindTexture(e,n,2),this.texturePerObjectIdColorsAndFlags.bindTexture(e,r,3),this.textureModelMatrices.bindTexture(e,i,4),this.texturePerObjectIdOffsets.bindTexture(e,a,5)}},{key:"bindPickCameraTexture",value:function(e,t){}},{key:"bindTriangleIndicesTextures",value:function(e,t,n,r){this.indicesPortionIdsPerBitnessTextures[r].bindTexture(e,t,6),this.indicesPerBitnessTextures[r].bindTexture(e,n,7)}},{key:"bindEdgeIndicesTextures",value:function(e,t,n,r){this.edgeIndicesPortionIdsPerBitnessTextures[r].bindTexture(e,t,6),this.edgeIndicesPerBitnessTextures[r].bindTexture(e,n,7)}}]),e}(),$f=function(){function e(t,n,r,i){var a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null;b(this,e),this._gl=t,this._texture=n,this._textureWidth=r,this._textureHeight=i,this._textureData=a}return P(e,[{key:"bindTexture",value:function(e,t,n){return e.bindTexture(t,this,n)}},{key:"bind",value:function(e){return this._gl.activeTexture(this._gl["TEXTURE"+e]),this._gl.bindTexture(this._gl.TEXTURE_2D,this._texture),!0}},{key:"unbind",value:function(e){}}]),e}(),ep={sizeDataColorsAndFlags:0,sizeDataPositionDecodeMatrices:0,sizeDataTextureOffsets:0,sizeDataTexturePositions:0,sizeDataTextureIndices:0,sizeDataTextureEdgeIndices:0,sizeDataTexturePortionIds:0,numberOfGeometries:0,numberOfPortions:0,numberOfLayers:0,numberOfTextures:0,totalPolygons:0,totalPolygons8Bits:0,totalPolygons16Bits:0,totalPolygons32Bits:0,totalEdges:0,totalEdges8Bits:0,totalEdges16Bits:0,totalEdges32Bits:0,cannotCreatePortion:{because10BitsObjectId:0,becauseTextureSize:0},overheadSizeAlignementIndices:0,overheadSizeAlignementEdgeIndices:0};window.printDataTextureRamStats=function(){console.log(JSON.stringify(ep,null,4));var e=0;Object.keys(ep).forEach((function(t){t.startsWith("size")&&(e+=ep[t])})),console.log("Total size ".concat(e," bytes (").concat((e/1e3/1e3).toFixed(2)," MB)")),console.log("Avg bytes / triangle: ".concat((e/ep.totalPolygons).toFixed(2)));var t={};Object.keys(ep).forEach((function(n){n.startsWith("size")&&(t[n]="".concat((ep[n]/e*100).toFixed(2)," % of total"))})),console.log(JSON.stringify({percentualRamUsage:t},null,4))};var tp=function(){function e(){b(this,e)}return P(e,[{key:"disableBindedTextureFiltering",value:function(e){e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}},{key:"generateCameraDataTexture",value:function(e,t,n,r){var i=e.createTexture();e.bindTexture(e.TEXTURE_2D,i),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);var a=new $f(e,i,4,3),s=!0;a.updateViewMatrix=function(n,i){e.bindTexture(e.TEXTURE_2D,a._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(r?Ie(n,r):n)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(i))};var o=function(){s&&(s=!1,a.updateViewMatrix(t.viewMatrix,t.project.matrix))};return t.on("matrix",(function(){return s=!0})),n.on("rendering",o),o(),a}},{key:"generatePickCameraDataTexture",value:function(e,t,n){var r=e.createTexture();e.bindTexture(e.TEXTURE_2D,r),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,3),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null);var i=new $f(e,r,4,3);return i.updateViewMatrix=function(r,a){e.bindTexture(e.TEXTURE_2D,i._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(n?Ie(r,n):r)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.viewNormalMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,2,4,1,e.RGBA,e.FLOAT,new Float32Array(a))},i}},{key:"generateModelTexture",value:function(e,t){var n=e.createTexture();return e.bindTexture(e.TEXTURE_2D,n),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA32F,4,2),e.texSubImage2D(e.TEXTURE_2D,0,0,0,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldMatrix)),e.texSubImage2D(e.TEXTURE_2D,0,0,1,4,1,e.RGBA,e.FLOAT,new Float32Array(t.worldNormalMatrix)),this.disableBindedTextureFiltering(e),e.bindTexture(e.TEXTURE_2D,null),new $f(e,n,4,2)}},{key:"generateTextureForColorsAndFlags",value:function(e,t,n,r,i,a,s){var o=t.length;this.numPortions=o;var l=4096,u=Math.ceil(o/512);if(0===u)throw"texture height===0";var c=new Uint8Array(16384*u);ep.sizeDataColorsAndFlags+=c.byteLength,ep.numberOfTextures++;for(var f=0;f>24&255,r[f]>>16&255,r[f]>>8&255,255&r[f]],32*f+16),c.set([i[f]>>24&255,i[f]>>16&255,i[f]>>8&255,255&i[f]],32*f+20),c.set([a[f]>>24&255,a[f]>>16&255,a[f]>>8&255,255&a[f]],32*f+24),c.set([s[f]?1:0,0,0,0],32*f+28);var p=e.createTexture();return e.bindTexture(e.TEXTURE_2D,p),e.texStorage2D(e.TEXTURE_2D,1,e.RGBA8UI,l,u),e.texSubImage2D(e.TEXTURE_2D,0,0,0,l,u,e.RGBA_INTEGER,e.UNSIGNED_BYTE,c,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new $f(e,p,l,u,c)}},{key:"generateTextureForObjectOffsets",value:function(e,t){var n=512,r=Math.ceil(t/n);if(0===r)throw"texture height===0";var i=new Float32Array(1536*r).fill(0);ep.sizeDataTextureOffsets+=i.byteLength,ep.numberOfTextures++;var a=e.createTexture();return e.bindTexture(e.TEXTURE_2D,a),e.texStorage2D(e.TEXTURE_2D,1,e.RGB32F,n,r),e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,e.RGB,e.FLOAT,i,0),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),new $f(e,a,n,r,i)}},{key:"generateTextureForPositionsDecodeMatrices",value:function(e,t,n){var r=t.length;if(0===r)throw"num decode+entity matrices===0";var i=2048,a=Math.ceil(r/512),s=new Float32Array(8192*a);ep.sizeDataPositionDecodeMatrices+=s.byteLength,ep.numberOfTextures++;for(var o=Z.mat4(),l=0;l65536&&ep.cannotCreatePortion.because10BitsObjectId++;var n=this._numPortions+t<=65536,r=void 0!==e.geometryId&&null!==e.geometryId?"".concat(e.geometryId,"#").concat(0):"".concat(e.id,"#").concat(0);if(!this._bucketGeometries[r]){var i=Math.max(this._state.numIndices8Bits,this._state.numIndices16Bits,this._state.numIndices32Bits),a=0,s=0;e.buckets.forEach((function(e){a+=e.positionsCompressed.length/3,s+=e.indices.length/3})),(this._state.numVertices+a>16777216||i+s>16777216)&&ep.cannotCreatePortion.becauseTextureSize++,n&&(n=this._state.numVertices+a<=16777216&&i+s<=16777216)}return n}},{key:"createPortion",value:function(e){var t=this;if(this._finalized)throw"Already finalized";var n=[],r=e.worldAABB;e.buckets.forEach((function(i,a){var s=void 0!==e.geometryId&&null!==e.geometryId?"".concat(e.geometryId,"#").concat(a):"".concat(e.id,"#").concat(a),o=t._bucketGeometries[s];o||(o=t._createBucketGeometry(e,i),t._bucketGeometries[s]=o);var l=Z.collapseAABB3(op),u=t._createSubPortion(e,o,i,l);Z.expandAABB3(r,l),n.push(u)}));var i=this._state.origin;0===i[0]&&0===i[1]&&0===i[2]||(r[0]+=i[0],r[1]+=i[1],r[2]+=i[2],r[3]+=i[0],r[4]+=i[1],r[5]+=i[2]),Z.expandAABB3(this.aabb,r);var a=this._portionToSubPortionsMap.length;return this._portionToSubPortionsMap.push(n),this.model.numPortions++,a}},{key:"_createBucketGeometry",value:function(e,t){if(t.indices){var n=8*Math.ceil(t.indices.length/3/8)*3;ep.overheadSizeAlignementIndices+=2*(n-t.indices.length);var r=new Uint32Array(n);r.fill(0),r.set(t.indices),t.indices=r}if(t.edgeIndices){var i=8*Math.ceil(t.edgeIndices.length/2/8)*2;ep.overheadSizeAlignementEdgeIndices+=2*(i-t.edgeIndices.length);var a=new Uint32Array(i);a.fill(0),a.set(t.edgeIndices),t.edgeIndices=a}for(var s,o=t.positionsCompressed,l=t.indices,u=t.edgeIndices,c=this._buffer,f=c.positionsCompressed.length/3,p=o.length/3,A=0,d=o.length;A0){var y,m=3*t.numTriangles;t.numVertices<=256?(y=p.perTriangleNumberPortionId8Bits,A.numIndices8Bits+=m,ep.totalPolygons8Bits+=t.numTriangles):t.numVertices<=65536?(y=p.perTriangleNumberPortionId16Bits,A.numIndices16Bits+=m,ep.totalPolygons16Bits+=t.numTriangles):(y=p.perTriangleNumberPortionId32Bits,A.numIndices32Bits+=m,ep.totalPolygons32Bits+=t.numTriangles),ep.totalPolygons+=t.numTriangles;for(var w=0;w0){var g,T=2*t.numEdges;t.numVertices<=256?(g=p.perEdgeNumberPortionId8Bits,A.numEdgeIndices8Bits+=T,ep.totalEdges8Bits+=t.numEdges):t.numVertices<=65536?(g=p.perEdgeNumberPortionId16Bits,A.numEdgeIndices16Bits+=T,ep.totalEdges16Bits+=t.numEdges):(g=p.perEdgeNumberPortionId32Bits,A.numEdgeIndices32Bits+=T,ep.totalEdges32Bits+=t.numEdges),ep.totalEdges+=t.numEdges;for(var E=0;E0&&(n.texturePerEdgeIdPortionIds8Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId8Bits)),i.perEdgeNumberPortionId16Bits.length>0&&(n.texturePerEdgeIdPortionIds16Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId16Bits)),i.perEdgeNumberPortionId32Bits.length>0&&(n.texturePerEdgeIdPortionIds32Bits=this._dataTextureGenerator.generateTextureForPackedPortionIds(r,i.perEdgeNumberPortionId32Bits)),i.indices8Bits.length>0&&(n.texturePerPolygonIdIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitIndices(r,i.indices8Bits)),i.indices16Bits.length>0&&(n.texturePerPolygonIdIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitIndices(r,i.indices16Bits)),i.indices32Bits.length>0&&(n.texturePerPolygonIdIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitIndices(r,i.indices32Bits)),i.edgeIndices8Bits.length>0&&(n.texturePerPolygonIdEdgeIndices8Bits=this._dataTextureGenerator.generateTextureFor8BitsEdgeIndices(r,i.edgeIndices8Bits)),i.edgeIndices16Bits.length>0&&(n.texturePerPolygonIdEdgeIndices16Bits=this._dataTextureGenerator.generateTextureFor16BitsEdgeIndices(r,i.edgeIndices16Bits)),i.edgeIndices32Bits.length>0&&(n.texturePerPolygonIdEdgeIndices32Bits=this._dataTextureGenerator.generateTextureFor32BitsEdgeIndices(r,i.edgeIndices32Bits)),this.model._modelMatricesTexture||(this.model._modelMatricesTexture=this._dataTextureGenerator.generateModelTexture(r,this.model)),n.textureModelMatrices=this.model._modelMatricesTexture,n.cameraTexture=this._dataTextureGenerator.generateCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this.model.scene,this._state.origin.slice()),n.textureCameraMatrices=n.cameraTexture,n.texturePickCameraMatrices=this._dataTextureGenerator.generatePickCameraDataTexture(this.model.scene.canvas.gl,this.model.scene.camera,this._state.origin.slice()),n.finalize(),this._buffer=null,this._bucketGeometries={},this._finalized=!0,this._deferredSetFlagsDirty=!1,this._onSceneRendering=this.model.scene.on("rendering",(function(){e._deferredSetFlagsDirty&&e._uploadDeferredFlags(),e._numUpdatesInFrame=0}))}}},{key:"isEmpty",value:function(){return 0===this._numPortions}},{key:"initFlags",value:function(e,t,n){t&Te&&(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++),t&Ce&&(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++),t&Re&&(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++),t&_e&&(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++),t&De&&(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++),t&Be&&(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++),t&be&&(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++),t&Ee&&(this._numCulledLayerPortions++,this.model.numCulledLayerPortions++),n&&(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++);this._setFlags(e,t,n,true),this._setFlags2(e,t,true)}},{key:"flushInitFlags",value:function(){this._setDeferredFlags(),this._setDeferredFlags2()}},{key:"setVisible",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Te?(this._numVisibleLayerPortions++,this.model.numVisibleLayerPortions++):(this._numVisibleLayerPortions--,this.model.numVisibleLayerPortions--),this._setFlags(e,t,n)}},{key:"setHighlighted",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Ce?(this._numHighlightedLayerPortions++,this.model.numHighlightedLayerPortions++):(this._numHighlightedLayerPortions--,this.model.numHighlightedLayerPortions--),this._setFlags(e,t,n)}},{key:"setXRayed",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Re?(this._numXRayedLayerPortions++,this.model.numXRayedLayerPortions++):(this._numXRayedLayerPortions--,this.model.numXRayedLayerPortions--),this._setFlags(e,t,n)}},{key:"setSelected",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&_e?(this._numSelectedLayerPortions++,this.model.numSelectedLayerPortions++):(this._numSelectedLayerPortions--,this.model.numSelectedLayerPortions--),this._setFlags(e,t,n)}},{key:"setEdges",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Be?(this._numEdgesLayerPortions++,this.model.numEdgesLayerPortions++):(this._numEdgesLayerPortions--,this.model.numEdgesLayerPortions--),this._setFlags(e,t,n)}},{key:"setClippable",value:function(e,t){if(!this._finalized)throw"Not finalized";t&De?(this._numClippableLayerPortions++,this.model.numClippableLayerPortions++):(this._numClippableLayerPortions--,this.model.numClippableLayerPortions--),this._setFlags2(e,t)}},{key:"_beginDeferredFlags",value:function(){this._deferredSetFlagsActive=!0}},{key:"_uploadDeferredFlags",value:function(){if(this._deferredSetFlagsActive=!1,this._deferredSetFlagsDirty){this._deferredSetFlagsDirty=!1;var e=this.model.scene.canvas.gl,t=this._dataTextureState;e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdColorsAndFlags._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdColorsAndFlags._textureWidth,t.texturePerObjectIdColorsAndFlags._textureHeight,e.RGBA_INTEGER,e.UNSIGNED_BYTE,t.texturePerObjectIdColorsAndFlags._textureData),e.bindTexture(e.TEXTURE_2D,t.texturePerObjectIdOffsets._texture),e.texSubImage2D(e.TEXTURE_2D,0,0,0,t.texturePerObjectIdOffsets._textureWidth,t.texturePerObjectIdOffsets._textureHeight,e.RGB,e.FLOAT,t.texturePerObjectIdOffsets._textureData)}}},{key:"setCulled",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&Ee?(this._numCulledLayerPortions+=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions++):(this._numCulledLayerPortions-=this._portionToSubPortionsMap[e].length,this.model.numCulledLayerPortions--),this._setFlags(e,t,n)}},{key:"setCollidable",value:function(e,t){if(!this._finalized)throw"Not finalized"}},{key:"setPickable",value:function(e,t,n){if(!this._finalized)throw"Not finalized";t&be?(this._numPickableLayerPortions++,this.model.numPickableLayerPortions++):(this._numPickableLayerPortions--,this.model.numPickableLayerPortions--),this._setFlags(e,t,n)}},{key:"setColor",value:function(e,t){for(var n=this._portionToSubPortionsMap[e],r=0,i=n.length;r=10&&this._beginDeferredFlags(),r.bindTexture(r.TEXTURE_2D,n.texturePerObjectIdColorsAndFlags._texture),r.texSubImage2D(r.TEXTURE_2D,0,e%512*8,Math.floor(e/512),1,1,r.RGBA_INTEGER,r.UNSIGNED_BYTE,ap))}},{key:"setTransparent",value:function(e,t,n){n?(this._numTransparentLayerPortions++,this.model.numTransparentLayerPortions++):(this._numTransparentLayerPortions--,this.model.numTransparentLayerPortions--),this._setFlags(e,t,n)}},{key:"_setFlags",value:function(e,t,n){for(var r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],i=this._portionToSubPortionsMap[e],a=0,s=i.length;a3&&void 0!==arguments[3]&&arguments[3];if(!this._finalized)throw"Not finalized";var i,a,s=!!(t&Te),o=!!(t&Re),l=!!(t&Ce),u=!!(t&_e),c=!!(t&Be),f=!!(t&be),p=!!(t&Ee);i=!s||p||o?to.NOT_RENDERED:n?to.COLOR_TRANSPARENT:to.COLOR_OPAQUE,a=!s||p?to.NOT_RENDERED:u?to.SILHOUETTE_SELECTED:l?to.SILHOUETTE_HIGHLIGHTED:o?to.SILHOUETTE_XRAYED:to.NOT_RENDERED;var A=0;A=!s||p?to.NOT_RENDERED:u?to.EDGES_SELECTED:l?to.EDGES_HIGHLIGHTED:o?to.EDGES_XRAYED:c?n?to.EDGES_COLOR_TRANSPARENT:to.EDGES_COLOR_OPAQUE:to.NOT_RENDERED;var d=s&&f?to.PICK:to.NOT_RENDERED,v=this._dataTextureState,h=this.model.scene.canvas.gl;ap[0]=i,ap[1]=a,ap[2]=A,ap[3]=d,v.texturePerObjectIdColorsAndFlags._textureData.set(ap,32*e+8),this._deferredSetFlagsActive||r?this._deferredSetFlagsDirty=!0:(++this._numUpdatesInFrame>=10&&this._beginDeferredFlags(),h.bindTexture(h.TEXTURE_2D,v.texturePerObjectIdColorsAndFlags._texture),h.texSubImage2D(h.TEXTURE_2D,0,e%512*8+2,Math.floor(e/512),1,1,h.RGBA_INTEGER,h.UNSIGNED_BYTE,ap))}},{key:"_setDeferredFlags",value:function(){}},{key:"_setFlags2",value:function(e,t){for(var n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=this._portionToSubPortionsMap[e],i=0,a=r.length;i2&&void 0!==arguments[2]&&arguments[2];if(!this._finalized)throw"Not finalized";var r=t&De?255:0,i=this._dataTextureState,a=this.model.scene.canvas.gl;ap[0]=r,ap[1]=0,ap[2]=1,ap[3]=2,i.texturePerObjectIdColorsAndFlags._textureData.set(ap,32*e+12),this._deferredSetFlagsActive||n?this._deferredSetFlagsDirty=!0:(++this._numUpdatesInFrame>=10&&this._beginDeferredFlags(),a.bindTexture(a.TEXTURE_2D,i.texturePerObjectIdColorsAndFlags._texture),a.texSubImage2D(a.TEXTURE_2D,0,e%512*8+3,Math.floor(e/512),1,1,a.RGBA_INTEGER,a.UNSIGNED_BYTE,ap))}},{key:"_setDeferredFlags2",value:function(){}},{key:"setOffset",value:function(e,t){for(var n=this._portionToSubPortionsMap[e],r=0,i=n.length;r=10&&this._beginDeferredFlags(),r.bindTexture(r.TEXTURE_2D,n.texturePerObjectIdOffsets._texture),r.texSubImage2D(r.TEXTURE_2D,0,0,e,1,1,r.RGB,r.FLOAT,sp))}},{key:"drawColorOpaque",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),t.withSAO&&this.model.saoEnabled?this._dataTextureRenderers.colorRendererWithSAO&&this._dataTextureRenderers.colorRendererWithSAO.drawLayer(t,this,to.COLOR_OPAQUE):this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"_updateBackfaceCull",value:function(e,t){var n=this.model.backfaces||e.sectioned;if(t.backfaces!==n){var r=t.gl;n?r.disable(r.CULL_FACE):r.enable(r.CULL_FACE),t.backfaces=n}}},{key:"drawColorTransparent",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numTransparentLayerPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.colorRenderer&&this._dataTextureRenderers.colorRenderer.drawLayer(t,this,to.COLOR_TRANSPARENT))}},{key:"drawDepth",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.depthRenderer&&this._dataTextureRenderers.depthRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawNormals",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&this._numTransparentLayerPortions!==this._numPortions&&this._numXRayedLayerPortions!==this._numPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.normalsRenderer&&this._dataTextureRenderers.normalsRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawSilhouetteXRayed",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_XRAYED))}},{key:"drawSilhouetteHighlighted",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_HIGHLIGHTED))}},{key:"drawSilhouetteSelected",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.silhouetteRenderer&&this._dataTextureRenderers.silhouetteRenderer.drawLayer(t,this,to.SILHOUETTE_SELECTED))}},{key:"drawEdgesColorOpaque",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,to.EDGES_COLOR_OPAQUE)}},{key:"drawEdgesColorTransparent",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numEdgesLayerPortions&&0!==this._numTransparentLayerPortions&&this._dataTextureRenderers.edgesColorRenderer&&this._dataTextureRenderers.edgesColorRenderer.drawLayer(t,this,to.EDGES_COLOR_TRANSPARENT)}},{key:"drawEdgesHighlighted",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numHighlightedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_HIGHLIGHTED)}},{key:"drawEdgesSelected",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numSelectedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_SELECTED)}},{key:"drawEdgesXRayed",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&0!==this._numXRayedLayerPortions&&this._dataTextureRenderers.edgesRenderer&&this._dataTextureRenderers.edgesRenderer.drawLayer(t,this,to.EDGES_XRAYED)}},{key:"drawOcclusion",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.occlusionRenderer&&this._dataTextureRenderers.occlusionRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"drawShadow",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.shadowRenderer&&this._dataTextureRenderers.shadowRenderer.drawLayer(t,this,to.COLOR_OPAQUE))}},{key:"setPickMatrices",value:function(e,t){0!==this._numVisibleLayerPortions&&this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(e,t)}},{key:"drawPickMesh",value:function(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickMeshRenderer&&this._dataTextureRenderers.pickMeshRenderer.drawLayer(t,this,to.PICK))}},{key:"drawPickDepths",value:function(e,t){0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickDepthRenderer&&this._dataTextureRenderers.pickDepthRenderer.drawLayer(t,this,to.PICK))}},{key:"drawSnapInitDepthBuf",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.snapDepthBufInitRenderer&&this._dataTextureRenderers.snapDepthBufInitRenderer.drawLayer(t,this,to.PICK))}},{key:"drawSnapDepths",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.vertexDepthRenderer&&this._dataTextureRenderers.vertexDepthRenderer.drawLayer(t,this,to.PICK))}},{key:"drawPickNormals",value:function(e,t){this._numCulledLayerPortions!==this._numPortions&&0!==this._numVisibleLayerPortions&&(this._updateBackfaceCull(e,t),this._dataTextureRenderers.pickNormalsRenderer&&this._dataTextureRenderers.pickNormalsRenderer.drawLayer(t,this,to.PICK))}},{key:"destroy",value:function(){if(!this._destroyed){var e=this._state;e.metallicRoughnessBuf&&(e.metallicRoughnessBuf.destroy(),e.metallicRoughnessBuf=null),this.model.scene.off(this._onSceneRendering),e.destroy(),this._destroyed=!0}}}]),e}(),fp=Z.vec3();Z.mat4();var pp=Z.vec3([1,1,1]),Ap=Z.vec3([0,0,0]),dp=Z.vec3([0,0,0]),vp=Z.identityQuaternion(),hp=Z.identityMat4(),Ip=new Uint8Array([255,255,255]),yp=function(e){I(n,ve);var t=m(n);function n(e){var r,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),(r=t.call(this,e,i))._dtxEnabled=r.scene.dtxEnabled&&!1!==i.dtxEnabled,r._enableVertexWelding=!1,r._enableIndexBucketing=!0,r._vboBatchingLayerScratchMemory=eo(),r._textureTranscoder=i.textureTranscoder||Ec(r.scene.viewer),r._maxGeometryBatchSize=i.maxGeometryBatchSize,r._aabb=Z.collapseAABB3(),r._aabbDirty=!0,r._quantizationRanges={},r._vboInstancingLayers={},r._vboBatchingLayers={},r._dtxLayers={},r._meshList=[],r.layerList=[],r._entityList=[],r._geometries={},r._dtxBuckets={},r._textures={},r._textureSets={},r._meshes={},r._entities={},r._scheduledMeshes={},r.renderFlags=new ea,r.numGeometries=0,r.numPortions=0,r.numVisibleLayerPortions=0,r.numTransparentLayerPortions=0,r.numXRayedLayerPortions=0,r.numHighlightedLayerPortions=0,r.numSelectedLayerPortions=0,r.numEdgesLayerPortions=0,r.numPickableLayerPortions=0,r.numClippableLayerPortions=0,r.numCulledLayerPortions=0,r.numEntities=0,r._numTriangles=0,r._numLines=0,r._numPoints=0,r._edgeThreshold=i.edgeThreshold||10,r._origin=Z.vec3(i.origin||[0,0,0]),r._position=Z.vec3(i.position||[0,0,0]),r._rotation=Z.vec3(i.rotation||[0,0,0]),r._quaternion=Z.vec4(i.quaternion||[0,0,0,1]),r._conjugateQuaternion=Z.vec4(i.quaternion||[0,0,0,1]),i.rotation&&Z.eulerToQuaternion(r._rotation,"XYZ",r._quaternion),r._scale=Z.vec3(i.scale||[1,1,1]),r._worldRotationMatrix=Z.mat4(),r._worldRotationMatrixConjugate=Z.mat4(),r._matrix=Z.mat4(),r._matrixDirty=!0,r._rebuildMatrices(),r._worldNormalMatrix=Z.mat4(),Z.inverseMat4(r._matrix,r._worldNormalMatrix),Z.transposeMat4(r._worldNormalMatrix),(i.matrix||i.position||i.rotation||i.scale||i.quaternion)&&(r._viewMatrix=Z.mat4(),r._viewNormalMatrix=Z.mat4(),r._viewMatrixDirty=!0,r._matrixNonIdentity=!0),r._opacity=1,r._colorize=[1,1,1],r._saoEnabled=!1!==i.saoEnabled,r._pbrEnabled=!1!==i.pbrEnabled,r._colorTextureEnabled=!1!==i.colorTextureEnabled,r._isModel=i.isModel,r._isModel&&r.scene._registerModel(g(r)),r._onCameraViewMatrix=r.scene.camera.on("matrix",(function(){r._viewMatrixDirty=!0})),r.scene.vfc.enabled&&(r._vfcManager=r.scene.vfc.getVFCManager(g(r))),r._createDefaultTextureSet(),r.visible=i.visible,r.culled=i.culled,r.pickable=i.pickable,r.clippable=i.clippable,r.collidable=i.collidable,r.castsShadow=i.castsShadow,r.receivesShadow=i.receivesShadow,r.xrayed=i.xrayed,r.highlighted=i.highlighted,r.selected=i.selected,r.edges=i.edges,r.colorize=i.colorize,r.opacity=i.opacity,r.backfaces=i.backfaces,r}return P(n,[{key:"_createDefaultTextureSet",value:function(){var e=new pc({id:"defaultColorTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})}),t=new pc({id:"defaultMetalRoughTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,1,1,1]})}),n=new pc({id:"defaultNormalsTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,0,0,0]})}),r=new pc({id:"defaultEmissiveTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[0,0,0,1]})}),i=new pc({id:"defaultOcclusionTexture",texture:new Ha({gl:this.scene.canvas.gl,preloadColor:[1,1,1,1]})});this._textures.defaultColorTexture=e,this._textures.defaultMetalRoughTexture=t,this._textures.defaultNormalsTexture=n,this._textures.defaultEmissiveTexture=r,this._textures.defaultOcclusionTexture=i,this._textureSets.defaultTextureSet=new fc({id:"defaultTextureSet",model:this,colorTexture:e,metallicRoughnessTexture:t,normalsTexture:n,emissiveTexture:r,occlusionTexture:i})}},{key:"isPerformanceModel",get:function(){return!0}},{key:"objects",get:function(){return this._entities}},{key:"origin",get:function(){return this._origin}},{key:"position",get:function(){return this._position},set:function(e){this._position.set(e||[0,0,0]),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"rotation",get:function(){return this._rotation},set:function(e){this._rotation.set(e||[0,0,0]),Z.eulerToQuaternion(this._rotation,"XYZ",this._quaternion),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"quaternion",get:function(){return this._quaternion},set:function(e){this._quaternion.set(e||[0,0,0,1]),Z.quaternionToEuler(this._quaternion,"XYZ",this._rotation),this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"scale",get:function(){return this._scale},set:function(e){}},{key:"matrix",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._matrix},set:function(e){this._matrix.set(e||hp),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),Z.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),Z.translateMat4v(this._position,this._matrix),this._matrixDirty=!1,this._setWorldMatrixDirty(),this._setWorldAABBDirty(),this.glRedraw()}},{key:"rotationMatrix",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrix}},{key:"_rebuildMatrices",value:function(){this._matrixDirty&&(Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrix),Z.conjugateQuaternion(this._quaternion,this._conjugateQuaternion),Z.quaternionToRotationMat4(this._quaternion,this._worldRotationMatrixConjugate),this._matrix.set(this._worldRotationMatrix),Z.translateMat4v(this._position,this._matrix),this._matrixDirty=!1)}},{key:"rotationMatrixConjugate",get:function(){return this._matrixDirty&&this._rebuildMatrices(),this._worldRotationMatrixConjugate}},{key:"_setWorldMatrixDirty",value:function(){this._matrixDirty=!0}},{key:"_setLocalAABBDirty",value:function(){for(var e=0,t=this._entityList.length;e0},set:function(e){e=!1!==e,this._visible=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._xrayed=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._highlighted=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._selected=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!!e,this._edges=e;for(var t=0,n=this._entityList.length;t0},set:function(e){e=!1!==e,this._pickable=e;for(var t=0,n=this._entityList.length;t0)e.colorsCompressed=new Uint8Array(e.colorsCompressed);else if(e.colors&&e.colors.length>0){for(var n=e.colors,r=new Uint8Array(n.length),i=0,a=n.length;i0){var t=Z.collapseAABB3();Z.expandAABB3Points3(t,e.positionsCompressed),An.decompressAABB(t,e.positionsDecodeMatrix),Z.AABB3ToOBB3(t,e.obb)}else if(e.positions&&e.positions.length>0){var n=Z.collapseAABB3();Z.expandAABB3Points3(n,e.positions),Z.AABB3ToOBB3(n,e.obb)}}(e.geometry)}e.numPrimitives=this._getNumPrimitives(e),this._vfcManager&&!this._vfcManager.finalized?this._vfcManager.addMesh(e):this._createMesh(e)}else this.error("[createMesh] SceneModel.createMesh() config missing: id")}},{key:"_createMesh",value:function(e){var t=new Js(this,e.id,e.color,e.opacity);t.pickId=this.scene._renderer.getPickID(t);var n=t.pickId,r=n>>24&255,i=n>>16&255,a=n>>8&255,s=255&n;switch(e.pickColor=new Uint8Array([s,a,i,r]),e.worldAABB=Z.collapseAABB3(),e.aabb=e.worldAABB,e.solid="solid"===e.primitive,t.origin=Z.vec3(e.origin),e.type){case 2:t.layer=this._getDTXLayer(e);break;case 1:t.layer=this._getVBOBatchingLayer(e);break;case 0:t.layer=this._getVBOInstancingLayer(e)}t.portionId=t.layer.createPortion(e),t.aabb=e.worldAABB,t.numPrimitives=e.numPrimitives,Z.expandAABB3(this._aabb,t.aabb),this._meshes[e.id]=t,this._meshList.push(t)}},{key:"_getNumPrimitives",value:function(e){var t=0;switch(e.geometry?e.geometry.primitive:e.primitive){case"triangles":case"solid":case"surface":switch(e.type){case 2:for(var n=0,r=e.buckets.length;n>>0).toString(16)}},{key:"_getVBOInstancingLayer",value:function(e){var t=this,n=e.origin,r=e.textureSetId||"-",i=e.geometryId,a="".concat(Math.round(n[0]),".").concat(Math.round(n[1]),".").concat(Math.round(n[2]),".").concat(r,".").concat(i),s=this._vboInstancingLayers[a];if(s)return s;for(var o=e.textureSet,l=e.geometry;!s;)switch(l.primitive){case"triangles":case"surface":console.log("[SceneModel ".concat(this.id,"]: creating TrianglesInstancingLayer")),s=new pu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0,solid:!1});break;case"solid":console.log("[SceneModel ".concat(this.id,"]: creating TrianglesInstancingLayer")),s=new pu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0,solid:!0});break;case"lines":console.log("[SceneModel ".concat(this.id,"]: creating LinesInstancingLayer")),s=new Nu({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0});break;case"points":console.log("[SceneModel ".concat(this.id,"]: creating PointsInstancingLayer")),s=new cc({model:t,textureSet:o,geometry:l,origin:n,layerIndex:0})}return this._vboInstancingLayers[a]=s,this.layerList.push(s),s}},{key:"createEntity",value:function(e){if(void 0===e.id?e.id=Z.createUUID():this.scene.components[e.id]&&(this.error("Scene already has a Component with this ID: ".concat(e.id," - will assign random ID")),e.id=Z.createUUID()),void 0!==e.meshIds){var t=0;if(this._visible&&!1!==e.visible&&(t|=Te),this._pickable&&!1!==e.pickable&&(t|=be),this._culled&&!1!==e.culled&&(t|=Ee),this._clippable&&!1!==e.clippable&&(t|=De),this._collidable&&!1!==e.collidable&&(t|=Pe),this._edges&&!1!==e.edges&&(t|=Be),this._xrayed&&!1!==e.xrayed&&(t|=Re),this._highlighted&&!1!==e.highlighted&&(t|=Ce),this._selected&&!1!==e.selected&&(t|=_e),e.flags=t,this._vfcManager&&!this._vfcManager.finalized){for(var n=0,r=e.meshIds.length;nt.sortId?1:0}));for(var s=0,o=this.layerList.length;s0&&0===this.renderFlags.numVisibleLayers?this.renderFlags.culled=!0:this._updateRenderFlags()}},{key:"_updateRenderFlagsVisibleLayers",value:function(){var e=this.renderFlags;e.numLayers=this.layerList.length,e.numVisibleLayers=0;for(var t=0,n=this.layerList.length;t0)for(var a=0;a0&&(e.colorTransparent=!0),this.numXRayedLayerPortions>0){var t=this.scene.xrayMaterial._state;t.fill&&(t.fillAlpha<1?e.xrayedSilhouetteTransparent=!0:e.xrayedSilhouetteOpaque=!0),t.edges&&(t.edgeAlpha<1?e.xrayedEdgesTransparent=!0:e.xrayedEdgesOpaque=!0)}if(this.numEdgesLayerPortions>0)this.scene.edgeMaterial._state.edges&&(e.edgesOpaque=this.numTransparentLayerPortions0&&(e.edgesTransparent=!0));if(this.numSelectedLayerPortions>0){var n=this.scene.selectedMaterial._state;n.fill&&(n.fillAlpha<1?e.selectedSilhouetteTransparent=!0:e.selectedSilhouetteOpaque=!0),n.edges&&(n.edgeAlpha<1?e.selectedEdgesTransparent=!0:e.selectedEdgesOpaque=!0)}if(this.numHighlightedLayerPortions>0){var r=this.scene.highlightMaterial._state;r.fill&&(r.fillAlpha<1?e.highlightedSilhouetteTransparent=!0:e.highlightedSilhouetteOpaque=!0),r.edges&&(r.edgeAlpha<1?e.highlightedEdgesTransparent=!0:e.highlightedEdgesOpaque=!0)}}}},{key:"drawColorOpaque",value:function(e){for(var t=this.renderFlags,n=0,r=t.visibleLayers.length;n2&&void 0!==arguments[2]&&arguments[2],r=e.positionsCompressed||[],i=Bc(e.indices||[],t),a=Nc(e.edgeIndices||[]);function s(e,t){if(e>t){var n=e;e=t,t=n}function r(n,r){return n!==e?e-n:r!==t?t-r:0}for(var i=0,s=(a.length>>1)-1;i<=s;){var o=s+i>>1,l=r(a[2*o],a[2*o+1]);if(l>0)i=o+1;else{if(!(l<0))return o;s=o-1}}return-i-1}var o=new Int32Array(a.length/2);o.fill(0);var l=r.length/3;if(l>8*(1<p.maxNumPositions&&(p=f()),p.bucketNumber>8)return[e];-1===u[h]&&(u[h]=p.numPositions++,p.positionsCompressed.push(r[3*h]),p.positionsCompressed.push(r[3*h+1]),p.positionsCompressed.push(r[3*h+2])),-1===u[I]&&(u[I]=p.numPositions++,p.positionsCompressed.push(r[3*I]),p.positionsCompressed.push(r[3*I+1]),p.positionsCompressed.push(r[3*I+2])),-1===u[y]&&(u[y]=p.numPositions++,p.positionsCompressed.push(r[3*y]),p.positionsCompressed.push(r[3*y+1]),p.positionsCompressed.push(r[3*y+2])),p.indices.push(u[h]),p.indices.push(u[I]),p.indices.push(u[y]);var m=void 0;(m=s(h,I))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]])),(m=s(h,y))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]])),(m=s(I,y))>=0&&0===o[m]&&(o[m]=1,p.edgeIndices.push(u[a[2*m]]),p.edgeIndices.push(u[a[2*m+1]]))}var w=t/8*2,g=t/8,T=2*r.length+(i.length+a.length)*w,E=0;return r.length,c.forEach((function(e){E+=2*e.positionsCompressed.length+(e.indices.length+e.edgeIndices.length)*g,e.positionsCompressed.length})),E>T?[e]:(n&&Lc(c,e),c)}({positionsCompressed:r,indices:i,edgeIndices:a},r.length/3>65536?16:8):s=[{positionsCompressed:r,indices:i,edgeIndices:a}];return s}var wp=function(e){I(n,yp);var t=m(n);function n(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),t.call(this,e,r)}return P(n)}(),gp=function(e){I(n,ve);var t=m(n);function n(e){var r,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return b(this,n),(r=t.call(this,e,i))._skyboxMesh=new fa(g(r),{geometry:new hn(g(r),{primitive:"triangles",positions:[1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,1,1,-1,-1,1,1,-1,1,1,1,1,1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,-1,-1,1,-1,1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1],uv:[.5,.6666,.25,.6666,.25,.3333,.5,.3333,.5,.6666,.5,.3333,.75,.3333,.75,.6666,.5,.6666,.5,1,.25,1,.25,.6666,.25,.6666,0,.6666,0,.3333,.25,.3333,.25,0,.5,0,.5,.3333,.25,.3333,.75,.3333,1,.3333,1,.6666,.75,.6666],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]}),background:!0,scale:[2e3,2e3,2e3],rotation:[0,-90,0],material:new gn(g(r),{ambient:[0,0,0],diffuse:[0,0,0],specular:[0,0,0],emissive:[1,1,1],emissiveMap:new Qa(g(r),{src:i.src,flipY:!0,wrapS:"clampToEdge",wrapT:"clampToEdge",encoding:i.encoding||"sRGB"}),backfaces:!0}),visible:!1,pickable:!1,clippable:!1,collidable:!1}),r.size=i.size,r.active=i.active,r}return P(n,[{key:"size",get:function(){return this._size},set:function(e){this._size=e||1e3,this._skyboxMesh.scale=[this._size,this._size,this._size]}},{key:"active",get:function(){return this._skyboxMesh.visible},set:function(e){this._skyboxMesh.visible=e}}]),n}(),Tp=function(){function e(){b(this,e)}return P(e,[{key:"transcode",value:function(e,t){}},{key:"destroy",value:function(){}}]),e}(),Ep=Z.vec4(),bp=Z.vec4(),Dp=Z.vec3(),Pp=Z.vec3(),Rp=Z.vec3(),Cp=Z.vec4(),_p=Z.vec4(),Bp=Z.vec4(),Op=function(){function e(t){b(this,e),this._scene=t}return P(e,[{key:"dollyToCanvasPos",value:function(e,t,n){var r=!1,i=this._scene.camera;if(e){var a=Z.subVec3(e,i.eye,Dp);r=Z.lenVec3(a)0&&void 0!==arguments[0])||arguments[0];I.style.cursor="move",w(),e&&g()}function w(){c=i.pointerCanvasPos[0],f=i.pointerCanvasPos[1],p=i.pointerCanvasPos[0],A=i.pointerCanvasPos[1]}function g(){u.pickCursorPos=i.pointerCanvasPos,u.schedulePickSurface=!0,u.update(),u.picked&&u.pickedSurface&&u.pickResult&&u.pickResult.worldPos?(d=!0,v.set(u.pickResult.worldPos)):d=!1}document.addEventListener("keydown",this._documentKeyDownHandler=function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled){var n=e.keyCode;y[n]=!0}}),document.addEventListener("keyup",this._documentKeyUpHandler=function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled){var n=e.keyCode;y[n]=!1}}),I.addEventListener("mousedown",this._mouseDownHandler=function(e){if(r.active&&r.pointerEnabled)switch(e.which){case 1:y[t.input.KEY_SHIFT]||r.planView?(s=!0,m()):(s=!0,m(!1));break;case 2:o=!0,m();break;case 3:l=!0,r.panRightClick&&m()}}),document.addEventListener("mousemove",this._documentMouseMoveHandler=function(){if(r.active&&r.pointerEnabled&&(s||o||l)){var e=t.canvas.boundary,n=e[2],u=e[3],p=i.pointerCanvasPos[0],A=i.pointerCanvasPos[1];if(y[t.input.KEY_SHIFT]||r.planView||!r.panRightClick&&o||r.panRightClick&&l){var h=p-c,I=A-f,m=t.camera;if("perspective"===m.projection){var w=Math.abs(d?Z.lenVec3(Z.subVec3(v,t.camera.eye,[])):t.camera.eyeLookDist)*Math.tan(m.perspective.fov/2*Math.PI/180);a.panDeltaX+=1.5*h*w/u,a.panDeltaY+=1.5*I*w/u}else a.panDeltaX+=.5*m.ortho.scale*(h/u),a.panDeltaY+=.5*m.ortho.scale*(I/u)}else!s||o||l||r.planView||(r.firstPerson?(a.rotateDeltaY-=(p-c)/n*r.dragRotationRate/2,a.rotateDeltaX+=(A-f)/u*(r.dragRotationRate/4)):(a.rotateDeltaY-=(p-c)/n*(1.5*r.dragRotationRate),a.rotateDeltaX+=(A-f)/u*(1.5*r.dragRotationRate)));c=p,f=A}}),I.addEventListener("mousemove",this._canvasMouseMoveHandler=function(e){r.active&&r.pointerEnabled&&i.mouseover&&(h=!0)}),document.addEventListener("mouseup",this._documentMouseUpHandler=function(e){if(r.active&&r.pointerEnabled)switch(e.which){case 1:case 2:case 3:s=!1,o=!1,l=!1}}),I.addEventListener("mouseup",this._mouseUpHandler=function(e){if(r.active&&r.pointerEnabled){if(3===e.which){!function(e,t){if(e){for(var n=e.target,r=0,i=0,a=0,s=0;n.offsetParent;)r+=n.offsetLeft,i+=n.offsetTop,a+=n.scrollLeft,s+=n.scrollTop,n=n.offsetParent;t[0]=e.pageX+a-r,t[1]=e.pageY+s-i}else e=window.event,t[0]=e.x,t[1]=e.y}(e,Gp);var t=Gp[0],i=Gp[1];Math.abs(t-p)<3&&Math.abs(i-A)<3&&n.cameraControl.fire("rightClick",{pagePos:[Math.round(e.pageX),Math.round(e.pageY)],canvasPos:Gp,event:e},!0)}I.style.removeProperty("cursor")}}),I.addEventListener("mouseenter",this._mouseEnterHandler=function(){r.active&&r.pointerEnabled});var T=1/60,E=null;I.addEventListener("wheel",this._mouseWheelHandler=function(e){if(r.active&&r.pointerEnabled){var t=performance.now()/1e3,n=null!==E?t-E:0;E=t,n>.05&&(n=.05),n0?n.cameraFlight.flyTo(Kp,(function(){n.pivotController.getPivoting()&&r.followPointer&&n.pivotController.showPivot()})):(n.cameraFlight.jumpTo(Kp),n.pivotController.getPivoting()&&r.followPointer&&n.pivotController.showPivot())}}}))}return P(e,[{key:"reset",value:function(){}},{key:"destroy",value:function(){this._scene.input.off(this._onSceneKeyDown)}}]),e}(),Xp=function(){function e(t,n,r,i,a){var s=this;b(this,e),this._scene=t;var o=n.pickController,l=n.pivotController,u=n.cameraControl;this._clicks=0,this._timeout=null,this._lastPickedEntityId=null;var c=!1,f=!1,p=this._scene.canvas.canvas,A=function(e){var r;e&&e.worldPos&&(r=e.worldPos);var i=e&&e.entity?e.entity.aabb:t.aabb;if(r){var a=t.camera;Z.subVec3(a.eye,a.look,[]),n.cameraFlight.flyTo({aabb:i})}else n.cameraFlight.flyTo({aabb:i})};p.addEventListener("mousemove",this._canvasMouseMoveHandler=function(e){if(r.active&&r.pointerEnabled&&!c&&!f){var n=u.hasSubs("hover"),a=u.hasSubs("hoverOut"),l=u.hasSubs("hoverOff"),p=u.hasSubs("hoverSurface"),A=u.hasSubs("hoverSnapOrSurface");if(n||a||l||p||A)if(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=!0,o.schedulePickSurface=p,o.scheduleSnapOrPick=A,o.update(),o.pickResult){if(o.pickResult.entity){var d=o.pickResult.entity.id;s._lastPickedEntityId!==d&&(void 0!==s._lastPickedEntityId&&u.fire("hoverOut",{entity:t.objects[s._lastPickedEntityId]},!0),u.fire("hoverEnter",o.pickResult,!0),s._lastPickedEntityId=d)}u.fire("hover",o.pickResult,!0),(o.pickResult.worldPos||o.pickResult.snappedWorldPos)&&u.fire("hoverSurface",o.pickResult,!0)}else void 0!==s._lastPickedEntityId&&(u.fire("hoverOut",{entity:t.objects[s._lastPickedEntityId]},!0),s._lastPickedEntityId=void 0),u.fire("hoverOff",{canvasPos:o.pickCursorPos},!0)}}),p.addEventListener("mousedown",this._canvasMouseDownHandler=function(e){if(1===e.which&&(c=!0),3===e.which&&(f=!0),1===e.which&&r.active&&r.pointerEnabled&&(i.mouseDownClientX=e.clientX,i.mouseDownClientY=e.clientY,i.mouseDownCursorX=i.pointerCanvasPos[0],i.mouseDownCursorY=i.pointerCanvasPos[1],!r.firstPerson&&r.followPointer&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickSurface=!0,o.update(),1===e.which))){var n=o.pickResult;n&&n.worldPos?(l.setPivotPos(n.worldPos),l.startPivot()):(r.smartPivot?l.setCanvasPivotPos(i.pointerCanvasPos):l.setPivotPos(t.camera.look),l.startPivot())}}),document.addEventListener("mouseup",this._documentMouseUpHandler=function(e){1===e.which&&(c=!1),3===e.which&&(f=!1)}),p.addEventListener("mouseup",this._canvasMouseUpHandler=function(e){if(r.active&&r.pointerEnabled&&(1===e.which&&(l.hidePivot(),!(Math.abs(e.clientX-i.mouseDownClientX)>3||Math.abs(e.clientY-i.mouseDownClientY)>3)))){var a=u.hasSubs("picked"),c=u.hasSubs("pickedNothing"),f=u.hasSubs("pickedSurface"),p=u.hasSubs("doublePicked"),d=u.hasSubs("doublePickedSurface"),v=u.hasSubs("doublePickedNothing");if(!(r.doublePickFlyTo||p||d||v))return(a||c||f)&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=!0,o.schedulePickSurface=f,o.update(),o.pickResult?(u.fire("picked",o.pickResult,!0),o.pickedSurface&&u.fire("pickedSurface",o.pickResult,!0)):u.fire("pickedNothing",{canvasPos:i.pointerCanvasPos},!0)),void(s._clicks=0);if(s._clicks++,1===s._clicks){o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=r.doublePickFlyTo,o.schedulePickSurface=f,o.update();var h=o.pickResult,I=o.pickedSurface;s._timeout=setTimeout((function(){h?(u.fire("picked",h,!0),I&&(u.fire("pickedSurface",h,!0),!r.firstPerson&&r.followPointer&&(n.pivotController.setPivotPos(h.worldPos),n.pivotController.startPivot()&&n.pivotController.showPivot()))):u.fire("pickedNothing",{canvasPos:i.pointerCanvasPos},!0),s._clicks=0}),r.doubleClickTimeFrame)}else{if(null!==s._timeout&&(window.clearTimeout(s._timeout),s._timeout=null),o.pickCursorPos=i.pointerCanvasPos,o.schedulePickEntity=r.doublePickFlyTo||p||d,o.schedulePickSurface=o.schedulePickEntity&&d,o.update(),o.pickResult){if(u.fire("doublePicked",o.pickResult,!0),o.pickedSurface&&u.fire("doublePickedSurface",o.pickResult,!0),r.doublePickFlyTo&&(A(o.pickResult),!r.firstPerson&&r.followPointer)){var y=o.pickResult.entity.aabb,m=Z.getAABB3Center(y);n.pivotController.setPivotPos(m),n.pivotController.startPivot()&&n.pivotController.showPivot()}}else if(u.fire("doublePickedNothing",{canvasPos:i.pointerCanvasPos},!0),r.doublePickFlyTo&&(A(),!r.firstPerson&&r.followPointer)){var w=t.aabb,g=Z.getAABB3Center(w);n.pivotController.setPivotPos(g),n.pivotController.startPivot()&&n.pivotController.showPivot()}s._clicks=0}}},!1)}return P(e,[{key:"reset",value:function(){this._clicks=0,this._lastPickedEntityId=null,this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}},{key:"destroy",value:function(){var e=this._scene.canvas.canvas;e.removeEventListener("mousemove",this._canvasMouseMoveHandler),e.removeEventListener("mousedown",this._canvasMouseDownHandler),document.removeEventListener("mouseup",this._documentMouseUpHandler),e.removeEventListener("mouseup",this._canvasMouseUpHandler),this._timeout&&(window.clearTimeout(this._timeout),this._timeout=null)}}]),e}(),qp=function(){function e(t,n,r,i,a){b(this,e),this._scene=t;var s=t.input,o=[],l=t.canvas.canvas,u=!0;this._onSceneMouseMove=s.on("mousemove",(function(){u=!0})),this._onSceneKeyDown=s.on("keydown",(function(e){r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&i.mouseover&&(o[e]=!0,e===s.KEY_SHIFT&&(l.style.cursor="move"))})),this._onSceneKeyUp=s.on("keyup",(function(e){r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&(o[e]=!1,e===s.KEY_SHIFT&&(l.style.cursor=null))})),this._onTick=t.on("tick",(function(e){if(r.active&&r.pointerEnabled&&t.input.keyboardEnabled&&i.mouseover){var l=n.cameraControl,c=e.deltaTime/1e3;if(!r.planView){var f=l._isKeyDownForAction(l.ROTATE_Y_POS,o),p=l._isKeyDownForAction(l.ROTATE_Y_NEG,o),A=l._isKeyDownForAction(l.ROTATE_X_POS,o),d=l._isKeyDownForAction(l.ROTATE_X_NEG,o),v=c*r.keyboardRotationRate;(f||p||A||d)&&(!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),f?a.rotateDeltaY+=v:p&&(a.rotateDeltaY-=v),A?a.rotateDeltaX+=v:d&&(a.rotateDeltaX-=v),!r.firstPerson&&r.followPointer&&n.pivotController.startPivot())}if(!o[s.KEY_CTRL]&&!o[s.KEY_ALT]){var h=l._isKeyDownForAction(l.DOLLY_BACKWARDS,o),I=l._isKeyDownForAction(l.DOLLY_FORWARDS,o);if(h||I){var y=c*r.keyboardDollyRate;!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),I?a.dollyDelta-=y:h&&(a.dollyDelta+=y),u&&(i.followPointerDirty=!0,u=!1)}}var m=l._isKeyDownForAction(l.PAN_FORWARDS,o),w=l._isKeyDownForAction(l.PAN_BACKWARDS,o),g=l._isKeyDownForAction(l.PAN_LEFT,o),T=l._isKeyDownForAction(l.PAN_RIGHT,o),E=l._isKeyDownForAction(l.PAN_UP,o),b=l._isKeyDownForAction(l.PAN_DOWN,o),D=(o[s.KEY_ALT]?.3:1)*c*r.keyboardPanRate;(m||w||g||T||E||b)&&(!r.firstPerson&&r.followPointer&&n.pivotController.startPivot(),b?a.panDeltaY+=D:E&&(a.panDeltaY+=-D),T?a.panDeltaX+=-D:g&&(a.panDeltaX+=D),w?a.panDeltaZ+=D:m&&(a.panDeltaZ+=-D))}}))}return P(e,[{key:"reset",value:function(){}},{key:"destroy",value:function(){this._scene.off(this._onTick),this._scene.input.off(this._onSceneMouseMove),this._scene.input.off(this._onSceneKeyDown),this._scene.input.off(this._onSceneKeyUp)}}]),e}(),Jp=Z.vec3(),Zp=function(){function e(t,n,r,i,a){b(this,e),this._scene=t;var s=t.camera,o=n.pickController,l=n.pivotController,u=n.panController,c=1,f=1,p=null;this._onTick=t.on("tick",(function(){if(r.active&&r.pointerEnabled){var e="default";if(Math.abs(a.dollyDelta)<.001&&(a.dollyDelta=0),Math.abs(a.rotateDeltaX)<.001&&(a.rotateDeltaX=0),Math.abs(a.rotateDeltaY)<.001&&(a.rotateDeltaY=0),0===a.rotateDeltaX&&0===a.rotateDeltaY||(a.dollyDelta=0),r.followPointer&&--c<=0&&(c=1,0!==a.dollyDelta)){if(0===a.rotateDeltaY&&0===a.rotateDeltaX&&r.followPointer&&i.followPointerDirty&&(o.pickCursorPos=i.pointerCanvasPos,o.schedulePickSurface=!0,o.update(),o.pickResult&&o.pickResult.worldPos?p=o.pickResult.worldPos:(f=1,p=null),i.followPointerDirty=!1),p){var n=Math.abs(Z.lenVec3(Z.subVec3(p,t.camera.eye,Jp)));f=n/r.dollyProximityThreshold}fr.longTapRadius||Math.abs(I)>r.longTapRadius)&&(clearTimeout(i.longTouchTimeout),i.longTouchTimeout=null),r.planView){var y=t.camera;if("perspective"===y.projection){var m=Math.abs(t.camera.eyeLookDist)*Math.tan(y.perspective.fov/2*Math.PI/180);a.panDeltaX+=h*m/l*r.touchPanRate,a.panDeltaY+=I*m/l*r.touchPanRate}else a.panDeltaX+=.5*y.ortho.scale*(h/l)*r.touchPanRate,a.panDeltaY+=.5*y.ortho.scale*(I/l)*r.touchPanRate}else a.rotateDeltaY-=h/o*(1*r.dragRotationRate),a.rotateDeltaX+=I/l*(1.5*r.dragRotationRate)}else if(2===d){var w=A[0],g=A[1];tA(w,u),tA(g,c);var T=Z.geometricMeanVec2(p[0],p[1]),E=Z.geometricMeanVec2(u,c),b=Z.vec2();Z.subVec2(T,E,b);var D=b[0],P=b[1],R=t.camera,C=Z.distVec2([w.pageX,w.pageY],[g.pageX,g.pageY]),_=(Z.distVec2(p[0],p[1])-C)*r.touchDollyRate;if(a.dollyDelta=_,Math.abs(_)<1)if("perspective"===R.projection){var B=s.pickResult?s.pickResult.worldPos:t.center,O=Math.abs(Z.lenVec3(Z.subVec3(B,t.camera.eye,[])))*Math.tan(R.perspective.fov/2*Math.PI/180);a.panDeltaX-=D*O/l*r.touchPanRate,a.panDeltaY-=P*O/l*r.touchPanRate}else a.panDeltaX-=.5*R.ortho.scale*(D/l)*r.touchPanRate,a.panDeltaY-=.5*R.ortho.scale*(P/l)*r.touchPanRate;i.pointerCanvasPos=E}for(var S=0;S-1&&t-f<150&&(p>-1&&f-p<325?(rA(a[0],o.pickCursorPos),o.schedulePickEntity=!0,o.schedulePickSurface=s,o.update(),o.pickResult?(o.pickResult.touchInput=!0,l.fire("doublePicked",o.pickResult),o.pickedSurface&&l.fire("doublePickedSurface",o.pickResult),r.doublePickFlyTo&&d(o.pickResult)):(l.fire("doublePickedNothing"),r.doublePickFlyTo&&d()),p=-1):Z.distVec2(u[0],c)<4&&(rA(a[0],o.pickCursorPos),o.schedulePickEntity=!0,o.schedulePickSurface=s,o.update(),o.pickResult?(o.pickResult.touchInput=!0,l.fire("picked",o.pickResult),o.pickedSurface&&l.fire("pickedSurface",o.pickResult)):l.fire("pickedNothing"),p=t),f=-1),u.length=n.length;for(var A=0,v=n.length;A1&&void 0!==arguments[1]?arguments[1]:{};b(this,n),(r=t.call(this,e,i)).PAN_LEFT=0,r.PAN_RIGHT=1,r.PAN_UP=2,r.PAN_DOWN=3,r.PAN_FORWARDS=4,r.PAN_BACKWARDS=5,r.ROTATE_X_POS=6,r.ROTATE_X_NEG=7,r.ROTATE_Y_POS=8,r.ROTATE_Y_NEG=9,r.DOLLY_FORWARDS=10,r.DOLLY_BACKWARDS=11,r.AXIS_VIEW_RIGHT=12,r.AXIS_VIEW_BACK=13,r.AXIS_VIEW_LEFT=14,r.AXIS_VIEW_FRONT=15,r.AXIS_VIEW_TOP=16,r.AXIS_VIEW_BOTTOM=17,r._keyMap={},r.scene.canvas.canvas.oncontextmenu=function(e){e.preventDefault()},r._configs={longTapTimeout:600,longTapRadius:5,active:!0,keyboardLayout:"qwerty",navMode:"orbit",planView:!1,firstPerson:!1,followPointer:!0,doublePickFlyTo:!0,panRightClick:!0,showPivot:!1,pointerEnabled:!0,constrainVertical:!1,smartPivot:!1,doubleClickTimeFrame:250,snapMode:"vertex",snapRadius:30,dragRotationRate:360,keyboardRotationRate:90,rotationInertia:0,keyboardPanRate:1,touchPanRate:1,panInertia:.5,keyboardDollyRate:10,mouseWheelDollyRate:100,touchDollyRate:.2,dollyInertia:0,dollyProximityThreshold:30,dollyMinSpeed:.04},r._states={pointerCanvasPos:Z.vec2(),mouseover:!1,followPointerDirty:!0,mouseDownClientX:0,mouseDownClientY:0,mouseDownCursorX:0,mouseDownCursorY:0,touchStartTime:null,activeTouches:[],tapStartPos:Z.vec2(),tapStartTime:-1,lastTapTime:-1,longTouchTimeout:null},r._updates={rotateDeltaX:0,rotateDeltaY:0,panDeltaX:0,panDeltaY:0,panDeltaZ:0,dollyDelta:0};var a=r.scene;return r._controllers={cameraControl:g(r),pickController:new Up(g(r),r._configs),pivotController:new Hp(a,r._configs),panController:new Op(a),cameraFlight:new _s(g(r),{duration:.5})},r._handlers=[new $p(r.scene,r._controllers,r._configs,r._states,r._updates),new nA(r.scene,r._controllers,r._configs,r._states,r._updates),new kp(r.scene,r._controllers,r._configs,r._states,r._updates),new Yp(r.scene,r._controllers,r._configs,r._states,r._updates),new Xp(r.scene,r._controllers,r._configs,r._states,r._updates),new iA(r.scene,r._controllers,r._configs,r._states,r._updates),new qp(r.scene,r._controllers,r._configs,r._states,r._updates)],r._cameraUpdater=new Zp(r.scene,r._controllers,r._configs,r._states,r._updates),r.navMode=i.navMode,i.planView&&(r.planView=i.planView),r.constrainVertical=i.constrainVertical,i.keyboardLayout?r.keyboardLayout=i.keyboardLayout:r.keyMap=i.keyMap,r.doublePickFlyTo=i.doublePickFlyTo,r.panRightClick=i.panRightClick,r.active=i.active,r.followPointer=i.followPointer,r.rotationInertia=i.rotationInertia,r.keyboardPanRate=i.keyboardPanRate,r.touchPanRate=i.touchPanRate,r.keyboardRotationRate=i.keyboardRotationRate,r.dragRotationRate=i.dragRotationRate,r.touchDollyRate=i.touchDollyRate,r.dollyInertia=i.dollyInertia,r.dollyProximityThreshold=i.dollyProximityThreshold,r.dollyMinSpeed=i.dollyMinSpeed,r.panInertia=i.panInertia,r.pointerEnabled=!0,r.keyboardDollyRate=i.keyboardDollyRate,r.mouseWheelDollyRate=i.mouseWheelDollyRate,r}return P(n,[{key:"keyMap",get:function(){return this._keyMap},set:function(e){if(e=e||"qwerty",ae.isString(e)){var t=this.scene.input,n={};switch(e){default:this.error("Unsupported value for 'keyMap': "+e+" defaulting to 'qwerty'");case"qwerty":n[this.PAN_LEFT]=[t.KEY_A],n[this.PAN_RIGHT]=[t.KEY_D],n[this.PAN_UP]=[t.KEY_Z],n[this.PAN_DOWN]=[t.KEY_X],n[this.PAN_BACKWARDS]=[],n[this.PAN_FORWARDS]=[],n[this.DOLLY_FORWARDS]=[t.KEY_W,t.KEY_ADD],n[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],n[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],n[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],n[this.ROTATE_Y_POS]=[t.KEY_Q,t.KEY_LEFT_ARROW],n[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],n[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],n[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],n[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],n[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],n[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],n[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6];break;case"azerty":n[this.PAN_LEFT]=[t.KEY_Q],n[this.PAN_RIGHT]=[t.KEY_D],n[this.PAN_UP]=[t.KEY_W],n[this.PAN_DOWN]=[t.KEY_X],n[this.PAN_BACKWARDS]=[],n[this.PAN_FORWARDS]=[],n[this.DOLLY_FORWARDS]=[t.KEY_Z,t.KEY_ADD],n[this.DOLLY_BACKWARDS]=[t.KEY_S,t.KEY_SUBTRACT],n[this.ROTATE_X_POS]=[t.KEY_DOWN_ARROW],n[this.ROTATE_X_NEG]=[t.KEY_UP_ARROW],n[this.ROTATE_Y_POS]=[t.KEY_A,t.KEY_LEFT_ARROW],n[this.ROTATE_Y_NEG]=[t.KEY_E,t.KEY_RIGHT_ARROW],n[this.AXIS_VIEW_RIGHT]=[t.KEY_NUM_1],n[this.AXIS_VIEW_BACK]=[t.KEY_NUM_2],n[this.AXIS_VIEW_LEFT]=[t.KEY_NUM_3],n[this.AXIS_VIEW_FRONT]=[t.KEY_NUM_4],n[this.AXIS_VIEW_TOP]=[t.KEY_NUM_5],n[this.AXIS_VIEW_BOTTOM]=[t.KEY_NUM_6]}this._keyMap=n}else{var r=e;this._keyMap=r}}},{key:"_isKeyDownForAction",value:function(e,t){var n=this._keyMap[e];if(!n)return!1;t||(t=this.scene.input.keyDown);for(var r=0,i=n.length;r1&&void 0!==arguments[1]?arguments[1]:{};if(this.finalized)throw"MetaScene already finalized - can't add more data";this._globalizeIDs(e,t);var n=this.metaScene;if(e.propertySets)for(var r=0,i=e.propertySets.length;r0?fA(t):null,s=n&&n.length>0?fA(n):null;return function e(t){if(t){var n=!0;(s&&s[t.type]||a&&!a[t.type])&&(n=!1),n&&r.push(t.id);var i=t.children;if(i)for(var o=0,l=i.length;o * Copyright (c) 2022 Niklas von Hertzen