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/builder.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build static or skinned triangle geometry from coloured parts. Workers can produce attribute arrays for a renderer on the main thread.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import * as THREE from 'three';
|
|
2
7
|
import { markStill } from '../rendering/still-casters.js';
|
|
3
8
|
import { box, TRS } from './primitives.js';
|
|
@@ -7,7 +12,7 @@ import { box, TRS } from './primitives.js';
|
|
|
7
12
|
// (geometryOf), and the primitives it places
|
|
8
13
|
// ════════════════════════════════════════════════════════════════════════
|
|
9
14
|
const _n3 = new THREE.Matrix3(),
|
|
10
|
-
|
|
15
|
+
scratchVertex = new THREE.Vector3(),
|
|
11
16
|
_m5 = new THREE.Matrix4();
|
|
12
17
|
// Three.js calls this upload callback with the attribute as its receiver. Static
|
|
13
18
|
// geometry can release its CPU copy once the GPU owns the buffer.
|
|
@@ -49,8 +54,8 @@ function sphereOf(points) {
|
|
|
49
54
|
const dx = points[i] - cx,
|
|
50
55
|
dy = points[i + 1] - cy,
|
|
51
56
|
dz = points[i + 2] - cz,
|
|
52
|
-
|
|
53
|
-
if (
|
|
57
|
+
distanceSquared = dx * dx + dy * dy + dz * dz;
|
|
58
|
+
if (distanceSquared > far) far = distanceSquared;
|
|
54
59
|
}
|
|
55
60
|
return [cx, cy, cz, Math.sqrt(far)];
|
|
56
61
|
}
|
|
@@ -59,38 +64,38 @@ function sphereOf(points) {
|
|
|
59
64
|
// `sphere`: its bounding sphere as [x, y, z, radius], where it was worked out already, so the points are
|
|
60
65
|
// not read again.
|
|
61
66
|
function geometryOf(parts, sphere = null) {
|
|
62
|
-
const
|
|
67
|
+
const geometry = new THREE.BufferGeometry(),
|
|
63
68
|
vertices = parts.position.length / 3;
|
|
64
69
|
for (const name in ATTRIBUTES) {
|
|
65
70
|
const array = parts[name],
|
|
66
71
|
size = vertices ? array?.length / vertices : ATTRIBUTES[name];
|
|
67
|
-
if (array)
|
|
72
|
+
if (array) geometry.setAttribute(name, new THREE.BufferAttribute(array, size, name !== 'skinIndex' && !(array instanceof Float32Array)));
|
|
68
73
|
}
|
|
69
|
-
if (parts.index)
|
|
74
|
+
if (parts.index) geometry.setIndex(new THREE.BufferAttribute(parts.index, 1).onUpload(releaseArray));
|
|
70
75
|
const points = parts.position;
|
|
71
76
|
if (!sphere && points instanceof Float32Array && points.length) sphere = sphereOf(points);
|
|
72
|
-
if (sphere)
|
|
73
|
-
else
|
|
77
|
+
if (sphere) geometry.boundingSphere = new THREE.Sphere(new THREE.Vector3(sphere[0], sphere[1], sphere[2]), sphere[3]);
|
|
78
|
+
else geometry.computeBoundingSphere();
|
|
74
79
|
// Skinned meshes compute bounds on the CPU. Keep their positions, joint indices
|
|
75
80
|
// and weights available, including when several meshes share the geometry.
|
|
76
|
-
for (const name in
|
|
77
|
-
return
|
|
81
|
+
for (const name in geometry.attributes) if (!(parts.skinIndex && SKIN_BOUNDS.includes(name))) geometry.attributes[name].onUpload(releaseArray);
|
|
82
|
+
return geometry;
|
|
78
83
|
}
|
|
79
84
|
// A mesh of a builder's arrays (GB.parts), as GB.mesh makes one, in `parent`
|
|
80
85
|
function meshOf(parts, material, parent, shadow = true) {
|
|
81
|
-
const
|
|
82
|
-
parent.add(
|
|
83
|
-
return
|
|
86
|
+
const mesh = stillMesh(geometryOf(parts), material, shadow);
|
|
87
|
+
parent.add(mesh);
|
|
88
|
+
return mesh;
|
|
84
89
|
}
|
|
85
90
|
// A static mesh with its local transform fixed and optional shadow casting.
|
|
86
91
|
function stillMesh(geometry, material, shadow) {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (shadow) markStill(
|
|
93
|
-
return
|
|
92
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
93
|
+
mesh.castShadow = shadow;
|
|
94
|
+
mesh.receiveShadow = true;
|
|
95
|
+
mesh.matrixAutoUpdate = false;
|
|
96
|
+
mesh.updateMatrix();
|
|
97
|
+
if (shadow) markStill(mesh);
|
|
98
|
+
return mesh;
|
|
94
99
|
}
|
|
95
100
|
// Share GPU buffers and bounds while giving each instanced mesh a geometry identity
|
|
96
101
|
// of its own. Three.js can then reuse the vertex bindings for each mesh.
|
|
@@ -114,22 +119,26 @@ function withRoom(array, room, used) {
|
|
|
114
119
|
return roomier;
|
|
115
120
|
}
|
|
116
121
|
// A normal's or a colour's number as the byte a compact geometry stores (the typed array it goes into wraps what is out of range).
|
|
117
|
-
const normalByte =
|
|
118
|
-
colourByte =
|
|
122
|
+
const normalByte = component => Math.round(component * 127),
|
|
123
|
+
colourByte = component => Math.min(255, Math.round(component * 255)),
|
|
119
124
|
OPAQUE = 255; // a padded colour's fourth byte: an alpha of one
|
|
120
125
|
// The first `count` bytes of three to a vertex, four to a vertex: the three and then `fourth`
|
|
121
126
|
function padded(bytes, count, fourth) {
|
|
122
127
|
const out = new bytes.constructor((count / 3) * 4);
|
|
123
|
-
for (let i = 0,
|
|
124
|
-
out[
|
|
125
|
-
out[
|
|
126
|
-
out[
|
|
127
|
-
out[
|
|
128
|
+
for (let i = 0, writeOffset = 0; i < count; i += 3, writeOffset += 4) {
|
|
129
|
+
out[writeOffset] = bytes[i];
|
|
130
|
+
out[writeOffset + 1] = bytes[i + 1];
|
|
131
|
+
out[writeOffset + 2] = bytes[i + 2];
|
|
132
|
+
out[writeOffset + 3] = fourth;
|
|
128
133
|
}
|
|
129
134
|
return out;
|
|
130
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Accumulate coloured primitives and triangles in one set of vertex buffers.
|
|
138
|
+
* Import this class as `GeometryBuilder` from the package root. Use `parts`
|
|
139
|
+
* to transfer the result from a worker, or `mesh` to add it directly to a scene.
|
|
140
|
+
*/
|
|
131
141
|
class GB {
|
|
132
|
-
// merges many coloured parts into one static geometry
|
|
133
142
|
constructor() {
|
|
134
143
|
// Positions, normals and colours fill typed arrays that grow, three numbers to a vertex: four bytes a number where a plain
|
|
135
144
|
// array of numbers takes eight. Normals and colours are kept twice, as floats and as the bytes a compact geometry stores
|
|
@@ -177,48 +186,48 @@ class GB {
|
|
|
177
186
|
this.pb.push(this.count);
|
|
178
187
|
this.bounds.push(1e9, -1e9, 1e9, -1e9, 1e9, -1e9);
|
|
179
188
|
}
|
|
180
|
-
add(
|
|
181
|
-
const
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
numbers =
|
|
189
|
+
add(geometry, color, transform) {
|
|
190
|
+
const sourceGeometry = geometry.index ? geometry.userData.ni || (geometry.userData.ni = geometry.toNonIndexed()) : geometry;
|
|
191
|
+
const positionArray = sourceGeometry.attributes.position.array,
|
|
192
|
+
normalArray = sourceGeometry.attributes.normal.array,
|
|
193
|
+
numbers = positionArray.length,
|
|
185
194
|
parts = !!this.pb;
|
|
186
195
|
if (parts) {
|
|
187
196
|
this.startPart();
|
|
188
197
|
this._ra = false;
|
|
189
198
|
}
|
|
190
|
-
if (this.M)
|
|
191
|
-
_n3.getNormalMatrix(
|
|
199
|
+
if (this.M) transform = _m5.multiplyMatrices(this.M, transform);
|
|
200
|
+
_n3.getNormalMatrix(transform);
|
|
192
201
|
this.reserve(numbers);
|
|
193
202
|
// Three.js's Vector3.applyMatrix4, applyMatrix3 and normalize, written out with the matrices' numbers read once for the
|
|
194
203
|
// part: the same sums in the same order, so every vertex is where Three.js would put it
|
|
195
|
-
const [e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15] =
|
|
204
|
+
const [e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15] = transform.elements,
|
|
196
205
|
[f0, f1, f2, f3, f4, f5, f6, f7, f8] = _n3.elements,
|
|
197
206
|
flat = e3 === 0 && e7 === 0 && e11 === 0 && e15 === 1, // no perspective in the matrix: the division Three.js does is by one
|
|
198
207
|
{ ox, oy, oz, positions, normals, colours, normalBytes, colourBytes } = this,
|
|
199
|
-
[
|
|
200
|
-
rByte = colourByte(
|
|
201
|
-
grByte = colourByte(
|
|
202
|
-
bByte = colourByte(
|
|
203
|
-
if (!(Math.min(
|
|
204
|
-
let
|
|
208
|
+
[red, green, blue] = color,
|
|
209
|
+
rByte = colourByte(red),
|
|
210
|
+
grByte = colourByte(green),
|
|
211
|
+
bByte = colourByte(blue);
|
|
212
|
+
if (!(Math.min(red, green, blue) >= 0 && Math.max(red, green, blue) <= 1)) this.wide = true;
|
|
213
|
+
let writeOffset = this.count,
|
|
205
214
|
x0 = 1e9,
|
|
206
215
|
x1 = -1e9,
|
|
207
216
|
y0 = 1e9,
|
|
208
217
|
y1 = -1e9,
|
|
209
218
|
z0 = 1e9,
|
|
210
219
|
z1 = -1e9;
|
|
211
|
-
for (let i = 0; i < numbers; i += 3,
|
|
212
|
-
let x =
|
|
213
|
-
y =
|
|
214
|
-
z =
|
|
220
|
+
for (let i = 0; i < numbers; i += 3, writeOffset += 3) {
|
|
221
|
+
let x = positionArray[i],
|
|
222
|
+
y = positionArray[i + 1],
|
|
223
|
+
z = positionArray[i + 2];
|
|
215
224
|
const w = flat ? 1 : 1 / (e3 * x + e7 * y + e11 * z + e15),
|
|
216
225
|
px = (e0 * x + e4 * y + e8 * z + e12) * w + ox,
|
|
217
226
|
py = (e1 * x + e5 * y + e9 * z + e13) * w + oy,
|
|
218
227
|
pz = (e2 * x + e6 * y + e10 * z + e14) * w + oz;
|
|
219
|
-
positions[
|
|
220
|
-
positions[
|
|
221
|
-
positions[
|
|
228
|
+
positions[writeOffset] = px;
|
|
229
|
+
positions[writeOffset + 1] = py;
|
|
230
|
+
positions[writeOffset + 2] = pz;
|
|
222
231
|
if (parts) {
|
|
223
232
|
if (px < x0) x0 = px;
|
|
224
233
|
if (px > x1) x1 = px;
|
|
@@ -227,9 +236,9 @@ class GB {
|
|
|
227
236
|
if (pz < z0) z0 = pz;
|
|
228
237
|
if (pz > z1) z1 = pz;
|
|
229
238
|
}
|
|
230
|
-
x =
|
|
231
|
-
y =
|
|
232
|
-
z =
|
|
239
|
+
x = normalArray[i];
|
|
240
|
+
y = normalArray[i + 1];
|
|
241
|
+
z = normalArray[i + 2];
|
|
233
242
|
let nx = f0 * x + f3 * y + f6 * z,
|
|
234
243
|
ny = f1 * x + f4 * y + f7 * z,
|
|
235
244
|
nz = f2 * x + f5 * y + f8 * z;
|
|
@@ -237,24 +246,24 @@ class GB {
|
|
|
237
246
|
nx *= unit;
|
|
238
247
|
ny *= unit;
|
|
239
248
|
nz *= unit;
|
|
240
|
-
normals[
|
|
241
|
-
normals[
|
|
242
|
-
normals[
|
|
243
|
-
normalBytes[
|
|
244
|
-
normalBytes[
|
|
245
|
-
normalBytes[
|
|
246
|
-
colours[
|
|
247
|
-
colours[
|
|
248
|
-
colours[
|
|
249
|
-
colourBytes[
|
|
250
|
-
colourBytes[
|
|
251
|
-
colourBytes[
|
|
249
|
+
normals[writeOffset] = nx;
|
|
250
|
+
normals[writeOffset + 1] = ny;
|
|
251
|
+
normals[writeOffset + 2] = nz;
|
|
252
|
+
normalBytes[writeOffset] = normalByte(nx);
|
|
253
|
+
normalBytes[writeOffset + 1] = normalByte(ny);
|
|
254
|
+
normalBytes[writeOffset + 2] = normalByte(nz);
|
|
255
|
+
colours[writeOffset] = red;
|
|
256
|
+
colours[writeOffset + 1] = green;
|
|
257
|
+
colours[writeOffset + 2] = blue;
|
|
258
|
+
colourBytes[writeOffset] = rByte;
|
|
259
|
+
colourBytes[writeOffset + 1] = grByte;
|
|
260
|
+
colourBytes[writeOffset + 2] = bByte;
|
|
252
261
|
}
|
|
253
|
-
this.count =
|
|
262
|
+
this.count = writeOffset;
|
|
254
263
|
if (parts) this.bounds.splice(-6, 6, x0, x1, y0, y1, z0, z1);
|
|
255
264
|
const vertices = numbers / 3;
|
|
256
265
|
if (this.tg) {
|
|
257
|
-
const tag = this.tags?.get(
|
|
266
|
+
const tag = this.tags?.get(color) ?? this.tv; // a builder may say what each of its colours is made of (a person's body: its metal, its leather)
|
|
258
267
|
for (let i = 0; i < vertices; i++) this.tg.push(tag);
|
|
259
268
|
}
|
|
260
269
|
if (this.sk)
|
|
@@ -267,20 +276,21 @@ class GB {
|
|
|
267
276
|
}
|
|
268
277
|
return this;
|
|
269
278
|
}
|
|
270
|
-
box(x, y, z
|
|
271
|
-
|
|
279
|
+
/** Append a box with its base centred at (x, y, z). Dimensions are in world units and rotations are in radians. */
|
|
280
|
+
box(x, y, z, sx, sy, sz, color, ry = 0, rx = 0, rz = 0) {
|
|
281
|
+
return this.add(box, color, TRS(x, y, z, sx, sy, sz, ry, rx, rz));
|
|
272
282
|
}
|
|
273
|
-
raw(x, y, z, nx, ny, nz,
|
|
283
|
+
raw(x, y, z, nx, ny, nz, red, green, blue, primaryBone = 0, secondaryBone = 0, w = 1) {
|
|
274
284
|
// one vertex, optionally weighted between two bones
|
|
275
285
|
if (this.M) {
|
|
276
|
-
|
|
277
|
-
x =
|
|
278
|
-
y =
|
|
279
|
-
z =
|
|
280
|
-
|
|
281
|
-
nx =
|
|
282
|
-
ny =
|
|
283
|
-
nz =
|
|
286
|
+
scratchVertex.set(x, y, z).applyMatrix4(this.M);
|
|
287
|
+
x = scratchVertex.x;
|
|
288
|
+
y = scratchVertex.y;
|
|
289
|
+
z = scratchVertex.z;
|
|
290
|
+
scratchVertex.set(nx, ny, nz).transformDirection(this.M);
|
|
291
|
+
nx = scratchVertex.x;
|
|
292
|
+
ny = scratchVertex.y;
|
|
293
|
+
nz = scratchVertex.z;
|
|
284
294
|
}
|
|
285
295
|
if (this.pb) {
|
|
286
296
|
// vertices put in one after another are one part
|
|
@@ -298,30 +308,30 @@ class GB {
|
|
|
298
308
|
if (z > box[at + 5]) box[at + 5] = z;
|
|
299
309
|
}
|
|
300
310
|
this.reserve(3);
|
|
301
|
-
if (!(Math.min(
|
|
302
|
-
const
|
|
311
|
+
if (!(Math.min(red, green, blue) >= 0 && Math.max(red, green, blue) <= 1 && Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) <= 1)) this.wide = true;
|
|
312
|
+
const writeOffset = this.count,
|
|
303
313
|
{ positions, normals, colours, normalBytes, colourBytes } = this;
|
|
304
|
-
positions[
|
|
305
|
-
positions[
|
|
306
|
-
positions[
|
|
307
|
-
normals[
|
|
308
|
-
normals[
|
|
309
|
-
normals[
|
|
310
|
-
normalBytes[
|
|
311
|
-
normalBytes[
|
|
312
|
-
normalBytes[
|
|
313
|
-
colours[
|
|
314
|
-
colours[
|
|
315
|
-
colours[
|
|
316
|
-
colourBytes[
|
|
317
|
-
colourBytes[
|
|
318
|
-
colourBytes[
|
|
319
|
-
this.count =
|
|
314
|
+
positions[writeOffset] = x;
|
|
315
|
+
positions[writeOffset + 1] = y;
|
|
316
|
+
positions[writeOffset + 2] = z;
|
|
317
|
+
normals[writeOffset] = nx;
|
|
318
|
+
normals[writeOffset + 1] = ny;
|
|
319
|
+
normals[writeOffset + 2] = nz;
|
|
320
|
+
normalBytes[writeOffset] = normalByte(nx);
|
|
321
|
+
normalBytes[writeOffset + 1] = normalByte(ny);
|
|
322
|
+
normalBytes[writeOffset + 2] = normalByte(nz);
|
|
323
|
+
colours[writeOffset] = red;
|
|
324
|
+
colours[writeOffset + 1] = green;
|
|
325
|
+
colours[writeOffset + 2] = blue;
|
|
326
|
+
colourBytes[writeOffset] = colourByte(red);
|
|
327
|
+
colourBytes[writeOffset + 1] = colourByte(green);
|
|
328
|
+
colourBytes[writeOffset + 2] = colourByte(blue);
|
|
329
|
+
this.count = writeOffset + 3;
|
|
320
330
|
if (this.tg) this.tg.push(this.tv);
|
|
321
331
|
if (this.sk) {
|
|
322
|
-
this.sk.push(
|
|
332
|
+
this.sk.push(primaryBone);
|
|
323
333
|
if (this.sk2) {
|
|
324
|
-
this.sk2.push(
|
|
334
|
+
this.sk2.push(secondaryBone);
|
|
325
335
|
this.skw.push(w);
|
|
326
336
|
}
|
|
327
337
|
}
|
|
@@ -372,10 +382,10 @@ class GB {
|
|
|
372
382
|
const skip = lo - part.first,
|
|
373
383
|
limit = hi - lo;
|
|
374
384
|
for (let i = 0; i < part.corners.length; i += 3) {
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (
|
|
385
|
+
const firstCorner = part.corners[i] - skip,
|
|
386
|
+
secondCorner = part.corners[i + 1] - skip,
|
|
387
|
+
thirdCorner = part.corners[i + 2] - skip;
|
|
388
|
+
if (firstCorner >= 0 && firstCorner < limit && secondCorner >= 0 && secondCorner < limit && thirdCorner >= 0 && thirdCorner < limit) corners.push(firstCorner, secondCorner, thirdCorner);
|
|
379
389
|
}
|
|
380
390
|
}
|
|
381
391
|
this.triangles(first + lo - start, corners, first + hi - start);
|
|
@@ -387,83 +397,83 @@ class GB {
|
|
|
387
397
|
// A sculptMesh, tinted between two colours by its first channel. cut(dir) leaves out what it is true for; the triangles its edge
|
|
388
398
|
// crosses are clipped where the edge runs (found by halving along each crossed side), so a hairline, beard or hood ends in a
|
|
389
399
|
// smooth line instead of the sphere's triangles.
|
|
390
|
-
sculpt(
|
|
391
|
-
const { P, N, A, B, D } =
|
|
392
|
-
|
|
400
|
+
sculpt(sculptedMesh, color, secondColor, cut, scale = 1) {
|
|
401
|
+
const { P: positions, N: normals, A: tintWeights, B: shades, D: directions } = sculptedMesh,
|
|
402
|
+
boneIndex = this.bone,
|
|
393
403
|
ox = this.ox,
|
|
394
404
|
oy = this.oy,
|
|
395
405
|
oz = this.oz;
|
|
396
|
-
const emit =
|
|
397
|
-
const
|
|
398
|
-
|
|
406
|
+
const emit = vertex => {
|
|
407
|
+
const tintWeight = vertex[9],
|
|
408
|
+
shade = vertex[10];
|
|
399
409
|
this.raw(
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
(
|
|
407
|
-
(
|
|
408
|
-
(
|
|
409
|
-
|
|
410
|
-
|
|
410
|
+
vertex[0] * scale + ox,
|
|
411
|
+
vertex[1] * scale + oy,
|
|
412
|
+
vertex[2] * scale + oz,
|
|
413
|
+
vertex[3],
|
|
414
|
+
vertex[4],
|
|
415
|
+
vertex[5],
|
|
416
|
+
(color[0] + (secondColor[0] - color[0]) * tintWeight) * shade,
|
|
417
|
+
(color[1] + (secondColor[1] - color[1]) * tintWeight) * shade,
|
|
418
|
+
(color[2] + (secondColor[2] - color[2]) * tintWeight) * shade,
|
|
419
|
+
boneIndex,
|
|
420
|
+
boneIndex,
|
|
411
421
|
1,
|
|
412
422
|
);
|
|
413
423
|
};
|
|
414
424
|
const vert = i => [
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
425
|
+
positions[i * 3],
|
|
426
|
+
positions[i * 3 + 1],
|
|
427
|
+
positions[i * 3 + 2],
|
|
428
|
+
normals[i * 3],
|
|
429
|
+
normals[i * 3 + 1],
|
|
430
|
+
normals[i * 3 + 2],
|
|
431
|
+
directions[i * 3],
|
|
432
|
+
directions[i * 3 + 1],
|
|
433
|
+
directions[i * 3 + 2],
|
|
434
|
+
tintWeights[i],
|
|
435
|
+
shades[i],
|
|
426
436
|
];
|
|
427
|
-
const mix = (
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
return
|
|
437
|
+
const mix = (startVertex, endVertex, fraction) => {
|
|
438
|
+
const interpolated = startVertex.map((x, j) => x + (endVertex[j] - x) * fraction),
|
|
439
|
+
directionLength = Math.hypot(interpolated[6], interpolated[7], interpolated[8]) || 1,
|
|
440
|
+
normalLength = Math.hypot(interpolated[3], interpolated[4], interpolated[5]) || 1;
|
|
441
|
+
interpolated[6] /= directionLength;
|
|
442
|
+
interpolated[7] /= directionLength;
|
|
443
|
+
interpolated[8] /= directionLength;
|
|
444
|
+
interpolated[3] /= normalLength;
|
|
445
|
+
interpolated[4] /= normalLength;
|
|
446
|
+
interpolated[5] /= normalLength;
|
|
447
|
+
return interpolated;
|
|
438
448
|
};
|
|
439
|
-
const out =
|
|
440
|
-
for (let
|
|
441
|
-
const
|
|
442
|
-
o0 = cut ? !!cut(
|
|
443
|
-
o1 = cut ? !!cut(
|
|
444
|
-
o2 = cut ? !!cut(
|
|
449
|
+
const out = vertex => !!cut(vertex[6], vertex[7], vertex[8]);
|
|
450
|
+
for (let triangleStart = 0; triangleStart < sculptedMesh.n; triangleStart += 3) {
|
|
451
|
+
const directionOffset = triangleStart * 3,
|
|
452
|
+
o0 = cut ? !!cut(directions[directionOffset], directions[directionOffset + 1], directions[directionOffset + 2]) : false,
|
|
453
|
+
o1 = cut ? !!cut(directions[directionOffset + 3], directions[directionOffset + 4], directions[directionOffset + 5]) : false,
|
|
454
|
+
o2 = cut ? !!cut(directions[directionOffset + 6], directions[directionOffset + 7], directions[directionOffset + 8]) : false;
|
|
445
455
|
if (o0 && o1 && o2) continue;
|
|
446
456
|
if (!o0 && !o1 && !o2) {
|
|
447
|
-
for (let i =
|
|
457
|
+
for (let i = triangleStart; i < triangleStart + 3; i++) emit(vert(i));
|
|
448
458
|
continue;
|
|
449
459
|
}
|
|
450
|
-
const tri = [vert(
|
|
451
|
-
|
|
460
|
+
const tri = [vert(triangleStart), vert(triangleStart + 1), vert(triangleStart + 2)],
|
|
461
|
+
outsideFlags = [o0, o1, o2],
|
|
452
462
|
poly = [];
|
|
453
463
|
for (let e = 0; e < 3; e++) {
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
if (!
|
|
458
|
-
if (
|
|
464
|
+
const edgeStart = tri[e],
|
|
465
|
+
edgeEnd = tri[(e + 1) % 3],
|
|
466
|
+
startOutside = outsideFlags[e];
|
|
467
|
+
if (!startOutside) poly.push(edgeStart);
|
|
468
|
+
if (startOutside !== outsideFlags[(e + 1) % 3]) {
|
|
459
469
|
let lo = 0,
|
|
460
470
|
hi = 1;
|
|
461
|
-
for (let
|
|
471
|
+
for (let iteration = 0; iteration < 7; iteration++) {
|
|
462
472
|
const mid = (lo + hi) / 2;
|
|
463
|
-
if (out(mix(
|
|
473
|
+
if (out(mix(edgeStart, edgeEnd, mid)) === startOutside) lo = mid;
|
|
464
474
|
else hi = mid;
|
|
465
475
|
}
|
|
466
|
-
poly.push(mix(
|
|
476
|
+
poly.push(mix(edgeStart, edgeEnd, startOutside ? hi : lo));
|
|
467
477
|
}
|
|
468
478
|
}
|
|
469
479
|
for (let i = 1; i < poly.length - 1; i++) {
|
|
@@ -473,11 +483,12 @@ class GB {
|
|
|
473
483
|
}
|
|
474
484
|
}
|
|
475
485
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
486
|
+
/**
|
|
487
|
+
* Copy the filled attributes into right-sized typed arrays for `geometryOf`.
|
|
488
|
+
* The arrays can be transferred to another thread. The builder retains its own storage.
|
|
489
|
+
* @param {boolean} [compact] - Store normals and colours as normalized bytes.
|
|
490
|
+
* @param {boolean} [pad] - Pad compact normals and colours to four bytes per vertex for aligned GPU uploads.
|
|
491
|
+
*/
|
|
481
492
|
parts(compact, pad = false) {
|
|
482
493
|
if (this.spent) throw new Error('GB: built again after its parts were let go (release)');
|
|
483
494
|
const filled = array => array.slice(0, this.count),
|
|
@@ -491,23 +502,23 @@ class GB {
|
|
|
491
502
|
if (this.tg) parts.aTag = new Float32Array(this.tg);
|
|
492
503
|
if (this.sk) {
|
|
493
504
|
// rigid skinning: every vertex follows exactly one bone
|
|
494
|
-
const
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
for (let i = 0; i <
|
|
505
|
+
const vertexCount = this.sk.length,
|
|
506
|
+
skinIndices = (parts.skinIndex = new Uint8Array(vertexCount * 4)),
|
|
507
|
+
skinWeights = (parts.skinWeight = new Uint8Array(vertexCount * 4));
|
|
508
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
498
509
|
const w = this.sk2 ? Math.round(this.skw[i] * 255) : 255;
|
|
499
|
-
|
|
500
|
-
|
|
510
|
+
skinIndices[i * 4] = this.sk[i];
|
|
511
|
+
skinWeights[i * 4] = w;
|
|
501
512
|
if (this.sk2) {
|
|
502
|
-
|
|
503
|
-
|
|
513
|
+
skinIndices[i * 4 + 1] = this.sk2[i];
|
|
514
|
+
skinWeights[i * 4 + 1] = 255 - w;
|
|
504
515
|
}
|
|
505
516
|
}
|
|
506
517
|
if (this.sk3)
|
|
507
518
|
for (const [i, third] of this.sk3) {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
519
|
+
skinIndices[i * 4 + 2] = third[0];
|
|
520
|
+
skinWeights[i * 4 + 2] = Math.min(255 - skinWeights[i * 4], Math.round(third[1] * 255));
|
|
521
|
+
skinWeights[i * 4 + 1] = 255 - skinWeights[i * 4] - skinWeights[i * 4 + 2];
|
|
511
522
|
}
|
|
512
523
|
}
|
|
513
524
|
return parts;
|
|
@@ -532,11 +543,12 @@ class GB {
|
|
|
532
543
|
if (this.skw) this.skw = [];
|
|
533
544
|
this.spent = true;
|
|
534
545
|
}
|
|
535
|
-
mesh
|
|
536
|
-
|
|
546
|
+
/** Build a static mesh, release the builder’s backing arrays and attach the mesh to the optional parent. */
|
|
547
|
+
mesh(material, parent, shadow = true) {
|
|
548
|
+
const mesh = stillMesh(this.build(), material, shadow);
|
|
537
549
|
this.release();
|
|
538
|
-
if (parent) parent.add(
|
|
539
|
-
return
|
|
550
|
+
if (parent) parent.add(mesh);
|
|
551
|
+
return mesh;
|
|
540
552
|
}
|
|
541
553
|
}
|
|
542
554
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pack a grid into one bit per cell. The predicate receives the row-major index
|
|
3
|
+
* `row * width + column`. Transfer the returned `bits.buffer` with the mask when
|
|
4
|
+
* handing it to a worker.
|
|
5
|
+
* @param {number} width - Positive integer column count.
|
|
6
|
+
* @param {number} height - Positive integer row count.
|
|
7
|
+
* @param {(index: number) => boolean} includesIndex - Selects cells to mark.
|
|
8
|
+
*/
|
|
9
|
+
export function packCellMask(width, height, includesIndex) {
|
|
10
|
+
if (!(Number.isSafeInteger(width) && width > 0 && Number.isSafeInteger(height) && height > 0))
|
|
11
|
+
throw new RangeError('Cell mask dimensions must be positive integers');
|
|
12
|
+
const length = width * height;
|
|
13
|
+
const bits = new Uint8Array(Math.ceil(length / 8));
|
|
14
|
+
for (let cellIndex = 0; cellIndex < length; cellIndex++) {
|
|
15
|
+
if (includesIndex(cellIndex)) bits[Math.floor(cellIndex / 8)] |= 1 << (cellIndex & 7);
|
|
16
|
+
}
|
|
17
|
+
return { width, height, bits };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Read membership at cell coordinates, rounding down within a cell. Coordinates outside the grid return false. */
|
|
21
|
+
export function cellIsSet(mask, x, y) {
|
|
22
|
+
if (!(x >= 0 && x < mask.width && y >= 0 && y < mask.height)) return false;
|
|
23
|
+
const cellIndex = Math.floor(y) * mask.width + Math.floor(x);
|
|
24
|
+
return (mask.bits[Math.floor(cellIndex / 8)] & (1 << (cellIndex & 7))) !== 0;
|
|
25
|
+
}
|