Skip to content

Commit

Permalink
Merge pull request #1172 from Kurtil/core/32bitNormals
Browse files Browse the repository at this point in the history
Draw pick normal using 32 bits per color channel instead of default 8
  • Loading branch information
xeolabs authored Oct 12, 2023
2 parents d0b27d0 + 3f1bf52 commit 8e7beca
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class TrianglesDataTexturePickNormalsFlatRenderer {
src.push("uniform vec3 sectionPlaneDir" + i + ";");
}
}
src.push("out vec4 outNormal;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = vFlags2 > 0;");
Expand All @@ -483,7 +483,7 @@ export class TrianglesDataTexturePickNormalsFlatRenderer {
src.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );");
src.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );");
src.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );");
src.push(" outNormal = vec4((worldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(worldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Program} from "../../../../webgl/Program.js";
import {createRTCViewMat, getPlaneRTCPos} from "../../../../math/rtcCoords.js";
import {getPlaneRTCPos} from "../../../../math/rtcCoords.js";
import {math} from "../../../../math/math.js";

const tempVec3a = math.vec3();
Expand Down Expand Up @@ -393,7 +393,7 @@ export class TrianglesDataTexturePickNormalsRenderer {
}
}
src.push("in vec3 vWorldNormal;");
src.push("out vec4 outNormal;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = vFlags2 > 0u;");
Expand All @@ -412,7 +412,7 @@ export class TrianglesDataTexturePickNormalsRenderer {
// src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
src.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;");
}
src.push(" outNormal = vec4((vWorldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(vWorldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { math } from "../../../../math/math.js";
import {VBOSceneModelTriangleBatchingRenderer} from "../../VBOSceneModelRenderers.js";

/**
Expand Down Expand Up @@ -90,7 +91,7 @@ class TrianglesBatchingPickNormalsFlatRenderer extends VBOSceneModelTriangleBatc
src.push("uniform vec3 sectionPlaneDir" + i + ";");
}
}
src.push("out vec4 outColor;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
Expand All @@ -110,7 +111,7 @@ class TrianglesBatchingPickNormalsFlatRenderer extends VBOSceneModelTriangleBatc
src.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );");
src.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );");
src.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );");
src.push(" outColor = vec4((worldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(worldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { math } from "../../../../math/math.js";
import {VBOSceneModelTriangleBatchingRenderer} from "../../VBOSceneModelRenderers.js";

/**
Expand Down Expand Up @@ -103,7 +104,7 @@ class TrianglesBatchingPickNormalsRenderer extends VBOSceneModelTriangleBatching
}
}
src.push("in vec3 vWorldNormal;");
src.push("out vec4 outColor;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
Expand All @@ -120,7 +121,7 @@ class TrianglesBatchingPickNormalsRenderer extends VBOSceneModelTriangleBatching
if (scene.logarithmicDepthBufferEnabled) {
src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
}
src.push(" outColor = vec4((vWorldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(vWorldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { math } from "../../../../math/math.js";
import {VBOSceneModelTriangleInstancingRenderer} from "../../VBOSceneModelRenderers.js";

/**
Expand Down Expand Up @@ -100,8 +101,7 @@ class TrianglesInstancingPickNormalsFlatRenderer extends VBOSceneModelTriangleIn
src.push("uniform vec3 sectionPlaneDir" + i + ";");
}
}
src.push("in vec3 vWorldNormal;");
src.push("out vec4 outColor;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
Expand All @@ -121,7 +121,7 @@ class TrianglesInstancingPickNormalsFlatRenderer extends VBOSceneModelTriangleIn
src.push(" vec3 xTangent = dFdx( vWorldPosition.xyz );");
src.push(" vec3 yTangent = dFdy( vWorldPosition.xyz );");
src.push(" vec3 worldNormal = normalize( cross( xTangent, yTangent ) );");
src.push(" outColor = vec4((worldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(worldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { math } from "../../../../math/math.js";
import {VBOSceneModelTriangleInstancingRenderer} from "../../VBOSceneModelRenderers.js";

/**
Expand Down Expand Up @@ -118,7 +119,7 @@ class TrianglesInstancingPickNormalsRenderer extends VBOSceneModelTriangleInstan
}
}
src.push("in vec3 vWorldNormal;");
src.push("out vec4 outColor;");
src.push("out highp ivec4 outNormal;");
src.push("void main(void) {");
if (clipping) {
src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
Expand All @@ -135,7 +136,7 @@ class TrianglesInstancingPickNormalsRenderer extends VBOSceneModelTriangleInstan
if (scene.logarithmicDepthBufferEnabled) {
src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
}
src.push(" outColor = vec4((vWorldNormal * 0.5) + 0.5, 1.0);");
src.push(` outNormal = ivec4(vWorldNormal * float(${math.MAX_INT}), 1.0);`);
src.push("}");
return src;
}
Expand Down
18 changes: 14 additions & 4 deletions src/viewer/scene/webgl/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,20 +1477,30 @@ const Renderer = function (scene, options) {
frameCtx.pickViewMatrix = pickViewMatrix;
frameCtx.pickProjMatrix = pickProjMatrix;

const pickNormalBuffer = renderBufferManager.getRenderBuffer("pick-normal");

pickNormalBuffer.bind(gl.RGBA32I);

gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);

gl.clearColor(0, 0, 0, 0);
gl.enable(gl.DEPTH_TEST);
gl.disable(gl.CULL_FACE);
gl.disable(gl.BLEND);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.clear(gl.DEPTH_BUFFER_BIT);
gl.clearBufferiv(gl.COLOR, 0, new Int32Array([0, 0, 0, 0]));

pickable.drawPickNormals(frameCtx); // Draw color-encoded fragment World-space normals

const resolutionScale = scene.canvas.resolutionScale;
const pix = pickBuffer.read(Math.round(canvasPos[0] * resolutionScale), Math.round(canvasPos[1] * resolutionScale));
const pix = pickNormalBuffer.read(Math.round(canvasPos[0] * resolutionScale), Math.round(canvasPos[1] * resolutionScale), gl.RGBA_INTEGER, gl.INT, Int32Array, 4);

const worldNormal = [(pix[0] / 256.0) - 0.5, (pix[1] / 256.0) - 0.5, (pix[2] / 256.0) - 0.5];
pickNormalBuffer.unbind();

const worldNormal = [
pix[0] / math.MAX_INT,
pix[1] / math.MAX_INT,
pix[2] / math.MAX_INT,
];

math.normalizeVec3(worldNormal);

Expand Down

0 comments on commit 8e7beca

Please sign in to comment.