xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / life.js 127 lines5.0 kB Raw
1import * as THREE from '../vendor/three.module.js';
2
3const PUFFS = 5;
4const PUFF_LIFE = 3.4;
5const MAX_SOURCES = 24;
6const FLIES = 48;
7const FLY_RANGE = 4.5;
8const FLOCKS = 2;
9const BIRDS = 3;
10
11export const wind = { value: 0 };
12
13export function sway(material, amount) {
14  material.onBeforeCompile = (sh) => {
15    sh.uniforms.uWind = wind;
16    sh.vertexShader = `uniform float uWind;\n${sh.vertexShader}`.replace('#include <begin_vertex>', `#include <begin_vertex>
17      float phase = instanceMatrix[3].x * 0.7 + instanceMatrix[3].z * 0.5;
18      float bend = max(position.y, 0.0) * ${amount.toFixed(3)};
19      transformed.x += sin(uWind * 1.7 + phase) * bend;
20      transformed.z += cos(uWind * 1.3 + phase * 1.3) * bend * 0.6;`);
21  };
22  return material;
23}
24
25const m4 = new THREE.Matrix4();
26const q = new THREE.Quaternion();
27const v = new THREE.Vector3();
28const s = new THREE.Vector3();
29const qz = new THREE.Quaternion();
30const UP = new THREE.Vector3(0, 1, 0);
31const FWD = new THREE.Vector3(0, 0, 1);
32const FLIP = new THREE.Quaternion().setFromAxisAngle(UP, Math.PI);
33
34function wing() {
35  const g = new THREE.BufferGeometry();
36  g.setAttribute('position', new THREE.Float32BufferAttribute([0, 0, 0.06, 0, 0, -0.06, 0.22, 0, 0], 3));
37  g.computeVertexNormals();
38  return g;
39}
40
41export function createLife(group) {
42  const center = new THREE.Vector3();
43  let homed = false;
44  const smoke = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(0.09, 0), new THREE.MeshStandardMaterial({ color: '#a8a29a', roughness: 1, flatShading: true, transparent: true, opacity: 0.55, depthWrite: false }), MAX_SOURCES * PUFFS);
45  smoke.frustumCulled = false;
46  smoke.count = 0;
47  smoke.raycast = () => {};
48  group.add(smoke);
49  let sources = [];
50
51  const flyPos = new Float32Array(FLIES * 3);
52  const flySeed = Array.from({ length: FLIES }, () => [Math.random() * 100, Math.random() * 2 - 1, Math.random() * 2 - 1, 0.2 + Math.random() * 0.8]);
53  const flyGeo = new THREE.BufferGeometry();
54  flyGeo.setAttribute('position', new THREE.BufferAttribute(flyPos, 3));
55  const flies = new THREE.Points(flyGeo, new THREE.PointsMaterial({ color: '#e6ff9c', size: 0.09, transparent: true, opacity: 0, blending: THREE.AdditiveBlending, depthWrite: false }));
56  flies.frustumCulled = false;
57  flies.raycast = () => {};
58  group.add(flies);
59
60  const birds = new THREE.InstancedMesh(wing(), new THREE.MeshStandardMaterial({ color: '#2b2621', side: THREE.DoubleSide, roughness: 1 }), FLOCKS * BIRDS * 2);
61  birds.frustumCulled = false;
62  birds.castShadow = true;
63  birds.raycast = () => {};
64  group.add(birds);
65  const flocks = Array.from({ length: FLOCKS }, (_, i) => ({ r: 3.5 + i * 2.2, h: 2.8 + i * 0.6, speed: (i ? -1 : 1) * (0.22 + i * 0.05), phase: i * 2.4, cx: (i ? -1 : 1) * 2.5, cz: (i ? 2 : -1.5) }));
66  const flags = [];
67
68  function setSources(list) {
69    sources = list.slice(0, MAX_SOURCES);
70    smoke.count = sources.length * PUFFS;
71  }
72
73  function track(obj) {
74    obj.traverse((o) => o.userData.wave && flags.push({ o, phase: Math.random() * 6 }));
75  }
76
77  function update(t, dark, focus) {
78    wind.value = t;
79    sources.forEach((src, i) => {
80      for (let k = 0; k < PUFFS; k++) {
81        const life = ((t + k * (PUFF_LIFE / PUFFS) + i * 0.37) % PUFF_LIFE) / PUFF_LIFE;
82        const size = (src.size || 1) * (life < 0.7 ? 0.6 + life * 1.4 : 1.58 * (1 - (life - 0.7) / 0.3));
83        v.set(src.p.x + life * 0.35 + Math.sin(t + k) * 0.05, src.p.y + life * 1.3, src.p.z + Math.cos(t * 0.7 + k) * 0.05);
84        smoke.setMatrixAt(i * PUFFS + k, m4.compose(v, q.identity(), s.setScalar(Math.max(0.01, size))));
85      }
86    });
87    smoke.instanceMatrix.needsUpdate = true;
88
89    flies.visible = dark > 0.05;
90    flies.material.opacity = dark;
91    if (flies.visible) {
92      flySeed.forEach(([seed, ox, oz, hy], i) => {
93        flyPos[i * 3] = focus.x + ox * FLY_RANGE + Math.sin(t * 0.4 + seed) * 0.6;
94        flyPos[i * 3 + 1] = focus.y + hy + Math.sin(t * 1.3 + seed * 2) * 0.15;
95        flyPos[i * 3 + 2] = focus.z + oz * FLY_RANGE + Math.cos(t * 0.33 + seed) * 0.6;
96      });
97      flyGeo.attributes.position.needsUpdate = true;
98    }
99
100    if (!homed) center.copy(focus);
101    homed = true;
102    center.lerp(focus, 0.004);
103    birds.visible = dark < 0.6;
104    if (birds.visible) {
105      let n = 0;
106      for (const f of flocks) {
107        const a0 = f.phase + t * f.speed;
108        for (let b = 0; b < BIRDS; b++) {
109          const a = a0 - b * 0.12 * Math.sign(f.speed);
110          const r = f.r + (b % 2 ? 0.35 : -0.35) * Math.min(b, 1);
111          v.set(center.x + f.cx + Math.cos(a) * r, center.y + f.h + Math.sin(t * 0.8 + b) * 0.1, center.z + f.cz + Math.sin(a) * r);
112          const flap = Math.sin(t * 9 + b * 1.7) * 0.6;
113          for (const side of [1, -1]) {
114            q.setFromAxisAngle(UP, -a).multiply(qz.setFromAxisAngle(FWD, side * flap));
115            if (side < 0) q.multiply(FLIP);
116            birds.setMatrixAt(n++, m4.compose(v, q, s.setScalar(1)));
117          }
118        }
119      }
120      birds.instanceMatrix.needsUpdate = true;
121    }
122
123    for (const f of flags) f.o.rotation.y = Math.sin(t * 2.3 + f.phase) * 0.3;
124  }
125
126  return { setSources, track, update };
127}