智慧水务管理系统 - 精河县供水工程综合管理平台

PrimitiveGaussianSplatVS.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "//\n\
  3. // Vertex shader for Gaussian splats.\n\
  4. \n\
  5. // The splats are rendered as quads in view space. Splat attributes are loaded from a texture with precomputed 3D covariance.\n\
  6. \n\
  7. // Passes local quad coordinates and color to the fragment shader for Gaussian evaluation. \n\
  8. //\n\
  9. // Discards splats outside the view frustum or with negligible screen size.\n\
  10. //\n\
  11. #if defined(HAS_SPHERICAL_HARMONICS)\n\
  12. const uint coefficientCount[3] = uint[3](3u,8u,15u);\n\
  13. const float SH_C1 = 0.48860251;\n\
  14. const float SH_C2[5] = float[5](\n\
  15. 1.092548430,\n\
  16. -1.09254843,\n\
  17. 0.315391565,\n\
  18. -1.09254843,\n\
  19. 0.546274215\n\
  20. );\n\
  21. \n\
  22. const float SH_C3[7] = float[7](\n\
  23. -0.59004358,\n\
  24. 2.890611442,\n\
  25. -0.45704579,\n\
  26. 0.373176332,\n\
  27. -0.45704579,\n\
  28. 1.445305721,\n\
  29. -0.59004358\n\
  30. );\n\
  31. \n\
  32. //Retrieve SH coefficient. Currently RG32UI format\n\
  33. uvec2 loadSHCoeff(uint splatID, int index) {\n\
  34. ivec2 shTexSize = textureSize(u_sphericalHarmonicsTexture, 0);\n\
  35. uint dims = coefficientCount[uint(u_sphericalHarmonicsDegree)-1u];\n\
  36. uint splatsPerRow = uint(shTexSize.x) / dims;\n\
  37. uint shIndex = (splatID%splatsPerRow) * dims + uint(index);\n\
  38. ivec2 shPosCoord = ivec2(shIndex, splatID / splatsPerRow);\n\
  39. return texelFetch(u_sphericalHarmonicsTexture, shPosCoord, 0).rg;\n\
  40. }\n\
  41. \n\
  42. //Unpack RG32UI half float coefficients to vec3\n\
  43. vec3 halfToVec3(uvec2 packed) {\n\
  44. return vec3(unpackHalf2x16(packed.x), unpackHalf2x16(packed.y).x);\n\
  45. }\n\
  46. \n\
  47. vec3 loadAndExpandSHCoeff(uint splatID, int index) {\n\
  48. uvec2 coeff = loadSHCoeff(splatID, index);\n\
  49. return halfToVec3(coeff);\n\
  50. }\n\
  51. \n\
  52. vec3 evaluateSH(uint splatID, vec3 viewDir) {\n\
  53. vec3 result = vec3(0.0);\n\
  54. int coeffIndex = 0;\n\
  55. float x = viewDir.x, y = viewDir.y, z = viewDir.z;\n\
  56. \n\
  57. if (u_sphericalHarmonicsDegree >= 1.) {\n\
  58. vec3 sh1 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  59. vec3 sh2 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  60. vec3 sh3 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  61. result += -SH_C1 * y * sh1 + SH_C1 * z * sh2 - SH_C1 * x * sh3;\n\
  62. \n\
  63. if (u_sphericalHarmonicsDegree >= 2.) {\n\
  64. float xx = x * x;\n\
  65. float yy = y * y;\n\
  66. float zz = z * z;\n\
  67. float xy = x * y;\n\
  68. float yz = y * z;\n\
  69. float xz = x * z;\n\
  70. \n\
  71. vec3 sh4 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  72. vec3 sh5 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  73. vec3 sh6 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  74. vec3 sh7 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  75. vec3 sh8 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  76. result += SH_C2[0] * xy * sh4 +\n\
  77. SH_C2[1] * yz * sh5 +\n\
  78. SH_C2[2] * (2.0f * zz - xx - yy) * sh6 +\n\
  79. SH_C2[3] * xz * sh7 +\n\
  80. SH_C2[4] * (xx - yy) * sh8;\n\
  81. \n\
  82. if (u_sphericalHarmonicsDegree >= 3.) {\n\
  83. vec3 sh9 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  84. vec3 sh10 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  85. vec3 sh11 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  86. vec3 sh12 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  87. vec3 sh13 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  88. vec3 sh14 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  89. vec3 sh15 = loadAndExpandSHCoeff(splatID, coeffIndex++);\n\
  90. result += SH_C3[0] * y * (3.0f * xx - yy) * sh9 +\n\
  91. SH_C3[1] * xy * z * sh10 +\n\
  92. SH_C3[2] * y * (4.0f * zz - xx - yy) * sh11 +\n\
  93. SH_C3[3] * z * (2.0f * zz - 3.0f * xx - 3.0f * yy) * sh12 +\n\
  94. SH_C3[4] * x * (4.0f * zz - xx - yy) * sh13 +\n\
  95. SH_C3[5] * z * (xx - yy) * sh14 +\n\
  96. SH_C3[6] * x * (xx - 3.0f * yy) * sh15;\n\
  97. }\n\
  98. }\n\
  99. }\n\
  100. return result;\n\
  101. }\n\
  102. #endif\n\
  103. \n\
  104. // Transforms and projects splat covariance into screen space and extracts the major and minor axes of the Gaussian ellipsoid\n\
  105. // which is used to calculate the vertex position in clip space.\n\
  106. vec4 calcCovVectors(vec3 viewPos, mat3 Vrk) {\n\
  107. vec4 t = vec4(viewPos, 1.0);\n\
  108. vec2 focal = vec2(czm_projection[0][0] * czm_viewport.z, czm_projection[1][1] * czm_viewport.w);\n\
  109. \n\
  110. vec2 J1 = focal / t.z;\n\
  111. vec2 J2 = -focal * vec2(t.x, t.y) / (t.z * t.z);\n\
  112. mat3 J = mat3(\n\
  113. J1.x, 0.0, J2.x,\n\
  114. 0.0, J1.y, J2.y,\n\
  115. 0.0, 0.0, 0.0\n\
  116. );\n\
  117. \n\
  118. mat3 R = mat3(czm_modelView);\n\
  119. \n\
  120. //transform our covariance into view space\n\
  121. //ensures orientation is correct\n\
  122. mat3 Vrk_view = R * Vrk * transpose(R);\n\
  123. mat3 cov = transpose(J) * Vrk_view * J;\n\
  124. \n\
  125. float diagonal1 = cov[0][0] + .3;\n\
  126. float offDiagonal = cov[0][1];\n\
  127. float diagonal2 = cov[1][1] + .3;\n\
  128. \n\
  129. float mid = 0.5 * (diagonal1 + diagonal2);\n\
  130. float radius = length(vec2((diagonal1 - diagonal2) * 0.5, offDiagonal));\n\
  131. float lambda1 = mid + radius;\n\
  132. float lambda2 = max(mid - radius, 0.1);\n\
  133. \n\
  134. vec2 diagonalVector = normalize(vec2(offDiagonal, lambda1 - diagonal1));\n\
  135. \n\
  136. return vec4(\n\
  137. min(sqrt(2.0 * lambda1), 1024.0) * diagonalVector,\n\
  138. min(sqrt(2.0 * lambda2), 1024.0) * vec2(diagonalVector.y, -diagonalVector.x)\n\
  139. );\n\
  140. }\n\
  141. \n\
  142. highp vec4 discardVec = vec4(0.0, 0.0, 2.0, 1.0);\n\
  143. \n\
  144. void main() {\n\
  145. uint texIdx = uint(a_splatIndex);\n\
  146. // u_splatRowMask and u_splatRowShift encode the row width of the splat\n\
  147. // attribute texture. The texture width is always maximumTextureSize, which\n\
  148. // varies by GPU, so these are passed as uniforms rather than constants.\n\
  149. // rowMask = maximumTextureSize/2 - 1\n\
  150. // rowShift = log2(maximumTextureSize/2)\n\
  151. uint rowMask = uint(u_splatRowMask);\n\
  152. uint rowShift = uint(u_splatRowShift);\n\
  153. ivec2 posCoord = ivec2(int((texIdx & rowMask) << 1), int(texIdx >> rowShift));\n\
  154. vec4 splatPosition = vec4( uintBitsToFloat(uvec4(texelFetch(u_splatAttributeTexture, posCoord, 0))) );\n\
  155. \n\
  156. vec4 splatViewPos = czm_modelView * vec4(splatPosition.xyz, 1.0);\n\
  157. vec4 clipPosition = czm_projection * splatViewPos;\n\
  158. \n\
  159. float clip = 1.2 * clipPosition.w;\n\
  160. if (clipPosition.z < -clip || clipPosition.x < -clip || clipPosition.x > clip ||\n\
  161. clipPosition.y < -clip || clipPosition.y > clip) {\n\
  162. gl_Position = vec4(0.0, 0.0, 2.0, 1.0);\n\
  163. return;\n\
  164. }\n\
  165. \n\
  166. ivec2 covCoord = ivec2(int(((texIdx & rowMask) << 1) | 1u), int(texIdx >> rowShift));\n\
  167. uvec4 covariance = uvec4(texelFetch(u_splatAttributeTexture, covCoord, 0));\n\
  168. \n\
  169. gl_Position = clipPosition;\n\
  170. \n\
  171. vec2 u1 = unpackHalf2x16(covariance.x) ;\n\
  172. vec2 u2 = unpackHalf2x16(covariance.y);\n\
  173. vec2 u3 = unpackHalf2x16(covariance.z);\n\
  174. mat3 Vrk = mat3(u1.x, u1.y, u2.x, u1.y, u2.y, u3.x, u2.x, u3.x, u3.y);\n\
  175. \n\
  176. vec4 covVectors = calcCovVectors(splatViewPos.xyz, Vrk);\n\
  177. \n\
  178. if (dot(covVectors.xy, covVectors.xy) < 4.0 && dot(covVectors.zw, covVectors.zw) < 4.0) {\n\
  179. gl_Position = discardVec;\n\
  180. return;\n\
  181. }\n\
  182. \n\
  183. vec2 corner = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2) - 1.;\n\
  184. \n\
  185. gl_Position += vec4((corner.x * covVectors.xy + corner.y * covVectors.zw) / czm_viewport.zw * gl_Position.w, 0, 0);\n\
  186. gl_Position.z = clamp(gl_Position.z, -abs(gl_Position.w), abs(gl_Position.w));\n\
  187. \n\
  188. v_vertPos = corner ;\n\
  189. v_splatColor = vec4(covariance.w & 0xffu, (covariance.w >> 8) & 0xffu, (covariance.w >> 16) & 0xffu, (covariance.w >> 24) & 0xffu) / 255.0;\n\
  190. #if defined(HAS_SPHERICAL_HARMONICS)\n\
  191. vec4 splatWC = czm_inverseView * splatViewPos;\n\
  192. vec3 viewDirModel = normalize(u_inverseModelRotation * (splatWC.xyz - u_cameraPositionWC.xyz));\n\
  193. \n\
  194. v_splatColor.rgb += evaluateSH(texIdx, viewDirModel).rgb;\n\
  195. #endif\n\
  196. v_splitDirection = u_splitDirection;\n\
  197. }";