Reference Source

src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickNormalsRenderer.js

  1. import { math } from "../../../../../math/math.js";
  2. import {TrianglesBatchingRenderer} from "./TrianglesBatchingRenderer.js";
  3.  
  4.  
  5. /**
  6. * @private
  7. */
  8. export class TrianglesPickNormalsRenderer extends TrianglesBatchingRenderer {
  9.  
  10. _buildVertexShader() {
  11. const scene = this._scene;
  12. const clipping = scene._sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  13. const src = [];
  14. src.push("#version 300 es");
  15. src.push("// Triangles batching pick normals vertex shader");
  16. src.push("uniform int renderPass;");
  17. src.push("in vec3 position;");
  18. if (scene.entityOffsetsEnabled) {
  19. src.push("in vec3 offset;");
  20. }
  21. src.push("in vec3 normal;");
  22. src.push("in float flags;");
  23. src.push("uniform bool pickInvisible;");
  24.  
  25. this._addMatricesUniformBlockLines(src);
  26.  
  27. this._addRemapClipPosLines(src, 3);
  28.  
  29. if (scene.logarithmicDepthBufferEnabled) {
  30. src.push("uniform float logDepthBufFC;");
  31. src.push("out float vFragDepth;");
  32. src.push("bool isPerspectiveMatrix(mat4 m) {");
  33. src.push(" return (m[2][3] == - 1.0);");
  34. src.push("}");
  35. src.push("out float isPerspective;");
  36. }
  37. src.push("vec3 octDecode(vec2 oct) {");
  38. src.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));");
  39. src.push(" if (v.z < 0.0) {");
  40. src.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);");
  41. src.push(" }");
  42. src.push(" return normalize(v);");
  43. src.push("}");
  44. if (clipping) {
  45. src.push("out vec4 vWorldPosition;");
  46. src.push("out float vFlags;");
  47. }
  48. src.push("out vec3 vWorldNormal;");
  49. src.push("out vec4 outColor;");
  50. src.push("void main(void) {");
  51. // pickFlag = NOT_RENDERED | PICK
  52. // renderPass = PICK
  53. src.push(`int pickFlag = int(flags) >> 12 & 0xF;`);
  54. src.push(`if (pickFlag != renderPass) {`);
  55. src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"); // Cull vertex
  56. src.push(" } else {");
  57. src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
  58. if (scene.entityOffsetsEnabled) {
  59. src.push(" worldPosition.xyz = worldPosition.xyz + offset;");
  60. }
  61. src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");
  62. src.push(" vec3 worldNormal = octDecode(normal.xy); ");
  63. src.push(" vWorldNormal = worldNormal;");
  64. if (clipping) {
  65. src.push(" vWorldPosition = worldPosition;");
  66. src.push(" vFlags = flags;");
  67. }
  68. src.push("vec4 clipPos = projMatrix * viewPosition;");
  69. if (scene.logarithmicDepthBufferEnabled) {
  70. src.push("vFragDepth = 1.0 + clipPos.w;");
  71. src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
  72. }
  73. src.push("gl_Position = remapClipPos(clipPos);");
  74. src.push(" }");
  75. src.push("}");
  76. return src;
  77. }
  78.  
  79. _buildFragmentShader() {
  80. const scene = this._scene;
  81. const sectionPlanesState = scene._sectionPlanesState;
  82. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  83. const src = [];
  84. src.push("#version 300 es");
  85. src.push("// Triangles batching pick normals fragment shader");
  86. src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
  87. src.push("precision highp float;");
  88. src.push("precision highp int;");
  89. src.push("#else");
  90. src.push("precision mediump float;");
  91. src.push("precision mediump int;");
  92. src.push("#endif");
  93. if (scene.logarithmicDepthBufferEnabled) {
  94. src.push("in float isPerspective;");
  95. src.push("uniform float logDepthBufFC;");
  96. src.push("in float vFragDepth;");
  97. }
  98. if (clipping) {
  99. src.push("in vec4 vWorldPosition;");
  100. src.push("in float vFlags;");
  101. for (var i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  102. src.push("uniform bool sectionPlaneActive" + i + ";");
  103. src.push("uniform vec3 sectionPlanePos" + i + ";");
  104. src.push("uniform vec3 sectionPlaneDir" + i + ";");
  105. }
  106. }
  107. src.push("in vec3 vWorldNormal;");
  108. src.push("out highp ivec4 outNormal;");
  109. src.push("void main(void) {");
  110. if (clipping) {
  111. src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
  112. src.push(" if (clippable) {");
  113. src.push(" float dist = 0.0;");
  114. for (let i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  115. src.push(" if (sectionPlaneActive" + i + ") {");
  116. src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
  117. src.push(" }");
  118. }
  119. src.push(" if (dist > 0.0) { discard; }");
  120. src.push(" }");
  121. }
  122. if (scene.logarithmicDepthBufferEnabled) {
  123. src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
  124. }
  125. src.push(` outNormal = ivec4(vWorldNormal * float(${math.MAX_INT}), 1.0);`);
  126. src.push("}");
  127. return src;
  128. }
  129. }