xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / flora.js 94 lines4.4 kB Raw
1import * as THREE from '../vendor/three.module.js';
2
3function merge(...parts) {
4  const geometries = parts.map(([g, x = 0, y = 0, z = 0]) => (g.index ? g.toNonIndexed() : g.clone()).translate(x, y, z));
5  const out = new THREE.BufferGeometry();
6  for (const name of ['position', 'normal']) {
7    const data = new Float32Array(geometries.reduce((n, g) => n + g.attributes[name].array.length, 0));
8    let at = 0;
9    for (const g of geometries) {
10      data.set(g.attributes[name].array, at);
11      at += g.attributes[name].array.length;
12    }
13    out.setAttribute(name, new THREE.BufferAttribute(data, 3));
14  }
15  return out;
16}
17
18const hash = (x, y, z) => {
19  const v = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453;
20  return v - Math.floor(v);
21};
22
23function rough(geometry, amount) {
24  const p = geometry.attributes.position;
25  for (let i = 0; i < p.count; i++) {
26    const [x, y, z] = [p.getX(i), p.getY(i), p.getZ(i)];
27    const k = (n) => (hash(Math.round(x * 50), Math.round(y * 50), Math.round(z * 50) + n) - 0.5) * 2 * amount;
28    p.setXYZ(i, x + k(0), y + k(1) * 0.4, z + k(2));
29  }
30  geometry.computeVertexNormals();
31  return geometry;
32}
33
34const tier = (r, h, base, sides = 6, rot = 0) => [new THREE.ConeGeometry(r, h, sides, 1, true).rotateY(rot), 0, base + h / 2, 0];
35const ball = (r, x, y, z, detail = 0, sx = 1, sy = 1) => [new THREE.IcosahedronGeometry(r, detail).scale(sx, sy, sx), x, y, z];
36const prism = (w, h, d, x, y, z, rot = 0) => [new THREE.BoxGeometry(w, h, d).translate(0, h / 2, 0).rotateY(rot), x, y, z];
37
38const PINE_TIERS = [[0.36, 0.44, 0.2], [0.3, 0.42, 0.44], [0.23, 0.4, 0.68], [0.15, 0.38, 0.92]];
39const pine = (scale = 1) => merge(...PINE_TIERS.map(([r, h, b], k) => tier(r * scale, h, b, 6, k * 0.6)));
40
41export const CANOPY = {
42  frostpine: pine(),
43  pine: pine(0.95),
44  round: merge(ball(0.4, 0, 0.8, 0, 0, 1, 0.92), ball(0.28, 0.26, 0.54, 0.12), ball(0.26, -0.22, 0.6, -0.16), ball(0.2, 0.04, 1.12, 0.1)),
45  bog: merge(ball(0.42, 0, 0.5, 0, 1, 1.25, 0.65), ball(0.3, 0.32, 0.38, 0.12, 0, 1.2, 0.7), ball(0.25, -0.3, 0.42, -0.2, 0, 1.2, 0.7)),
46  dead: merge(
47    [new THREE.OctahedronGeometry(0.3).scale(1.3, 0.7, 1.3), 0.02, 1.0, 0],
48    [new THREE.OctahedronGeometry(0.22).scale(1.3, 0.7, 1.3), 0.32, 0.72, 0.05],
49    [new THREE.OctahedronGeometry(0.2).scale(1.3, 0.7, 1.3), -0.28, 0.62, -0.1],
50    [new THREE.OctahedronGeometry(0.14).scale(1.2, 0.7, 1.2), 0.08, 1.28, 0.1],
51  ),
52};
53export const SNOW_TIPS = merge(...PINE_TIERS.map(([r, h, b], k) => [new THREE.ConeGeometry(r * 0.6, h * 0.55, 6, 1, true).rotateY(k * 0.6), 0, b + h * 0.72, 0]));
54export const TRUNK = merge([new THREE.CylinderGeometry(0.05, 0.09, 1, 4, 1, true), 0, 0.5, 0]);
55export const TREE_TRUNK = { frostpine: 0.28, pine: 0.3, round: 0.5, bog: 0.3, dead: 0.8 };
56
57const CLUSTER = [
58  [0.52, 1.0, 0, 0, 0.1, 6],
59  [0.4, 0.66, -0.48, 0.24, 0.5, 5],
60  [0.36, 0.55, 0.44, -0.3, 0.9, 6],
61  [0.28, 0.4, 0.16, 0.56, 1.3, 5],
62  [0.26, 0.34, -0.3, -0.5, 0.2, 5],
63];
64const peaks = CLUSTER.map(([r0, h, x, z, rot, sides]) => {
65  const r = r0 * 1.2;
66  const body = rough(new THREE.ConeGeometry(r, h, sides, 1, true).rotateY(rot).translate(x, h / 2, z), r * 0.12);
67  const cap = body.clone();
68  const p = cap.attributes.position;
69  for (let i = 0; i < p.count; i++) p.setXYZ(i, x + (p.getX(i) - x) * 0.5, h + (p.getY(i) - h) * 0.46, z + (p.getZ(i) - z) * 0.5);
70  cap.computeVertexNormals();
71  return { body, cap };
72});
73export const MOUNTAIN = merge(...peaks.map((p) => [p.body]));
74export const MOUNTAIN_CAP = merge(...peaks.map((p) => [p.cap]));
75
76export const ROCKS = {
77  boulder: merge(
78    [rough(new THREE.IcosahedronGeometry(0.38, 0).scale(1.1, 0.75, 1), 0.05), 0, 0.25, 0],
79    [rough(new THREE.IcosahedronGeometry(0.22, 0).scale(1.1, 0.8, 1), 0.04), 0.42, 0.14, 0.15],
80    [rough(new THREE.IcosahedronGeometry(0.16, 0), 0.02), -0.3, 0.09, 0.34],
81  ),
82  shard: merge(
83    [new THREE.OctahedronGeometry(0.3).scale(0.5, 1.5, 0.5).rotateZ(0.12), 0, 0.42, 0],
84    [new THREE.OctahedronGeometry(0.22).scale(0.5, 1.4, 0.5).rotateZ(-0.3), 0.3, 0.28, 0.1],
85    [new THREE.OctahedronGeometry(0.16).scale(0.5, 1.3, 0.5).rotateX(0.4), -0.22, 0.2, 0.2],
86  ),
87  block: merge(prism(0.55, 0.85, 0.5, 0, 0, 0, 0.3), prism(0.4, 0.5, 0.38, 0.45, 0, 0.18, 0.9), prism(0.3, 0.3, 0.32, -0.38, 0, 0.28, 0.1)),
88};
89
90export const TUFT = merge(
91  [new THREE.ConeGeometry(0.06, 0.24, 3, 1, true), 0, 0.12, 0],
92  [new THREE.ConeGeometry(0.05, 0.18, 3, 1, true).rotateY(1), 0.08, 0.09, 0.03],
93  [new THREE.ConeGeometry(0.05, 0.16, 3, 1, true).rotateY(2), -0.07, 0.08, -0.04],
94);