AmbientOcclusionGenerate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform sampler2D randomTexture;\n\
  3. uniform sampler2D depthTexture;\n\
  4. uniform float intensity;\n\
  5. uniform float bias;\n\
  6. uniform float lengthCap;\n\
  7. uniform float stepSize;\n\
  8. uniform float frustumLength;\n\
  9. varying vec2 v_textureCoordinates;\n\
  10. vec4 clipToEye(vec2 uv, float depth)\n\
  11. {\n\
  12. vec2 xy = vec2((uv.x * 2.0 - 1.0), ((1.0 - uv.y) * 2.0 - 1.0));\n\
  13. vec4 posEC = czm_inverseProjection * vec4(xy, depth, 1.0);\n\
  14. posEC = posEC / posEC.w;\n\
  15. return posEC;\n\
  16. }\n\
  17. vec3 getNormalXEdge(vec3 posInCamera, float depthU, float depthD, float depthL, float depthR, vec2 pixelSize)\n\
  18. {\n\
  19. vec4 posInCameraUp = clipToEye(v_textureCoordinates - vec2(0.0, pixelSize.y), depthU);\n\
  20. vec4 posInCameraDown = clipToEye(v_textureCoordinates + vec2(0.0, pixelSize.y), depthD);\n\
  21. vec4 posInCameraLeft = clipToEye(v_textureCoordinates - vec2(pixelSize.x, 0.0), depthL);\n\
  22. vec4 posInCameraRight = clipToEye(v_textureCoordinates + vec2(pixelSize.x, 0.0), depthR);\n\
  23. vec3 up = posInCamera.xyz - posInCameraUp.xyz;\n\
  24. vec3 down = posInCameraDown.xyz - posInCamera.xyz;\n\
  25. vec3 left = posInCamera.xyz - posInCameraLeft.xyz;\n\
  26. vec3 right = posInCameraRight.xyz - posInCamera.xyz;\n\
  27. vec3 DX = length(left) < length(right) ? left : right;\n\
  28. vec3 DY = length(up) < length(down) ? up : down;\n\
  29. return normalize(cross(DY, DX));\n\
  30. }\n\
  31. void main(void)\n\
  32. {\n\
  33. float depth = czm_readDepth(depthTexture, v_textureCoordinates);\n\
  34. vec4 posInCamera = clipToEye(v_textureCoordinates, depth);\n\
  35. if (posInCamera.z > frustumLength)\n\
  36. {\n\
  37. gl_FragColor = vec4(1.0);\n\
  38. return;\n\
  39. }\n\
  40. vec2 pixelSize = czm_pixelRatio / czm_viewport.zw;\n\
  41. float depthU = czm_readDepth(depthTexture, v_textureCoordinates - vec2(0.0, pixelSize.y));\n\
  42. float depthD = czm_readDepth(depthTexture, v_textureCoordinates + vec2(0.0, pixelSize.y));\n\
  43. float depthL = czm_readDepth(depthTexture, v_textureCoordinates - vec2(pixelSize.x, 0.0));\n\
  44. float depthR = czm_readDepth(depthTexture, v_textureCoordinates + vec2(pixelSize.x, 0.0));\n\
  45. vec3 normalInCamera = getNormalXEdge(posInCamera.xyz, depthU, depthD, depthL, depthR, pixelSize);\n\
  46. float ao = 0.0;\n\
  47. vec2 sampleDirection = vec2(1.0, 0.0);\n\
  48. float gapAngle = 90.0 * czm_radiansPerDegree;\n\
  49. float randomVal = texture2D(randomTexture, v_textureCoordinates).x;\n\
  50. for (int i = 0; i < 4; i++)\n\
  51. {\n\
  52. float newGapAngle = gapAngle * (float(i) + randomVal);\n\
  53. float cosVal = cos(newGapAngle);\n\
  54. float sinVal = sin(newGapAngle);\n\
  55. vec2 rotatedSampleDirection = vec2(cosVal * sampleDirection.x - sinVal * sampleDirection.y, sinVal * sampleDirection.x + cosVal * sampleDirection.y);\n\
  56. float localAO = 0.0;\n\
  57. float localStepSize = stepSize;\n\
  58. for (int j = 0; j < 6; j++)\n\
  59. {\n\
  60. vec2 newCoords = v_textureCoordinates + rotatedSampleDirection * localStepSize * pixelSize;\n\
  61. if(newCoords.x > 1.0 || newCoords.y > 1.0 || newCoords.x < 0.0 || newCoords.y < 0.0)\n\
  62. {\n\
  63. break;\n\
  64. }\n\
  65. float stepDepthInfo = czm_readDepth(depthTexture, newCoords);\n\
  66. vec4 stepPosInCamera = clipToEye(newCoords, stepDepthInfo);\n\
  67. vec3 diffVec = stepPosInCamera.xyz - posInCamera.xyz;\n\
  68. float len = length(diffVec);\n\
  69. if (len > lengthCap)\n\
  70. {\n\
  71. break;\n\
  72. }\n\
  73. float dotVal = clamp(dot(normalInCamera, normalize(diffVec)), 0.0, 1.0 );\n\
  74. float weight = len / lengthCap;\n\
  75. weight = 1.0 - weight * weight;\n\
  76. if (dotVal < bias)\n\
  77. {\n\
  78. dotVal = 0.0;\n\
  79. }\n\
  80. localAO = max(localAO, dotVal * weight);\n\
  81. localStepSize += stepSize;\n\
  82. }\n\
  83. ao += localAO;\n\
  84. }\n\
  85. ao /= 4.0;\n\
  86. ao = 1.0 - clamp(ao, 0.0, 1.0);\n\
  87. ao = pow(ao, intensity);\n\
  88. gl_FragColor = vec4(vec3(ao), 1.0);\n\
  89. }\n\
  90. ";