Home
API reference

cloudLayerFragmentShader

strata-game-library/shaders

strata-game-library/shaders


strata-game-library/shaders / cloudLayerFragmentShader

Variable: cloudLayerFragmentShader

const cloudLayerFragmentShader: "\n #define PI 3.14159265359\n \n uniform float uTime;\n uniform float uCoverage;\n uniform float uDensity;\n uniform float uAltitude;\n uniform vec3 uCloudColor;\n uniform vec3 uShadowColor;\n uniform vec2 uWindDirection;\n uniform float uWindSpeed;\n uniform float uSunIntensity;\n uniform float uSunAngle;\n uniform vec3 uSunColor;\n uniform float uScale;\n \n varying vec2 vUv;\n varying vec3 vWorldPosition;\n \n float hash(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);\n }\n \n float noise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = fract(p);\n f = f * f * (3.0 - 2.0 * f);\n \n float a = hash(i);\n float b = hash(i + vec2(1.0, 0.0));\n float c = hash(i + vec2(0.0, 1.0));\n float d = hash(i + vec2(1.0, 1.0));\n \n return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);\n }\n \n float fbm(vec2 p, int octaves) {\n float value = 0.0;\n float amplitude = 0.5;\n float frequency = 1.0;\n float maxValue = 0.0;\n \n #define MAX_FBM_OCTAVES 8\n for (int i = 0; i < MAX_FBM_OCTAVES; i++) {\n if (i >= octaves) break;\n value += amplitude * noise(p * frequency);\n maxValue += amplitude;\n amplitude *= 0.5;\n frequency *= 2.0;\n }\n \n return maxValue > 0.0 ? value / maxValue : 0.0;\n }\n \n void main() {\n vec2 windOffset = uWindDirection * uWindSpeed * uTime;\n vec2 samplePos = (vUv + windOffset) * uScale;\n \n float cloud = fbm(samplePos, 6);\n float detail = fbm(samplePos * 3.0 + 100.0, 4) * 0.5;\n cloud = cloud + detail * 0.3;\n \n float threshold = 1.0 - uCoverage;\n cloud = smoothstep(threshold - 0.1, threshold + 0.2, cloud);\n cloud *= uDensity;\n \n float heightGradient = fbm(samplePos * 0.5, 3);\n float cloudOpacity = cloud * (0.5 + heightGradient * 0.5);\n \n float sunHeight = sin(uSunAngle * PI / 180.0);\n vec3 lightDir = normalize(vec3(0.5, sunHeight, 0.5));\n float shadowSample = fbm(samplePos + lightDir.xz * 0.1, 4);\n float shadow = smoothstep(0.3, 0.7, shadowSample);\n \n vec3 baseColor = mix(uShadowColor, uCloudColor, shadow);\n vec3 sunTint = uSunColor * uSunIntensity * max(0.0, sunHeight);\n vec3 finalColor = baseColor + sunTint * 0.2 * cloud;\n \n float edgeFade = smoothstep(0.0, 0.1, vUv.x) * smoothstep(1.0, 0.9, vUv.x);\n edgeFade *= smoothstep(0.0, 0.1, vUv.y) * smoothstep(1.0, 0.9, vUv.y);\n cloudOpacity *= edgeFade;\n \n gl_FragColor = vec4(finalColor, cloudOpacity);\n }\n"

Defined in: clouds.ts:21