AmbientOcclusionGenerate.glsl 3.9 KB

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