Skip to content

Commit 1f3f36c

Browse files
cjw6kclaude
andcommitted
fix: Make lighting follow actual Sun position
- Lighting component now uses Sun's actual position from celestial calculations instead of a fixed location - TidalEarth shader now includes sun-based diffuse lighting so the lit side of Earth correctly faces the Sun - Adds ambient light (0.15) so dark side isn't completely black Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ff72825 commit 1f3f36c

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

tidal-harmonics/src/components/canvas/Lighting.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { useScene } from '@/hooks/useScene';
1+
import { useCelestialPositions } from '@/hooks/useCelestialPositions';
22

33
export function Lighting() {
4-
const { scale } = useScene();
4+
const { sun } = useCelestialPositions();
55

66
return (
77
<>
88
<ambientLight intensity={0.1} />
99
<directionalLight
10-
position={[-scale.SUN_DISTANCE, 0, 0]}
10+
position={sun}
1111
intensity={1.5}
1212
castShadow
1313
/>

tidal-harmonics/src/components/canvas/TidalEarth.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import { TEXTURE_URLS, ROTATION_SPEEDS } from '@/lib/constants';
1111
const 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

4348
const 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

Comments
 (0)