@@ -11,11 +11,13 @@ import { TEXTURE_URLS, ROTATION_SPEEDS } from '@/lib/constants';
1111const tidalVertexShader = `
1212 uniform vec3 moonDirection;
1313 uniform vec3 sunDirection;
14+ uniform vec3 sunLightDir;
1415 uniform float tidalAmplitude;
1516
1617 varying float vTidalDisplacement;
1718 varying vec2 vUv;
1819 varying vec3 vNormal;
20+ varying vec3 vWorldNormal;
1921
2022 void main() {
2123 vUv = uv;
@@ -36,25 +38,37 @@ const tidalVertexShader = `
3638 // Apply displacement along normal
3739 vec3 newPosition = position * (1.0 + displacement);
3840
41+ // Transform normal to world space for lighting
42+ vWorldNormal = normalize((modelMatrix * vec4(normal, 0.0)).xyz);
43+
3944 gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
4045 }
4146` ;
4247
4348const tidalFragmentShader = `
4449 uniform sampler2D map;
4550 uniform float tidalIntensity;
51+ uniform vec3 sunLightDir;
4652
4753 varying float vTidalDisplacement;
4854 varying vec2 vUv;
4955 varying vec3 vNormal;
56+ varying vec3 vWorldNormal;
5057
5158 void main() {
5259 vec3 baseColor = texture2D(map, vUv).rgb;
5360
61+ // Sun-based lighting (diffuse)
62+ float diffuse = max(dot(normalize(vWorldNormal), normalize(sunLightDir)), 0.0);
63+ // Add ambient so dark side isn't completely black
64+ float lighting = 0.15 + diffuse * 0.85;
65+
66+ vec3 litColor = baseColor * lighting;
67+
5468 // Color tint on bulges - more visible for pedagogical effect
5569 float bulgeIntensity = abs(vTidalDisplacement) * tidalIntensity;
5670 // Amplify the tint so bulge areas show subtle blue highlight
57- vec3 bulgeColor = mix(baseColor , vec3(0.3, 0.5, 1.0), clamp(bulgeIntensity * 3.0, 0.0, 0.4));
71+ vec3 bulgeColor = mix(litColor , vec3(0.3, 0.5, 1.0), clamp(bulgeIntensity * 3.0, 0.0, 0.4));
5872
5973 gl_FragColor = vec4(bulgeColor, 1.0);
6074 }
@@ -65,6 +79,7 @@ interface TidalUniforms {
6579 map : { value : Texture } ;
6680 moonDirection : { value : Vector3 } ;
6781 sunDirection : { value : Vector3 } ;
82+ sunLightDir : { value : Vector3 } ;
6883 tidalAmplitude : { value : number } ;
6984 tidalIntensity : { value : number } ;
7085}
@@ -85,6 +100,7 @@ export function TidalEarth() {
85100 map : { value : texture } ,
86101 moonDirection : { value : new Vector3 ( ) } ,
87102 sunDirection : { value : new Vector3 ( ) } ,
103+ sunLightDir : { value : new Vector3 ( ) } ,
88104 tidalAmplitude : { value : 0.0 } ,
89105 tidalIntensity : { value : 1.0 } ,
90106 } ) ,
@@ -114,6 +130,8 @@ export function TidalEarth() {
114130
115131 uniforms . moonDirection . value . copy ( moonDir ) ;
116132 uniforms . sunDirection . value . copy ( sunDir ) ;
133+ // Sun light direction in world space (not transformed) for lighting
134+ uniforms . sunLightDir . value . set ( sun [ 0 ] , sun [ 1 ] , sun [ 2 ] ) . normalize ( ) ;
117135
118136 // Pedagogical tidal amplitude for visible effect
119137 // Physical amplitude (0.53m / 6,371km = 8.3e-8) is invisible even at 50,000×
0 commit comments