From e778637b94d95fd2b7c0e90995b2ebabd4f477bc Mon Sep 17 00:00:00 2001 From: AlexandruPopovici Date: Tue, 18 Oct 2022 22:12:02 +0300 Subject: [PATCH] Added ner and far camera planes controls to see how they're values change depth encoding precision --- packages/viewer-sandbox/src/Sandbox.ts | 24 +++++++++ .../viewer/src/modules/SpeckleRenderer.ts | 52 +++++++++---------- .../materials/shaders/speckle-depth-frag.ts | 19 +------ .../speckle-static-ao-generate-frag.ts | 18 +++---- 4 files changed, 59 insertions(+), 54 deletions(-) diff --git a/packages/viewer-sandbox/src/Sandbox.ts b/packages/viewer-sandbox/src/Sandbox.ts index 9fdadf4fa..3f8f78c7b 100644 --- a/packages/viewer-sandbox/src/Sandbox.ts +++ b/packages/viewer-sandbox/src/Sandbox.ts @@ -335,6 +335,30 @@ export default class Sandbox { this.viewer.requestRender() }) + postFolder + .addInput({ near: 0.01 }, 'near', { + min: 0, + max: 2, + step: 0.001 + }) + .on('change', (ev) => { + this.viewer.cameraHandler.activeCam.camera.near = ev.value + this.viewer.cameraHandler.activeCam.camera.updateProjectionMatrix() + this.viewer.requestRender() + }) + + postFolder + .addInput({ far: 10 }, 'far', { + min: 0, + max: 10000, + step: 1 + }) + .on('change', (ev) => { + this.viewer.cameraHandler.activeCam.camera.far = ev.value + this.viewer.cameraHandler.activeCam.camera.updateProjectionMatrix() + this.viewer.requestRender() + }) + postFolder .addInput(Sandbox.sceneParams, 'tonemapping', { options: { diff --git a/packages/viewer/src/modules/SpeckleRenderer.ts b/packages/viewer/src/modules/SpeckleRenderer.ts index 43e9b7cdd..6afa7d1d5 100644 --- a/packages/viewer/src/modules/SpeckleRenderer.ts +++ b/packages/viewer/src/modules/SpeckleRenderer.ts @@ -264,32 +264,32 @@ export default class SpeckleRenderer { } } - const v = new Vector3() - const box = this.sceneBox - const camPos = new Vector3().copy( - this.viewer.cameraHandler.activeCam.camera.position - ) - let d = 0 - v.set(box.min.x, box.min.y, box.min.z) // 000 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.min.x, box.min.y, box.max.z) // 001 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.min.x, box.max.y, box.min.z) // 010 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.min.x, box.max.y, box.max.z) // 011 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.max.x, box.min.y, box.min.z) // 100 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.max.x, box.min.y, box.max.z) // 101 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.max.x, box.max.y, box.min.z) // 110 - d = Math.max(camPos.distanceTo(v), d) - v.set(box.max.x, box.max.y, box.max.z) // 111 - d = Math.max(camPos.distanceTo(v), d) - this.viewer.cameraHandler.camera.far = d - this.viewer.cameraHandler.activeCam.camera.far = d - this.viewer.cameraHandler.activeCam.camera.updateProjectionMatrix() - this.viewer.cameraHandler.camera.updateProjectionMatrix() + // const v = new Vector3() + // const box = this.sceneBox + // const camPos = new Vector3().copy( + // this.viewer.cameraHandler.activeCam.camera.position + // ) + // let d = 0 + // v.set(box.min.x, box.min.y, box.min.z) // 000 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.min.x, box.min.y, box.max.z) // 001 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.min.x, box.max.y, box.min.z) // 010 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.min.x, box.max.y, box.max.z) // 011 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.max.x, box.min.y, box.min.z) // 100 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.max.x, box.min.y, box.max.z) // 101 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.max.x, box.max.y, box.min.z) // 110 + // d = Math.max(camPos.distanceTo(v), d) + // v.set(box.max.x, box.max.y, box.max.z) // 111 + // d = Math.max(camPos.distanceTo(v), d) + // this.viewer.cameraHandler.camera.far = d + // this.viewer.cameraHandler.activeCam.camera.far = d + // this.viewer.cameraHandler.activeCam.camera.updateProjectionMatrix() + // this.viewer.cameraHandler.camera.updateProjectionMatrix() const currentAzimuth = this.viewer.cameraHandler.controls.azimuthAngle const currentPolar = this.viewer.cameraHandler.controls.polarAngle diff --git a/packages/viewer/src/modules/materials/shaders/speckle-depth-frag.ts b/packages/viewer/src/modules/materials/shaders/speckle-depth-frag.ts index df7ea3d3b..3d5ac0417 100644 --- a/packages/viewer/src/modules/materials/shaders/speckle-depth-frag.ts +++ b/packages/viewer/src/modules/materials/shaders/speckle-depth-frag.ts @@ -11,23 +11,6 @@ export const speckleDepthFrag = /* glsl */ ` #include #include varying vec2 vHighPrecisionZW; - -highp vec4 encode32(highp float f) { - highp float e =5.0; - - highp float F = abs(f); - highp float Sign = step(0.0,-f); - highp float Exponent = floor(log2(F)); - highp float Mantissa = (exp2(- Exponent) * F); - Exponent = floor(log2(F) + 127.0) + floor(log2(Mantissa)); - highp vec4 rgba; - rgba[0] = 128.0 * Sign + floor(Exponent*exp2(-1.0)); - rgba[1] = 128.0 * mod(Exponent,2.0) + mod(floor(Mantissa*128.0),128.0); - rgba[2] = floor(mod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0))); - rgba[3] = floor(exp2(23.0)*mod(Mantissa,exp2(-15.0))); - return rgba / 255.; -} - void main() { #include vec4 diffuseColor = vec4( 1.0 ); @@ -50,7 +33,7 @@ void main() { #if DEPTH_PACKING == 3200 gl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity ); #elif DEPTH_PACKING == 3201 - gl_FragColor = encode32( fragCoordZ ); + gl_FragColor = packDepthToRGBA( fragCoordZ ); #endif } ` diff --git a/packages/viewer/src/modules/materials/shaders/speckle-static-ao-generate-frag.ts b/packages/viewer/src/modules/materials/shaders/speckle-static-ao-generate-frag.ts index 90e97ea1f..a455b1116 100644 --- a/packages/viewer/src/modules/materials/shaders/speckle-static-ao-generate-frag.ts +++ b/packages/viewer/src/modules/materials/shaders/speckle-static-ao-generate-frag.ts @@ -38,18 +38,9 @@ export const speckleStaticAoGenerateFrag = /* glsl */ ` vec4 getDefaultColor( const in vec2 screenPosition ) { return vec4( 1.0 ); } - - highp float decode32(highp vec4 rgba) { - highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; - highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; - highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); - highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - return Result; - } float getDepth( const in vec2 screenPosition ) { - vec4 plm = texture2D( tDepth, screenPosition ) * 255.; - return decode32( plm.rgba ); + return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) ); } float getLinearDepth( const in vec2 screenPosition ) { @@ -192,6 +183,13 @@ export const speckleStaticAoGenerateFrag = /* glsl */ ` #endif } + highp float decode32(highp vec4 rgba) { + highp float Sign = 1.0 - step(128.0,rgba[0])*2.0; + highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0; + highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000); + highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + return Result; + } float scaleDividedByCameraFar;