Skip to content

Commit f69cebe

Browse files
committed
use ray-sphere intersection for cloud shadows
Replace the fixed UV offset with a ray-sphere intersection that traces from each surface point toward the sun to find the exact cloud layer hit point. Increases shadow strength from 60% to 90%.
1 parent 3a8db5d commit f69cebe

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ const char *fs3D = "#version 330\n"
6262
" \n"
6363
" float cShadow = 1.0;\n"
6464
" if (showClouds == 1) {\n"
65-
" vec2 cUV = fragTexCoord + vec2(sunDir.x, -sunDir.y) * 0.005;\n"
65+
" float cloudR = earthRadius * (1.0 + 25.0 / 6371.0);\n"
66+
" float b = 2.0 * dot(fragPos, sunDir);\n"
67+
" float c = earthRadius * earthRadius - cloudR * cloudR;\n"
68+
" float t = (-b + sqrt(b * b - 4.0 * c)) * 0.5;\n"
69+
" vec3 cn = normalize(fragPos + t * sunDir);\n"
70+
" vec2 cUV = vec2(atan(-cn.z, cn.x) / 6.28318530718 + 0.5, cn.y);\n"
71+
" cUV.y = acos(clamp(cUV.y, -1.0, 1.0)) / 3.14159265359;\n"
6672
" cUV.x = fract(cUV.x + cloudUVOffset);\n"
6773
" float cAlpha = texture(texture2, cUV).a;\n"
68-
" cShadow = mix(1.0, 0.4, cAlpha * smoothstep(-0.1, 0.2, intensity));\n"
74+
" cShadow = mix(1.0, 0.1, cAlpha * smoothstep(-0.1, 0.2, intensity));\n"
6975
" }\n"
7076
" \n"
7177
" scatteredDay = (scatteredDay * cShadow) + specular;\n"

0 commit comments

Comments
 (0)