fragment-shader.glsl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. const float GOLDEN_RATIO = 1.61803398875;
  2. const vec3 EXTINCTION_MULT = vec3(0.8, 0.8, 1.0);
  3. const float DUAL_LOBE_WEIGHT = 0.7;
  4. const float AMBIENT_STRENGTH = 0.1;
  5. const float CLOUD_LIGHT_MULTIPLIER = 50.0;
  6. const vec3 CLOUD_LIGHT_DIR = normalize(vec3(-1.0, 0.0, 0.0));
  7. const float CLOUD_EXPOSURE = 1.0;
  8. const float CLOUD_STEPS_MIN = 16.0;
  9. const float CLOUD_STEPS_MAX = 128.0;
  10. const float CLOUD_LIGHT_STEPS = 12.0;
  11. const float CLOUD_DENSITY = 0.5;
  12. const vec3 CLOUD_OFFSET = vec3(0.0, 0.0, 0.0);
  13. vec3 CLOUD_SIZE = vec3(4000.0);
  14. vec3 CLOUD_BOUNDS_MIN;
  15. vec3 CLOUD_BOUNDS_MAX;
  16. const float CLOUD_BASE_STRENGTH = 0.8;
  17. const float CLOUD_DETAIL_STRENGTH = 0.2;
  18. const vec3 CLOUD_COLOUR = vec3(1.0);
  19. const float CLOUD_FALLOFF = 25.0;
  20. // #define SHOW_CLOUD_MAP
  21. float HenyeyGreenstein(float g, float mu) {
  22. float gg = g * g;
  23. return (1.0 / (4.0 * PI)) * ((1.0 - gg) / pow(1.0 + gg - 2.0 * g * mu, 1.5));
  24. }
  25. float IsotropicPhaseFunction(float g, float costh) {
  26. return 1.0 / (4.0 * PI);
  27. }
  28. float DualHenyeyGreenstein(float g, float costh) {
  29. return mix(HenyeyGreenstein(-g, costh), HenyeyGreenstein(g, costh), DUAL_LOBE_WEIGHT);
  30. }
  31. float PhaseFunction(float g, float costh) {
  32. return DualHenyeyGreenstein(g, costh);
  33. }
  34. vec4 SamplePerlinWorleyNoise(vec3 pos) {
  35. vec3 coord = pos.xzy * vec3(1.0 / 32.0, 1.0 / 32.0, 1.0 / 64.0) * 1.0;
  36. vec4 s = texture(perlinWorley, coord);
  37. return s;
  38. }
  39. float SampleLowResolutionCloudMap(vec3 p) {
  40. float sdfValue = sdfBox(p, vec3(50.0));
  41. sdfValue = sdCutSphere(p, 60.0, -40.0);
  42. sdfValue = min(sdfValue, sdCutSphere(p - vec3(60.0, -20.0, 0.0), 40.0, -20.0));
  43. sdfValue = min(sdfValue, sdCutSphere(p - vec3(-60.0, -20.0, -50.0), 20.0, -20.0));
  44. return sdfValue;
  45. }
  46. float SampleHighResolutionCloudDetail(float cloudSDF, vec3 worldPos, vec3 cameraOrigin, float curTime) {
  47. float cloud = circularOut(linearstep(0.0, -CLOUD_FALLOFF, cloudSDF)) * 0.85;
  48. if(cloud > 0.0) {
  49. vec3 samplePos = worldPos + vec3(-2.0 * curTime, 0.0, curTime) * 1.5;
  50. float shapeSize = 0.4;
  51. vec4 perlinWorleySample = SamplePerlinWorleyNoise(samplePos * shapeSize);
  52. float shapeStrength = CLOUD_BASE_STRENGTH;
  53. cloud = saturate(remap(cloud, shapeStrength * perlinWorleySample.x, 1.0, 0.0, 1.0));
  54. if(cloud > 0.0) {
  55. float distToSample = distance(cameraOrigin, worldPos);
  56. float t_detailDropout = smoothstep(1000.0, 800.0, distToSample);
  57. if (t_detailDropout > 0.0) {
  58. samplePos += vec3(4.0 * curTime, 3.0 * curTime, 2.0 * curTime) * 0.01;
  59. float detailSize = 1.8;
  60. float detailStrength = CLOUD_DETAIL_STRENGTH * t_detailDropout;
  61. float detail = SamplePerlinWorleyNoise(detailSize * samplePos).y;
  62. cloud = saturate(remap(cloud, detailStrength * detail, 1.0, 0.0, 1.0));
  63. }
  64. }
  65. }
  66. return cloud * CLOUD_DENSITY;
  67. }
  68. // Adapted from: https://twitter.com/FewesW/status/1364629939568451587/photo/1
  69. vec3 MultipleOctaveScattering(float density, float mu) {
  70. float attenuation = 0.2;
  71. float contribution = 0.2;
  72. float phaseAttenuation = 0.5;
  73. float a = 1.0;
  74. float b = 1.0;
  75. float c = 1.0;
  76. float g = 0.85;
  77. const float scatteringOctaves = 4.0;
  78. vec3 luminance = vec3(0.0);
  79. for (float i = 0.0; i < scatteringOctaves; i++) {
  80. float phaseFunction = PhaseFunction(0.3 * c, mu);
  81. vec3 beers = exp(-density * EXTINCTION_MULT * a);
  82. luminance += b * phaseFunction * beers;
  83. a *= attenuation;
  84. b *= contribution;
  85. c *= (1.0 - phaseAttenuation);
  86. }
  87. return luminance;
  88. }
  89. vec3 CalculateLightEnergy(
  90. vec3 lightOrigin, vec3 lightDirection, vec3 cameraOrigin, float mu, float maxDistance, float curTime) {
  91. float stepLength = maxDistance / CLOUD_LIGHT_STEPS;
  92. float lightRayDensity = 0.0;
  93. float distAccumulated = 0.0;
  94. for(float j = 0.0; j < CLOUD_LIGHT_STEPS; j++) {
  95. vec3 lightSamplePos = lightOrigin + lightDirection * distAccumulated;
  96. float cloudSDF = SampleLowResolutionCloudMap(lightSamplePos);
  97. lightRayDensity += SampleHighResolutionCloudDetail(cloudSDF, lightSamplePos, cameraOrigin, curTime) * stepLength;
  98. distAccumulated += stepLength;
  99. }
  100. vec3 beersLaw = MultipleOctaveScattering(lightRayDensity, mu);
  101. vec3 powder = 1.0 - exp(-lightRayDensity * 2.0 * EXTINCTION_MULT);
  102. return beersLaw * mix(2.0 * powder, vec3(1.0), remap(mu, -1.0, 1.0, 0.0, 1.0));
  103. }
  104. struct ScatteringTransmittance {
  105. vec3 scattering;
  106. vec3 transmittance;
  107. };
  108. ScatteringTransmittance CloudMarch(vec2 pixelCoords, vec3 cameraOrigin, vec3 cameraDirection, float curTime) {
  109. AABB cloudAABB;
  110. cloudAABB.min = CLOUD_BOUNDS_MIN;
  111. cloudAABB.max = CLOUD_BOUNDS_MAX;
  112. ScatteringTransmittance result;
  113. result.scattering = vec3(0.0);
  114. result.transmittance = vec3(1.0);
  115. AABBIntersectResult rayCloudIntersection = intersectAABB(cameraOrigin, cameraDirection, cloudAABB);
  116. if (rayCloudIntersection.near >= rayCloudIntersection.far) {
  117. // Debug
  118. // return vec4(vec3(0.0), 0.0);
  119. return result;
  120. }
  121. if (insideAABB(cameraOrigin, cloudAABB)) {
  122. rayCloudIntersection.near = 0.0;
  123. }
  124. vec3 sunDirection = CLOUD_LIGHT_DIR;
  125. vec3 sunLightColour = vec3(1.0);
  126. vec3 sunLight = sunLightColour * CLOUD_LIGHT_MULTIPLIER;
  127. vec3 ambient = vec3(AMBIENT_STRENGTH * sunLightColour);
  128. // TODO: Cap steps based on distance
  129. vec2 aspect = vec2(1.0, resolution.y / resolution.x);
  130. float blueNoiseSample = texture2D(blueNoise, (pixelCoords / resolution + 0.5) * aspect * (resolution.x / 32.0)).x;
  131. // Animating Noise For Integration Over Time
  132. // https://blog.demofox.org/2017/10/31/animating-noise-for-integration-over-time/
  133. blueNoiseSample = fract(blueNoiseSample + float(frame % 32) * GOLDEN_RATIO);
  134. float mu = dot(cameraDirection, sunDirection);
  135. float phaseFunction = PhaseFunction(0.3, mu);
  136. float distNearToFar = rayCloudIntersection.far - rayCloudIntersection.near;
  137. float stepDropoff = linearstep(1.0, 0.0, pow(dot(vec3(0.0, 1.0, 0.0), cameraDirection), 4.0));
  138. const int NUM_COUNT = 16;
  139. float lqStepLength = distNearToFar / CLOUD_STEPS_MIN;
  140. float hqStepLength = lqStepLength / float(NUM_COUNT);
  141. float numCloudSteps = CLOUD_STEPS_MAX;
  142. float offset = lqStepLength * blueNoiseSample;
  143. float distTravelled = rayCloudIntersection.near;
  144. int hqMarcherCountdown = 0;
  145. float previousStepLength = 0.0;
  146. for (float i = 0.0; i < numCloudSteps; i++) {
  147. if (distTravelled > rayCloudIntersection.far) {
  148. break;
  149. }
  150. vec3 samplePos = cameraOrigin + cameraDirection * distTravelled;
  151. float cloudMapSDFSample = SampleLowResolutionCloudMap(samplePos);
  152. float currentStepLength = cloudMapSDFSample;
  153. if (hqMarcherCountdown <= 0) {
  154. if (cloudMapSDFSample < hqStepLength) {
  155. // Hit some clouds, step back
  156. hqMarcherCountdown = NUM_COUNT;
  157. distTravelled += hqStepLength * blueNoiseSample;
  158. } else {
  159. distTravelled += currentStepLength;
  160. continue;
  161. }
  162. }
  163. if (hqMarcherCountdown > 0) {
  164. hqMarcherCountdown--;
  165. if (cloudMapSDFSample < 0.0) {
  166. hqMarcherCountdown = NUM_COUNT;
  167. float extinction = SampleHighResolutionCloudDetail(cloudMapSDFSample, samplePos, cameraOrigin, curTime);
  168. if (extinction > 0.01) {
  169. vec3 luminance = ambient + sunLight * CalculateLightEnergy(samplePos, sunDirection, cameraOrigin, mu, 50.0, curTime);
  170. vec3 transmittance = exp(-extinction * hqStepLength * EXTINCTION_MULT);
  171. vec3 integScatt = extinction * (luminance - luminance * transmittance) / extinction;
  172. result.scattering += result.transmittance * integScatt;
  173. result.transmittance *= transmittance;
  174. if (length(result.transmittance) <= 0.01) {
  175. result.transmittance = vec3(0.0);
  176. break;
  177. }
  178. }
  179. }
  180. distTravelled += hqStepLength;
  181. }
  182. previousStepLength = currentStepLength;
  183. }
  184. result.scattering = col3(result.scattering) * CLOUD_COLOUR;
  185. result.transmittance = saturate3(result.transmittance);
  186. return result;
  187. }
  188. float RenderGlow(float dist, float radius, float intensity) {
  189. dist = max(dist, 1e-6);
  190. return (1.0 - exp(-25.0 * (radius / dist))) * 0.1 + (1.0 - exp(-0.05 * (radius / dist) * (radius / dist))) * 2.0;
  191. }
  192. vec4 RenderSky(vec3 cameraOrigin, vec3 cameraDir, float curTime) {
  193. vec3 pos;
  194. float skyT1 = pow(smoothstep(0.0, 1.0, vUvs.y), 0.5);
  195. float skyT2 = pow(smoothstep(0.5, 1.0, vUvs.y), 1.0);
  196. vec3 c1 = col3(COLOUR_LIGHT_BLUE * 0.25);
  197. vec3 c2 = col3(COLOUR_BRIGHT_BLUE);
  198. vec3 c3 = col3(COLOUR_BRIGHT_BLUE * 1.25);
  199. vec3 sky = mix(c1, c2, skyT1);
  200. sky = mix(sky, c3, skyT2);
  201. float mu = remap(dot(cameraDir, CLOUD_LIGHT_DIR), -1.0, 1.0, 1.0, 0.0);
  202. float glow = RenderGlow(mu, 0.001, 0.5);
  203. sky += col3(glow, glow, 0.0);
  204. vec4 result = vec4(sky, 0.0);
  205. return result;
  206. }
  207. mat3 MakeCamera(vec3 ro, vec3 rd, vec3 ru) {
  208. vec3 z = normalize(rd - ro);
  209. vec3 cp = ru;
  210. vec3 x = normalize(cross(z, cp));
  211. vec3 y = cross(x, z);
  212. return mat3(x, y, z);
  213. }
  214. void main() {
  215. vec2 pixelCoords = (vUvs - 0.5) * resolution;
  216. float curTime = time * TIME_SPEED + TIME_OFFSET;
  217. CLOUD_SIZE = vec3(100.0);
  218. CLOUD_BOUNDS_MIN = CLOUD_OFFSET - CLOUD_SIZE;
  219. CLOUD_BOUNDS_MAX = CLOUD_OFFSET + CLOUD_SIZE;
  220. vec3 rayOrigin = vec3(200.0, 50.0, -150.0) * 0.75;
  221. vec3 rayLookAt = vec3(80.0, -10.0, 45.0) + CLOUD_LIGHT_DIR * 150.0;
  222. mat3 camera = MakeCamera(rayOrigin, rayLookAt, vec3(0.0, 1.0, 0.0));
  223. vec2 rayCoords = (2.0 * (gl_FragCoord.xy - 0.5) - resolution) / resolution.y;
  224. vec3 rayDir = normalize(vec3(rayCoords, 2.0));
  225. vec4 pixel = RenderSky(rayOrigin, camera * rayDir, curTime);
  226. ScatteringTransmittance scatterTransmittance = CloudMarch(
  227. pixelCoords, rayOrigin, normalize(camera * rayDir), curTime);
  228. vec3 colour;
  229. #ifdef USE_OKLAB
  230. colour = oklabToRGB(pixel.xyz) * scatterTransmittance.transmittance + oklabToRGB(scatterTransmittance.scattering) * CLOUD_EXPOSURE;
  231. colour = ACESToneMap(colour);
  232. colour = col3(colour);
  233. #else
  234. colour = pixel.xyz * scatterTransmittance.transmittance + scatterTransmittance.scattering * CLOUD_EXPOSURE;
  235. colour = ACESToneMap(colour);
  236. #endif
  237. gl_FragColor = vec4(colour, pixel.w);
  238. }