From ed85f7f6bd5e1841518e0c52448e90eba3c8b988 Mon Sep 17 00:00:00 2001 From: nilscb Date: Fri, 27 Oct 2023 15:15:19 +0200 Subject: [PATCH] perf: Apply flag "ZIncreasingDownwards" in vertex shaders MapLayer. (#1741) - This is related to earlier commit. MapLayer was missing - Earlier commit: performance: Apply flag "ZIncreasingDownwards" in vertex shaders for relevant layers. #1724 (#1737) --- .../src/layers/map/mapLayer.ts | 2 -- .../src/layers/map/privateMapLayer.ts | 8 +++++- .../subsurface-viewer/src/layers/map/utils.ts | 28 ++++++++----------- .../src/layers/map/vertex.glsl.ts | 8 ++++-- .../src/layers/map/vertex_lines.glsl.ts | 7 ++++- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/typescript/packages/subsurface-viewer/src/layers/map/mapLayer.ts b/typescript/packages/subsurface-viewer/src/layers/map/mapLayer.ts index 12e80d168..09b7d41c1 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/mapLayer.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/mapLayer.ts @@ -55,7 +55,6 @@ export type Params = { frame: Frame; smoothShading: boolean; gridLines: boolean; - ZIncreasingDownwards: boolean; }; /** @@ -337,7 +336,6 @@ export default class MapLayer extends CompositeLayer { frame: this.props.frame, smoothShading: this.props.smoothShading, gridLines: this.props.gridLines, - ZIncreasingDownwards: this.props.ZIncreasingDownwards, }; const [ diff --git a/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts b/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts index 4e967abd0..ed994b7fa 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts @@ -227,6 +227,7 @@ export default class privateMapLayer extends Layer { isColorMapClampColorTransparent, isClampColor, smoothShading, + ZIncreasingDownwards: this.props.ZIncreasingDownwards, }) .draw(); gl.disable(GL.POLYGON_OFFSET_FILL); @@ -236,7 +237,12 @@ export default class privateMapLayer extends Layer { } if (this.props.gridLines) { - mesh_lines_model.draw(); + mesh_lines_model + .setUniforms({ + ...uniforms, + ZIncreasingDownwards: this.props.ZIncreasingDownwards, + }) + .draw(); } if (!this.state["isLoaded"]) { diff --git a/typescript/packages/subsurface-viewer/src/layers/map/utils.ts b/typescript/packages/subsurface-viewer/src/layers/map/utils.ts index 3bdeae46a..b0f8a5422 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/utils.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/utils.ts @@ -44,8 +44,7 @@ function calcNormal( ox: number, oy: number, dx: number, - dy: number, - multZ: number + dy: number ) { if (!smoothShading) { return [1, 1, 1]; @@ -73,11 +72,11 @@ function calcNormal( } const hh = ny - 1 - h; // Note use hh for h for getting y values. - const p0 = [ox + w * dx, oy + hh * dy, i0_act ? -meshData[i0] * multZ : 0]; // eslint-disable-line - const p1 = [ ox + (w - 1) * dx, oy + hh * dy, i1_act ? -meshData[i1] * multZ : 0]; // eslint-disable-line - const p2 = [ ox + w * dx, oy + (hh + 1) * dy, i2_act ? -meshData[i2] * multZ : 0]; // eslint-disable-line - const p3 = [ ox + (w + 1) * dx, oy + hh * dy, i3_act ? -meshData[i3] * multZ : 0]; // eslint-disable-line - const p4 = [ ox + w * dx, oy + (hh - 1) * dy, i4_act ? -meshData[i4] * multZ : 0]; // eslint-disable-line + const p0 = [ox + w * dx, oy + hh * dy, i0_act ? meshData[i0] : 0]; // eslint-disable-line + const p1 = [ ox + (w - 1) * dx, oy + hh * dy, i1_act ? meshData[i1] : 0]; // eslint-disable-line + const p2 = [ ox + w * dx, oy + (hh + 1) * dy, i2_act ? meshData[i2] : 0]; // eslint-disable-line + const p3 = [ ox + (w + 1) * dx, oy + hh * dy, i3_act ? meshData[i3] : 0]; // eslint-disable-line + const p4 = [ ox + w * dx, oy + (hh - 1) * dy, i4_act ? meshData[i4] : 0]; // eslint-disable-line const v1 = [p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]] as Vec; const v2 = [p2[0] - p0[0], p2[1] - p0[1], p2[2] - p0[2]] as Vec; @@ -143,9 +142,6 @@ export function makeFullMesh(params: Params) { const frame = params.frame; const smoothShading = params.smoothShading; const gridLines = params.gridLines; - const ZIncreasingDownwards = params.ZIncreasingDownwards; - - const multZ = ZIncreasingDownwards ? 1 : -1; const meshZValueRange = getFloat32ArrayMinMax(meshData); const propertyValueRange = getFloat32ArrayMinMax(propertiesData); @@ -206,7 +202,7 @@ export function makeFullMesh(params: Params) { const x0 = ox + w * dx; const y0 = oy + (ny - 1 - h) * dy; // See note above. - const z = isMesh ? -meshData[i0] * multZ : 0; + const z = isMesh ? meshData[i0] : 0; const propertyValue = propertiesData[i0]; @@ -215,7 +211,7 @@ export function makeFullMesh(params: Params) { positions[3 * i + 2] = z; if (smoothShading) { - const normal = calcNormal(w, h, nx, ny, isMesh, smoothShading, meshData, ox, oy, dx, dy, multZ); // eslint-disable-line + const normal = calcNormal(w, h, nx, ny, isMesh, smoothShading, meshData, ox, oy, dx, dy); // eslint-disable-line normals[3 * i + 0] = normal[0] * 127; // Normalize to signed 8 bit. normals[3 * i + 1] = normal[1] * 127; normals[3 * i + 2] = normal[2] * 127; @@ -334,19 +330,19 @@ export function makeFullMesh(params: Params) { const x0 = ox + w * dx; const y0 = oy + hh * dy; - const z0 = isMesh ? -meshData[i0] * multZ : 0; + const z0 = isMesh ? meshData[i0] : 0; const x1 = ox + (w + 1) * dx; const y1 = oy + hh * dy; - const z1 = isMesh ? -meshData[i1] * multZ : 0; + const z1 = isMesh ? meshData[i1] : 0; const x2 = ox + (w + 1) * dx; const y2 = oy + (hh - 1) * dy; // Note hh - 1 here. - const z2 = isMesh ? -meshData[i2] * multZ : 0; + const z2 = isMesh ? meshData[i2] : 0; const x3 = ox + w * dx; const y3 = oy + (hh - 1) * dy; // Note hh - 1 here. - const z3 = isMesh ? -meshData[i3] * multZ : 0; + const z3 = isMesh ? meshData[i3] : 0; const propertyIndex = h * (nx - 1) + w; // (nx - 1) -> the width of the property 2D array is one less than for the nodes in this case. const propertyValue = propertiesData[propertyIndex]; diff --git a/typescript/packages/subsurface-viewer/src/layers/map/vertex.glsl.ts b/typescript/packages/subsurface-viewer/src/layers/map/vertex.glsl.ts index cecf04760..11fa35277 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/vertex.glsl.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/vertex.glsl.ts @@ -20,14 +20,18 @@ out vec3 worldPos; out float property; flat out int vertexIndex; +uniform bool ZIncreasingDownwards; void main(void) { geometry.pickingColor = vec3(1.0, 1.0, 0.0); vertexIndex = gl_VertexID; + vec3 position = positions; + position[2] *= ZIncreasingDownwards ? -1.0 : 1.0; + cameraPosition = project_uCameraPosition; - worldPos = positions; + worldPos = position; normals_commonspace = normals; @@ -35,7 +39,7 @@ void main(void) { property = properties; - position_commonspace = vec4(project_position(positions), 0.0); + position_commonspace = vec4(project_position(position), 0.0); gl_Position = project_common_position_to_clipspace(position_commonspace); DECKGL_FILTER_GL_POSITION(gl_Position, geometry); diff --git a/typescript/packages/subsurface-viewer/src/layers/map/vertex_lines.glsl.ts b/typescript/packages/subsurface-viewer/src/layers/map/vertex_lines.glsl.ts index 286877acb..eedc8fdee 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/vertex_lines.glsl.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/vertex_lines.glsl.ts @@ -8,8 +8,13 @@ in vec3 positions; out vec4 position_commonspace; +uniform bool ZIncreasingDownwards; + void main(void) { - vec3 position_commonspace = project_position(positions); + vec3 position = positions; + position[2] *= ZIncreasingDownwards ? -1.0 : 1.0; + + vec3 position_commonspace = project_position(position); gl_Position = project_common_position_to_clipspace(vec4(position_commonspace, 0.0)); } `;