roamwright 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +12 -0
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/examples/index.html +19 -0
- package/examples/island.js +62 -0
- package/geometry/builder.js +546 -0
- package/geometry/instances.js +76 -0
- package/geometry/placement.js +65 -0
- package/geometry/primitives.js +172 -0
- package/index.js +8 -0
- package/licenses/baking-lab.txt +21 -0
- package/licenses/fidelityfx-fsr.txt +19 -0
- package/licenses/three.txt +21 -0
- package/math/curve.js +94 -0
- package/math/noise.js +80 -0
- package/math/random.js +27 -0
- package/math/scalar.js +12 -0
- package/motion/player.js +248 -0
- package/motion/spaces.js +206 -0
- package/package.json +50 -4
- package/rendering/context.js +20 -0
- package/rendering/dof.js +59 -0
- package/rendering/eye-adapt.js +119 -0
- package/rendering/piece-batch.js +411 -0
- package/rendering/post-shaders.js +236 -0
- package/rendering/post.js +520 -0
- package/rendering/still-casters.js +37 -0
- package/rendering/taa.js +171 -0
- package/rendering/textures.js +63 -0
- package/rendering/world-matrices.js +77 -0
- package/runtime/first-use.js +60 -0
- package/runtime/gpu-timing.js +75 -0
- package/runtime/hand-over.js +102 -0
- package/runtime/pool.js +153 -0
- package/runtime/quality-fit.js +194 -0
- package/runtime/quality-store.js +58 -0
- package/runtime/seen.js +36 -0
- package/runtime/worker.js +53 -0
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { markStill } from '../rendering/still-casters.js';
|
|
3
|
+
import { box, TRS } from './primitives.js';
|
|
4
|
+
|
|
5
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
6
|
+
// Geometry toolkit: a builder that merges many coloured parts into one geometry (GB), the geometry of what it filled
|
|
7
|
+
// (geometryOf), and the primitives it places
|
|
8
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9
|
+
const _n3 = new THREE.Matrix3(),
|
|
10
|
+
_v = new THREE.Vector3(),
|
|
11
|
+
_m5 = new THREE.Matrix4();
|
|
12
|
+
// A built geometry is never changed again, so once it is on the graphics card the copy of its arrays kept here is let go: together
|
|
13
|
+
// they are several hundred megabytes, which a phone does not have. (Three.js calls this with the attribute as `this`.)
|
|
14
|
+
function releaseArray() {
|
|
15
|
+
this.bytes = this.array.byteLength; // what stands on the card, for the reckoning of memory
|
|
16
|
+
this.array = null;
|
|
17
|
+
}
|
|
18
|
+
// What a geometry made by a builder is made of, in order, with the numbers to a vertex of each; a skinned mesh reads the
|
|
19
|
+
// positions and the two skin attributes to find its bounds.
|
|
20
|
+
const ATTRIBUTES = { position: 3, normal: 3, color: 3, aTag: 1, skinIndex: 4, skinWeight: 4 },
|
|
21
|
+
SKIN_BOUNDS = ['position', 'skinIndex', 'skinWeight'];
|
|
22
|
+
// The bounding sphere of points [x, y, z, ...] as Three.js works it out (computeBoundingSphere: the centre of their box, the
|
|
23
|
+
// distance of the farthest), as [x, y, z, radius], read straight from the array rather than point by point through the attribute:
|
|
24
|
+
// workers can compute a bound before transferring their buffers.
|
|
25
|
+
function sphereOf(points) {
|
|
26
|
+
if (!points.length) return [0, 0, 0, 0];
|
|
27
|
+
let x0 = Infinity,
|
|
28
|
+
y0 = Infinity,
|
|
29
|
+
z0 = Infinity,
|
|
30
|
+
x1 = -Infinity,
|
|
31
|
+
y1 = -Infinity,
|
|
32
|
+
z1 = -Infinity;
|
|
33
|
+
for (let i = 0; i < points.length; i += 3) {
|
|
34
|
+
const x = points[i],
|
|
35
|
+
y = points[i + 1],
|
|
36
|
+
z = points[i + 2];
|
|
37
|
+
if (x < x0) x0 = x;
|
|
38
|
+
if (x > x1) x1 = x;
|
|
39
|
+
if (y < y0) y0 = y;
|
|
40
|
+
if (y > y1) y1 = y;
|
|
41
|
+
if (z < z0) z0 = z;
|
|
42
|
+
if (z > z1) z1 = z;
|
|
43
|
+
}
|
|
44
|
+
const cx = (x0 + x1) * 0.5,
|
|
45
|
+
cy = (y0 + y1) * 0.5,
|
|
46
|
+
cz = (z0 + z1) * 0.5;
|
|
47
|
+
let far = 0;
|
|
48
|
+
for (let i = 0; i < points.length; i += 3) {
|
|
49
|
+
const dx = points[i] - cx,
|
|
50
|
+
dy = points[i + 1] - cy,
|
|
51
|
+
dz = points[i + 2] - cz,
|
|
52
|
+
d = dx * dx + dy * dy + dz * dz;
|
|
53
|
+
if (d > far) far = d;
|
|
54
|
+
}
|
|
55
|
+
return [cx, cy, cz, Math.sqrt(far)];
|
|
56
|
+
}
|
|
57
|
+
// A geometry of the arrays a builder filled (GB.parts). Byte normals, colours and skin weights are read as fractions; a padded
|
|
58
|
+
// normal or colour has four numbers to a vertex (the fourth a colour's opaque alpha, which Three.js reads as vertex alpha).
|
|
59
|
+
// `sphere`: its bounding sphere as [x, y, z, radius], where it was worked out already, so the points are
|
|
60
|
+
// not read again.
|
|
61
|
+
function geometryOf(parts, sphere = null) {
|
|
62
|
+
const g = new THREE.BufferGeometry(),
|
|
63
|
+
vertices = parts.position.length / 3;
|
|
64
|
+
for (const name in ATTRIBUTES) {
|
|
65
|
+
const array = parts[name],
|
|
66
|
+
size = vertices ? array?.length / vertices : ATTRIBUTES[name];
|
|
67
|
+
if (array) g.setAttribute(name, new THREE.BufferAttribute(array, size, name !== 'skinIndex' && !(array instanceof Float32Array)));
|
|
68
|
+
}
|
|
69
|
+
if (parts.index) g.setIndex(new THREE.BufferAttribute(parts.index, 1).onUpload(releaseArray));
|
|
70
|
+
const points = parts.position;
|
|
71
|
+
if (!sphere && points instanceof Float32Array && points.length) sphere = sphereOf(points);
|
|
72
|
+
if (sphere) g.boundingSphere = new THREE.Sphere(new THREE.Vector3(sphere[0], sphere[1], sphere[2]), sphere[3]);
|
|
73
|
+
else g.computeBoundingSphere();
|
|
74
|
+
// a skinned mesh works out its own bounds the first time it is drawn, from the positions and the bones each follows, and
|
|
75
|
+
// several meshes may share one geometry (the riders' horses): a skinned geometry keeps those three
|
|
76
|
+
for (const name in g.attributes) if (!(parts.skinIndex && SKIN_BOUNDS.includes(name))) g.attributes[name].onUpload(releaseArray);
|
|
77
|
+
return g;
|
|
78
|
+
}
|
|
79
|
+
// A mesh of a builder's arrays (GB.parts), as GB.mesh makes one, in `parent`
|
|
80
|
+
function meshOf(parts, material, parent, shadow = true) {
|
|
81
|
+
const m = stillMesh(geometryOf(parts), material, shadow);
|
|
82
|
+
parent.add(m);
|
|
83
|
+
return m;
|
|
84
|
+
}
|
|
85
|
+
// A static mesh with its local transform fixed and optional shadow casting.
|
|
86
|
+
function stillMesh(geometry, material, shadow) {
|
|
87
|
+
const m = new THREE.Mesh(geometry, material);
|
|
88
|
+
m.castShadow = shadow;
|
|
89
|
+
m.receiveShadow = true;
|
|
90
|
+
m.matrixAutoUpdate = false;
|
|
91
|
+
m.updateMatrix();
|
|
92
|
+
if (shadow) markStill(m);
|
|
93
|
+
return m;
|
|
94
|
+
}
|
|
95
|
+
// A geometry of its own over another's attributes and index: the same buffers on the card, and the source's bounds (worked out
|
|
96
|
+
// first, while its arrays are there). For instanced meshes that would share one geometry with instances of their own each: the
|
|
97
|
+
// renderer keeps the binding of a mesh's attributes by geometry and program, so each draw of such a mesh bound every attribute
|
|
98
|
+
// anew and made garbage doing it (hundreds a frame over a land's cover).
|
|
99
|
+
function geometryView(source) {
|
|
100
|
+
if (!source.boundingSphere) source.computeBoundingSphere();
|
|
101
|
+
const view = new THREE.BufferGeometry();
|
|
102
|
+
view.setIndex(source.index);
|
|
103
|
+
for (const name in source.attributes) view.setAttribute(name, source.attributes[name]);
|
|
104
|
+
view.boundingSphere = source.boundingSphere.clone();
|
|
105
|
+
return view;
|
|
106
|
+
}
|
|
107
|
+
const ROOM = 768, // the numbers a builder has room for when it is first filled,
|
|
108
|
+
GROWTH = 2, // and how much more room it takes each time it runs out (doubling copies each number once on average: Aren's pass copied 15.5 million numbers growing by half)
|
|
109
|
+
NO_FLOATS = new Float32Array(0),
|
|
110
|
+
NO_NORMAL_BYTES = new Int8Array(0),
|
|
111
|
+
NO_COLOUR_BYTES = new Uint8Array(0);
|
|
112
|
+
// A typed array like `array` with room for `room` numbers, the first `used` of them carried over.
|
|
113
|
+
function withRoom(array, room, used) {
|
|
114
|
+
const roomier = new array.constructor(room);
|
|
115
|
+
roomier.set(array.subarray(0, used));
|
|
116
|
+
return roomier;
|
|
117
|
+
}
|
|
118
|
+
// 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).
|
|
119
|
+
const normalByte = n => Math.round(n * 127),
|
|
120
|
+
colourByte = c => Math.min(255, Math.round(c * 255)),
|
|
121
|
+
OPAQUE = 255; // a padded colour's fourth byte: an alpha of one
|
|
122
|
+
// The first `count` bytes of three to a vertex, four to a vertex: the three and then `fourth`
|
|
123
|
+
function padded(bytes, count, fourth) {
|
|
124
|
+
const out = new bytes.constructor((count / 3) * 4);
|
|
125
|
+
for (let i = 0, o = 0; i < count; i += 3, o += 4) {
|
|
126
|
+
out[o] = bytes[i];
|
|
127
|
+
out[o + 1] = bytes[i + 1];
|
|
128
|
+
out[o + 2] = bytes[i + 2];
|
|
129
|
+
out[o + 3] = fourth;
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
class GB {
|
|
134
|
+
// merges many coloured parts into one static geometry
|
|
135
|
+
constructor() {
|
|
136
|
+
// Positions, normals and colours fill typed arrays that grow, three numbers to a vertex: four bytes a number where a plain
|
|
137
|
+
// array of numbers takes eight. Normals and colours are kept twice, as floats and as the bytes a compact geometry stores
|
|
138
|
+
// (build), each rounded from the number as it was worked out, so either geometry comes out as if the numbers had been kept whole.
|
|
139
|
+
this.count = 0; // numbers filled so far
|
|
140
|
+
this.indexed = null; // ranges of shared vertices, with their triangles in draw order
|
|
141
|
+
this.positions = this.normals = this.colours = NO_FLOATS;
|
|
142
|
+
this.normalBytes = NO_NORMAL_BYTES;
|
|
143
|
+
this.colourBytes = NO_COLOUR_BYTES;
|
|
144
|
+
this.ox = this.oy = this.oz = 0;
|
|
145
|
+
this.bone = 0;
|
|
146
|
+
this.sk = null;
|
|
147
|
+
this.M = null;
|
|
148
|
+
this.tg = null;
|
|
149
|
+
this.tv = 0;
|
|
150
|
+
this.sk2 = null;
|
|
151
|
+
this.sk3 = null; // a third bone for the few vertices that have one (third)
|
|
152
|
+
this.spent = false; // true once release has let the parts go
|
|
153
|
+
this.wide = false; // true once a colour outside 0..1 or a normal's number outside -1..1 is put in: bytes cannot hold it
|
|
154
|
+
this.skw = null;
|
|
155
|
+
// Set `pb` to [] before filling to have each part's start noted in it (kitCells cuts a town's kit into cells by its parts);
|
|
156
|
+
// `bounds` then holds each part's box, six numbers a part, measured before the positions are rounded to floats.
|
|
157
|
+
this.bounds = [];
|
|
158
|
+
}
|
|
159
|
+
// What has been filled so far, for whoever looks at it: positions, normals and colours, three numbers to a vertex.
|
|
160
|
+
get p() {
|
|
161
|
+
return this.positions.subarray(0, this.count);
|
|
162
|
+
}
|
|
163
|
+
get n() {
|
|
164
|
+
return this.normals.subarray(0, this.count);
|
|
165
|
+
}
|
|
166
|
+
get c() {
|
|
167
|
+
return this.colours.subarray(0, this.count);
|
|
168
|
+
}
|
|
169
|
+
// Makes room for `more` numbers.
|
|
170
|
+
reserve(more) {
|
|
171
|
+
const needed = this.count + more;
|
|
172
|
+
if (needed <= this.positions.length) return;
|
|
173
|
+
const room = Math.max(needed, ROOM, Math.ceil(this.positions.length * GROWTH));
|
|
174
|
+
for (const part of ['positions', 'normals', 'colours', 'normalBytes', 'colourBytes'])
|
|
175
|
+
this[part] = withRoom(this[part], room, this.count);
|
|
176
|
+
}
|
|
177
|
+
// Notes that a part begins here, where parts are noted (pb): its start, and a box with nothing in it yet.
|
|
178
|
+
startPart() {
|
|
179
|
+
this.pb.push(this.count);
|
|
180
|
+
this.bounds.push(1e9, -1e9, 1e9, -1e9, 1e9, -1e9);
|
|
181
|
+
}
|
|
182
|
+
add(geo, col, m) {
|
|
183
|
+
const g = geo.index ? geo.userData.ni || (geo.userData.ni = geo.toNonIndexed()) : geo;
|
|
184
|
+
const pa = g.attributes.position.array,
|
|
185
|
+
na = g.attributes.normal.array,
|
|
186
|
+
numbers = pa.length,
|
|
187
|
+
parts = !!this.pb;
|
|
188
|
+
if (parts) {
|
|
189
|
+
this.startPart();
|
|
190
|
+
this._ra = false;
|
|
191
|
+
}
|
|
192
|
+
if (this.M) m = _m5.multiplyMatrices(this.M, m);
|
|
193
|
+
_n3.getNormalMatrix(m);
|
|
194
|
+
this.reserve(numbers);
|
|
195
|
+
// Three.js's Vector3.applyMatrix4, applyMatrix3 and normalize, written out with the matrices' numbers read once for the
|
|
196
|
+
// part: the same sums in the same order, so every vertex is where Three.js would put it
|
|
197
|
+
const [e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15] = m.elements,
|
|
198
|
+
[f0, f1, f2, f3, f4, f5, f6, f7, f8] = _n3.elements,
|
|
199
|
+
flat = e3 === 0 && e7 === 0 && e11 === 0 && e15 === 1, // no perspective in the matrix: the division Three.js does is by one
|
|
200
|
+
{ ox, oy, oz, positions, normals, colours, normalBytes, colourBytes } = this,
|
|
201
|
+
[r, gr, b] = col,
|
|
202
|
+
rByte = colourByte(r),
|
|
203
|
+
grByte = colourByte(gr),
|
|
204
|
+
bByte = colourByte(b);
|
|
205
|
+
if (!(Math.min(r, gr, b) >= 0 && Math.max(r, gr, b) <= 1)) this.wide = true;
|
|
206
|
+
let k = this.count,
|
|
207
|
+
x0 = 1e9,
|
|
208
|
+
x1 = -1e9,
|
|
209
|
+
y0 = 1e9,
|
|
210
|
+
y1 = -1e9,
|
|
211
|
+
z0 = 1e9,
|
|
212
|
+
z1 = -1e9;
|
|
213
|
+
for (let i = 0; i < numbers; i += 3, k += 3) {
|
|
214
|
+
let x = pa[i],
|
|
215
|
+
y = pa[i + 1],
|
|
216
|
+
z = pa[i + 2];
|
|
217
|
+
const w = flat ? 1 : 1 / (e3 * x + e7 * y + e11 * z + e15),
|
|
218
|
+
px = (e0 * x + e4 * y + e8 * z + e12) * w + ox,
|
|
219
|
+
py = (e1 * x + e5 * y + e9 * z + e13) * w + oy,
|
|
220
|
+
pz = (e2 * x + e6 * y + e10 * z + e14) * w + oz;
|
|
221
|
+
positions[k] = px;
|
|
222
|
+
positions[k + 1] = py;
|
|
223
|
+
positions[k + 2] = pz;
|
|
224
|
+
if (parts) {
|
|
225
|
+
if (px < x0) x0 = px;
|
|
226
|
+
if (px > x1) x1 = px;
|
|
227
|
+
if (py < y0) y0 = py;
|
|
228
|
+
if (py > y1) y1 = py;
|
|
229
|
+
if (pz < z0) z0 = pz;
|
|
230
|
+
if (pz > z1) z1 = pz;
|
|
231
|
+
}
|
|
232
|
+
x = na[i];
|
|
233
|
+
y = na[i + 1];
|
|
234
|
+
z = na[i + 2];
|
|
235
|
+
let nx = f0 * x + f3 * y + f6 * z,
|
|
236
|
+
ny = f1 * x + f4 * y + f7 * z,
|
|
237
|
+
nz = f2 * x + f5 * y + f8 * z;
|
|
238
|
+
const unit = 1 / (Math.sqrt(nx * nx + ny * ny + nz * nz) || 1);
|
|
239
|
+
nx *= unit;
|
|
240
|
+
ny *= unit;
|
|
241
|
+
nz *= unit;
|
|
242
|
+
normals[k] = nx;
|
|
243
|
+
normals[k + 1] = ny;
|
|
244
|
+
normals[k + 2] = nz;
|
|
245
|
+
normalBytes[k] = normalByte(nx);
|
|
246
|
+
normalBytes[k + 1] = normalByte(ny);
|
|
247
|
+
normalBytes[k + 2] = normalByte(nz);
|
|
248
|
+
colours[k] = r;
|
|
249
|
+
colours[k + 1] = gr;
|
|
250
|
+
colours[k + 2] = b;
|
|
251
|
+
colourBytes[k] = rByte;
|
|
252
|
+
colourBytes[k + 1] = grByte;
|
|
253
|
+
colourBytes[k + 2] = bByte;
|
|
254
|
+
}
|
|
255
|
+
this.count = k;
|
|
256
|
+
if (parts) this.bounds.splice(-6, 6, x0, x1, y0, y1, z0, z1);
|
|
257
|
+
const vertices = numbers / 3;
|
|
258
|
+
if (this.tg) {
|
|
259
|
+
const tag = this.tags?.get(col) ?? this.tv; // a builder may say what each of its colours is made of (a person's body: its metal, its leather)
|
|
260
|
+
for (let i = 0; i < vertices; i++) this.tg.push(tag);
|
|
261
|
+
}
|
|
262
|
+
if (this.sk)
|
|
263
|
+
for (let i = 0; i < vertices; i++) {
|
|
264
|
+
this.sk.push(this.bone);
|
|
265
|
+
if (this.sk2) {
|
|
266
|
+
this.sk2.push(this.bone);
|
|
267
|
+
this.skw.push(1);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
box(x, y, z, sx, sy, sz, col, ry = 0, rx = 0, rz = 0) {
|
|
273
|
+
return this.add(box, col, TRS(x, y, z, sx, sy, sz, ry, rx, rz));
|
|
274
|
+
}
|
|
275
|
+
raw(x, y, z, nx, ny, nz, r, g, b, b1 = 0, b2 = 0, w = 1) {
|
|
276
|
+
// one vertex, optionally weighted between two bones
|
|
277
|
+
if (this.M) {
|
|
278
|
+
_v.set(x, y, z).applyMatrix4(this.M);
|
|
279
|
+
x = _v.x;
|
|
280
|
+
y = _v.y;
|
|
281
|
+
z = _v.z;
|
|
282
|
+
_v.set(nx, ny, nz).transformDirection(this.M);
|
|
283
|
+
nx = _v.x;
|
|
284
|
+
ny = _v.y;
|
|
285
|
+
nz = _v.z;
|
|
286
|
+
}
|
|
287
|
+
if (this.pb) {
|
|
288
|
+
// vertices put in one after another are one part
|
|
289
|
+
if (!this._ra) {
|
|
290
|
+
this.startPart();
|
|
291
|
+
this._ra = true;
|
|
292
|
+
}
|
|
293
|
+
const box = this.bounds,
|
|
294
|
+
at = box.length - 6;
|
|
295
|
+
if (x < box[at]) box[at] = x;
|
|
296
|
+
if (x > box[at + 1]) box[at + 1] = x;
|
|
297
|
+
if (y < box[at + 2]) box[at + 2] = y;
|
|
298
|
+
if (y > box[at + 3]) box[at + 3] = y;
|
|
299
|
+
if (z < box[at + 4]) box[at + 4] = z;
|
|
300
|
+
if (z > box[at + 5]) box[at + 5] = z;
|
|
301
|
+
}
|
|
302
|
+
this.reserve(3);
|
|
303
|
+
if (!(Math.min(r, g, b) >= 0 && Math.max(r, g, b) <= 1 && Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) <= 1)) this.wide = true;
|
|
304
|
+
const k = this.count,
|
|
305
|
+
{ positions, normals, colours, normalBytes, colourBytes } = this;
|
|
306
|
+
positions[k] = x;
|
|
307
|
+
positions[k + 1] = y;
|
|
308
|
+
positions[k + 2] = z;
|
|
309
|
+
normals[k] = nx;
|
|
310
|
+
normals[k + 1] = ny;
|
|
311
|
+
normals[k + 2] = nz;
|
|
312
|
+
normalBytes[k] = normalByte(nx);
|
|
313
|
+
normalBytes[k + 1] = normalByte(ny);
|
|
314
|
+
normalBytes[k + 2] = normalByte(nz);
|
|
315
|
+
colours[k] = r;
|
|
316
|
+
colours[k + 1] = g;
|
|
317
|
+
colours[k + 2] = b;
|
|
318
|
+
colourBytes[k] = colourByte(r);
|
|
319
|
+
colourBytes[k + 1] = colourByte(g);
|
|
320
|
+
colourBytes[k + 2] = colourByte(b);
|
|
321
|
+
this.count = k + 3;
|
|
322
|
+
if (this.tg) this.tg.push(this.tv);
|
|
323
|
+
if (this.sk) {
|
|
324
|
+
this.sk.push(b1);
|
|
325
|
+
if (this.sk2) {
|
|
326
|
+
this.sk2.push(b2);
|
|
327
|
+
this.skw.push(w);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
// Gives the vertex added last a third bone, whose share is taken from its second bone's (a face, where jaw, lips and skull meet).
|
|
332
|
+
third(bone, weight) {
|
|
333
|
+
(this.sk3 ||= new Map()).set(this.sk.length - 1, [bone, weight]);
|
|
334
|
+
}
|
|
335
|
+
// The vertices written since `first` form these triangles, numbered locally from zero. Ordinary parts stay implicit.
|
|
336
|
+
triangles(first, corners, end = this.count / 3) {
|
|
337
|
+
(this.indexed ||= []).push({ first, end, corners });
|
|
338
|
+
}
|
|
339
|
+
// Fill the element buffer once the final number of vertices (and hence the index width) is known.
|
|
340
|
+
elementArray() {
|
|
341
|
+
const vertices = this.count / 3;
|
|
342
|
+
let count = vertices;
|
|
343
|
+
for (const { first, end, corners } of this.indexed) count += corners.length - (end - first);
|
|
344
|
+
// WebGL 2 reserves the largest unsigned index for primitive restart.
|
|
345
|
+
const index = vertices <= 65535 ? new Uint16Array(count) : new Uint32Array(count);
|
|
346
|
+
let v = 0,
|
|
347
|
+
at = 0;
|
|
348
|
+
for (const { first, end, corners } of this.indexed) {
|
|
349
|
+
while (v < first) index[at++] = v++;
|
|
350
|
+
for (let i = 0; i < corners.length; i++) index[at++] = first + corners[i];
|
|
351
|
+
v = end;
|
|
352
|
+
}
|
|
353
|
+
while (v < vertices) index[at++] = v++;
|
|
354
|
+
return index;
|
|
355
|
+
}
|
|
356
|
+
// Takes the numbers `from`..`to` of another builder's vertices as they are, with their tags where both keep tags.
|
|
357
|
+
take(other, from, to) {
|
|
358
|
+
const first = this.count / 3;
|
|
359
|
+
this.reserve(to - from);
|
|
360
|
+
for (const part of ['positions', 'normals', 'colours', 'normalBytes', 'colourBytes'])
|
|
361
|
+
this[part].set(other[part].subarray(from, to), this.count);
|
|
362
|
+
this.count += to - from;
|
|
363
|
+
if (other.indexed) {
|
|
364
|
+
const start = from / 3,
|
|
365
|
+
end = to / 3;
|
|
366
|
+
for (const part of other.indexed) {
|
|
367
|
+
const lo = Math.max(start, part.first),
|
|
368
|
+
hi = Math.min(end, part.end);
|
|
369
|
+
if (lo >= hi) continue;
|
|
370
|
+
let corners = part.corners;
|
|
371
|
+
if (lo !== part.first || hi !== part.end) {
|
|
372
|
+
// A partial range can only draw triangles whose three vertices were taken.
|
|
373
|
+
corners = [];
|
|
374
|
+
const skip = lo - part.first,
|
|
375
|
+
limit = hi - lo;
|
|
376
|
+
for (let i = 0; i < part.corners.length; i += 3) {
|
|
377
|
+
const a = part.corners[i] - skip,
|
|
378
|
+
b = part.corners[i + 1] - skip,
|
|
379
|
+
c = part.corners[i + 2] - skip;
|
|
380
|
+
if (a >= 0 && a < limit && b >= 0 && b < limit && c >= 0 && c < limit) corners.push(a, b, c);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
this.triangles(first + lo - start, corners, first + hi - start);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (other.wide) this.wide = true;
|
|
387
|
+
if (this.tg && other.tg) for (let i = from / 3; i < to / 3; i++) this.tg.push(other.tg[i]);
|
|
388
|
+
}
|
|
389
|
+
// A sculptMesh, tinted between two colours by its first channel. cut(dir) leaves out what it is true for; the triangles its edge
|
|
390
|
+
// crosses are clipped where the edge runs (found by halving along each crossed side), so a hairline, beard or hood ends in a
|
|
391
|
+
// smooth line instead of the sphere's triangles.
|
|
392
|
+
sculpt(m, col, col2, cut, k = 1) {
|
|
393
|
+
const { P, N, A, B, D } = m,
|
|
394
|
+
bn = this.bone,
|
|
395
|
+
ox = this.ox,
|
|
396
|
+
oy = this.oy,
|
|
397
|
+
oz = this.oz;
|
|
398
|
+
const emit = v => {
|
|
399
|
+
const a = v[9],
|
|
400
|
+
sh = v[10];
|
|
401
|
+
this.raw(
|
|
402
|
+
v[0] * k + ox,
|
|
403
|
+
v[1] * k + oy,
|
|
404
|
+
v[2] * k + oz,
|
|
405
|
+
v[3],
|
|
406
|
+
v[4],
|
|
407
|
+
v[5],
|
|
408
|
+
(col[0] + (col2[0] - col[0]) * a) * sh,
|
|
409
|
+
(col[1] + (col2[1] - col[1]) * a) * sh,
|
|
410
|
+
(col[2] + (col2[2] - col[2]) * a) * sh,
|
|
411
|
+
bn,
|
|
412
|
+
bn,
|
|
413
|
+
1,
|
|
414
|
+
);
|
|
415
|
+
};
|
|
416
|
+
const vert = i => [
|
|
417
|
+
P[i * 3],
|
|
418
|
+
P[i * 3 + 1],
|
|
419
|
+
P[i * 3 + 2],
|
|
420
|
+
N[i * 3],
|
|
421
|
+
N[i * 3 + 1],
|
|
422
|
+
N[i * 3 + 2],
|
|
423
|
+
D[i * 3],
|
|
424
|
+
D[i * 3 + 1],
|
|
425
|
+
D[i * 3 + 2],
|
|
426
|
+
A[i],
|
|
427
|
+
B[i],
|
|
428
|
+
];
|
|
429
|
+
const mix = (u, v, t) => {
|
|
430
|
+
const r = u.map((x, j) => x + (v[j] - x) * t),
|
|
431
|
+
l = Math.hypot(r[6], r[7], r[8]) || 1,
|
|
432
|
+
ln = Math.hypot(r[3], r[4], r[5]) || 1;
|
|
433
|
+
r[6] /= l;
|
|
434
|
+
r[7] /= l;
|
|
435
|
+
r[8] /= l;
|
|
436
|
+
r[3] /= ln;
|
|
437
|
+
r[4] /= ln;
|
|
438
|
+
r[5] /= ln;
|
|
439
|
+
return r;
|
|
440
|
+
};
|
|
441
|
+
const out = v => !!cut(v[6], v[7], v[8]);
|
|
442
|
+
for (let t = 0; t < m.n; t += 3) {
|
|
443
|
+
const q = t * 3,
|
|
444
|
+
o0 = cut ? !!cut(D[q], D[q + 1], D[q + 2]) : false,
|
|
445
|
+
o1 = cut ? !!cut(D[q + 3], D[q + 4], D[q + 5]) : false,
|
|
446
|
+
o2 = cut ? !!cut(D[q + 6], D[q + 7], D[q + 8]) : false;
|
|
447
|
+
if (o0 && o1 && o2) continue;
|
|
448
|
+
if (!o0 && !o1 && !o2) {
|
|
449
|
+
for (let i = t; i < t + 3; i++) emit(vert(i));
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const tri = [vert(t), vert(t + 1), vert(t + 2)],
|
|
453
|
+
o = [o0, o1, o2],
|
|
454
|
+
poly = [];
|
|
455
|
+
for (let e = 0; e < 3; e++) {
|
|
456
|
+
const a = tri[e],
|
|
457
|
+
b = tri[(e + 1) % 3],
|
|
458
|
+
oa = o[e];
|
|
459
|
+
if (!oa) poly.push(a);
|
|
460
|
+
if (oa !== o[(e + 1) % 3]) {
|
|
461
|
+
let lo = 0,
|
|
462
|
+
hi = 1;
|
|
463
|
+
for (let s = 0; s < 7; s++) {
|
|
464
|
+
const mid = (lo + hi) / 2;
|
|
465
|
+
if (out(mix(a, b, mid)) === oa) lo = mid;
|
|
466
|
+
else hi = mid;
|
|
467
|
+
}
|
|
468
|
+
poly.push(mix(a, b, oa ? hi : lo));
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
for (let i = 1; i < poly.length - 1; i++) {
|
|
472
|
+
emit(poly[0]);
|
|
473
|
+
emit(poly[i]);
|
|
474
|
+
emit(poly[i + 1]);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// What has been filled, as the arrays a geometry is made of (geometryOf), each of its own size: the room to grow is not kept.
|
|
479
|
+
// A worker hands these to the page, which has the geometry made of them. `compact`: byte normals and colours, a third of the
|
|
480
|
+
// memory, for the big generated blocks. `pad` (with compact): four bytes to a normal and a colour, the fourth a normal's 0
|
|
481
|
+
// and a colour's opaque alpha, as the graphics card keeps them: ANGLE on Metal copies a vertex buffer whose stride is not a
|
|
482
|
+
// multiple of four bytes into one that is and keeps both, so a three-byte normal or colour took seven bytes of the card.
|
|
483
|
+
parts(compact, pad = false) {
|
|
484
|
+
if (this.spent) throw new Error('GB: built again after its parts were let go (release)');
|
|
485
|
+
const filled = array => array.slice(0, this.count),
|
|
486
|
+
bytes = (array, fourth) => (pad ? padded(array, this.count, fourth) : filled(array)),
|
|
487
|
+
parts = {
|
|
488
|
+
position: filled(this.positions),
|
|
489
|
+
normal: compact ? bytes(this.normalBytes, 0) : filled(this.normals),
|
|
490
|
+
color: compact ? bytes(this.colourBytes, OPAQUE) : filled(this.colours),
|
|
491
|
+
};
|
|
492
|
+
if (this.indexed) parts.index = this.elementArray();
|
|
493
|
+
if (this.tg) parts.aTag = new Float32Array(this.tg);
|
|
494
|
+
if (this.sk) {
|
|
495
|
+
// rigid skinning: every vertex follows exactly one bone
|
|
496
|
+
const n = this.sk.length,
|
|
497
|
+
si = (parts.skinIndex = new Uint8Array(n * 4)),
|
|
498
|
+
sw = (parts.skinWeight = new Uint8Array(n * 4));
|
|
499
|
+
for (let i = 0; i < n; i++) {
|
|
500
|
+
const w = this.sk2 ? Math.round(this.skw[i] * 255) : 255;
|
|
501
|
+
si[i * 4] = this.sk[i];
|
|
502
|
+
sw[i * 4] = w;
|
|
503
|
+
if (this.sk2) {
|
|
504
|
+
si[i * 4 + 1] = this.sk2[i];
|
|
505
|
+
sw[i * 4 + 1] = 255 - w;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (this.sk3)
|
|
509
|
+
for (const [i, third] of this.sk3) {
|
|
510
|
+
si[i * 4 + 2] = third[0];
|
|
511
|
+
sw[i * 4 + 2] = Math.min(255 - sw[i * 4], Math.round(third[1] * 255));
|
|
512
|
+
sw[i * 4 + 1] = 255 - sw[i * 4] - sw[i * 4 + 2];
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return parts;
|
|
516
|
+
}
|
|
517
|
+
// A geometry of what has been filled: byte normals and colours (a quarter of the floats' memory, and the same numbers to within
|
|
518
|
+
// the bytes' step, as the shaders read them) unless a number was put in that bytes cannot hold.
|
|
519
|
+
build(compact = !this.wide) {
|
|
520
|
+
return geometryOf(this.parts(compact));
|
|
521
|
+
}
|
|
522
|
+
// Lets go of the parts once they have been built into a geometry. A builder that stays in reach afterwards (in a closure of
|
|
523
|
+
// the function that filled it, as in a town's builder) would otherwise hold tens of megabytes of plain numbers for good.
|
|
524
|
+
release() {
|
|
525
|
+
this.positions = this.normals = this.colours = NO_FLOATS;
|
|
526
|
+
this.normalBytes = NO_NORMAL_BYTES;
|
|
527
|
+
this.colourBytes = NO_COLOUR_BYTES;
|
|
528
|
+
this.count = 0;
|
|
529
|
+
this.indexed = null;
|
|
530
|
+
this.bounds = [];
|
|
531
|
+
if (this.tg) this.tg = [];
|
|
532
|
+
if (this.sk) this.sk = [];
|
|
533
|
+
if (this.sk2) this.sk2 = [];
|
|
534
|
+
this.sk3 = null;
|
|
535
|
+
if (this.skw) this.skw = [];
|
|
536
|
+
this.spent = true;
|
|
537
|
+
}
|
|
538
|
+
mesh(mat, parent, shadow = true) {
|
|
539
|
+
const m = stillMesh(this.build(), mat, shadow);
|
|
540
|
+
this.release();
|
|
541
|
+
if (parent) parent.add(m);
|
|
542
|
+
return m;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
export { GB, geometryOf, geometryView, meshOf, releaseArray, sphereOf };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { geometryView } from './builder.js';
|
|
3
|
+
import { markStill } from '../rendering/still-casters.js';
|
|
4
|
+
|
|
5
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
6
|
+
// Copies of one model: the arrays that say where each stands and its tint, filled on a worker of the pool or on the page
|
|
7
|
+
// (instanceArrays, putInstance), and the instanced meshes the page draws from them (instancedMesh, instancedTwin).
|
|
8
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9
|
+
|
|
10
|
+
// The arrays of `n` copies: a matrix and a linear colour to each, filled by putInstance.
|
|
11
|
+
const instanceArrays = n => ({ matrices: new Float32Array(n * 16), colours: new Float32Array(n * 3) });
|
|
12
|
+
// Copy `i` of `arrays`: placed by `matrix` (TRS), tinted (r, g, b), as InstancedMesh.setMatrixAt and setColorAt would write them.
|
|
13
|
+
function putInstance(arrays, i, matrix, r, g, b) {
|
|
14
|
+
matrix.toArray(arrays.matrices, i * 16);
|
|
15
|
+
arrays.colours[i * 3] = r;
|
|
16
|
+
arrays.colours[i * 3 + 1] = g;
|
|
17
|
+
arrays.colours[i * 3 + 2] = b;
|
|
18
|
+
}
|
|
19
|
+
// An instanced mesh of `geometry` drawn at the copies of `arrays`. It is made empty and then given them, so that it does not
|
|
20
|
+
// first write a matrix of its own for every copy only to have it replaced.
|
|
21
|
+
function instancedMesh(geometry, material, { matrices, colours }) {
|
|
22
|
+
const mesh = new THREE.InstancedMesh(geometry, material, 0);
|
|
23
|
+
mesh.instanceMatrix = new THREE.InstancedBufferAttribute(matrices, 16);
|
|
24
|
+
mesh.instanceColor = new THREE.InstancedBufferAttribute(colours, 3);
|
|
25
|
+
mesh.count = matrices.length / 16;
|
|
26
|
+
return mesh;
|
|
27
|
+
}
|
|
28
|
+
// A second model drawn at the copies of `mesh` (its far version), once the mesh's bounds are worked out: it shares the arrays
|
|
29
|
+
// on the graphics card and the bounds.
|
|
30
|
+
function instancedTwin(geometry, material, mesh) {
|
|
31
|
+
const twin = new THREE.InstancedMesh(geometry, material, 0);
|
|
32
|
+
twin.instanceMatrix = mesh.instanceMatrix;
|
|
33
|
+
twin.instanceColor = mesh.instanceColor;
|
|
34
|
+
twin.count = mesh.count;
|
|
35
|
+
twin.boundingSphere = mesh.boundingSphere;
|
|
36
|
+
return twin;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Copies of one unit shape gathered as a builder gathers its parts (add(shape, colour, matrix), as GB's add, at the builder's origin
|
|
40
|
+
// ox, oy, oz), then drawn as one instanced mesh: thousands of small stones that would be one mesh of their own vertices are one
|
|
41
|
+
// shape and a matrix and a colour to each, a fortieth of the bytes to hand to the card, and as many triangles drawn.
|
|
42
|
+
class Copies {
|
|
43
|
+
constructor(shape) {
|
|
44
|
+
this.shape = shape;
|
|
45
|
+
this.ox = this.oy = this.oz = 0;
|
|
46
|
+
this.matrices = [];
|
|
47
|
+
this.colours = [];
|
|
48
|
+
}
|
|
49
|
+
add(shape, col, m) {
|
|
50
|
+
if (shape !== this.shape) throw new Error('Copies hold one shape');
|
|
51
|
+
const e = m.elements;
|
|
52
|
+
this.matrices.push(...e);
|
|
53
|
+
const at = this.matrices.length - 4; // the translation, moved to the builder's origin
|
|
54
|
+
this.matrices[at] += this.ox;
|
|
55
|
+
this.matrices[at + 1] += this.oy;
|
|
56
|
+
this.matrices[at + 2] += this.oz;
|
|
57
|
+
this.colours.push(col[0], col[1], col[2]);
|
|
58
|
+
}
|
|
59
|
+
// The instanced mesh of the copies under `parent`, standing still, casting a shadow unless told not to; the lists are let go.
|
|
60
|
+
mesh(material, parent, shadow = true) {
|
|
61
|
+
const mesh = instancedMesh(geometryView(this.shape), material, {
|
|
62
|
+
matrices: new Float32Array(this.matrices),
|
|
63
|
+
colours: new Float32Array(this.colours),
|
|
64
|
+
});
|
|
65
|
+
mesh.castShadow = shadow;
|
|
66
|
+
mesh.receiveShadow = true;
|
|
67
|
+
mesh.matrixAutoUpdate = false;
|
|
68
|
+
mesh.computeBoundingSphere();
|
|
69
|
+
if (shadow) markStill(mesh);
|
|
70
|
+
this.matrices = this.colours = null;
|
|
71
|
+
if (parent) parent.add(mesh);
|
|
72
|
+
return mesh;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { Copies, instanceArrays, instancedMesh, instancedTwin, putInstance };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// An instance's matrix written straight into an instanced mesh's matrices (16 numbers from `at`), for code that places
|
|
3
|
+
// thousands of instances a frame (crowds, herds, flocks). A placement p says where: at (x, y, z), of size s, turned by ry about
|
|
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
|
+
// Euler angles and quaternion on the way. The placement is an object the caller makes once and fills in for each instance:
|
|
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.
|
|
7
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
8
|
+
const placement = () => ({ x: 0, y: 0, z: 0, s: 1, ry: 0, rx: 0, rz: 0 });
|
|
9
|
+
|
|
10
|
+
// Turned and tipped (rx), not rolled: a person of a crowd, standing or fallen, a beast of a herd.
|
|
11
|
+
function placeAt(matrices, at, p) {
|
|
12
|
+
const s = p.s,
|
|
13
|
+
a = Math.cos(p.rx) * s,
|
|
14
|
+
b = Math.sin(p.rx),
|
|
15
|
+
c = Math.cos(p.ry),
|
|
16
|
+
d = Math.sin(p.ry);
|
|
17
|
+
matrices[at] = c * s;
|
|
18
|
+
matrices[at + 1] = 0;
|
|
19
|
+
matrices[at + 2] = -d * s;
|
|
20
|
+
matrices[at + 3] = 0;
|
|
21
|
+
matrices[at + 4] = d * b * s;
|
|
22
|
+
matrices[at + 5] = a;
|
|
23
|
+
matrices[at + 6] = c * b * s;
|
|
24
|
+
matrices[at + 7] = 0;
|
|
25
|
+
matrices[at + 8] = a * d;
|
|
26
|
+
matrices[at + 9] = -b * s;
|
|
27
|
+
matrices[at + 10] = a * c;
|
|
28
|
+
matrices[at + 11] = 0;
|
|
29
|
+
matrices[at + 12] = p.x;
|
|
30
|
+
matrices[at + 13] = p.y;
|
|
31
|
+
matrices[at + 14] = p.z;
|
|
32
|
+
matrices[at + 15] = 1;
|
|
33
|
+
}
|
|
34
|
+
// Turned, tipped and rolled (rz): a bird banking into its turn.
|
|
35
|
+
function placeBankedAt(matrices, at, p) {
|
|
36
|
+
const s = p.s,
|
|
37
|
+
a = Math.cos(p.rx),
|
|
38
|
+
b = Math.sin(p.rx),
|
|
39
|
+
c = Math.cos(p.ry),
|
|
40
|
+
d = Math.sin(p.ry),
|
|
41
|
+
e = Math.cos(p.rz),
|
|
42
|
+
f = Math.sin(p.rz),
|
|
43
|
+
ce = c * e,
|
|
44
|
+
cf = c * f,
|
|
45
|
+
de = d * e,
|
|
46
|
+
df = d * f;
|
|
47
|
+
matrices[at] = (ce + df * b) * s;
|
|
48
|
+
matrices[at + 1] = a * f * s;
|
|
49
|
+
matrices[at + 2] = (cf * b - de) * s;
|
|
50
|
+
matrices[at + 3] = 0;
|
|
51
|
+
matrices[at + 4] = (de * b - cf) * s;
|
|
52
|
+
matrices[at + 5] = a * e * s;
|
|
53
|
+
matrices[at + 6] = (df + ce * b) * s;
|
|
54
|
+
matrices[at + 7] = 0;
|
|
55
|
+
matrices[at + 8] = a * d * s;
|
|
56
|
+
matrices[at + 9] = -b * s;
|
|
57
|
+
matrices[at + 10] = a * c * s;
|
|
58
|
+
matrices[at + 11] = 0;
|
|
59
|
+
matrices[at + 12] = p.x;
|
|
60
|
+
matrices[at + 13] = p.y;
|
|
61
|
+
matrices[at + 14] = p.z;
|
|
62
|
+
matrices[at + 15] = 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { placeAt, placeBankedAt, placement };
|