Reference Source

src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsPickMeshRenderer.js

  1. import {VBOBatchingPointsRenderer} from "../VBOBatchingPointsRenderer.js";
  2.  
  3. /**
  4. * @private
  5. */
  6.  
  7.  
  8. export class VBOBatchingPointsPickMeshRenderer extends VBOBatchingPointsRenderer {
  9. _getHash() {
  10. return this._scene._sectionPlanesState.getHash() + (this._scene.pointsMaterial.hash);
  11. }
  12.  
  13. _buildVertexShader() {
  14.  
  15. const scene = this._scene;
  16. const clipping = scene._sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  17. const pointsMaterial = scene.pointsMaterial._state;
  18. const src = [];
  19. src.push ('#version 300 es');
  20. src.push("// Points batching pick mesh vertex shader");
  21. src.push("uniform int renderPass;");
  22.  
  23. src.push("in vec3 position;");
  24. if (scene.entityOffsetsEnabled) {
  25. src.push("in vec3 offset;");
  26. }
  27. src.push("in float flags;");
  28.  
  29. src.push("in vec4 pickColor;");
  30.  
  31. src.push("uniform bool pickInvisible;");
  32.  
  33. this._addMatricesUniformBlockLines(src);
  34.  
  35. this._addRemapClipPosLines(src);
  36.  
  37. src.push("uniform float pointSize;");
  38. if (pointsMaterial.perspectivePoints) {
  39. src.push("uniform float nearPlaneHeight;");
  40. }
  41.  
  42. if (scene.logarithmicDepthBufferEnabled) {
  43. src.push("uniform float logDepthBufFC;");
  44. src.push("out float vFragDepth;");
  45. }
  46.  
  47. if (clipping) {
  48. src.push("out vec4 vWorldPosition;");
  49. src.push("out float vFlags;");
  50. }
  51.  
  52. src.push("out vec4 vPickColor;");
  53.  
  54. src.push("void main(void) {");
  55.  
  56. // pickFlag = NOT_RENDERED | PICK
  57. // renderPass = PICK
  58.  
  59. src.push(`int pickFlag = int(flags) >> 12 & 0xF;`);
  60. src.push(`if (pickFlag != renderPass) {`);
  61. src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"); // Cull vertex
  62.  
  63. src.push(" } else {");
  64. src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
  65. if (scene.entityOffsetsEnabled) {
  66. src.push(" worldPosition.xyz = worldPosition.xyz + offset;");
  67. }
  68. src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");
  69. src.push(" vPickColor = vec4(float(pickColor.r) / 255.0, float(pickColor.g) / 255.0, float(pickColor.b) / 255.0, float(pickColor.a) / 255.0);");
  70. if (clipping) {
  71. src.push(" vWorldPosition = worldPosition;");
  72. src.push(" vFlags = flags;");
  73. }
  74. src.push("vec4 clipPos = projMatrix * viewPosition;");
  75. if (scene.logarithmicDepthBufferEnabled) {
  76. src.push("vFragDepth = 1.0 + clipPos.w;");
  77. }
  78. src.push("gl_Position = remapClipPos(clipPos);");
  79. if (pointsMaterial.perspectivePoints) {
  80. src.push("gl_PointSize = (nearPlaneHeight * pointSize) / clipPos.w;");
  81. src.push("gl_PointSize = max(gl_PointSize, " + Math.floor(pointsMaterial.minPerspectivePointSize) + ".0);");
  82. src.push("gl_PointSize = min(gl_PointSize, " + Math.floor(pointsMaterial.maxPerspectivePointSize) + ".0);");
  83. } else {
  84. src.push("gl_PointSize = pointSize;");
  85. }
  86. src.push("gl_PointSize += 10.0;");
  87. src.push(" }");
  88. src.push("}");
  89. return src;
  90. }
  91.  
  92. _buildFragmentShader() {
  93. const scene = this._scene;
  94. const sectionPlanesState = scene._sectionPlanesState;
  95. const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
  96. const src = [];
  97. src.push ('#version 300 es');
  98. src.push("// Points batching pick mesh vertex shader");
  99. src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
  100. src.push("precision highp float;");
  101. src.push("precision highp int;");
  102. src.push("#else");
  103. src.push("precision mediump float;");
  104. src.push("precision mediump int;");
  105. src.push("#endif");
  106. if (scene.logarithmicDepthBufferEnabled) {
  107. src.push("uniform float logDepthBufFC;");
  108. src.push("in float vFragDepth;");
  109. }
  110. if (clipping) {
  111. src.push("in vec4 vWorldPosition;");
  112. src.push("in float vFlags;");
  113. for (var i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  114. src.push("uniform bool sectionPlaneActive" + i + ";");
  115. src.push("uniform vec3 sectionPlanePos" + i + ";");
  116. src.push("uniform vec3 sectionPlaneDir" + i + ";");
  117. }
  118. }
  119. src.push("in vec4 vPickColor;");
  120. src.push("out vec4 outColor;");
  121. src.push("void main(void) {");
  122. if (scene.pointsMaterial.roundPoints) {
  123. src.push(" vec2 cxy = 2.0 * gl_PointCoord - 1.0;");
  124. src.push(" float r = dot(cxy, cxy);");
  125. src.push(" if (r > 1.0) {");
  126. src.push(" discard;");
  127. src.push(" }");
  128. }
  129. if (clipping) {
  130. src.push(" bool clippable = (int(vFlags) >> 16 & 0xF) == 1;");
  131. src.push(" if (clippable) {");
  132. src.push(" float dist = 0.0;");
  133. for (let i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
  134. src.push(" if (sectionPlaneActive" + i + ") {");
  135. src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
  136. src.push(" }");
  137. }
  138. src.push(" if (dist > 0.0) { discard; }");
  139. src.push(" }");
  140. }
  141. if (scene.logarithmicDepthBufferEnabled) {
  142. src.push("gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;");
  143. }
  144. src.push(" outColor = vPickColor; ");
  145. src.push("}");
  146. return src;
  147. }
  148. }