// Morning golden hour over the bay: the sun just up over the East Bay behind the Bay Bridge, // lighting the waterfront face of the city while the fog still sits on the western hills. A // gradient sky, the sun's light and shadows, haze, clouds, and an environment map made from // the same sky so glass and water reflect it. import * as THREE from "three"; import { FOG } from "../palette.js"; const SUN_AZIMUTH = (106 * Math.PI) / 180; // heading of the sun: east, a little south const SUN_ELEVATION = (19 * Math.PI) / 180; export const SUN_DIR = new THREE.Vector3(Math.sin(SUN_AZIMUTH) * Math.cos(SUN_ELEVATION), Math.sin(SUN_ELEVATION), -Math.cos(SUN_AZIMUTH) * Math.cos(SUN_ELEVATION)).normalize(); const SKY = { zenith: new THREE.Color(0x4a82c2), horizon: new THREE.Color(FOG), horizonSun: new THREE.Color(0xfbc58c), ground: new THREE.Color(0x7d828a), }; function skyMaterial() { return new THREE.ShaderMaterial({ side: THREE.BackSide, depthWrite: false, fog: false, uniforms: { uSun: { value: SUN_DIR }, uZenith: { value: SKY.zenith }, uHorizon: { value: SKY.horizon }, uHorizonSun: { value: SKY.horizonSun }, uGround: { value: SKY.ground }, uDisc: { value: 1 }, }, vertexShader: /* glsl */ ` varying vec3 vDir; void main() { vDir = position; vec4 p = projectionMatrix * modelViewMatrix * vec4(position, 1.0); gl_Position = p.xyww; }`, fragmentShader: /* glsl */ ` uniform vec3 uSun, uZenith, uHorizon, uHorizonSun, uGround; uniform float uDisc; varying vec3 vDir; void main() { vec3 d = normalize(vDir); float h = d.y; vec2 flatD = normalize(d.xz + 1e-5); vec2 flatS = normalize(uSun.xz); // pow() of a negative is NaN, and one NaN in the environment map blacks out every shadow float toward = clamp(dot(flatD, flatS) * 0.5 + 0.5, 0.0, 1.0); vec3 horizon = mix(uHorizon, uHorizonSun, pow(toward, 2.4)); vec3 col = mix(horizon, uZenith, pow(clamp(h * 1.7, 0.0, 1.0), 0.5)); float s = max(dot(d, uSun), 0.0); col += vec3(1.0, 0.78, 0.5) * pow(s, 6.0) * 0.45; col += vec3(1.0, 0.86, 0.62) * pow(s, 64.0) * 0.8; col += vec3(1.0, 0.93, 0.8) * smoothstep(0.9994, 0.99975, s) * 6.0 * uDisc; if (h < 0.0) col = mix(horizon, uGround, clamp(-h * 5.0, 0.0, 1.0)); gl_FragColor = vec4(col, 1.0); #include #include }`, }); } export function buildSky(scene, renderer, { shadowSize = 2048, castShadow = true } = {}) { const dome = new THREE.Mesh(new THREE.SphereGeometry(1, 48, 24), skyMaterial()); dome.scale.setScalar(30000); dome.frustumCulled = false; dome.renderOrder = -10; scene.add(dome); // The environment: the same sky, softened, lights everything that is not in direct sun. const envScene = new THREE.Scene(); const envDome = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), skyMaterial()); envDome.material.uniforms.uDisc.value = 0.25; envDome.scale.setScalar(100); envScene.add(envDome); const pmrem = new THREE.PMREMGenerator(renderer); const env = pmrem.fromScene(envScene, 0.02).texture; scene.environment = env; scene.environmentIntensity = 0.42; pmrem.dispose(); scene.fog = new THREE.FogExp2(FOG, 0.00011); const sun = new THREE.DirectionalLight(0xffc488, 4.2); sun.castShadow = castShadow; sun.shadow.mapSize.set(shadowSize, shadowSize); const c = sun.shadow.camera; c.left = -110; c.right = 110; c.top = 110; c.bottom = -110; c.near = 10; c.far = 900; sun.shadow.bias = -0.0004; sun.shadow.normalBias = 0.6; sun.shadow.radius = 2; scene.add(sun); scene.add(sun.target); const hemi = new THREE.HemisphereLight(0xb9d2ee, 0x6a6052, 0.8); scene.add(hemi); // Clouds: a few flat-bottomed puffs over the west and north, where the low sun lights them // warm against the blue. const clouds = new THREE.Group(); const cloudMat = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1, emissive: 0x7d8da8, emissiveIntensity: 0.38, fog: true }); const puff = new THREE.IcosahedronGeometry(1, 3); const spots = [ [-5200, 1100, -2600, 1.2], [-6800, 1300, 900, 1.4], [-4400, 1200, 3400, 1.1], [-2600, 1400, -6800, 1.3], [-7800, 1500, -5200, 1.5], [1200, 1500, -8200, 1.2], [-3000, 1600, 7600, 1.3], [4200, 1700, -9000, 1.1], [-9600, 1600, 2400, 1.6], [2400, 1800, 9200, 1.0], ]; let seed = 7; const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647); for (const [x, y, z, s] of spots) { const g = new THREE.Group(); const n = 7 + Math.floor(rnd() * 5); for (let k = 0; k < n; k++) { const m = new THREE.Mesh(puff, cloudMat); const mid = 1 - Math.abs(k / (n - 1) - 0.5) * 1.2; const r = (70 + rnd() * 90) * s * (0.6 + mid * 0.6); m.scale.set(r * (1.2 + rnd() * 0.5), r * (0.5 + rnd() * 0.25) * (0.7 + mid * 0.5), r * (1 + rnd() * 0.4)); m.position.set((k - n / 2) * 105 * s + rnd() * 50, r * 0.25 + rnd() * 30 * s, (rnd() - 0.5) * 160 * s); g.add(m); } g.position.set(x, y, z); g.rotation.y = rnd() * Math.PI; clouds.add(g); } scene.add(clouds); const texel = (2 * 110) / shadowSize; return { dome, sun, env, /** Keeps the sun's shadow box centered on (x, y, z), snapped to texels so edges hold still. */ follow(x, y, z) { const right = new THREE.Vector3(0, 1, 0).cross(SUN_DIR).normalize(); const up = SUN_DIR.clone().cross(right).normalize(); const p = new THREE.Vector3(x, y, z); const a = Math.round(p.dot(right) / texel) * texel; const b = Math.round(p.dot(up) / texel) * texel; const d = p.dot(SUN_DIR); const snapped = right.multiplyScalar(a).add(up.multiplyScalar(b)).add(SUN_DIR.clone().multiplyScalar(d)); sun.target.position.copy(snapped); sun.position.copy(snapped).addScaledVector(SUN_DIR, 450); sun.target.updateMatrixWorld(); }, }; }