@casmo/tree.js 0.1.1 → 0.2.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 +106 -16
- package/dist/apply-CcodnAbs.js +250 -0
- package/dist/apply-CcodnAbs.js.map +1 -0
- package/dist/bake/index.d.ts +2 -2
- 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 +7 -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 +95 -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 +10 -1
- package/dist/forest/Forest.d.ts.map +1 -1
- package/dist/forest/nodes.d.ts +4 -1
- package/dist/forest/nodes.d.ts.map +1 -1
- package/dist/{forest-OX9WvAwh.js → forest-DTrD8puH.js} +695 -546
- package/dist/forest-DTrD8puH.js.map +1 -0
- package/dist/generator/branches.d.ts +2 -2
- package/dist/generator/branches.d.ts.map +1 -1
- 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 +4 -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/trunk.d.ts +8 -2
- package/dist/generator/trunk.d.ts.map +1 -1
- package/dist/generator/types.d.ts +34 -5
- package/dist/generator/types.d.ts.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +48 -4
- package/dist/index.js.map +1 -1
- package/dist/materials/index.d.ts +6 -0
- package/dist/materials/index.d.ts.map +1 -1
- package/dist/names-C6-wuJ29.js +589 -0
- package/dist/names-C6-wuJ29.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 +213 -0
- package/dist/species/almond.js.map +1 -0
- package/dist/species/almond.json +201 -0
- package/dist/species/apple.js +220 -0
- package/dist/species/apple.js.map +1 -0
- package/dist/species/apple.json +211 -0
- package/dist/species/ash.js +177 -0
- package/dist/species/ash.js.map +1 -0
- package/dist/species/ash.json +168 -0
- package/dist/species/beech.js +164 -0
- package/dist/species/beech.js.map +1 -0
- package/dist/species/beech.json +158 -0
- package/dist/species/birch.js +160 -0
- package/dist/species/birch.js.map +1 -0
- package/dist/species/birch.json +154 -0
- package/dist/species/catalog.d.ts +61 -0
- package/dist/species/catalog.d.ts.map +1 -0
- package/dist/species/cedar.js +159 -0
- package/dist/species/cedar.js.map +1 -0
- package/dist/species/cedar.json +159 -0
- package/dist/species/cherry.js +227 -0
- package/dist/species/cherry.js.map +1 -0
- package/dist/species/cherry.json +215 -0
- package/dist/species/chestnut.js +220 -0
- package/dist/species/chestnut.js.map +1 -0
- package/dist/species/chestnut.json +211 -0
- package/dist/species/douglas-fir.js +164 -0
- package/dist/species/douglas-fir.js.map +1 -0
- package/dist/species/douglas-fir.json +161 -0
- package/dist/species/elm.js +163 -0
- package/dist/species/elm.js.map +1 -0
- package/dist/species/elm.json +154 -0
- package/dist/species/hemlock.js +165 -0
- package/dist/species/hemlock.js.map +1 -0
- package/dist/species/hemlock.json +162 -0
- package/dist/species/hickory.js +190 -0
- package/dist/species/hickory.js.map +1 -0
- package/dist/species/hickory.json +183 -0
- package/dist/species/index.d.ts +10 -0
- package/dist/species/index.d.ts.map +1 -0
- package/dist/species/juniper.js +148 -0
- package/dist/species/juniper.js.map +1 -0
- package/dist/species/juniper.json +145 -0
- package/dist/species/loaders.d.ts +7 -0
- package/dist/species/loaders.d.ts.map +1 -0
- package/dist/species/mahogany.js +191 -0
- package/dist/species/mahogany.js.map +1 -0
- package/dist/species/mahogany.json +187 -0
- package/dist/species/maple.js +167 -0
- package/dist/species/maple.js.map +1 -0
- package/dist/species/maple.json +162 -0
- package/dist/species/oak.js +210 -0
- package/dist/species/oak.js.map +1 -0
- package/dist/species/oak.json +203 -0
- package/dist/species/olive.js +179 -0
- package/dist/species/olive.js.map +1 -0
- package/dist/species/olive.json +172 -0
- package/dist/species/peach.js +213 -0
- package/dist/species/peach.js.map +1 -0
- package/dist/species/peach.json +201 -0
- package/dist/species/pear.js +203 -0
- package/dist/species/pear.js.map +1 -0
- package/dist/species/pear.json +191 -0
- package/dist/species/pine.js +153 -0
- package/dist/species/pine.js.map +1 -0
- package/dist/species/pine.json +153 -0
- package/dist/species/plum.js +227 -0
- package/dist/species/plum.js.map +1 -0
- package/dist/species/plum.json +215 -0
- package/dist/species/red-oak.js +210 -0
- package/dist/species/red-oak.js.map +1 -0
- package/dist/species/red-oak.json +203 -0
- package/dist/species/redwood.js +164 -0
- package/dist/species/redwood.js.map +1 -0
- package/dist/species/redwood.json +161 -0
- package/dist/species/sequoia.js +157 -0
- package/dist/species/sequoia.js.map +1 -0
- package/dist/species/sequoia.json +157 -0
- package/dist/species/spruce.js +160 -0
- package/dist/species/spruce.js.map +1 -0
- package/dist/species/spruce.json +157 -0
- package/dist/species/teak.js +156 -0
- package/dist/species/teak.js.map +1 -0
- package/dist/species/teak.json +148 -0
- package/dist/species/types.d.ts +61 -0
- package/dist/species/types.d.ts.map +1 -0
- package/dist/species/walnut.js +210 -0
- package/dist/species/walnut.js.map +1 -0
- package/dist/species/walnut.json +203 -0
- package/dist/species/white-oak.js +203 -0
- package/dist/species/white-oak.js.map +1 -0
- package/dist/species/white-oak.json +199 -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 +9 -8
- package/dist/styles/low-poly.js.map +1 -1
- 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 +10 -16
- 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 +9 -8
- 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 +6 -4
- 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
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
// src/definition/types.ts
|
|
2
|
+
/**
|
|
3
|
+
* Tree Definition schema (see GLOSSARY.md). Plain JSON: it is what the Tree Editor exports
|
|
4
|
+
* and what `createTree` consumes. Every field is documented with its unit and allowed range;
|
|
5
|
+
* the runtime rules live in `schema.ts` and the defaults in `defaults.ts`.
|
|
6
|
+
*
|
|
7
|
+
* Conventions: lengths in metres (scene units), angles in degrees, colours as `#rrggbb`,
|
|
8
|
+
* "fraction" fields are 0..1. Fields named `*Variation` are Seed ranges: the Seed picks a
|
|
9
|
+
* value within ± that fraction of the base value.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Current Tree Definition schema version. Bump it, and add a migration in `migrations.ts`,
|
|
13
|
+
* whenever the format changes or a generator change would alter existing trees (ADR 0003).
|
|
14
|
+
*/
|
|
15
|
+
var CURRENT_SCHEMA_VERSION = 3;
|
|
16
|
+
/** Maximum number of branching levels below the trunk. */
|
|
17
|
+
var MAX_BRANCH_LEVELS = 4;
|
|
18
|
+
/** All Leaf Types. */
|
|
19
|
+
var LEAF_TYPES = [
|
|
20
|
+
"broad",
|
|
21
|
+
"lobed",
|
|
22
|
+
"palmate",
|
|
23
|
+
"compound",
|
|
24
|
+
"needle",
|
|
25
|
+
"scale"
|
|
26
|
+
];
|
|
27
|
+
/** All Bark Types. */
|
|
28
|
+
var BARK_TYPES = [
|
|
29
|
+
"furrowed",
|
|
30
|
+
"smooth",
|
|
31
|
+
"papery",
|
|
32
|
+
"fibrous",
|
|
33
|
+
"plated"
|
|
34
|
+
];
|
|
35
|
+
/** Every Fruit Type. */
|
|
36
|
+
var FRUIT_TYPES = [
|
|
37
|
+
"round",
|
|
38
|
+
"pear",
|
|
39
|
+
"nut",
|
|
40
|
+
"cone",
|
|
41
|
+
"berry"
|
|
42
|
+
];
|
|
43
|
+
// src/definition/crown-shape.ts
|
|
44
|
+
/** The cone's shortest branches, as a fraction of the level's declared length. */
|
|
45
|
+
var MIN_CONE = .1;
|
|
46
|
+
/**
|
|
47
|
+
* The conifer cone as a Crown Shape length curve for a branch level starting at `start`: full length
|
|
48
|
+
* at `start`, shrinking linearly to a tenth of it near the trunk tip, never below a tenth (the v2
|
|
49
|
+
* generator's `max(0.1, (1 - t) / (1 - start))`). Written by the v2 -> v3 migration and used by every
|
|
50
|
+
* Style's own conifer.
|
|
51
|
+
*/
|
|
52
|
+
function coneCurve(start) {
|
|
53
|
+
return [
|
|
54
|
+
{
|
|
55
|
+
t: start,
|
|
56
|
+
value: 1
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
t: .9 + MIN_CONE * start,
|
|
60
|
+
value: MIN_CONE
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
t: 1,
|
|
64
|
+
value: MIN_CONE
|
|
65
|
+
}
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
// src/definition/defaults.ts
|
|
69
|
+
/** Defaults for one branching level; fills fields missing from a partial `branches[i]`. */
|
|
70
|
+
var DEFAULT_BRANCH_LEVEL = {
|
|
71
|
+
count: 6,
|
|
72
|
+
start: .35,
|
|
73
|
+
end: .95,
|
|
74
|
+
angle: 45,
|
|
75
|
+
angleVariation: .2,
|
|
76
|
+
spiral: 137.5,
|
|
77
|
+
length: .5,
|
|
78
|
+
lengthVariation: .2,
|
|
79
|
+
radius: .6,
|
|
80
|
+
taper: .3,
|
|
81
|
+
sides: 6,
|
|
82
|
+
sections: 5,
|
|
83
|
+
bend: .1,
|
|
84
|
+
gravity: .1
|
|
85
|
+
};
|
|
86
|
+
/** Defaults for the fields of a `fruit` block (a Definition has no Fruit unless it has the block). */
|
|
87
|
+
var DEFAULT_FRUIT = {
|
|
88
|
+
type: "round",
|
|
89
|
+
count: 2,
|
|
90
|
+
size: .12,
|
|
91
|
+
sizeVariation: .2,
|
|
92
|
+
hang: .7
|
|
93
|
+
};
|
|
94
|
+
/** Fruit amount when the Season Profile has no `fruit` track: all Fruit at full size all year. */
|
|
95
|
+
var DEFAULT_FRUIT_AMOUNT = [{
|
|
96
|
+
t: 0,
|
|
97
|
+
value: 1
|
|
98
|
+
}];
|
|
99
|
+
/** Fruit colour when the Season Profile has no `fruitColor` track. */
|
|
100
|
+
var DEFAULT_FRUIT_COLOR = [{
|
|
101
|
+
t: 0,
|
|
102
|
+
value: "#b8322a"
|
|
103
|
+
}];
|
|
104
|
+
/**
|
|
105
|
+
* The documented defaults: a small broadleaf tree with two branching levels.
|
|
106
|
+
* Anything a Definition leaves out is taken from here.
|
|
107
|
+
*/
|
|
108
|
+
var DEFAULT_DEFINITION = {
|
|
109
|
+
schemaVersion: 3,
|
|
110
|
+
name: "",
|
|
111
|
+
trunk: {
|
|
112
|
+
length: 4,
|
|
113
|
+
radius: .25,
|
|
114
|
+
taper: .4,
|
|
115
|
+
sides: 8,
|
|
116
|
+
sections: 8,
|
|
117
|
+
lengthVariation: .15,
|
|
118
|
+
bend: .08
|
|
119
|
+
},
|
|
120
|
+
branches: [{ ...DEFAULT_BRANCH_LEVEL }, {
|
|
121
|
+
...DEFAULT_BRANCH_LEVEL,
|
|
122
|
+
count: 4,
|
|
123
|
+
start: .2,
|
|
124
|
+
angle: 40,
|
|
125
|
+
length: .45,
|
|
126
|
+
sides: 4,
|
|
127
|
+
sections: 3
|
|
128
|
+
}],
|
|
129
|
+
leaves: {
|
|
130
|
+
count: 8,
|
|
131
|
+
shape: "card",
|
|
132
|
+
size: .25,
|
|
133
|
+
sizeVariation: .2,
|
|
134
|
+
start: .3,
|
|
135
|
+
angle: 40
|
|
136
|
+
},
|
|
137
|
+
materials: {
|
|
138
|
+
shading: "standard",
|
|
139
|
+
bark: {
|
|
140
|
+
color: "#6b4a2b",
|
|
141
|
+
roughness: .9,
|
|
142
|
+
map: null,
|
|
143
|
+
normalMap: null
|
|
144
|
+
},
|
|
145
|
+
leaf: {
|
|
146
|
+
roughness: .7,
|
|
147
|
+
map: null,
|
|
148
|
+
alphaTest: .5
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
season: {
|
|
152
|
+
leafColor: [
|
|
153
|
+
{
|
|
154
|
+
t: 0,
|
|
155
|
+
value: "#8bc34a"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
t: .25,
|
|
159
|
+
value: "#3e7d2b"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
t: .5,
|
|
163
|
+
value: "#3e7d2b"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
t: .62,
|
|
167
|
+
value: "#d8892b"
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
t: .72,
|
|
171
|
+
value: "#a0401f"
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
leafCoverage: [
|
|
175
|
+
{
|
|
176
|
+
t: 0,
|
|
177
|
+
value: .3
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
t: .1,
|
|
181
|
+
value: 1
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
t: .62,
|
|
185
|
+
value: 1
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
t: .75,
|
|
189
|
+
value: 0
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
t: .97,
|
|
193
|
+
value: 0
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
t: 1,
|
|
197
|
+
value: .3
|
|
198
|
+
}
|
|
199
|
+
],
|
|
200
|
+
blossom: [{
|
|
201
|
+
t: 0,
|
|
202
|
+
value: 0
|
|
203
|
+
}],
|
|
204
|
+
blossomColor: "#f7c6d9"
|
|
205
|
+
},
|
|
206
|
+
wind: {
|
|
207
|
+
trunk: .2,
|
|
208
|
+
branches: .5,
|
|
209
|
+
leaves: .8,
|
|
210
|
+
frequency: 1
|
|
211
|
+
},
|
|
212
|
+
variation: {
|
|
213
|
+
color: .1,
|
|
214
|
+
growth: .1,
|
|
215
|
+
season: .05
|
|
216
|
+
},
|
|
217
|
+
sculpts: []
|
|
218
|
+
};
|
|
219
|
+
// src/definition/migrations.ts
|
|
220
|
+
/**
|
|
221
|
+
* Schema migrations. `MIGRATIONS[n]` turns a raw (unvalidated) schemaVersion `n` object into
|
|
222
|
+
* a raw schemaVersion `n + 1` object. Migrations run in order before defaults and validation,
|
|
223
|
+
* so they only need to move/rename what changed; missing fields are defaulted afterwards.
|
|
224
|
+
*
|
|
225
|
+
* To change the format: bump CURRENT_SCHEMA_VERSION, add `MIGRATIONS[old]`, and add a
|
|
226
|
+
* fixture in test/fixtures/definition-v<old>.json with a test.
|
|
227
|
+
*/
|
|
228
|
+
var MIGRATIONS = {
|
|
229
|
+
/**
|
|
230
|
+
* v1 -> v2: `bark` moved into `materials.bark`; branches, leaves, Season Profile, Wind,
|
|
231
|
+
* variation and sculpts were added. A v1 tree was a bare trunk, so it keeps no branches
|
|
232
|
+
* and no leaves (same tree as before).
|
|
233
|
+
*/
|
|
234
|
+
1: (input) => {
|
|
235
|
+
const { bark, ...rest } = input;
|
|
236
|
+
return {
|
|
237
|
+
...rest,
|
|
238
|
+
schemaVersion: 2,
|
|
239
|
+
branches: [],
|
|
240
|
+
leaves: { count: 0 },
|
|
241
|
+
...bark === void 0 ? {} : { materials: { bark } }
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
/**
|
|
245
|
+
* v2 -> v3: `form` is removed (ADR 0004). A conifer's cone becomes a Crown Shape length curve on
|
|
246
|
+
* the first branch level (identical tree); a broadleaf just drops `form`. Unknown `form` values are
|
|
247
|
+
* kept so validation reports them.
|
|
248
|
+
*/
|
|
249
|
+
2: (input) => {
|
|
250
|
+
const { form, ...rest } = input;
|
|
251
|
+
const out = {
|
|
252
|
+
...rest,
|
|
253
|
+
schemaVersion: 3
|
|
254
|
+
};
|
|
255
|
+
if (form !== void 0 && form !== "broadleaf" && form !== "conifer") out.form = form;
|
|
256
|
+
if (form !== "conifer") return out;
|
|
257
|
+
const branches = Array.isArray(rest.branches) ? rest.branches : DEFAULT_DEFINITION.branches.map((b) => ({ ...b }));
|
|
258
|
+
if (branches.length === 0) return out;
|
|
259
|
+
const [first, ...deeper] = branches;
|
|
260
|
+
if (typeof first !== "object" || first === null || Array.isArray(first)) return out;
|
|
261
|
+
const level = first;
|
|
262
|
+
const start = typeof level.start === "number" ? level.start : DEFAULT_BRANCH_LEVEL.start;
|
|
263
|
+
return {
|
|
264
|
+
...out,
|
|
265
|
+
branches: [{
|
|
266
|
+
...level,
|
|
267
|
+
lengthProfile: coneCurve(start)
|
|
268
|
+
}, ...deeper]
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
// src/definition/schema.ts
|
|
273
|
+
/** Thrown for an invalid Tree Definition; `field` is the path of the bad field, e.g. `branches[1].angle`. */
|
|
274
|
+
var TreeDefinitionError = class extends Error {
|
|
275
|
+
field;
|
|
276
|
+
constructor(field, message) {
|
|
277
|
+
super(`Invalid Tree Definition: ${field} ${message}`);
|
|
278
|
+
this.name = "TreeDefinitionError";
|
|
279
|
+
this.field = field;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
var ROOT = "(root)";
|
|
283
|
+
var join = (path, key) => path === ROOT ? key : `${path}.${key}`;
|
|
284
|
+
var show = (v) => v === void 0 ? "undefined" : JSON.stringify(v);
|
|
285
|
+
function isObject(v) {
|
|
286
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
287
|
+
}
|
|
288
|
+
/** Leaf schema from a predicate; `undefined` takes the fallback, or is reported missing. */
|
|
289
|
+
function leaf(expected, ok) {
|
|
290
|
+
return (value, fallback, path) => {
|
|
291
|
+
const v = value === void 0 ? fallback : value;
|
|
292
|
+
if (v === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
293
|
+
if (!ok(v)) throw new TreeDefinitionError(path, `must be ${expected}, got ${show(v)}`);
|
|
294
|
+
return Array.isArray(v) ? [...v] : v;
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
var isNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
298
|
+
var range = (min, max) => leaf(`a number from ${min} to ${max}`, (v) => isNum(v) && v >= min && v <= max);
|
|
299
|
+
var unit = range(0, 1);
|
|
300
|
+
var nonNegative = leaf("a number >= 0", (v) => isNum(v) && v >= 0);
|
|
301
|
+
var positive = leaf("a number > 0", (v) => isNum(v) && v > 0);
|
|
302
|
+
var finite = leaf("a finite number", isNum);
|
|
303
|
+
var intAtLeast = (min) => leaf(`an integer >= ${min}`, (v) => Number.isInteger(v) && v >= min);
|
|
304
|
+
/** Faces around a Fruit (its polygon detail). */
|
|
305
|
+
var fruitSides = leaf("an integer from 3 to 16", (v) => Number.isInteger(v) && v >= 3 && v <= 16);
|
|
306
|
+
var hexColor = leaf("a colour like \"#6b4a2b\"", (v) => typeof v === "string" && /^#[0-9a-f]{6}$/i.test(v));
|
|
307
|
+
var text = leaf("a string", (v) => typeof v === "string");
|
|
308
|
+
var url = leaf("a URL string or null", (v) => v === null || typeof v === "string" && v.length > 0);
|
|
309
|
+
var vec3 = leaf("[x, y, z] numbers", (v) => Array.isArray(v) && v.length === 3 && v.every(isNum));
|
|
310
|
+
var oneOf = (...values) => leaf(`one of ${values.map((x) => `"${x}"`).join(", ")}`, (v) => values.includes(v));
|
|
311
|
+
var literal = (expected) => leaf(String(expected), (v) => v === expected);
|
|
312
|
+
/** An optional field: absent (undefined) stays absent, anything else must match `schema`. */
|
|
313
|
+
function optional(schema) {
|
|
314
|
+
return (value, fallback, path) => value === void 0 && fallback === void 0 ? void 0 : schema(value, fallback, path);
|
|
315
|
+
}
|
|
316
|
+
/** An optional block: absent stays absent; a given (possibly partial) block is filled from `defaults`. */
|
|
317
|
+
function optionalBlock(schema, defaults) {
|
|
318
|
+
return (value, fallback, path) => value === void 0 && fallback === void 0 ? void 0 : schema(value, fallback ?? defaults, path);
|
|
319
|
+
}
|
|
320
|
+
/** Object with exactly these fields; unknown fields are rejected (catches typos). */
|
|
321
|
+
function obj(fields) {
|
|
322
|
+
return (value, fallback, path) => {
|
|
323
|
+
if (value === void 0 && fallback === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
324
|
+
const given = value === void 0 ? {} : value;
|
|
325
|
+
if (!isObject(given)) throw new TreeDefinitionError(path, `must be an object, got ${show(given)}`);
|
|
326
|
+
for (const key of Object.keys(given)) if (!(key in fields)) throw new TreeDefinitionError(join(path, key), "is not a known field");
|
|
327
|
+
const out = {};
|
|
328
|
+
for (const key of Object.keys(fields)) {
|
|
329
|
+
const field = fields[key](given[key], fallback?.[key], join(path, key));
|
|
330
|
+
if (field !== void 0) out[key] = field;
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/** Array; a missing array takes the fallback array, each item is filled from `itemFallback`. */
|
|
336
|
+
function arr(item, opts = {}) {
|
|
337
|
+
const { min = 0, max = Infinity, itemFallback } = opts;
|
|
338
|
+
return (value, fallback, path) => {
|
|
339
|
+
const v = value === void 0 ? fallback : value;
|
|
340
|
+
if (v === void 0) throw new TreeDefinitionError(path, "is missing");
|
|
341
|
+
if (!Array.isArray(v)) throw new TreeDefinitionError(path, `must be an array, got ${show(v)}`);
|
|
342
|
+
if (v.length < min) throw new TreeDefinitionError(path, `must have at least ${min} entries`);
|
|
343
|
+
if (v.length > max) throw new TreeDefinitionError(path, `must have at most ${max} entries`);
|
|
344
|
+
return v.map((x, i) => item(x, itemFallback, `${path}[${i}]`));
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Adds a cross-field check; `check` returns [field, problem] or null. */
|
|
348
|
+
function refine(schema, check) {
|
|
349
|
+
return (value, fallback, path) => {
|
|
350
|
+
const v = schema(value, fallback, path);
|
|
351
|
+
const problem = check(v);
|
|
352
|
+
if (problem) throw new TreeDefinitionError(join(path, problem[0]), problem[1]);
|
|
353
|
+
return v;
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/** Union discriminated by `key`. */
|
|
357
|
+
function tagged(key, variants) {
|
|
358
|
+
const names = Object.keys(variants);
|
|
359
|
+
return (value, fallback, path) => {
|
|
360
|
+
if (!isObject(value)) throw new TreeDefinitionError(path, `must be an object, got ${show(value)}`);
|
|
361
|
+
const variant = variants[value[key]];
|
|
362
|
+
if (!variant) throw new TreeDefinitionError(join(path, key), `must be one of ${names.map((n) => `"${n}"`).join(", ")}, got ${show(value[key])}`);
|
|
363
|
+
return variant(value, fallback, path);
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/** Keyframe track: at least one key, `t` in 0..1 and non-decreasing. */
|
|
367
|
+
function track(value) {
|
|
368
|
+
const keys = arr(obj({
|
|
369
|
+
t: unit,
|
|
370
|
+
value
|
|
371
|
+
}), { min: 1 });
|
|
372
|
+
return (v, fallback, path) => {
|
|
373
|
+
const out = keys(v, fallback, path);
|
|
374
|
+
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}`);
|
|
375
|
+
return out;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
var branchLevel = refine(obj({
|
|
379
|
+
count: intAtLeast(0),
|
|
380
|
+
start: unit,
|
|
381
|
+
end: unit,
|
|
382
|
+
angle: range(0, 180),
|
|
383
|
+
angleVariation: unit,
|
|
384
|
+
spiral: range(0, 360),
|
|
385
|
+
length: positive,
|
|
386
|
+
lengthVariation: unit,
|
|
387
|
+
radius: unit,
|
|
388
|
+
taper: unit,
|
|
389
|
+
sides: intAtLeast(3),
|
|
390
|
+
sections: intAtLeast(1),
|
|
391
|
+
bend: unit,
|
|
392
|
+
gravity: range(-1, 1),
|
|
393
|
+
lengthProfile: optional(track(nonNegative)),
|
|
394
|
+
angleProfile: optional(track(nonNegative))
|
|
395
|
+
}), (b) => b.end < b.start ? ["end", `must be >= start (${b.start}), got ${b.end}`] : null);
|
|
396
|
+
var branchPath = leaf("a branch path like \"0\", \"1\" or \"0.3.1\"", (v) => typeof v === "string" && /^\d+(\.\d+)*$/.test(v));
|
|
397
|
+
var sculpt = tagged("action", {
|
|
398
|
+
delete: obj({
|
|
399
|
+
action: literal("delete"),
|
|
400
|
+
seed: finite,
|
|
401
|
+
path: branchPath
|
|
402
|
+
}),
|
|
403
|
+
move: obj({
|
|
404
|
+
action: literal("move"),
|
|
405
|
+
seed: finite,
|
|
406
|
+
path: branchPath,
|
|
407
|
+
rotation: vec3,
|
|
408
|
+
scale: positive
|
|
409
|
+
}),
|
|
410
|
+
leaves: obj({
|
|
411
|
+
action: literal("leaves"),
|
|
412
|
+
seed: finite,
|
|
413
|
+
path: branchPath,
|
|
414
|
+
coverage: unit
|
|
415
|
+
})
|
|
416
|
+
});
|
|
417
|
+
/** The whole Tree Definition schema (current version). Mirrors the interfaces in types.ts. */
|
|
418
|
+
var treeDefinition = obj({
|
|
419
|
+
schemaVersion: literal(3),
|
|
420
|
+
name: text,
|
|
421
|
+
species: optional(text),
|
|
422
|
+
trunk: obj({
|
|
423
|
+
length: positive,
|
|
424
|
+
radius: positive,
|
|
425
|
+
taper: unit,
|
|
426
|
+
sides: intAtLeast(3),
|
|
427
|
+
sections: intAtLeast(1),
|
|
428
|
+
lengthVariation: unit,
|
|
429
|
+
bend: unit,
|
|
430
|
+
stems: optional(intAtLeast(1)),
|
|
431
|
+
spread: optional(range(0, 90)),
|
|
432
|
+
gravity: optional(range(-1, 1))
|
|
433
|
+
}),
|
|
434
|
+
branches: arr(branchLevel, {
|
|
435
|
+
max: 4,
|
|
436
|
+
itemFallback: DEFAULT_BRANCH_LEVEL
|
|
437
|
+
}),
|
|
438
|
+
leaves: obj({
|
|
439
|
+
type: optional(oneOf(...LEAF_TYPES)),
|
|
440
|
+
count: intAtLeast(0),
|
|
441
|
+
shape: oneOf("card", "clump", "needle", "puff"),
|
|
442
|
+
size: positive,
|
|
443
|
+
sizeVariation: unit,
|
|
444
|
+
start: unit,
|
|
445
|
+
angle: range(0, 180)
|
|
446
|
+
}),
|
|
447
|
+
fruit: optionalBlock(obj({
|
|
448
|
+
type: oneOf(...FRUIT_TYPES),
|
|
449
|
+
count: nonNegative,
|
|
450
|
+
size: positive,
|
|
451
|
+
sizeVariation: unit,
|
|
452
|
+
hang: unit,
|
|
453
|
+
sides: optional(fruitSides)
|
|
454
|
+
}), DEFAULT_FRUIT),
|
|
455
|
+
materials: obj({
|
|
456
|
+
shading: oneOf("standard", "flat", "toon"),
|
|
457
|
+
bark: obj({
|
|
458
|
+
type: optional(oneOf(...BARK_TYPES)),
|
|
459
|
+
color: hexColor,
|
|
460
|
+
roughness: unit,
|
|
461
|
+
map: url,
|
|
462
|
+
normalMap: url
|
|
463
|
+
}),
|
|
464
|
+
leaf: obj({
|
|
465
|
+
roughness: unit,
|
|
466
|
+
map: url,
|
|
467
|
+
alphaTest: unit
|
|
468
|
+
})
|
|
469
|
+
}),
|
|
470
|
+
season: obj({
|
|
471
|
+
leafColor: track(hexColor),
|
|
472
|
+
leafCoverage: track(unit),
|
|
473
|
+
blossom: track(unit),
|
|
474
|
+
blossomColor: hexColor,
|
|
475
|
+
fruit: optional(track(unit)),
|
|
476
|
+
fruitColor: optional(track(hexColor))
|
|
477
|
+
}),
|
|
478
|
+
wind: obj({
|
|
479
|
+
trunk: unit,
|
|
480
|
+
branches: unit,
|
|
481
|
+
leaves: unit,
|
|
482
|
+
frequency: positive
|
|
483
|
+
}),
|
|
484
|
+
variation: obj({
|
|
485
|
+
color: unit,
|
|
486
|
+
growth: unit,
|
|
487
|
+
season: unit
|
|
488
|
+
}),
|
|
489
|
+
sculpts: arr(sculpt)
|
|
490
|
+
});
|
|
491
|
+
/**
|
|
492
|
+
* Validates a raw current-version Definition and fills every missing field from
|
|
493
|
+
* {@link DEFAULT_DEFINITION}. Returns a fresh object (never aliases input or defaults).
|
|
494
|
+
*/
|
|
495
|
+
function validateDefinition(raw) {
|
|
496
|
+
return treeDefinition(raw, DEFAULT_DEFINITION, ROOT);
|
|
497
|
+
}
|
|
498
|
+
// src/definition/parse.ts
|
|
499
|
+
/**
|
|
500
|
+
* Turns untrusted input into a complete, valid Tree Definition:
|
|
501
|
+
* - `input` is a Definition object (e.g. parsed JSON) or a JSON string;
|
|
502
|
+
* - older `schemaVersion`s are migrated to {@link CURRENT_SCHEMA_VERSION};
|
|
503
|
+
* - missing fields are filled from {@link DEFAULT_DEFINITION} (missing fields of a
|
|
504
|
+
* `branches[i]` entry from {@link DEFAULT_BRANCH_LEVEL});
|
|
505
|
+
* - every field is validated; unknown fields are rejected.
|
|
506
|
+
* Throws {@link TreeDefinitionError} naming the first bad field (`field`, e.g. `branches[1].angle`).
|
|
507
|
+
* Never mutates `input`.
|
|
508
|
+
*/
|
|
509
|
+
function parseDefinition(input) {
|
|
510
|
+
let raw = input;
|
|
511
|
+
if (typeof raw === "string") try {
|
|
512
|
+
raw = JSON.parse(raw);
|
|
513
|
+
} catch (e) {
|
|
514
|
+
throw new TreeDefinitionError("(root)", `is not valid JSON: ${e.message}`);
|
|
515
|
+
}
|
|
516
|
+
if (!isObject(raw)) throw new TreeDefinitionError("(root)", "must be an object");
|
|
517
|
+
const version = raw.schemaVersion;
|
|
518
|
+
if (version === void 0) throw new TreeDefinitionError("schemaVersion", "is missing");
|
|
519
|
+
if (!Number.isInteger(version) || version < 1) throw new TreeDefinitionError("schemaVersion", `must be an integer >= 1, got ${JSON.stringify(version)}`);
|
|
520
|
+
if (version > 3) throw new TreeDefinitionError("schemaVersion", `is ${version}, newer than this version of @casmo/tree.js supports (3); upgrade the library`);
|
|
521
|
+
for (let v = version; v < 3; v++) raw = MIGRATIONS[v](raw);
|
|
522
|
+
return validateDefinition(raw);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Starts from a Definition (e.g. a built-in Style) and changes a few parameters:
|
|
526
|
+
* objects are merged deeply, arrays and values replace. Returns a new, validated Definition.
|
|
527
|
+
*
|
|
528
|
+
* ```ts
|
|
529
|
+
* const tall = extendDefinition(broadleaf, { trunk: { length: 7 }, wind: { leaves: 1 } });
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
function extendDefinition(base, patch) {
|
|
533
|
+
const parsed = parseDefinition(base);
|
|
534
|
+
if (!isObject(patch)) throw new TreeDefinitionError("(root)", "patch must be an object");
|
|
535
|
+
return parseDefinition({
|
|
536
|
+
...merge(parsed, patch),
|
|
537
|
+
schemaVersion: 3
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
function merge(base, patch) {
|
|
541
|
+
const out = { ...base };
|
|
542
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
543
|
+
if (value === void 0) continue;
|
|
544
|
+
out[key] = isObject(value) && isObject(base[key]) ? merge(base[key], value) : value;
|
|
545
|
+
}
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Fetches a Tree Definition JSON file (e.g. a Tree Editor export) and parses it with
|
|
550
|
+
* {@link parseDefinition}. Rejects with an Error for an HTTP error, or a TreeDefinitionError.
|
|
551
|
+
*/
|
|
552
|
+
async function fetchDefinition(url, init) {
|
|
553
|
+
const response = await fetch(url, init);
|
|
554
|
+
if (!response.ok) throw new Error(`Could not load Tree Definition from ${String(url)}: HTTP ${response.status}`);
|
|
555
|
+
return parseDefinition(await response.text());
|
|
556
|
+
}
|
|
557
|
+
// src/styles/names.ts
|
|
558
|
+
/** Every built-in Style: its display label and the shading its Definitions use (one shading per Style). */
|
|
559
|
+
var STYLES = {
|
|
560
|
+
"low-poly": {
|
|
561
|
+
label: "Low-poly",
|
|
562
|
+
shading: "flat"
|
|
563
|
+
},
|
|
564
|
+
stylized: {
|
|
565
|
+
label: "Stylized",
|
|
566
|
+
shading: "toon"
|
|
567
|
+
},
|
|
568
|
+
realistic: {
|
|
569
|
+
label: "Realistic",
|
|
570
|
+
shading: "standard"
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
/** Names of all built-in Styles. */
|
|
574
|
+
var STYLE_NAMES = Object.keys(STYLES);
|
|
575
|
+
/** Display label of a built-in Style, e.g. 'Low-poly'. */
|
|
576
|
+
function styleLabel(name) {
|
|
577
|
+
return STYLES[name].label;
|
|
578
|
+
}
|
|
579
|
+
/** The shading a built-in Style draws with. */
|
|
580
|
+
function styleShading(name) {
|
|
581
|
+
return STYLES[name].shading;
|
|
582
|
+
}
|
|
583
|
+
/** The built-in Style a Definition was drawn in, recognised by its `materials.shading`. */
|
|
584
|
+
function styleOfShading(shading) {
|
|
585
|
+
return STYLE_NAMES.find((name) => STYLES[name].shading === shading);
|
|
586
|
+
}
|
|
587
|
+
export { FRUIT_TYPES as _, extendDefinition as a, TreeDefinitionError as c, DEFAULT_FRUIT as d, DEFAULT_FRUIT_AMOUNT as f, CURRENT_SCHEMA_VERSION as g, BARK_TYPES as h, styleShading as i, DEFAULT_BRANCH_LEVEL as l, coneCurve as m, styleLabel as n, fetchDefinition as o, DEFAULT_FRUIT_COLOR as p, styleOfShading as r, parseDefinition as s, STYLE_NAMES as t, DEFAULT_DEFINITION as u, LEAF_TYPES as v, MAX_BRANCH_LEVELS as y };
|
|
588
|
+
|
|
589
|
+
//# sourceMappingURL=names-C6-wuJ29.js.map
|