Reference Source

src/viewer/scene/mesh/pick/PickTriangleShaderSource.js

  1. /**
  2. * @private
  3. */
  4.  
  5. class PickTriangleShaderSource {
  6. constructor(mesh) {
  7. this.vertex = buildVertex(mesh);
  8. this.fragment = buildFragment(mesh);
  9. }
  10. }
  11.  
  12. function buildVertex(mesh) {
  13. const scene = mesh.scene;
  14. const sectionPlanesState = scene._sectionPlanesState;
  15. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  16. const quantizedGeometry = !!mesh._geometry._state.compressGeometry;
  17. const src = [];
  18. src.push('#version 300 es');
  19. src.push("// Surface picking vertex shader");
  20. src.push("in vec3 position;");
  21. src.push("in vec4 color;");
  22. src.push("uniform mat4 modelMatrix;");
  23. src.push("uniform mat4 viewMatrix;");
  24. src.push("uniform mat4 projMatrix;");
  25. src.push("uniform vec3 offset;");
  26. if (clipping) {
  27. src.push("uniform bool clippable;");
  28. src.push("out vec4 vWorldPosition;");
  29. }
  30. if (scene.logarithmicDepthBufferEnabled) {
  31. src.push("uniform float logDepthBufFC;");
  32. src.push("out float vFragDepth;");
  33. src.push("bool isPerspectiveMatrix(mat4 m) {");
  34. src.push(" return (m[2][3] == - 1.0);");
  35. src.push("}");
  36. src.push("out float isPerspective;");
  37. }
  38.  
  39. src.push("uniform vec2 pickClipPos;");
  40.  
  41. src.push("vec4 remapClipPos(vec4 clipPos) {");
  42. src.push(" clipPos.xy /= clipPos.w;")
  43. src.push(" clipPos.xy -= pickClipPos;");
  44. src.push(" clipPos.xy *= clipPos.w;")
  45. src.push(" return clipPos;")
  46. src.push("}");
  47.  
  48. src.push("out vec4 vColor;");
  49. if (quantizedGeometry) {
  50. src.push("uniform mat4 positionsDecodeMatrix;");
  51. }
  52. src.push("void main(void) {");
  53. src.push("vec4 localPosition = vec4(position, 1.0); ");
  54. if (quantizedGeometry) {
  55. src.push("localPosition = positionsDecodeMatrix * localPosition;");
  56. }
  57. src.push(" vec4 worldPosition = modelMatrix * localPosition; ");
  58. src.push(" worldPosition.xyz = worldPosition.xyz + offset;");
  59. src.push(" vec4 viewPosition = viewMatrix * worldPosition;");
  60. if (clipping) {
  61. src.push(" vWorldPosition = worldPosition;");
  62. }
  63. src.push(" vColor = color;");
  64. src.push("vec4 clipPos = projMatrix * viewPosition;");
  65. if (scene.logarithmicDepthBufferEnabled) {
  66. src.push("vFragDepth = 1.0 + clipPos.w;");
  67. src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
  68. }
  69. src.push("gl_Position = remapClipPos(clipPos);");
  70. src.push("}");
  71. return src;
  72. }
  73.  
  74. function buildFragment(mesh) {
  75. const scene = mesh.scene;
  76. const sectionPlanesState = scene._sectionPlanesState;
  77. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  78. const src = [];
  79. src.push('#version 300 es');
  80. src.push("// Surface picking fragment shader");
  81. src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
  82. src.push("precision highp float;");
  83. src.push("precision highp int;");
  84. src.push("#else");
  85. src.push("precision mediump float;");
  86. src.push("precision mediump int;");
  87. src.push("#endif");
  88. src.push("in vec4 vColor;");
  89. if (scene.logarithmicDepthBufferEnabled) {
  90. src.push("in float isPerspective;");
  91. src.push("uniform float logDepthBufFC;");
  92. src.push("in float vFragDepth;");
  93. }
  94. if (clipping) {
  95. src.push("uniform bool clippable;");
  96. src.push("in vec4 vWorldPosition;");
  97. for (let i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  98. src.push("uniform bool sectionPlaneActive" + i + ";");
  99. src.push("uniform vec3 sectionPlanePos" + i + ";");
  100. src.push("uniform vec3 sectionPlaneDir" + i + ";");
  101. }
  102. }
  103. src.push("out vec4 outColor;");
  104. src.push("void main(void) {");
  105. if (clipping) {
  106. src.push("if (clippable) {");
  107. src.push(" float dist = 0.0;");
  108. for (let i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  109. src.push("if (sectionPlaneActive" + i + ") {");
  110. src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
  111. src.push("}");
  112. }
  113. src.push(" if (dist > 0.0) { discard; }");
  114. src.push("}");
  115. }
  116. if (scene.logarithmicDepthBufferEnabled) {
  117. src.push("gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
  118. }
  119. src.push(" outColor = vColor;");
  120. src.push("}");
  121. return src;
  122. }
  123.  
  124. export {PickTriangleShaderSource};