1import * as THREE from '../vendor/three.module.js'; 2import { part } from './props.js'; 3 4const KEYS = [ 5 { u: 0.0, sun: '#ffb27a', sunI: 1.5, sky: '#e0c4a8', ground: '#4a3626', hemiI: 0.9, haze: '#3a2c24', lamp: 0.4 }, 6 { u: 0.12, sun: '#ffe2bc', sunI: 2.3, sky: '#dfe2e6', ground: '#4a3a2a', hemiI: 1.25, haze: '#2e2a26', lamp: 0 }, 7 { u: 0.45, sun: '#fff0d8', sunI: 2.6, sky: '#e4ebf2', ground: '#4c3c2c', hemiI: 1.35, haze: '#2e2a26', lamp: 0 }, 8 { u: 0.62, sun: '#ff9858', sunI: 1.6, sky: '#d8a88c', ground: '#3e2c22', hemiI: 0.95, haze: '#34241e', lamp: 0.5 }, 9 { u: 0.72, sun: '#9fb2ff', sunI: 0.55, sky: '#56669a', ground: '#16161e', hemiI: 0.55, haze: '#0e1018', lamp: 1 }, 10 { u: 0.94, sun: '#9fb2ff', sunI: 0.5, sky: '#4a5a8a', ground: '#141420', hemiI: 0.5, haze: '#0c0e16', lamp: 1 }, 11 { u: 1.0, sun: '#ffb27a', sunI: 1.5, sky: '#e0c4a8', ground: '#4a3626', hemiI: 0.9, haze: '#3a2c24', lamp: 0.4 }, 12]; 13const NIGHT_AT = 0.72; 14const EASE = 1.6; 15const CAMP_SECONDS = 4.5; 16 17const c1 = new THREE.Color(); 18const c2 = new THREE.Color(); 19const mix = (a, b, k) => c1.set(a).lerp(c2.set(b), k); 20 21function sample(u) { 22 const i = KEYS.findIndex((k) => k.u > u); 23 const a = KEYS[i - 1]; 24 const b = KEYS[i]; 25 const k = (u - a.u) / (b.u - a.u); 26 const lerp = (x, y) => x + (y - x) * k; 27 return { a, b, k, sunI: lerp(a.sunI, b.sunI), hemiI: lerp(a.hemiI, b.hemiI), lamp: lerp(a.lamp, b.lamp) }; 28} 29 30function buildFire() { 31 const g = new THREE.Group(); 32 for (let i = 0; i < 6; i++) { 33 const a = (i / 6) * Math.PI * 2; 34 part(g, 'sph', '#6e665c', [Math.cos(a) * 0.2, 0.04, Math.sin(a) * 0.2], [0.1, 0.07, 0.1]); 35 } 36 [0, 1.1, 2.2].forEach((a) => { 37 part(g, 'cyl', '#4a3320', [0, 0.06, 0], [0.04, 0.32, 0.04]).rotation.set(Math.PI / 2, a, 0); 38 }); 39 const flame = part(g, 'cone', '#ffb040', [0, 0.18, 0], [0.16, 0.3, 0.16], { emissive: '#ff7a1a', emissiveIntensity: 2 }); 40 flame.castShadow = false; 41 [[-0.42, 0.25, 0.4], [0.45, -0.3, -0.6]].forEach(([x, z, r]) => { 42 part(g, 'cone', '#8a6a3e', [x, 0.2, z], [0.42, 0.4, 0.42]).rotation.y = r; 43 }); 44 const light = new THREE.PointLight('#ff9a40', 0, 6, 1.6); 45 light.position.y = 0.6; 46 g.add(light); 47 return { group: g, flame, light }; 48} 49 50export function createSky(scene) { 51 const hemi = new THREE.HemisphereLight('#dfe6f0', '#4a3a2a', 1.3); 52 scene.add(hemi); 53 const sun = new THREE.DirectionalLight('#ffe4bd', 2.4); 54 sun.castShadow = true; 55 sun.shadow.mapSize.set(2048, 2048); 56 Object.assign(sun.shadow.camera, { left: -22, right: 22, top: 18, bottom: -18, near: 1, far: 90 }); 57 sun.shadow.bias = -0.0006; 58 scene.add(sun, sun.target); 59 const lantern = new THREE.PointLight('#ffb468', 0, 7, 1.4); 60 scene.add(lantern); 61 const fire = buildFire(); 62 fire.group.visible = false; 63 scene.add(fire.group); 64 scene.background = new THREE.Color(); 65 scene.fog = new THREE.Fog('#000', 26, 62); 66 67 let u = 0; 68 let goal = 0; 69 let camping = null; 70 let t = 0; 71 let dark = 0; 72 73 const clockU = (c) => c.time / (c.dayLength + c.nightLength); 74 75 function setClock(c, instant = false) { 76 goal = clockU(c); 77 if (instant) u = goal; 78 } 79 80 function apply(focus) { 81 const s = sample(u); 82 sun.color.copy(mix(s.a.sun, s.b.sun, s.k)); 83 sun.intensity = s.sunI; 84 hemi.color.copy(mix(s.a.sky, s.b.sky, s.k)); 85 hemi.groundColor.copy(mix(s.a.ground, s.b.ground, s.k)); 86 hemi.intensity = s.hemiI; 87 const haze = mix(s.a.haze, s.b.haze, s.k); 88 scene.background.copy(haze); 89 scene.fog.color.copy(haze); 90 const day = Math.min(1, u / NIGHT_AT); 91 const arc = u < NIGHT_AT ? Math.PI * (0.1 + 0.8 * day) : Math.PI * 0.35; 92 sun.position.set(focus.x - Math.cos(arc) * 30, focus.y + 14 + Math.sin(arc) * 18, focus.z + 12); 93 sun.target.position.copy(focus); 94 lantern.position.set(focus.x, focus.y + 1.6, focus.z + 0.2); 95 lantern.intensity = camping ? 0 : s.lamp * 4; 96 dark = Math.max(0, (s.lamp - 0.3) / 0.7); 97 } 98 99 function update(dt, focus) { 100 t += dt; 101 if (camping) { 102 camping.t += dt; 103 const k = Math.min(1, camping.t / CAMP_SECONDS); 104 u = camping.from + (1 - camping.from) * k; 105 fire.light.intensity = (k < 0.85 ? 10 : 10 * (1 - (k - 0.85) / 0.15)) * (0.85 + 0.15 * Math.sin(t * 23) * Math.sin(t * 7)); 106 fire.flame.scale.set(0.16, 0.3 + 0.05 * Math.sin(t * 17), 0.16); 107 if (k >= 1) { 108 u = 0; 109 goal = 0; 110 fire.group.visible = false; 111 camping.done(); 112 camping = null; 113 } 114 } else { 115 let d = goal - u; 116 if (d < -0.5) d += 1; 117 u = (u + d * Math.min(1, dt * EASE) + 1) % 1; 118 } 119 apply(focus); 120 } 121 122 function camp(at) { 123 fire.group.position.copy(at); 124 fire.group.visible = true; 125 return new Promise((done) => (camping = { t: 0, from: u, done })); 126 } 127 128 return { setClock, update, camp, dark: () => dark, night: () => (camping ? Math.min(1, camping.t / CAMP_SECONDS) : null) }; 129}