roamwright 0.2.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 +19 -21
- 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 +47 -0
- 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 +60 -0
- package/geometry/instance-visibility.js +41 -0
- package/geometry/instances.js +35 -12
- package/geometry/outline-grid.js +7 -7
- package/geometry/part-ranges.js +18 -0
- 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 +101 -92
- 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 +29 -0
- 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 +8 -4
- package/rendering/caster-motion.js +5 -5
- package/rendering/cell-lod.js +147 -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 +91 -26
- 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-budget.js +49 -0
- package/runtime/frame-pacing.js +33 -0
- package/runtime/gpu-timing.js +13 -10
- 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
|
@@ -1,100 +1,132 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Tessellate a parametric sheet into triangles with smooth normals for a geometry builder.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
4
6
|
const buffers = [];
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Append two triangles per grid cell. On an X/Z sheet where u increases along +X
|
|
9
|
+
* and v along +Z, the front faces point upward. A second side duplicates the
|
|
10
|
+
* triangles with reversed winding and normals.
|
|
11
|
+
* @param {Pick<import('./builder.js').GB, 'raw'>} builder - Destination for the generated vertices.
|
|
12
|
+
* @param {number} columns - Positive integer cell count along u.
|
|
13
|
+
* @param {number} rows - Positive integer cell count along v.
|
|
14
|
+
* @param {(u: number, v: number) => ArrayLike<number>} pointAt - Map coordinates in [0, 1] to [x, y, z].
|
|
15
|
+
* @param {ArrayLike<number> | ((u: number, v: number) => ArrayLike<number>)} surfaceColor -
|
|
16
|
+
* Linear RGB colour or a function sampled at cell centres, or at vertices when blending.
|
|
17
|
+
* @param {{blend?: boolean, flip?: boolean, back?: boolean | ArrayLike<number>}} [options] -
|
|
18
|
+
* Blend vertex colours, flip the front winding, or add a back side. Set back to true
|
|
19
|
+
* to reuse front colours, or supply a separate linear RGB colour.
|
|
20
|
+
* @example
|
|
21
|
+
* ```js
|
|
22
|
+
* import { GeometryBuilder } from 'roamwright';
|
|
23
|
+
* import { sampleSurface } from 'roamwright/geometry/sampled-surface.js';
|
|
24
|
+
*
|
|
25
|
+
* const ground = new GeometryBuilder();
|
|
26
|
+
* sampleSurface(ground, 8, 8, (u, v) => [16 * u, 2 * Math.sin(u * Math.PI), 16 * v], [0.3, 0.5, 0.2]);
|
|
27
|
+
* const parts = ground.parts(); // 128 triangles
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function sampleSurface(builder, columns, rows, pointAt, surfaceColor, options = {}) {
|
|
7
31
|
const scratch = buffers.pop() ?? { positions: [], normals: [], colors: [] };
|
|
8
32
|
try {
|
|
9
|
-
writeSurface(
|
|
33
|
+
writeSurface(builder, columns, rows, pointAt, surfaceColor, options, scratch);
|
|
10
34
|
} finally {
|
|
11
35
|
buffers.push(scratch);
|
|
12
36
|
}
|
|
13
37
|
}
|
|
14
38
|
|
|
15
|
-
function writeSurface(
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
back =
|
|
21
|
-
|
|
22
|
-
blend =
|
|
23
|
-
|
|
24
|
-
for (let
|
|
25
|
-
for (let
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
function writeSurface(builder, columns, rows, pointAt, surfaceColor, options, scratch) {
|
|
40
|
+
const rowStride = columns + 1,
|
|
41
|
+
positions = scratch.positions,
|
|
42
|
+
normals = scratch.normals,
|
|
43
|
+
hasColorSampler = typeof surfaceColor === 'function',
|
|
44
|
+
back = options.back,
|
|
45
|
+
normalSign = options.flip ? -1 : 1,
|
|
46
|
+
blend = hasColorSampler && options.blend,
|
|
47
|
+
colors = scratch.colors;
|
|
48
|
+
for (let row = 0; row <= rows; row++)
|
|
49
|
+
for (let column = 0; column <= columns; column++) {
|
|
50
|
+
const point = pointAt(column / columns, row / rows),
|
|
51
|
+
componentOffset = (row * rowStride + column) * 3;
|
|
52
|
+
positions[componentOffset] = point[0];
|
|
53
|
+
positions[componentOffset + 1] = point[1];
|
|
54
|
+
positions[componentOffset + 2] = point[2];
|
|
31
55
|
if (blend) {
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
56
|
+
const vertexColor = surfaceColor(column / columns, row / rows);
|
|
57
|
+
colors[componentOffset] = vertexColor[0];
|
|
58
|
+
colors[componentOffset + 1] = vertexColor[1];
|
|
59
|
+
colors[componentOffset + 2] = vertexColor[2];
|
|
36
60
|
}
|
|
37
61
|
}
|
|
38
|
-
for (let
|
|
39
|
-
for (let
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const tx =
|
|
46
|
-
ty =
|
|
47
|
-
tz =
|
|
48
|
-
sx =
|
|
49
|
-
sy =
|
|
50
|
-
sz =
|
|
62
|
+
for (let row = 0; row <= rows; row++)
|
|
63
|
+
for (let column = 0; column <= columns; column++) {
|
|
64
|
+
const leftOffset = (row * rowStride + Math.max(0, column - 1)) * 3,
|
|
65
|
+
rightOffset = (row * rowStride + Math.min(columns, column + 1)) * 3,
|
|
66
|
+
previousRowOffset = (Math.max(0, row - 1) * rowStride + column) * 3,
|
|
67
|
+
nextRowOffset = (Math.min(rows, row + 1) * rowStride + column) * 3,
|
|
68
|
+
componentOffset = (row * rowStride + column) * 3;
|
|
69
|
+
const tx = positions[rightOffset] - positions[leftOffset],
|
|
70
|
+
ty = positions[rightOffset + 1] - positions[leftOffset + 1],
|
|
71
|
+
tz = positions[rightOffset + 2] - positions[leftOffset + 2],
|
|
72
|
+
sx = positions[nextRowOffset] - positions[previousRowOffset],
|
|
73
|
+
sy = positions[nextRowOffset + 1] - positions[previousRowOffset + 1],
|
|
74
|
+
sz = positions[nextRowOffset + 2] - positions[previousRowOffset + 2];
|
|
51
75
|
const nx = sy * tz - sz * ty,
|
|
52
76
|
ny = sz * tx - sx * tz,
|
|
53
77
|
nz = sx * ty - sy * tx,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
78
|
+
normalDivisor = (Math.hypot(nx, ny, nz) || 1) * normalSign;
|
|
79
|
+
normals[componentOffset] = nx / normalDivisor;
|
|
80
|
+
normals[componentOffset + 1] = ny / normalDivisor;
|
|
81
|
+
normals[componentOffset + 2] = nz / normalDivisor;
|
|
58
82
|
}
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
if (
|
|
62
|
-
|
|
83
|
+
const writeVertex = (column, row, sideSign, frontColor) => {
|
|
84
|
+
const componentOffset = (row * rowStride + column) * 3;
|
|
85
|
+
if (frontColor) builder.raw(
|
|
86
|
+
positions[componentOffset], positions[componentOffset + 1], positions[componentOffset + 2],
|
|
87
|
+
normals[componentOffset] * sideSign, normals[componentOffset + 1] * sideSign, normals[componentOffset + 2] * sideSign,
|
|
88
|
+
frontColor[0], frontColor[1], frontColor[2],
|
|
89
|
+
);
|
|
90
|
+
else builder.raw(
|
|
91
|
+
positions[componentOffset], positions[componentOffset + 1], positions[componentOffset + 2],
|
|
92
|
+
normals[componentOffset] * sideSign, normals[componentOffset + 1] * sideSign, normals[componentOffset + 2] * sideSign,
|
|
93
|
+
colors[componentOffset], colors[componentOffset + 1], colors[componentOffset + 2],
|
|
94
|
+
);
|
|
63
95
|
};
|
|
64
|
-
for (let
|
|
65
|
-
for (let
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
96
|
+
for (let row = 0; row < rows; row++)
|
|
97
|
+
for (let column = 0; column < columns; column++) {
|
|
98
|
+
const frontColor = blend ? null : hasColorSampler ? surfaceColor((column + 0.5) / columns, (row + 0.5) / rows) : surfaceColor,
|
|
99
|
+
backColor = back === true ? frontColor : back;
|
|
100
|
+
if (normalSign > 0) {
|
|
101
|
+
writeVertex(column, row, 1, frontColor);
|
|
102
|
+
writeVertex(column, row + 1, 1, frontColor);
|
|
103
|
+
writeVertex(column + 1, row + 1, 1, frontColor);
|
|
104
|
+
writeVertex(column, row, 1, frontColor);
|
|
105
|
+
writeVertex(column + 1, row + 1, 1, frontColor);
|
|
106
|
+
writeVertex(column + 1, row, 1, frontColor);
|
|
75
107
|
} else {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
108
|
+
writeVertex(column, row, 1, frontColor);
|
|
109
|
+
writeVertex(column + 1, row + 1, 1, frontColor);
|
|
110
|
+
writeVertex(column, row + 1, 1, frontColor);
|
|
111
|
+
writeVertex(column, row, 1, frontColor);
|
|
112
|
+
writeVertex(column + 1, row, 1, frontColor);
|
|
113
|
+
writeVertex(column + 1, row + 1, 1, frontColor);
|
|
82
114
|
}
|
|
83
115
|
if (back) {
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
116
|
+
if (normalSign > 0) {
|
|
117
|
+
writeVertex(column, row, -1, backColor);
|
|
118
|
+
writeVertex(column + 1, row + 1, -1, backColor);
|
|
119
|
+
writeVertex(column, row + 1, -1, backColor);
|
|
120
|
+
writeVertex(column, row, -1, backColor);
|
|
121
|
+
writeVertex(column + 1, row, -1, backColor);
|
|
122
|
+
writeVertex(column + 1, row + 1, -1, backColor);
|
|
91
123
|
} else {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
124
|
+
writeVertex(column, row, -1, backColor);
|
|
125
|
+
writeVertex(column, row + 1, -1, backColor);
|
|
126
|
+
writeVertex(column + 1, row + 1, -1, backColor);
|
|
127
|
+
writeVertex(column, row, -1, backColor);
|
|
128
|
+
writeVertex(column + 1, row + 1, -1, backColor);
|
|
129
|
+
writeVertex(column + 1, row, -1, backColor);
|
|
98
130
|
}
|
|
99
131
|
}
|
|
100
132
|
}
|
package/geometry/scatter-grid.js
CHANGED
|
@@ -1,4 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Place repeatable patches of scenery around a moving observer.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sample one candidate per world cell around the observer. Each level scans a square.
|
|
8
|
+
* The fill callback decides which candidates to keep. A rejected candidate leaves its
|
|
9
|
+
* output index available to the next one. Ring bounds are available to `finish` for
|
|
10
|
+
* fading or clipping overlap between levels.
|
|
11
|
+
* @param {object} options - Sampling levels and callbacks.
|
|
12
|
+
* @param {{cell: number, half: number, refill: number}[]} options.rings - Levels from fine to coarse.
|
|
13
|
+
* Cell width and refill distance are in world units. At full density each side spans `2 * half` cells.
|
|
14
|
+
* @param {(col: number, row: number, seed: number) => number} options.hash - Repeatable offset in [0, 1).
|
|
15
|
+
* @param {[number, number]} options.seeds - Separate hash seeds for the X and Z offsets.
|
|
16
|
+
* @param {(level: number, index: number, x: number, z: number, h1: number, h2: number, col: number, row: number, cell: number) => boolean} options.fill -
|
|
17
|
+
* Write the candidate at the supplied index and return true to count it.
|
|
18
|
+
* @param {(level: number) => void} [options.start] - Called before a level refills so its old output can be cleared.
|
|
19
|
+
* @param {(grids: {cell: number, count: number, inner: number, outer: number}[]) => void} [options.finish] -
|
|
20
|
+
* Called once after any levels refill. Counts and bounds describe the current levels and are reused on later updates.
|
|
21
|
+
* @returns {(x: number, z: number, density?: number) => boolean} Update using world X/Z. Density scales each
|
|
22
|
+
* level’s half-width from zero to full size. Returns true when at least one level refills.
|
|
23
|
+
* @example
|
|
24
|
+
* ```js
|
|
25
|
+
* import { createHash } from 'roamwright/math/random.js';
|
|
26
|
+
* import { createScatterGrid } from 'roamwright/geometry/scatter-grid.js';
|
|
27
|
+
*
|
|
28
|
+
* const trees = [];
|
|
29
|
+
* const update = createScatterGrid({
|
|
30
|
+
* rings: [{ cell: 8, half: 2, refill: 4 }],
|
|
31
|
+
* hash: createHash(42), seeds: [1, 2],
|
|
32
|
+
* start() { trees.length = 0; },
|
|
33
|
+
* fill(level, index, x, z) {
|
|
34
|
+
* trees[index] = [x, z];
|
|
35
|
+
* return true;
|
|
36
|
+
* },
|
|
37
|
+
* });
|
|
38
|
+
* update(0, 0); // 16 tree positions
|
|
39
|
+
* update(1, 0); // false, still inside the refill distance
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
2
42
|
export function createScatterGrid({ rings, hash, seeds, fill, start, finish }) {
|
|
3
43
|
const grids = rings.map(ring => ({ ...ring, x: Infinity, z: Infinity, halfUsed: -1, count: 0, inner: 0, outer: 0 }));
|
|
4
44
|
const update = (x, z, density = 1) => {
|
|
@@ -11,16 +51,16 @@ export function createScatterGrid({ rings, hash, seeds, fill, start, finish }) {
|
|
|
11
51
|
grid.outer = Math.max(inner, half * grid.cell - grid.refill);
|
|
12
52
|
inner = grid.outer;
|
|
13
53
|
if (grid.halfUsed === half && Math.abs(x - grid.x) + Math.abs(z - grid.z) < grid.refill) continue;
|
|
14
|
-
const
|
|
54
|
+
const centreColumn = Math.floor(x / grid.cell), centreRow = Math.floor(z / grid.cell);
|
|
15
55
|
grid.x = x;
|
|
16
56
|
grid.z = z;
|
|
17
57
|
grid.halfUsed = half;
|
|
18
58
|
grid.count = 0;
|
|
19
59
|
start?.(level);
|
|
20
|
-
for (let row =
|
|
21
|
-
for (let col =
|
|
22
|
-
const
|
|
23
|
-
if (fill(level, grid.count, (col +
|
|
60
|
+
for (let row = centreRow - half; row < centreRow + half; row++) {
|
|
61
|
+
for (let col = centreColumn - half; col < centreColumn + half; col++) {
|
|
62
|
+
const offsetX = hash(col, row, seeds[0]), offsetZ = hash(col, row, seeds[1]);
|
|
63
|
+
if (fill(level, grid.count, (col + offsetX) * grid.cell, (row + offsetZ) * grid.cell, offsetX, offsetZ, col, row, grid.cell)) grid.count++;
|
|
24
64
|
}
|
|
25
65
|
}
|
|
26
66
|
changed = true;
|
package/geometry/sculpt.js
CHANGED
|
@@ -13,8 +13,8 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
13
13
|
// Level 1 halves detail. Level 2 is for the smallest screen-space silhouettes.
|
|
14
14
|
let LOD_LO = 0;
|
|
15
15
|
// rings: [y, rx, rz, cx, cz, col, bone1, bone2, weight1]; a ring marked `fine` is left out of the far and light versions.
|
|
16
|
-
//
|
|
17
|
-
// ring puts it (a hem that dips, a ring that slopes, a muscle's swell);
|
|
16
|
+
// options.fold(v, angle) scales the radius (pleats); options.lay(point, angle, ring) moves a ring's point [x, y, z] at that angle from where the level
|
|
17
|
+
// ring puts it (a hem that dips, a ring that slopes, a muscle's swell); options.tint(point, angle, ring) is how bright the ring's colour is
|
|
18
18
|
// there (a crease's shade, at most 1); o.sk(y, a, ring) gives a point's bones and weight in place of its ring's; o.a0/o.a1
|
|
19
19
|
// give an open sheet; o.joined puts each point in once and joins the triangles through them by number (a sixth of the points,
|
|
20
20
|
// for a builder whose geometry is drawn by its numbers: a person's body). o.relief(y, a, ring) is what stands out of the surface
|
|
@@ -24,52 +24,52 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
24
24
|
const RELIEF_STEP = [0.002, 0.02], // how far apart the relief is read to find its slope: metres of height, radians round
|
|
25
25
|
_lq = [],
|
|
26
26
|
_lm = [];
|
|
27
|
-
function loft(
|
|
27
|
+
function loft(builder, ox, oy, oz, rings, segments, options = {}) {
|
|
28
28
|
if (LOD_LO) {
|
|
29
|
-
|
|
29
|
+
segments = LOD_LO > 1 ? Math.max(5, Math.round(segments * 0.38)) : Math.max(5, (segments + 1) >> 1);
|
|
30
30
|
rings = rings.filter(r => !r.fine);
|
|
31
31
|
}
|
|
32
|
-
const
|
|
33
|
-
a0 =
|
|
34
|
-
a1 =
|
|
32
|
+
const ringCount = rings.length,
|
|
33
|
+
a0 = options.a0 ?? 0,
|
|
34
|
+
a1 = options.a1 ?? TAU,
|
|
35
35
|
closed = a1 - a0 > TAU - 1e-4,
|
|
36
|
-
ns = closed ?
|
|
36
|
+
ns = closed ? segments : segments + 1,
|
|
37
37
|
P_ = _lp,
|
|
38
38
|
N_ = _ln,
|
|
39
39
|
T_ = _lt,
|
|
40
|
-
{ fold, lay, tint } =
|
|
41
|
-
up = rings[
|
|
42
|
-
for (let i = 0; i <
|
|
40
|
+
{ fold, lay, tint } = options,
|
|
41
|
+
up = rings[ringCount - 1][0] > rings[0][0];
|
|
42
|
+
for (let i = 0; i < ringCount; i++) {
|
|
43
43
|
const r = rings[i];
|
|
44
44
|
for (let j = 0; j < ns; j++) {
|
|
45
|
-
const
|
|
46
|
-
k = fold ? fold(i / (
|
|
45
|
+
const angle = a0 + ((a1 - a0) * j) / segments,
|
|
46
|
+
k = fold ? fold(i / (ringCount - 1), angle) : 1,
|
|
47
47
|
q = (i * ns + j) * 3;
|
|
48
|
-
_pt[0] = r[3] + Math.cos(
|
|
48
|
+
_pt[0] = r[3] + Math.cos(angle) * r[1] * k;
|
|
49
49
|
_pt[1] = r[0];
|
|
50
|
-
_pt[2] = r[4] + Math.sin(
|
|
51
|
-
if (lay) lay(_pt,
|
|
52
|
-
if (tint) T_[i * ns + j] = tint(_pt,
|
|
50
|
+
_pt[2] = r[4] + Math.sin(angle) * r[2] * k;
|
|
51
|
+
if (lay) lay(_pt, angle, r);
|
|
52
|
+
if (tint) T_[i * ns + j] = tint(_pt, angle, r);
|
|
53
53
|
P_[q] = ox + _pt[0];
|
|
54
54
|
P_[q + 1] = oy + _pt[1];
|
|
55
55
|
P_[q + 2] = oz + _pt[2];
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
const
|
|
59
|
-
for (let i = 0; i <
|
|
58
|
+
const vertexOffset = (i, j) => (i * ns + (closed ? (j + ns) % ns : clamp(j, 0, ns - 1))) * 3;
|
|
59
|
+
for (let i = 0; i < ringCount; i++)
|
|
60
60
|
for (let j = 0; j < ns; j++) {
|
|
61
|
-
const
|
|
62
|
-
B_ =
|
|
63
|
-
C_ =
|
|
64
|
-
|
|
61
|
+
const previousSideOffset = vertexOffset(i, j - 1),
|
|
62
|
+
B_ = vertexOffset(i, j + 1),
|
|
63
|
+
C_ = vertexOffset(Math.max(0, i - 1), j),
|
|
64
|
+
nextRingOffset = vertexOffset(Math.min(ringCount - 1, i + 1), j),
|
|
65
65
|
q = (i * ns + j) * 3,
|
|
66
66
|
r = rings[i];
|
|
67
|
-
const tx = P_[B_] - P_[
|
|
68
|
-
ty = P_[B_ + 1] - P_[
|
|
69
|
-
tz = P_[B_ + 2] - P_[
|
|
70
|
-
ux = P_[
|
|
71
|
-
uy = P_[
|
|
72
|
-
uz = P_[
|
|
67
|
+
const tx = P_[B_] - P_[previousSideOffset],
|
|
68
|
+
ty = P_[B_ + 1] - P_[previousSideOffset + 1],
|
|
69
|
+
tz = P_[B_ + 2] - P_[previousSideOffset + 2],
|
|
70
|
+
ux = P_[nextRingOffset] - P_[C_],
|
|
71
|
+
uy = P_[nextRingOffset + 1] - P_[C_ + 1],
|
|
72
|
+
uz = P_[nextRingOffset + 2] - P_[C_ + 2];
|
|
73
73
|
const nx = ty * uz - tz * uy,
|
|
74
74
|
ny = tz * ux - tx * uz,
|
|
75
75
|
nz = tx * uy - ty * ux,
|
|
@@ -79,47 +79,47 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
79
79
|
N_[q + 1] = (ny / l) * s;
|
|
80
80
|
N_[q + 2] = (nz / l) * s;
|
|
81
81
|
}
|
|
82
|
-
if (
|
|
83
|
-
const tag =
|
|
84
|
-
sk =
|
|
82
|
+
if (options.relief) raise(rings, ns, closed, a0, (a1 - a0) / segments, vertexOffset, options.relief);
|
|
83
|
+
const tag = builder.tv,
|
|
84
|
+
sk = options.sk,
|
|
85
85
|
V = (i, j) => {
|
|
86
|
-
const q =
|
|
86
|
+
const q = vertexOffset(i, j),
|
|
87
87
|
r = rings[i],
|
|
88
|
-
|
|
88
|
+
ringColor = r[5],
|
|
89
89
|
t = tint ? T_[q / 3] : 1,
|
|
90
|
-
|
|
91
|
-
if (vertexTag)
|
|
92
|
-
const w = sk ? sk(r[0],
|
|
93
|
-
|
|
90
|
+
angle = a0 + ((a1 - a0) * j) / segments;
|
|
91
|
+
if (vertexTag) builder.tv = vertexTag(builder, r, angle, options, tag);
|
|
92
|
+
const w = sk ? sk(r[0], angle, r) : null;
|
|
93
|
+
builder.raw(
|
|
94
94
|
P_[q],
|
|
95
95
|
P_[q + 1],
|
|
96
96
|
P_[q + 2],
|
|
97
97
|
N_[q],
|
|
98
98
|
N_[q + 1],
|
|
99
99
|
N_[q + 2],
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
ringColor[0] * t,
|
|
101
|
+
ringColor[1] * t,
|
|
102
|
+
ringColor[2] * t,
|
|
103
103
|
w ? w[0] : r[6] || 0,
|
|
104
104
|
w ? w[1] : (r[7] ?? (r[6] || 0)),
|
|
105
105
|
w ? w[2] : (r[8] ?? 1),
|
|
106
106
|
);
|
|
107
107
|
};
|
|
108
|
-
if (
|
|
108
|
+
if (options.joined) {
|
|
109
109
|
// each point once, and the triangles through them by number
|
|
110
|
-
const first =
|
|
110
|
+
const first = builder.count / 3,
|
|
111
111
|
corners = [],
|
|
112
112
|
at = (i, j) => i * ns + (closed ? j % ns : j);
|
|
113
|
-
for (let i = 0; i <
|
|
114
|
-
for (let i = 0; i <
|
|
115
|
-
for (let j = 0; j <
|
|
113
|
+
for (let i = 0; i < ringCount; i++) for (let j = 0; j < ns; j++) V(i, j);
|
|
114
|
+
for (let i = 0; i < ringCount - 1; i++)
|
|
115
|
+
for (let j = 0; j < segments; j++) {
|
|
116
116
|
if (up) corners.push(at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j), at(i + 1, j + 1), at(i, j + 1));
|
|
117
117
|
else corners.push(at(i, j), at(i + 1, j + 1), at(i + 1, j), at(i, j), at(i, j + 1), at(i + 1, j + 1));
|
|
118
118
|
}
|
|
119
|
-
|
|
119
|
+
builder.triangles(first, corners);
|
|
120
120
|
} else
|
|
121
|
-
for (let i = 0; i <
|
|
122
|
-
for (let j = 0; j <
|
|
121
|
+
for (let i = 0; i < ringCount - 1; i++)
|
|
122
|
+
for (let j = 0; j < segments; j++) {
|
|
123
123
|
if (up) {
|
|
124
124
|
V(i, j);
|
|
125
125
|
V(i + 1, j);
|
|
@@ -136,38 +136,38 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
136
136
|
V(i + 1, j + 1);
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
if (vertexTag)
|
|
139
|
+
if (vertexTag) builder.tv = tag;
|
|
140
140
|
}
|
|
141
141
|
// Moves a loft's points (_lp, with their normals _ln) out by its relief and turns each normal down the relief's slopes: along the
|
|
142
142
|
// ring (the slope for each radian, over the metres a radian is there) and from ring to ring (the slope for each metre of
|
|
143
143
|
// height, over the metres of surface a metre of height is there).
|
|
144
|
-
function raise(rings, ns, closed, a0, step,
|
|
145
|
-
const
|
|
144
|
+
function raise(rings, ns, closed, a0, step, vertexOffset, relief) {
|
|
145
|
+
const ringCount = rings.length,
|
|
146
146
|
P_ = _lp,
|
|
147
147
|
N_ = _ln,
|
|
148
148
|
[dy, da] = RELIEF_STEP;
|
|
149
|
-
for (let i = 0; i <
|
|
149
|
+
for (let i = 0; i < ringCount; i++) {
|
|
150
150
|
const r = rings[i],
|
|
151
151
|
y = r[0],
|
|
152
152
|
low = Math.max(0, i - 1),
|
|
153
|
-
high = Math.min(
|
|
153
|
+
high = Math.min(ringCount - 1, i + 1),
|
|
154
154
|
rise = rings[high][0] - rings[low][0];
|
|
155
155
|
for (let j = 0; j < ns; j++) {
|
|
156
|
-
const
|
|
156
|
+
const angle = a0 + step * j,
|
|
157
157
|
q = (i * ns + j) * 3,
|
|
158
|
-
|
|
159
|
-
B_ =
|
|
160
|
-
C_ =
|
|
161
|
-
|
|
162
|
-
out = relief(y,
|
|
163
|
-
round = (relief(y,
|
|
164
|
-
up = (relief(y + dy,
|
|
165
|
-
tx = P_[B_] - P_[
|
|
166
|
-
ty = P_[B_ + 1] - P_[
|
|
167
|
-
tz = P_[B_ + 2] - P_[
|
|
168
|
-
ux = P_[
|
|
169
|
-
uy = P_[
|
|
170
|
-
uz = P_[
|
|
158
|
+
previousSideOffset = vertexOffset(i, j - 1),
|
|
159
|
+
B_ = vertexOffset(i, j + 1),
|
|
160
|
+
C_ = vertexOffset(low, j),
|
|
161
|
+
nextRingOffset = vertexOffset(high, j),
|
|
162
|
+
out = relief(y, angle, r),
|
|
163
|
+
round = (relief(y, angle + da, r) - relief(y, angle - da, r)) / (2 * da),
|
|
164
|
+
up = (relief(y + dy, angle, r) - relief(y - dy, angle, r)) / (2 * dy),
|
|
165
|
+
tx = P_[B_] - P_[previousSideOffset],
|
|
166
|
+
ty = P_[B_ + 1] - P_[previousSideOffset + 1],
|
|
167
|
+
tz = P_[B_ + 2] - P_[previousSideOffset + 2],
|
|
168
|
+
ux = P_[nextRingOffset] - P_[C_],
|
|
169
|
+
uy = P_[nextRingOffset + 1] - P_[C_ + 1],
|
|
170
|
+
uz = P_[nextRingOffset + 2] - P_[C_ + 2],
|
|
171
171
|
turn = (closed ? 2 : Math.min(ns - 1, j + 1) - Math.max(0, j - 1)) * step, // radians between the two neighbours (one step at an open sheet's edge)
|
|
172
172
|
kt = (round * turn) / (tx * tx + ty * ty + tz * tz || 1),
|
|
173
173
|
ku = (up * rise) / (ux * ux + uy * uy + uz * uz || 1),
|
|
@@ -183,14 +183,14 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
183
183
|
_lm[q + 2] = nz / l;
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
-
for (let k =
|
|
186
|
+
for (let k = ringCount * ns * 3 - 1; k >= 0; k--) {
|
|
187
187
|
P_[k] = _lq[k];
|
|
188
188
|
N_[k] = _lm[k];
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
const meshes = new Map();
|
|
192
|
-
// A sphere pushed into shape by
|
|
193
|
-
function sculptMesh(key, detail,
|
|
192
|
+
// A sphere pushed into shape by samplePoint(direction, outPos, channels). Cached by key; channels are a colour mix and a brightness.
|
|
193
|
+
function sculptMesh(key, detail, samplePoint, warp = 0) {
|
|
194
194
|
if (LOD_LO > 1) {
|
|
195
195
|
key += ':light';
|
|
196
196
|
detail = Math.max(2, (detail + 1) >> 2);
|
|
@@ -203,10 +203,10 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
203
203
|
n = pa.length / 3;
|
|
204
204
|
const P_ = new Float32Array(n * 3),
|
|
205
205
|
N_ = new Float32Array(n * 3),
|
|
206
|
-
|
|
206
|
+
tintWeights = new Float32Array(n),
|
|
207
207
|
Bc = new Float32Array(n),
|
|
208
|
-
|
|
209
|
-
const
|
|
208
|
+
directions = new Float32Array(n * 3);
|
|
209
|
+
const direction = new THREE.Vector3(),
|
|
210
210
|
t1 = new THREE.Vector3(),
|
|
211
211
|
t2 = new THREE.Vector3(),
|
|
212
212
|
p0 = new THREE.Vector3(),
|
|
@@ -216,31 +216,31 @@ export function createSculptor({ vertexTag } = {}) {
|
|
|
216
216
|
c2 = [0, 1],
|
|
217
217
|
e = 0.02;
|
|
218
218
|
for (let i = 0; i < n; i++) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
t1.set(
|
|
219
|
+
direction.set(pa[i * 3], pa[i * 3 + 1], pa[i * 3 + 2] + warp).normalize();
|
|
220
|
+
directions[i * 3] = direction.x;
|
|
221
|
+
directions[i * 3 + 1] = direction.y;
|
|
222
|
+
directions[i * 3 + 2] = direction.z;
|
|
223
|
+
samplePoint(direction, p0, ch);
|
|
224
|
+
t1.set(direction.z, 0, -direction.x);
|
|
225
225
|
if (t1.lengthSq() < 1e-4) t1.set(1, 0, 0);
|
|
226
226
|
t1.normalize();
|
|
227
|
-
t2.crossVectors(
|
|
228
|
-
|
|
229
|
-
|
|
227
|
+
t2.crossVectors(direction, t1);
|
|
228
|
+
samplePoint(t1.multiplyScalar(e).add(direction).normalize(), p1, c2);
|
|
229
|
+
samplePoint(t2.multiplyScalar(e).add(direction).normalize(), p2, c2);
|
|
230
230
|
p1.sub(p0);
|
|
231
231
|
p2.sub(p0);
|
|
232
232
|
t1.crossVectors(p1, p2).normalize();
|
|
233
|
-
if (t1.dot(
|
|
233
|
+
if (t1.dot(direction) < 0) t1.negate();
|
|
234
234
|
P_[i * 3] = p0.x;
|
|
235
235
|
P_[i * 3 + 1] = p0.y;
|
|
236
236
|
P_[i * 3 + 2] = p0.z;
|
|
237
237
|
N_[i * 3] = t1.x;
|
|
238
238
|
N_[i * 3 + 1] = t1.y;
|
|
239
239
|
N_[i * 3 + 2] = t1.z;
|
|
240
|
-
|
|
240
|
+
tintWeights[i] = ch[0];
|
|
241
241
|
Bc[i] = ch[1];
|
|
242
242
|
}
|
|
243
|
-
const mesh = { P: P_, N: N_, A, B: Bc, D, n };
|
|
243
|
+
const mesh = { P: P_, N: N_, A: tintWeights, B: Bc, D: directions, n };
|
|
244
244
|
meshes.set(key, mesh);
|
|
245
245
|
return mesh;
|
|
246
246
|
}
|
|
@@ -251,13 +251,13 @@ const _e1 = new THREE.Vector3(),
|
|
|
251
251
|
_e2 = new THREE.Vector3();
|
|
252
252
|
// One triangle of points [x, y, z, nx, ny, nz, own colour, own weight], wound so that it faces the way its normals do. A point
|
|
253
253
|
// without a colour or a weight of its own takes col and weight.
|
|
254
|
-
export function triangle(
|
|
255
|
-
_e1.set(
|
|
256
|
-
_e2.set(
|
|
254
|
+
export function triangle(builder, firstPoint, secondPoint, thirdPoint, col, bone, other = bone, weight = 1) {
|
|
255
|
+
_e1.set(secondPoint[0] - firstPoint[0], secondPoint[1] - firstPoint[1], secondPoint[2] - firstPoint[2]);
|
|
256
|
+
_e2.set(thirdPoint[0] - firstPoint[0], thirdPoint[1] - firstPoint[1], thirdPoint[2] - firstPoint[2]);
|
|
257
257
|
_e1.cross(_e2);
|
|
258
|
-
const flip = _e1.x * (
|
|
259
|
-
for (const v of flip ? [
|
|
260
|
-
const
|
|
261
|
-
|
|
258
|
+
const flip = _e1.x * (firstPoint[3] + secondPoint[3] + thirdPoint[3]) + _e1.y * (firstPoint[4] + secondPoint[4] + thirdPoint[4]) + _e1.z * (firstPoint[5] + secondPoint[5] + thirdPoint[5]) < 0;
|
|
259
|
+
for (const v of flip ? [firstPoint, thirdPoint, secondPoint] : [firstPoint, secondPoint, thirdPoint]) {
|
|
260
|
+
const vertexColor = v[6] || col;
|
|
261
|
+
builder.raw(v[0], v[1], v[2], v[3], v[4], v[5], vertexColor[0], vertexColor[1], vertexColor[2], bone, other, v[7] ?? weight);
|
|
262
262
|
}
|
|
263
263
|
}
|