diff --git a/viewer/packages/rendering/src/glsl/base/determineNodeAppearance.glsl b/viewer/packages/rendering/src/glsl/base/determineNodeAppearance.glsl index 50d83409ed4..f97f60a9fbc 100644 --- a/viewer/packages/rendering/src/glsl/base/determineNodeAppearance.glsl +++ b/viewer/packages/rendering/src/glsl/base/determineNodeAppearance.glsl @@ -2,11 +2,12 @@ NodeAppearance determineNodeAppearance(sampler2D nodeAppearanceTexture, vec2 textureSize, highp float treeIndex) { - float dataTextureWidth = textureSize.x; - float dataTextureHeight = textureSize.y; + highp int dataTextureWidth = int(textureSize.x); + highp int dataTextureHeight = int(textureSize.y); - int xTreeIndexTextureCoord = int(mod(treeIndex, dataTextureWidth)); - int yTreeIndexTextureCoord = int(floor(treeIndex / dataTextureWidth)); + highp int iTreeIndex = int(treeIndex); + highp int xTreeIndexTextureCoord = iTreeIndex % dataTextureWidth; + highp int yTreeIndexTextureCoord = iTreeIndex / dataTextureWidth; vec4 texel = texelFetch(nodeAppearanceTexture, ivec2(xTreeIndexTextureCoord, yTreeIndexTextureCoord), 0); float alphaUnwrapped = floor((texel.a * 255.0) + 0.5); diff --git a/viewer/packages/rendering/src/glsl/sector/instancedMesh.frag b/viewer/packages/rendering/src/glsl/sector/instancedMesh.frag index 82d442ae866..9f9f28840c5 100644 --- a/viewer/packages/rendering/src/glsl/sector/instancedMesh.frag +++ b/viewer/packages/rendering/src/glsl/sector/instancedMesh.frag @@ -7,18 +7,21 @@ precision highp float; #pragma glslify: import('../base/determineVisibility.glsl'); #pragma glslify: import('../base/determineColor.glsl'); #pragma glslify: import('../base/isClipped.glsl') +#pragma glslify: import('../treeIndex/treeIndexPacking.glsl') uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_viewPosition; +in TreeIndexPacked v_treeIndexPacked; -void main() { - NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); +void main() +{ + highp float treeIndex = unpackTreeIndex(v_treeIndexPacked); + NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; } @@ -29,5 +32,5 @@ void main() { vec4 color = determineColor(v_color, appearance); vec3 normal = derivateNormal(v_viewPosition); - updateFragmentColor(renderMode, color, v_treeIndex, normal, gl_FragCoord.z, matCapTexture, GeometryType.InstancedMesh); + updateFragmentColor(renderMode, color, treeIndex, normal, gl_FragCoord.z, matCapTexture, GeometryType.InstancedMesh); } diff --git a/viewer/packages/rendering/src/glsl/sector/instancedMesh.vert b/viewer/packages/rendering/src/glsl/sector/instancedMesh.vert index 6eb366b534e..e81db5b1b6f 100644 --- a/viewer/packages/rendering/src/glsl/sector/instancedMesh.vert +++ b/viewer/packages/rendering/src/glsl/sector/instancedMesh.vert @@ -1,4 +1,5 @@ #pragma glslify: import('../base/determineMatrixOverride.glsl') +#pragma glslify: import('../treeIndex/treeIndexPacking.glsl') uniform mat4 modelMatrix; uniform mat4 viewMatrix; @@ -13,12 +14,14 @@ in mat4 a_instanceMatrix; in float a_treeIndex; in vec3 a_color; -flat out float v_treeIndex; out vec3 v_color; out vec3 v_viewPosition; -void main() -{ +out TreeIndexPacked v_treeIndexPacked; + +void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); + mat4 treeIndexWorldTransform = determineMatrixOverride( a_treeIndex, treeIndexTextureSize, @@ -32,6 +35,5 @@ void main() vec3 transformed = (a_instanceMatrix * vec4(position, 1.0)).xyz; vec4 modelViewPosition = viewMatrix * treeIndexWorldTransform * modelMatrix * vec4(transformed, 1.0); v_viewPosition = modelViewPosition.xyz; - v_treeIndex = a_treeIndex; gl_Position = projectionMatrix * modelViewPosition; } diff --git a/viewer/packages/rendering/src/glsl/sector/mesh.frag b/viewer/packages/rendering/src/glsl/sector/mesh.frag index 21750f71b64..501e909b80d 100644 --- a/viewer/packages/rendering/src/glsl/sector/mesh.frag +++ b/viewer/packages/rendering/src/glsl/sector/mesh.frag @@ -7,18 +7,23 @@ precision highp float; #pragma glslify: import('../base/determineColor.glsl'); #pragma glslify: import('../base/determineVisibility.glsl'); #pragma glslify: import('../base/isClipped.glsl') +#pragma glslify: import('../treeIndex/treeIndexPacking.glsl') uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; + in vec3 v_color; in vec3 v_viewPosition; +in TreeIndexPacked v_treeIndexPacked; + void main() { + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); + NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/mesh.vert b/viewer/packages/rendering/src/glsl/sector/mesh.vert index 655cb968996..73592ebfb54 100644 --- a/viewer/packages/rendering/src/glsl/sector/mesh.vert +++ b/viewer/packages/rendering/src/glsl/sector/mesh.vert @@ -1,4 +1,5 @@ #pragma glslify: import('../base/determineMatrixOverride.glsl') +#pragma glslify: import('../treeIndex/treeIndexPacking.glsl') uniform mat4 modelMatrix; uniform mat4 viewMatrix; @@ -13,12 +14,13 @@ in vec3 color; in float treeIndex; out vec3 v_color; -flat out float v_treeIndex; out vec3 v_viewPosition; +out TreeIndexPacked v_treeIndexPacked; +out mediump float v_treeIndexSubHundreds; void main() { + v_treeIndexPacked = packTreeIndex(treeIndex); v_color = color; - v_treeIndex = treeIndex; mat4 treeIndexWorldTransform = determineMatrixOverride( treeIndex, diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/circle.frag b/viewer/packages/rendering/src/glsl/sector/primitives/circle.frag index be4afb401ec..21df06a5e9c 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/circle.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/circle.frag @@ -5,20 +5,23 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; in vec2 v_xy; in vec3 v_color; in vec3 v_normal; in vec3 vViewPosition; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); float dist = dot(v_xy, v_xy); vec3 normal = normalize( v_normal ); if (dist > 0.25) diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/circle.vert b/viewer/packages/rendering/src/glsl/sector/primitives/circle.vert index 3a64c34320a..5ab59dadbc0 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/circle.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/circle.vert @@ -1,4 +1,5 @@ -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -19,12 +20,13 @@ in vec3 a_normal; out vec2 v_xy; out vec3 v_color; out vec3 v_normal; -flat out float v_treeIndex; out vec3 vViewPosition; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); v_xy = vec2(position.x, position.y); - v_treeIndex = a_treeIndex; mat4 treeIndexWorldTransform = determineMatrixOverride( a_treeIndex, diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/cone.frag b/viewer/packages/rendering/src/glsl/sector/primitives/cone.frag index 612eca21d6f..39d8550fb33 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/cone.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/cone.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); #pragma glslify: import('../../math/constants.glsl') uniform sampler2D colorDataTexture; @@ -24,11 +25,13 @@ in float v_angle; in float v_arcAngle; in vec4 v_centerA; in vec4 v_V; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/cone.vert b/viewer/packages/rendering/src/glsl/sector/primitives/cone.vert index 2a2a13a4c93..a5979979cb6 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/cone.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/cone.vert @@ -1,5 +1,6 @@ #pragma glslify: import('../../math/mul3.glsl') -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -25,7 +26,6 @@ in vec3 a_localXAxis; in float a_angle; in float a_arcAngle; -flat out float v_treeIndex; // We pack the radii into w-components out vec4 v_centerB; // U, V, axis represent the 3x3 cone basis. @@ -40,7 +40,10 @@ out float v_arcAngle; out vec3 v_color; out vec3 v_normal; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); mat4 treeIndexWorldTransform = determineMatrixOverride( a_treeIndex, treeIndexTextureSize, @@ -83,7 +86,6 @@ void main() { surfacePoint = mul3(modelViewMatrix, surfacePoint); // out data - v_treeIndex = a_treeIndex; v_angle = a_angle; v_arcAngle = a_arcAngle; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.frag b/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.frag index be54d2bd54b..242e113a850 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); #pragma glslify: import('../../math/constants.glsl') uniform sampler2D colorDataTexture; @@ -23,11 +24,13 @@ in vec4 axis; in vec4 v_centerA; in vec4 v_centerB; in float height; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.vert b/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.vert index b7ff8e85094..508ec674481 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/eccentricCone.vert @@ -1,5 +1,6 @@ #pragma glslify: import('../../math/mul3.glsl') #pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -22,7 +23,6 @@ in float a_radiusB; in vec3 a_normal; in vec3 a_color; -flat out float v_treeIndex; // We pack the radii into w-components out vec4 v_centerA; out vec4 v_centerB; @@ -36,7 +36,10 @@ out float height; out vec3 v_color; out vec3 v_normal; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); mat4 treeIndexWorldTransform = determineMatrixOverride( a_treeIndex, @@ -116,7 +119,6 @@ void main() { V.w = surfacePoint.y; axis.w = surfacePoint.z; - v_treeIndex = a_treeIndex; v_color = a_color; v_normal = normalMatrix * normal; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.frag b/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.frag index be59a6d0cce..2ae120cffa1 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; @@ -22,11 +23,13 @@ in float height; in vec4 U; in vec4 V; in vec4 sphereNormal; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.vert b/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.vert index 395ea924313..a07a61bd452 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/ellipsoidSegment.vert @@ -1,5 +1,6 @@ #pragma glslify: import('../../math/mul3.glsl') -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 inverseNormalMatrix; @@ -22,7 +23,6 @@ in float a_horizontalRadius; in float a_verticalRadius; in float a_height; -flat out float v_treeIndex; // We pack vRadius as w-component of center out vec4 center; out float hRadius; @@ -36,7 +36,10 @@ out vec4 sphereNormal; out vec3 v_color; out vec3 v_normal; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); mat4 treeIndexWorldTransform = determineMatrixOverride( a_treeIndex, @@ -90,7 +93,6 @@ void main() { vec3 surfacePoint = centerOfSegment + mat3(lDir, left, up) * displacement; vec3 transformed = surfacePoint; - v_treeIndex = a_treeIndex; surfacePoint = mul3(modelViewMatrix, surfacePoint); center.xyz = mul3(modelViewMatrix, centerWithOffset); center.w = a_verticalRadius; // Pack radius into w-component diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.frag b/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.frag index ffa6ddd9a98..528ef411905 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); #pragma glslify: import('../../math/constants.glsl') // TODO general cylinder and cone are very similar and used @@ -29,11 +30,13 @@ in float v_arcAngle; in float v_surfacePointY; in vec4 v_planeA; in vec4 v_planeB; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.vert b/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.vert index 692e0158e7c..630b1f96a8a 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/generalCylinder.vert @@ -1,5 +1,6 @@ #pragma glslify: import('../../math/mul3.glsl') -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -27,7 +28,6 @@ in vec3 a_localXAxis; in float a_angle; in float a_arcAngle; -flat out float v_treeIndex; // We pack the radii into w-components out vec4 v_centerB; // U, V, axis represent the 3x3 cone basis. @@ -43,7 +43,10 @@ out float v_arcAngle; out vec3 v_color; out vec3 v_normal; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); mat4 modelViewMatrix = viewMatrix * modelMatrix; @@ -86,7 +89,6 @@ void main() { surfacePoint = mul3(modelViewMatrix, surfacePoint); // varying data - v_treeIndex = a_treeIndex; v_angle = a_angle; v_arcAngle = a_arcAngle; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/generalring.frag b/viewer/packages/rendering/src/glsl/sector/primitives/generalring.frag index 07f77f5cc36..68f0a38ca72 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/generalring.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/generalring.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); #pragma glslify: import('../../math/constants.glsl') uniform sampler2D colorDataTexture; @@ -17,12 +18,14 @@ in float v_oneMinusThicknessSqr; in vec2 v_xy; in float v_angle; in float v_arcAngle; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; in vec3 vViewPosition; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/generalring.vert b/viewer/packages/rendering/src/glsl/sector/primitives/generalring.vert index 5b126768360..b9a139f9ebd 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/generalring.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/generalring.vert @@ -1,4 +1,5 @@ -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -19,7 +20,6 @@ in float a_arcAngle; in float a_thickness; in vec3 a_normal; -flat out float v_treeIndex; out float v_oneMinusThicknessSqr; out vec2 v_xy; out float v_angle; @@ -28,8 +28,10 @@ out vec3 v_color; out vec3 v_normal; out vec3 vViewPosition; +out TreeIndexPacked v_treeIndexPacked; + void main() { - v_treeIndex = a_treeIndex; + v_treeIndexPacked = packTreeIndex(a_treeIndex); v_oneMinusThicknessSqr = (1.0 - a_thickness) * (1.0 - a_thickness); v_xy = vec2(position.x, position.y); v_angle = a_angle; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.frag b/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.frag index f03220795af..46065fcfe65 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.frag @@ -5,7 +5,8 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; @@ -22,11 +23,13 @@ in float height; in vec4 U; in vec4 V; in vec4 sphereNormal; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.vert b/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.vert index 6c48c42f8a7..1192107a372 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/sphericalSegment.vert @@ -1,4 +1,5 @@ #pragma glslify: import('../../math/mul3.glsl') +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 inverseNormalMatrix; @@ -17,7 +18,6 @@ in float a_horizontalRadius; in float a_verticalRadius; in float a_height; -flat out float v_treeIndex; // We pack vRadius as w-component of center out vec4 center; out float hRadius; @@ -32,8 +32,10 @@ out vec4 sphereNormal; out vec3 v_color; out vec3 v_normal; +out TreeIndexPacked v_treeIndexPacked; + void main() { - v_treeIndex = a_treeIndex; + v_treeIndexPacked = packTreeIndex(a_treeIndex); v_color = a_color; mat4 modelViewMatrix = viewMatrix * modelMatrix; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.frag b/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.frag index 81133c39e96..c1d5210e8d5 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.frag @@ -5,19 +5,22 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; in vec3 vViewPosition; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.vert b/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.vert index ccde5fa310a..d0074cd6a76 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/torusSegment.vert @@ -1,4 +1,5 @@ -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -18,12 +19,14 @@ in float a_arcAngle; in float a_radius; in float a_tubeRadius; -flat out float v_treeIndex; out vec3 v_color; out vec3 v_normal; out vec3 vViewPosition; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); // normalized theta and phi are packed into positions float theta = position.x * a_arcAngle; float phi = position.y; @@ -49,7 +52,6 @@ void main() { vec3 center = (a_instanceMatrix * vec4(a_radius * cosTheta, a_radius * sinTheta, 0.0, 1.0)).xyz; vec3 objectNormal = normalize(transformed.xyz - center); - v_treeIndex = a_treeIndex; v_color = a_color; v_normal = normalMatrix * normalize(inverseModelMatrix * treeIndexWorldTransform * modelMatrix * vec4(objectNormal, 0.0)).xyz; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.frag b/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.frag index 81133c39e96..6073664debf 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.frag +++ b/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.frag @@ -5,19 +5,23 @@ precision highp float; #pragma glslify: import('../../base/determineNodeAppearance.glsl'); #pragma glslify: import('../../base/determineVisibility.glsl'); #pragma glslify: import('../../base/determineColor.glsl'); -#pragma glslify: import('../../base/isClipped.glsl') +#pragma glslify: import('../../base/isClipped.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform sampler2D colorDataTexture; uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; in vec3 vViewPosition; -void main() { +in TreeIndexPacked v_treeIndexPacked; + +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.vert b/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.vert index 484a17d0e35..1c975875f9b 100644 --- a/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.vert +++ b/viewer/packages/rendering/src/glsl/sector/primitives/trapezium.vert @@ -1,4 +1,5 @@ -#pragma glslify: import('../../base/determineMatrixOverride.glsl') +#pragma glslify: import('../../base/determineMatrixOverride.glsl'); +#pragma glslify: import('../../treeIndex/treeIndexPacking.glsl'); uniform mat4 inverseModelMatrix; uniform mat4 modelMatrix; @@ -18,12 +19,14 @@ in vec3 a_vertex2; in vec3 a_vertex3; in vec3 a_vertex4; -flat out float v_treeIndex; out vec3 v_color; out vec3 v_normal; out vec3 vViewPosition; +out TreeIndexPacked v_treeIndexPacked; + void main() { + v_treeIndexPacked = packTreeIndex(a_treeIndex); vec3 transformed; // reduce the avarage branchings if (position.x < 1.5) { @@ -42,7 +45,6 @@ void main() { vec3 objectNormal = cross(a_vertex1 - a_vertex2, a_vertex1 - a_vertex3); - v_treeIndex = a_treeIndex; v_color = a_color; v_normal = normalMatrix * normalize(inverseModelMatrix * treeIndexWorldTransform * modelMatrix * vec4(objectNormal, 0.0)).xyz; diff --git a/viewer/packages/rendering/src/glsl/sector/simple.frag b/viewer/packages/rendering/src/glsl/sector/simple.frag index 6c8cbc89dc9..69c4178ae2a 100644 --- a/viewer/packages/rendering/src/glsl/sector/simple.frag +++ b/viewer/packages/rendering/src/glsl/sector/simple.frag @@ -12,12 +12,14 @@ uniform sampler2D matCapTexture; uniform vec2 treeIndexTextureSize; uniform int renderMode; -flat in float v_treeIndex; in vec3 v_color; in vec3 v_normal; in vec3 vViewPosition; +in TreeIndexPacked v_treeIndexPacked; -void main() { +void main() +{ + highp float v_treeIndex = unpackTreeIndex(v_treeIndexPacked); NodeAppearance appearance = determineNodeAppearance(colorDataTexture, treeIndexTextureSize, v_treeIndex); if (!determineVisibility(appearance, renderMode)) { discard; diff --git a/viewer/packages/rendering/src/glsl/sector/simple.vert b/viewer/packages/rendering/src/glsl/sector/simple.vert index cfbf355fad2..d7dd7c7c3c1 100644 --- a/viewer/packages/rendering/src/glsl/sector/simple.vert +++ b/viewer/packages/rendering/src/glsl/sector/simple.vert @@ -19,13 +19,15 @@ in vec4 matrix1; in vec4 matrix2; in vec4 matrix3; -flat out float v_treeIndex; out vec3 v_color; out vec3 v_normal; out vec3 vViewPosition; -void main() { +out TreeIndexPacked v_treeIndexPacked; +void main() { + v_treeIndexHundreds = floor(treeIndex / 100.0); + v_treeIndexSubHundreds = round(mod(treeIndex, 100.0)); mat4 treeIndexWorldTransform = determineMatrixOverride( treeIndex, treeIndexTextureSize, @@ -34,7 +36,7 @@ void main() { transformOverrideTexture ); - v_treeIndex = treeIndex; + v_treeIndex = int(treeIndex); v_color = color; v_normal = normalize(normalMatrix * (inverseModelMatrix * treeIndexWorldTransform * modelMatrix * vec4(normalize(normal), 0.0)).xyz); mat4 instanceMatrix = mat4(matrix0, matrix1, matrix2, matrix3); diff --git a/viewer/packages/rendering/src/glsl/treeIndex/treeIndexPacking.glsl b/viewer/packages/rendering/src/glsl/treeIndex/treeIndexPacking.glsl new file mode 100644 index 00000000000..6fcd8d70d2e --- /dev/null +++ b/viewer/packages/rendering/src/glsl/treeIndex/treeIndexPacking.glsl @@ -0,0 +1,32 @@ +struct TreeIndexPacked +{ + highp float thousands; + highp float subThousands; +}; + +/** + * Packs a treeIndex integer (passed in as a float) into two floats. + * This avoids a precision issue causing wrong TreeIndexes to arrive to the fragment shader. + * + * Note: Instead of this we SHOULD use the "flat" modifier, but the "flat" modifier is + * super slow on iOS Metal. Packing of TreeIndexes should be removed when the WebGL extension + * EXT_provoking_vertex is implemented in WebKit. + * https://registry.khronos.org/webgl/extensions/EXT_provoking_vertex/ + */ +TreeIndexPacked packTreeIndex(float treeIndex) { + highp float roundedTreeIndex = round(treeIndex); + // We pack the potentially big input into two lower numbers representing the amount of 1000s and the "remaining up to 999 ones" + // This avoids high numbers, and keeps precision while being "ok" enough for our usecase. + highp float treeIndexThousands = floor(roundedTreeIndex / 1000.0); + highp float treeIndexSubThousands = mod(roundedTreeIndex, 1000.0); + + return TreeIndexPacked(treeIndexThousands, treeIndexSubThousands); +} + +/** + * Unpacks the packed treeIndex to an int representing the original input TreeIndex + */ +float unpackTreeIndex(TreeIndexPacked treeIndexPacked) { + highp float treeIndex = round(treeIndexPacked.thousands) * 1000.0 + round(treeIndexPacked.subThousands); + return treeIndex; +}