@casmo/tree.js 0.2.0 → 0.4.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 +392 -1
- package/dist/{apply-CcodnAbs.js → apply-Cio2t7CZ.js} +51 -16
- package/dist/apply-Cio2t7CZ.js.map +1 -0
- package/dist/bake/index.d.ts +43 -4
- package/dist/bake/index.d.ts.map +1 -1
- package/dist/definition/defaults.d.ts +9 -1
- package/dist/definition/defaults.d.ts.map +1 -1
- package/dist/definition/index.d.ts +2 -2
- package/dist/definition/index.d.ts.map +1 -1
- package/dist/definition/schema.d.ts.map +1 -1
- package/dist/definition/types.d.ts +86 -0
- package/dist/definition/types.d.ts.map +1 -1
- package/dist/effects/effects.d.ts +51 -6
- package/dist/effects/effects.d.ts.map +1 -1
- package/dist/effects/glow.d.ts +5 -0
- package/dist/effects/glow.d.ts.map +1 -1
- package/dist/effects/grade.d.ts +3 -0
- package/dist/effects/grade.d.ts.map +1 -1
- package/dist/effects/highlight.d.ts +35 -0
- package/dist/effects/highlight.d.ts.map +1 -0
- package/dist/effects/index.d.ts +5 -1
- package/dist/effects/index.d.ts.map +1 -1
- package/dist/effects/inner-lines.d.ts +39 -0
- package/dist/effects/inner-lines.d.ts.map +1 -0
- package/dist/effects/live.d.ts +12 -0
- package/dist/effects/live.d.ts.map +1 -0
- package/dist/effects/mask.d.ts +23 -11
- package/dist/effects/mask.d.ts.map +1 -1
- package/dist/effects/outline.d.ts +12 -0
- package/dist/effects/outline.d.ts.map +1 -1
- package/dist/effects/pass.d.ts +27 -0
- package/dist/effects/pass.d.ts.map +1 -0
- package/dist/effects/screen.d.ts +31 -0
- package/dist/effects/screen.d.ts.map +1 -0
- package/dist/effects/suggested.d.ts +5 -0
- package/dist/effects/suggested.d.ts.map +1 -1
- package/dist/effects/tree-id.d.ts +50 -0
- package/dist/effects/tree-id.d.ts.map +1 -0
- package/dist/effects.js +772 -57
- package/dist/effects.js.map +1 -1
- package/dist/forest/Forest.d.ts +29 -1
- package/dist/forest/Forest.d.ts.map +1 -1
- package/dist/forest/nodes.d.ts +2 -0
- package/dist/forest/nodes.d.ts.map +1 -1
- package/dist/{forest-DTrD8puH.js → forest-lg61rhKM.js} +1125 -191
- package/dist/forest-lg61rhKM.js.map +1 -0
- package/dist/generator/crown.d.ts +28 -0
- package/dist/generator/crown.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 +2 -1
- package/dist/generator/index.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 +8 -0
- package/dist/generator/trunk.d.ts.map +1 -1
- package/dist/generator/types.d.ts +27 -1
- 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 +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +234 -28
- 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 +7 -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-C6-wuJ29.js → names-BE9he6CS.js} +133 -9
- package/dist/names-BE9he6CS.js.map +1 -0
- package/dist/react.js +1 -1
- package/dist/species/almond.js +12 -2
- package/dist/species/almond.json +10 -1
- package/dist/species/apple.js +12 -2
- package/dist/species/apple.json +10 -1
- package/dist/species/ash.js +12 -2
- package/dist/species/ash.json +10 -1
- package/dist/species/beech.js +12 -2
- package/dist/species/beech.json +10 -1
- package/dist/species/birch.js +12 -2
- package/dist/species/birch.json +10 -1
- package/dist/species/cedar.js +12 -2
- package/dist/species/cedar.json +10 -1
- package/dist/species/cherry.js +12 -2
- package/dist/species/cherry.json +10 -1
- package/dist/species/chestnut.js +12 -2
- package/dist/species/chestnut.json +10 -1
- package/dist/species/douglas-fir.js +12 -2
- package/dist/species/douglas-fir.json +10 -1
- package/dist/species/elm.js +12 -2
- package/dist/species/elm.json +10 -1
- package/dist/species/hemlock.js +12 -2
- package/dist/species/hemlock.json +10 -1
- package/dist/species/hickory.js +12 -2
- package/dist/species/hickory.json +10 -1
- package/dist/species/juniper.js +12 -2
- package/dist/species/juniper.json +10 -1
- package/dist/species/mahogany.js +12 -2
- package/dist/species/mahogany.json +10 -1
- package/dist/species/maple.js +12 -2
- package/dist/species/maple.json +10 -1
- package/dist/species/oak.js +12 -2
- package/dist/species/oak.json +10 -1
- package/dist/species/olive.js +12 -2
- package/dist/species/olive.json +10 -1
- package/dist/species/peach.js +12 -2
- package/dist/species/peach.json +10 -1
- package/dist/species/pear.js +12 -2
- package/dist/species/pear.json +10 -1
- package/dist/species/pine.js +12 -2
- package/dist/species/pine.json +10 -1
- package/dist/species/plum.js +12 -2
- package/dist/species/plum.json +10 -1
- package/dist/species/red-oak.js +12 -2
- package/dist/species/red-oak.json +10 -1
- package/dist/species/redwood.js +12 -2
- package/dist/species/redwood.json +10 -1
- package/dist/species/sequoia.js +12 -2
- package/dist/species/sequoia.json +10 -1
- package/dist/species/spruce.js +12 -2
- package/dist/species/spruce.json +10 -1
- package/dist/species/teak.js +12 -2
- package/dist/species/teak.json +10 -1
- package/dist/species/types.d.ts +7 -3
- package/dist/species/types.d.ts.map +1 -1
- package/dist/species/walnut.js +12 -2
- package/dist/species/walnut.json +10 -1
- package/dist/species/white-oak.js +12 -2
- package/dist/species/white-oak.json +10 -1
- package/dist/styles/apply.d.ts.map +1 -1
- package/dist/styles/low-poly.js +6 -4
- 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/realistic.js +6 -4
- package/dist/styles/realistic.js.map +1 -1
- package/dist/styles/stylized.js +6 -4
- package/dist/styles/stylized.js.map +1 -1
- package/dist/tree/Tree.d.ts +28 -1
- package/dist/tree/Tree.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/apply-CcodnAbs.js.map +0 -1
- package/dist/forest-DTrD8puH.js.map +0 -1
- package/dist/names-C6-wuJ29.js.map +0 -1
|
@@ -1,6 +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";
|
|
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";
|
|
4
4
|
// src/random/index.ts
|
|
5
5
|
/**
|
|
6
6
|
* Random: the only source of randomness in tree.js.
|
|
@@ -382,9 +382,9 @@ var cross$1 = (a, b) => [
|
|
|
382
382
|
a[2] * b[0] - a[0] * b[2],
|
|
383
383
|
a[0] * b[1] - a[1] * b[0]
|
|
384
384
|
];
|
|
385
|
-
var length = (a) => hypot3(a[0], a[1], a[2]);
|
|
386
|
-
function normalize(a) {
|
|
387
|
-
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);
|
|
388
388
|
return l > 1e-12 ? scale(a, 1 / l) : [
|
|
389
389
|
0,
|
|
390
390
|
1,
|
|
@@ -398,7 +398,7 @@ var lerp = (a, b, t) => [
|
|
|
398
398
|
];
|
|
399
399
|
/** Any unit vector perpendicular to the unit vector `v`. */
|
|
400
400
|
function perpendicular(v) {
|
|
401
|
-
return normalize(cross$1(Math.abs(v[1]) < .9 ? [
|
|
401
|
+
return normalize$1(cross$1(Math.abs(v[1]) < .9 ? [
|
|
402
402
|
0,
|
|
403
403
|
1,
|
|
404
404
|
0
|
|
@@ -416,19 +416,19 @@ function perpendicular(v) {
|
|
|
416
416
|
function branchFrames(branch) {
|
|
417
417
|
const pts = branch.points.map((p) => p.position);
|
|
418
418
|
const n = pts.length;
|
|
419
|
-
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)]));
|
|
420
420
|
const frames = [];
|
|
421
421
|
for (let i = 0; i < n; i++) {
|
|
422
422
|
const tangent = i === 0 && branch.parent === null ? [
|
|
423
423
|
0,
|
|
424
424
|
1,
|
|
425
425
|
0
|
|
426
|
-
] : 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)));
|
|
427
427
|
let normal;
|
|
428
428
|
if (i === 0) normal = perpendicular(tangent);
|
|
429
429
|
else {
|
|
430
430
|
const previous = frames[i - 1].normal;
|
|
431
|
-
normal = normalize(sub(previous, scale(tangent, dot$1(previous, tangent))));
|
|
431
|
+
normal = normalize$1(sub(previous, scale(tangent, dot$1(previous, tangent))));
|
|
432
432
|
}
|
|
433
433
|
frames.push({
|
|
434
434
|
tangent,
|
|
@@ -441,7 +441,7 @@ function branchFrames(branch) {
|
|
|
441
441
|
/** Total length of a branch's centre line. */
|
|
442
442
|
function pathLength(branch) {
|
|
443
443
|
let total = 0;
|
|
444
|
-
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));
|
|
445
445
|
return total;
|
|
446
446
|
}
|
|
447
447
|
/** The point, frame and radius at fraction `t` (0..1) of a branch's length. */
|
|
@@ -452,11 +452,11 @@ function sampleBranch(branch, frames, t) {
|
|
|
452
452
|
for (let i = 1; i < pts.length; i++) {
|
|
453
453
|
const a = pts[i - 1];
|
|
454
454
|
const b = pts[i];
|
|
455
|
-
const segment = length(sub(b.position, a.position));
|
|
455
|
+
const segment = length$1(sub(b.position, a.position));
|
|
456
456
|
if (walked + segment >= target || i === pts.length - 1) {
|
|
457
457
|
const f = segment > 0 ? Math.min(Math.max((target - walked) / segment, 0), 1) : 0;
|
|
458
|
-
const tangent = normalize(sub(b.position, a.position));
|
|
459
|
-
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))));
|
|
460
460
|
return {
|
|
461
461
|
position: lerp(a.position, b.position, f),
|
|
462
462
|
radius: a.radius + (b.radius - a.radius) * f,
|
|
@@ -475,7 +475,7 @@ function sampleBranch(branch, frames, t) {
|
|
|
475
475
|
};
|
|
476
476
|
}
|
|
477
477
|
// src/generator/branches.ts
|
|
478
|
-
var DEG$
|
|
478
|
+
var DEG$4 = Math.PI / 180;
|
|
479
479
|
/** How far `gravity` = ±1 turns a branch over its whole length (sum of per-section pulls). */
|
|
480
480
|
var GRAVITY_PER_BRANCH = 1.5;
|
|
481
481
|
/**
|
|
@@ -515,10 +515,10 @@ function growChildren(parent, level, random) {
|
|
|
515
515
|
const r = random.fork(path);
|
|
516
516
|
const t = level.start + (level.end - level.start) * (i + r.range(.15, .85)) / count;
|
|
517
517
|
const at = sampleBranch(parent, frames, t);
|
|
518
|
-
const angle = Math.min(level.angle * crownShape(level.angleProfile, t) * (1 + r.range(-level.angleVariation, level.angleVariation)), 180) * DEG$
|
|
519
|
-
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;
|
|
520
520
|
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
521
|
-
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))));
|
|
522
522
|
const pull = [
|
|
523
523
|
0,
|
|
524
524
|
level.gravity * GRAVITY_PER_BRANCH / level.sections,
|
|
@@ -540,7 +540,7 @@ function growChildren(parent, level, random) {
|
|
|
540
540
|
r.range(-1, 1),
|
|
541
541
|
r.range(-1, 1)
|
|
542
542
|
];
|
|
543
|
-
direction = normalize(add(add(direction, scale(wander, level.bend)), pull));
|
|
543
|
+
direction = normalize$1(add(add(direction, scale(wander, level.bend)), pull));
|
|
544
544
|
position = add(position, scale(direction, step));
|
|
545
545
|
}
|
|
546
546
|
children.push({
|
|
@@ -554,6 +554,218 @@ function growChildren(parent, level, random) {
|
|
|
554
554
|
}
|
|
555
555
|
return children;
|
|
556
556
|
}
|
|
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
|
+
};
|
|
576
|
+
/**
|
|
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.
|
|
619
|
+
*
|
|
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.
|
|
626
|
+
*/
|
|
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;
|
|
637
|
+
}
|
|
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
|
+
];
|
|
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
|
+
}
|
|
732
|
+
return {
|
|
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;
|
|
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;
|
|
768
|
+
}
|
|
557
769
|
// src/generator/wind.ts
|
|
558
770
|
var NO_SWAY = {
|
|
559
771
|
base: 0,
|
|
@@ -711,7 +923,7 @@ function clumpTemplate() {
|
|
|
711
923
|
0,
|
|
712
924
|
1
|
|
713
925
|
]
|
|
714
|
-
].map((v) => normalize(v));
|
|
926
|
+
].map((v) => normalize$1(v));
|
|
715
927
|
const centre = [
|
|
716
928
|
0,
|
|
717
929
|
0,
|
|
@@ -842,7 +1054,7 @@ function puffTemplate() {
|
|
|
842
1054
|
const key = a < b ? `${a}-${b}` : `${b}-${a}`;
|
|
843
1055
|
let index = midpoints.get(key);
|
|
844
1056
|
if (index === void 0) {
|
|
845
|
-
index = normals.push(normalize(add(normals[a], normals[b]))) - 1;
|
|
1057
|
+
index = normals.push(normalize$1(add(normals[a], normals[b]))) - 1;
|
|
846
1058
|
midpoints.set(key, index);
|
|
847
1059
|
}
|
|
848
1060
|
return index;
|
|
@@ -1001,7 +1213,7 @@ function solidGeometry(template, placements, part) {
|
|
|
1001
1213
|
const v = l * n + c;
|
|
1002
1214
|
const local = template.positions[c];
|
|
1003
1215
|
positions.set(add(placement.position, scale(toWorld(local), placement.size)), v * 3);
|
|
1004
|
-
normals.set(normalize(toWorld(template.normals[c])), v * 3);
|
|
1216
|
+
normals.set(normalize$1(toWorld(template.normals[c])), v * 3);
|
|
1005
1217
|
const along = Math.min(Math.max(local[2], 0), 1);
|
|
1006
1218
|
uvs.set([local[0] + .5, along], v * 2);
|
|
1007
1219
|
colors.set(placement.tint, v * 3);
|
|
@@ -1021,7 +1233,7 @@ function solidGeometry(template, placements, part) {
|
|
|
1021
1233
|
};
|
|
1022
1234
|
}
|
|
1023
1235
|
// src/generator/fruit.ts
|
|
1024
|
-
var DEG$
|
|
1236
|
+
var DEG$3 = Math.PI / 180;
|
|
1025
1237
|
/** Angle between twig and Fruit when it does not hang (`hang` 0), degrees. */
|
|
1026
1238
|
var FRUIT_TILT = 60;
|
|
1027
1239
|
/** Fruit sits on the outer part of its twig: from this fraction of the twig length to its tip. */
|
|
@@ -1046,7 +1258,7 @@ function placeFruit(definition, branches, random) {
|
|
|
1046
1258
|
1,
|
|
1047
1259
|
2
|
|
1048
1260
|
].map(() => tintRandom.range(-half, half));
|
|
1049
|
-
const tilt = FRUIT_TILT * DEG$
|
|
1261
|
+
const tilt = FRUIT_TILT * DEG$3;
|
|
1050
1262
|
const down = [
|
|
1051
1263
|
0,
|
|
1052
1264
|
-1,
|
|
@@ -1067,14 +1279,14 @@ function placeFruit(definition, branches, random) {
|
|
|
1067
1279
|
const at = sampleBranch(branch, frames, t);
|
|
1068
1280
|
const azimuth = r.range(0, Math.PI * 2);
|
|
1069
1281
|
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
1070
|
-
const hanging = lerp(normalize(add(scale(at.tangent, cos(tilt)), scale(across, sin$1(tilt)))), down, fruit.hang);
|
|
1071
|
-
const direction = length(hanging) > .001 ? normalize(hanging) : across;
|
|
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;
|
|
1072
1284
|
const side0 = cross$1(direction, [
|
|
1073
1285
|
0,
|
|
1074
1286
|
1,
|
|
1075
1287
|
0
|
|
1076
1288
|
]);
|
|
1077
|
-
const normal = dot$1(side0, side0) > 1e-6 ? normalize(cross$1(normalize(side0), direction)) : perpendicular(direction);
|
|
1289
|
+
const normal = dot$1(side0, side0) > 1e-6 ? normalize$1(cross$1(normalize$1(side0), direction)) : perpendicular(direction);
|
|
1078
1290
|
const size = fruit.size * (1 + r.range(-fruit.sizeVariation, fruit.sizeVariation));
|
|
1079
1291
|
const brightness = r.range(-half, half);
|
|
1080
1292
|
const tint = treeTint.map((c) => 1 + c + brightness);
|
|
@@ -1218,7 +1430,7 @@ function nutTemplate() {
|
|
|
1218
1430
|
0,
|
|
1219
1431
|
.5
|
|
1220
1432
|
];
|
|
1221
|
-
const directions = ico.positions.map((p) => normalize([
|
|
1433
|
+
const directions = ico.positions.map((p) => normalize$1([
|
|
1222
1434
|
p[0],
|
|
1223
1435
|
p[1],
|
|
1224
1436
|
p[2] - .5
|
|
@@ -1228,7 +1440,7 @@ function nutTemplate() {
|
|
|
1228
1440
|
const key = a < b ? `${a}-${b}` : `${b}-${a}`;
|
|
1229
1441
|
let index = midpoints.get(key);
|
|
1230
1442
|
if (index === void 0) {
|
|
1231
|
-
index = directions.push(normalize(add(directions[a], directions[b]))) - 1;
|
|
1443
|
+
index = directions.push(normalize$1(add(directions[a], directions[b]))) - 1;
|
|
1232
1444
|
midpoints.set(key, index);
|
|
1233
1445
|
}
|
|
1234
1446
|
return index;
|
|
@@ -1347,7 +1559,7 @@ function fruitTemplate(type, sides) {
|
|
|
1347
1559
|
/** Low-detail chestnut husk: an icosahedron (its corners are the spikes). */
|
|
1348
1560
|
function coarseNutTemplate() {
|
|
1349
1561
|
const ico = clumpTemplate();
|
|
1350
|
-
const directions = ico.positions.map((p) => normalize([
|
|
1562
|
+
const directions = ico.positions.map((p) => normalize$1([
|
|
1351
1563
|
p[0],
|
|
1352
1564
|
p[1],
|
|
1353
1565
|
p[2] - .5
|
|
@@ -1425,7 +1637,7 @@ function growthTiming(definition, seed, branches, leaves) {
|
|
|
1425
1637
|
};
|
|
1426
1638
|
}
|
|
1427
1639
|
// src/generator/leaves.ts
|
|
1428
|
-
var DEG$
|
|
1640
|
+
var DEG$2 = Math.PI / 180;
|
|
1429
1641
|
/** Leaf card width as a fraction of its length. */
|
|
1430
1642
|
var LEAF_WIDTH = .5;
|
|
1431
1643
|
/**
|
|
@@ -1445,7 +1657,7 @@ function placeLeaves(definition, branches, random) {
|
|
|
1445
1657
|
1,
|
|
1446
1658
|
2
|
|
1447
1659
|
].map(() => tintRandom.range(-half, half));
|
|
1448
|
-
const tilt = Math.min(leaves.angle, 180) * DEG$
|
|
1660
|
+
const tilt = Math.min(leaves.angle, 180) * DEG$2;
|
|
1449
1661
|
const out = [];
|
|
1450
1662
|
for (const branch of branches) {
|
|
1451
1663
|
if (branch.level !== outermost) continue;
|
|
@@ -1457,15 +1669,15 @@ function placeLeaves(definition, branches, random) {
|
|
|
1457
1669
|
const at = sampleBranch(branch, frames, t);
|
|
1458
1670
|
const azimuth = r.range(0, Math.PI * 2);
|
|
1459
1671
|
const across = add(scale(at.normal, cos(azimuth)), scale(at.binormal, sin$1(azimuth)));
|
|
1460
|
-
const direction = normalize(add(scale(at.tangent, cos(tilt)), scale(across, sin$1(tilt))));
|
|
1672
|
+
const direction = normalize$1(add(scale(at.tangent, cos(tilt)), scale(across, sin$1(tilt))));
|
|
1461
1673
|
const side0 = cross$1(direction, [
|
|
1462
1674
|
0,
|
|
1463
1675
|
1,
|
|
1464
1676
|
0
|
|
1465
1677
|
]);
|
|
1466
|
-
const side = dot$1(side0, side0) > 1e-6 ? normalize(side0) : perpendicular(direction);
|
|
1678
|
+
const side = dot$1(side0, side0) > 1e-6 ? normalize$1(side0) : perpendicular(direction);
|
|
1467
1679
|
const roll = r.range(-.4, .4);
|
|
1468
|
-
const normal = normalize(add(scale(cross$1(side, direction), cos(roll)), scale(side, sin$1(roll))));
|
|
1680
|
+
const normal = normalize$1(add(scale(cross$1(side, direction), cos(roll)), scale(side, sin$1(roll))));
|
|
1469
1681
|
const size = leaves.size * (1 + r.range(-leaves.sizeVariation, leaves.sizeVariation));
|
|
1470
1682
|
const brightness = r.range(-half, half);
|
|
1471
1683
|
const tint = treeTint.map((c) => 1 + c + brightness);
|
|
@@ -1551,10 +1763,136 @@ function leafGeometry(leaves, level, seed, variation = { season: 0 }, growth = [
|
|
|
1551
1763
|
attributes
|
|
1552
1764
|
};
|
|
1553
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
|
+
}
|
|
1554
1891
|
// src/generator/mesh.ts
|
|
1555
1892
|
/**
|
|
1556
1893
|
* Sweeps a ring of `sides` vertices along a branch's points, using parallel-transport
|
|
1557
|
-
* 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.
|
|
1558
1896
|
*/
|
|
1559
1897
|
function sweepBranch(branch, sides, random, season = {
|
|
1560
1898
|
shift: 0,
|
|
@@ -1563,12 +1901,11 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1563
1901
|
}, growth = {
|
|
1564
1902
|
start: 0,
|
|
1565
1903
|
end: 0
|
|
1566
|
-
}, sway) {
|
|
1904
|
+
}, sway, stem) {
|
|
1567
1905
|
const pts = branch.points;
|
|
1568
|
-
const
|
|
1569
|
-
const rings = pts.length;
|
|
1906
|
+
const rings = stem && stem.flare > 0 ? flaredRings(branch, stem) : plainRings(branch);
|
|
1570
1907
|
const ringSize = sides + 1;
|
|
1571
|
-
const vertexCount = rings * ringSize + 1;
|
|
1908
|
+
const vertexCount = rings.length * ringSize + 1;
|
|
1572
1909
|
const positions = new Float32Array(vertexCount * 3);
|
|
1573
1910
|
const normals = new Float32Array(vertexCount * 3);
|
|
1574
1911
|
const uvs = new Float32Array(vertexCount * 2);
|
|
@@ -1583,22 +1920,21 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1583
1920
|
growth,
|
|
1584
1921
|
sway
|
|
1585
1922
|
};
|
|
1586
|
-
const slope = (pts[0].radius - pts[rings - 1].radius) / Math.max(pathLength(branch), 1e-6);
|
|
1587
1923
|
let v = 0;
|
|
1588
|
-
|
|
1589
|
-
const { position, radius } = pts[r];
|
|
1590
|
-
const { tangent, normal, binormal } = frames[r];
|
|
1924
|
+
rings.forEach(({ position, radius, tangent, normal, binormal, along, slope }, r) => {
|
|
1591
1925
|
for (let s = 0; s <= sides; s++) {
|
|
1592
1926
|
const angle = s % sides / sides * Math.PI * 2;
|
|
1593
1927
|
const radial = add(scale(normal, cos(angle)), scale(binormal, sin$1(angle)));
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
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);
|
|
1598
1934
|
v++;
|
|
1599
1935
|
}
|
|
1600
|
-
}
|
|
1601
|
-
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++) {
|
|
1602
1938
|
const a = r * ringSize + s;
|
|
1603
1939
|
const b = a + 1;
|
|
1604
1940
|
const c = a + ringSize;
|
|
@@ -1606,11 +1942,11 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1606
1942
|
indices.push(a, b, c, b, d, c);
|
|
1607
1943
|
}
|
|
1608
1944
|
const tip = v;
|
|
1609
|
-
positions.set(pts[
|
|
1610
|
-
normals.set(
|
|
1945
|
+
positions.set(pts[pts.length - 1].position, tip * 3);
|
|
1946
|
+
normals.set(rings[rings.length - 1].tangent, tip * 3);
|
|
1611
1947
|
uvs.set([.5, 1], tip * 2);
|
|
1612
1948
|
writePart(attributes, tip, part, 1);
|
|
1613
|
-
const lastRing = (rings - 1) * ringSize;
|
|
1949
|
+
const lastRing = (rings.length - 1) * ringSize;
|
|
1614
1950
|
for (let s = 0; s < sides; s++) indices.push(lastRing + s, lastRing + s + 1, tip);
|
|
1615
1951
|
return {
|
|
1616
1952
|
positions,
|
|
@@ -1620,6 +1956,72 @@ function sweepBranch(branch, sides, random, season = {
|
|
|
1620
1956
|
attributes
|
|
1621
1957
|
};
|
|
1622
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
|
+
});
|
|
2024
|
+
}
|
|
1623
2025
|
/** Concatenates geometry buffers into one (indices are offset). */
|
|
1624
2026
|
function mergeBuffers(parts) {
|
|
1625
2027
|
const vertexCount = parts.reduce((n, p) => n + p.positions.length / 3, 0);
|
|
@@ -1644,6 +2046,281 @@ function mergeBuffers(parts) {
|
|
|
1644
2046
|
}
|
|
1645
2047
|
return out;
|
|
1646
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
|
+
}
|
|
1647
2324
|
// src/generator/sculpt.ts
|
|
1648
2325
|
/**
|
|
1649
2326
|
* The Sculpt pass (ADR 0003): hand edits stored in the Definition, applied after the procedural
|
|
@@ -1732,125 +2409,6 @@ function eulerXYZ([x, y, z]) {
|
|
|
1732
2409
|
m[6] * vx + m[7] * vy + m[8] * vz
|
|
1733
2410
|
];
|
|
1734
2411
|
}
|
|
1735
|
-
// src/generator/trunk.ts
|
|
1736
|
-
var DEG = Math.PI / 180;
|
|
1737
|
-
/** The leader: the part of a Stem above this fraction of its length is what trunk gravity bends. */
|
|
1738
|
-
var LEADER_START = 2 / 3;
|
|
1739
|
-
/** How far trunk `gravity` = ±1 turns the leader over its length, degrees. */
|
|
1740
|
-
var LEADER_BEND = 120;
|
|
1741
|
-
/** Seed range of each Stem's lean: ± this fraction of `spread`. */
|
|
1742
|
-
var SPREAD_VARIATION = .3;
|
|
1743
|
-
/** Seed range of each Stem's place around the ring: ± this fraction of the gap between Stems. */
|
|
1744
|
-
var RING_JITTER = .25;
|
|
1745
|
-
/** Radius of the ring the Stems stand in, as a fraction of the trunk radius (several Stems only). */
|
|
1746
|
-
var RING_RADIUS = 1;
|
|
1747
|
-
/**
|
|
1748
|
-
* Builds the Stems: `trunk.stems` full trunks standing in a small ring at the base, paths "0", "1", ...
|
|
1749
|
-
*
|
|
1750
|
-
* Path stability (ADR 0003): Stem 0's centre line draws from `random.fork('trunk')` exactly as a
|
|
1751
|
-
* single trunk always did, Stem i > 0 from `random.fork('trunk/<i>')`; the ring and the leans come
|
|
1752
|
-
* from `random.fork('stems')`. So a single upright Stem without gravity is today's trunk, bit for bit.
|
|
1753
|
-
*/
|
|
1754
|
-
function buildStems(definition, random) {
|
|
1755
|
-
const { trunk } = definition;
|
|
1756
|
-
const { stems: count = 1, spread = 0, gravity = 0 } = trunk;
|
|
1757
|
-
const ring = random.fork("stems");
|
|
1758
|
-
const turn = ring.range(0, Math.PI * 2);
|
|
1759
|
-
const stems = [];
|
|
1760
|
-
for (let i = 0; i < count; i++) {
|
|
1761
|
-
const r = ring.fork(i);
|
|
1762
|
-
const azimuth = turn + (i + r.range(-.25, RING_JITTER)) / count * Math.PI * 2;
|
|
1763
|
-
const tilt = Math.min(spread * (1 + r.range(-.3, SPREAD_VARIATION)), 90) * DEG;
|
|
1764
|
-
const offset = count > 1 ? trunk.radius * RING_RADIUS : 0;
|
|
1765
|
-
const line = centreLine(trunk, random.fork(i === 0 ? "trunk" : `trunk/${i}`));
|
|
1766
|
-
stems.push(placeStem(String(i), line, gravity, azimuth, tilt, offset));
|
|
1767
|
-
}
|
|
1768
|
-
return stems;
|
|
1769
|
-
}
|
|
1770
|
-
/** One upright, unbent Stem's centre line at the origin, varied by the Seed within the trunk's ranges. */
|
|
1771
|
-
function centreLine(trunk, random) {
|
|
1772
|
-
const length = trunk.length * (1 + random.range(-trunk.lengthVariation, trunk.lengthVariation));
|
|
1773
|
-
const sectionLength = length / trunk.sections;
|
|
1774
|
-
const maxDrift = trunk.bend * sectionLength;
|
|
1775
|
-
const points = [];
|
|
1776
|
-
let x = 0;
|
|
1777
|
-
let z = 0;
|
|
1778
|
-
for (let i = 0; i <= trunk.sections; i++) {
|
|
1779
|
-
const t = i / trunk.sections;
|
|
1780
|
-
if (i > 0) {
|
|
1781
|
-
x += random.range(-maxDrift, maxDrift);
|
|
1782
|
-
z += random.range(-maxDrift, maxDrift);
|
|
1783
|
-
}
|
|
1784
|
-
points.push({
|
|
1785
|
-
position: [
|
|
1786
|
-
x,
|
|
1787
|
-
t * length,
|
|
1788
|
-
z
|
|
1789
|
-
],
|
|
1790
|
-
radius: trunk.radius * (1 + (trunk.taper - 1) * t)
|
|
1791
|
-
});
|
|
1792
|
-
}
|
|
1793
|
-
return points;
|
|
1794
|
-
}
|
|
1795
|
-
/**
|
|
1796
|
-
* Bends the leader by `gravity`, leans the Stem `tilt` radians out towards `azimuth` and moves its
|
|
1797
|
-
* base `offset` out along the ring. All bending happens in the vertical plane towards `azimuth`.
|
|
1798
|
-
*/
|
|
1799
|
-
function placeStem(path, line, gravity, azimuth, tilt, offset) {
|
|
1800
|
-
const out = [
|
|
1801
|
-
cos(azimuth),
|
|
1802
|
-
0,
|
|
1803
|
-
sin$1(azimuth)
|
|
1804
|
-
];
|
|
1805
|
-
const axis = cross$1([
|
|
1806
|
-
0,
|
|
1807
|
-
1,
|
|
1808
|
-
0
|
|
1809
|
-
], out);
|
|
1810
|
-
let points = line;
|
|
1811
|
-
if (gravity !== 0) {
|
|
1812
|
-
const sections = line.length - 1;
|
|
1813
|
-
const first = Math.min(sections - 1, Math.ceil(LEADER_START * sections));
|
|
1814
|
-
const step = -gravity * LEADER_BEND * DEG / (sections - first);
|
|
1815
|
-
let bend = 0;
|
|
1816
|
-
points = line.slice(0, first + 1);
|
|
1817
|
-
for (let k = first; k < sections; k++) {
|
|
1818
|
-
bend = Math.min(Math.max(bend + step, -tilt), Math.PI - tilt);
|
|
1819
|
-
const segment = sub(line[k + 1].position, line[k].position);
|
|
1820
|
-
points.push({
|
|
1821
|
-
position: add(points[k].position, rotate$1(segment, axis, bend)),
|
|
1822
|
-
radius: line[k + 1].radius
|
|
1823
|
-
});
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
if (tilt !== 0) points = points.map((p) => ({
|
|
1827
|
-
position: rotate$1(p.position, axis, tilt),
|
|
1828
|
-
radius: p.radius
|
|
1829
|
-
}));
|
|
1830
|
-
if (offset !== 0) points = points.map((p) => ({
|
|
1831
|
-
position: add(p.position, scale(out, offset)),
|
|
1832
|
-
radius: p.radius
|
|
1833
|
-
}));
|
|
1834
|
-
return {
|
|
1835
|
-
path,
|
|
1836
|
-
parent: null,
|
|
1837
|
-
level: 0,
|
|
1838
|
-
attach: 0,
|
|
1839
|
-
direction: rotate$1([
|
|
1840
|
-
0,
|
|
1841
|
-
1,
|
|
1842
|
-
0
|
|
1843
|
-
], axis, tilt),
|
|
1844
|
-
points
|
|
1845
|
-
};
|
|
1846
|
-
}
|
|
1847
|
-
/** Rotates `v` about the unit `axis` by `angle` radians (Rodrigues). */
|
|
1848
|
-
function rotate$1(v, axis, angle) {
|
|
1849
|
-
if (angle === 0) return v;
|
|
1850
|
-
const c = cos(angle);
|
|
1851
|
-
const s = sin$1(angle);
|
|
1852
|
-
return add(add(scale(v, c), scale(cross$1(axis, v), s)), scale(axis, dot$1(axis, v) * (1 - c)));
|
|
1853
|
-
}
|
|
1854
2412
|
// src/generator/index.ts
|
|
1855
2413
|
/**
|
|
1856
2414
|
* The Generator: a pure function from a (parsed) Tree Definition and a Seed to tree data.
|
|
@@ -1867,30 +2425,48 @@ function generateTree(definition, seed) {
|
|
|
1867
2425
|
const sidesOf = (level) => level === 0 ? definition.trunk.sides : definition.branches[level - 1].sides;
|
|
1868
2426
|
const leaves = sculptLeaves(placeLeaves(definition, branches, random.fork("leaves")), sculpts, seed);
|
|
1869
2427
|
const fruit = placeFruit(definition, branches, random.fork("fruit"));
|
|
2428
|
+
const roots = growRoots(definition, branches.filter((b) => b.parent === null), random);
|
|
1870
2429
|
const partGrowth = growthTiming(definition, seed, branches, leaves);
|
|
1871
2430
|
const growth = {
|
|
1872
2431
|
...partGrowth,
|
|
1873
|
-
fruit: fruitGrowth(seed, fruit, new Map(branches.map((b, i) => [b.path, partGrowth.branches[i]])))
|
|
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
|
|
1874
2441
|
};
|
|
1875
2442
|
const sway = branchSway(branches, definition.branches.length);
|
|
1876
|
-
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]))]);
|
|
1877
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);
|
|
1878
2446
|
const fruitBuffers = fruitGeometry(fruit, definition.fruit ?? { type: "round" }, definition.branches.length + 2, seed, definition.variation, growth.fruit, sway);
|
|
1879
2447
|
const skeleton = {
|
|
1880
2448
|
branches,
|
|
1881
2449
|
leaves,
|
|
1882
|
-
fruit
|
|
2450
|
+
fruit,
|
|
2451
|
+
roots
|
|
1883
2452
|
};
|
|
1884
2453
|
writeTrunkSway(wood, trunkHeight(skeleton));
|
|
1885
2454
|
writeTrunkSway(leafBuffers, trunkHeight(skeleton));
|
|
1886
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);
|
|
1887
2462
|
return {
|
|
1888
2463
|
seed,
|
|
1889
2464
|
skeleton,
|
|
1890
2465
|
wood,
|
|
1891
2466
|
leaves: leafBuffers,
|
|
1892
2467
|
fruit: fruitBuffers,
|
|
1893
|
-
growth
|
|
2468
|
+
growth,
|
|
2469
|
+
crown
|
|
1894
2470
|
};
|
|
1895
2471
|
}
|
|
1896
2472
|
/**
|
|
@@ -1944,7 +2520,7 @@ var SAPLING_SCALE = .2;
|
|
|
1944
2520
|
var SAPLING_THICKNESS = .4;
|
|
1945
2521
|
/** Smallest window width, so parts with `start == end` (the trunk) are simply grown. */
|
|
1946
2522
|
var MIN_WINDOW = 1e-5;
|
|
1947
|
-
var treeScale
|
|
2523
|
+
var treeScale = (growth) => SAPLING_SCALE + .8 * growth;
|
|
1948
2524
|
var thickness$1 = (growth) => SAPLING_THICKNESS + .6 * growth;
|
|
1949
2525
|
/** How far one part has sprouted (0 = not yet, 1 = full size), eased; 1 from `end` on. */
|
|
1950
2526
|
function partProgress$1(growth, start, end) {
|
|
@@ -1964,7 +2540,7 @@ function growPositions(buffers, growth) {
|
|
|
1964
2540
|
const { positions, normals } = buffers;
|
|
1965
2541
|
const { aOrigin, aGrowth } = buffers.attributes;
|
|
1966
2542
|
const out = new Float32Array(positions.length);
|
|
1967
|
-
const scale = treeScale
|
|
2543
|
+
const scale = treeScale(g);
|
|
1968
2544
|
const thin = 1 - thickness$1(g);
|
|
1969
2545
|
for (let v = 0; v < positions.length / 3; v++) {
|
|
1970
2546
|
const [start, end, radius] = [
|
|
@@ -1983,8 +2559,8 @@ function growPositions(buffers, growth) {
|
|
|
1983
2559
|
return out;
|
|
1984
2560
|
}
|
|
1985
2561
|
// src/growth/tsl.ts
|
|
1986
|
-
/** Size of the whole tree at Growth `g` (twin of `treeScale`). */
|
|
1987
|
-
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));
|
|
1988
2564
|
/** Branch thickness at Growth `g` (twin of `thickness`). */
|
|
1989
2565
|
var thickness = (g) => float(SAPLING_THICKNESS).add(float(1 - SAPLING_THICKNESS).mul(g));
|
|
1990
2566
|
/** How far one part has sprouted, eased (twin of `partProgress`). */
|
|
@@ -2010,13 +2586,31 @@ function growthPosition(growth, position = positionLocal) {
|
|
|
2010
2586
|
data.y,
|
|
2011
2587
|
data.z
|
|
2012
2588
|
];
|
|
2013
|
-
const scale =
|
|
2589
|
+
const scale = growthScale(g);
|
|
2014
2590
|
const thin = float(1).sub(thickness(g));
|
|
2015
2591
|
const shrink = float(1).sub(partProgress(g, start, end));
|
|
2016
2592
|
const thick = position.sub(normalGeometry.mul(radius).mul(thin));
|
|
2017
2593
|
const sprouted = thick.sub(thick.sub(origin).mul(shrink));
|
|
2018
2594
|
return sprouted.sub(sprouted.mul(float(1).sub(scale)));
|
|
2019
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
|
+
}
|
|
2020
2614
|
// src/textures/index.ts
|
|
2021
2615
|
var TEXTURES_DIR = "./textures/";
|
|
2022
2616
|
var MODULE_URL = import.meta.url;
|
|
@@ -2125,6 +2719,78 @@ function applyLeafMap(material, leaf, textures) {
|
|
|
2125
2719
|
material.alphaTest = leaf.alphaTest;
|
|
2126
2720
|
}
|
|
2127
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
|
+
}
|
|
2128
2794
|
// src/materials/toon.ts
|
|
2129
2795
|
/** Brightness of the shaded side, relative to the lit side (the toon "shadow tone"). */
|
|
2130
2796
|
var SHADE = .5;
|
|
@@ -2135,18 +2801,32 @@ var BAND_END = .2;
|
|
|
2135
2801
|
* Soft toon lighting: two tones (lit and shaded) per light with a smooth band in between,
|
|
2136
2802
|
* plus plain diffuse ambient/hemisphere light. No specular or environment reflections. Built
|
|
2137
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.
|
|
2138
2807
|
*/
|
|
2139
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
|
+
}
|
|
2140
2818
|
direct({ lightDirection, lightColor, reflectedLight }) {
|
|
2141
2819
|
const dotNL = normalView.dot(lightDirection);
|
|
2142
2820
|
const tone = mix(float(SHADE), float(1), smoothstep(BAND_START, BAND_END, dotNL));
|
|
2143
2821
|
const irradiance = lightColor.mul(tone);
|
|
2144
2822
|
reflectedLight.directDiffuse.addAssign(irradiance.mul(BRDF_Lambert({ diffuseColor: diffuseColor.rgb })));
|
|
2823
|
+
if (this.translucency) reflectedLight.directDiffuse.addAssign(translucencyDirect(this.translucency, lightDirection, lightColor));
|
|
2145
2824
|
}
|
|
2146
2825
|
indirect(builder) {
|
|
2147
2826
|
const { ambientOcclusion, irradiance, reflectedLight } = builder.context;
|
|
2148
2827
|
reflectedLight.indirectDiffuse.addAssign(irradiance.mul(BRDF_Lambert({ diffuseColor })));
|
|
2149
2828
|
reflectedLight.indirectDiffuse.mulAssign(ambientOcclusion);
|
|
2829
|
+
if (this.translucency) reflectedLight.indirectDiffuse.addAssign(translucencyIndirect(this.translucency, builder));
|
|
2150
2830
|
}
|
|
2151
2831
|
};
|
|
2152
2832
|
/**
|
|
@@ -2181,19 +2861,43 @@ function createShadedMaterial(shading) {
|
|
|
2181
2861
|
}
|
|
2182
2862
|
/** Whether a shading model wants solid (flat) colours rather than smooth colour gradients. */
|
|
2183
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
|
+
}
|
|
2184
2883
|
// src/materials/index.ts
|
|
2185
2884
|
/**
|
|
2186
2885
|
* Bark material: a TSL node material (ADR 0002). Growth/Season/Wind uniforms and
|
|
2187
2886
|
* per-Style variants hook in here in later tickets (#5-#10). `materials.bark.map` / `.normalMap`
|
|
2188
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.
|
|
2189
2891
|
*/
|
|
2190
|
-
function createBarkMaterial(definition, textures) {
|
|
2892
|
+
function createBarkMaterial(definition, textures, growth = float(1)) {
|
|
2191
2893
|
const material = createShadedMaterial(definition.materials.shading);
|
|
2192
2894
|
material.name = "tree.js bark";
|
|
2193
2895
|
material.userData.treePart = "bark";
|
|
2194
2896
|
const base = color(definition.materials.bark.color);
|
|
2195
2897
|
const shaded = isFlatShading(definition.materials.shading) ? base : mix(base.mul(.75), base, uv().y.clamp(0, 1).pow(float(.5)));
|
|
2196
|
-
|
|
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);
|
|
2197
2901
|
material.roughness = definition.materials.bark.roughness;
|
|
2198
2902
|
material.metalness = 0;
|
|
2199
2903
|
return material;
|
|
@@ -2233,6 +2937,130 @@ function createFruitMaterial(definition) {
|
|
|
2233
2937
|
}
|
|
2234
2938
|
/** Fruit skin is a little glossier than leaves. */
|
|
2235
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
|
+
};
|
|
2236
3064
|
// src/renderer/index.ts
|
|
2237
3065
|
/** Thrown when a tree is drawn by anything other than `WebGPURenderer` (ADR 0002). */
|
|
2238
3066
|
var UnsupportedRendererError = class extends Error {
|
|
@@ -2558,6 +3386,7 @@ var Tree = class extends Group {
|
|
|
2558
3386
|
/** This tree's response to the Wind, from `definition.wind`; `windResponse.set(...)` changes it live. */
|
|
2559
3387
|
windResponse;
|
|
2560
3388
|
windBinding;
|
|
3389
|
+
features;
|
|
2561
3390
|
/**
|
|
2562
3391
|
* Resolves once every texture of this tree has loaded (at once for a tree without textures);
|
|
2563
3392
|
* rejects if one fails. Await it before a first render that must show the textures.
|
|
@@ -2570,7 +3399,7 @@ var Tree = class extends Group {
|
|
|
2570
3399
|
this.seed = data.seed;
|
|
2571
3400
|
this.data = data;
|
|
2572
3401
|
this.growthUniform = uniform(clampGrowth(options.growth ?? 1));
|
|
2573
|
-
const bark = createBarkMaterial(definition, options.textures);
|
|
3402
|
+
const bark = createBarkMaterial(definition, options.textures, this.growthUniform);
|
|
2574
3403
|
const leaf = createLeafMaterial(definition, options.textures);
|
|
2575
3404
|
bark.positionNode = growthPosition(this.growthUniform);
|
|
2576
3405
|
leaf.positionNode = growthPosition(this.growthUniform);
|
|
@@ -2602,6 +3431,56 @@ var Tree = class extends Group {
|
|
|
2602
3431
|
fruit.positionNode = growthPosition(this.growthUniform, fruitSeason.position);
|
|
2603
3432
|
applyWind(fruit, offset);
|
|
2604
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;
|
|
2605
3484
|
}
|
|
2606
3485
|
/**
|
|
2607
3486
|
* The Wind this tree sways with (shared with every other tree using it). Assigning another
|
|
@@ -2694,8 +3573,8 @@ function instanceState() {
|
|
|
2694
3573
|
windPhase: state.w
|
|
2695
3574
|
};
|
|
2696
3575
|
}
|
|
2697
|
-
/** Rotates `v` by the instance quaternion (`inverse` = by its conjugate). */
|
|
2698
|
-
function
|
|
3576
|
+
/** Rotates `v` by the instance quaternion (`inverse` = by its conjugate): tree space to forest space. */
|
|
3577
|
+
function instanceRotate(v, inverse = false) {
|
|
2699
3578
|
const q = attribute("iRotation", "vec4");
|
|
2700
3579
|
const u = inverse ? q.xyz.negate() : q.xyz;
|
|
2701
3580
|
const t = cross(u, v).mul(2);
|
|
@@ -2718,11 +3597,11 @@ function forestPosition(wind, response, base = positionGeometry) {
|
|
|
2718
3597
|
const { scale, growth, windPhase } = instanceState();
|
|
2719
3598
|
const grown = growthPosition(growth, base);
|
|
2720
3599
|
const offset = windOffset(wind, response, {
|
|
2721
|
-
direction:
|
|
3600
|
+
direction: instanceRotate(objectWindDirection(wind), true),
|
|
2722
3601
|
phase: windPhase
|
|
2723
3602
|
});
|
|
2724
3603
|
const displacement = grown.add(offset).sub(positionGeometry);
|
|
2725
|
-
return positionLocal.add(
|
|
3604
|
+
return positionLocal.add(instanceRotate(displacement).mul(scale));
|
|
2726
3605
|
}
|
|
2727
3606
|
// src/forest/placement.ts
|
|
2728
3607
|
function placementPosition(placement) {
|
|
@@ -2795,6 +3674,8 @@ var ForestTree = class {
|
|
|
2795
3674
|
windPhase;
|
|
2796
3675
|
/** @internal Slot in the Variant's instance buffers. */
|
|
2797
3676
|
slot = -1;
|
|
3677
|
+
/** @internal The Variant drawing this tree, null once it is removed. */
|
|
3678
|
+
owner = null;
|
|
2798
3679
|
growthValue;
|
|
2799
3680
|
seasonValue;
|
|
2800
3681
|
/** @internal Use {@link Forest.addTree}. */
|
|
@@ -2918,7 +3799,7 @@ var ForestVariant = class {
|
|
|
2918
3799
|
phase: 0
|
|
2919
3800
|
});
|
|
2920
3801
|
const position = forestPosition(wind, this.windResponse);
|
|
2921
|
-
this.barkMaterial = createBarkMaterial(definition, textures);
|
|
3802
|
+
this.barkMaterial = createBarkMaterial(definition, textures, instanceState().growth);
|
|
2922
3803
|
this.barkMaterial.positionNode = position;
|
|
2923
3804
|
this.leafMaterial = createLeafMaterial(definition, textures);
|
|
2924
3805
|
this.leafMaterial.positionNode = position;
|
|
@@ -2964,6 +3845,7 @@ var ForestVariant = class {
|
|
|
2964
3845
|
add(tree) {
|
|
2965
3846
|
if (this.trees.length === this.capacity) this.allocate(this.capacity * 2);
|
|
2966
3847
|
tree.slot = this.trees.length;
|
|
3848
|
+
tree.owner = this;
|
|
2967
3849
|
this.trees.push(tree);
|
|
2968
3850
|
this.write(tree);
|
|
2969
3851
|
this.setCount();
|
|
@@ -2977,6 +3859,7 @@ var ForestVariant = class {
|
|
|
2977
3859
|
this.write(last);
|
|
2978
3860
|
}
|
|
2979
3861
|
tree.slot = -1;
|
|
3862
|
+
tree.owner = null;
|
|
2980
3863
|
this.setCount();
|
|
2981
3864
|
}
|
|
2982
3865
|
/** @internal Writes one tree's transform and state into its instance slot. */
|
|
@@ -3083,6 +3966,7 @@ var Forest = class extends Group {
|
|
|
3083
3966
|
windBinding;
|
|
3084
3967
|
growthValue;
|
|
3085
3968
|
seasonValue;
|
|
3969
|
+
features;
|
|
3086
3970
|
constructor(definition, placements = [], options = {}) {
|
|
3087
3971
|
super();
|
|
3088
3972
|
const count = options.variants ?? 4;
|
|
@@ -3095,6 +3979,17 @@ var Forest = class extends Group {
|
|
|
3095
3979
|
this.seasonValue = checkedSeason(options.season ?? 0);
|
|
3096
3980
|
const capacity = Math.max(MIN_CAPACITY, 2 ** Math.ceil(Math.log2(Math.max(1, placements.length / count * 1.25))));
|
|
3097
3981
|
this.variants = Array.from({ length: count }, (_, i) => new ForestVariant(this, definition, i, variantSeed(this.seed, i), this.windBinding, capacity, options.textures));
|
|
3982
|
+
this.features = new LiveMaterialFeatures(definition, this.variants.map((v) => ({
|
|
3983
|
+
leaf: v.leafMaterial,
|
|
3984
|
+
materials: [
|
|
3985
|
+
v.barkMaterial,
|
|
3986
|
+
v.leafMaterial,
|
|
3987
|
+
v.fruitMaterial
|
|
3988
|
+
],
|
|
3989
|
+
crown: v.data.crown,
|
|
3990
|
+
growth: instanceState().growth,
|
|
3991
|
+
toObject: (normal) => instanceRotate(normal)
|
|
3992
|
+
})));
|
|
3098
3993
|
const textures = this.variants.flatMap((v) => [
|
|
3099
3994
|
v.barkMaterial.map,
|
|
3100
3995
|
v.barkMaterial.normalMap,
|
|
@@ -3182,6 +4077,45 @@ var Forest = class extends Group {
|
|
|
3182
4077
|
this.seasonValue = checkedSeason(value);
|
|
3183
4078
|
for (const tree of this.byKey.values()) tree.season = this.seasonValue;
|
|
3184
4079
|
}
|
|
4080
|
+
/**
|
|
4081
|
+
* Crown Normals amount (0..1), from the Definition's `materials.leaf.crownNormals`; null when it is off.
|
|
4082
|
+
* Setting it (clamped to 0..1) only writes a uniform: no regeneration, no recompile. Throws on a
|
|
4083
|
+
* forest without Crown Normals (switching the feature on needs a new forest).
|
|
4084
|
+
*/
|
|
4085
|
+
get crownNormals() {
|
|
4086
|
+
return this.features.crownNormals;
|
|
4087
|
+
}
|
|
4088
|
+
set crownNormals(value) {
|
|
4089
|
+
this.features.crownNormals = value;
|
|
4090
|
+
}
|
|
4091
|
+
/**
|
|
4092
|
+
* Leaf Translucency settings (a copy), from the Definition's `materials.leaf.translucency`; null when it
|
|
4093
|
+
* is off. Setting any of them (each clamped to 0..1, the others kept) only writes uniforms: no
|
|
4094
|
+
* regeneration, no recompile. Throws on a forest without Leaf Translucency.
|
|
4095
|
+
*/
|
|
4096
|
+
get translucency() {
|
|
4097
|
+
return this.features.translucency;
|
|
4098
|
+
}
|
|
4099
|
+
set translucency(value) {
|
|
4100
|
+
this.features.translucency = value;
|
|
4101
|
+
}
|
|
4102
|
+
/**
|
|
4103
|
+
* The Material Features this forest shows, with their current (possibly live-tuned) values: the
|
|
4104
|
+
* Definition's (see `materialFeaturesOf`), except that Crown Normals at 0 count as off.
|
|
4105
|
+
*/
|
|
4106
|
+
get materialFeatures() {
|
|
4107
|
+
return this.features.values;
|
|
4108
|
+
}
|
|
4109
|
+
/**
|
|
4110
|
+
* Structural Occlusion strength (0..1) of every tree, from the Definition's `materials.occlusion`; null
|
|
4111
|
+
* when it has none. Setting it (clamped to 0..1) only writes a uniform. Throws without Structural Occlusion.
|
|
4112
|
+
*/
|
|
4113
|
+
get occlusionStrength() {
|
|
4114
|
+
return this.features.occlusionStrength;
|
|
4115
|
+
}
|
|
4116
|
+
set occlusionStrength(value) {
|
|
4117
|
+
this.features.occlusionStrength = value;
|
|
4118
|
+
}
|
|
3185
4119
|
/** Frees every Variant's geometries, materials and instance buffers, and removes the forest from its parent. */
|
|
3186
4120
|
dispose() {
|
|
3187
4121
|
for (const variant of this.variants) variant.dispose();
|
|
@@ -3201,6 +4135,6 @@ var Forest = class extends Group {
|
|
|
3201
4135
|
function createForest(definition, placements = [], options = {}) {
|
|
3202
4136
|
return new Forest(parseDefinition(definition), placements, options);
|
|
3203
4137
|
}
|
|
3204
|
-
export {
|
|
4138
|
+
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 };
|
|
3205
4139
|
|
|
3206
|
-
//# sourceMappingURL=forest-
|
|
4140
|
+
//# sourceMappingURL=forest-lg61rhKM.js.map
|