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

PrimitiveGaussianSplatVS.glsl 7.5KB

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