From cc8580b8da8099e663c44e4baafeca7b89fc2e38 Mon Sep 17 00:00:00 2001 From: AlexandruPopovici Date: Wed, 27 Jul 2022 22:54:45 +0300 Subject: [PATCH] #828 Eliminated the redundant position attributes for the line geometries. Like for meshes, re-used the original position attributes as the high parts of our encoding --- packages/viewer-sandbox/src/main.ts | 4 +--- .../viewer/src/modules/batching/LineBatch.ts | 1 + .../viewer/src/modules/batching/MeshBatch.ts | 5 ++++- .../viewer/src/modules/converter/Geometry.ts | 20 +++++-------------- .../materials/shaders/speckle-line-vert.ts | 8 ++------ 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/packages/viewer-sandbox/src/main.ts b/packages/viewer-sandbox/src/main.ts index 0750ac923..a302ba525 100644 --- a/packages/viewer-sandbox/src/main.ts +++ b/packages/viewer-sandbox/src/main.ts @@ -39,6 +39,4 @@ sandbox.makeGenericUI() sandbox.makeSceneUI() sandbox.makeFilteringUI() // Load demo object -sandbox.loadUrl( - 'https://speckle.xyz/streams/da9e320dad/commits/5388ef24b8?c=%5B-7.66134,10.82932,6.41935,-0.07739,-13.88552,1.8697,0,1%5D' -) +sandbox.loadUrl('https://latest.speckle.dev/streams/3ed8357f29/commits/d10f2af1ce') diff --git a/packages/viewer/src/modules/batching/LineBatch.ts b/packages/viewer/src/modules/batching/LineBatch.ts index 5a449af17..271024e9e 100644 --- a/packages/viewer/src/modules/batching/LineBatch.ts +++ b/packages/viewer/src/modules/batching/LineBatch.ts @@ -182,6 +182,7 @@ export default class LineBatch implements Batch { private makeLineGeometryTriangle(position: Float32Array): LineSegmentsGeometry { const geometry = new LineSegmentsGeometry() + /** This will set the instanceStart and instanceEnd attributes. These will be our high parts */ geometry.setPositions(position) const buffer = new Float32Array(position.length + position.length / 3) diff --git a/packages/viewer/src/modules/batching/MeshBatch.ts b/packages/viewer/src/modules/batching/MeshBatch.ts index e503462ac..f8ad9b02a 100644 --- a/packages/viewer/src/modules/batching/MeshBatch.ts +++ b/packages/viewer/src/modules/batching/MeshBatch.ts @@ -285,7 +285,10 @@ export default class MeshBatch implements Batch { } if (position) { - /** When RTE enabled, we'll be storing the high component of the encoding here */ + /** When RTE enabled, we'll be storing the high component of the encoding here, + * which considering our current encoding method is actually the original casted + * down float32 position! + */ this.geometry.setAttribute('position', new Float32BufferAttribute(position, 3)) } diff --git a/packages/viewer/src/modules/converter/Geometry.ts b/packages/viewer/src/modules/converter/Geometry.ts index 2dbbe561d..88f5025de 100644 --- a/packages/viewer/src/modules/converter/Geometry.ts +++ b/packages/viewer/src/modules/converter/Geometry.ts @@ -42,7 +42,11 @@ export class Geometry { geometry.type === 'LineSegmentsGeometry' ) { const position_low = new Float32Array(doublePositions.length) - const position_high = new Float32Array(doublePositions.length) + /** This is the default instanceStart + instanceEnd interleaved attribute buffer + * We're not altering it in reality since the high part of our encoding is the + * original position double value casted down to float + */ + const position_high = geometry.attributes.instanceStart.array as Float32Array Geometry.DoubleToHighLowBuffer(doublePositions, position_low, position_high) @@ -59,20 +63,6 @@ export class Geometry { 'instanceEndLow', new InterleavedBufferAttribute(instanceBufferLow, 3, 3) ) // xyz - - const instanceBufferHigh = new InstancedInterleavedBuffer( - new Float32Array(position_high), - 6, - 1 - ) // xyz, xyz - geometry.setAttribute( - 'instanceStartHigh', - new InterleavedBufferAttribute(instanceBufferHigh, 3, 0) - ) // xyz - geometry.setAttribute( - 'instanceEndHigh', - new InterleavedBufferAttribute(instanceBufferHigh, 3, 3) - ) // xyz } } diff --git a/packages/viewer/src/modules/materials/shaders/speckle-line-vert.ts b/packages/viewer/src/modules/materials/shaders/speckle-line-vert.ts index 338867b26..9c8a3760f 100644 --- a/packages/viewer/src/modules/materials/shaders/speckle-line-vert.ts +++ b/packages/viewer/src/modules/materials/shaders/speckle-line-vert.ts @@ -46,12 +46,8 @@ export const speckleLineVert = /* glsl */ ` #endif #ifdef USE_RTE - // attribute vec3 position_high; - // attribute vec3 position_low; attribute vec3 instanceStartLow; - attribute vec3 instanceStartHigh; attribute vec3 instanceEndLow; - attribute vec3 instanceEndHigh; uniform vec3 uViewer_high; uniform vec3 uViewer_low; #endif @@ -106,14 +102,14 @@ export const speckleLineVert = /* glsl */ ` /** Source https://github.com/virtualglobebook/OpenGlobe/blob/master/Source/Examples/Chapter05/Jitter/GPURelativeToEyeDSFUN90/Shaders/VS.glsl */ vec3 t1 = instanceStartLow.xyz - uViewer_low; vec3 e = t1 - instanceStartLow.xyz; - vec3 t2 = ((-uViewer_low - e) + (instanceStartLow.xyz - (t1 - e))) + instanceStartHigh.xyz - uViewer_high; + vec3 t2 = ((-uViewer_low - e) + (instanceStartLow.xyz - (t1 - e))) + instanceStart.xyz - uViewer_high; vec3 highDifference = t1 + t2; vec3 lowDifference = t2 - (highDifference - t1); vec4 start = modelViewMatrix * vec4(highDifference.xyz + lowDifference.xyz , 1.); t1 = instanceEndLow.xyz - uViewer_low; e = t1 - instanceEndLow.xyz; - t2 = ((-uViewer_low - e) + (instanceEndLow.xyz - (t1 - e))) + instanceEndHigh.xyz - uViewer_high; + t2 = ((-uViewer_low - e) + (instanceEndLow.xyz - (t1 - e))) + instanceEnd.xyz - uViewer_high; highDifference = t1 + t2; lowDifference = t2 - (highDifference - t1); vec4 end = modelViewMatrix * vec4(highDifference.xyz + lowDifference.xyz , 1.);