diff --git a/src/viewer/scene/model/dtx/triangles/TrianglesDataTextureRenderers.js b/src/viewer/scene/model/dtx/triangles/TrianglesDataTextureRenderers.js index 594a8cd6fc..681b94b967 100644 --- a/src/viewer/scene/model/dtx/triangles/TrianglesDataTextureRenderers.js +++ b/src/viewer/scene/model/dtx/triangles/TrianglesDataTextureRenderers.js @@ -6,7 +6,6 @@ import {TrianglesDataTexturePickMeshRenderer} from "./renderers/TrianglesDataTex import {TrianglesDataTexturePickDepthRenderer} from "./renderers/TrianglesDataTexturePickDepthRenderer.js"; import {TrianglesDataTextureSnapDepthRenderer} from "./renderers/TrianglesDataTextureSnapDepthRenderer.js"; import {TrianglesDataTextureSnapDepthBufInitRenderer} from "./renderers/TrianglesDataTextureSnapDepthBufInitRenderer.js"; -import {TrianglesDataTexturePickNormalsRenderer} from "./renderers/TrianglesDataTexturePickNormalsRenderer.js"; import {TrianglesDataTextureOcclusionRenderer} from "./renderers/TrianglesDataTextureOcclusionRenderer.js"; import {TrianglesDataTextureDepthRenderer} from "./renderers/TrianglesDataTextureDepthRenderer.js"; import {TrianglesDataTextureNormalsRenderer} from "./renderers/TrianglesDataTextureNormalsRenderer.js"; @@ -107,6 +106,9 @@ class TrianglesDataTextureRenderers { if (!this._pickDepthRenderer) { this._pickDepthRenderer = new TrianglesDataTexturePickDepthRenderer(this._scene); } + if (!this._pickNormalsRenderer) { + this._pickNormalsRenderer = new TrianglesDataTexturePickNormalsFlatRenderer(this._scene); + } if (!this._vertexDepthRenderer) { this._vertexDepthRenderer = new TrianglesDataTextureSnapDepthRenderer(this._scene); } @@ -131,9 +133,9 @@ class TrianglesDataTextureRenderers { } get colorQualityRendererWithSAO() { - if (!this._colorQualityRendererWithSAO) { - this._colorQualityRendererWithSAO = new TrianglesDataTextureColorQualityRenderer(this._scene, true); - } + // if (!this._colorQualityRendererWithSAO) { + // this._colorQualityRendererWithSAO = new TrianglesDataTextureColorQualityRenderer(this._scene, true); + // } return this._colorQualityRendererWithSAO; } @@ -181,7 +183,7 @@ class TrianglesDataTextureRenderers { get pickNormalsRenderer() { if (!this._pickNormalsRenderer) { - this._pickNormalsRenderer = new TrianglesDataTexturePickNormalsRenderer(this._scene); + this._pickNormalsRenderer = new TrianglesDataTexturePickNormalsFlatRenderer(this._scene); } return this._pickNormalsRenderer; } diff --git a/src/viewer/scene/model/dtx/triangles/renderers/TrianglesDataTexturePickNormalsFlatRenderer.js b/src/viewer/scene/model/dtx/triangles/renderers/TrianglesDataTexturePickNormalsFlatRenderer.js index f1ec3ebdb3..c1c51a33f9 100644 --- a/src/viewer/scene/model/dtx/triangles/renderers/TrianglesDataTexturePickNormalsFlatRenderer.js +++ b/src/viewer/scene/model/dtx/triangles/renderers/TrianglesDataTexturePickNormalsFlatRenderer.js @@ -1,111 +1,109 @@ import {Program} from "../../../../webgl/Program.js"; -import {createRTCViewMat, getPlaneRTCPos} from "../../../../math/rtcCoords.js"; import {math} from "../../../../math/math.js"; +import {createRTCViewMat, getPlaneRTCPos} from "../../../../math/rtcCoords.js"; const tempVec3a = math.vec3(); +const tempVec3b = math.vec3(); +const tempVec3c = math.vec3(); +const tempVec3d = math.vec3(); + +const tempVec4a = math.vec4(); + +const tempMat4a = math.mat4(); /** * @private */ export class TrianglesDataTexturePickNormalsFlatRenderer { - constructor(scene) { + constructor(scene, withSAO) { this._scene = scene; + this._withSAO = withSAO; this._hash = this._getHash(); this._allocate(); } getValid() { return this._hash === this._getHash(); - } + }; _getHash() { - return this._scene._sectionPlanesState.getHash(); + const scene = this._scene; + return [scene._lightsState.getHash(), scene._sectionPlanesState.getHash(), (this._withSAO ? "sao" : "nosao")].join(";"); } drawLayer(frameCtx, dataTextureLayer, renderPass) { - const model = dataTextureLayer.model; - const scene = model.scene; + + const scene = this._scene; const camera = scene.camera; + const model = dataTextureLayer.model; const gl = scene.canvas.gl; const state = dataTextureLayer._state; + const textureState = state.textureState; const origin = dataTextureLayer._state.origin; + const {position, rotationMatrix, rotationMatrixConjugate} = model; if (!this._program) { - this._allocate(dataTextureLayer); + this._allocate(); + if (this.errors) { + return; + } } if (frameCtx.lastProgramId !== this._program.id) { frameCtx.lastProgramId = this._program.id; - this._bindProgram(); + this._bindProgram(frameCtx, state); } - var rr = this._program.bindTexture( - this._uTexturePerObjectIdPositionsDecodeMatrix, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerObjectIdPositionsDecodeMatrix); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 1 - ); - - var rr2 = this._program.bindTexture( - this._uTexturePerVertexIdCoordinates, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerVertexIdCoordinates); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 2 - ); - - var rr3 = this._program.bindTexture( + textureState.bindCommonTextures( + this._program, + this._uTexturePerObjectIdPositionsDecodeMatrix, + this._uTexturePerVertexIdCoordinates, this._uTexturePerObjectIdColorsAndFlags, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerObjectIdColorsAndFlags); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 3 - ); - - gl.uniform1i(this._uRenderPass, renderPass); - gl.uniform1i(this._uPickInvisible, frameCtx.pickInvisible); + this._uTextureModelMatrices, + this._uTexturePerObjectIdOffsets + ); + + let rtcViewMatrix; + let rtcCameraEye; + + const gotOrigin = (origin[0] !== 0 || origin[1] !== 0 || origin[2] !== 0); + const gotPosition = (position[0] !== 0 || position[1] !== 0 || position[2] !== 0); + if (gotOrigin || gotPosition) { + const rtcOrigin = tempVec3a; + if (gotOrigin) { + const rotatedOrigin = math.transformPoint3(rotationMatrix, origin, tempVec3b); + 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); + rtcCameraEye = tempVec3c; + 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; // TODO: make pickMatrix + rtcCameraEye = camera.eye; + } gl.uniform2fv(this._uPickClipPos, frameCtx.pickClipPos); gl.uniform2f(this._uDrawingBufferSize, gl.drawingBufferWidth, gl.drawingBufferHeight); - - gl.uniformMatrix4fv(this._uWorldMatrix, false, model.worldMatrix); - - const pickViewMatrix = frameCtx.pickViewMatrix || camera.viewMatrix; - const viewMatrix = origin ? createRTCViewMat(pickViewMatrix, origin) : pickViewMatrix; - - gl.uniformMatrix4fv(this._uViewMatrix, false, viewMatrix); - gl.uniformMatrix4fv(this._uProjMatrix, false, frameCtx.pickProjMatrix); - + gl.uniformMatrix4fv(this._uSceneModelWorldMatrix, false, rotationMatrixConjugate); + gl.uniformMatrix4fv(this._uViewMatrix, false, rtcViewMatrix); + gl.uniformMatrix4fv(this._uProjMatrix, false, camera.projMatrix); // TODO: pickProjMatrix + gl.uniform3fv(this._uCameraEyeRtc, rtcCameraEye); + gl.uniform1i(this._uRenderPass, renderPass); if (scene.logarithmicDepthBufferEnabled) { - const logDepthBufFC = 2.0 / (Math.log(camera.project.far + 1.0) / Math.LN2); // TODO: Far should be from projection matrix? + const logDepthBufFC = 2.0 / (Math.log(frameCtx.pickZFar + 1.0) / Math.LN2); gl.uniform1f(this._uLogDepthBufFC, logDepthBufFC); } - const numSectionPlanes = scene._sectionPlanesState.sectionPlanes.length; if (numSectionPlanes > 0) { const sectionPlanes = scene._sectionPlanesState.sectionPlanes; @@ -113,161 +111,73 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { const renderFlags = model.renderFlags; for (let sectionPlaneIndex = 0; sectionPlaneIndex < numSectionPlanes; sectionPlaneIndex++) { const sectionPlaneUniforms = this._uSectionPlanes[sectionPlaneIndex]; - const active = renderFlags.sectionPlanesActivePerLayer[baseIndex + sectionPlaneIndex]; - gl.uniform1i(sectionPlaneUniforms.active, active ? 1 : 0); - if (active) { - const sectionPlane = sectionPlanes[sectionPlaneIndex]; - if (origin) { - const rtcSectionPlanePos = getPlaneRTCPos(sectionPlane.dist, sectionPlane.dir, origin, tempVec3a); - gl.uniform3fv(sectionPlaneUniforms.pos, rtcSectionPlanePos); - } else { - gl.uniform3fv(sectionPlaneUniforms.pos, sectionPlane.pos); + if (sectionPlaneUniforms) { + const active = renderFlags.sectionPlanesActivePerLayer[baseIndex + sectionPlaneIndex]; + gl.uniform1i(sectionPlaneUniforms.active, active ? 1 : 0); + if (active) { + const sectionPlane = sectionPlanes[sectionPlaneIndex]; + if (origin) { + const rtcSectionPlanePos = getPlaneRTCPos(sectionPlane.dist, sectionPlane.dir, origin, tempVec3d); + gl.uniform3fv(sectionPlaneUniforms.pos, rtcSectionPlanePos); + } else { + gl.uniform3fv(sectionPlaneUniforms.pos, sectionPlane.pos); + } + gl.uniform3fv(sectionPlaneUniforms.dir, sectionPlane.dir); } - gl.uniform3fv(sectionPlaneUniforms.dir, sectionPlane.dir); } } } - //============================================================= - // TODO: Use drawElements count and offset to draw only one entity - //============================================================= - if (state.numIndices8Bits > 0) { - var rr4 = this._program.bindTexture( - this._uTexturePerPolygonIdPortionIds, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdPortionIds8Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 4 - ); - - var rr5 = this._program.bindTexture( - this._uTexturePerPolygonIdIndices, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdIndices8Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 5 - ); - + textureState.bindTriangleIndicesTextures( + this._program, + this._uTexturePerPolygonIdPortionIds, + this._uTexturePerPolygonIdIndices, + 8 // 8 bits indices + ); gl.drawArrays(gl.TRIANGLES, 0, state.numIndices8Bits); } if (state.numIndices16Bits > 0) { - var rr4 = this._program.bindTexture( - this._uTexturePerPolygonIdPortionIds, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdPortionIds16Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 4 - ); - - var rr5 = this._program.bindTexture( - this._uTexturePerPolygonIdIndices, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdIndices16Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 5 - ); - + textureState.bindTriangleIndicesTextures( + this._program, + this._uTexturePerPolygonIdPortionIds, + this._uTexturePerPolygonIdIndices, + 16 // 16 bits indices + ); gl.drawArrays(gl.TRIANGLES, 0, state.numIndices16Bits); } if (state.numIndices32Bits > 0) { - var rr4 = this._program.bindTexture( - this._uTexturePerPolygonIdPortionIds, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdPortionIds32Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 4 - ); - - var rr5 = this._program.bindTexture( - this._uTexturePerPolygonIdIndices, - { - bind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, state.texturePerPolygonIdIndices32Bits); - return true; - }, - unbind: function (unit) { - gl.activeTexture(gl["TEXTURE" + unit]); - gl.bindTexture(gl.TEXTURE_2D, null); - } - }, - 5 - ); - + textureState.bindTriangleIndicesTextures( + this._program, + this._uTexturePerPolygonIdPortionIds, + this._uTexturePerPolygonIdIndices, + 32 // 32 bits indices + ); gl.drawArrays(gl.TRIANGLES, 0, state.numIndices32Bits); } - frameCtx.drawElements++; } _allocate() { - const scene = this._scene; const gl = scene.canvas.gl; - this._program = new Program(gl, this._buildShader()); - if (this._program.errors) { this.errors = this._program.errors; return; } - const program = this._program; - this._uRenderPass = program.getLocation("renderPass"); this._uPickInvisible = program.getLocation("pickInvisible"); this._uPickClipPos = program.getLocation("pickClipPos"); this._uDrawingBufferSize = program.getLocation("drawingBufferSize"); - this._uPositionsDecodeMatrix = program.getLocation("positionsDecodeMatrix"); - this._uWorldMatrix = program.getLocation("worldMatrix"); + this._uSceneModelWorldMatrix = program.getLocation("sceneModelWorldMatrix"); this._uViewMatrix = program.getLocation("viewMatrix"); this._uProjMatrix = program.getLocation("projMatrix"); this._uSectionPlanes = []; - for (let i = 0, len = scene._sectionPlanesState.sectionPlanes.length; i < len; i++) { this._uSectionPlanes.push({ active: program.getLocation("sectionPlaneActive" + i), @@ -275,24 +185,30 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { dir: program.getLocation("sectionPlaneDir" + i) }); } - - this._aPackedVertexId = program.getAttribute("packedVertexId"); - - if (scene.logarithmicDepthBufferEnabled) { this._uLogDepthBufFC = program.getLocation("logDepthBufFC"); } - - this._uTexturePerObjectIdPositionsDecodeMatrix = "uTexturePerObjectIdPositionsDecodeMatrix"; - this._uTexturePerObjectIdColorsAndFlags = "uTexturePerObjectIdColorsAndFlags"; - this._uTexturePerVertexIdCoordinates = "uTexturePerVertexIdCoordinates"; - this._uTexturePerPolygonIdNormals = "uTexturePerPolygonIdNormals"; - this._uTexturePerPolygonIdIndices = "uTexturePerPolygonIdIndices"; - this._uTexturePerPolygonIdPortionIds = "uTexturePerPolygonIdPortionIds"; + this._uTexturePerObjectIdPositionsDecodeMatrix = "uTexturePerObjectIdPositionsDecodeMatrix"; + this._uTexturePerObjectIdColorsAndFlags = "uTexturePerObjectIdColorsAndFlags"; + this._uTexturePerVertexIdCoordinates = "uTexturePerVertexIdCoordinates"; + this._uTexturePerPolygonIdNormals = "uTexturePerPolygonIdNormals"; + this._uTexturePerPolygonIdIndices = "uTexturePerPolygonIdIndices"; + this._uTexturePerPolygonIdPortionIds = "uTexturePerPolygonIdPortionIds"; + this._uTextureModelMatrices = "uTextureModelMatrices"; + this._uTexturePerObjectIdOffsets = "uTexturePerObjectIdOffsets"; + this._uCameraEyeRtc = program.getLocation("uCameraEyeRtc"); } - _bindProgram() { - this._program.bind(); + _bindProgram(frameCtx) { + const scene = this._scene; + const gl = scene.canvas.gl; + const program = this._program; + const project = scene.camera.project; + program.bind(); + if (scene.logarithmicDepthBufferEnabled) { + const logDepthBufFC = 2.0 / (Math.log(project.far + 1.0) / Math.LN2); + gl.uniform1f(this._uLogDepthBufFC, logDepthBufFC); + } } _buildShader() { @@ -304,10 +220,13 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { _buildVertexShader() { const scene = this._scene; - const clipping = scene._sectionPlanesState.sectionPlanes.length > 0; + const sectionPlanesState = scene._sectionPlanesState; + const lightsState = scene._lightsState; + const clipping = sectionPlanesState.sectionPlanes.length > 0; + let light; const src = []; src.push("#version 300 es"); - src.push("// Triangles dataTexture pick flat normals vertex shader"); + src.push("// trianglesDatatextureNormalsRenderer vertex shader"); src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"); src.push("precision highp float;"); @@ -325,123 +244,147 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { src.push("uniform int renderPass;"); - src.push("in uvec3 packedVertexId;"); - if (scene.entityOffsetsEnabled) { src.push("in vec3 offset;"); } - src.push("uniform bool pickInvisible;"); - src.push("uniform mat4 worldMatrix;"); + src.push("uniform mat4 sceneModelWorldMatrix;"); src.push("uniform mat4 viewMatrix;"); src.push("uniform mat4 projMatrix;"); - // src.push("uniform sampler2D uOcclusionTexture;"); - src.push("uniform sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"); - src.push("uniform usampler2D uTexturePerObjectIdColorsAndFlags;"); - src.push("uniform usampler2D uTexturePerVertexIdCoordinates;"); - src.push("uniform usampler2D uTexturePerPolygonIdIndices;"); - src.push("uniform isampler2D uTexturePerPolygonIdNormals;"); - src.push("uniform usampler2D uTexturePerPolygonIdPortionIds;"); + + src.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"); + src.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"); + src.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"); + src.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"); + src.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"); + src.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"); + src.push("uniform highp sampler2D uTextureModelMatrices;"); + src.push("uniform vec3 uCameraEyeRtc;"); + + src.push("vec3 positions[3];") if (scene.logarithmicDepthBufferEnabled) { src.push("uniform float logDepthBufFC;"); 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;"); } - src.push("uniform vec2 drawingBufferSize;"); src.push("uniform vec2 pickClipPos;"); + src.push("uniform vec2 drawingBufferSize;"); src.push("vec4 remapClipPos(vec4 clipPos) {"); - src.push(" clipPos.xy /= clipPos.w;"); - src.push(` clipPos.xy = (clipPos.xy - pickClipPos) * (drawingBufferSize / 3.0);`); + src.push(" clipPos.xy /= clipPos.w;") + src.push(` clipPos.xy = (clipPos.xy - pickClipPos) * drawingBufferSize;`); src.push(" clipPos.xy *= clipPos.w;") src.push(" return clipPos;") src.push("}"); + src.push("bool isPerspectiveMatrix(mat4 m) {"); + src.push(" return (m[2][3] == - 1.0);"); + src.push("}"); + src.push("out vec4 vWorldPosition;"); + if (clipping) { - src.push("out int vFlags2;"); + src.push("flat out uint vFlags2;"); } + src.push("void main(void) {"); // constants - // src.push("int objectIndex = int(packedVertexId.g) & 4095;"); src.push("int polygonIndex = gl_VertexID / 3;") - src.push("int h_normal_index = polygonIndex & 4095;") - src.push("int v_normal_index = polygonIndex >> 12;") - // get packed object-id - src.push("int h_packed_object_id_index = ((polygonIndex >> 3) / 2) & 4095;") - src.push("int v_packed_object_id_index = ((polygonIndex >> 3) / 2) >> 12;") - - src.push("ivec3 packedObjectId = ivec3(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).rgb);"); + src.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;") + src.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;") - src.push("int objectIndex;") - src.push("if (((polygonIndex >> 3) % 2) == 0) {") - src.push(" objectIndex = (packedObjectId.r << 4) + (packedObjectId.g >> 4);") - src.push("} else {") - src.push(" objectIndex = ((packedObjectId.g & 15) << 8) + packedObjectId.b;") - src.push("}") + src.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);"); src.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);"); - // get vertex base - src.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"); + // get flags & flags2 + src.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"); + src.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"); // pickFlag = NOT_RENDERED | PICK - src.push("int h_index = polygonIndex & 4095;") - src.push("int v_index = polygonIndex >> 12;") + // renderPass = PICK + src.push(`if (int(flags.w) != renderPass) {`); + src.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"); // Cull vertex + src.push(" return;"); // Cull vertex + src.push("} else {"); + // get vertex base + src.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"); + src.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"); + src.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;"); + src.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;") + src.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;") src.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));"); src.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;") - src.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;") src.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;") - src.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));") - - // get flags & flags2 - src.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"); - src.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"); - + src.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"); // get position - src.push("vec3 position1 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));") - src.push("vec3 position2 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));") - src.push("vec3 position3 = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));") + src.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));") + src.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));") + src.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));") + + // get color + src.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"); + + src.push(`if (color.a == 0u) {`); + src.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"); // Cull vertex + src.push(" return;"); + src.push("};"); // get normal - src.push("vec3 normal = normalize(cross(position3 - position1, position2 - position1));"); - src.push("int vertexNumber = gl_VertexID % 3;"); + src.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));"); + src.push("vec3 position;"); - src.push("if (vertexNumber == 0) position = position1;"); - src.push("else if (vertexNumber == 1) position = position2;"); - src.push("else position = position3;"); + src.push("position = positions[gl_VertexID % 3];"); + + src.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"); + + // when the geometry is not solid, if needed, flip the triangle winding + src.push("if (solid != 1u) {"); + src.push("if (isPerspectiveMatrix(projMatrix)) {"); + src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(sceneModelWorldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;") + // src.push("vColor = vec4(vec3(1, -1, 0)*dot(normalize(position.xyz - uCameraEyeRtcInQuantizedSpace), normal), 1);") + src.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {"); + src.push("position = positions[2 - (gl_VertexID % 3)];"); + src.push("viewNormal = -viewNormal;"); + src.push("}"); + src.push("} else {"); + src.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);"); + src.push("if (viewNormal.z < 0.0) {"); + src.push("position = positions[2 - (gl_VertexID % 3)];"); + src.push("}"); + src.push("}"); + src.push("}"); + + src.push("vec4 worldPosition = sceneModelWorldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); "); + + // get XYZ offset + 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; "); - // flags.w = NOT_RENDERED | PICK - // renderPass = PICK - src.push(`if (int(flags.w) != 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; "); - src.push(" vWorldPosition = worldPosition;"); - if (clipping) { - src.push(" vFlags2 = flags2.r;"); - } src.push("vec4 clipPos = projMatrix * viewPosition;"); + if (scene.logarithmicDepthBufferEnabled) { src.push("vFragDepth = 1.0 + clipPos.w;"); src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));"); } + + src.push("vWorldPosition = worldPosition;"); + + if (clipping) { + src.push("vFlags2 = flags2.r;"); + } src.push("gl_Position = remapClipPos(clipPos);"); - src.push(" }"); + src.push("}"); src.push("}"); return src; } @@ -451,12 +394,8 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { const sectionPlanesState = scene._sectionPlanesState; const clipping = sectionPlanesState.sectionPlanes.length > 0; const src = []; - src.push ('#version 300 es'); - src.push("// Triangles dataTexture pick flat normals fragment shader"); - src.push("#extension GL_OES_standard_derivatives : enable"); - if (scene.logarithmicDepthBufferEnabled) { - src.push("#extension GL_EXT_frag_depth : enable"); - } + src.push('#version 300 es'); + src.push("// TrianglesDataTexturePickNormalsRenderer fragment shader"); src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"); src.push("precision highp float;"); src.push("precision highp int;"); @@ -471,8 +410,8 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { } src.push("in vec4 vWorldPosition;"); if (clipping) { - src.push("in int vFlags2;"); - for (var i = 0; i < sectionPlanesState.sectionPlanes.length; i++) { + src.push("flat in uint vFlags2;"); + for (let i = 0, len = sectionPlanesState.sectionPlanes.length; i < len; i++) { src.push("uniform bool sectionPlaneActive" + i + ";"); src.push("uniform vec3 sectionPlanePos" + i + ";"); src.push("uniform vec3 sectionPlaneDir" + i + ";"); @@ -481,16 +420,18 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { src.push("out highp ivec4 outNormal;"); src.push("void main(void) {"); if (clipping) { - src.push(" bool clippable = vFlags2 > 0;"); + src.push(" bool clippable = vFlags2 > 0u;"); src.push(" if (clippable) {"); - src.push(" float dist = 0.0;"); - for (var i = 0; i < sectionPlanesState.sectionPlanes.length; i++) { - src.push(" if (sectionPlaneActive" + i + ") {"); - src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);"); - src.push(" }"); + src.push(" float dist = 0.0;"); + for (let i = 0, len = sectionPlanesState.sectionPlanes.length; i < len; i++) { + src.push("if (sectionPlaneActive" + i + ") {"); + src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);"); + src.push("}"); } - src.push(" if (dist > 0.0) { discard; }"); + src.push(" if (dist > 0.0) { "); + src.push(" discard;") src.push(" }"); + src.push("}"); } if (scene.logarithmicDepthBufferEnabled) { src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;"); @@ -514,4 +455,3 @@ export class TrianglesDataTexturePickNormalsFlatRenderer { this._program = null; } } -