roamwright 0.4.0 → 0.20.1
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/CONTRIBUTING.md +3 -1
- package/README.md +11 -34
- package/geometry/bedding.js +141 -0
- package/geometry/builder-cells.js +42 -0
- package/geometry/builder-frame.js +47 -0
- package/geometry/builder.js +189 -177
- package/geometry/cell-mask.js +25 -0
- package/geometry/creased-sheet.js +34 -34
- package/geometry/ear.js +36 -36
- package/geometry/erosion.js +65 -42
- package/geometry/face-grid.js +65 -65
- package/geometry/foliage.js +10 -10
- package/geometry/hair-gathers.js +41 -41
- package/geometry/hair-locks.js +41 -41
- package/geometry/hair-patch.js +18 -18
- package/geometry/hair-shell.js +31 -31
- package/geometry/head-profile.js +15 -15
- package/geometry/head-shape.js +77 -77
- package/geometry/heightfield.js +27 -18
- package/geometry/index-parts.js +2 -2
- package/geometry/instance-visibility.js +12 -12
- package/geometry/instances.js +15 -15
- package/geometry/outline-grid.js +7 -7
- package/geometry/part-ranges.js +3 -3
- package/geometry/path-field.js +63 -33
- package/geometry/placement.js +52 -52
- package/geometry/point-buckets.js +50 -0
- package/geometry/primitives.js +63 -47
- package/geometry/sagging-tube.js +27 -27
- package/geometry/sampled-surface.js +109 -77
- package/geometry/scatter-grid.js +46 -6
- package/geometry/sculpt.js +93 -93
- package/geometry/shore-grid.js +77 -34
- package/geometry/strata-field.js +92 -92
- package/geometry/strata-tiles.js +91 -91
- package/geometry/stream.js +70 -48
- package/geometry/terrain-chunks.js +87 -82
- package/geometry/terrain-extension.js +6 -6
- package/index.js +5 -0
- package/math/cell-edge.js +16 -16
- package/math/cell-key.js +6 -0
- package/math/circle-clearances.js +19 -0
- package/math/cloth-textures.js +6 -6
- package/math/cloud-textures.js +20 -20
- package/math/color.js +14 -14
- package/math/curve.js +22 -22
- package/math/mountain-ranges.js +216 -165
- package/math/noise.js +101 -49
- package/math/particle-noise.js +28 -28
- package/math/periodic.js +5 -5
- package/math/random.js +24 -15
- package/math/rectangles.js +52 -0
- package/math/rock-texture.js +7 -7
- package/math/scalar.js +5 -5
- package/math/surface-textures.js +54 -54
- package/motion/arm-reach.js +24 -24
- package/motion/ballistic-arc.js +11 -0
- package/motion/biped-cycle.js +44 -0
- package/motion/biped-leg.js +37 -0
- package/motion/biped-stride.js +98 -122
- package/motion/contact-transition.js +33 -0
- package/motion/crowd-alarms.js +39 -39
- package/motion/crowd-routine.js +94 -65
- package/motion/daily-clock.js +9 -9
- package/motion/deck-surface.js +38 -0
- package/motion/ground-lookahead.js +5 -5
- package/motion/instance-sight.js +8 -8
- package/motion/player.js +36 -28
- package/motion/quadruped.js +155 -155
- package/motion/spaces.js +32 -32
- package/motion/weather.js +73 -24
- package/motion/wind.js +32 -13
- package/package.json +7 -3
- package/rendering/caster-motion.js +5 -5
- package/rendering/cell-lod.js +138 -82
- package/rendering/cloud-dome.js +28 -28
- package/rendering/cloud-field.js +2 -2
- package/rendering/compilation-schedule.js +68 -11
- package/rendering/contact-shadows.js +2 -2
- package/rendering/context.js +12 -0
- package/rendering/crowd-skinning.js +13 -13
- package/rendering/crust.js +18 -12
- package/rendering/daylight.js +3 -3
- package/rendering/detail-range.js +28 -0
- package/rendering/dispose-meshes.js +18 -0
- package/rendering/dof.js +9 -9
- package/rendering/eye-adapt.js +14 -14
- package/rendering/far-shadow.js +8 -8
- package/rendering/flocks.js +78 -78
- package/rendering/garment-material.js +17 -17
- package/rendering/glow-lines.js +13 -13
- package/rendering/hair-strands.js +20 -20
- package/rendering/height-probe.js +149 -19
- package/rendering/label-layout.js +46 -0
- package/rendering/label-range.js +38 -0
- package/rendering/light-budget.js +52 -52
- package/rendering/light-shafts.js +23 -23
- package/rendering/local-lights.js +15 -4
- package/rendering/material-lod.js +49 -49
- package/rendering/particle-emitters.js +90 -90
- package/rendering/particles.js +78 -67
- package/rendering/piece-batch.js +17 -6
- package/rendering/planar-reflection.js +38 -38
- package/rendering/post-shaders.js +17 -13
- package/rendering/post.js +71 -71
- package/rendering/rain-streaks.js +67 -0
- package/rendering/road-surface.js +11 -11
- package/rendering/sea-shaders.js +11 -11
- package/rendering/shader-warmup.js +29 -16
- package/rendering/shadow-alignment.js +4 -4
- package/rendering/shadow-depth.js +7 -7
- package/rendering/sky-occlusion.js +16 -16
- package/rendering/sky.js +16 -6
- package/rendering/stage-lights.js +6 -6
- package/rendering/static-point-shadow.js +3 -3
- package/rendering/still-casters.js +1 -1
- package/rendering/strata-material.js +10 -10
- package/rendering/stream-flow.js +10 -10
- package/rendering/taa.js +26 -26
- package/rendering/talus.js +15 -15
- package/rendering/terrain-rock.js +7 -7
- package/rendering/texture-fills.js +8 -8
- package/rendering/textures.js +28 -28
- package/rendering/wakes.js +24 -24
- package/rendering/water-material.js +36 -23
- package/rendering/world-matrices.js +38 -38
- package/runtime/diagnostic-log.js +2 -2
- package/runtime/first-use.js +33 -28
- package/runtime/frame-pacing.js +33 -0
- package/runtime/gpu-timing.js +7 -7
- package/runtime/grid-cache.js +1 -1
- package/runtime/grid-rows.js +44 -5
- package/runtime/hand-over.js +41 -23
- package/runtime/pool.js +58 -16
- package/runtime/quality-fit.js +1 -1
- package/runtime/search.js +22 -22
- package/runtime/spatial-audio.js +13 -13
- package/runtime/withdrawable-task.js +35 -0
- package/runtime/worker.js +7 -0
package/geometry/path-field.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Measure the distance to a path or bake a sampled route into row-major terrain grids.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { clamp } from '../math/scalar.js';
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
/**
|
|
9
|
+
* @param {[number, number][]} line - Ordered X/Z points in world units.
|
|
10
|
+
* @returns {[number, number, number, number]} Bounds in the order [minX, maxX, minZ, maxZ].
|
|
11
|
+
*/
|
|
4
12
|
function pathBox(line) {
|
|
5
13
|
const box = [Infinity, -Infinity, Infinity, -Infinity];
|
|
6
14
|
for (const [x, z] of line) {
|
|
@@ -12,24 +20,34 @@ function pathBox(line) {
|
|
|
12
20
|
return box;
|
|
13
21
|
}
|
|
14
22
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Find the closest point on a path strictly inside the search radius.
|
|
25
|
+
* @param {{line: [number, number][], box: ReturnType<typeof pathBox>}} path - Polyline with its current bounds.
|
|
26
|
+
* @param {number} x - Query X in world units.
|
|
27
|
+
* @param {number} z - Query Z in world units.
|
|
28
|
+
* @param {number} reach - Maximum search distance in the same units.
|
|
29
|
+
* @param {{d?: number, t?: number, side?: number}} near - Caller-owned result. On a hit, d is the distance,
|
|
30
|
+
* t is the fractional segment index divided by the segment count, and side is the sign of the X/Z
|
|
31
|
+
* cross product of the segment direction with the query offset (positive for a point on the line).
|
|
32
|
+
* @returns True on a hit. A miss leaves the result intact.
|
|
33
|
+
*/
|
|
34
|
+
function nearestPathPoint(path, x, z, reach, near) {
|
|
35
|
+
const bounds = path.box;
|
|
36
|
+
if (x < bounds[0] - reach || x > bounds[1] + reach || z < bounds[2] - reach || z > bounds[3] + reach) return false;
|
|
19
37
|
let best = reach * reach,
|
|
20
38
|
at = -1;
|
|
21
|
-
for (let i = 0; i <
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
dx =
|
|
25
|
-
dz =
|
|
26
|
-
px = x -
|
|
27
|
-
pz = z -
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
best =
|
|
32
|
-
at = (i +
|
|
39
|
+
for (let i = 0; i < path.line.length - 1; i++) {
|
|
40
|
+
const segmentStart = path.line[i],
|
|
41
|
+
segmentEnd = path.line[i + 1],
|
|
42
|
+
dx = segmentEnd[0] - segmentStart[0],
|
|
43
|
+
dz = segmentEnd[1] - segmentStart[1],
|
|
44
|
+
px = x - segmentStart[0],
|
|
45
|
+
pz = z - segmentStart[1],
|
|
46
|
+
segmentFraction = clamp((px * dx + pz * dz) / (dx * dx + dz * dz), 0, 1),
|
|
47
|
+
distanceSquared = (px - dx * segmentFraction) ** 2 + (pz - dz * segmentFraction) ** 2;
|
|
48
|
+
if (distanceSquared < best) {
|
|
49
|
+
best = distanceSquared;
|
|
50
|
+
at = (i + segmentFraction) / (path.line.length - 1);
|
|
33
51
|
near.side = dx * pz - dz * px < 0 ? -1 : 1;
|
|
34
52
|
}
|
|
35
53
|
}
|
|
@@ -39,23 +57,35 @@ function nearestPathPoint(c, x, z, reach, near) {
|
|
|
39
57
|
return true;
|
|
40
58
|
}
|
|
41
59
|
|
|
42
|
-
|
|
43
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Update the grid around each path sample with the smallest distance seen so far.
|
|
62
|
+
* Initialise a fresh distance grid with Infinity. The radius bounds a square of candidate
|
|
63
|
+
* cells, so a written distance near a corner can exceed the radius. Densify long
|
|
64
|
+
* segments before stamping if intermediate positions should also contribute.
|
|
65
|
+
* @param {{x0: number, z0: number, width: number, height: number, spacing: number}} layout -
|
|
66
|
+
* Origin in world units, dimensions in samples and spacing in world units.
|
|
67
|
+
* @param {[number, number][]} points - Path samples in world X/Z.
|
|
68
|
+
* @param {number} radius - Nonnegative search extent in world units.
|
|
69
|
+
* @param {Float32Array | Float64Array | number[]} grid - Writable row-major nearest distances.
|
|
70
|
+
* @param {Float32Array | Float64Array | number[] | null} [valueGrid] - Optional companion grid updated when a sample wins.
|
|
71
|
+
* @param {(t: number) => number} [valueFn] - Value at the normalized point index. Defaults to zero.
|
|
72
|
+
*/
|
|
73
|
+
function stampPath({ x0, z0, width, height, spacing }, points, radius, grid, valueGrid, valueFn) {
|
|
44
74
|
if (!(spacing > 0) || !Number.isFinite(spacing) || !Number.isFinite(radius) || radius < 0) throw new RangeError('Expected positive spacing and a nonnegative radius');
|
|
45
|
-
const
|
|
46
|
-
for (let
|
|
47
|
-
const px =
|
|
48
|
-
pz =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
for (let j = Math.max(0,
|
|
53
|
-
for (let i = Math.max(0,
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
grid[
|
|
58
|
-
if (valueGrid) valueGrid[
|
|
75
|
+
const cellRadius = Math.ceil(radius / spacing);
|
|
76
|
+
for (let sampleIndex = 0; sampleIndex < points.length; sampleIndex++) {
|
|
77
|
+
const px = points[sampleIndex][0],
|
|
78
|
+
pz = points[sampleIndex][1],
|
|
79
|
+
centreColumn = Math.round((px - x0) / spacing),
|
|
80
|
+
centreRow = Math.round((pz - z0) / spacing);
|
|
81
|
+
const sampleValue = valueFn ? valueFn(points.length > 1 ? sampleIndex / (points.length - 1) : 0) : 0;
|
|
82
|
+
for (let j = Math.max(0, centreRow - cellRadius); j <= Math.min(height - 1, centreRow + cellRadius); j++) {
|
|
83
|
+
for (let i = Math.max(0, centreColumn - cellRadius); i <= Math.min(width - 1, centreColumn + cellRadius); i++) {
|
|
84
|
+
const distance = Math.hypot(i * spacing + x0 - px, j * spacing + z0 - pz),
|
|
85
|
+
cellIndex = j * width + i;
|
|
86
|
+
if (distance < grid[cellIndex]) {
|
|
87
|
+
grid[cellIndex] = distance;
|
|
88
|
+
if (valueGrid) valueGrid[cellIndex] = sampleValue;
|
|
59
89
|
}
|
|
60
90
|
}
|
|
61
91
|
}
|
package/geometry/placement.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// ════════════════════════════════════════════════════════════════════════
|
|
2
|
-
// An instance's matrix written straight into an instanced mesh's matrices (16 numbers
|
|
3
|
-
// thousands of instances a frame (crowds, herds, flocks).
|
|
2
|
+
// An instance's matrix written straight into an instanced mesh's matrices (16 numbers starting at `offset`), for code that places
|
|
3
|
+
// thousands of instances a frame (crowds, herds, flocks). The transform places each instance at (x, y, z), of size s, turned by ry about
|
|
4
4
|
// the vertical and then rx and rz in Three.js's 'YXZ' order, as TRS(x, y, z, s, s, s, ry, rx, rz) composes it, without its
|
|
5
5
|
// Euler angles and quaternion on the way. The placement is an object the caller makes once and fills in for each instance:
|
|
6
6
|
// numbers handed over one by one to a function the engine does not take into its caller whole are made anew at every call.
|
|
@@ -8,58 +8,58 @@
|
|
|
8
8
|
const placement = () => ({ x: 0, y: 0, z: 0, s: 1, ry: 0, rx: 0, rz: 0 });
|
|
9
9
|
|
|
10
10
|
// Turned and tipped (rx), not rolled: a person of a crowd, standing or fallen, a beast of a herd.
|
|
11
|
-
function placeAt(matrices,
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
matrices[
|
|
18
|
-
matrices[
|
|
19
|
-
matrices[
|
|
20
|
-
matrices[
|
|
21
|
-
matrices[
|
|
22
|
-
matrices[
|
|
23
|
-
matrices[
|
|
24
|
-
matrices[
|
|
25
|
-
matrices[
|
|
26
|
-
matrices[
|
|
27
|
-
matrices[
|
|
28
|
-
matrices[
|
|
29
|
-
matrices[
|
|
30
|
-
matrices[
|
|
31
|
-
matrices[
|
|
32
|
-
matrices[
|
|
11
|
+
function placeAt(matrices, offset, transform) {
|
|
12
|
+
const scale = transform.s,
|
|
13
|
+
scaledCosPitch = Math.cos(transform.rx) * scale,
|
|
14
|
+
sinPitch = Math.sin(transform.rx),
|
|
15
|
+
cosYaw = Math.cos(transform.ry),
|
|
16
|
+
sinYaw = Math.sin(transform.ry);
|
|
17
|
+
matrices[offset] = cosYaw * scale;
|
|
18
|
+
matrices[offset + 1] = 0;
|
|
19
|
+
matrices[offset + 2] = -sinYaw * scale;
|
|
20
|
+
matrices[offset + 3] = 0;
|
|
21
|
+
matrices[offset + 4] = sinYaw * sinPitch * scale;
|
|
22
|
+
matrices[offset + 5] = scaledCosPitch;
|
|
23
|
+
matrices[offset + 6] = cosYaw * sinPitch * scale;
|
|
24
|
+
matrices[offset + 7] = 0;
|
|
25
|
+
matrices[offset + 8] = scaledCosPitch * sinYaw;
|
|
26
|
+
matrices[offset + 9] = -sinPitch * scale;
|
|
27
|
+
matrices[offset + 10] = scaledCosPitch * cosYaw;
|
|
28
|
+
matrices[offset + 11] = 0;
|
|
29
|
+
matrices[offset + 12] = transform.x;
|
|
30
|
+
matrices[offset + 13] = transform.y;
|
|
31
|
+
matrices[offset + 14] = transform.z;
|
|
32
|
+
matrices[offset + 15] = 1;
|
|
33
33
|
}
|
|
34
34
|
// Turned, tipped and rolled (rz): a bird banking into its turn.
|
|
35
|
-
function placeBankedAt(matrices,
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
matrices[
|
|
48
|
-
matrices[
|
|
49
|
-
matrices[
|
|
50
|
-
matrices[
|
|
51
|
-
matrices[
|
|
52
|
-
matrices[
|
|
53
|
-
matrices[
|
|
54
|
-
matrices[
|
|
55
|
-
matrices[
|
|
56
|
-
matrices[
|
|
57
|
-
matrices[
|
|
58
|
-
matrices[
|
|
59
|
-
matrices[
|
|
60
|
-
matrices[
|
|
61
|
-
matrices[
|
|
62
|
-
matrices[
|
|
35
|
+
function placeBankedAt(matrices, offset, transform) {
|
|
36
|
+
const scale = transform.s,
|
|
37
|
+
cosPitch = Math.cos(transform.rx),
|
|
38
|
+
sinPitch = Math.sin(transform.rx),
|
|
39
|
+
cosYaw = Math.cos(transform.ry),
|
|
40
|
+
sinYaw = Math.sin(transform.ry),
|
|
41
|
+
cosRoll = Math.cos(transform.rz),
|
|
42
|
+
sinRoll = Math.sin(transform.rz),
|
|
43
|
+
yawRollCosines = cosYaw * cosRoll,
|
|
44
|
+
cosYawSinRoll = cosYaw * sinRoll,
|
|
45
|
+
sinYawCosRoll = sinYaw * cosRoll,
|
|
46
|
+
yawRollSines = sinYaw * sinRoll;
|
|
47
|
+
matrices[offset] = (yawRollCosines + yawRollSines * sinPitch) * scale;
|
|
48
|
+
matrices[offset + 1] = cosPitch * sinRoll * scale;
|
|
49
|
+
matrices[offset + 2] = (cosYawSinRoll * sinPitch - sinYawCosRoll) * scale;
|
|
50
|
+
matrices[offset + 3] = 0;
|
|
51
|
+
matrices[offset + 4] = (sinYawCosRoll * sinPitch - cosYawSinRoll) * scale;
|
|
52
|
+
matrices[offset + 5] = cosPitch * cosRoll * scale;
|
|
53
|
+
matrices[offset + 6] = (yawRollSines + yawRollCosines * sinPitch) * scale;
|
|
54
|
+
matrices[offset + 7] = 0;
|
|
55
|
+
matrices[offset + 8] = cosPitch * sinYaw * scale;
|
|
56
|
+
matrices[offset + 9] = -sinPitch * scale;
|
|
57
|
+
matrices[offset + 10] = cosPitch * cosYaw * scale;
|
|
58
|
+
matrices[offset + 11] = 0;
|
|
59
|
+
matrices[offset + 12] = transform.x;
|
|
60
|
+
matrices[offset + 13] = transform.y;
|
|
61
|
+
matrices[offset + 14] = transform.z;
|
|
62
|
+
matrices[offset + 15] = 1;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export { placeAt, placeBankedAt, placement };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { cellKey } from '../math/cell-key.js';
|
|
2
|
+
|
|
3
|
+
// Index points of an append-only list. Filed x, z and radius are immutable.
|
|
4
|
+
export class PointBuckets {
|
|
5
|
+
// `list`: the things, an array that only grows; `cell`: metres to a side of a cell; `radius(item)`: how far the thing reaches
|
|
6
|
+
// beyond its point, if it has a size (the largest is kept, so a test can widen its reach by it).
|
|
7
|
+
constructor(list, cell, radius = () => 0) {
|
|
8
|
+
if (!(Number.isFinite(cell) && cell > 0)) throw new RangeError('Cell size must be positive and finite');
|
|
9
|
+
this.list = list;
|
|
10
|
+
this.cell = cell;
|
|
11
|
+
this.radius = radius;
|
|
12
|
+
this.widest = 0; // the largest radius filed
|
|
13
|
+
this.filed = 0; // how many of the list are filed
|
|
14
|
+
this.cells = new Map();
|
|
15
|
+
}
|
|
16
|
+
// Files what has been pushed onto the list since the last test.
|
|
17
|
+
catchUp() {
|
|
18
|
+
const { list, cell, cells } = this;
|
|
19
|
+
for (; this.filed < list.length; this.filed++) {
|
|
20
|
+
const item = list[this.filed],
|
|
21
|
+
key = cellKey(Math.floor(item.x / cell), Math.floor(item.z / cell));
|
|
22
|
+
this.widest = Math.max(this.widest, this.radius(item));
|
|
23
|
+
const held = cells.get(key);
|
|
24
|
+
if (held) held.push(item);
|
|
25
|
+
else cells.set(key, [item]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// Visit candidate cells in column, row and insertion order. The predicate performs
|
|
29
|
+
// the exact distance or footprint test. widen extends reach by the largest filed radius.
|
|
30
|
+
find(x, z, reach, test, widen = false) {
|
|
31
|
+
this.catchUp();
|
|
32
|
+
const { cell, cells } = this,
|
|
33
|
+
searchRadius = reach + (widen ? this.widest : 0),
|
|
34
|
+
i1 = Math.floor((x + searchRadius) / cell),
|
|
35
|
+
j0 = Math.floor((z - searchRadius) / cell),
|
|
36
|
+
j1 = Math.floor((z + searchRadius) / cell),
|
|
37
|
+
i0 = Math.floor((x - searchRadius) / cell);
|
|
38
|
+
if (!Number.isSafeInteger(i0) || !Number.isSafeInteger(i1) || !Number.isSafeInteger(j0) || !Number.isSafeInteger(j1))
|
|
39
|
+
throw new RangeError('Query exceeds the supported grid coordinates');
|
|
40
|
+
for (let column = i0; column <= i1; column++)
|
|
41
|
+
for (let row = j0; row <= j1; row++) {
|
|
42
|
+
const held = cells.get(cellKey(column, row));
|
|
43
|
+
if (held) for (const item of held) if (test(item)) return item;
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
some(x, z, reach, test, widen = false) {
|
|
48
|
+
return this.find(x, z, reach, test, widen) !== undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/geometry/primitives.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit meshes, transforms and colour helpers for assembling models with a geometry builder.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import * as THREE from 'three';
|
|
2
7
|
import { TAU } from '../math/scalar.js';
|
|
3
8
|
import { createNoise } from '../math/noise.js';
|
|
@@ -7,58 +12,65 @@ import { createNoise } from '../math/noise.js';
|
|
|
7
12
|
// between two points (stick)
|
|
8
13
|
// ════════════════════════════════════════════════════════════════════════
|
|
9
14
|
const _m4 = new THREE.Matrix4(),
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
rotationScratch = new THREE.Quaternion(),
|
|
16
|
+
eulerScratch = new THREE.Euler(),
|
|
17
|
+
scaleScratch = new THREE.Vector3(),
|
|
18
|
+
positionScratch = new THREE.Vector3(),
|
|
19
|
+
colorScratch = new THREE.Color();
|
|
15
20
|
function TRS(x, y, z, sx = 1, sy = 1, sz = 1, ry = 0, rx = 0, rz = 0) {
|
|
16
21
|
// returns a shared matrix: use it immediately
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return _m4.compose(
|
|
22
|
+
eulerScratch.set(rx, ry, rz, 'YXZ');
|
|
23
|
+
rotationScratch.setFromEuler(eulerScratch);
|
|
24
|
+
return _m4.compose(positionScratch.set(x, y, z), rotationScratch, scaleScratch.set(sx, sy, sz));
|
|
20
25
|
}
|
|
21
26
|
// A unit triangular prism, the shape of a gable roof.
|
|
22
27
|
function prismGeometry() {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
const backLeft = [-0.5, 0, -0.5],
|
|
29
|
+
backRight = [0.5, 0, -0.5],
|
|
30
|
+
frontRight = [0.5, 0, 0.5],
|
|
31
|
+
frontLeft = [-0.5, 0, 0.5],
|
|
32
|
+
backRidge = [0, 1, -0.5],
|
|
33
|
+
frontRidge = [0, 1, 0.5];
|
|
34
|
+
const geometry = new THREE.BufferGeometry();
|
|
35
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute([
|
|
36
|
+
backLeft, frontLeft, frontRidge,
|
|
37
|
+
backLeft, frontRidge, backRidge,
|
|
38
|
+
backRight, backRidge, frontRidge,
|
|
39
|
+
backRight, frontRidge, frontRight,
|
|
40
|
+
frontLeft, frontRight, frontRidge,
|
|
41
|
+
backRight, backLeft, backRidge,
|
|
42
|
+
].flat(), 3));
|
|
43
|
+
geometry.computeVertexNormals();
|
|
44
|
+
return geometry;
|
|
33
45
|
}
|
|
34
46
|
// The two triangular ends of that prism alone: a gable's wall, under a roof drawn without them.
|
|
35
47
|
function gableGeometry() {
|
|
36
|
-
const
|
|
37
|
-
|
|
48
|
+
const geometry = new THREE.BufferGeometry();
|
|
49
|
+
geometry.setAttribute(
|
|
38
50
|
'position',
|
|
39
51
|
new THREE.Float32BufferAttribute([-0.5, 0, 0.5, 0.5, 0, 0.5, 0, 1, 0.5, 0.5, 0, -0.5, -0.5, 0, -0.5, 0, 1, -0.5], 3),
|
|
40
52
|
);
|
|
41
|
-
|
|
42
|
-
return
|
|
53
|
+
geometry.computeVertexNormals();
|
|
54
|
+
return geometry;
|
|
43
55
|
}
|
|
44
56
|
// The ways a face can look, as [axis, sign]
|
|
45
57
|
const WAYS = { '+x': [0, 1], '-x': [0, -1], '+y': [1, 1], '-y': [1, -1], '+z': [2, 1], '-z': [2, -1] };
|
|
46
58
|
// A unit shape without its flat faces that look the ways named ('+x', '-y', ...), the rest in their order: for a part whose faces
|
|
47
59
|
// lie against something, where no one can see them (the back of a box fixed to a wall, the foot of a rod set in one)
|
|
48
60
|
function without(shape, ...ways) {
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
n =
|
|
61
|
+
const sourceGeometry = shape.index ? shape.toNonIndexed() : shape,
|
|
62
|
+
positions = sourceGeometry.attributes.position.array,
|
|
63
|
+
n = sourceGeometry.attributes.normal.array,
|
|
52
64
|
away = t => ways.some(w => [0, 3, 6].every(k => n[t + k + WAYS[w][0]] * WAYS[w][1] > 0.99)),
|
|
53
65
|
kept = [];
|
|
54
|
-
for (let t = 0; t <
|
|
55
|
-
const pick =
|
|
66
|
+
for (let t = 0; t < positions.length; t += 9) if (!away(t)) kept.push(t);
|
|
67
|
+
const pick = attribute => {
|
|
56
68
|
const out = new Float32Array(kept.length * 9);
|
|
57
|
-
kept.forEach((t, i) => out.set(
|
|
69
|
+
kept.forEach((t, i) => out.set(attribute.subarray(t, t + 9), i * 9));
|
|
58
70
|
return new THREE.BufferAttribute(out, 3);
|
|
59
71
|
},
|
|
60
72
|
q = new THREE.BufferGeometry();
|
|
61
|
-
q.setAttribute('position', pick(
|
|
73
|
+
q.setAttribute('position', pick(positions));
|
|
62
74
|
q.setAttribute('normal', pick(n));
|
|
63
75
|
return q;
|
|
64
76
|
}
|
|
@@ -72,9 +84,9 @@ const _dir = new THREE.Vector3(),
|
|
|
72
84
|
_a = new THREE.Vector3(),
|
|
73
85
|
_b = new THREE.Vector3();
|
|
74
86
|
function lump(fineness, shape) {
|
|
75
|
-
const
|
|
76
|
-
position =
|
|
77
|
-
normal =
|
|
87
|
+
const geometry = new THREE.IcosahedronGeometry(1, fineness),
|
|
88
|
+
position = geometry.attributes.position.array,
|
|
89
|
+
normal = geometry.attributes.normal.array,
|
|
78
90
|
near = 0.03; // how far round the sphere the shape is sampled to find which way it faces
|
|
79
91
|
for (let i = 0; i < position.length; i += 3) {
|
|
80
92
|
_dir.set(position[i], position[i + 1], position[i + 2]).normalize();
|
|
@@ -90,7 +102,7 @@ function lump(fineness, shape) {
|
|
|
90
102
|
position.set([_here.x, _here.y, _here.z], i);
|
|
91
103
|
normal.set([_a.x, _a.y, _a.z], i);
|
|
92
104
|
}
|
|
93
|
-
return
|
|
105
|
+
return geometry;
|
|
94
106
|
}
|
|
95
107
|
// A stone broken from the rock, about a metre across and a little lower than it is wide: worn round by the weather, with three
|
|
96
108
|
// faces where it split along its joints, and flat where it lies; shaded round where it is worn and flat on its broken faces
|
|
@@ -99,23 +111,27 @@ const STONE_JOINTS = [
|
|
|
99
111
|
[0.62, 0.5, 0.6, 0.37],
|
|
100
112
|
[-0.74, 0.18, 0.64, 0.39],
|
|
101
113
|
[0.12, 0.3, -0.95, 0.4],
|
|
102
|
-
].map(([x, y, z,
|
|
114
|
+
].map(([x, y, z, planeDistance]) => [x / Math.hypot(x, y, z), y / Math.hypot(x, y, z), z / Math.hypot(x, y, z), planeDistance]); // [the face's normal, its distance from the middle]
|
|
103
115
|
const box = new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0),
|
|
104
116
|
cyl6 = new THREE.CylinderGeometry(0.5, 0.5, 1, 6).translate(0, 0.5, 0);
|
|
117
|
+
/**
|
|
118
|
+
* Build a library of reusable unit geometries for scenery, roofs and figures.
|
|
119
|
+
* @param fbm - Fractal noise sampler used to shape the rock. Pass the world’s seeded sampler to reproduce it.
|
|
120
|
+
*/
|
|
105
121
|
export function createUnitShapes(fbm = createNoise().fbm) {
|
|
106
122
|
function stonePoint(dir, out) {
|
|
107
123
|
let r = 0.5 * (0.8 + 0.4 * fbm(dir.x * 1.7 + 3, dir.z * 1.7 + dir.y * 1.3, 2, 190));
|
|
108
|
-
for (const [nx, ny, nz,
|
|
124
|
+
for (const [nx, ny, nz, planeDistance] of STONE_JOINTS) {
|
|
109
125
|
const towards = dir.x * nx + dir.y * ny + dir.z * nz;
|
|
110
|
-
if (towards > 0.05) r = Math.min(r,
|
|
126
|
+
if (towards > 0.05) r = Math.min(r, planeDistance / towards);
|
|
111
127
|
}
|
|
112
128
|
return out.set(dir.x * r, Math.max(dir.y * r * 0.82, -0.3), dir.z * r);
|
|
113
129
|
}
|
|
114
130
|
function brokenStone() {
|
|
115
|
-
const
|
|
131
|
+
const geometry = lump(1, stonePoint);
|
|
116
132
|
// white vertex colours: an instanced rock on a material that reads vertex colours would otherwise be drawn black
|
|
117
|
-
|
|
118
|
-
return
|
|
133
|
+
geometry.setAttribute('color', new THREE.Float32BufferAttribute(new Float32Array(geometry.attributes.position.count * 3).fill(1), 3));
|
|
134
|
+
return geometry;
|
|
119
135
|
}
|
|
120
136
|
return {
|
|
121
137
|
box,
|
|
@@ -145,14 +161,14 @@ export function createUnitShapes(fbm = createNoise().fbm) {
|
|
|
145
161
|
dome16: new THREE.SphereGeometry(0.5, 20, 8, 0, TAU, 0, Math.PI / 2),
|
|
146
162
|
};
|
|
147
163
|
}
|
|
148
|
-
function C(hex, jitter = 0,
|
|
164
|
+
function C(hex, jitter = 0, random) {
|
|
149
165
|
// sRGB hex -> linear [r,g,b], optionally brightness-jittered
|
|
150
|
-
|
|
151
|
-
const k = jitter &&
|
|
152
|
-
return [
|
|
166
|
+
colorScratch.setHex(hex);
|
|
167
|
+
const k = jitter && random ? 1 + (random() - 0.5) * 2 * jitter : 1;
|
|
168
|
+
return [colorScratch.r * k, colorScratch.g * k, colorScratch.b * k];
|
|
153
169
|
}
|
|
154
170
|
|
|
155
|
-
//
|
|
171
|
+
// Append a rod of the given radius between the start and end points, both [x, y, z].
|
|
156
172
|
const _rod = {
|
|
157
173
|
m: new THREE.Matrix4(),
|
|
158
174
|
at: new THREE.Vector3(),
|
|
@@ -161,12 +177,12 @@ const _rod = {
|
|
|
161
177
|
way: new THREE.Vector3(),
|
|
162
178
|
},
|
|
163
179
|
UP = new THREE.Vector3(0, 1, 0);
|
|
164
|
-
function stick(
|
|
180
|
+
function stick(builder, start, end, radius, color, geometry = cyl6) {
|
|
165
181
|
const { m, at, turn, size, way } = _rod;
|
|
166
|
-
way.set(
|
|
182
|
+
way.set(end[0] - start[0], end[1] - start[1], end[2] - start[2]);
|
|
167
183
|
const len = way.length();
|
|
168
184
|
turn.setFromUnitVectors(UP, way.divideScalar(len));
|
|
169
|
-
|
|
185
|
+
builder.add(geometry, color, m.compose(at.set(start[0], start[1], start[2]), turn, size.set(radius, len, radius)));
|
|
170
186
|
}
|
|
171
187
|
|
|
172
188
|
export { box, C, TRS, lump, stick, without };
|
package/geometry/sagging-tube.js
CHANGED
|
@@ -13,12 +13,12 @@ export function createSaggingTube({ spanSegments, radialSegments, radius, slack
|
|
|
13
13
|
if (![radius, slack, extraLength].every(Number.isFinite) || radius <= 0 || slack < 1 || extraLength < 0) {
|
|
14
14
|
throw new RangeError('Expected a positive radius, slack of at least one and nonnegative extra length');
|
|
15
15
|
}
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const pointScratch = new THREE.Vector3(),
|
|
17
|
+
tangentScratch = new THREE.Vector3(),
|
|
18
|
+
normalScratch = new THREE.Vector3(),
|
|
19
|
+
binormalScratch = new THREE.Vector3(),
|
|
20
20
|
UP = new THREE.Vector3(0, 1, 0);
|
|
21
|
-
return function update(anchors, count,
|
|
21
|
+
return function update(anchors, count, positions, normals) {
|
|
22
22
|
let ring = 0;
|
|
23
23
|
for (let s = 0; s < count - 1; s++) {
|
|
24
24
|
const from = anchors[s],
|
|
@@ -28,29 +28,29 @@ export function createSaggingTube({ spanSegments, radialSegments, radius, slack
|
|
|
28
28
|
sag = Math.sqrt((3 * span * Math.max(0, length - span)) / 8); // a parabola of that length over that span
|
|
29
29
|
for (let j = s ? 1 : 0; j <= spanSegments; j++, ring++) {
|
|
30
30
|
const u = j / spanSegments;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
pointScratch.lerpVectors(from, to, u);
|
|
32
|
+
pointScratch.y -= sag * 4 * u * (1 - u);
|
|
33
|
+
tangentScratch.subVectors(to, from);
|
|
34
|
+
tangentScratch.y -= sag * 4 * (1 - 2 * u);
|
|
35
|
+
tangentScratch.normalize();
|
|
36
|
+
normalScratch.crossVectors(tangentScratch, UP);
|
|
37
|
+
if (normalScratch.lengthSq() < 1e-6) normalScratch.set(1, 0, 0);
|
|
38
|
+
normalScratch.normalize();
|
|
39
|
+
binormalScratch.crossVectors(normalScratch, tangentScratch);
|
|
40
40
|
for (let k = 0; k < radialSegments; k++) {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
nx =
|
|
46
|
-
ny =
|
|
47
|
-
nz =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
const ringAngle = (k / radialSegments) * Math.PI * 2,
|
|
42
|
+
cosAngle = Math.cos(ringAngle),
|
|
43
|
+
sinAngle = Math.sin(ringAngle),
|
|
44
|
+
positionOffset = (ring * radialSegments + k) * 3,
|
|
45
|
+
nx = normalScratch.x * cosAngle + binormalScratch.x * sinAngle,
|
|
46
|
+
ny = normalScratch.y * cosAngle + binormalScratch.y * sinAngle,
|
|
47
|
+
nz = normalScratch.z * cosAngle + binormalScratch.z * sinAngle;
|
|
48
|
+
normals[positionOffset] = nx;
|
|
49
|
+
normals[positionOffset + 1] = ny;
|
|
50
|
+
normals[positionOffset + 2] = nz;
|
|
51
|
+
positions[positionOffset] = pointScratch.x + nx * radius;
|
|
52
|
+
positions[positionOffset + 1] = pointScratch.y + ny * radius;
|
|
53
|
+
positions[positionOffset + 2] = pointScratch.z + nz * radius;
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|