Fix clouds3d: move starField out of main() (GLSL syntax)

GLSL does not allow nested function definitions; the starField()
helper must live at top-level alongside hash3/noise3/fbm3.
This commit is contained in:
Richard Zink committed 2026-10-07 19:04:50 +02:00
1 parent ee821dd724
commit 468e66058e
1 file changed
+15 -14
+15 -14
View File
@@ -98,6 +98,20 @@ var FRAG_SRC = [
'profile = max(profile, 0.0);', 'profile = max(profile, 0.0);',
'return smoothstep(baseThresh, baseThresh + 0.18, n) * profile;', 'return smoothstep(baseThresh, baseThresh + 0.18, n) * profile;',
'}', '}',
// Star field: project ray onto a far sphere, hash-grid for
// positions. Visible above the horizon at night with a small
// twinkle.
'float starField(vec3 r) {',
'vec3 sp = r * 220.0;',
'vec3 sf = floor(sp);',
'vec3 sfp = fract(sp) - 0.5;',
'float h = hash3(sf);',
'float starD = length(sfp);',
'float mask = step(0.985, h) * smoothstep(0.35, 0.0, starD);',
'float twinkle = 0.55 + 0.45 * sin(uTime * 3.0 + h * 47.0);',
'return mask * twinkle;',
'}',
'}',
'void main() {', 'void main() {',
'vec2 uv = (gl_FragCoord.xy / uResolution.xy) * 2.0 - 1.0;', 'vec2 uv = (gl_FragCoord.xy / uResolution.xy) * 2.0 - 1.0;',
'uv.x *= uResolution.x / uResolution.y;', 'uv.x *= uResolution.x / uResolution.y;',
@@ -115,20 +129,7 @@ var FRAG_SRC = [
// Sky gradient based on ray Y (background). // Sky gradient based on ray Y (background).
'float skyT = clamp(rd.y * 0.7 + 0.5, 0.0, 1.0);', 'float skyT = clamp(rd.y * 0.7 + 0.5, 0.0, 1.0);',
'vec3 col = mix(uBg, uSky, skyT);', 'vec3 col = mix(uBg, uSky, skyT);',
// Stars: project ray onto a far sphere, hash-grid for star // Stars: hash-grid sphere, above horizon, only at night.
// positions. Only visible above the horizon and at night,
// with a small twinkle so they don't look like a static
// dither pattern.
'float starField(vec3 r) {',
'vec3 sp = r * 220.0;',
'vec3 sf = floor(sp);',
'vec3 sfp = fract(sp) - 0.5;',
'float h = hash3(sf);',
'float starD = length(sfp);',
'float mask = step(0.985, h) * smoothstep(0.35, 0.0, starD);',
'float twinkle = 0.55 + 0.45 * sin(uTime * 3.0 + h * 47.0);',
'return mask * twinkle;',
'}',
'float stars = starField(rd) * smoothstep(0.05, 0.35, rd.y) * (1.0 - uDayProgress);', 'float stars = starField(rd) * smoothstep(0.05, 0.35, rd.y) * (1.0 - uDayProgress);',
'col = mix(col, uLight, clamp(stars, 0.0, 1.0) * 0.9);', 'col = mix(col, uLight, clamp(stars, 0.0, 1.0) * 0.9);',
// Density threshold: weather 'sunny' -> high (sparse clouds), // Density threshold: weather 'sunny' -> high (sparse clouds),