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

ConvolveSpecularMapFS.glsl 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. precision highp float;
  2. in vec3 v_textureCoordinates;
  3. uniform float u_roughness;
  4. uniform samplerCube u_radianceTexture;
  5. uniform vec3 u_faceDirection;
  6. float vdcRadicalInverse(int i)
  7. {
  8. float r;
  9. float base = 2.0;
  10. float value = 0.0;
  11. float invBase = 1.0 / base;
  12. float invBi = invBase;
  13. for (int x = 0; x < 100; x++)
  14. {
  15. if (i <= 0)
  16. {
  17. break;
  18. }
  19. r = mod(float(i), base);
  20. value += r * invBi;
  21. invBi *= invBase;
  22. i = int(float(i) * invBase);
  23. }
  24. return value;
  25. }
  26. vec2 hammersley2D(int i, int N)
  27. {
  28. return vec2(float(i) / float(N), vdcRadicalInverse(i));
  29. }
  30. vec3 importanceSampleGGX(vec2 xi, float alphaRoughness, vec3 N)
  31. {
  32. float alphaRoughnessSquared = alphaRoughness * alphaRoughness;
  33. float phi = czm_twoPi * xi.x;
  34. float cosTheta = sqrt((1.0 - xi.y) / (1.0 + (alphaRoughnessSquared - 1.0) * xi.y));
  35. float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
  36. vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
  37. vec3 upVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
  38. vec3 tangentX = normalize(cross(upVector, N));
  39. vec3 tangentY = cross(N, tangentX);
  40. return tangentX * H.x + tangentY * H.y + N * H.z;
  41. }
  42. // Sample count is relatively low for the sake of performance, but should still be enough to prevent artifacting in lower roughnesses
  43. const int samples = 128;
  44. void main() {
  45. vec3 normal = u_faceDirection;
  46. vec3 V = normalize(v_textureCoordinates);
  47. float roughness = u_roughness;
  48. vec4 color = vec4(0.0);
  49. float weight = 0.0;
  50. for (int i = 0; i < samples; ++i) {
  51. vec2 xi = hammersley2D(i, samples);
  52. vec3 H = importanceSampleGGX(xi, roughness, V);
  53. vec3 L = 2.0 * dot(V, H) * H - V; // reflected vector
  54. float NdotL = max(dot(V, L), 0.0);
  55. if (NdotL > 0.0) {
  56. color += vec4(czm_textureCube(u_radianceTexture, L).rgb, 1.0) * NdotL;
  57. weight += NdotL;
  58. }
  59. }
  60. out_FragColor = color / weight;
  61. }