@casmo/tree.js 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -16
- package/dist/apply-Cio2t7CZ.js +285 -0
- package/dist/apply-Cio2t7CZ.js.map +1 -0
- package/dist/bake/index.d.ts +44 -5
- package/dist/bake/index.d.ts.map +1 -1
- package/dist/definition/crown-shape.d.ts +9 -0
- package/dist/definition/crown-shape.d.ts.map +1 -0
- package/dist/definition/defaults.d.ts +15 -1
- package/dist/definition/defaults.d.ts.map +1 -1
- package/dist/definition/index.d.ts +4 -3
- package/dist/definition/index.d.ts.map +1 -1
- package/dist/definition/migrations.d.ts +0 -8
- package/dist/definition/migrations.d.ts.map +1 -1
- package/dist/definition/schema.d.ts.map +1 -1
- package/dist/definition/types.d.ts +181 -6
- package/dist/definition/types.d.ts.map +1 -1
- package/dist/effects/mask.d.ts +3 -3
- package/dist/effects/mask.d.ts.map +1 -1
- package/dist/effects.js +3 -3
- package/dist/effects.js.map +1 -1
- package/dist/forest/Forest.d.ts +37 -2
- package/dist/forest/Forest.d.ts.map +1 -1
- package/dist/forest/nodes.d.ts +6 -1
- package/dist/forest/nodes.d.ts.map +1 -1
- package/dist/{forest-OX9WvAwh.js → forest-ChEaWJrf.js} +1763 -684
- package/dist/forest-ChEaWJrf.js.map +1 -0
- package/dist/generator/branches.d.ts +2 -2
- package/dist/generator/branches.d.ts.map +1 -1
- package/dist/generator/crown.d.ts +28 -0
- package/dist/generator/crown.d.ts.map +1 -0
- package/dist/generator/fruit.d.ts +28 -0
- package/dist/generator/fruit.d.ts.map +1 -0
- package/dist/generator/growth.d.ts +1 -1
- package/dist/generator/growth.d.ts.map +1 -1
- package/dist/generator/index.d.ts +5 -4
- package/dist/generator/index.d.ts.map +1 -1
- package/dist/generator/leaf-shapes.d.ts +33 -1
- package/dist/generator/leaf-shapes.d.ts.map +1 -1
- package/dist/generator/mesh.d.ts +10 -2
- package/dist/generator/mesh.d.ts.map +1 -1
- package/dist/generator/occlusion.d.ts +8 -0
- package/dist/generator/occlusion.d.ts.map +1 -0
- package/dist/generator/roots.d.ts +16 -0
- package/dist/generator/roots.d.ts.map +1 -0
- package/dist/generator/trunk.d.ts +16 -2
- package/dist/generator/trunk.d.ts.map +1 -1
- package/dist/generator/types.d.ts +61 -6
- package/dist/generator/types.d.ts.map +1 -1
- package/dist/growth/index.d.ts +1 -1
- package/dist/growth/index.d.ts.map +1 -1
- package/dist/growth/tsl.d.ts +2 -0
- package/dist/growth/tsl.d.ts.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +274 -24
- package/dist/index.js.map +1 -1
- package/dist/materials/crown-nodes.d.ts +20 -0
- package/dist/materials/crown-nodes.d.ts.map +1 -0
- package/dist/materials/crown-normals.d.ts +17 -0
- package/dist/materials/crown-normals.d.ts.map +1 -0
- package/dist/materials/ground-tint.d.ts +13 -0
- package/dist/materials/ground-tint.d.ts.map +1 -0
- package/dist/materials/index.d.ts +13 -1
- package/dist/materials/index.d.ts.map +1 -1
- package/dist/materials/leaf-translucency-model.d.ts +14 -0
- package/dist/materials/leaf-translucency-model.d.ts.map +1 -0
- package/dist/materials/leaf-translucency.d.ts +73 -0
- package/dist/materials/leaf-translucency.d.ts.map +1 -0
- package/dist/materials/material-features.d.ts +43 -0
- package/dist/materials/material-features.d.ts.map +1 -0
- package/dist/materials/occlusion.d.ts +10 -0
- package/dist/materials/occlusion.d.ts.map +1 -0
- package/dist/materials/toon.d.ts +18 -1
- package/dist/materials/toon.d.ts.map +1 -1
- package/dist/names-BE9he6CS.js +713 -0
- package/dist/names-BE9he6CS.js.map +1 -0
- package/dist/react/Tree.d.ts +5 -2
- package/dist/react/Tree.d.ts.map +1 -1
- package/dist/react.js +10 -6
- package/dist/react.js.map +1 -1
- package/dist/season/nodes.d.ts +16 -0
- package/dist/season/nodes.d.ts.map +1 -1
- package/dist/season/profile.d.ts +26 -0
- package/dist/season/profile.d.ts.map +1 -1
- package/dist/species/almond.js +223 -0
- package/dist/species/almond.js.map +1 -0
- package/dist/species/almond.json +210 -0
- package/dist/species/apple.js +230 -0
- package/dist/species/apple.js.map +1 -0
- package/dist/species/apple.json +220 -0
- package/dist/species/ash.js +187 -0
- package/dist/species/ash.js.map +1 -0
- package/dist/species/ash.json +177 -0
- package/dist/species/beech.js +174 -0
- package/dist/species/beech.js.map +1 -0
- package/dist/species/beech.json +167 -0
- package/dist/species/birch.js +170 -0
- package/dist/species/birch.js.map +1 -0
- package/dist/species/birch.json +163 -0
- package/dist/species/catalog.d.ts +61 -0
- package/dist/species/catalog.d.ts.map +1 -0
- package/dist/species/cedar.js +169 -0
- package/dist/species/cedar.js.map +1 -0
- package/dist/species/cedar.json +168 -0
- package/dist/species/cherry.js +237 -0
- package/dist/species/cherry.js.map +1 -0
- package/dist/species/cherry.json +224 -0
- package/dist/species/chestnut.js +230 -0
- package/dist/species/chestnut.js.map +1 -0
- package/dist/species/chestnut.json +220 -0
- package/dist/species/douglas-fir.js +174 -0
- package/dist/species/douglas-fir.js.map +1 -0
- package/dist/species/douglas-fir.json +170 -0
- package/dist/species/elm.js +173 -0
- package/dist/species/elm.js.map +1 -0
- package/dist/species/elm.json +163 -0
- package/dist/species/hemlock.js +175 -0
- package/dist/species/hemlock.js.map +1 -0
- package/dist/species/hemlock.json +171 -0
- package/dist/species/hickory.js +200 -0
- package/dist/species/hickory.js.map +1 -0
- package/dist/species/hickory.json +192 -0
- package/dist/species/index.d.ts +10 -0
- package/dist/species/index.d.ts.map +1 -0
- package/dist/species/juniper.js +158 -0
- package/dist/species/juniper.js.map +1 -0
- package/dist/species/juniper.json +154 -0
- package/dist/species/loaders.d.ts +7 -0
- package/dist/species/loaders.d.ts.map +1 -0
- package/dist/species/mahogany.js +201 -0
- package/dist/species/mahogany.js.map +1 -0
- package/dist/species/mahogany.json +196 -0
- package/dist/species/maple.js +177 -0
- package/dist/species/maple.js.map +1 -0
- package/dist/species/maple.json +171 -0
- package/dist/species/oak.js +220 -0
- package/dist/species/oak.js.map +1 -0
- package/dist/species/oak.json +212 -0
- package/dist/species/olive.js +189 -0
- package/dist/species/olive.js.map +1 -0
- package/dist/species/olive.json +181 -0
- package/dist/species/peach.js +223 -0
- package/dist/species/peach.js.map +1 -0
- package/dist/species/peach.json +210 -0
- package/dist/species/pear.js +213 -0
- package/dist/species/pear.js.map +1 -0
- package/dist/species/pear.json +200 -0
- package/dist/species/pine.js +163 -0
- package/dist/species/pine.js.map +1 -0
- package/dist/species/pine.json +162 -0
- package/dist/species/plum.js +237 -0
- package/dist/species/plum.js.map +1 -0
- package/dist/species/plum.json +224 -0
- package/dist/species/red-oak.js +220 -0
- package/dist/species/red-oak.js.map +1 -0
- package/dist/species/red-oak.json +212 -0
- package/dist/species/redwood.js +174 -0
- package/dist/species/redwood.js.map +1 -0
- package/dist/species/redwood.json +170 -0
- package/dist/species/sequoia.js +167 -0
- package/dist/species/sequoia.js.map +1 -0
- package/dist/species/sequoia.json +166 -0
- package/dist/species/spruce.js +170 -0
- package/dist/species/spruce.js.map +1 -0
- package/dist/species/spruce.json +166 -0
- package/dist/species/teak.js +166 -0
- package/dist/species/teak.js.map +1 -0
- package/dist/species/teak.json +157 -0
- package/dist/species/types.d.ts +65 -0
- package/dist/species/types.d.ts.map +1 -0
- package/dist/species/walnut.js +220 -0
- package/dist/species/walnut.js.map +1 -0
- package/dist/species/walnut.json +212 -0
- package/dist/species/white-oak.js +213 -0
- package/dist/species/white-oak.js.map +1 -0
- package/dist/species/white-oak.json +208 -0
- package/dist/species.js +160 -0
- package/dist/species.js.map +1 -0
- package/dist/styles/apply.d.ts +12 -0
- package/dist/styles/apply.d.ts.map +1 -0
- package/dist/styles/low-poly/index.d.ts +1 -1
- package/dist/styles/low-poly/index.d.ts.map +1 -1
- package/dist/styles/low-poly.js +13 -10
- package/dist/styles/low-poly.js.map +1 -1
- package/dist/styles/material-features.d.ts +27 -0
- package/dist/styles/material-features.d.ts.map +1 -0
- package/dist/styles/names.d.ts +28 -0
- package/dist/styles/names.d.ts.map +1 -0
- package/dist/styles/realistic/index.d.ts +3 -8
- package/dist/styles/realistic/index.d.ts.map +1 -1
- package/dist/styles/realistic/textures.d.ts +20 -0
- package/dist/styles/realistic/textures.d.ts.map +1 -0
- package/dist/styles/realistic.js +14 -18
- package/dist/styles/realistic.js.map +1 -1
- package/dist/styles/registry.d.ts +2 -13
- package/dist/styles/registry.d.ts.map +1 -1
- package/dist/styles/stylized/index.d.ts +1 -1
- package/dist/styles/stylized/index.d.ts.map +1 -1
- package/dist/styles/stylized.js +13 -10
- package/dist/styles/stylized.js.map +1 -1
- package/dist/styles/types.d.ts +16 -2
- package/dist/styles/types.d.ts.map +1 -1
- package/dist/textures/realistic/bark-fibrous-normal.png +0 -0
- package/dist/textures/realistic/bark-fibrous.png +0 -0
- package/dist/textures/realistic/bark-papery-normal.png +0 -0
- package/dist/textures/realistic/bark-papery.png +0 -0
- package/dist/textures/realistic/bark-plated-normal.png +0 -0
- package/dist/textures/realistic/bark-plated.png +0 -0
- package/dist/textures/realistic/bark-smooth-normal.png +0 -0
- package/dist/textures/realistic/bark-smooth.png +0 -0
- package/dist/textures/realistic/leaf-compound.png +0 -0
- package/dist/textures/realistic/leaf-lobed.png +0 -0
- package/dist/textures/realistic/leaf-palmate.png +0 -0
- package/dist/textures/realistic/scale.png +0 -0
- package/dist/tree/Tree.d.ts +34 -5
- package/dist/tree/Tree.d.ts.map +1 -1
- package/package.json +9 -2
- package/dist/forest-OX9WvAwh.js.map +0 -1
- package/dist/types-BZjHhNz7.js +0 -20
- package/dist/types-BZjHhNz7.js.map +0 -1
|
@@ -1,411 +1,6 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { BufferAttribute, BufferGeometry, Color, DoubleSide, DynamicDrawUsage, Euler, Group, InstancedBufferAttribute, InstancedInterleavedBuffer, InstancedMesh, InterleavedBuffer, InterleavedBufferAttribute, LightingModel, Matrix4, Mesh, MeshStandardNodeMaterial, NoColorSpace, Quaternion, RepeatWrapping, SRGBColorSpace, Texture, TextureLoader, Vector3 } from "three/webgpu";
|
|
3
|
-
import { BRDF_Lambert, attribute, color, cross, diffuseColor, dot, float, fract, mix, modelWorldMatrixInverse, normalGeometry, normalMap, normalView, positionGeometry, positionLocal, select, sin, smoothstep, texture, uniform, uv, varying, vec2, vec3, vec4 } from "three/tsl";
|
|
4
|
-
// src/definition/defaults.ts
|
|
5
|
-
/** Defaults for one branching level; fills fields missing from a partial `branches[i]`. */
|
|
6
|
-
var DEFAULT_BRANCH_LEVEL = {
|
|
7
|
-
count: 6,
|
|
8
|
-
start: .35,
|
|
9
|
-
end: .95,
|
|
10
|
-
angle: 45,
|
|
11
|
-
angleVariation: .2,
|
|
12
|
-
spiral: 137.5,
|
|
13
|
-
length: .5,
|
|
14
|
-
lengthVariation: .2,
|
|
15
|
-
radius: .6,
|
|
16
|
-
taper: .3,
|
|
17
|
-
sides: 6,
|
|
18
|
-
sections: 5,
|
|
19
|
-
bend: .1,
|
|
20
|
-
gravity: .1
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* The documented defaults: a small broadleaf tree with two branching levels.
|
|
24
|
-
* Anything a Definition leaves out is taken from here.
|
|
25
|
-
*/
|
|
26
|
-
var DEFAULT_DEFINITION = {
|
|
27
|
-
schemaVersion: 2,
|
|
28
|
-
name: "",
|
|
29
|
-
form: "broadleaf",
|
|
30
|
-
trunk: {
|
|
31
|
-
length: 4,
|
|
32
|
-
radius: .25,
|
|
33
|
-
taper: .4,
|
|
34
|
-
sides: 8,
|
|
35
|
-
sections: 8,
|
|
36
|
-
lengthVariation: .15,
|
|
37
|
-
bend: .08
|
|
38
|
-
},
|
|
39
|
-
branches: [{ ...DEFAULT_BRANCH_LEVEL }, {
|
|
40
|
-
...DEFAULT_BRANCH_LEVEL,
|
|
41
|
-
count: 4,
|
|
42
|
-
start: .2,
|
|
43
|
-
angle: 40,
|
|
44
|
-
length: .45,
|
|
45
|
-
sides: 4,
|
|
46
|
-
sections: 3
|
|
47
|
-
}],
|
|
48
|
-
leaves: {
|
|
49
|
-
count: 8,
|
|
50
|
-
shape: "card",
|
|
51
|
-
size: .25,
|
|
52
|
-
sizeVariation: .2,
|
|
53
|
-
start: .3,
|
|
54
|
-
angle: 40
|
|
55
|
-
},
|
|
56
|
-
materials: {
|
|
57
|
-
shading: "standard",
|
|
58
|
-
bark: {
|
|
59
|
-
color: "#6b4a2b",
|
|
60
|
-
roughness: .9,
|
|
61
|
-
map: null,
|
|
62
|
-
normalMap: null
|
|
63
|
-
},
|
|
64
|
-
leaf: {
|
|
65
|
-
roughness: .7,
|
|
66
|
-
map: null,
|
|
67
|
-
alphaTest: .5
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
season: {
|
|
71
|
-
leafColor: [
|
|
72
|
-
{
|
|
73
|
-
t: 0,
|
|
74
|
-
value: "#8bc34a"
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
t: .25,
|
|
78
|
-
value: "#3e7d2b"
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
t: .5,
|
|
82
|
-
value: "#3e7d2b"
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
t: .62,
|
|
86
|
-
value: "#d8892b"
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
t: .72,
|
|
90
|
-
value: "#a0401f"
|
|
91
|
-
}
|
|
92
|
-
],
|
|
93
|
-
leafCoverage: [
|
|
94
|
-
{
|
|
95
|
-
t: 0,
|
|
96
|
-
value: .3
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
t: .1,
|
|
100
|
-
value: 1
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
t: .62,
|
|
104
|
-
value: 1
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
t: .75,
|
|
108
|
-
value: 0
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
t: .97,
|
|
112
|
-
value: 0
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
t: 1,
|
|
116
|
-
value: .3
|
|
117
|
-
}
|
|
118
|
-
],
|
|
119
|
-
blossom: [{
|
|
120
|
-
t: 0,
|
|
121
|
-
value: 0
|
|
122
|
-
}],
|
|
123
|
-
blossomColor: "#f7c6d9"
|
|
124
|
-
},
|
|
125
|
-
wind: {
|
|
126
|
-
trunk: .2,
|
|
127
|
-
branches: .5,
|
|
128
|
-
leaves: .8,
|
|
129
|
-
frequency: 1
|
|
130
|
-
},
|
|
131
|
-
variation: {
|
|
132
|
-
color: .1,
|
|
133
|
-
growth: .1,
|
|
134
|
-
season: .05
|
|
135
|
-
},
|
|
136
|
-
sculpts: []
|
|
137
|
-
};
|
|
138
|
-
// src/definition/migrations.ts
|
|
139
|
-
var MIGRATIONS = {
|
|
140
|
-
/**
|
|
141
|
-
* v1 -> v2: `bark` moved into `materials.bark`; branches, leaves, Season Profile, Wind,
|
|
142
|
-
* variation and sculpts were added. A v1 tree was a bare trunk, so it keeps no branches
|
|
143
|
-
* and no leaves (same tree as before).
|
|
144
|
-
*/
|
|
145
|
-
1: (input) => {
|
|
146
|
-
const { bark, ...rest } = input;
|
|
147
|
-
return {
|
|
148
|
-
...rest,
|
|
149
|
-
schemaVersion: 2,
|
|
150
|
-
branches: [],
|
|
151
|
-
leaves: { count: 0 },
|
|
152
|
-
...bark === void 0 ? {} : { materials: { bark } }
|
|
153
|
-
};
|
|
154
|
-
} };
|
|
155
|
-
// src/definition/schema.ts
|
|
156
|
-
/** Thrown for an invalid Tree Definition; `field` is the path of the bad field, e.g. `branches[1].angle`. */
|
|
157
|
-
var TreeDefinitionError = class extends Error {
|
|
158
|
-
field;
|
|
159
|
-
constructor(field, message) {
|
|
160
|
-
super(`Invalid Tree Definition: ${field} ${message}`);
|
|
161
|
-
this.name = "TreeDefinitionError";
|
|
162
|
-
this.field = field;
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
var ROOT = "(root)";
|
|
166
|
-
var join = (path, key) => path === ROOT ? key : `${path}.${key}`;
|
|
167
|
-
var show = (v) => v === void 0 ? "undefined" : JSON.stringify(v);
|
|
168
|
-
function isObject(v) {
|
|
169
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
170
|
-
}
|
|
171
|
-
/** Leaf schema from a predicate; `undefined` takes the fallback, or is reported missing. */
|
|
172
|
-
function leaf(expected, ok) {
|
|
173
|
-
return (value, fallback, path) => {
|
|
174
|
-
const v = value === void 0 ? fallback : value;
|
|
175
|
-
if (v === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
176
|
-
if (!ok(v)) throw new TreeDefinitionError(path, `must be ${expected}, got ${show(v)}`);
|
|
177
|
-
return Array.isArray(v) ? [...v] : v;
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
var isNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
181
|
-
var range = (min, max) => leaf(`a number from ${min} to ${max}`, (v) => isNum(v) && v >= min && v <= max);
|
|
182
|
-
var unit = range(0, 1);
|
|
183
|
-
var positive = leaf("a number > 0", (v) => isNum(v) && v > 0);
|
|
184
|
-
var finite = leaf("a finite number", isNum);
|
|
185
|
-
var intAtLeast = (min) => leaf(`an integer >= ${min}`, (v) => Number.isInteger(v) && v >= min);
|
|
186
|
-
var hexColor = leaf("a colour like \"#6b4a2b\"", (v) => typeof v === "string" && /^#[0-9a-f]{6}$/i.test(v));
|
|
187
|
-
var text = leaf("a string", (v) => typeof v === "string");
|
|
188
|
-
var url = leaf("a URL string or null", (v) => v === null || typeof v === "string" && v.length > 0);
|
|
189
|
-
var vec3$1 = leaf("[x, y, z] numbers", (v) => Array.isArray(v) && v.length === 3 && v.every(isNum));
|
|
190
|
-
var oneOf = (...values) => leaf(`one of ${values.map((x) => `"${x}"`).join(", ")}`, (v) => values.includes(v));
|
|
191
|
-
var literal = (expected) => leaf(String(expected), (v) => v === expected);
|
|
192
|
-
/** Object with exactly these fields; unknown fields are rejected (catches typos). */
|
|
193
|
-
function obj(fields) {
|
|
194
|
-
return (value, fallback, path) => {
|
|
195
|
-
if (value === void 0 && fallback === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
196
|
-
const given = value === void 0 ? {} : value;
|
|
197
|
-
if (!isObject(given)) throw new TreeDefinitionError(path, `must be an object, got ${show(given)}`);
|
|
198
|
-
for (const key of Object.keys(given)) if (!(key in fields)) throw new TreeDefinitionError(join(path, key), "is not a known field");
|
|
199
|
-
const out = {};
|
|
200
|
-
for (const key of Object.keys(fields)) out[key] = fields[key](given[key], fallback?.[key], join(path, key));
|
|
201
|
-
return out;
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
/** Array; a missing array takes the fallback array, each item is filled from `itemFallback`. */
|
|
205
|
-
function arr(item, opts = {}) {
|
|
206
|
-
const { min = 0, max = Infinity, itemFallback } = opts;
|
|
207
|
-
return (value, fallback, path) => {
|
|
208
|
-
const v = value === void 0 ? fallback : value;
|
|
209
|
-
if (v === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
210
|
-
if (!Array.isArray(v)) throw new TreeDefinitionError(path, `must be an array, got ${show(v)}`);
|
|
211
|
-
if (v.length < min) throw new TreeDefinitionError(path, `must have at least ${min} entries`);
|
|
212
|
-
if (v.length > max) throw new TreeDefinitionError(path, `must have at most ${max} entries`);
|
|
213
|
-
return v.map((x, i) => item(x, itemFallback, `${path}[${i}]`));
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
/** Adds a cross-field check; `check` returns [field, problem] or null. */
|
|
217
|
-
function refine(schema, check) {
|
|
218
|
-
return (value, fallback, path) => {
|
|
219
|
-
const v = schema(value, fallback, path);
|
|
220
|
-
const problem = check(v);
|
|
221
|
-
if (problem) throw new TreeDefinitionError(join(path, problem[0]), problem[1]);
|
|
222
|
-
return v;
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
/** Union discriminated by `key`. */
|
|
226
|
-
function tagged(key, variants) {
|
|
227
|
-
const names = Object.keys(variants);
|
|
228
|
-
return (value, fallback, path) => {
|
|
229
|
-
if (!isObject(value)) throw new TreeDefinitionError(path, `must be an object, got ${show(value)}`);
|
|
230
|
-
const variant = variants[value[key]];
|
|
231
|
-
if (!variant) throw new TreeDefinitionError(join(path, key), `must be one of ${names.map((n) => `"${n}"`).join(", ")}, got ${show(value[key])}`);
|
|
232
|
-
return variant(value, fallback, path);
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
/** Keyframe track: at least one key, `t` in 0..1 and non-decreasing. */
|
|
236
|
-
function track(value) {
|
|
237
|
-
const keys = arr(obj({
|
|
238
|
-
t: unit,
|
|
239
|
-
value
|
|
240
|
-
}), { min: 1 });
|
|
241
|
-
return (v, fallback, path) => {
|
|
242
|
-
const out = keys(v, fallback, path);
|
|
243
|
-
for (let i = 1; i < out.length; i++) if (out[i].t < out[i - 1].t) throw new TreeDefinitionError(`${path}[${i}].t`, `must be >= the previous keyframe's t (${out[i - 1].t}), got ${out[i].t}`);
|
|
244
|
-
return out;
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
var branchLevel = refine(obj({
|
|
248
|
-
count: intAtLeast(0),
|
|
249
|
-
start: unit,
|
|
250
|
-
end: unit,
|
|
251
|
-
angle: range(0, 180),
|
|
252
|
-
angleVariation: unit,
|
|
253
|
-
spiral: range(0, 360),
|
|
254
|
-
length: positive,
|
|
255
|
-
lengthVariation: unit,
|
|
256
|
-
radius: unit,
|
|
257
|
-
taper: unit,
|
|
258
|
-
sides: intAtLeast(3),
|
|
259
|
-
sections: intAtLeast(1),
|
|
260
|
-
bend: unit,
|
|
261
|
-
gravity: range(-1, 1)
|
|
262
|
-
}), (b) => b.end < b.start ? ["end", `must be >= start (${b.start}), got ${b.end}`] : null);
|
|
263
|
-
var branchPath = leaf("a branch path like \"0\" or \"0.3.1\"", (v) => typeof v === "string" && /^0(\.\d+)*$/.test(v));
|
|
264
|
-
var sculpt = tagged("action", {
|
|
265
|
-
delete: obj({
|
|
266
|
-
action: literal("delete"),
|
|
267
|
-
seed: finite,
|
|
268
|
-
path: branchPath
|
|
269
|
-
}),
|
|
270
|
-
move: obj({
|
|
271
|
-
action: literal("move"),
|
|
272
|
-
seed: finite,
|
|
273
|
-
path: branchPath,
|
|
274
|
-
rotation: vec3$1,
|
|
275
|
-
scale: positive
|
|
276
|
-
}),
|
|
277
|
-
leaves: obj({
|
|
278
|
-
action: literal("leaves"),
|
|
279
|
-
seed: finite,
|
|
280
|
-
path: branchPath,
|
|
281
|
-
coverage: unit
|
|
282
|
-
})
|
|
283
|
-
});
|
|
284
|
-
/** The whole Tree Definition schema (current version). Mirrors the interfaces in types.ts. */
|
|
285
|
-
var treeDefinition = obj({
|
|
286
|
-
schemaVersion: literal(2),
|
|
287
|
-
name: text,
|
|
288
|
-
form: oneOf("broadleaf", "conifer"),
|
|
289
|
-
trunk: obj({
|
|
290
|
-
length: positive,
|
|
291
|
-
radius: positive,
|
|
292
|
-
taper: unit,
|
|
293
|
-
sides: intAtLeast(3),
|
|
294
|
-
sections: intAtLeast(1),
|
|
295
|
-
lengthVariation: unit,
|
|
296
|
-
bend: unit
|
|
297
|
-
}),
|
|
298
|
-
branches: arr(branchLevel, {
|
|
299
|
-
max: 4,
|
|
300
|
-
itemFallback: DEFAULT_BRANCH_LEVEL
|
|
301
|
-
}),
|
|
302
|
-
leaves: obj({
|
|
303
|
-
count: intAtLeast(0),
|
|
304
|
-
shape: oneOf("card", "clump", "needle", "puff"),
|
|
305
|
-
size: positive,
|
|
306
|
-
sizeVariation: unit,
|
|
307
|
-
start: unit,
|
|
308
|
-
angle: range(0, 180)
|
|
309
|
-
}),
|
|
310
|
-
materials: obj({
|
|
311
|
-
shading: oneOf("standard", "flat", "toon"),
|
|
312
|
-
bark: obj({
|
|
313
|
-
color: hexColor,
|
|
314
|
-
roughness: unit,
|
|
315
|
-
map: url,
|
|
316
|
-
normalMap: url
|
|
317
|
-
}),
|
|
318
|
-
leaf: obj({
|
|
319
|
-
roughness: unit,
|
|
320
|
-
map: url,
|
|
321
|
-
alphaTest: unit
|
|
322
|
-
})
|
|
323
|
-
}),
|
|
324
|
-
season: obj({
|
|
325
|
-
leafColor: track(hexColor),
|
|
326
|
-
leafCoverage: track(unit),
|
|
327
|
-
blossom: track(unit),
|
|
328
|
-
blossomColor: hexColor
|
|
329
|
-
}),
|
|
330
|
-
wind: obj({
|
|
331
|
-
trunk: unit,
|
|
332
|
-
branches: unit,
|
|
333
|
-
leaves: unit,
|
|
334
|
-
frequency: positive
|
|
335
|
-
}),
|
|
336
|
-
variation: obj({
|
|
337
|
-
color: unit,
|
|
338
|
-
growth: unit,
|
|
339
|
-
season: unit
|
|
340
|
-
}),
|
|
341
|
-
sculpts: arr(sculpt)
|
|
342
|
-
});
|
|
343
|
-
/**
|
|
344
|
-
* Validates a raw current-version Definition and fills every missing field from
|
|
345
|
-
* {@link DEFAULT_DEFINITION}. Returns a fresh object (never aliases input or defaults).
|
|
346
|
-
*/
|
|
347
|
-
function validateDefinition(raw) {
|
|
348
|
-
return treeDefinition(raw, DEFAULT_DEFINITION, ROOT);
|
|
349
|
-
}
|
|
350
|
-
// src/definition/parse.ts
|
|
351
|
-
/**
|
|
352
|
-
* Turns untrusted input into a complete, valid Tree Definition:
|
|
353
|
-
* - `input` is a Definition object (e.g. parsed JSON) or a JSON string;
|
|
354
|
-
* - older `schemaVersion`s are migrated to {@link CURRENT_SCHEMA_VERSION};
|
|
355
|
-
* - missing fields are filled from {@link DEFAULT_DEFINITION} (missing fields of a
|
|
356
|
-
* `branches[i]` entry from {@link DEFAULT_BRANCH_LEVEL});
|
|
357
|
-
* - every field is validated; unknown fields are rejected.
|
|
358
|
-
* Throws {@link TreeDefinitionError} naming the first bad field (`field`, e.g. `branches[1].angle`).
|
|
359
|
-
* Never mutates `input`.
|
|
360
|
-
*/
|
|
361
|
-
function parseDefinition(input) {
|
|
362
|
-
let raw = input;
|
|
363
|
-
if (typeof raw === "string") try {
|
|
364
|
-
raw = JSON.parse(raw);
|
|
365
|
-
} catch (e) {
|
|
366
|
-
throw new TreeDefinitionError("(root)", `is not valid JSON: ${e.message}`);
|
|
367
|
-
}
|
|
368
|
-
if (!isObject(raw)) throw new TreeDefinitionError("(root)", "must be an object");
|
|
369
|
-
const version = raw.schemaVersion;
|
|
370
|
-
if (version === void 0) throw new TreeDefinitionError("schemaVersion", "is missing");
|
|
371
|
-
if (!Number.isInteger(version) || version < 1) throw new TreeDefinitionError("schemaVersion", `must be an integer >= 1, got ${JSON.stringify(version)}`);
|
|
372
|
-
if (version > 2) throw new TreeDefinitionError("schemaVersion", `is ${version}, newer than this version of @casmo/tree.js supports (2); upgrade the library`);
|
|
373
|
-
for (let v = version; v < 2; v++) raw = MIGRATIONS[v](raw);
|
|
374
|
-
return validateDefinition(raw);
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Starts from a Definition (e.g. a built-in Style) and changes a few parameters:
|
|
378
|
-
* objects are merged deeply, arrays and values replace. Returns a new, validated Definition.
|
|
379
|
-
*
|
|
380
|
-
* ```ts
|
|
381
|
-
* const tall = extendDefinition(broadleaf, { trunk: { length: 7 }, wind: { leaves: 1 } });
|
|
382
|
-
* ```
|
|
383
|
-
*/
|
|
384
|
-
function extendDefinition(base, patch) {
|
|
385
|
-
const parsed = parseDefinition(base);
|
|
386
|
-
if (!isObject(patch)) throw new TreeDefinitionError("(root)", "patch must be an object");
|
|
387
|
-
return parseDefinition({
|
|
388
|
-
...merge(parsed, patch),
|
|
389
|
-
schemaVersion: 2
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
function merge(base, patch) {
|
|
393
|
-
const out = { ...base };
|
|
394
|
-
for (const [key, value] of Object.entries(patch)) {
|
|
395
|
-
if (value === void 0) continue;
|
|
396
|
-
out[key] = isObject(value) && isObject(base[key]) ? merge(base[key], value) : value;
|
|
397
|
-
}
|
|
398
|
-
return out;
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Fetches a Tree Definition JSON file (e.g. a Tree Editor export) and parses it with
|
|
402
|
-
* {@link parseDefinition}. Rejects with an Error for an HTTP error, or a TreeDefinitionError.
|
|
403
|
-
*/
|
|
404
|
-
async function fetchDefinition(url, init) {
|
|
405
|
-
const response = await fetch(url, init);
|
|
406
|
-
if (!response.ok) throw new Error(`Could not load Tree Definition from ${String(url)}: HTTP ${response.status}`);
|
|
407
|
-
return parseDefinition(await response.text());
|
|
408
|
-
}
|
|
1
|
+
import { g as DEFAULT_FRUIT_COLOR, h as DEFAULT_FRUIT_AMOUNT, o as materialFeaturesOf, t as STYLE_NAMES, u as parseDefinition } from "./names-BE9he6CS.js";
|
|
2
|
+
import { BufferAttribute, BufferGeometry, Color, DoubleSide, DynamicDrawUsage, Euler, Group, InstancedBufferAttribute, InstancedInterleavedBuffer, InstancedMesh, InterleavedBuffer, InterleavedBufferAttribute, LightingModel, Matrix4, Mesh, MeshStandardNodeMaterial, NoColorSpace, PhysicalLightingModel, Quaternion, RepeatWrapping, SRGBColorSpace, Texture, TextureLoader, Vector3 } from "three/webgpu";
|
|
3
|
+
import { BRDF_Lambert, attribute, color, cross, diffuseColor, dot, float, fract, length, mix, modelWorldMatrixInverse, normalGeometry, normalMap, normalView, normalize, positionGeometry, positionLocal, positionViewDirection, select, sin, smoothstep, texture, transformNormalToView, uniform, uv, varying, vec2, vec3, vec4 } from "three/tsl";
|
|
409
4
|
// src/random/index.ts
|
|
410
5
|
/**
|
|
411
6
|
* Random: the only source of randomness in tree.js.
|
|
@@ -668,6 +263,13 @@ function hexToLinear(hex) {
|
|
|
668
263
|
srgbToLinear((n & 255) / 255)
|
|
669
264
|
];
|
|
670
265
|
}
|
|
266
|
+
/** A Season Profile's Fruit tracks, with the defaults for absent ones. */
|
|
267
|
+
function fruitTracks(profile) {
|
|
268
|
+
return {
|
|
269
|
+
fruit: profile.fruit ?? DEFAULT_FRUIT_AMOUNT,
|
|
270
|
+
fruitColor: profile.fruitColor ?? DEFAULT_FRUIT_COLOR
|
|
271
|
+
};
|
|
272
|
+
}
|
|
671
273
|
/**
|
|
672
274
|
* Evaluates the Season Profile at `season`, shifted by `shift` (a Seed's timing offset,
|
|
673
275
|
* see {@link seasonShift}) and wrapped around the year.
|
|
@@ -682,7 +284,12 @@ function evaluateSeason(profile, season, shift = 0) {
|
|
|
682
284
|
})), s),
|
|
683
285
|
leafCoverage: sampleTrack(profile.leafCoverage, s),
|
|
684
286
|
blossom: sampleTrack(profile.blossom, s),
|
|
685
|
-
blossomColor: hexToLinear(profile.blossomColor)
|
|
287
|
+
blossomColor: hexToLinear(profile.blossomColor),
|
|
288
|
+
fruit: sampleTrack(fruitTracks(profile).fruit, s),
|
|
289
|
+
fruitColor: sampleTrack(fruitTracks(profile).fruitColor.map((k) => ({
|
|
290
|
+
t: k.t,
|
|
291
|
+
value: hexToLinear(k.value)
|
|
292
|
+
})), s)
|
|
686
293
|
};
|
|
687
294
|
}
|
|
688
295
|
/** The {@link PartSeason} of vertex `vertex` in `aSeason` attribute data (3 floats per vertex). */
|
|
@@ -709,6 +316,15 @@ function leafSeasonAttribute(seed, variation, leafId) {
|
|
|
709
316
|
blossomRank: seedRandom(seed, `${leafId}#blossom`)
|
|
710
317
|
};
|
|
711
318
|
}
|
|
319
|
+
/** The Season timing (`aSeason` attribute) of one Fruit: like a leaf's, with the Fruit's rank as `coverageRank`. */
|
|
320
|
+
function fruitSeasonAttribute(seed, variation, fruitId) {
|
|
321
|
+
const jitter = (seedRandom(seed, `fruit/${fruitId}#season`) * 2 - 1) * (variation.season / 2);
|
|
322
|
+
return {
|
|
323
|
+
shift: seasonShift(seed, variation) + jitter,
|
|
324
|
+
coverageRank: seedRandom(seed, `fruit/${fruitId}#rank`),
|
|
325
|
+
blossomRank: 0
|
|
326
|
+
};
|
|
327
|
+
}
|
|
712
328
|
/** `aSeason` for wood vertices: the tree's timing shift, no ranks. */
|
|
713
329
|
function woodSeasonAttribute(seed, variation) {
|
|
714
330
|
return {
|
|
@@ -730,6 +346,20 @@ function leafSeason(profile, season, part) {
|
|
|
730
346
|
color: blossom ? state.blossomColor : state.leafColor
|
|
731
347
|
};
|
|
732
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Evaluates one Fruit from its Season timing (`aSeason` of the fruit mesh: shift, the Fruit's
|
|
351
|
+
* rank as `coverageRank`): the exact CPU twin of the Fruit shader (see `createFruitSeasonNodes`).
|
|
352
|
+
* A Fruit shows while the Fruit amount is above its rank, scaled about its attachment point by the amount.
|
|
353
|
+
*/
|
|
354
|
+
function fruitSeason(profile, season, part) {
|
|
355
|
+
const state = evaluateSeason(profile, season, part.shift);
|
|
356
|
+
const visible = state.fruit > part.coverageRank;
|
|
357
|
+
return {
|
|
358
|
+
visible,
|
|
359
|
+
scale: visible ? state.fruit : 0,
|
|
360
|
+
color: state.fruitColor
|
|
361
|
+
};
|
|
362
|
+
}
|
|
733
363
|
// src/generator/vec.ts
|
|
734
364
|
var add = (a, b) => [
|
|
735
365
|
a[0] + b[0],
|
|
@@ -752,9 +382,9 @@ var cross$1 = (a, b) => [
|
|
|
752
382
|
a[2] * b[0] - a[0] * b[2],
|
|
753
383
|
a[0] * b[1] - a[1] * b[0]
|
|
754
384
|
];
|
|
755
|
-
var length = (a) => hypot3(a[0], a[1], a[2]);
|
|
756
|
-
function normalize(a) {
|
|
757
|
-
const l = length(a);
|
|
385
|
+
var length$1 = (a) => hypot3(a[0], a[1], a[2]);
|
|
386
|
+
function normalize$1(a) {
|
|
387
|
+
const l = length$1(a);
|
|
758
388
|
return l > 1e-12 ? scale(a, 1 / l) : [
|
|
759
389
|
0,
|
|
760
390
|
1,
|
|
@@ -768,7 +398,7 @@ var lerp = (a, b, t) => [
|
|
|
768
398
|
];
|
|
769
399
|
/** Any unit vector perpendicular to the unit vector `v`. */
|
|
770
400
|
function perpendicular(v) {
|
|
771
|
-
return normalize(cross$1(Math.abs(v[1]) < .9 ? [
|
|
401
|
+
return normalize$1(cross$1(Math.abs(v[1]) < .9 ? [
|
|
772
402
|
0,
|
|
773
403
|
1,
|
|
774
404
|
0
|
|
@@ -786,19 +416,19 @@ function perpendicular(v) {
|
|
|
786
416
|
function branchFrames(branch) {
|
|
787
417
|
const pts = branch.points.map((p) => p.position);
|
|
788
418
|
const n = pts.length;
|
|
789
|
-
const segment = (i) => normalize(sub(pts[Math.min(i + 1, n - 1)], pts[Math.min(i, n - 2)]));
|
|
419
|
+
const segment = (i) => normalize$1(sub(pts[Math.min(i + 1, n - 1)], pts[Math.min(i, n - 2)]));
|
|
790
420
|
const frames = [];
|
|
791
421
|
for (let i = 0; i < n; i++) {
|
|
792
422
|
const tangent = i === 0 && branch.parent === null ? [
|
|
793
423
|
0,
|
|
794
424
|
1,
|
|
795
425
|
0
|
|
796
|
-
] : i === 0 || i === n - 1 ? segment(Math.min(i, n - 2)) : normalize(add(segment(i - 1), segment(i)));
|
|
426
|
+
] : i === 0 || i === n - 1 ? segment(Math.min(i, n - 2)) : normalize$1(add(segment(i - 1), segment(i)));
|
|
797
427
|
let normal;
|
|
798
428
|
if (i === 0) normal = perpendicular(tangent);
|
|
799
429
|
else {
|
|
800
430
|
const previous = frames[i - 1].normal;
|
|
801
|
-
normal = normalize(sub(previous, scale(tangent, dot$1(previous, tangent))));
|
|
431
|
+
normal = normalize$1(sub(previous, scale(tangent, dot$1(previous, tangent))));
|
|
802
432
|
}
|
|
803
433
|
frames.push({
|
|
804
434
|
tangent,
|
|
@@ -811,7 +441,7 @@ function branchFrames(branch) {
|
|
|
811
441
|
/** Total length of a branch's centre line. */
|
|
812
442
|
function pathLength(branch) {
|
|
813
443
|
let total = 0;
|
|
814
|
-
for (let i = 1; i < branch.points.length; i++) total += length(sub(branch.points[i].position, branch.points[i - 1].position));
|
|
444
|
+
for (let i = 1; i < branch.points.length; i++) total += length$1(sub(branch.points[i].position, branch.points[i - 1].position));
|
|
815
445
|
return total;
|
|
816
446
|
}
|
|
817
447
|
/** The point, frame and radius at fraction `t` (0..1) of a branch's length. */
|
|
@@ -822,11 +452,11 @@ function sampleBranch(branch, frames, t) {
|
|
|
822
452
|
for (let i = 1; i < pts.length; i++) {
|
|
823
453
|
const a = pts[i - 1];
|
|
824
454
|
const b = pts[i];
|
|
825
|
-
const segment = length(sub(b.position, a.position));
|
|
455
|
+
const segment = length$1(sub(b.position, a.position));
|
|
826
456
|
if (walked + segment >= target || i === pts.length - 1) {
|
|
827
457
|
const f = segment > 0 ? Math.min(Math.max((target - walked) / segment, 0), 1) : 0;
|
|
828
|
-
const tangent = normalize(sub(b.position, a.position));
|
|
829
|
-
const normal = normalize(sub(frames[i - 1].normal, scale(tangent, dot$1(frames[i - 1].normal, tangent))));
|
|
458
|
+
const tangent = normalize$1(sub(b.position, a.position));
|
|
459
|
+
const normal = normalize$1(sub(frames[i - 1].normal, scale(tangent, dot$1(frames[i - 1].normal, tangent))));
|
|
830
460
|
return {
|
|
831
461
|
position: lerp(a.position, b.position, f),
|
|
832
462
|
radius: a.radius + (b.radius - a.radius) * f,
|
|
@@ -845,36 +475,36 @@ function sampleBranch(branch, frames, t) {
|
|
|
845
475
|
};
|
|
846
476
|
}
|
|
847
477
|
// src/generator/branches.ts
|
|
848
|
-
var DEG$
|
|
478
|
+
var DEG$4 = Math.PI / 180;
|
|
849
479
|
/** How far `gravity` = ±1 turns a branch over its whole length (sum of per-section pulls). */
|
|
850
480
|
var GRAVITY_PER_BRANCH = 1.5;
|
|
851
481
|
/**
|
|
852
|
-
* Grows all branching levels
|
|
482
|
+
* Grows all branching levels on every Stem, breadth first (level by level, over all Stems).
|
|
853
483
|
*
|
|
854
484
|
* Branch path stability (ADR 0003): a parent's spiral phase comes from `random.fork(parentPath)`
|
|
855
485
|
* and each child's shape from `random.fork(childPath)`. So a branch depends only on its
|
|
856
486
|
* ancestors and its own path, never on its siblings' subtrees, on deeper levels or on leaves.
|
|
857
487
|
*/
|
|
858
|
-
function buildBranches(definition,
|
|
488
|
+
function buildBranches(definition, stems, random) {
|
|
859
489
|
const out = [];
|
|
860
|
-
const queue = [
|
|
490
|
+
const queue = [...stems];
|
|
861
491
|
for (let q = 0; q < queue.length; q++) {
|
|
862
492
|
const parent = queue[q];
|
|
863
493
|
const level = definition.branches[parent.level];
|
|
864
494
|
if (!level) continue;
|
|
865
|
-
const children = growChildren(parent, level, random
|
|
495
|
+
const children = growChildren(parent, level, random);
|
|
866
496
|
out.push(...children);
|
|
867
497
|
queue.push(...children);
|
|
868
498
|
}
|
|
869
499
|
return out;
|
|
870
500
|
}
|
|
871
|
-
/**
|
|
872
|
-
var
|
|
501
|
+
/** A Crown Shape curve's multiplier at attach position `t`; an absent curve is flat (1). */
|
|
502
|
+
var crownShape = (curve, t) => curve ? sampleTrack(curve, t) : 1;
|
|
873
503
|
/**
|
|
874
|
-
*
|
|
875
|
-
*
|
|
504
|
+
* Grows one parent's children. The Crown Shape curves (`lengthProfile`, `angleProfile`) scale each
|
|
505
|
+
* child's length and angle by where it sits on the parent (ADR 0004).
|
|
876
506
|
*/
|
|
877
|
-
function growChildren(parent, level, random
|
|
507
|
+
function growChildren(parent, level, random) {
|
|
878
508
|
const count = level.count;
|
|
879
509
|
const azimuth0 = random.fork(parent.path).range(0, Math.PI * 2);
|
|
880
510
|
const frames = branchFrames(parent);
|
|
@@ -885,18 +515,17 @@ function growChildren(parent, level, random, cone) {
|
|
|
885
515
|
const r = random.fork(path);
|
|
886
516
|
const t = level.start + (level.end - level.start) * (i + r.range(.15, .85)) / count;
|
|
887
517
|
const at = sampleBranch(parent, frames, t);
|
|
888
|
-
const angle = Math.min(level.angle * (1 + r.range(-level.angleVariation, level.angleVariation)), 180) * DEG$
|
|
889
|
-
const azimuth = azimuth0 + i * level.spiral * DEG$
|
|
518
|
+
const angle = Math.min(level.angle * crownShape(level.angleProfile, t) * (1 + r.range(-level.angleVariation, level.angleVariation)), 180) * DEG$4;
|
|
519
|
+
const azimuth = azimuth0 + i * level.spiral * DEG$4;
|
|
890
520
|
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
891
|
-
const start = normalize(add(scale(at.tangent, cos(angle)), scale(across, sin$1(angle))));
|
|
521
|
+
const start = normalize$1(add(scale(at.tangent, cos(angle)), scale(across, sin$1(angle))));
|
|
892
522
|
const pull = [
|
|
893
523
|
0,
|
|
894
524
|
level.gravity * GRAVITY_PER_BRANCH / level.sections,
|
|
895
525
|
0
|
|
896
526
|
];
|
|
897
527
|
let direction = start;
|
|
898
|
-
const
|
|
899
|
-
const step = parentLength * level.length * profile * (1 + r.range(-level.lengthVariation, level.lengthVariation)) / level.sections;
|
|
528
|
+
const step = parentLength * level.length * crownShape(level.lengthProfile, t) * (1 + r.range(-level.lengthVariation, level.lengthVariation)) / level.sections;
|
|
900
529
|
const baseRadius = at.radius * level.radius;
|
|
901
530
|
const points = [];
|
|
902
531
|
let position = at.position;
|
|
@@ -911,7 +540,7 @@ function growChildren(parent, level, random, cone) {
|
|
|
911
540
|
r.range(-1, 1),
|
|
912
541
|
r.range(-1, 1)
|
|
913
542
|
];
|
|
914
|
-
direction = normalize(add(add(direction, scale(wander, level.bend)), pull));
|
|
543
|
+
direction = normalize$1(add(add(direction, scale(wander, level.bend)), pull));
|
|
915
544
|
position = add(position, scale(direction, step));
|
|
916
545
|
}
|
|
917
546
|
children.push({
|
|
@@ -925,67 +554,217 @@ function growChildren(parent, level, random, cone) {
|
|
|
925
554
|
}
|
|
926
555
|
return children;
|
|
927
556
|
}
|
|
928
|
-
// src/generator/
|
|
929
|
-
/**
|
|
930
|
-
var
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
557
|
+
// src/generator/crown.ts
|
|
558
|
+
/** Axes of the ellipsoid. */
|
|
559
|
+
var D = 3;
|
|
560
|
+
var MAX_STEPS = 400;
|
|
561
|
+
/** No axis is flatter than this fraction of the largest one (a single flat leaf card still has a volume). */
|
|
562
|
+
var MIN_FLATNESS = .001;
|
|
563
|
+
/** The Volume of a tree with no leaves and no branches (every Stem Sculpted away). */
|
|
564
|
+
var EMPTY = {
|
|
565
|
+
center: [
|
|
566
|
+
0,
|
|
567
|
+
0,
|
|
568
|
+
0
|
|
569
|
+
],
|
|
570
|
+
radii: [
|
|
571
|
+
1,
|
|
572
|
+
1,
|
|
573
|
+
1
|
|
574
|
+
]
|
|
575
|
+
};
|
|
939
576
|
/**
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
577
|
+
* How far `p` sits from the Crown Volume's centre, in units of the ellipsoid:
|
|
578
|
+
* 0 at the centre, 1 on the shell, > 1 outside. (`1 - crownDistance` is how deep a point is in the crown.)
|
|
579
|
+
*/
|
|
580
|
+
function crownDistance({ center, radii }, p) {
|
|
581
|
+
const x = (p[0] - center[0]) / radii[0];
|
|
582
|
+
const y = (p[1] - center[1]) / radii[1];
|
|
583
|
+
const z = (p[2] - center[2]) / radii[2];
|
|
584
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* The unit outward normal at `p` of the Crown Volume's ellipsoid through `p` (the shell scaled to pass
|
|
588
|
+
* through it), i.e. the normalized gradient `(p - center) / radii²`. At the centre itself: straight up.
|
|
589
|
+
*/
|
|
590
|
+
function crownNormal({ center, radii }, p) {
|
|
591
|
+
const x = (p[0] - center[0]) / (radii[0] * radii[0]);
|
|
592
|
+
const y = (p[1] - center[1]) / (radii[1] * radii[1]);
|
|
593
|
+
const z = (p[2] - center[2]) / (radii[2] * radii[2]);
|
|
594
|
+
const length = Math.sqrt(x * x + y * y + z * z);
|
|
595
|
+
return length > 0 ? [
|
|
596
|
+
x / length,
|
|
597
|
+
y / length,
|
|
598
|
+
z / length
|
|
599
|
+
] : [
|
|
600
|
+
0,
|
|
601
|
+
1,
|
|
602
|
+
0
|
|
603
|
+
];
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Fits the Crown Volume to the leaf vertex positions of the fully grown tree. A tree without leaves gets
|
|
607
|
+
* a Volume around its branches (centre lines, Roots excluded).
|
|
608
|
+
*/
|
|
609
|
+
function crownVolume(leafPositions, branches) {
|
|
610
|
+
if (leafPositions.length > 0) return fitEllipsoid(leafPositions);
|
|
611
|
+
const points = branches.flatMap((b) => b.points.map((p) => p.position));
|
|
612
|
+
return points.length > 0 ? fitEllipsoid(Float64Array.from(points.flat())) : {
|
|
613
|
+
center: [...EMPTY.center],
|
|
614
|
+
radii: [...EMPTY.radii]
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* The smallest axis-aligned ellipsoid around `positions` (x, y, z triples), close to minimum volume.
|
|
945
619
|
*
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
620
|
+
* Frank-Wolfe on the dual (as in Khachiyan's algorithm, restricted to axis-aligned ellipsoids): weights
|
|
621
|
+
* on the points give a weighted mean `m` and per-axis variance `v`, and the ellipsoid `sum (p - m)² / (3v)
|
|
622
|
+
* <= 1`. Each step moves weight to the point furthest outside, or off the weighted point furthest inside
|
|
623
|
+
* (an away step), by an exact line search on `sum log v`.
|
|
624
|
+
* Converged, the furthest point lies on the shell; the ellipsoid is then scaled to contain every point
|
|
625
|
+
* exactly. Only + - * / and sqrt, no randomness: the same numbers on every machine.
|
|
949
626
|
*/
|
|
950
|
-
function
|
|
951
|
-
const
|
|
952
|
-
const
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
start,
|
|
961
|
-
end: start + BRANCH_DURATION
|
|
962
|
-
};
|
|
627
|
+
function fitEllipsoid(positions) {
|
|
628
|
+
const n = positions.length / D;
|
|
629
|
+
const u = new Float64Array(n);
|
|
630
|
+
const core = /* @__PURE__ */ new Set();
|
|
631
|
+
for (let i = 0; i < D; i++) {
|
|
632
|
+
let lo = 0;
|
|
633
|
+
let hi = 0;
|
|
634
|
+
for (let j = 1; j < n; j++) {
|
|
635
|
+
if (positions[j * D + i] < positions[lo * D + i]) lo = j;
|
|
636
|
+
if (positions[j * D + i] > positions[hi * D + i]) hi = j;
|
|
963
637
|
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
const
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
const
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
638
|
+
core.add(lo).add(hi);
|
|
639
|
+
}
|
|
640
|
+
for (const j of core) u[j] = 1 / core.size;
|
|
641
|
+
const m = [
|
|
642
|
+
0,
|
|
643
|
+
0,
|
|
644
|
+
0
|
|
645
|
+
];
|
|
646
|
+
const q = [
|
|
647
|
+
0,
|
|
648
|
+
0,
|
|
649
|
+
0
|
|
650
|
+
];
|
|
651
|
+
const v = [
|
|
652
|
+
0,
|
|
653
|
+
0,
|
|
654
|
+
0
|
|
655
|
+
];
|
|
656
|
+
const p = [
|
|
657
|
+
0,
|
|
658
|
+
0,
|
|
659
|
+
0
|
|
660
|
+
];
|
|
661
|
+
const at = (j) => {
|
|
662
|
+
p[0] = positions[j * D];
|
|
663
|
+
p[1] = positions[j * D + 1];
|
|
664
|
+
p[2] = positions[j * D + 2];
|
|
665
|
+
return p;
|
|
666
|
+
};
|
|
667
|
+
for (const j of core) {
|
|
668
|
+
at(j);
|
|
669
|
+
for (let i = 0; i < D; i++) {
|
|
670
|
+
m[i] += u[j] * p[i];
|
|
671
|
+
q[i] += u[j] * p[i] * p[i];
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
const floorOf = () => MIN_FLATNESS * MIN_FLATNESS * Math.max(q[0] - m[0] * m[0], q[1] - m[1] * m[1], q[2] - m[2] * m[2], 1e-12);
|
|
675
|
+
let floor = floorOf();
|
|
676
|
+
const variances = () => {
|
|
677
|
+
for (let i = 0; i < D; i++) v[i] = Math.max(q[i] - m[i] * m[i], floor);
|
|
678
|
+
};
|
|
679
|
+
/**
|
|
680
|
+
* Scores every point (its squared distance in units of sqrt(v) per axis): the furthest point of all,
|
|
681
|
+
* and the nearest point that carries weight. Returns [furthest, its score, nearest, its score].
|
|
682
|
+
*/
|
|
683
|
+
const extremes = () => {
|
|
684
|
+
const [m0, m1, m2] = m;
|
|
685
|
+
const w0 = 1 / v[0];
|
|
686
|
+
const w1 = 1 / v[1];
|
|
687
|
+
const w2 = 1 / v[2];
|
|
688
|
+
let far = 0;
|
|
689
|
+
let farScore = -1;
|
|
690
|
+
let near = -1;
|
|
691
|
+
let nearScore = Infinity;
|
|
692
|
+
for (let j = 0, o = 0; j < n; j++, o += D) {
|
|
693
|
+
const x = positions[o] - m0;
|
|
694
|
+
const y = positions[o + 1] - m1;
|
|
695
|
+
const z = positions[o + 2] - m2;
|
|
696
|
+
const score = x * x * w0 + y * y * w1 + z * z * w2;
|
|
697
|
+
if (score > farScore) {
|
|
698
|
+
farScore = score;
|
|
699
|
+
far = j;
|
|
700
|
+
}
|
|
701
|
+
if (score < nearScore && u[j] > 0) {
|
|
702
|
+
nearScore = score;
|
|
703
|
+
near = j;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return [
|
|
707
|
+
far,
|
|
708
|
+
farScore,
|
|
709
|
+
near,
|
|
710
|
+
nearScore
|
|
711
|
+
];
|
|
984
712
|
};
|
|
713
|
+
variances();
|
|
714
|
+
let [far, score, near, nearScore] = extremes();
|
|
715
|
+
for (let step = 0; step < MAX_STEPS && score > D * 1.01; step++) {
|
|
716
|
+
const away = D - nearScore > score - D && u[near] < 1;
|
|
717
|
+
const j = away ? near : far;
|
|
718
|
+
at(j);
|
|
719
|
+
const drop = -u[j] / (1 - u[j]);
|
|
720
|
+
const a = away ? lineSearch(m, q, p, floor, drop, 0) : lineSearch(m, q, p, floor, 0, 1);
|
|
721
|
+
const weight = a === drop ? 0 : (1 - a) * u[j] + a;
|
|
722
|
+
for (let k = 0; k < n; k++) u[k] *= 1 - a;
|
|
723
|
+
u[j] = weight;
|
|
724
|
+
for (let i = 0; i < D; i++) {
|
|
725
|
+
m[i] = (1 - a) * m[i] + a * p[i];
|
|
726
|
+
q[i] = (1 - a) * q[i] + a * p[i] * p[i];
|
|
727
|
+
}
|
|
728
|
+
floor = floorOf();
|
|
729
|
+
variances();
|
|
730
|
+
[far, score, near, nearScore] = extremes();
|
|
731
|
+
}
|
|
985
732
|
return {
|
|
986
|
-
|
|
987
|
-
|
|
733
|
+
center: [
|
|
734
|
+
m[0],
|
|
735
|
+
m[1],
|
|
736
|
+
m[2]
|
|
737
|
+
],
|
|
738
|
+
radii: [
|
|
739
|
+
Math.sqrt(v[0] * score),
|
|
740
|
+
Math.sqrt(v[1] * score),
|
|
741
|
+
Math.sqrt(v[2] * score)
|
|
742
|
+
]
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* The step `a` in `lo..hi` towards point `p` (away from it when negative) that maximizes `sum log v(a)`,
|
|
747
|
+
* where moving weight `a` to `p` gives `v(a) = (1-a) q + a p² - ((1-a) m + a p)²`. Concave in `a`, so its
|
|
748
|
+
* derivative `sum v'(a) / v(a)` falls through 0 at most once: bisect for it.
|
|
749
|
+
*/
|
|
750
|
+
function lineSearch(m, q, p, floor, lo, hi) {
|
|
751
|
+
const slope = (a) => {
|
|
752
|
+
let s = 0;
|
|
753
|
+
for (let i = 0; i < D; i++) {
|
|
754
|
+
const mean = (1 - a) * m[i] + a * p[i];
|
|
755
|
+
const variance = Math.max((1 - a) * q[i] + a * p[i] * p[i] - mean * mean, floor);
|
|
756
|
+
s += (p[i] * p[i] - q[i] - 2 * mean * (p[i] - m[i])) / variance;
|
|
757
|
+
}
|
|
758
|
+
return s;
|
|
988
759
|
};
|
|
760
|
+
if (slope(lo) <= 0) return lo;
|
|
761
|
+
if (slope(hi) >= 0) return hi;
|
|
762
|
+
for (let k = 0; k < 40; k++) {
|
|
763
|
+
const mid = (lo + hi) / 2;
|
|
764
|
+
if (slope(mid) > 0) lo = mid;
|
|
765
|
+
else hi = mid;
|
|
766
|
+
}
|
|
767
|
+
return (lo + hi) / 2;
|
|
989
768
|
}
|
|
990
769
|
// src/generator/wind.ts
|
|
991
770
|
var NO_SWAY = {
|
|
@@ -1144,7 +923,7 @@ function clumpTemplate() {
|
|
|
1144
923
|
0,
|
|
1145
924
|
1
|
|
1146
925
|
]
|
|
1147
|
-
].map((v) => normalize(v));
|
|
926
|
+
].map((v) => normalize$1(v));
|
|
1148
927
|
const centre = [
|
|
1149
928
|
0,
|
|
1150
929
|
0,
|
|
@@ -1275,7 +1054,7 @@ function puffTemplate() {
|
|
|
1275
1054
|
const key = a < b ? `${a}-${b}` : `${b}-${a}`;
|
|
1276
1055
|
let index = midpoints.get(key);
|
|
1277
1056
|
if (index === void 0) {
|
|
1278
|
-
index = normals.push(normalize(add(normals[a], normals[b]))) - 1;
|
|
1057
|
+
index = normals.push(normalize$1(add(normals[a], normals[b]))) - 1;
|
|
1279
1058
|
midpoints.set(key, index);
|
|
1280
1059
|
}
|
|
1281
1060
|
return index;
|
|
@@ -1400,38 +1179,45 @@ function orient(template) {
|
|
|
1400
1179
|
* every vertex carries the leaf's tint as its colour and the leaf's part attributes.
|
|
1401
1180
|
*/
|
|
1402
1181
|
function solidLeafGeometry(leaves, shape, level, seed, variation = { season: 0 }, growth = [], branchSway = /* @__PURE__ */ new Map()) {
|
|
1403
|
-
|
|
1182
|
+
return solidGeometry(TEMPLATES[shape], leaves, (leaf, l) => ({
|
|
1183
|
+
level,
|
|
1184
|
+
attach: leaf.attach,
|
|
1185
|
+
random: seedRandom(seed, leaf.id),
|
|
1186
|
+
origin: leaf.position,
|
|
1187
|
+
season: leafSeasonAttribute(seed, variation, leaf.id),
|
|
1188
|
+
growth: growth[l] ?? {
|
|
1189
|
+
start: 0,
|
|
1190
|
+
end: 0
|
|
1191
|
+
},
|
|
1192
|
+
sway: leafSway(branchSway.get(leaf.branch) ?? NO_SWAY, leaf.attach)
|
|
1193
|
+
}));
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* One small closed mesh per placement: the template in the placement's frame
|
|
1197
|
+
* (across, normal, direction), scaled by its size, every vertex carrying the placement's tint
|
|
1198
|
+
* as its colour and its part attributes (`part`).
|
|
1199
|
+
*/
|
|
1200
|
+
function solidGeometry(template, placements, part) {
|
|
1404
1201
|
const n = template.positions.length;
|
|
1405
|
-
const positions = new Float32Array(
|
|
1406
|
-
const normals = new Float32Array(
|
|
1407
|
-
const uvs = new Float32Array(
|
|
1408
|
-
const colors = new Float32Array(
|
|
1409
|
-
const indices = new Uint32Array(
|
|
1410
|
-
const attributes = createAttributes(
|
|
1411
|
-
|
|
1412
|
-
const side = cross$1(
|
|
1413
|
-
const toWorld = ([x, y, z]) => add(add(scale(side, x), scale(
|
|
1414
|
-
const
|
|
1415
|
-
level,
|
|
1416
|
-
attach: leaf.attach,
|
|
1417
|
-
random: seedRandom(seed, leaf.id),
|
|
1418
|
-
origin: leaf.position,
|
|
1419
|
-
season: leafSeasonAttribute(seed, variation, leaf.id),
|
|
1420
|
-
growth: growth[l] ?? {
|
|
1421
|
-
start: 0,
|
|
1422
|
-
end: 0
|
|
1423
|
-
},
|
|
1424
|
-
sway: leafSway(branchSway.get(leaf.branch) ?? NO_SWAY, leaf.attach)
|
|
1425
|
-
};
|
|
1202
|
+
const positions = new Float32Array(placements.length * n * 3);
|
|
1203
|
+
const normals = new Float32Array(placements.length * n * 3);
|
|
1204
|
+
const uvs = new Float32Array(placements.length * n * 2);
|
|
1205
|
+
const colors = new Float32Array(placements.length * n * 3);
|
|
1206
|
+
const indices = new Uint32Array(placements.length * template.triangles.length * 3);
|
|
1207
|
+
const attributes = createAttributes(placements.length * n);
|
|
1208
|
+
placements.forEach((placement, l) => {
|
|
1209
|
+
const side = cross$1(placement.normal, placement.direction);
|
|
1210
|
+
const toWorld = ([x, y, z]) => add(add(scale(side, x), scale(placement.normal, y)), scale(placement.direction, z));
|
|
1211
|
+
const info = part(placement, l);
|
|
1426
1212
|
for (let c = 0; c < n; c++) {
|
|
1427
1213
|
const v = l * n + c;
|
|
1428
1214
|
const local = template.positions[c];
|
|
1429
|
-
positions.set(add(
|
|
1430
|
-
normals.set(normalize(toWorld(template.normals[c])), v * 3);
|
|
1215
|
+
positions.set(add(placement.position, scale(toWorld(local), placement.size)), v * 3);
|
|
1216
|
+
normals.set(normalize$1(toWorld(template.normals[c])), v * 3);
|
|
1431
1217
|
const along = Math.min(Math.max(local[2], 0), 1);
|
|
1432
1218
|
uvs.set([local[0] + .5, along], v * 2);
|
|
1433
|
-
colors.set(
|
|
1434
|
-
writePart(attributes, v,
|
|
1219
|
+
colors.set(placement.tint, v * 3);
|
|
1220
|
+
writePart(attributes, v, info, along);
|
|
1435
1221
|
}
|
|
1436
1222
|
template.triangles.forEach((triangle, t) => {
|
|
1437
1223
|
indices.set(triangle.map((i) => l * n + i), (l * template.triangles.length + t) * 3);
|
|
@@ -1446,19 +1232,24 @@ function solidLeafGeometry(leaves, shape, level, seed, variation = { season: 0 }
|
|
|
1446
1232
|
attributes
|
|
1447
1233
|
};
|
|
1448
1234
|
}
|
|
1449
|
-
// src/generator/
|
|
1450
|
-
var DEG = Math.PI / 180;
|
|
1451
|
-
/**
|
|
1452
|
-
var
|
|
1235
|
+
// src/generator/fruit.ts
|
|
1236
|
+
var DEG$3 = Math.PI / 180;
|
|
1237
|
+
/** Angle between twig and Fruit when it does not hang (`hang` 0), degrees. */
|
|
1238
|
+
var FRUIT_TILT = 60;
|
|
1239
|
+
/** Fruit sits on the outer part of its twig: from this fraction of the twig length to its tip. */
|
|
1240
|
+
var FRUIT_START = .3;
|
|
1241
|
+
/** Fruit starts to grow no earlier than this Growth (it only shows on a fully grown tree) ... */
|
|
1242
|
+
var FRUIT_GROWTH_START = .9;
|
|
1243
|
+
/** ... plus up to this much per Fruit (from the Seed). */
|
|
1244
|
+
var FRUIT_GROWTH_JITTER = .05;
|
|
1453
1245
|
/**
|
|
1454
|
-
* Places
|
|
1455
|
-
*
|
|
1456
|
-
*
|
|
1457
|
-
* `random.fork('tint')`. The shape ('card' here, 'clump'/'needle'/'puff' in `leaf-shapes.ts`) only affects the mesh.
|
|
1246
|
+
* Places Fruit on the outermost branches (the trunk if there are no branch levels), `fruit.count`
|
|
1247
|
+
* per branch (a fraction = a chance of one more). `random` is the Generator's `fork('fruit')`: each Fruit draws from its own
|
|
1248
|
+
* `random.fork(fruitId)`, so Fruit never shifts the branches or leaves (ADR 0006).
|
|
1458
1249
|
*/
|
|
1459
|
-
function
|
|
1460
|
-
const {
|
|
1461
|
-
if (
|
|
1250
|
+
function placeFruit(definition, branches, random) {
|
|
1251
|
+
const { fruit } = definition;
|
|
1252
|
+
if (!fruit || fruit.count === 0) return [];
|
|
1462
1253
|
const outermost = definition.branches.length;
|
|
1463
1254
|
const half = definition.variation.color / 2;
|
|
1464
1255
|
const tintRandom = random.fork("tint");
|
|
@@ -1467,28 +1258,36 @@ function placeLeaves(definition, branches, random) {
|
|
|
1467
1258
|
1,
|
|
1468
1259
|
2
|
|
1469
1260
|
].map(() => tintRandom.range(-half, half));
|
|
1470
|
-
const tilt =
|
|
1261
|
+
const tilt = FRUIT_TILT * DEG$3;
|
|
1262
|
+
const down = [
|
|
1263
|
+
0,
|
|
1264
|
+
-1,
|
|
1265
|
+
0
|
|
1266
|
+
];
|
|
1267
|
+
const whole = Math.floor(fruit.count);
|
|
1268
|
+
const chance = fruit.count - whole;
|
|
1471
1269
|
const out = [];
|
|
1472
1270
|
for (const branch of branches) {
|
|
1473
1271
|
if (branch.level !== outermost) continue;
|
|
1272
|
+
const count = whole + (chance > 0 && random.fork(`${branch.path}/chance`).next() < chance ? 1 : 0);
|
|
1273
|
+
if (count === 0) continue;
|
|
1474
1274
|
const frames = branchFrames(branch);
|
|
1475
|
-
for (let i = 0; i <
|
|
1275
|
+
for (let i = 0; i < count; i++) {
|
|
1476
1276
|
const id = `${branch.path}/${i}`;
|
|
1477
1277
|
const r = random.fork(id);
|
|
1478
|
-
const t =
|
|
1278
|
+
const t = FRUIT_START + .7 * (i + r.range(.1, .9)) / count;
|
|
1479
1279
|
const at = sampleBranch(branch, frames, t);
|
|
1480
1280
|
const azimuth = r.range(0, Math.PI * 2);
|
|
1481
1281
|
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
1482
|
-
const
|
|
1282
|
+
const hanging = lerp(normalize$1(add(scale(at.tangent, cos(tilt)), scale(across, sin$1(tilt)))), down, fruit.hang);
|
|
1283
|
+
const direction = length$1(hanging) > .001 ? normalize$1(hanging) : across;
|
|
1483
1284
|
const side0 = cross$1(direction, [
|
|
1484
1285
|
0,
|
|
1485
1286
|
1,
|
|
1486
1287
|
0
|
|
1487
1288
|
]);
|
|
1488
|
-
const
|
|
1489
|
-
const
|
|
1490
|
-
const normal = normalize(add(scale(cross$1(side, direction), cos(roll)), scale(side, sin$1(roll))));
|
|
1491
|
-
const size = leaves.size * (1 + r.range(-leaves.sizeVariation, leaves.sizeVariation));
|
|
1289
|
+
const normal = dot$1(side0, side0) > 1e-6 ? normalize$1(cross$1(normalize$1(side0), direction)) : perpendicular(direction);
|
|
1290
|
+
const size = fruit.size * (1 + r.range(-fruit.sizeVariation, fruit.sizeVariation));
|
|
1492
1291
|
const brightness = r.range(-half, half);
|
|
1493
1292
|
const tint = treeTint.map((c) => 1 + c + brightness);
|
|
1494
1293
|
out.push({
|
|
@@ -1506,40 +1305,431 @@ function placeLeaves(definition, branches, random) {
|
|
|
1506
1305
|
}
|
|
1507
1306
|
return out;
|
|
1508
1307
|
}
|
|
1509
|
-
/**
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
.
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1308
|
+
/**
|
|
1309
|
+
* When each Fruit grows: only at the very end of Growth (from about {@link FRUIT_GROWTH_START},
|
|
1310
|
+
* never before its twig is fully grown), so a sapling bears none. Computed apart from the other
|
|
1311
|
+
* parts' timing, so Fruit never shifts the Growth of wood or leaves.
|
|
1312
|
+
*/
|
|
1313
|
+
function fruitGrowth(seed, fruit, twigGrowth) {
|
|
1314
|
+
return fruit.map((f) => {
|
|
1315
|
+
const from = FRUIT_GROWTH_START + seedRandom(seed, `growth/fruit/${f.id}`) * FRUIT_GROWTH_JITTER;
|
|
1316
|
+
return {
|
|
1317
|
+
start: Math.min(Math.max(from, twigGrowth.get(f.branch)?.end ?? 0), 1),
|
|
1318
|
+
end: 1
|
|
1319
|
+
};
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Fruit geometry: one small solid per Fruit from its Fruit Type's template, hanging from its
|
|
1324
|
+
* attachment point. Vertex colours carry the Seed's tint; `aSeason` = (timing shift, fruit rank, 0);
|
|
1325
|
+
* it sways with its twig at the attachment point (no leaf flutter).
|
|
1326
|
+
*/
|
|
1327
|
+
function fruitGeometry(fruit, { type, sides }, level, seed, variation, growth, branchSway) {
|
|
1328
|
+
if (fruit.length === 0) return emptyGeometry();
|
|
1329
|
+
return solidGeometry(fruitTemplate(type, sides), fruit, (f, i) => {
|
|
1330
|
+
const twig = branchSway.get(f.branch) ?? NO_SWAY;
|
|
1331
|
+
return {
|
|
1332
|
+
level,
|
|
1333
|
+
attach: f.attach,
|
|
1334
|
+
random: seedRandom(seed, `fruit/${f.id}`),
|
|
1335
|
+
origin: f.position,
|
|
1336
|
+
season: fruitSeasonAttribute(seed, variation, f.id),
|
|
1337
|
+
growth: growth[i] ?? {
|
|
1338
|
+
start: 0,
|
|
1339
|
+
end: 0
|
|
1340
|
+
},
|
|
1341
|
+
sway: {
|
|
1342
|
+
base: twig.base + twig.gain * f.attach,
|
|
1343
|
+
gain: 0,
|
|
1344
|
+
flutter: false
|
|
1345
|
+
}
|
|
1346
|
+
};
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
/** Empty geometry buffers (a tree without Fruit). */
|
|
1350
|
+
function emptyGeometry() {
|
|
1351
|
+
return {
|
|
1352
|
+
positions: /* @__PURE__ */ new Float32Array(0),
|
|
1353
|
+
normals: /* @__PURE__ */ new Float32Array(0),
|
|
1354
|
+
uvs: /* @__PURE__ */ new Float32Array(0),
|
|
1355
|
+
colors: /* @__PURE__ */ new Float32Array(0),
|
|
1356
|
+
indices: /* @__PURE__ */ new Uint32Array(0),
|
|
1357
|
+
attributes: createAttributes(0)
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
/** A solid of revolution around z: rings of `sides` vertices at (z, radius), closed by a point at each end. */
|
|
1361
|
+
function lathe(profile, sides, ends, twist = 0) {
|
|
1362
|
+
const positions = [[
|
|
1363
|
+
0,
|
|
1364
|
+
0,
|
|
1365
|
+
ends[0]
|
|
1366
|
+
], [
|
|
1367
|
+
0,
|
|
1368
|
+
0,
|
|
1369
|
+
ends[1]
|
|
1370
|
+
]];
|
|
1371
|
+
const normals = [[
|
|
1372
|
+
0,
|
|
1373
|
+
0,
|
|
1374
|
+
-1
|
|
1375
|
+
], [
|
|
1376
|
+
0,
|
|
1377
|
+
0,
|
|
1378
|
+
1
|
|
1379
|
+
]];
|
|
1380
|
+
const triangles = [];
|
|
1381
|
+
profile.forEach(([z, r], ring) => {
|
|
1382
|
+
for (let s = 0; s < sides; s++) {
|
|
1383
|
+
const angle = (s + ring * twist) / sides * Math.PI * 2;
|
|
1384
|
+
const radial = [
|
|
1385
|
+
cos(angle),
|
|
1386
|
+
sin$1(angle),
|
|
1387
|
+
0
|
|
1388
|
+
];
|
|
1389
|
+
positions.push([
|
|
1390
|
+
radial[0] * r,
|
|
1391
|
+
radial[1] * r,
|
|
1392
|
+
z
|
|
1393
|
+
]);
|
|
1394
|
+
normals.push(radial);
|
|
1395
|
+
}
|
|
1396
|
+
});
|
|
1397
|
+
const at = (ring, s) => 2 + ring * sides + s % sides;
|
|
1398
|
+
for (let s = 0; s < sides; s++) {
|
|
1399
|
+
triangles.push([
|
|
1400
|
+
0,
|
|
1401
|
+
at(0, s),
|
|
1402
|
+
at(0, s + 1)
|
|
1403
|
+
]);
|
|
1404
|
+
for (let ring = 0; ring < profile.length - 1; ring++) triangles.push([
|
|
1405
|
+
at(ring, s),
|
|
1406
|
+
at(ring + 1, s),
|
|
1407
|
+
at(ring + 1, s + 1)
|
|
1408
|
+
], [
|
|
1409
|
+
at(ring, s),
|
|
1410
|
+
at(ring + 1, s + 1),
|
|
1411
|
+
at(ring, s + 1)
|
|
1412
|
+
]);
|
|
1413
|
+
triangles.push([
|
|
1414
|
+
1,
|
|
1415
|
+
at(profile.length - 1, s + 1),
|
|
1416
|
+
at(profile.length - 1, s)
|
|
1417
|
+
]);
|
|
1418
|
+
}
|
|
1419
|
+
return orient({
|
|
1420
|
+
positions,
|
|
1421
|
+
normals,
|
|
1422
|
+
triangles
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
/** Chestnut husk: a ball (icosphere, diameter 0.8) with a spike at each of the icosahedron's 12 corners. */
|
|
1426
|
+
function nutTemplate() {
|
|
1427
|
+
const ico = clumpTemplate();
|
|
1428
|
+
const centre = [
|
|
1429
|
+
0,
|
|
1430
|
+
0,
|
|
1431
|
+
.5
|
|
1432
|
+
];
|
|
1433
|
+
const directions = ico.positions.map((p) => normalize$1([
|
|
1434
|
+
p[0],
|
|
1435
|
+
p[1],
|
|
1436
|
+
p[2] - .5
|
|
1437
|
+
]));
|
|
1438
|
+
const midpoints = /* @__PURE__ */ new Map();
|
|
1439
|
+
const midpoint = (a, b) => {
|
|
1440
|
+
const key = a < b ? `${a}-${b}` : `${b}-${a}`;
|
|
1441
|
+
let index = midpoints.get(key);
|
|
1442
|
+
if (index === void 0) {
|
|
1443
|
+
index = directions.push(normalize$1(add(directions[a], directions[b]))) - 1;
|
|
1444
|
+
midpoints.set(key, index);
|
|
1445
|
+
}
|
|
1446
|
+
return index;
|
|
1447
|
+
};
|
|
1448
|
+
const triangles = ico.triangles.flatMap(([a, b, c]) => {
|
|
1449
|
+
const [ab, bc, ca] = [
|
|
1450
|
+
midpoint(a, b),
|
|
1451
|
+
midpoint(b, c),
|
|
1452
|
+
midpoint(c, a)
|
|
1453
|
+
];
|
|
1454
|
+
return [
|
|
1455
|
+
[
|
|
1456
|
+
a,
|
|
1457
|
+
ab,
|
|
1458
|
+
ca
|
|
1459
|
+
],
|
|
1460
|
+
[
|
|
1461
|
+
b,
|
|
1462
|
+
bc,
|
|
1463
|
+
ab
|
|
1464
|
+
],
|
|
1465
|
+
[
|
|
1466
|
+
c,
|
|
1467
|
+
ca,
|
|
1468
|
+
bc
|
|
1469
|
+
],
|
|
1470
|
+
[
|
|
1471
|
+
ab,
|
|
1472
|
+
bc,
|
|
1473
|
+
ca
|
|
1474
|
+
]
|
|
1475
|
+
];
|
|
1476
|
+
});
|
|
1477
|
+
return orient({
|
|
1478
|
+
positions: directions.map((d, i) => add(centre, scale(d, i < 12 ? .5 : .36))),
|
|
1479
|
+
normals: directions,
|
|
1480
|
+
triangles
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
/** Each Fruit Type's smooth profile (z, radius) along its axis, and its end points. */
|
|
1484
|
+
var PROFILES = {
|
|
1485
|
+
round: {
|
|
1486
|
+
rings: [
|
|
1487
|
+
[.04, .22],
|
|
1488
|
+
[.2, .44],
|
|
1489
|
+
[.45, .5],
|
|
1490
|
+
[.7, .45],
|
|
1491
|
+
[.9, .28]
|
|
1492
|
+
],
|
|
1493
|
+
ends: [.1, 1]
|
|
1494
|
+
},
|
|
1495
|
+
pear: {
|
|
1496
|
+
rings: [
|
|
1497
|
+
[.08, .1],
|
|
1498
|
+
[.25, .18],
|
|
1499
|
+
[.45, .3],
|
|
1500
|
+
[.65, .4],
|
|
1501
|
+
[.82, .38],
|
|
1502
|
+
[.95, .2]
|
|
1503
|
+
],
|
|
1504
|
+
ends: [0, 1]
|
|
1505
|
+
},
|
|
1506
|
+
cone: {
|
|
1507
|
+
rings: [
|
|
1508
|
+
[.08, .2],
|
|
1509
|
+
[.22, .32],
|
|
1510
|
+
[.38, .36],
|
|
1511
|
+
[.54, .34],
|
|
1512
|
+
[.7, .28],
|
|
1513
|
+
[.86, .18]
|
|
1514
|
+
],
|
|
1515
|
+
ends: [0, 1],
|
|
1516
|
+
twist: .5
|
|
1517
|
+
},
|
|
1518
|
+
berry: {
|
|
1519
|
+
rings: [
|
|
1520
|
+
[.15, .36],
|
|
1521
|
+
[.5, .5],
|
|
1522
|
+
[.85, .36]
|
|
1523
|
+
],
|
|
1524
|
+
ends: [0, 1]
|
|
1525
|
+
}
|
|
1526
|
+
};
|
|
1527
|
+
/** Coarse profiles (low detail): two rings (berry one), a few flat facets. */
|
|
1528
|
+
var COARSE = {
|
|
1529
|
+
round: [[.28, .48], [.72, .42]],
|
|
1530
|
+
pear: [[.3, .2], [.75, .4]],
|
|
1531
|
+
cone: [[.25, .32], [.65, .28]],
|
|
1532
|
+
berry: [[.5, .5]]
|
|
1533
|
+
};
|
|
1534
|
+
/** At or below this many sides, Fruit uses the coarse profiles (and the nut a plain icosahedron). */
|
|
1535
|
+
var COARSE_SIDES = 5;
|
|
1536
|
+
/** The default templates: each Fruit Type's own sides (Definitions without `fruit.sides`). */
|
|
1537
|
+
var DEFAULT_SIDES = {
|
|
1538
|
+
round: 8,
|
|
1539
|
+
pear: 8,
|
|
1540
|
+
cone: 7,
|
|
1541
|
+
berry: 6
|
|
1542
|
+
};
|
|
1543
|
+
var templates = /* @__PURE__ */ new Map();
|
|
1544
|
+
/** The template of a Fruit Type drawn with `sides` faces around (absent = the Type's own detail), cached. */
|
|
1545
|
+
function fruitTemplate(type, sides) {
|
|
1546
|
+
const key = `${type}/${sides ?? ""}`;
|
|
1547
|
+
let template = templates.get(key);
|
|
1548
|
+
if (!template) {
|
|
1549
|
+
const coarse = sides !== void 0 && sides <= COARSE_SIDES;
|
|
1550
|
+
if (type === "nut") template = coarse ? coarseNutTemplate() : nutTemplate();
|
|
1551
|
+
else {
|
|
1552
|
+
const { rings, ends, twist } = PROFILES[type];
|
|
1553
|
+
template = lathe(coarse ? COARSE[type] : rings, sides ?? DEFAULT_SIDES[type], ends, twist);
|
|
1554
|
+
}
|
|
1555
|
+
templates.set(key, template);
|
|
1556
|
+
}
|
|
1557
|
+
return template;
|
|
1558
|
+
}
|
|
1559
|
+
/** Low-detail chestnut husk: an icosahedron (its corners are the spikes). */
|
|
1560
|
+
function coarseNutTemplate() {
|
|
1561
|
+
const ico = clumpTemplate();
|
|
1562
|
+
const directions = ico.positions.map((p) => normalize$1([
|
|
1563
|
+
p[0],
|
|
1564
|
+
p[1],
|
|
1565
|
+
p[2] - .5
|
|
1566
|
+
]));
|
|
1567
|
+
return orient({
|
|
1568
|
+
positions: directions.map((d) => add([
|
|
1569
|
+
0,
|
|
1570
|
+
0,
|
|
1571
|
+
.5
|
|
1572
|
+
], scale(d, .45))),
|
|
1573
|
+
normals: directions,
|
|
1574
|
+
triangles: ico.triangles
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
// src/generator/growth.ts
|
|
1578
|
+
/** Delay of a branch after its parent is grown, per unit of `attach` (lower branches sprout first). */
|
|
1579
|
+
var BRANCH_STAGGER = .6;
|
|
1580
|
+
/** Time a branch takes from sprouting to full size. */
|
|
1581
|
+
var BRANCH_DURATION = 1;
|
|
1582
|
+
/** Leaves wait this long after their branch is grown ... */
|
|
1583
|
+
var LEAF_DELAY = .3;
|
|
1584
|
+
/** ... plus this much per unit of `attach` (leaves near the base first) ... */
|
|
1585
|
+
var LEAF_STAGGER = .5;
|
|
1586
|
+
/** ... and take this long to unfold. */
|
|
1587
|
+
var LEAF_DURATION = .6;
|
|
1588
|
+
/**
|
|
1589
|
+
* When each part grows, as Growth windows (0..1) parallel to the skeleton arrays.
|
|
1590
|
+
*
|
|
1591
|
+
* Order: the trunk is there from Growth 0 (it only scales with the whole tree); a branch sprouts
|
|
1592
|
+
* once its parent is fully grown, lower branches first; leaves come after their branch. So a part
|
|
1593
|
+
* always sprouts from a fully-grown parent and never floats. The last part finishes at Growth 1.
|
|
1594
|
+
*
|
|
1595
|
+
* Seed (within `variation.growth` = v): each part's start is delayed by up to v/2 of its own window
|
|
1596
|
+
* (its end stays put, so the order above holds), and the whole timeline is warped by `t^k`,
|
|
1597
|
+
* k in 1 ± v (a monotone map that keeps 0 and 1). Together a window moves by less than v.
|
|
1598
|
+
*/
|
|
1599
|
+
function growthTiming(definition, seed, branches, leaves) {
|
|
1600
|
+
const raw = /* @__PURE__ */ new Map();
|
|
1601
|
+
const branchWindows = branches.map((branch) => {
|
|
1602
|
+
let window = {
|
|
1603
|
+
start: 0,
|
|
1604
|
+
end: 0
|
|
1605
|
+
};
|
|
1606
|
+
if (branch.parent !== null) {
|
|
1607
|
+
const start = raw.get(branch.parent).end + branch.attach * BRANCH_STAGGER;
|
|
1608
|
+
window = {
|
|
1609
|
+
start,
|
|
1610
|
+
end: start + BRANCH_DURATION
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
raw.set(branch.path, window);
|
|
1614
|
+
return window;
|
|
1615
|
+
});
|
|
1616
|
+
const leafWindows = leaves.map((leaf) => {
|
|
1617
|
+
const start = raw.get(leaf.branch).end + LEAF_DELAY + leaf.attach * LEAF_STAGGER;
|
|
1618
|
+
return {
|
|
1619
|
+
start,
|
|
1620
|
+
end: start + LEAF_DURATION
|
|
1621
|
+
};
|
|
1622
|
+
});
|
|
1623
|
+
const total = Math.max(0, ...branchWindows.map((w) => w.end), ...leafWindows.map((w) => w.end));
|
|
1624
|
+
const v = definition.variation.growth;
|
|
1625
|
+
const k = 1 + (2 * seedRandom(seed, "growth") - 1) * v;
|
|
1626
|
+
const warp = (t) => total > 0 ? pow(Math.min(1, t / total), k) : 0;
|
|
1627
|
+
const finish = (w, key) => {
|
|
1628
|
+
const delay = seedRandom(seed, `growth/${key}`) * (v / 2) * (w.end - w.start);
|
|
1629
|
+
return {
|
|
1630
|
+
start: warp(w.start + delay),
|
|
1631
|
+
end: warp(w.end)
|
|
1632
|
+
};
|
|
1633
|
+
};
|
|
1634
|
+
return {
|
|
1635
|
+
branches: branchWindows.map((w, i) => finish(w, branches[i].path)),
|
|
1636
|
+
leaves: leafWindows.map((w, i) => finish(w, leaves[i].id))
|
|
1637
|
+
};
|
|
1638
|
+
}
|
|
1639
|
+
// src/generator/leaves.ts
|
|
1640
|
+
var DEG$2 = Math.PI / 180;
|
|
1641
|
+
/** Leaf card width as a fraction of its length. */
|
|
1642
|
+
var LEAF_WIDTH = .5;
|
|
1643
|
+
/**
|
|
1644
|
+
* Places leaves on the outermost branches (the trunk if there are no branch levels).
|
|
1645
|
+
* Each leaf draws from `random.fork(leafId)`, so leaves never shift the branches and
|
|
1646
|
+
* one leaf never shifts another. The colour tint shared by the whole tree comes from
|
|
1647
|
+
* `random.fork('tint')`. The shape ('card' here, 'clump'/'needle'/'puff' in `leaf-shapes.ts`) only affects the mesh.
|
|
1648
|
+
*/
|
|
1649
|
+
function placeLeaves(definition, branches, random) {
|
|
1650
|
+
const { leaves } = definition;
|
|
1651
|
+
if (leaves.count === 0) return [];
|
|
1652
|
+
const outermost = definition.branches.length;
|
|
1653
|
+
const half = definition.variation.color / 2;
|
|
1654
|
+
const tintRandom = random.fork("tint");
|
|
1655
|
+
const treeTint = [
|
|
1656
|
+
0,
|
|
1657
|
+
1,
|
|
1658
|
+
2
|
|
1659
|
+
].map(() => tintRandom.range(-half, half));
|
|
1660
|
+
const tilt = Math.min(leaves.angle, 180) * DEG$2;
|
|
1661
|
+
const out = [];
|
|
1662
|
+
for (const branch of branches) {
|
|
1663
|
+
if (branch.level !== outermost) continue;
|
|
1664
|
+
const frames = branchFrames(branch);
|
|
1665
|
+
for (let i = 0; i < leaves.count; i++) {
|
|
1666
|
+
const id = `${branch.path}/${i}`;
|
|
1667
|
+
const r = random.fork(id);
|
|
1668
|
+
const t = leaves.start + (1 - leaves.start) * (i + r.range(.1, .9)) / leaves.count;
|
|
1669
|
+
const at = sampleBranch(branch, frames, t);
|
|
1670
|
+
const azimuth = r.range(0, Math.PI * 2);
|
|
1671
|
+
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
1672
|
+
const direction = normalize$1(add(scale(at.tangent, cos(tilt)), scale(across, sin$1(tilt))));
|
|
1673
|
+
const side0 = cross$1(direction, [
|
|
1674
|
+
0,
|
|
1675
|
+
1,
|
|
1676
|
+
0
|
|
1677
|
+
]);
|
|
1678
|
+
const side = dot$1(side0, side0) > 1e-6 ? normalize$1(side0) : perpendicular(direction);
|
|
1679
|
+
const roll = r.range(-.4, .4);
|
|
1680
|
+
const normal = normalize$1(add(scale(cross$1(side, direction), cos(roll)), scale(side, sin$1(roll))));
|
|
1681
|
+
const size = leaves.size * (1 + r.range(-leaves.sizeVariation, leaves.sizeVariation));
|
|
1682
|
+
const brightness = r.range(-half, half);
|
|
1683
|
+
const tint = treeTint.map((c) => 1 + c + brightness);
|
|
1684
|
+
out.push({
|
|
1685
|
+
id,
|
|
1686
|
+
branch: branch.path,
|
|
1687
|
+
index: i,
|
|
1688
|
+
attach: t,
|
|
1689
|
+
position: at.position,
|
|
1690
|
+
direction,
|
|
1691
|
+
normal,
|
|
1692
|
+
size,
|
|
1693
|
+
tint
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return out;
|
|
1698
|
+
}
|
|
1699
|
+
/** One diamond-shaped card per leaf (4 vertices, 2 triangles); vertex colours carry the leaf's tint. */
|
|
1700
|
+
function leafGeometry(leaves, level, seed, variation = { season: 0 }, growth = [], branchSway = /* @__PURE__ */ new Map()) {
|
|
1701
|
+
const positions = new Float32Array(leaves.length * 4 * 3);
|
|
1702
|
+
const normals = new Float32Array(leaves.length * 4 * 3);
|
|
1703
|
+
const uvs = new Float32Array(leaves.length * 4 * 2);
|
|
1704
|
+
const colors = new Float32Array(leaves.length * 4 * 3);
|
|
1705
|
+
const indices = new Uint32Array(leaves.length * 6);
|
|
1706
|
+
const attributes = createAttributes(leaves.length * 4);
|
|
1707
|
+
leaves.forEach((leaf, l) => {
|
|
1708
|
+
const side = cross$1(leaf.direction, leaf.normal);
|
|
1709
|
+
const halfWidth = leaf.size * LEAF_WIDTH / 2;
|
|
1710
|
+
const mid = add(leaf.position, scale(leaf.direction, leaf.size * .4));
|
|
1711
|
+
const corners = [
|
|
1712
|
+
leaf.position,
|
|
1713
|
+
add(mid, scale(side, halfWidth)),
|
|
1714
|
+
add(leaf.position, scale(leaf.direction, leaf.size)),
|
|
1715
|
+
add(mid, scale(side, -halfWidth))
|
|
1716
|
+
];
|
|
1717
|
+
const cornerUvs = [
|
|
1718
|
+
.5,
|
|
1719
|
+
0,
|
|
1720
|
+
1,
|
|
1721
|
+
.4,
|
|
1722
|
+
.5,
|
|
1723
|
+
1,
|
|
1724
|
+
0,
|
|
1725
|
+
.4
|
|
1726
|
+
];
|
|
1727
|
+
const part = {
|
|
1728
|
+
level,
|
|
1729
|
+
attach: leaf.attach,
|
|
1730
|
+
random: seedRandom(seed, leaf.id),
|
|
1731
|
+
origin: leaf.position,
|
|
1732
|
+
season: leafSeasonAttribute(seed, variation, leaf.id),
|
|
1543
1733
|
growth: growth[l] ?? {
|
|
1544
1734
|
start: 0,
|
|
1545
1735
|
end: 0
|
|
@@ -1573,10 +1763,136 @@ function leafGeometry(leaves, level, seed, variation = { season: 0 }, growth = [
|
|
|
1573
1763
|
attributes
|
|
1574
1764
|
};
|
|
1575
1765
|
}
|
|
1766
|
+
// src/generator/trunk.ts
|
|
1767
|
+
var DEG$1 = Math.PI / 180;
|
|
1768
|
+
/** The leader: the part of a Stem above this fraction of its length is what trunk gravity bends. */
|
|
1769
|
+
var LEADER_START = 2 / 3;
|
|
1770
|
+
/** How far trunk `gravity` = ±1 turns the leader over its length, degrees. */
|
|
1771
|
+
var LEADER_BEND = 120;
|
|
1772
|
+
/** Seed range of each Stem's lean: ± this fraction of `spread`. */
|
|
1773
|
+
var SPREAD_VARIATION = .3;
|
|
1774
|
+
/** Seed range of each Stem's place around the ring: ± this fraction of the gap between Stems. */
|
|
1775
|
+
var RING_JITTER = .25;
|
|
1776
|
+
/** Radius of the ring the Stems stand in, as a fraction of the trunk radius (several Stems only). */
|
|
1777
|
+
var RING_RADIUS = 1;
|
|
1778
|
+
/** How far a Stem's base ring sinks straight down below the ground, as a fraction of the trunk radius (no seam on uneven terrain). */
|
|
1779
|
+
var BASE_SINK = .1;
|
|
1780
|
+
/** Root Flare zone: the bottom fraction of a Stem's length (measured along it, after Sculpts) that widens. */
|
|
1781
|
+
var FLARE_ZONE = .12;
|
|
1782
|
+
/** Root Flare width: at `flare` = 1 the base ring is (1 + this) × the trunk radius. */
|
|
1783
|
+
var FLARE_WIDTH = .8;
|
|
1784
|
+
/**
|
|
1785
|
+
* Builds the Stems: `trunk.stems` full trunks standing in a small ring at the base, paths "0", "1", ...
|
|
1786
|
+
*
|
|
1787
|
+
* Path stability (ADR 0003): Stem 0's centre line draws from `random.fork('trunk')` exactly as a
|
|
1788
|
+
* single trunk always did, Stem i > 0 from `random.fork('trunk/<i>')`; the ring and the leans come
|
|
1789
|
+
* from `random.fork('stems')`. So a single upright Stem without gravity is today's trunk, bit for bit.
|
|
1790
|
+
*/
|
|
1791
|
+
function buildStems(definition, random) {
|
|
1792
|
+
const { trunk } = definition;
|
|
1793
|
+
const { stems: count = 1, spread = 0, gravity = 0 } = trunk;
|
|
1794
|
+
const ring = random.fork("stems");
|
|
1795
|
+
const turn = ring.range(0, Math.PI * 2);
|
|
1796
|
+
const stems = [];
|
|
1797
|
+
for (let i = 0; i < count; i++) {
|
|
1798
|
+
const r = ring.fork(i);
|
|
1799
|
+
const azimuth = turn + (i + r.range(-.25, RING_JITTER)) / count * Math.PI * 2;
|
|
1800
|
+
const tilt = Math.min(spread * (1 + r.range(-.3, SPREAD_VARIATION)), 90) * DEG$1;
|
|
1801
|
+
const offset = count > 1 ? trunk.radius * RING_RADIUS : 0;
|
|
1802
|
+
const line = centreLine(trunk, random.fork(i === 0 ? "trunk" : `trunk/${i}`));
|
|
1803
|
+
stems.push(placeStem(String(i), line, gravity, azimuth, tilt, offset));
|
|
1804
|
+
}
|
|
1805
|
+
return stems;
|
|
1806
|
+
}
|
|
1807
|
+
/** One upright, unbent Stem's centre line at the origin, varied by the Seed within the trunk's ranges. */
|
|
1808
|
+
function centreLine(trunk, random) {
|
|
1809
|
+
const length = trunk.length * (1 + random.range(-trunk.lengthVariation, trunk.lengthVariation));
|
|
1810
|
+
const sectionLength = length / trunk.sections;
|
|
1811
|
+
const maxDrift = trunk.bend * sectionLength;
|
|
1812
|
+
const points = [];
|
|
1813
|
+
let x = 0;
|
|
1814
|
+
let z = 0;
|
|
1815
|
+
for (let i = 0; i <= trunk.sections; i++) {
|
|
1816
|
+
const t = i / trunk.sections;
|
|
1817
|
+
if (i > 0) {
|
|
1818
|
+
x += random.range(-maxDrift, maxDrift);
|
|
1819
|
+
z += random.range(-maxDrift, maxDrift);
|
|
1820
|
+
}
|
|
1821
|
+
points.push({
|
|
1822
|
+
position: [
|
|
1823
|
+
x,
|
|
1824
|
+
t * length,
|
|
1825
|
+
z
|
|
1826
|
+
],
|
|
1827
|
+
radius: trunk.radius * (1 + (trunk.taper - 1) * t)
|
|
1828
|
+
});
|
|
1829
|
+
}
|
|
1830
|
+
return points;
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* Bends the leader by `gravity`, leans the Stem `tilt` radians out towards `azimuth` and moves its
|
|
1834
|
+
* base `offset` out along the ring. All bending happens in the vertical plane towards `azimuth`.
|
|
1835
|
+
*/
|
|
1836
|
+
function placeStem(path, line, gravity, azimuth, tilt, offset) {
|
|
1837
|
+
const out = [
|
|
1838
|
+
cos(azimuth),
|
|
1839
|
+
0,
|
|
1840
|
+
sin$1(azimuth)
|
|
1841
|
+
];
|
|
1842
|
+
const axis = cross$1([
|
|
1843
|
+
0,
|
|
1844
|
+
1,
|
|
1845
|
+
0
|
|
1846
|
+
], out);
|
|
1847
|
+
let points = line;
|
|
1848
|
+
if (gravity !== 0) {
|
|
1849
|
+
const sections = line.length - 1;
|
|
1850
|
+
const first = Math.min(sections - 1, Math.ceil(LEADER_START * sections));
|
|
1851
|
+
const step = -gravity * LEADER_BEND * DEG$1 / (sections - first);
|
|
1852
|
+
let bend = 0;
|
|
1853
|
+
points = line.slice(0, first + 1);
|
|
1854
|
+
for (let k = first; k < sections; k++) {
|
|
1855
|
+
bend = Math.min(Math.max(bend + step, -tilt), Math.PI - tilt);
|
|
1856
|
+
const segment = sub(line[k + 1].position, line[k].position);
|
|
1857
|
+
points.push({
|
|
1858
|
+
position: add(points[k].position, rotate(segment, axis, bend)),
|
|
1859
|
+
radius: line[k + 1].radius
|
|
1860
|
+
});
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
if (tilt !== 0) points = points.map((p) => ({
|
|
1864
|
+
position: rotate(p.position, axis, tilt),
|
|
1865
|
+
radius: p.radius
|
|
1866
|
+
}));
|
|
1867
|
+
if (offset !== 0) points = points.map((p) => ({
|
|
1868
|
+
position: add(p.position, scale(out, offset)),
|
|
1869
|
+
radius: p.radius
|
|
1870
|
+
}));
|
|
1871
|
+
return {
|
|
1872
|
+
path,
|
|
1873
|
+
parent: null,
|
|
1874
|
+
level: 0,
|
|
1875
|
+
attach: 0,
|
|
1876
|
+
direction: rotate([
|
|
1877
|
+
0,
|
|
1878
|
+
1,
|
|
1879
|
+
0
|
|
1880
|
+
], axis, tilt),
|
|
1881
|
+
points
|
|
1882
|
+
};
|
|
1883
|
+
}
|
|
1884
|
+
/** Rotates `v` about the unit `axis` by `angle` radians (Rodrigues). */
|
|
1885
|
+
function rotate(v, axis, angle) {
|
|
1886
|
+
if (angle === 0) return v;
|
|
1887
|
+
const c = cos(angle);
|
|
1888
|
+
const s = sin$1(angle);
|
|
1889
|
+
return add(add(scale(v, c), scale(cross$1(axis, v), s)), scale(axis, dot$1(axis, v) * (1 - c)));
|
|
1890
|
+
}
|
|
1576
1891
|
// src/generator/mesh.ts
|
|
1577
1892
|
/**
|
|
1578
1893
|
* Sweeps a ring of `sides` vertices along a branch's points, using parallel-transport
|
|
1579
|
-
* frames so branches in any direction get even rings, and caps the tip.
|
|
1894
|
+
* frames so branches in any direction get even rings, and caps the tip. A Stem (`stem` given)
|
|
1895
|
+
* sinks its base ring below the ground and widens by its Root Flare; its skeleton is never changed.
|
|
1580
1896
|
*/
|
|
1581
1897
|
function sweepBranch(branch, sides, random, season = {
|
|
1582
1898
|
shift: 0,
|
|
@@ -1585,12 +1901,11 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1585
1901
|
}, growth = {
|
|
1586
1902
|
start: 0,
|
|
1587
1903
|
end: 0
|
|
1588
|
-
}, sway) {
|
|
1904
|
+
}, sway, stem) {
|
|
1589
1905
|
const pts = branch.points;
|
|
1590
|
-
const
|
|
1591
|
-
const rings = pts.length;
|
|
1906
|
+
const rings = stem && stem.flare > 0 ? flaredRings(branch, stem) : plainRings(branch);
|
|
1592
1907
|
const ringSize = sides + 1;
|
|
1593
|
-
const vertexCount = rings * ringSize + 1;
|
|
1908
|
+
const vertexCount = rings.length * ringSize + 1;
|
|
1594
1909
|
const positions = new Float32Array(vertexCount * 3);
|
|
1595
1910
|
const normals = new Float32Array(vertexCount * 3);
|
|
1596
1911
|
const uvs = new Float32Array(vertexCount * 2);
|
|
@@ -1605,42 +1920,107 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1605
1920
|
growth,
|
|
1606
1921
|
sway
|
|
1607
1922
|
};
|
|
1608
|
-
const slope = (pts[0].radius - pts[rings - 1].radius) / Math.max(pathLength(branch), 1e-6);
|
|
1609
1923
|
let v = 0;
|
|
1610
|
-
|
|
1611
|
-
const { position, radius } = pts[r];
|
|
1612
|
-
const { tangent, normal, binormal } = frames[r];
|
|
1924
|
+
rings.forEach(({ position, radius, tangent, normal, binormal, along, slope }, r) => {
|
|
1613
1925
|
for (let s = 0; s <= sides; s++) {
|
|
1614
1926
|
const angle = s % sides / sides * Math.PI * 2;
|
|
1615
1927
|
const radial = add(scale(normal, cos(angle)), scale(binormal, sin$1(angle)));
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1928
|
+
const vertex = add(position, scale(radial, radius));
|
|
1929
|
+
if (stem && r === 0) vertex[1] -= BASE_SINK * pts[0].radius;
|
|
1930
|
+
positions.set(vertex, v * 3);
|
|
1931
|
+
normals.set(normalize$1(add(radial, scale(tangent, slope))), v * 3);
|
|
1932
|
+
uvs.set([s / sides, along], v * 2);
|
|
1933
|
+
writePart(attributes, v, part, along, radius);
|
|
1620
1934
|
v++;
|
|
1621
1935
|
}
|
|
1622
|
-
}
|
|
1623
|
-
for (let r = 0; r < rings - 1; r++) for (let s = 0; s < sides; s++) {
|
|
1936
|
+
});
|
|
1937
|
+
for (let r = 0; r < rings.length - 1; r++) for (let s = 0; s < sides; s++) {
|
|
1624
1938
|
const a = r * ringSize + s;
|
|
1625
1939
|
const b = a + 1;
|
|
1626
1940
|
const c = a + ringSize;
|
|
1627
1941
|
const d = c + 1;
|
|
1628
1942
|
indices.push(a, b, c, b, d, c);
|
|
1629
1943
|
}
|
|
1630
|
-
const tip = v;
|
|
1631
|
-
positions.set(pts[
|
|
1632
|
-
normals.set(
|
|
1633
|
-
uvs.set([.5, 1], tip * 2);
|
|
1634
|
-
writePart(attributes, tip, part, 1);
|
|
1635
|
-
const lastRing = (rings - 1) * ringSize;
|
|
1636
|
-
for (let s = 0; s < sides; s++) indices.push(lastRing + s, lastRing + s + 1, tip);
|
|
1637
|
-
return {
|
|
1638
|
-
positions,
|
|
1639
|
-
normals,
|
|
1640
|
-
uvs,
|
|
1641
|
-
indices: Uint32Array.from(indices),
|
|
1642
|
-
attributes
|
|
1643
|
-
};
|
|
1944
|
+
const tip = v;
|
|
1945
|
+
positions.set(pts[pts.length - 1].position, tip * 3);
|
|
1946
|
+
normals.set(rings[rings.length - 1].tangent, tip * 3);
|
|
1947
|
+
uvs.set([.5, 1], tip * 2);
|
|
1948
|
+
writePart(attributes, tip, part, 1);
|
|
1949
|
+
const lastRing = (rings.length - 1) * ringSize;
|
|
1950
|
+
for (let s = 0; s < sides; s++) indices.push(lastRing + s, lastRing + s + 1, tip);
|
|
1951
|
+
return {
|
|
1952
|
+
positions,
|
|
1953
|
+
normals,
|
|
1954
|
+
uvs,
|
|
1955
|
+
indices: Uint32Array.from(indices),
|
|
1956
|
+
attributes
|
|
1957
|
+
};
|
|
1958
|
+
}
|
|
1959
|
+
/** A ring at every skeleton point. Radius change per unit length tilts the normals of a tapered branch towards its tip. */
|
|
1960
|
+
function plainRings(branch) {
|
|
1961
|
+
const pts = branch.points;
|
|
1962
|
+
const frames = branchFrames(branch);
|
|
1963
|
+
const slope = (pts[0].radius - pts[pts.length - 1].radius) / Math.max(pathLength(branch), 1e-6);
|
|
1964
|
+
return pts.map(({ position, radius }, r) => ({
|
|
1965
|
+
position,
|
|
1966
|
+
radius,
|
|
1967
|
+
...frames[r],
|
|
1968
|
+
along: r / (pts.length - 1),
|
|
1969
|
+
slope
|
|
1970
|
+
}));
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* A Stem's rings with its Root Flare: `sections` extra rings in the bottom {@link FLARE_ZONE} of its
|
|
1974
|
+
* length, closer together towards the base, and every ring in that zone widened by a multiplier that
|
|
1975
|
+
* eases from 1 at the top of the zone to 1 + {@link FLARE_WIDTH} × flare at the base (quadratic, so
|
|
1976
|
+
* the flare leaves the trunk without a crease). Normals follow the flared surface.
|
|
1977
|
+
*/
|
|
1978
|
+
function flaredRings(branch, { flare, sections }) {
|
|
1979
|
+
const pts = branch.points;
|
|
1980
|
+
const total = Math.max(pathLength(branch), 1e-6);
|
|
1981
|
+
const zone = FLARE_ZONE * total;
|
|
1982
|
+
const slope = (pts[0].radius - pts[pts.length - 1].radius) / total;
|
|
1983
|
+
const extra = Array.from({ length: sections }, (_, k) => zone * ((k + 1) / sections) * ((k + 1) / sections));
|
|
1984
|
+
const points = [];
|
|
1985
|
+
let walked = 0;
|
|
1986
|
+
for (let i = 0; i < pts.length; i++) {
|
|
1987
|
+
const a = pts[i];
|
|
1988
|
+
points.push({
|
|
1989
|
+
...a,
|
|
1990
|
+
along: i / (pts.length - 1),
|
|
1991
|
+
distance: walked
|
|
1992
|
+
});
|
|
1993
|
+
if (i === pts.length - 1) break;
|
|
1994
|
+
const b = pts[i + 1];
|
|
1995
|
+
const segment = length$1(sub(b.position, a.position));
|
|
1996
|
+
const gap = 1e-6 * total;
|
|
1997
|
+
for (const d of extra) if (d > walked + gap && d < walked + segment - gap) {
|
|
1998
|
+
const f = (d - walked) / segment;
|
|
1999
|
+
points.push({
|
|
2000
|
+
position: lerp(a.position, b.position, f),
|
|
2001
|
+
radius: a.radius + (b.radius - a.radius) * f,
|
|
2002
|
+
along: (i + f) / (pts.length - 1),
|
|
2003
|
+
distance: d
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
walked += segment;
|
|
2007
|
+
}
|
|
2008
|
+
const frames = branchFrames({
|
|
2009
|
+
...branch,
|
|
2010
|
+
points
|
|
2011
|
+
});
|
|
2012
|
+
const width = FLARE_WIDTH * flare;
|
|
2013
|
+
return points.map(({ position, radius, along, distance }, r) => {
|
|
2014
|
+
const u = Math.max(1 - distance / zone, 0);
|
|
2015
|
+
const multiplier = 1 + width * u * u;
|
|
2016
|
+
return {
|
|
2017
|
+
position,
|
|
2018
|
+
radius: radius * multiplier,
|
|
2019
|
+
...frames[r],
|
|
2020
|
+
along,
|
|
2021
|
+
slope: slope * multiplier + radius * 2 * width * u / zone
|
|
2022
|
+
};
|
|
2023
|
+
});
|
|
1644
2024
|
}
|
|
1645
2025
|
/** Concatenates geometry buffers into one (indices are offset). */
|
|
1646
2026
|
function mergeBuffers(parts) {
|
|
@@ -1666,6 +2046,281 @@ function mergeBuffers(parts) {
|
|
|
1666
2046
|
}
|
|
1667
2047
|
return out;
|
|
1668
2048
|
}
|
|
2049
|
+
// src/generator/occlusion.ts
|
|
2050
|
+
/**
|
|
2051
|
+
* Structural Occlusion (GLOSSARY): how open each vertex is to the sky, from the tree's own shape.
|
|
2052
|
+
* 0 = fully occluded, 1 = open. A deterministic mix (a product) of two terms, both measured on the fully
|
|
2053
|
+
* grown tree with every leaf on it (so it is the same in every Season and at every Growth):
|
|
2054
|
+
*
|
|
2055
|
+
* - crown depth: how deep the vertex sits inside the Crown Volume (`1 - crownDistance`, 0 at the shell);
|
|
2056
|
+
* - leaf cover above: the leaf area above the vertex, from a coarse voxel grid of leaf area, summed
|
|
2057
|
+
* upwards (as a leaf area index) and turned into openness, which falls from 1 towards FLOOR as it grows.
|
|
2058
|
+
*
|
|
2059
|
+
* No randomness and no ray tracing; only + - * / and sqrt, so the same numbers on every machine.
|
|
2060
|
+
* The Definition's `strength` is not used here: the material mixes it in (`aoNode`).
|
|
2061
|
+
*/
|
|
2062
|
+
/** Grid cells along the largest extent of the leaves. */
|
|
2063
|
+
var CELLS = 12;
|
|
2064
|
+
/** Empty cells around the leaves, so the grid's edge (and the horizontal blur's spill) reads as open sky. */
|
|
2065
|
+
var PAD = 2;
|
|
2066
|
+
/** How fast the cover above darkens: openness `FLOOR + (1 - FLOOR) / (1 + COVER × leaf area per ground area above)`. */
|
|
2067
|
+
var COVER = .3;
|
|
2068
|
+
/** The openness under an endless canopy: light still bounces in from the sides and the ground. */
|
|
2069
|
+
var FLOOR = .3;
|
|
2070
|
+
/** How much darker the very centre of the Crown Volume is than its shell (before the cover above). */
|
|
2071
|
+
var DEPTH = .4;
|
|
2072
|
+
/**
|
|
2073
|
+
* Adds `aOcclusion` (1 float per vertex) to each of `parts` (wood, leaves, Fruit). A tree without leaves
|
|
2074
|
+
* has no crown to hide under: every vertex is open (1).
|
|
2075
|
+
*/
|
|
2076
|
+
function bakeOcclusion(parts, leaves, crown) {
|
|
2077
|
+
const cover = leaves.indices.length > 0 ? coverAbove(leaves) : null;
|
|
2078
|
+
for (const part of parts) {
|
|
2079
|
+
const p = part.positions;
|
|
2080
|
+
const array = new Float32Array(p.length / 3);
|
|
2081
|
+
if (cover) {
|
|
2082
|
+
const point = [
|
|
2083
|
+
0,
|
|
2084
|
+
0,
|
|
2085
|
+
0
|
|
2086
|
+
];
|
|
2087
|
+
for (let v = 0; v < array.length; v++) {
|
|
2088
|
+
point[0] = p[v * 3];
|
|
2089
|
+
point[1] = p[v * 3 + 1];
|
|
2090
|
+
point[2] = p[v * 3 + 2];
|
|
2091
|
+
const depth = Math.max(0, 1 - crownDistance(crown, point));
|
|
2092
|
+
array[v] = (1 - DEPTH * depth) * (FLOOR + .7 / (1 + COVER * cover(point)));
|
|
2093
|
+
}
|
|
2094
|
+
} else array.fill(1);
|
|
2095
|
+
part.attributes.aOcclusion = {
|
|
2096
|
+
array,
|
|
2097
|
+
itemSize: 1
|
|
2098
|
+
};
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
/**
|
|
2102
|
+
* The leaf area above a point per unit of ground area (a leaf area index, 0 above the crown), as a
|
|
2103
|
+
* function. Leaf triangles are binned by centroid into a grid of cubes, blurred a little sideways (the
|
|
2104
|
+
* sky is a hemisphere, not a point overhead), summed from the top down and sampled trilinearly.
|
|
2105
|
+
*/
|
|
2106
|
+
function coverAbove(leaves) {
|
|
2107
|
+
const p = leaves.positions;
|
|
2108
|
+
const min = [
|
|
2109
|
+
Infinity,
|
|
2110
|
+
Infinity,
|
|
2111
|
+
Infinity
|
|
2112
|
+
];
|
|
2113
|
+
const max = [
|
|
2114
|
+
-Infinity,
|
|
2115
|
+
-Infinity,
|
|
2116
|
+
-Infinity
|
|
2117
|
+
];
|
|
2118
|
+
for (let o = 0; o < p.length; o += 3) for (let i = 0; i < 3; i++) {
|
|
2119
|
+
min[i] = Math.min(min[i], p[o + i]);
|
|
2120
|
+
max[i] = Math.max(max[i], p[o + i]);
|
|
2121
|
+
}
|
|
2122
|
+
const size = Math.max(max[0] - min[0], max[1] - min[1], max[2] - min[2], 1e-6) / CELLS;
|
|
2123
|
+
const origin = min.map((m) => m - PAD * size);
|
|
2124
|
+
const [nx, ny, nz] = [
|
|
2125
|
+
0,
|
|
2126
|
+
1,
|
|
2127
|
+
2
|
|
2128
|
+
].map((i) => Math.ceil((max[i] - min[i]) / size) + 1 + 4);
|
|
2129
|
+
const at = (x, y, z) => (y * nz + z) * nx + x;
|
|
2130
|
+
const cell = (value, i, n) => Math.min(n - 1, Math.max(0, Math.floor((value - origin[i]) / size)));
|
|
2131
|
+
let area = new Float64Array(nx * ny * nz);
|
|
2132
|
+
const idx = leaves.indices;
|
|
2133
|
+
for (let t = 0; t < idx.length; t += 3) {
|
|
2134
|
+
const a = idx[t] * 3;
|
|
2135
|
+
const b = idx[t + 1] * 3;
|
|
2136
|
+
const c = idx[t + 2] * 3;
|
|
2137
|
+
const u = [
|
|
2138
|
+
p[b] - p[a],
|
|
2139
|
+
p[b + 1] - p[a + 1],
|
|
2140
|
+
p[b + 2] - p[a + 2]
|
|
2141
|
+
];
|
|
2142
|
+
const w = [
|
|
2143
|
+
p[c] - p[a],
|
|
2144
|
+
p[c + 1] - p[a + 1],
|
|
2145
|
+
p[c + 2] - p[a + 2]
|
|
2146
|
+
];
|
|
2147
|
+
const cx = u[1] * w[2] - u[2] * w[1];
|
|
2148
|
+
const cy = u[2] * w[0] - u[0] * w[2];
|
|
2149
|
+
const cz = u[0] * w[1] - u[1] * w[0];
|
|
2150
|
+
const triangle = Math.sqrt(cx * cx + cy * cy + cz * cz) / 2;
|
|
2151
|
+
const centre = [
|
|
2152
|
+
0,
|
|
2153
|
+
1,
|
|
2154
|
+
2
|
|
2155
|
+
].map((i) => (p[a + i] + p[b + i] + p[c + i]) / 3);
|
|
2156
|
+
area[at(cell(centre[0], 0, nx), cell(centre[1], 1, ny), cell(centre[2], 2, nz))] += triangle / (size * size);
|
|
2157
|
+
}
|
|
2158
|
+
for (const [dx, dz] of [[1, 0], [0, 1]]) {
|
|
2159
|
+
const blurred = new Float64Array(area.length);
|
|
2160
|
+
for (let y = 0; y < ny; y++) for (let z = 0; z < nz; z++) for (let x = 0; x < nx; x++) {
|
|
2161
|
+
const here = area[at(x, y, z)];
|
|
2162
|
+
const before = x - dx >= 0 && z - dz >= 0 ? area[at(x - dx, y, z - dz)] : 0;
|
|
2163
|
+
const after = x + dx < nx && z + dz < nz ? area[at(x + dx, y, z + dz)] : 0;
|
|
2164
|
+
blurred[at(x, y, z)] = (before + 2 * here + after) / 4;
|
|
2165
|
+
}
|
|
2166
|
+
area = blurred;
|
|
2167
|
+
}
|
|
2168
|
+
const above = new Float64Array(area.length);
|
|
2169
|
+
for (let z = 0; z < nz; z++) for (let x = 0; x < nx; x++) {
|
|
2170
|
+
let sum = 0;
|
|
2171
|
+
for (let y = ny - 1; y >= 0; y--) {
|
|
2172
|
+
const own = area[at(x, y, z)];
|
|
2173
|
+
above[at(x, y, z)] = sum + own / 2;
|
|
2174
|
+
sum += own;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
return (q) => {
|
|
2178
|
+
const f = [
|
|
2179
|
+
0,
|
|
2180
|
+
1,
|
|
2181
|
+
2
|
|
2182
|
+
].map((i) => (q[i] - origin[i]) / size - .5);
|
|
2183
|
+
const n = [
|
|
2184
|
+
nx,
|
|
2185
|
+
ny,
|
|
2186
|
+
nz
|
|
2187
|
+
];
|
|
2188
|
+
const lo = [];
|
|
2189
|
+
const t = [];
|
|
2190
|
+
for (let i = 0; i < 3; i++) {
|
|
2191
|
+
const g = Math.min(n[i] - 1, Math.max(0, f[i]));
|
|
2192
|
+
lo[i] = Math.min(n[i] - 2, Math.floor(g));
|
|
2193
|
+
t[i] = g - lo[i];
|
|
2194
|
+
}
|
|
2195
|
+
let value = 0;
|
|
2196
|
+
for (let k = 0; k < 8; k++) {
|
|
2197
|
+
const x = lo[0] + (k & 1);
|
|
2198
|
+
const y = lo[1] + (k >> 1 & 1);
|
|
2199
|
+
const z = lo[2] + (k >> 2 & 1);
|
|
2200
|
+
const weight = (k & 1 ? t[0] : 1 - t[0]) * (k >> 1 & 1 ? t[1] : 1 - t[1]) * (k >> 2 & 1 ? t[2] : 1 - t[2]);
|
|
2201
|
+
value += weight * above[at(x, y, z)];
|
|
2202
|
+
}
|
|
2203
|
+
return value;
|
|
2204
|
+
};
|
|
2205
|
+
}
|
|
2206
|
+
// src/generator/roots.ts
|
|
2207
|
+
var DEG = Math.PI / 180;
|
|
2208
|
+
/** Seed range of each Root's place around its Stem: ± this fraction of the gap between Roots. */
|
|
2209
|
+
var ROOT_JITTER = .25;
|
|
2210
|
+
/** Where a Root starts: this fraction of the trunk radius out from the Stem's centre, inside the Root Flare. */
|
|
2211
|
+
var ROOT_START_OUT = .5;
|
|
2212
|
+
/** ... and this many of the Root's own radii above the ground (it sits on the ground there), so its top blends into the flare. */
|
|
2213
|
+
var ROOT_START_UP = 1;
|
|
2214
|
+
/** How far `bend` = 1 turns a Root sideways per section (at most), radians. */
|
|
2215
|
+
var ROOT_BEND_TURN = .5;
|
|
2216
|
+
/** A Root tapers as (1 − t)^this along its length (t = 0 base .. 1 tip): below 1 it stays thick longer, then comes to a point. */
|
|
2217
|
+
var ROOT_TAPER = .6;
|
|
2218
|
+
/** A Root's tip ends at least this many of its base radii below the ground (it curves down to get there). */
|
|
2219
|
+
var ROOT_TIP_DEPTH = 1;
|
|
2220
|
+
/** Sides around a Root while `roots.sides` is absent: about two thirds of the trunk's. */
|
|
2221
|
+
var rootSides = (definition) => definition.roots?.sides ?? Math.max(3, Math.round(definition.trunk.sides * 2 / 3));
|
|
2222
|
+
/**
|
|
2223
|
+
* The part of the circle around a Stem its Roots are spread evenly over: azimuths `from` .. `from` +
|
|
2224
|
+
* `width` radians, measured from the horizontal unit vector `out` ([x, z]) round towards +z. A single
|
|
2225
|
+
* Stem's Roots go all the way round, starting at the Seed's `turn`. Of several Stems (standing in a
|
|
2226
|
+
* ring round the origin), each one's Roots cover only the half-circle facing `out`, away from the ring
|
|
2227
|
+
* centre, so they don't pile up between the Stems.
|
|
2228
|
+
*/
|
|
2229
|
+
function rootArc(definition, stem, turn) {
|
|
2230
|
+
const [x, , z] = stem.points[0].position;
|
|
2231
|
+
const away = hypot3(x, 0, z);
|
|
2232
|
+
if ((definition.trunk.stems ?? 1) <= 1 || away === 0) return {
|
|
2233
|
+
out: [1, 0],
|
|
2234
|
+
from: turn,
|
|
2235
|
+
width: Math.PI * 2
|
|
2236
|
+
};
|
|
2237
|
+
return {
|
|
2238
|
+
out: [x / away, z / away],
|
|
2239
|
+
from: -Math.PI / 2,
|
|
2240
|
+
width: Math.PI
|
|
2241
|
+
};
|
|
2242
|
+
}
|
|
2243
|
+
/** The horizontal unit vector ([x, z]) at `azimuth` in `arc`. */
|
|
2244
|
+
function toward(arc, azimuth) {
|
|
2245
|
+
const [c, s] = [cos(azimuth), sin$1(azimuth)];
|
|
2246
|
+
const [ox, oz] = arc.out;
|
|
2247
|
+
return [ox * c - oz * s, oz * c + ox * s];
|
|
2248
|
+
}
|
|
2249
|
+
/**
|
|
2250
|
+
* Grows the Roots (ADR 0007) of every Stem in `stems` (the Stems left after the Sculpt pass, as
|
|
2251
|
+
* they are after it: a deleted Stem has none, a moved one keeps them at its base, a scaled one
|
|
2252
|
+
* scales them with its length). Stem `<p>`'s Roots are `<p>/r0`, `<p>/r1`, ...; Root i draws only
|
|
2253
|
+
* from `random.fork('roots/<p>/<i>')` and the Stem's turn from `random.fork('roots/<p>')`, so Roots
|
|
2254
|
+
* never change the wood, leaves or Fruit, and one Stem's Sculpts never change another's Roots.
|
|
2255
|
+
*/
|
|
2256
|
+
function growRoots(definition, stems, random) {
|
|
2257
|
+
const roots = definition.roots;
|
|
2258
|
+
if (!roots) return [];
|
|
2259
|
+
return stems.flatMap((stem) => {
|
|
2260
|
+
const arc = rootArc(definition, stem, random.fork(`roots/${stem.path}`).range(0, Math.PI * 2));
|
|
2261
|
+
return Array.from({ length: roots.count }, (_, i) => {
|
|
2262
|
+
const r = random.fork(`roots/${stem.path}/${i}`);
|
|
2263
|
+
const azimuth = arc.from + (i + .5 + r.range(-.25, ROOT_JITTER)) / roots.count * arc.width;
|
|
2264
|
+
return growRoot(`${stem.path}/r${i}`, stem, roots, arc, azimuth, r);
|
|
2265
|
+
});
|
|
2266
|
+
});
|
|
2267
|
+
}
|
|
2268
|
+
/**
|
|
2269
|
+
* One Root: from just inside the Stem's Root Flare, slightly above the Stem's base (the ground), it runs out towards
|
|
2270
|
+
* `azimuth` (in `arc`) and down at `angle`, turns sideways by up to `bend` per section, and tapers to a point.
|
|
2271
|
+
* If the dive alone would not take its tip below the ground, it curves down (more towards the tip) until it does.
|
|
2272
|
+
*/
|
|
2273
|
+
function growRoot(path, stem, roots, arc, azimuth, r) {
|
|
2274
|
+
const base = stem.points[0];
|
|
2275
|
+
const sections = roots.sections ?? 4;
|
|
2276
|
+
const length = pathLength(stem) * roots.length * (1 + r.range(-roots.lengthVariation, roots.lengthVariation));
|
|
2277
|
+
const radius = base.radius * roots.radius;
|
|
2278
|
+
const step = length / sections;
|
|
2279
|
+
const pitch = roots.angle * DEG;
|
|
2280
|
+
const [outX, outZ] = toward(arc, azimuth);
|
|
2281
|
+
let heading = azimuth;
|
|
2282
|
+
let position = [
|
|
2283
|
+
base.position[0] + outX * base.radius * ROOT_START_OUT,
|
|
2284
|
+
base.position[1] + radius * ROOT_START_UP,
|
|
2285
|
+
base.position[2] + outZ * base.radius * ROOT_START_OUT
|
|
2286
|
+
];
|
|
2287
|
+
const positions = [position];
|
|
2288
|
+
for (let s = 0; s < sections; s++) {
|
|
2289
|
+
const [x, z] = toward(arc, heading);
|
|
2290
|
+
position = [
|
|
2291
|
+
position[0] + step * cos(pitch) * x,
|
|
2292
|
+
position[1] - step * sin$1(pitch),
|
|
2293
|
+
position[2] + step * cos(pitch) * z
|
|
2294
|
+
];
|
|
2295
|
+
positions.push(position);
|
|
2296
|
+
heading += r.range(-1, 1) * roots.bend * ROOT_BEND_TURN;
|
|
2297
|
+
}
|
|
2298
|
+
const drop = Math.max(0, positions[sections][1] + ROOT_TIP_DEPTH * radius);
|
|
2299
|
+
const points = positions.map(([x, y, z], s) => {
|
|
2300
|
+
const t = s / sections;
|
|
2301
|
+
return {
|
|
2302
|
+
position: [
|
|
2303
|
+
x,
|
|
2304
|
+
y - drop * t * t,
|
|
2305
|
+
z
|
|
2306
|
+
],
|
|
2307
|
+
radius: radius * pow(1 - t, ROOT_TAPER)
|
|
2308
|
+
};
|
|
2309
|
+
});
|
|
2310
|
+
const direction = [
|
|
2311
|
+
cos(pitch) * outX,
|
|
2312
|
+
-sin$1(pitch),
|
|
2313
|
+
cos(pitch) * outZ
|
|
2314
|
+
];
|
|
2315
|
+
return {
|
|
2316
|
+
path,
|
|
2317
|
+
parent: stem.path,
|
|
2318
|
+
level: 0,
|
|
2319
|
+
attach: 0,
|
|
2320
|
+
direction,
|
|
2321
|
+
points
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
1669
2324
|
// src/generator/sculpt.ts
|
|
1670
2325
|
/**
|
|
1671
2326
|
* The Sculpt pass (ADR 0003): hand edits stored in the Definition, applied after the procedural
|
|
@@ -1754,44 +2409,6 @@ function eulerXYZ([x, y, z]) {
|
|
|
1754
2409
|
m[6] * vx + m[7] * vy + m[8] * vz
|
|
1755
2410
|
];
|
|
1756
2411
|
}
|
|
1757
|
-
// src/generator/trunk.ts
|
|
1758
|
-
/** Builds the trunk centre line from the Definition, varied by the Seed within its ranges. */
|
|
1759
|
-
function buildTrunk(definition, random) {
|
|
1760
|
-
const { trunk } = definition;
|
|
1761
|
-
const length = trunk.length * (1 + random.range(-trunk.lengthVariation, trunk.lengthVariation));
|
|
1762
|
-
const sectionLength = length / trunk.sections;
|
|
1763
|
-
const maxDrift = trunk.bend * sectionLength;
|
|
1764
|
-
const points = [];
|
|
1765
|
-
let x = 0;
|
|
1766
|
-
let z = 0;
|
|
1767
|
-
for (let i = 0; i <= trunk.sections; i++) {
|
|
1768
|
-
const t = i / trunk.sections;
|
|
1769
|
-
if (i > 0) {
|
|
1770
|
-
x += random.range(-maxDrift, maxDrift);
|
|
1771
|
-
z += random.range(-maxDrift, maxDrift);
|
|
1772
|
-
}
|
|
1773
|
-
points.push({
|
|
1774
|
-
position: [
|
|
1775
|
-
x,
|
|
1776
|
-
t * length,
|
|
1777
|
-
z
|
|
1778
|
-
],
|
|
1779
|
-
radius: trunk.radius * (1 + (trunk.taper - 1) * t)
|
|
1780
|
-
});
|
|
1781
|
-
}
|
|
1782
|
-
return {
|
|
1783
|
-
path: "0",
|
|
1784
|
-
parent: null,
|
|
1785
|
-
level: 0,
|
|
1786
|
-
attach: 0,
|
|
1787
|
-
direction: [
|
|
1788
|
-
0,
|
|
1789
|
-
1,
|
|
1790
|
-
0
|
|
1791
|
-
],
|
|
1792
|
-
points
|
|
1793
|
-
};
|
|
1794
|
-
}
|
|
1795
2412
|
// src/generator/index.ts
|
|
1796
2413
|
/**
|
|
1797
2414
|
* The Generator: a pure function from a (parsed) Tree Definition and a Seed to tree data.
|
|
@@ -1802,44 +2419,73 @@ function buildTrunk(definition, random) {
|
|
|
1802
2419
|
*/
|
|
1803
2420
|
function generateTree(definition, seed) {
|
|
1804
2421
|
const random = createRandom(seed);
|
|
1805
|
-
const
|
|
2422
|
+
const stems = buildStems(definition, random);
|
|
1806
2423
|
const sculpts = sculptsFor(definition, seed);
|
|
1807
|
-
const branches = sculptBranches([
|
|
2424
|
+
const branches = sculptBranches([...stems, ...buildBranches(definition, stems, random.fork("branches"))], sculpts);
|
|
1808
2425
|
const sidesOf = (level) => level === 0 ? definition.trunk.sides : definition.branches[level - 1].sides;
|
|
1809
2426
|
const leaves = sculptLeaves(placeLeaves(definition, branches, random.fork("leaves")), sculpts, seed);
|
|
1810
|
-
const
|
|
2427
|
+
const fruit = placeFruit(definition, branches, random.fork("fruit"));
|
|
2428
|
+
const roots = growRoots(definition, branches.filter((b) => b.parent === null), random);
|
|
2429
|
+
const partGrowth = growthTiming(definition, seed, branches, leaves);
|
|
2430
|
+
const growth = {
|
|
2431
|
+
...partGrowth,
|
|
2432
|
+
fruit: fruitGrowth(seed, fruit, new Map(branches.map((b, i) => [b.path, partGrowth.branches[i]]))),
|
|
2433
|
+
roots: roots.map(() => ({
|
|
2434
|
+
start: 0,
|
|
2435
|
+
end: 0
|
|
2436
|
+
}))
|
|
2437
|
+
};
|
|
2438
|
+
const stemSweep = {
|
|
2439
|
+
flare: definition.trunk.flare ?? 0,
|
|
2440
|
+
sections: definition.trunk.flareSections ?? 3
|
|
2441
|
+
};
|
|
1811
2442
|
const sway = branchSway(branches, definition.branches.length);
|
|
1812
|
-
const
|
|
2443
|
+
const woodSeason = woodSeasonAttribute(seed, definition.variation);
|
|
2444
|
+
const wood = mergeBuffers([...branches.map((b, i) => sweepBranch(b, sidesOf(b.level), seedRandom(seed, b.path), woodSeason, growth.branches[i], sway.get(b.path), b.parent === null ? stemSweep : void 0)), ...roots.map((root, i) => sweepBranch(root, rootSides(definition), seedRandom(seed, root.path), woodSeason, growth.roots[i]))]);
|
|
1813
2445
|
const leafBuffers = definition.leaves.shape === "card" ? leafGeometry(leaves, definition.branches.length + 1, seed, definition.variation, growth.leaves, sway) : solidLeafGeometry(leaves, definition.leaves.shape, definition.branches.length + 1, seed, definition.variation, growth.leaves, sway);
|
|
2446
|
+
const fruitBuffers = fruitGeometry(fruit, definition.fruit ?? { type: "round" }, definition.branches.length + 2, seed, definition.variation, growth.fruit, sway);
|
|
1814
2447
|
const skeleton = {
|
|
1815
2448
|
branches,
|
|
1816
|
-
leaves
|
|
2449
|
+
leaves,
|
|
2450
|
+
fruit,
|
|
2451
|
+
roots
|
|
1817
2452
|
};
|
|
1818
2453
|
writeTrunkSway(wood, trunkHeight(skeleton));
|
|
1819
2454
|
writeTrunkSway(leafBuffers, trunkHeight(skeleton));
|
|
2455
|
+
writeTrunkSway(fruitBuffers, trunkHeight(skeleton));
|
|
2456
|
+
const crown = crownVolume(leafBuffers.positions, branches);
|
|
2457
|
+
if (definition.materials.occlusion) bakeOcclusion([
|
|
2458
|
+
wood,
|
|
2459
|
+
leafBuffers,
|
|
2460
|
+
fruitBuffers
|
|
2461
|
+
], leafBuffers, crown);
|
|
1820
2462
|
return {
|
|
1821
2463
|
seed,
|
|
1822
2464
|
skeleton,
|
|
1823
2465
|
wood,
|
|
1824
2466
|
leaves: leafBuffers,
|
|
1825
|
-
|
|
2467
|
+
fruit: fruitBuffers,
|
|
2468
|
+
growth,
|
|
2469
|
+
crown
|
|
1826
2470
|
};
|
|
1827
2471
|
}
|
|
1828
2472
|
/**
|
|
1829
|
-
* Height of the (possibly Sculpted)
|
|
1830
|
-
* delete
|
|
2473
|
+
* Height of the tallest (possibly Sculpted) Stem: the scale of a tree's Wind sway. For an upright
|
|
2474
|
+
* trunk that is its tip. A Sculpt may delete every Stem (an empty tree, nothing sways); it is then 1.
|
|
1831
2475
|
*/
|
|
1832
2476
|
function trunkHeight(skeleton) {
|
|
1833
|
-
const
|
|
1834
|
-
|
|
2477
|
+
const stems = skeleton.branches.filter((b) => b.parent === null);
|
|
2478
|
+
if (stems.length === 0) return 1;
|
|
2479
|
+
return Math.max(...stems.flatMap((stem) => stem.points.map((p) => p.position[1])));
|
|
1835
2480
|
}
|
|
1836
2481
|
/** Stable 32-bit hash (hex) of the generated geometry buffers, for determinism checks. */
|
|
1837
2482
|
function hashGeometry(geometry) {
|
|
1838
2483
|
return hex(hashBuffers(geometry));
|
|
1839
2484
|
}
|
|
1840
|
-
/** Stable hash of a tree's generated geometry (wood and
|
|
2485
|
+
/** Stable hash of a tree's generated geometry (wood, leaves and, if any, Fruit). */
|
|
1841
2486
|
function hashTreeData(data) {
|
|
1842
|
-
|
|
2487
|
+
const h = hashBuffers(data.leaves, hashBuffers(data.wood));
|
|
2488
|
+
return hex(data.fruit.positions.length > 0 ? hashBuffers(data.fruit, h) : h);
|
|
1843
2489
|
}
|
|
1844
2490
|
function hashBuffers(geometry, h) {
|
|
1845
2491
|
const arrays = [
|
|
@@ -1874,7 +2520,7 @@ var SAPLING_SCALE = .2;
|
|
|
1874
2520
|
var SAPLING_THICKNESS = .4;
|
|
1875
2521
|
/** Smallest window width, so parts with `start == end` (the trunk) are simply grown. */
|
|
1876
2522
|
var MIN_WINDOW = 1e-5;
|
|
1877
|
-
var treeScale
|
|
2523
|
+
var treeScale = (growth) => SAPLING_SCALE + .8 * growth;
|
|
1878
2524
|
var thickness$1 = (growth) => SAPLING_THICKNESS + .6 * growth;
|
|
1879
2525
|
/** How far one part has sprouted (0 = not yet, 1 = full size), eased; 1 from `end` on. */
|
|
1880
2526
|
function partProgress$1(growth, start, end) {
|
|
@@ -1894,7 +2540,7 @@ function growPositions(buffers, growth) {
|
|
|
1894
2540
|
const { positions, normals } = buffers;
|
|
1895
2541
|
const { aOrigin, aGrowth } = buffers.attributes;
|
|
1896
2542
|
const out = new Float32Array(positions.length);
|
|
1897
|
-
const scale = treeScale
|
|
2543
|
+
const scale = treeScale(g);
|
|
1898
2544
|
const thin = 1 - thickness$1(g);
|
|
1899
2545
|
for (let v = 0; v < positions.length / 3; v++) {
|
|
1900
2546
|
const [start, end, radius] = [
|
|
@@ -1913,8 +2559,8 @@ function growPositions(buffers, growth) {
|
|
|
1913
2559
|
return out;
|
|
1914
2560
|
}
|
|
1915
2561
|
// src/growth/tsl.ts
|
|
1916
|
-
/** Size of the whole tree at Growth `g` (twin of `treeScale`). */
|
|
1917
|
-
var
|
|
2562
|
+
/** Size of the whole tree at Growth `g` (twin of `treeScale`): the scale `growthPosition` gives the tree. */
|
|
2563
|
+
var growthScale = (g) => float(SAPLING_SCALE).add(float(1 - SAPLING_SCALE).mul(g));
|
|
1918
2564
|
/** Branch thickness at Growth `g` (twin of `thickness`). */
|
|
1919
2565
|
var thickness = (g) => float(SAPLING_THICKNESS).add(float(1 - SAPLING_THICKNESS).mul(g));
|
|
1920
2566
|
/** How far one part has sprouted, eased (twin of `partProgress`). */
|
|
@@ -1940,13 +2586,31 @@ function growthPosition(growth, position = positionLocal) {
|
|
|
1940
2586
|
data.y,
|
|
1941
2587
|
data.z
|
|
1942
2588
|
];
|
|
1943
|
-
const scale =
|
|
2589
|
+
const scale = growthScale(g);
|
|
1944
2590
|
const thin = float(1).sub(thickness(g));
|
|
1945
2591
|
const shrink = float(1).sub(partProgress(g, start, end));
|
|
1946
2592
|
const thick = position.sub(normalGeometry.mul(radius).mul(thin));
|
|
1947
2593
|
const sprouted = thick.sub(thick.sub(origin).mul(shrink));
|
|
1948
2594
|
return sprouted.sub(sprouted.mul(float(1).sub(scale)));
|
|
1949
2595
|
}
|
|
2596
|
+
// src/materials/ground-tint.ts
|
|
2597
|
+
/**
|
|
2598
|
+
* Ground Tint (GLOSSARY): how far the bark blends towards `tint.color` at tree-space height `y`
|
|
2599
|
+
* (on the grown tree): `strength` at and below the ground, easing (smoothstep) to 0 at `height`.
|
|
2600
|
+
*/
|
|
2601
|
+
function groundTintAmount(tint, y) {
|
|
2602
|
+
const t = Math.min(Math.max(y / tint.height, 0), 1);
|
|
2603
|
+
return tint.strength * (1 - t * t * (3 - 2 * t));
|
|
2604
|
+
}
|
|
2605
|
+
/**
|
|
2606
|
+
* The TSL twin of {@link groundTintAmount}, applied to a bark colour node: `mix(bark, tint, amount)`.
|
|
2607
|
+
* The height is the grown tree-space position (Growth `growth`, before Wind), from the vertex stage.
|
|
2608
|
+
*/
|
|
2609
|
+
function applyGroundTint(bark, tint, growth) {
|
|
2610
|
+
const y = varying(growthPosition(growth, positionGeometry).y);
|
|
2611
|
+
const amount = float(1).sub(smoothstep(0, tint.height, y)).mul(tint.strength);
|
|
2612
|
+
return mix(bark, color(tint.color), amount);
|
|
2613
|
+
}
|
|
1950
2614
|
// src/textures/index.ts
|
|
1951
2615
|
var TEXTURES_DIR = "./textures/";
|
|
1952
2616
|
var MODULE_URL = import.meta.url;
|
|
@@ -2055,6 +2719,78 @@ function applyLeafMap(material, leaf, textures) {
|
|
|
2055
2719
|
material.alphaTest = leaf.alphaTest;
|
|
2056
2720
|
}
|
|
2057
2721
|
var load = (url, kind, textures) => loadTexture(resolveTextureUrl(url, textures), kind);
|
|
2722
|
+
// src/materials/crown-nodes.ts
|
|
2723
|
+
/**
|
|
2724
|
+
* The Crown Volume (GLOSSARY) measured at the grown tree-space position (Growth `growth`, before Wind,
|
|
2725
|
+
* so Wind doesn't make it flicker). The Volume scales with the tree's Growth scale, as the vertices do,
|
|
2726
|
+
* so a sapling is measured against its own, smaller crown. Shared by the Material Features.
|
|
2727
|
+
*/
|
|
2728
|
+
function crownNodes(crown, growth) {
|
|
2729
|
+
const local = varying(growthPosition(growth, positionGeometry), "vCrownPosition").div(growthScale(float(growth).clamp(0, 1))).sub(vec3(...crown.center));
|
|
2730
|
+
const radii = vec3(...crown.radii);
|
|
2731
|
+
const unit = local.div(radii);
|
|
2732
|
+
const gradient = unit.div(radii);
|
|
2733
|
+
return {
|
|
2734
|
+
normal: select(length(gradient).greaterThan(1e-6), normalize(gradient), vec3(0, 1, 0)),
|
|
2735
|
+
distance: length(unit)
|
|
2736
|
+
};
|
|
2737
|
+
}
|
|
2738
|
+
// src/materials/leaf-translucency.ts
|
|
2739
|
+
/** The lighting context of a lighting model's builder, typed (see {@link LightingContext}). */
|
|
2740
|
+
var lightingContext = (builder) => builder.context;
|
|
2741
|
+
/** A lighting model's `direct` input, typed (see {@link DirectLight}). */
|
|
2742
|
+
var directLight = (input) => input;
|
|
2743
|
+
/**
|
|
2744
|
+
* How narrow the Leaf Translucency lobe (light from behind the leaf) is: `back = saturate(dot(V, -L))^BACK_POWER`. Higher = only when
|
|
2745
|
+
* looking almost straight into the light through the leaf.
|
|
2746
|
+
*/
|
|
2747
|
+
var BACK_POWER = 3;
|
|
2748
|
+
/**
|
|
2749
|
+
* The term for a leaf material: `values` the settings of `materials.leaf.translucency`, `crown` the tree's
|
|
2750
|
+
* Crown Volume (`TreeData.crown`, tree space, fully grown) and `growth` the Growth node the material
|
|
2751
|
+
* grows with (the Tree's uniform, or a Forest's per-instance Growth).
|
|
2752
|
+
*
|
|
2753
|
+
* Thinness is the Crown Volume distance (`crownNodes`): measured in the tree's own space on the grown
|
|
2754
|
+
* position before Wind (so Wind doesn't make it flicker), against the Volume scaled by the same Growth scale
|
|
2755
|
+
* the vertices get. Being in tree space, it is the same for every Forest instance whatever its rotation and
|
|
2756
|
+
* scale. It needs no vertex attribute.
|
|
2757
|
+
*/
|
|
2758
|
+
function leafTranslucencyTerm(values, crown, growth) {
|
|
2759
|
+
return {
|
|
2760
|
+
strength: values.strength,
|
|
2761
|
+
ambient: values.ambient,
|
|
2762
|
+
edge: values.edge,
|
|
2763
|
+
thinness: crownNodes(crown, growth).distance.clamp(0, 1)
|
|
2764
|
+
};
|
|
2765
|
+
}
|
|
2766
|
+
/** `mix(1, thinness, edge)`: 1 everywhere at edge 0, only the crown's thin edge glows at edge 1. */
|
|
2767
|
+
var edgeBoost = (term) => mix(float(1), term.thinness, term.edge);
|
|
2768
|
+
/**
|
|
2769
|
+
* Direct light shining through the leaf towards the viewer, for one light (add it to `directDiffuse`):
|
|
2770
|
+
* `lightColor × leaf diffuse × strength × back × edgeBoost`, where `back` is a smooth power of
|
|
2771
|
+
* dot(view direction, -light direction). `lightColor` already carries the light's shadow, so shadowed
|
|
2772
|
+
* leaves don't glow. The leaf diffuse is its Season colour (Season Profile x the Seed's tint x leaf map).
|
|
2773
|
+
* Unlike the lit side it has no Lambert 1/pi: like a real leaf against the sun, a strongly translucent leaf
|
|
2774
|
+
* glows brighter than one lit from the front.
|
|
2775
|
+
*/
|
|
2776
|
+
function translucencyDirect(term, lightDirection, lightColor) {
|
|
2777
|
+
const back = positionViewDirection.dot(lightDirection.negate()).clamp(0, 1).pow(BACK_POWER);
|
|
2778
|
+
return lightColor.mul(diffuseColor.rgb).mul(term.strength).mul(back).mul(edgeBoost(term));
|
|
2779
|
+
}
|
|
2780
|
+
/**
|
|
2781
|
+
* Indirect light shining through the leaf (add it to `indirectDiffuse` after the model's own AO step):
|
|
2782
|
+
* `irradiance × leaf diffuse × ambient × edgeBoost × ambient occlusion`, the leaf diffuse through the
|
|
2783
|
+
* Lambert BRDF like the rest of the indirect light.
|
|
2784
|
+
*
|
|
2785
|
+
* The irradiance from behind would be the irradiance sampled along the flipped normal; three's public
|
|
2786
|
+
* lighting-model API only exposes the irradiance already gathered along the normal (`context.irradiance`),
|
|
2787
|
+
* so that stands in for it. It is exact for ambient light and light probes' constant part; under a
|
|
2788
|
+
* hemisphere light it reads the side facing the viewer.
|
|
2789
|
+
*/
|
|
2790
|
+
function translucencyIndirect(term, builder) {
|
|
2791
|
+
const { ambientOcclusion, irradiance } = lightingContext(builder);
|
|
2792
|
+
return irradiance.mul(BRDF_Lambert({ diffuseColor: diffuseColor.rgb })).mul(term.ambient).mul(edgeBoost(term)).mul(ambientOcclusion);
|
|
2793
|
+
}
|
|
2058
2794
|
// src/materials/toon.ts
|
|
2059
2795
|
/** Brightness of the shaded side, relative to the lit side (the toon "shadow tone"). */
|
|
2060
2796
|
var SHADE = .5;
|
|
@@ -2065,18 +2801,32 @@ var BAND_END = .2;
|
|
|
2065
2801
|
* Soft toon lighting: two tones (lit and shaded) per light with a smooth band in between,
|
|
2066
2802
|
* plus plain diffuse ambient/hemisphere light. No specular or environment reflections. Built
|
|
2067
2803
|
* in TSL from the light direction only, so it needs no gradient texture (no image files).
|
|
2804
|
+
*
|
|
2805
|
+
* With a Leaf Translucency term (leaf material only, `applyLeafTranslucency`), leaves with light behind them also glow:
|
|
2806
|
+
* the same shared term the physical leaf model adds.
|
|
2068
2807
|
*/
|
|
2069
2808
|
var SoftToonLightingModel = class extends LightingModel {
|
|
2809
|
+
translucency;
|
|
2810
|
+
constructor(translucency = null) {
|
|
2811
|
+
super();
|
|
2812
|
+
this.translucency = translucency;
|
|
2813
|
+
}
|
|
2814
|
+
/** Whether this model adds Leaf Translucency. */
|
|
2815
|
+
get isLeafTranslucencyModel() {
|
|
2816
|
+
return this.translucency !== null;
|
|
2817
|
+
}
|
|
2070
2818
|
direct({ lightDirection, lightColor, reflectedLight }) {
|
|
2071
2819
|
const dotNL = normalView.dot(lightDirection);
|
|
2072
2820
|
const tone = mix(float(SHADE), float(1), smoothstep(BAND_START, BAND_END, dotNL));
|
|
2073
2821
|
const irradiance = lightColor.mul(tone);
|
|
2074
2822
|
reflectedLight.directDiffuse.addAssign(irradiance.mul(BRDF_Lambert({ diffuseColor: diffuseColor.rgb })));
|
|
2823
|
+
if (this.translucency) reflectedLight.directDiffuse.addAssign(translucencyDirect(this.translucency, lightDirection, lightColor));
|
|
2075
2824
|
}
|
|
2076
2825
|
indirect(builder) {
|
|
2077
2826
|
const { ambientOcclusion, irradiance, reflectedLight } = builder.context;
|
|
2078
2827
|
reflectedLight.indirectDiffuse.addAssign(irradiance.mul(BRDF_Lambert({ diffuseColor })));
|
|
2079
2828
|
reflectedLight.indirectDiffuse.mulAssign(ambientOcclusion);
|
|
2829
|
+
if (this.translucency) reflectedLight.indirectDiffuse.addAssign(translucencyIndirect(this.translucency, builder));
|
|
2080
2830
|
}
|
|
2081
2831
|
};
|
|
2082
2832
|
/**
|
|
@@ -2111,19 +2861,43 @@ function createShadedMaterial(shading) {
|
|
|
2111
2861
|
}
|
|
2112
2862
|
/** Whether a shading model wants solid (flat) colours rather than smooth colour gradients. */
|
|
2113
2863
|
var isFlatShading = (shading) => shading === "flat";
|
|
2864
|
+
// src/materials/crown-normals.ts
|
|
2865
|
+
/**
|
|
2866
|
+
* Crown Normals (GLOSSARY): blends the leaf material's normal from the leaf's own normal towards the
|
|
2867
|
+
* Crown Volume's normal at that point, by `amount` (0..1, a uniform: Definitions that differ only in it
|
|
2868
|
+
* share one shader, and a Tree tunes it live). Only call it when the feature is on (amount > 0); without
|
|
2869
|
+
* it the material keeps its own normal.
|
|
2870
|
+
*
|
|
2871
|
+
* - `growth`: the Growth node of the material's `positionNode` (the Volume scales with it).
|
|
2872
|
+
* - `toObject`: turns a tree-space normal into the mesh's own space (a Forest rotates it by the
|
|
2873
|
+
* instance, as it does the Growth and Wind displacement); default: tree space is object space.
|
|
2874
|
+
*
|
|
2875
|
+
* The leaf normal is the material's view-space normal: the screen-space facet normal under flat
|
|
2876
|
+
* shading (so facets stay visible below 1), the smooth one facing the viewer otherwise.
|
|
2877
|
+
*/
|
|
2878
|
+
function applyCrownNormals(material, amount, crown, growth, toObject = (normal) => normal) {
|
|
2879
|
+
const crownView = transformNormalToView(toObject(crownNodes(crown, growth).normal)).normalize();
|
|
2880
|
+
const blend = mix(normalView, crownView, amount);
|
|
2881
|
+
material.normalNode = select(length(blend).greaterThan(1e-4), blend.normalize(), crownView);
|
|
2882
|
+
}
|
|
2114
2883
|
// src/materials/index.ts
|
|
2115
2884
|
/**
|
|
2116
2885
|
* Bark material: a TSL node material (ADR 0002). Growth/Season/Wind uniforms and
|
|
2117
2886
|
* per-Style variants hook in here in later tickets (#5-#10). `materials.bark.map` / `.normalMap`
|
|
2118
2887
|
* load through `textures` (base path / URL overrides, see `TextureOptions`).
|
|
2888
|
+
*
|
|
2889
|
+
* `growth`: the Growth node the material's `positionNode` grows the tree with (default: fully grown).
|
|
2890
|
+
* A Ground Tint (`materials.bark.groundTint`) is measured on that grown position; without one it is unused.
|
|
2119
2891
|
*/
|
|
2120
|
-
function createBarkMaterial(definition, textures) {
|
|
2892
|
+
function createBarkMaterial(definition, textures, growth = float(1)) {
|
|
2121
2893
|
const material = createShadedMaterial(definition.materials.shading);
|
|
2122
2894
|
material.name = "tree.js bark";
|
|
2123
2895
|
material.userData.treePart = "bark";
|
|
2124
2896
|
const base = color(definition.materials.bark.color);
|
|
2125
2897
|
const shaded = isFlatShading(definition.materials.shading) ? base : mix(base.mul(.75), base, uv().y.clamp(0, 1).pow(float(.5)));
|
|
2126
|
-
|
|
2898
|
+
const { groundTint } = definition.materials.bark;
|
|
2899
|
+
const tinted = groundTint ? applyGroundTint(shaded, groundTint, growth) : shaded;
|
|
2900
|
+
material.colorNode = applyBarkMaps(material, definition.materials.bark, tinted, textures);
|
|
2127
2901
|
material.roughness = definition.materials.bark.roughness;
|
|
2128
2902
|
material.metalness = 0;
|
|
2129
2903
|
return material;
|
|
@@ -2146,6 +2920,147 @@ function createLeafMaterial(definition, textures) {
|
|
|
2146
2920
|
applyLeafMap(material, definition.materials.leaf, textures);
|
|
2147
2921
|
return material;
|
|
2148
2922
|
}
|
|
2923
|
+
/**
|
|
2924
|
+
* Fruit material: the Definition's shading (flat facets in low-poly, smooth toon in stylized,
|
|
2925
|
+
* smooth PBR in realistic), no textures. `.color` is the Fruit colour at Season 0; `Tree` sets the
|
|
2926
|
+
* Season colour (`createFruitSeasonNodes`), multiplied by the per-Fruit vertex colours (the Seed's tint).
|
|
2927
|
+
*/
|
|
2928
|
+
function createFruitMaterial(definition) {
|
|
2929
|
+
const material = createShadedMaterial(definition.materials.shading);
|
|
2930
|
+
material.name = "tree.js fruit";
|
|
2931
|
+
material.userData.treePart = "fruit";
|
|
2932
|
+
material.color.set(fruitTracks(definition.season).fruitColor[0].value);
|
|
2933
|
+
material.vertexColors = true;
|
|
2934
|
+
material.roughness = FRUIT_ROUGHNESS;
|
|
2935
|
+
material.metalness = 0;
|
|
2936
|
+
return material;
|
|
2937
|
+
}
|
|
2938
|
+
/** Fruit skin is a little glossier than leaves. */
|
|
2939
|
+
var FRUIT_ROUGHNESS = .45;
|
|
2940
|
+
// src/materials/leaf-translucency-model.ts
|
|
2941
|
+
/**
|
|
2942
|
+
* three's physical lighting model plus Leaf Translucency (flat and standard leaf materials): everything
|
|
2943
|
+
* the physical model does, and per light and for the indirect light the shared translucency term.
|
|
2944
|
+
* Only the public `direct` / `indirect` entry points are extended.
|
|
2945
|
+
*/
|
|
2946
|
+
var TranslucentLightingModel = class extends PhysicalLightingModel {
|
|
2947
|
+
translucency;
|
|
2948
|
+
isLeafTranslucencyModel = true;
|
|
2949
|
+
constructor(translucency) {
|
|
2950
|
+
super();
|
|
2951
|
+
this.translucency = translucency;
|
|
2952
|
+
}
|
|
2953
|
+
direct(input, builder) {
|
|
2954
|
+
super.direct(input, builder);
|
|
2955
|
+
const { lightDirection, lightColor, reflectedLight } = directLight(input);
|
|
2956
|
+
reflectedLight.directDiffuse.addAssign(translucencyDirect(this.translucency, lightDirection, lightColor));
|
|
2957
|
+
}
|
|
2958
|
+
indirect(builder) {
|
|
2959
|
+
super.indirect(builder);
|
|
2960
|
+
lightingContext(builder).reflectedLight.indirectDiffuse.addAssign(translucencyIndirect(this.translucency, builder));
|
|
2961
|
+
}
|
|
2962
|
+
};
|
|
2963
|
+
/**
|
|
2964
|
+
* Gives a leaf material Leaf Translucency (`materials.leaf.translucency` as uniforms, a Material Feature, ADR 0008):
|
|
2965
|
+
* its lighting model becomes the translucent one (the physical model with the term for flat and standard
|
|
2966
|
+
* shading, the soft toon model with the same term for toon). The material keeps its class and every node.
|
|
2967
|
+
* Only call it when the feature is set: without it the leaf material keeps today's lighting model.
|
|
2968
|
+
*
|
|
2969
|
+
* `crown` is the tree's Crown Volume (`TreeData.crown`); `growth` the Growth node the material's
|
|
2970
|
+
* `positionNode` grows with (a Tree's Growth uniform, or a Forest's per-instance Growth).
|
|
2971
|
+
*/
|
|
2972
|
+
function applyLeafTranslucency(material, values, crown, growth) {
|
|
2973
|
+
const term = leafTranslucencyTerm(values, crown, growth);
|
|
2974
|
+
const toon = material instanceof SoftToonNodeMaterial;
|
|
2975
|
+
material.setupLightingModel = () => toon ? new SoftToonLightingModel(term) : new TranslucentLightingModel(term);
|
|
2976
|
+
material.needsUpdate = true;
|
|
2977
|
+
}
|
|
2978
|
+
// src/materials/occlusion.ts
|
|
2979
|
+
/**
|
|
2980
|
+
* Structural Occlusion (GLOSSARY, ADR 0008) on a tree's own materials: each material's `aoNode` becomes
|
|
2981
|
+
* `mix(1, aOcclusion, strength)`, from the Generator's baked `aOcclusion` (only generated when
|
|
2982
|
+
* `materials.occlusion` is set). three applies it as it does the material's built-in AO (multiplied with an
|
|
2983
|
+
* app's SSAO/GTAO through `context.getAO`), so it only darkens ambient and indirect light; the toon model's
|
|
2984
|
+
* indirect light multiplies by it too. `strength` is a uniform, so tuning it needs no recompile.
|
|
2985
|
+
*/
|
|
2986
|
+
function applyStructuralOcclusion(materials, strength) {
|
|
2987
|
+
const ao = mix(float(1), attribute("aOcclusion", "float"), strength);
|
|
2988
|
+
for (const material of materials) if (material) material.aoNode = ao;
|
|
2989
|
+
}
|
|
2990
|
+
// src/materials/material-features.ts
|
|
2991
|
+
var clamp01 = (value) => Math.min(Math.max(value, 0), 1);
|
|
2992
|
+
/**
|
|
2993
|
+
* The Material Features (GLOSSARY, ADR 0008) of a Tree or Forest: applies those the Definition sets to
|
|
2994
|
+
* the materials of each target and owns their values as uniforms, shared by all targets, so a feature
|
|
2995
|
+
* that is on can be tuned live (no regeneration, no recompile). A feature that is off leaves the
|
|
2996
|
+
* materials exactly as they are and cannot be tuned (switching one on needs a new Tree or Forest).
|
|
2997
|
+
*/
|
|
2998
|
+
var LiveMaterialFeatures = class {
|
|
2999
|
+
crown;
|
|
3000
|
+
translucencyValues;
|
|
3001
|
+
occlusion;
|
|
3002
|
+
constructor(definition, targets) {
|
|
3003
|
+
const { crownNormals, translucency, occlusion } = materialFeaturesOf(definition);
|
|
3004
|
+
this.crown = crownNormals ? uniform(crownNormals) : null;
|
|
3005
|
+
this.translucencyValues = translucency ? {
|
|
3006
|
+
strength: uniform(translucency.strength),
|
|
3007
|
+
edge: uniform(translucency.edge),
|
|
3008
|
+
ambient: uniform(translucency.ambient)
|
|
3009
|
+
} : null;
|
|
3010
|
+
this.occlusion = occlusion ? uniform(occlusion.strength) : null;
|
|
3011
|
+
for (const { leaf, materials, crown, growth, toObject } of targets) {
|
|
3012
|
+
if (this.crown) applyCrownNormals(leaf, this.crown, crown, growth, toObject);
|
|
3013
|
+
if (this.translucencyValues) applyLeafTranslucency(leaf, this.translucencyValues, crown, growth);
|
|
3014
|
+
if (this.occlusion) applyStructuralOcclusion(materials, this.occlusion);
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3017
|
+
/** The features that are on, with their current values (same shape as `materialFeaturesOf`). */
|
|
3018
|
+
get values() {
|
|
3019
|
+
return {
|
|
3020
|
+
...this.crown ? { crownNormals: this.crown.value } : {},
|
|
3021
|
+
...this.translucencyValues ? { translucency: {
|
|
3022
|
+
strength: this.translucencyValues.strength.value,
|
|
3023
|
+
edge: this.translucencyValues.edge.value,
|
|
3024
|
+
ambient: this.translucencyValues.ambient.value
|
|
3025
|
+
} } : {},
|
|
3026
|
+
...this.occlusion ? { occlusion: { strength: this.occlusion.value } } : {}
|
|
3027
|
+
};
|
|
3028
|
+
}
|
|
3029
|
+
/** Crown Normals amount, null when off. */
|
|
3030
|
+
get crownNormals() {
|
|
3031
|
+
return this.crown?.value ?? null;
|
|
3032
|
+
}
|
|
3033
|
+
/** Sets the Crown Normals amount (clamped to 0..1); throws when the feature is off. */
|
|
3034
|
+
set crownNormals(value) {
|
|
3035
|
+
if (!this.crown) throw new Error("This tree has no Crown Normals: set `materials.leaf.crownNormals` in its Tree Definition to tune the amount");
|
|
3036
|
+
this.crown.value = clamp01(value);
|
|
3037
|
+
}
|
|
3038
|
+
/** Leaf Translucency settings (a copy), null when off. */
|
|
3039
|
+
get translucency() {
|
|
3040
|
+
return this.values.translucency ?? null;
|
|
3041
|
+
}
|
|
3042
|
+
/** Sets any of the Leaf Translucency settings (each clamped to 0..1; the others kept); throws when the feature is off. */
|
|
3043
|
+
set translucency(value) {
|
|
3044
|
+
if (!this.translucencyValues) throw new Error("This tree has no Leaf Translucency: set `materials.leaf.translucency` in its Tree Definition to tune it");
|
|
3045
|
+
for (const key of [
|
|
3046
|
+
"strength",
|
|
3047
|
+
"edge",
|
|
3048
|
+
"ambient"
|
|
3049
|
+
]) {
|
|
3050
|
+
const v = value[key];
|
|
3051
|
+
if (v !== void 0) this.translucencyValues[key].value = clamp01(v);
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
/** Structural Occlusion strength, null when off. */
|
|
3055
|
+
get occlusionStrength() {
|
|
3056
|
+
return this.occlusion?.value ?? null;
|
|
3057
|
+
}
|
|
3058
|
+
/** Sets the Structural Occlusion strength (clamped to 0..1); throws when the feature is off. */
|
|
3059
|
+
set occlusionStrength(value) {
|
|
3060
|
+
if (!this.occlusion) throw new Error("This tree has no Structural Occlusion: set `materials.occlusion` in its Tree Definition to tune its strength");
|
|
3061
|
+
this.occlusion.value = clamp01(value);
|
|
3062
|
+
}
|
|
3063
|
+
};
|
|
2149
3064
|
// src/renderer/index.ts
|
|
2150
3065
|
/** Thrown when a tree is drawn by anything other than `WebGPURenderer` (ADR 0002). */
|
|
2151
3066
|
var UnsupportedRendererError = class extends Error {
|
|
@@ -2178,8 +3093,7 @@ function keyframeNode(s, track, toUniform) {
|
|
|
2178
3093
|
}
|
|
2179
3094
|
function createSeasonNodes(profile, season = uniform(0)) {
|
|
2180
3095
|
const aSeason = attribute("aSeason", "vec3");
|
|
2181
|
-
const
|
|
2182
|
-
const s = select(shifted.greaterThan(1).or(shifted.lessThan(0)), fract(shifted), shifted);
|
|
3096
|
+
const s = shiftedSeason$1(season, aSeason);
|
|
2183
3097
|
const leafColor = keyframeNode(s, profile.leafColor, (v) => uniform(new Color(v)));
|
|
2184
3098
|
const coverage = keyframeNode(s, profile.leafCoverage, (v) => uniform(v));
|
|
2185
3099
|
const blossom = keyframeNode(s, profile.blossom, (v) => uniform(v));
|
|
@@ -2192,6 +3106,30 @@ function createSeasonNodes(profile, season = uniform(0)) {
|
|
|
2192
3106
|
visible: varying(select(shown, float(1), float(0))).greaterThan(.5)
|
|
2193
3107
|
};
|
|
2194
3108
|
}
|
|
3109
|
+
/** The part's own Season: the Season input plus the `aSeason.x` timing shift, wrapped around the year (as `wrapSeason`). */
|
|
3110
|
+
function shiftedSeason$1(season, aSeason) {
|
|
3111
|
+
const shifted = season.add(aSeason.x);
|
|
3112
|
+
return select(shifted.greaterThan(1).or(shifted.lessThan(0)), fract(shifted), shifted);
|
|
3113
|
+
}
|
|
3114
|
+
/**
|
|
3115
|
+
* Builds the TSL nodes that evaluate the Season Profile's Fruit tracks per Fruit, from the fruit
|
|
3116
|
+
* mesh's `aSeason` (timing shift, Fruit rank). The TSL twin of `fruitSeason`: a Fruit shows while
|
|
3117
|
+
* the Fruit amount is above its rank and swells with the amount. `season` is the Season input
|
|
3118
|
+
* (share the leaves' uniform so one write drives both).
|
|
3119
|
+
*/
|
|
3120
|
+
function createFruitSeasonNodes(profile, season, position = positionLocal) {
|
|
3121
|
+
const aSeason = attribute("aSeason", "vec3");
|
|
3122
|
+
const origin = attribute("aOrigin", "vec3");
|
|
3123
|
+
const s = shiftedSeason$1(season, aSeason);
|
|
3124
|
+
const tracks = fruitTracks(profile);
|
|
3125
|
+
const amount = keyframeNode(s, tracks.fruit, (v) => uniform(v));
|
|
3126
|
+
const scale = select(amount.greaterThan(aSeason.y), amount, float(0));
|
|
3127
|
+
return {
|
|
3128
|
+
color: varying(keyframeNode(s, tracks.fruitColor, (v) => uniform(new Color(v)))),
|
|
3129
|
+
scale,
|
|
3130
|
+
position: origin.add(position.sub(origin).mul(scale))
|
|
3131
|
+
};
|
|
3132
|
+
}
|
|
2195
3133
|
/**
|
|
2196
3134
|
* Hooks Season into a leaf material: `colorNode` (times the vertex-colour tint, and times the
|
|
2197
3135
|
* material's existing `colorNode`, such as a leaf card texture with alpha) and `maskNode`, which
|
|
@@ -2439,13 +3377,16 @@ var Tree = class extends Group {
|
|
|
2439
3377
|
wood;
|
|
2440
3378
|
/** Leaf cards (empty geometry for a bare tree). */
|
|
2441
3379
|
leaves;
|
|
2442
|
-
/**
|
|
3380
|
+
/** Fruit (empty geometry when the Definition has no Fruit). */
|
|
3381
|
+
fruit;
|
|
3382
|
+
/** This tree's Growth uniform (read by the wood, leaf and Fruit materials' `positionNode`). */
|
|
2443
3383
|
growthUniform;
|
|
2444
|
-
/** The Season uniform the leaf
|
|
3384
|
+
/** The Season uniform the leaf and Fruit materials read (shared nodes may reference it). */
|
|
2445
3385
|
seasonUniform;
|
|
2446
3386
|
/** This tree's response to the Wind, from `definition.wind`; `windResponse.set(...)` changes it live. */
|
|
2447
3387
|
windResponse;
|
|
2448
3388
|
windBinding;
|
|
3389
|
+
features;
|
|
2449
3390
|
/**
|
|
2450
3391
|
* Resolves once every texture of this tree has loaded (at once for a tree without textures);
|
|
2451
3392
|
* rejects if one fails. Await it before a first render that must show the textures.
|
|
@@ -2458,7 +3399,7 @@ var Tree = class extends Group {
|
|
|
2458
3399
|
this.seed = data.seed;
|
|
2459
3400
|
this.data = data;
|
|
2460
3401
|
this.growthUniform = uniform(clampGrowth(options.growth ?? 1));
|
|
2461
|
-
const bark = createBarkMaterial(definition, options.textures);
|
|
3402
|
+
const bark = createBarkMaterial(definition, options.textures, this.growthUniform);
|
|
2462
3403
|
const leaf = createLeafMaterial(definition, options.textures);
|
|
2463
3404
|
bark.positionNode = growthPosition(this.growthUniform);
|
|
2464
3405
|
leaf.positionNode = growthPosition(this.growthUniform);
|
|
@@ -2471,7 +3412,9 @@ var Tree = class extends Group {
|
|
|
2471
3412
|
].filter((t) => t !== null);
|
|
2472
3413
|
this.ready = Promise.all(textures.map(textureReady)).then(() => void 0);
|
|
2473
3414
|
this.ready.catch(() => {});
|
|
2474
|
-
|
|
3415
|
+
const fruit = createFruitMaterial(definition);
|
|
3416
|
+
this.fruit = treeMesh("fruit", toBufferGeometry(data.fruit), fruit);
|
|
3417
|
+
this.add(this.wood, this.leaves, this.fruit);
|
|
2475
3418
|
this.windBinding = new WindBinding(options.wind ?? sceneWind);
|
|
2476
3419
|
this.windResponse = new WindResponse(definition.wind, {
|
|
2477
3420
|
height: trunkHeight(data.skeleton),
|
|
@@ -2483,7 +3426,61 @@ var Tree = class extends Group {
|
|
|
2483
3426
|
const season = createSeasonNodes(definition.season);
|
|
2484
3427
|
applySeasonToLeafMaterial(this.leaves.material, season);
|
|
2485
3428
|
this.seasonUniform = season.season;
|
|
3429
|
+
const fruitSeason = createFruitSeasonNodes(definition.season, this.seasonUniform);
|
|
3430
|
+
fruit.colorNode = fruitSeason.color;
|
|
3431
|
+
fruit.positionNode = growthPosition(this.growthUniform, fruitSeason.position);
|
|
3432
|
+
applyWind(fruit, offset);
|
|
2486
3433
|
this.season = options.season ?? 0;
|
|
3434
|
+
this.features = new LiveMaterialFeatures(definition, [{
|
|
3435
|
+
leaf,
|
|
3436
|
+
materials: [
|
|
3437
|
+
bark,
|
|
3438
|
+
leaf,
|
|
3439
|
+
fruit
|
|
3440
|
+
],
|
|
3441
|
+
crown: data.crown,
|
|
3442
|
+
growth: this.growthUniform
|
|
3443
|
+
}]);
|
|
3444
|
+
}
|
|
3445
|
+
/**
|
|
3446
|
+
* Crown Normals amount (0..1), from the Definition's `materials.leaf.crownNormals`; null when it is off.
|
|
3447
|
+
* Setting it (clamped to 0..1) only writes a uniform: no regeneration, no recompile. Throws on a
|
|
3448
|
+
* tree without Crown Normals (switching the feature on needs a new tree).
|
|
3449
|
+
*/
|
|
3450
|
+
get crownNormals() {
|
|
3451
|
+
return this.features.crownNormals;
|
|
3452
|
+
}
|
|
3453
|
+
set crownNormals(value) {
|
|
3454
|
+
this.features.crownNormals = value;
|
|
3455
|
+
}
|
|
3456
|
+
/**
|
|
3457
|
+
* Leaf Translucency settings (a copy), from the Definition's `materials.leaf.translucency`; null when it
|
|
3458
|
+
* is off. Setting any of them (each clamped to 0..1, the others kept) only writes uniforms: no
|
|
3459
|
+
* regeneration, no recompile. Throws on a tree without Leaf Translucency.
|
|
3460
|
+
*/
|
|
3461
|
+
get translucency() {
|
|
3462
|
+
return this.features.translucency;
|
|
3463
|
+
}
|
|
3464
|
+
set translucency(value) {
|
|
3465
|
+
this.features.translucency = value;
|
|
3466
|
+
}
|
|
3467
|
+
/**
|
|
3468
|
+
* The Material Features this tree shows, with their current (possibly live-tuned) values: the
|
|
3469
|
+
* Definition's (see `materialFeaturesOf`), except that Crown Normals at 0 count as off.
|
|
3470
|
+
*/
|
|
3471
|
+
get materialFeatures() {
|
|
3472
|
+
return this.features.values;
|
|
3473
|
+
}
|
|
3474
|
+
/**
|
|
3475
|
+
* Structural Occlusion strength (0..1), from the Definition's `materials.occlusion`; null when it has none.
|
|
3476
|
+
* Setting it (clamped to 0..1) only writes a uniform: no regeneration, no recompile. Throws on a tree
|
|
3477
|
+
* without Structural Occlusion (there is no baked occlusion to show).
|
|
3478
|
+
*/
|
|
3479
|
+
get occlusionStrength() {
|
|
3480
|
+
return this.features.occlusionStrength;
|
|
3481
|
+
}
|
|
3482
|
+
set occlusionStrength(value) {
|
|
3483
|
+
this.features.occlusionStrength = value;
|
|
2487
3484
|
}
|
|
2488
3485
|
/**
|
|
2489
3486
|
* The Wind this tree sways with (shared with every other tree using it). Assigning another
|
|
@@ -2496,8 +3493,8 @@ var Tree = class extends Group {
|
|
|
2496
3493
|
this.windBinding.wind = value;
|
|
2497
3494
|
}
|
|
2498
3495
|
/**
|
|
2499
|
-
* Season: 0 (first day of spring) .. 1 (last day of winter); sets leaf colour, leaf coverage
|
|
2500
|
-
* and
|
|
3496
|
+
* Season: 0 (first day of spring) .. 1 (last day of winter); sets leaf colour, leaf coverage,
|
|
3497
|
+
* blossom and Fruit (amount, size, colour) from the Definition's Season Profile. Values outside 0..1 wrap around the year.
|
|
2501
3498
|
* Only updates a uniform, so it is cheap to animate.
|
|
2502
3499
|
*/
|
|
2503
3500
|
get season() {
|
|
@@ -2547,15 +3544,14 @@ function createTree(definition, options = {}) {
|
|
|
2547
3544
|
// src/styles/registry.ts
|
|
2548
3545
|
/**
|
|
2549
3546
|
* The built-in Styles, each a lazily loaded chunk (`@casmo/tree.js/styles/<name>`).
|
|
2550
|
-
* To add a Style:
|
|
3547
|
+
* To add a Style: its name, label and shading in names.ts, its loader here, its look in apply.ts
|
|
3548
|
+
* (plus its vite entry and package.json export). The editor and the scripts read these lists.
|
|
2551
3549
|
*/
|
|
2552
3550
|
var STYLE_LOADERS = {
|
|
2553
3551
|
"low-poly": () => import("./styles/low-poly.js"),
|
|
2554
3552
|
stylized: () => import("./styles/stylized.js"),
|
|
2555
3553
|
realistic: () => import("./styles/realistic.js")
|
|
2556
3554
|
};
|
|
2557
|
-
/** Names of all built-in Styles. */
|
|
2558
|
-
var STYLE_NAMES = Object.keys(STYLE_LOADERS);
|
|
2559
3555
|
/** Downloads a built-in Style (dynamic import) and resolves to it. Rejects for an unknown name. */
|
|
2560
3556
|
async function loadStyle(name) {
|
|
2561
3557
|
const load = STYLE_LOADERS[name];
|
|
@@ -2577,8 +3573,8 @@ function instanceState() {
|
|
|
2577
3573
|
windPhase: state.w
|
|
2578
3574
|
};
|
|
2579
3575
|
}
|
|
2580
|
-
/** Rotates `v` by the instance quaternion (`inverse` = by its conjugate). */
|
|
2581
|
-
function
|
|
3576
|
+
/** Rotates `v` by the instance quaternion (`inverse` = by its conjugate): tree space to forest space. */
|
|
3577
|
+
function instanceRotate(v, inverse = false) {
|
|
2582
3578
|
const q = attribute("iRotation", "vec4");
|
|
2583
3579
|
const u = inverse ? q.xyz.negate() : q.xyz;
|
|
2584
3580
|
const t = cross(u, v).mul(2);
|
|
@@ -2593,16 +3589,19 @@ function rotate(v, inverse = false) {
|
|
|
2593
3589
|
* `positionGeometry`, and only their displacement is turned into forest space with the
|
|
2594
3590
|
* instance rotation and scale: M·(p + d) = M·p + R·s·d. The Wind direction goes the other way
|
|
2595
3591
|
* (forest -> tree space) and each tree sways with its own phase.
|
|
3592
|
+
*
|
|
3593
|
+
* `base`: the tree-space position Growth starts from (default `positionGeometry`); Fruit passes
|
|
3594
|
+
* its Season-scaled position here.
|
|
2596
3595
|
*/
|
|
2597
|
-
function forestPosition(wind, response) {
|
|
3596
|
+
function forestPosition(wind, response, base = positionGeometry) {
|
|
2598
3597
|
const { scale, growth, windPhase } = instanceState();
|
|
2599
|
-
const grown = growthPosition(growth,
|
|
3598
|
+
const grown = growthPosition(growth, base);
|
|
2600
3599
|
const offset = windOffset(wind, response, {
|
|
2601
|
-
direction:
|
|
3600
|
+
direction: instanceRotate(objectWindDirection(wind), true),
|
|
2602
3601
|
phase: windPhase
|
|
2603
3602
|
});
|
|
2604
3603
|
const displacement = grown.add(offset).sub(positionGeometry);
|
|
2605
|
-
return positionLocal.add(
|
|
3604
|
+
return positionLocal.add(instanceRotate(displacement).mul(scale));
|
|
2606
3605
|
}
|
|
2607
3606
|
// src/forest/placement.ts
|
|
2608
3607
|
function placementPosition(placement) {
|
|
@@ -2764,7 +3763,8 @@ function shareGeometry(source) {
|
|
|
2764
3763
|
}
|
|
2765
3764
|
/**
|
|
2766
3765
|
* One Variant of a Forest: one Seed's tree, drawn once per tree that uses it with an instanced
|
|
2767
|
-
* wood mesh
|
|
3766
|
+
* wood mesh, an instanced leaf mesh and, when the Definition has Fruit, an instanced fruit mesh
|
|
3767
|
+
* (one draw call per material). All three share one instance matrix buffer.
|
|
2768
3768
|
*/
|
|
2769
3769
|
var ForestVariant = class {
|
|
2770
3770
|
forest;
|
|
@@ -2775,13 +3775,17 @@ var ForestVariant = class {
|
|
|
2775
3775
|
windResponse;
|
|
2776
3776
|
barkMaterial;
|
|
2777
3777
|
leafMaterial;
|
|
3778
|
+
/** Fruit material, null when the Definition has no Fruit. */
|
|
3779
|
+
fruitMaterial;
|
|
2778
3780
|
/** The trees drawn as this Variant, in instance order. */
|
|
2779
3781
|
trees = [];
|
|
2780
3782
|
woodMesh;
|
|
2781
3783
|
leafMesh;
|
|
3784
|
+
fruitMesh = null;
|
|
2782
3785
|
instanceData;
|
|
2783
3786
|
woodSource;
|
|
2784
3787
|
leafSource;
|
|
3788
|
+
fruitSource;
|
|
2785
3789
|
constructor(forest, definition, index, seed, wind, capacity, textures) {
|
|
2786
3790
|
this.forest = forest;
|
|
2787
3791
|
this.index = index;
|
|
@@ -2793,13 +3797,23 @@ var ForestVariant = class {
|
|
|
2793
3797
|
phase: 0
|
|
2794
3798
|
});
|
|
2795
3799
|
const position = forestPosition(wind, this.windResponse);
|
|
2796
|
-
this.barkMaterial = createBarkMaterial(definition, textures);
|
|
3800
|
+
this.barkMaterial = createBarkMaterial(definition, textures, instanceState().growth);
|
|
2797
3801
|
this.barkMaterial.positionNode = position;
|
|
2798
3802
|
this.leafMaterial = createLeafMaterial(definition, textures);
|
|
2799
3803
|
this.leafMaterial.positionNode = position;
|
|
2800
3804
|
applySeasonToLeafMaterial(this.leafMaterial, createSeasonNodes(definition.season, instanceState().season));
|
|
2801
3805
|
this.woodSource = interleavedGeometry(this.data.wood, height * WIND_BOUNDS);
|
|
2802
3806
|
this.leafSource = interleavedGeometry(this.data.leaves, height * WIND_BOUNDS);
|
|
3807
|
+
if (this.data.fruit.indices.length > 0) {
|
|
3808
|
+
const fruitSeason = createFruitSeasonNodes(definition.season, instanceState().season, positionGeometry);
|
|
3809
|
+
this.fruitMaterial = createFruitMaterial(definition);
|
|
3810
|
+
this.fruitMaterial.colorNode = fruitSeason.color;
|
|
3811
|
+
this.fruitMaterial.positionNode = forestPosition(wind, this.windResponse, fruitSeason.position);
|
|
3812
|
+
this.fruitSource = interleavedGeometry(this.data.fruit, height * WIND_BOUNDS);
|
|
3813
|
+
} else {
|
|
3814
|
+
this.fruitMaterial = null;
|
|
3815
|
+
this.fruitSource = null;
|
|
3816
|
+
}
|
|
2803
3817
|
this.allocate(capacity);
|
|
2804
3818
|
}
|
|
2805
3819
|
/** Instanced mesh of trunks and branches. Replaced (not resized) when the Variant outgrows its capacity. */
|
|
@@ -2810,6 +3824,18 @@ var ForestVariant = class {
|
|
|
2810
3824
|
get leaves() {
|
|
2811
3825
|
return this.leafMesh;
|
|
2812
3826
|
}
|
|
3827
|
+
/** Instanced mesh of the Fruit (null when the Definition has no Fruit). Replaced along with {@link wood}. */
|
|
3828
|
+
get fruit() {
|
|
3829
|
+
return this.fruitMesh;
|
|
3830
|
+
}
|
|
3831
|
+
/** The Variant's meshes: wood, leaves and (with Fruit) fruit. */
|
|
3832
|
+
get meshes() {
|
|
3833
|
+
return this.fruitMesh ? [
|
|
3834
|
+
this.woodMesh,
|
|
3835
|
+
this.leafMesh,
|
|
3836
|
+
this.fruitMesh
|
|
3837
|
+
] : [this.woodMesh, this.leafMesh];
|
|
3838
|
+
}
|
|
2813
3839
|
get capacity() {
|
|
2814
3840
|
return this.instanceData.count;
|
|
2815
3841
|
}
|
|
@@ -2853,8 +3879,7 @@ var ForestVariant = class {
|
|
|
2853
3879
|
tree.windPhase
|
|
2854
3880
|
], i * 8);
|
|
2855
3881
|
this.instanceData.needsUpdate = true;
|
|
2856
|
-
this.
|
|
2857
|
-
this.woodMesh.boundingBox = this.leafMesh.boundingBox = null;
|
|
3882
|
+
for (const mesh of this.meshes) mesh.boundingSphere = mesh.boundingBox = null;
|
|
2858
3883
|
}
|
|
2859
3884
|
/** @internal */
|
|
2860
3885
|
state(tree) {
|
|
@@ -2871,11 +3896,13 @@ var ForestVariant = class {
|
|
|
2871
3896
|
this.disposeMeshes();
|
|
2872
3897
|
this.woodSource.dispose();
|
|
2873
3898
|
this.leafSource.dispose();
|
|
3899
|
+
this.fruitSource?.dispose();
|
|
2874
3900
|
this.barkMaterial.dispose();
|
|
2875
3901
|
this.leafMaterial.dispose();
|
|
3902
|
+
this.fruitMaterial?.dispose();
|
|
2876
3903
|
}
|
|
2877
3904
|
setCount() {
|
|
2878
|
-
|
|
3905
|
+
for (const mesh of this.meshes) mesh.count = this.trees.length;
|
|
2879
3906
|
}
|
|
2880
3907
|
/**
|
|
2881
3908
|
* (Re)builds the instanced meshes with room for `capacity` trees. An InstancedMesh cannot grow,
|
|
@@ -2905,11 +3932,12 @@ var ForestVariant = class {
|
|
|
2905
3932
|
};
|
|
2906
3933
|
this.woodMesh = mesh("forest wood", this.woodSource, this.barkMaterial);
|
|
2907
3934
|
this.leafMesh = mesh("forest leaves", this.leafSource, this.leafMaterial);
|
|
3935
|
+
if (this.fruitSource && this.fruitMaterial) this.fruitMesh = mesh("forest fruit", this.fruitSource, this.fruitMaterial);
|
|
2908
3936
|
this.setCount();
|
|
2909
|
-
this.forest.add(this.
|
|
3937
|
+
this.forest.add(...this.meshes);
|
|
2910
3938
|
}
|
|
2911
3939
|
disposeMeshes() {
|
|
2912
|
-
for (const mesh of
|
|
3940
|
+
for (const mesh of this.meshes) {
|
|
2913
3941
|
mesh.removeFromParent();
|
|
2914
3942
|
mesh.geometry.dispose();
|
|
2915
3943
|
mesh.dispose();
|
|
@@ -2934,6 +3962,7 @@ var Forest = class extends Group {
|
|
|
2934
3962
|
windBinding;
|
|
2935
3963
|
growthValue;
|
|
2936
3964
|
seasonValue;
|
|
3965
|
+
features;
|
|
2937
3966
|
constructor(definition, placements = [], options = {}) {
|
|
2938
3967
|
super();
|
|
2939
3968
|
const count = options.variants ?? 4;
|
|
@@ -2946,6 +3975,17 @@ var Forest = class extends Group {
|
|
|
2946
3975
|
this.seasonValue = checkedSeason(options.season ?? 0);
|
|
2947
3976
|
const capacity = Math.max(MIN_CAPACITY, 2 ** Math.ceil(Math.log2(Math.max(1, placements.length / count * 1.25))));
|
|
2948
3977
|
this.variants = Array.from({ length: count }, (_, i) => new ForestVariant(this, definition, i, variantSeed(this.seed, i), this.windBinding, capacity, options.textures));
|
|
3978
|
+
this.features = new LiveMaterialFeatures(definition, this.variants.map((v) => ({
|
|
3979
|
+
leaf: v.leafMaterial,
|
|
3980
|
+
materials: [
|
|
3981
|
+
v.barkMaterial,
|
|
3982
|
+
v.leafMaterial,
|
|
3983
|
+
v.fruitMaterial
|
|
3984
|
+
],
|
|
3985
|
+
crown: v.data.crown,
|
|
3986
|
+
growth: instanceState().growth,
|
|
3987
|
+
toObject: (normal) => instanceRotate(normal)
|
|
3988
|
+
})));
|
|
2949
3989
|
const textures = this.variants.flatMap((v) => [
|
|
2950
3990
|
v.barkMaterial.map,
|
|
2951
3991
|
v.barkMaterial.normalMap,
|
|
@@ -3008,7 +4048,7 @@ var Forest = class extends Group {
|
|
|
3008
4048
|
}
|
|
3009
4049
|
/** The tree drawn by instance `instanceId` of a Variant mesh, e.g. from a raycast intersection. */
|
|
3010
4050
|
treeAt(intersection) {
|
|
3011
|
-
const variant = this.variants.find((v) => v.
|
|
4051
|
+
const variant = this.variants.find((v) => v.meshes.includes(intersection.object));
|
|
3012
4052
|
return variant && intersection.instanceId !== void 0 ? variant.trees[intersection.instanceId] : void 0;
|
|
3013
4053
|
}
|
|
3014
4054
|
/** What the GPU draws a tree with: its Growth and Season after its offsets, and its Wind phase. */
|
|
@@ -3033,6 +4073,45 @@ var Forest = class extends Group {
|
|
|
3033
4073
|
this.seasonValue = checkedSeason(value);
|
|
3034
4074
|
for (const tree of this.byKey.values()) tree.season = this.seasonValue;
|
|
3035
4075
|
}
|
|
4076
|
+
/**
|
|
4077
|
+
* Crown Normals amount (0..1), from the Definition's `materials.leaf.crownNormals`; null when it is off.
|
|
4078
|
+
* Setting it (clamped to 0..1) only writes a uniform: no regeneration, no recompile. Throws on a
|
|
4079
|
+
* forest without Crown Normals (switching the feature on needs a new forest).
|
|
4080
|
+
*/
|
|
4081
|
+
get crownNormals() {
|
|
4082
|
+
return this.features.crownNormals;
|
|
4083
|
+
}
|
|
4084
|
+
set crownNormals(value) {
|
|
4085
|
+
this.features.crownNormals = value;
|
|
4086
|
+
}
|
|
4087
|
+
/**
|
|
4088
|
+
* Leaf Translucency settings (a copy), from the Definition's `materials.leaf.translucency`; null when it
|
|
4089
|
+
* is off. Setting any of them (each clamped to 0..1, the others kept) only writes uniforms: no
|
|
4090
|
+
* regeneration, no recompile. Throws on a forest without Leaf Translucency.
|
|
4091
|
+
*/
|
|
4092
|
+
get translucency() {
|
|
4093
|
+
return this.features.translucency;
|
|
4094
|
+
}
|
|
4095
|
+
set translucency(value) {
|
|
4096
|
+
this.features.translucency = value;
|
|
4097
|
+
}
|
|
4098
|
+
/**
|
|
4099
|
+
* The Material Features this forest shows, with their current (possibly live-tuned) values: the
|
|
4100
|
+
* Definition's (see `materialFeaturesOf`), except that Crown Normals at 0 count as off.
|
|
4101
|
+
*/
|
|
4102
|
+
get materialFeatures() {
|
|
4103
|
+
return this.features.values;
|
|
4104
|
+
}
|
|
4105
|
+
/**
|
|
4106
|
+
* Structural Occlusion strength (0..1) of every tree, from the Definition's `materials.occlusion`; null
|
|
4107
|
+
* when it has none. Setting it (clamped to 0..1) only writes a uniform. Throws without Structural Occlusion.
|
|
4108
|
+
*/
|
|
4109
|
+
get occlusionStrength() {
|
|
4110
|
+
return this.features.occlusionStrength;
|
|
4111
|
+
}
|
|
4112
|
+
set occlusionStrength(value) {
|
|
4113
|
+
this.features.occlusionStrength = value;
|
|
4114
|
+
}
|
|
3036
4115
|
/** Frees every Variant's geometries, materials and instance buffers, and removes the forest from its parent. */
|
|
3037
4116
|
dispose() {
|
|
3038
4117
|
for (const variant of this.variants) variant.dispose();
|
|
@@ -3052,6 +4131,6 @@ var Forest = class extends Group {
|
|
|
3052
4131
|
function createForest(definition, placements = [], options = {}) {
|
|
3053
4132
|
return new Forest(parseDefinition(definition), placements, options);
|
|
3054
4133
|
}
|
|
3055
|
-
export {
|
|
4134
|
+
export { treeScale as A, hexToLinear as B, loadTexture as C, groundTintAmount as D, textureReady as E, KEYFRAME_EPSILON as F, seasonShift as G, leafSeasonAttribute as H, evaluateSeason as I, woodSeasonAttribute as J, segmentWeight as K, fruitSeason as L, hashTreeData as M, crownDistance as N, clampGrowth as O, crownNormal as P, fruitSeasonAttribute as R, getTextureOptions as S, setTextureOptions as T, partSeasonAt as U, leafSeason as V, sampleTrack as W, createRandom as X, wrapSeason as Y, createBarkMaterial as _, forestTreeKey as a, BARK_REPEAT as b, Tree as c, sceneWind as d, applySeasonToLeafMaterial as f, assertWebGPURenderer as g, UnsupportedRendererError as h, ForestVariant as i, hashGeometry as j, growPositions as k, WindResponse as l, createSeasonNodes as m, Forest as n, loadStyle as o, createFruitSeasonNodes as p, srgbToLinear as q, ForestTree as r, createTree as s, createForest as t, Wind as u, createLeafMaterial as v, resolveTextureUrl as w, DEFAULT_TEXTURE_BASE_PATH as x, isFlatShading as y, fruitTracks as z };
|
|
3056
4135
|
|
|
3057
|
-
//# sourceMappingURL=forest-
|
|
4136
|
+
//# sourceMappingURL=forest-ChEaWJrf.js.map
|