@geastack/gea3d 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/LICENSE +674 -0
- package/README.md +84 -2
- package/THIRD_PARTY_NOTICES.md +26 -0
- package/package.json +35 -4
- package/src/index.ts +6 -0
- package/src/native/api.ts +194 -0
- package/src/native/api.web.ts +968 -0
- package/src/native/constants.ts +53 -0
- package/src/native/gea3d_math.h +185 -0
- package/src/native/gea3d_native.cpp +2261 -0
- package/src/native/gea3d_native.h +196 -0
- package/src/native/gea3d_raster.h +1186 -0
- package/src/raster/pipeline.ts +520 -0
- package/src/raster/presenter.ts +25 -0
- package/src/renderer/GeaRenderer.ts +128 -0
- package/src/three/cache.ts +43 -0
- package/src/three/camera.ts +120 -0
- package/src/three/core.ts +1186 -0
- package/src/three/fog.ts +40 -0
- package/src/three/geometries.ts +528 -0
- package/src/three/index.ts +57 -0
- package/src/three/lights.ts +2 -0
- package/src/three/materials.ts +315 -0
- package/src/three/math.ts +1228 -0
- package/src/three/mesh.ts +2 -0
- package/src/three/raycaster.ts +101 -0
|
@@ -0,0 +1,1186 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
// Typed port of the three.js scene-graph core (API-compatible subset).
|
|
3
|
+
//
|
|
4
|
+
// The scene graph is mirrored natively (gea3d_native.cpp, float32): every
|
|
5
|
+
// Object3D owns a native node and syncNative() writes only what changed since
|
|
6
|
+
// the last frame (a transform, a flag, a material), so an unchanged object
|
|
7
|
+
// costs a few comparisons per frame. World matrices, culling and drawing are
|
|
8
|
+
// native; the TS matrices below serve app queries such as getWorldPosition.
|
|
9
|
+
//
|
|
10
|
+
// geatsc-driven design rules (embedded mode, verified against the emitted
|
|
11
|
+
// C++ — see gea3d/README.md "compiler findings"):
|
|
12
|
+
// - NO nullable class-typed fields (`T | null` boxes to gea_cpp_value):
|
|
13
|
+
// sentinels are used instead (parent === this, radius < 0, count === 0).
|
|
14
|
+
// - NO instanceof / downcasts (instanceof lowers to constant false):
|
|
15
|
+
// classification is double-dispatch through virtual methods.
|
|
16
|
+
// - NO interface-typed fields, NO dynamic property bags, NO Symbol protocols.
|
|
17
|
+
// - NO statements before super() in derived constructors (miscompiles).
|
|
18
|
+
|
|
19
|
+
import { ValueCache } from './cache'
|
|
20
|
+
import { Fog } from './fog'
|
|
21
|
+
import { Color, Euler, Matrix4, Quaternion, Vector3 } from './math'
|
|
22
|
+
import { Material } from './materials'
|
|
23
|
+
import {
|
|
24
|
+
bufferDataF32,
|
|
25
|
+
bufferDataU32,
|
|
26
|
+
createBuffer,
|
|
27
|
+
createGeometry,
|
|
28
|
+
createNode,
|
|
29
|
+
deleteBuffer,
|
|
30
|
+
deleteGeometry,
|
|
31
|
+
deleteNode,
|
|
32
|
+
geometryBuffers,
|
|
33
|
+
nodeFlags,
|
|
34
|
+
nodeGeometry,
|
|
35
|
+
nodeInstances,
|
|
36
|
+
nodeLight,
|
|
37
|
+
nodeLightTarget,
|
|
38
|
+
nodeMatrix,
|
|
39
|
+
nodeParent,
|
|
40
|
+
nodeTransform,
|
|
41
|
+
raycast,
|
|
42
|
+
rayResult,
|
|
43
|
+
sceneEnvironment,
|
|
44
|
+
BACKGROUND_FLAT,
|
|
45
|
+
BACKGROUND_GRADIENT,
|
|
46
|
+
BACKGROUND_NONE,
|
|
47
|
+
LIGHT_AMBIENT,
|
|
48
|
+
LIGHT_DIRECTIONAL,
|
|
49
|
+
LIGHT_HEMISPHERE,
|
|
50
|
+
LIGHT_POINT,
|
|
51
|
+
NODE_GROUP,
|
|
52
|
+
NODE_LIGHT,
|
|
53
|
+
NODE_LINE,
|
|
54
|
+
NODE_LINE_SEGMENTS,
|
|
55
|
+
NODE_MESH,
|
|
56
|
+
NODE_POINTS,
|
|
57
|
+
NODE_SPRITE,
|
|
58
|
+
RAY_DISTANCE,
|
|
59
|
+
RAY_FACE,
|
|
60
|
+
RAY_INSTANCE,
|
|
61
|
+
RAY_POINT_X,
|
|
62
|
+
RAY_POINT_Y,
|
|
63
|
+
RAY_POINT_Z,
|
|
64
|
+
} from '../native/api'
|
|
65
|
+
|
|
66
|
+
let _object3DId = 0
|
|
67
|
+
|
|
68
|
+
export class Sphere {
|
|
69
|
+
readonly center: Vector3
|
|
70
|
+
radius: number
|
|
71
|
+
|
|
72
|
+
constructor() {
|
|
73
|
+
this.center = new Vector3()
|
|
74
|
+
this.radius = -1 // < 0 = not computed yet
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class BufferAttribute {
|
|
79
|
+
readonly array: Float32Array
|
|
80
|
+
readonly itemSize: number
|
|
81
|
+
readonly count: number
|
|
82
|
+
// Bumped by `needsUpdate = true`; the renderer re-uploads when it moves.
|
|
83
|
+
version: number
|
|
84
|
+
// Native buffer holding `array` as of `uploadedVersion` (-1 = none).
|
|
85
|
+
nativeBuffer: number
|
|
86
|
+
private uploadedVersion: number
|
|
87
|
+
|
|
88
|
+
constructor(array: Float32Array, itemSize: number) {
|
|
89
|
+
this.array = array
|
|
90
|
+
this.itemSize = itemSize
|
|
91
|
+
this.count = itemSize > 0 ? Math.floor(array.length / itemSize) : 0
|
|
92
|
+
this.version = 0
|
|
93
|
+
this.nativeBuffer = -1
|
|
94
|
+
this.uploadedVersion = -1
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get needsUpdate(): boolean {
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
set needsUpdate(value: boolean) {
|
|
102
|
+
if (value) this.version++
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getX(index: number): number {
|
|
106
|
+
return this.array[index * this.itemSize]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getY(index: number): number {
|
|
110
|
+
return this.array[index * this.itemSize + 1]
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getZ(index: number): number {
|
|
114
|
+
return this.array[index * this.itemSize + 2]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setXYZ(index: number, x: number, y: number, z: number): BufferAttribute {
|
|
118
|
+
const i = index * this.itemSize
|
|
119
|
+
this.array[i] = x
|
|
120
|
+
this.array[i + 1] = y
|
|
121
|
+
this.array[i + 2] = z
|
|
122
|
+
return this
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Uploads the array when it changed; returns its native buffer (-1 when
|
|
126
|
+
// the attribute is empty).
|
|
127
|
+
upload(): number {
|
|
128
|
+
if (this.count === 0) return -1
|
|
129
|
+
|
|
130
|
+
if (this.nativeBuffer < 0) {
|
|
131
|
+
this.nativeBuffer = createBuffer()
|
|
132
|
+
this.uploadedVersion = -1
|
|
133
|
+
}
|
|
134
|
+
if (this.uploadedVersion !== this.version) {
|
|
135
|
+
bufferDataF32(this.nativeBuffer, this.array)
|
|
136
|
+
this.uploadedVersion = this.version
|
|
137
|
+
}
|
|
138
|
+
return this.nativeBuffer
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
releaseNative(): void {
|
|
142
|
+
if (this.nativeBuffer < 0) return
|
|
143
|
+
|
|
144
|
+
deleteBuffer(this.nativeBuffer)
|
|
145
|
+
this.nativeBuffer = -1
|
|
146
|
+
// Geometries holding this attribute see a change.
|
|
147
|
+
this.version++
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function float32ArrayFromNumbers(values: number[]): Float32Array {
|
|
152
|
+
const array = new Float32Array(values.length)
|
|
153
|
+
for (let i = 0; i < values.length; i++) array[i] = values[i]
|
|
154
|
+
return array
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export class Float32BufferAttribute extends BufferAttribute {
|
|
158
|
+
constructor(values: number[], itemSize: number) {
|
|
159
|
+
// No statements before super() — geatsc miscompiles them.
|
|
160
|
+
super(float32ArrayFromNumbers(values), itemSize)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// InstancedMesh per-instance data (matrices: itemSize 16, colours: 3).
|
|
165
|
+
export class InstancedBufferAttribute extends BufferAttribute {}
|
|
166
|
+
|
|
167
|
+
// Fixed-field attribute container. Attributes are never null: "absent" is an
|
|
168
|
+
// empty attribute (count === 0).
|
|
169
|
+
export class GeometryAttributes {
|
|
170
|
+
position: BufferAttribute
|
|
171
|
+
normal: BufferAttribute
|
|
172
|
+
color: BufferAttribute
|
|
173
|
+
uv: BufferAttribute
|
|
174
|
+
|
|
175
|
+
constructor() {
|
|
176
|
+
this.position = new BufferAttribute(new Float32Array(0), 3)
|
|
177
|
+
this.normal = new BufferAttribute(new Float32Array(0), 3)
|
|
178
|
+
this.color = new BufferAttribute(new Float32Array(0), 3)
|
|
179
|
+
this.uv = new BufferAttribute(new Float32Array(0), 2)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class BufferGeometry {
|
|
184
|
+
readonly attributes: GeometryAttributes
|
|
185
|
+
readonly boundingSphere: Sphere
|
|
186
|
+
// Empty = non-indexed (sequential triangles over position).
|
|
187
|
+
private indexArray: Uint32Array
|
|
188
|
+
private indexVersion: number
|
|
189
|
+
private uploadedIndexVersion: number
|
|
190
|
+
// Native ids (-1 = not uploaded / absent). Plain numeric sentinels —
|
|
191
|
+
// nullable or object-typed fields box in embedded geatsc.
|
|
192
|
+
nativeGeometry: number
|
|
193
|
+
nativePositionBuffer: number
|
|
194
|
+
nativeIndexBuffer: number
|
|
195
|
+
nativeColorBuffer: number
|
|
196
|
+
private nativeNormalBuffer: number
|
|
197
|
+
// Bumped when an attribute is replaced; with the attribute and index
|
|
198
|
+
// versions it tells whether anything changed since the last sync.
|
|
199
|
+
private attributeSets: number
|
|
200
|
+
private syncedState: number
|
|
201
|
+
|
|
202
|
+
constructor() {
|
|
203
|
+
this.attributes = new GeometryAttributes()
|
|
204
|
+
this.boundingSphere = new Sphere()
|
|
205
|
+
this.indexArray = new Uint32Array(0)
|
|
206
|
+
this.indexVersion = 0
|
|
207
|
+
this.uploadedIndexVersion = -1
|
|
208
|
+
this.nativeGeometry = -1
|
|
209
|
+
this.nativePositionBuffer = -1
|
|
210
|
+
this.nativeIndexBuffer = -1
|
|
211
|
+
this.nativeColorBuffer = -1
|
|
212
|
+
this.nativeNormalBuffer = -1
|
|
213
|
+
this.attributeSets = 0
|
|
214
|
+
this.syncedState = -1
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
setAttribute(name: string, attribute: BufferAttribute): BufferGeometry {
|
|
218
|
+
if (name === 'position') this.attributes.position = attribute
|
|
219
|
+
else if (name === 'normal') this.attributes.normal = attribute
|
|
220
|
+
else if (name === 'color') this.attributes.color = attribute
|
|
221
|
+
else if (name === 'uv') this.attributes.uv = attribute
|
|
222
|
+
this.boundingSphere.radius = -1
|
|
223
|
+
this.attributeSets++
|
|
224
|
+
return this
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
getAttribute(name: string): BufferAttribute {
|
|
228
|
+
if (name === 'position') return this.attributes.position
|
|
229
|
+
if (name === 'normal') return this.attributes.normal
|
|
230
|
+
if (name === 'color') return this.attributes.color
|
|
231
|
+
return this.attributes.uv
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
setIndex(indices: number[]): BufferGeometry {
|
|
235
|
+
const arr = new Uint32Array(indices.length)
|
|
236
|
+
for (let i = 0; i < indices.length; i++) arr[i] = indices[i]
|
|
237
|
+
this.indexArray = arr
|
|
238
|
+
this.indexVersion++
|
|
239
|
+
return this
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Returns the index array, lazily building sequential indices for
|
|
243
|
+
// non-indexed geometry (cached).
|
|
244
|
+
ensureIndexArray(): Uint32Array {
|
|
245
|
+
if (this.indexArray.length > 0) return this.indexArray
|
|
246
|
+
const count = this.attributes.position.count
|
|
247
|
+
if (count === 0) return this.indexArray
|
|
248
|
+
const arr = new Uint32Array(count)
|
|
249
|
+
for (let i = 0; i < count; i++) arr[i] = i
|
|
250
|
+
this.indexArray = arr
|
|
251
|
+
this.indexVersion++
|
|
252
|
+
return arr
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
triangleCount(): number {
|
|
256
|
+
if (this.indexArray.length > 0) return Math.floor(this.indexArray.length / 3)
|
|
257
|
+
return Math.floor(this.attributes.position.count / 3)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
computeBoundingSphere(): void {
|
|
261
|
+
const sphere = this.boundingSphere
|
|
262
|
+
const pos = this.attributes.position
|
|
263
|
+
if (pos.count === 0) {
|
|
264
|
+
sphere.center.set(0, 0, 0)
|
|
265
|
+
sphere.radius = 0
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
const arr = pos.array
|
|
269
|
+
const n = pos.count
|
|
270
|
+
let minX = arr[0]
|
|
271
|
+
let minY = arr[1]
|
|
272
|
+
let minZ = arr[2]
|
|
273
|
+
let maxX = minX
|
|
274
|
+
let maxY = minY
|
|
275
|
+
let maxZ = minZ
|
|
276
|
+
for (let i = 1; i < n; i++) {
|
|
277
|
+
const x = arr[i * 3]
|
|
278
|
+
const y = arr[i * 3 + 1]
|
|
279
|
+
const z = arr[i * 3 + 2]
|
|
280
|
+
if (x < minX) minX = x
|
|
281
|
+
if (x > maxX) maxX = x
|
|
282
|
+
if (y < minY) minY = y
|
|
283
|
+
if (y > maxY) maxY = y
|
|
284
|
+
if (z < minZ) minZ = z
|
|
285
|
+
if (z > maxZ) maxZ = z
|
|
286
|
+
}
|
|
287
|
+
const cx = (minX + maxX) * 0.5
|
|
288
|
+
const cy = (minY + maxY) * 0.5
|
|
289
|
+
const cz = (minZ + maxZ) * 0.5
|
|
290
|
+
sphere.center.set(cx, cy, cz)
|
|
291
|
+
let maxDistSq = 0
|
|
292
|
+
for (let i = 0; i < n; i++) {
|
|
293
|
+
const dx = arr[i * 3] - cx
|
|
294
|
+
const dy = arr[i * 3 + 1] - cy
|
|
295
|
+
const dz = arr[i * 3 + 2] - cz
|
|
296
|
+
const distSq = dx * dx + dy * dy + dz * dz
|
|
297
|
+
if (distSq > maxDistSq) maxDistSq = distSq
|
|
298
|
+
}
|
|
299
|
+
sphere.radius = Math.sqrt(maxDistSq)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Uploads changed attributes and returns the native geometry.
|
|
303
|
+
syncNative(): number {
|
|
304
|
+
const attributes = this.attributes
|
|
305
|
+
const state = attributes.position.version + attributes.color.version + attributes.normal.version + this.indexVersion + this.attributeSets
|
|
306
|
+
if (this.nativeGeometry >= 0 && state === this.syncedState) return this.nativeGeometry
|
|
307
|
+
|
|
308
|
+
if (this.nativeGeometry < 0) this.nativeGeometry = createGeometry()
|
|
309
|
+
this.syncedState = state
|
|
310
|
+
|
|
311
|
+
const position = attributes.position.upload()
|
|
312
|
+
const color = attributes.color.upload()
|
|
313
|
+
const normal = attributes.normal.upload()
|
|
314
|
+
const index = this.uploadIndex()
|
|
315
|
+
if (
|
|
316
|
+
position !== this.nativePositionBuffer ||
|
|
317
|
+
index !== this.nativeIndexBuffer ||
|
|
318
|
+
color !== this.nativeColorBuffer ||
|
|
319
|
+
normal !== this.nativeNormalBuffer
|
|
320
|
+
) {
|
|
321
|
+
geometryBuffers(this.nativeGeometry, position, index, color, normal)
|
|
322
|
+
this.nativePositionBuffer = position
|
|
323
|
+
this.nativeIndexBuffer = index
|
|
324
|
+
this.nativeColorBuffer = color
|
|
325
|
+
this.nativeNormalBuffer = normal
|
|
326
|
+
}
|
|
327
|
+
return this.nativeGeometry
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Frees the native copies; the next render uploads again.
|
|
331
|
+
dispose(): void {
|
|
332
|
+
const attributes = this.attributes
|
|
333
|
+
attributes.position.releaseNative()
|
|
334
|
+
attributes.normal.releaseNative()
|
|
335
|
+
attributes.color.releaseNative()
|
|
336
|
+
if (this.nativeIndexBuffer >= 0) deleteBuffer(this.nativeIndexBuffer)
|
|
337
|
+
if (this.nativeGeometry >= 0) deleteGeometry(this.nativeGeometry)
|
|
338
|
+
this.nativeGeometry = -1
|
|
339
|
+
this.nativePositionBuffer = -1
|
|
340
|
+
this.nativeIndexBuffer = -1
|
|
341
|
+
this.nativeColorBuffer = -1
|
|
342
|
+
this.nativeNormalBuffer = -1
|
|
343
|
+
this.uploadedIndexVersion = -1
|
|
344
|
+
this.syncedState = -1
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private uploadIndex(): number {
|
|
348
|
+
if (this.indexArray.length === 0) return -1
|
|
349
|
+
|
|
350
|
+
let buffer = this.nativeIndexBuffer
|
|
351
|
+
if (buffer < 0) {
|
|
352
|
+
buffer = createBuffer()
|
|
353
|
+
this.uploadedIndexVersion = -1
|
|
354
|
+
}
|
|
355
|
+
if (this.uploadedIndexVersion !== this.indexVersion) {
|
|
356
|
+
bufferDataU32(buffer, this.indexArray)
|
|
357
|
+
this.uploadedIndexVersion = this.indexVersion
|
|
358
|
+
}
|
|
359
|
+
return buffer
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// NOTE: no module-scope or lazy scratch singletons (miscompile in embedded
|
|
364
|
+
// geatsc — see math.ts). Cold-path helpers allocate small temporaries.
|
|
365
|
+
|
|
366
|
+
// Typed collection target for a scene walk (kept for apps that walk scenes
|
|
367
|
+
// themselves). Mesh and Light register via collectSelf double dispatch.
|
|
368
|
+
export class SceneCollector {
|
|
369
|
+
readonly meshes: Mesh[]
|
|
370
|
+
meshCount: number
|
|
371
|
+
readonly lights: Light[]
|
|
372
|
+
lightCount: number
|
|
373
|
+
|
|
374
|
+
constructor() {
|
|
375
|
+
this.meshes = []
|
|
376
|
+
this.meshCount = 0
|
|
377
|
+
this.lights = []
|
|
378
|
+
this.lightCount = 0
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
reset(): void {
|
|
382
|
+
this.meshCount = 0
|
|
383
|
+
this.lightCount = 0
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
addMesh(mesh: Mesh): void {
|
|
387
|
+
if (this.meshCount < this.meshes.length) this.meshes[this.meshCount] = mesh
|
|
388
|
+
else this.meshes.push(mesh)
|
|
389
|
+
this.meshCount++
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
addLight(light: Light): void {
|
|
393
|
+
if (this.lightCount < this.lights.length) this.lights[this.lightCount] = light
|
|
394
|
+
else this.lights.push(light)
|
|
395
|
+
this.lightCount++
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// A ray hit, three.js Raycaster style.
|
|
400
|
+
export class Intersection {
|
|
401
|
+
distance: number
|
|
402
|
+
readonly point: Vector3
|
|
403
|
+
object: Object3D
|
|
404
|
+
faceIndex: number
|
|
405
|
+
instanceId: number
|
|
406
|
+
|
|
407
|
+
constructor(object: Object3D) {
|
|
408
|
+
this.distance = 0
|
|
409
|
+
this.point = new Vector3()
|
|
410
|
+
this.object = object
|
|
411
|
+
this.faceIndex = -1
|
|
412
|
+
this.instanceId = -1
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Which view of an Object3D's rotation is current; the other is recomputed
|
|
417
|
+
// on its next read. `version` bumps on every rotation write.
|
|
418
|
+
class RotationState {
|
|
419
|
+
eulerStale: boolean
|
|
420
|
+
quaternionStale: boolean
|
|
421
|
+
version: number
|
|
422
|
+
|
|
423
|
+
constructor() {
|
|
424
|
+
this.eulerStale = false
|
|
425
|
+
this.quaternionStale = false
|
|
426
|
+
this.version = 0
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// What an Object3D last wrote to its native node. Explicit matrices are
|
|
431
|
+
// sent every frame: the native node keeps them only when they changed.
|
|
432
|
+
class NativeNode {
|
|
433
|
+
id: number
|
|
434
|
+
parent: number
|
|
435
|
+
// 0 nothing sent, 1 position/quaternion/scale, 2 explicit matrix
|
|
436
|
+
mode: number
|
|
437
|
+
px: number
|
|
438
|
+
py: number
|
|
439
|
+
pz: number
|
|
440
|
+
sx: number
|
|
441
|
+
sy: number
|
|
442
|
+
sz: number
|
|
443
|
+
rotationVersion: number
|
|
444
|
+
visible: boolean
|
|
445
|
+
frustumCulled: boolean
|
|
446
|
+
renderOrder: number
|
|
447
|
+
|
|
448
|
+
constructor() {
|
|
449
|
+
this.id = -1
|
|
450
|
+
this.parent = -2
|
|
451
|
+
this.mode = 0
|
|
452
|
+
this.px = 0
|
|
453
|
+
this.py = 0
|
|
454
|
+
this.pz = 0
|
|
455
|
+
this.sx = 0
|
|
456
|
+
this.sy = 0
|
|
457
|
+
this.sz = 0
|
|
458
|
+
this.rotationVersion = -1
|
|
459
|
+
this.visible = true
|
|
460
|
+
this.frustumCulled = true
|
|
461
|
+
this.renderOrder = Number.NaN
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
reset(): void {
|
|
465
|
+
this.id = -1
|
|
466
|
+
this.parent = -2
|
|
467
|
+
this.mode = 0
|
|
468
|
+
this.renderOrder = Number.NaN
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
sendMatrix(elements: Float32Array): void {
|
|
472
|
+
nodeMatrix(this.id, elements)
|
|
473
|
+
this.mode = 2
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export class Object3D {
|
|
478
|
+
readonly id: number
|
|
479
|
+
name: string
|
|
480
|
+
readonly position: Vector3
|
|
481
|
+
readonly rotation: Euler
|
|
482
|
+
readonly quaternion: Quaternion
|
|
483
|
+
readonly scale: Vector3
|
|
484
|
+
readonly up: Vector3
|
|
485
|
+
readonly matrix: Matrix4
|
|
486
|
+
readonly matrixWorld: Matrix4
|
|
487
|
+
matrixAutoUpdate: boolean
|
|
488
|
+
matrixWorldNeedsUpdate: boolean
|
|
489
|
+
visible: boolean
|
|
490
|
+
frustumCulled: boolean
|
|
491
|
+
renderOrder: number
|
|
492
|
+
// Sentinel: parent points at self when detached (nullable class fields
|
|
493
|
+
// box). The flag is the truth — object identity comparison (`x === this`)
|
|
494
|
+
// does not survive the gea_cpp_strict_equals lowering on wrapped pointers.
|
|
495
|
+
parent: Object3D
|
|
496
|
+
private parentAttached: boolean
|
|
497
|
+
readonly children: Object3D[]
|
|
498
|
+
isCamera: boolean
|
|
499
|
+
// Native node kind (NODE_* in native/api.ts), set by subclasses.
|
|
500
|
+
nodeKind: number
|
|
501
|
+
readonly nativeNode: NativeNode
|
|
502
|
+
private readonly rotationState: RotationState
|
|
503
|
+
|
|
504
|
+
constructor() {
|
|
505
|
+
_object3DId++
|
|
506
|
+
this.id = _object3DId
|
|
507
|
+
this.name = ''
|
|
508
|
+
this.position = new Vector3()
|
|
509
|
+
this.rotation = new Euler()
|
|
510
|
+
this.quaternion = new Quaternion()
|
|
511
|
+
this.scale = new Vector3(1, 1, 1)
|
|
512
|
+
this.up = new Vector3(0, 1, 0)
|
|
513
|
+
this.matrix = new Matrix4()
|
|
514
|
+
this.matrixWorld = new Matrix4()
|
|
515
|
+
this.matrixAutoUpdate = true
|
|
516
|
+
this.matrixWorldNeedsUpdate = false
|
|
517
|
+
this.visible = true
|
|
518
|
+
this.frustumCulled = true
|
|
519
|
+
this.renderOrder = 0
|
|
520
|
+
this.parent = this
|
|
521
|
+
this.parentAttached = false
|
|
522
|
+
this.children = []
|
|
523
|
+
this.isCamera = false
|
|
524
|
+
this.nodeKind = NODE_GROUP
|
|
525
|
+
this.nativeNode = new NativeNode()
|
|
526
|
+
this.rotationState = new RotationState()
|
|
527
|
+
|
|
528
|
+
// Euler and quaternion stay in sync lazily: a write marks the other view
|
|
529
|
+
// stale, and a read of a stale view recomputes it once.
|
|
530
|
+
const rotation = this.rotation
|
|
531
|
+
const quaternion = this.quaternion
|
|
532
|
+
const state = this.rotationState
|
|
533
|
+
rotation._onChange(() => {
|
|
534
|
+
state.quaternionStale = true
|
|
535
|
+
state.eulerStale = false
|
|
536
|
+
state.version++
|
|
537
|
+
})
|
|
538
|
+
quaternion._onChange(() => {
|
|
539
|
+
state.eulerStale = true
|
|
540
|
+
state.quaternionStale = false
|
|
541
|
+
state.version++
|
|
542
|
+
})
|
|
543
|
+
rotation._onRead(() => {
|
|
544
|
+
if (!state.eulerStale) return
|
|
545
|
+
state.eulerStale = false
|
|
546
|
+
rotation.setFromQuaternion(quaternion, '', false)
|
|
547
|
+
})
|
|
548
|
+
quaternion._onRead(() => {
|
|
549
|
+
if (!state.quaternionStale) return
|
|
550
|
+
state.quaternionStale = false
|
|
551
|
+
quaternion.setFromEuler(rotation, false)
|
|
552
|
+
})
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
hasParent(): boolean {
|
|
556
|
+
return this.parentAttached
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// The top of this object's graph (itself when detached).
|
|
560
|
+
root(): Object3D {
|
|
561
|
+
return this.parentAttached ? this.parent.root() : this
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Internal: writes the parent link from the child's side. `this.parent =
|
|
565
|
+
// <param>` stores a real pointer; `object.parent = this` inside add() nulls
|
|
566
|
+
// out (storing the enclosing `this` into another object's field miscompiles
|
|
567
|
+
// in embedded geatsc — passing `this` as a call argument works).
|
|
568
|
+
attachToParent(parent: Object3D): void {
|
|
569
|
+
this.parent = parent
|
|
570
|
+
this.parentAttached = true
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
add(object: Object3D): Object3D {
|
|
574
|
+
if (object.parentAttached) object.parent.remove(object)
|
|
575
|
+
// Through self(): geatsc emits `std::move` of the const receiver for a
|
|
576
|
+
// bare `this` argument, which ESP-IDF's -Werror=redundant-move rejects.
|
|
577
|
+
object.attachToParent(this.self())
|
|
578
|
+
this.children.push(object)
|
|
579
|
+
return this
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private self(): Object3D {
|
|
583
|
+
return this
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
remove(object: Object3D): Object3D {
|
|
587
|
+
// Identity search by id — Array.indexOf on class instances relies on the
|
|
588
|
+
// same strict-equals lowering that hasParent avoids.
|
|
589
|
+
const children = this.children
|
|
590
|
+
for (let i = 0; i < children.length; i++) {
|
|
591
|
+
if (children[i].id === object.id) {
|
|
592
|
+
object.parent = object
|
|
593
|
+
object.parentAttached = false
|
|
594
|
+
object.releaseNative()
|
|
595
|
+
children.splice(i, 1)
|
|
596
|
+
return this
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return this
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
clear(): Object3D {
|
|
603
|
+
for (let i = 0; i < this.children.length; i++) {
|
|
604
|
+
const child = this.children[i]
|
|
605
|
+
child.parent = child
|
|
606
|
+
child.parentAttached = false
|
|
607
|
+
child.releaseNative()
|
|
608
|
+
}
|
|
609
|
+
this.children.length = 0
|
|
610
|
+
return this
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
traverse(callback: (object: Object3D) => void): void {
|
|
614
|
+
callback(this)
|
|
615
|
+
const children = this.children
|
|
616
|
+
for (let i = 0; i < children.length; i++) children[i].traverse(callback)
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Double-dispatch hook: subclasses that render register themselves.
|
|
620
|
+
collectSelf(collector: SceneCollector): void {
|
|
621
|
+
// base objects contribute nothing
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
collect(collector: SceneCollector): void {
|
|
625
|
+
if (!this.visible) return
|
|
626
|
+
this.collectSelf(collector)
|
|
627
|
+
const children = this.children
|
|
628
|
+
for (let i = 0; i < children.length; i++) children[i].collect(collector)
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
rotateX(angle: number): Object3D {
|
|
632
|
+
return this.rotateOnAxisComponents(1, 0, 0, angle)
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
rotateY(angle: number): Object3D {
|
|
636
|
+
return this.rotateOnAxisComponents(0, 1, 0, angle)
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
rotateZ(angle: number): Object3D {
|
|
640
|
+
return this.rotateOnAxisComponents(0, 0, 1, angle)
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Rotates about a normalized axis in object space.
|
|
644
|
+
rotateOnAxis(axis: Vector3, angle: number): Object3D {
|
|
645
|
+
return this.rotateOnAxisComponents(axis.x, axis.y, axis.z, angle)
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
translateOnAxis(axis: Vector3, distance: number): Object3D {
|
|
649
|
+
return this.translateOnAxisComponents(axis.x, axis.y, axis.z, distance)
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
translateX(distance: number): Object3D {
|
|
653
|
+
return this.translateOnAxisComponents(1, 0, 0, distance)
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
translateY(distance: number): Object3D {
|
|
657
|
+
return this.translateOnAxisComponents(0, 1, 0, distance)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
translateZ(distance: number): Object3D {
|
|
661
|
+
return this.translateOnAxisComponents(0, 0, 1, distance)
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
getWorldPosition(target: Vector3): Vector3 {
|
|
665
|
+
this.updateWorldMatrix(true, false)
|
|
666
|
+
return target.setFromMatrixPosition(this.matrixWorld)
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// Orients the object toward the target point. Cameras look down their -Z;
|
|
670
|
+
// other objects face +Z toward the target (three.js semantics). Unlike
|
|
671
|
+
// three.js this takes numbers only — a `Vector3 | number` union parameter
|
|
672
|
+
// boxes in geatsc; use lookAtTarget(v) for the vector form. No parent-
|
|
673
|
+
// rotation compensation (fine for scene-root cameras).
|
|
674
|
+
lookAt(x: number, y: number = 0, z: number = 0): void {
|
|
675
|
+
let eyeX = this.position.x
|
|
676
|
+
let eyeY = this.position.y
|
|
677
|
+
let eyeZ = this.position.z
|
|
678
|
+
if (this.parentAttached) {
|
|
679
|
+
this.updateWorldMatrix(true, false)
|
|
680
|
+
const we = this.matrixWorld.elements
|
|
681
|
+
eyeX = we[12]
|
|
682
|
+
eyeY = we[13]
|
|
683
|
+
eyeZ = we[14]
|
|
684
|
+
}
|
|
685
|
+
const up = this.up
|
|
686
|
+
if (this.isCamera) this.quaternion.setFromLookAt(eyeX, eyeY, eyeZ, x, y, z, up.x, up.y, up.z)
|
|
687
|
+
else this.quaternion.setFromLookAt(x, y, z, eyeX, eyeY, eyeZ, up.x, up.y, up.z)
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
lookAtTarget(target: Vector3): void {
|
|
691
|
+
this.lookAt(target.x, target.y, target.z)
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
updateMatrix(): void {
|
|
695
|
+
this.matrix.compose(this.position, this.quaternion, this.scale)
|
|
696
|
+
this.matrixWorldNeedsUpdate = true
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
updateMatrixWorld(force: boolean = false): void {
|
|
700
|
+
if (this.matrixAutoUpdate) this.updateMatrix()
|
|
701
|
+
let f = force
|
|
702
|
+
if (this.matrixWorldNeedsUpdate || f) {
|
|
703
|
+
if (this.hasParent()) this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix)
|
|
704
|
+
else this.matrixWorld.copy(this.matrix)
|
|
705
|
+
this.matrixWorldNeedsUpdate = false
|
|
706
|
+
f = true
|
|
707
|
+
}
|
|
708
|
+
const children = this.children
|
|
709
|
+
for (let i = 0; i < children.length; i++) children[i].updateMatrixWorld(f)
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
updateWorldMatrix(updateParents: boolean, updateChildren: boolean): void {
|
|
713
|
+
if (updateParents && this.hasParent()) this.parent.updateWorldMatrix(true, false)
|
|
714
|
+
if (this.matrixAutoUpdate) this.updateMatrix()
|
|
715
|
+
if (this.hasParent()) this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix)
|
|
716
|
+
else this.matrixWorld.copy(this.matrix)
|
|
717
|
+
if (updateChildren) {
|
|
718
|
+
const children = this.children
|
|
719
|
+
for (let i = 0; i < children.length; i++) children[i].updateWorldMatrix(false, true)
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// ---- native mirror
|
|
724
|
+
|
|
725
|
+
// Writes this subtree's changes to its native nodes. `parent` is the
|
|
726
|
+
// native parent node (-1 for a root).
|
|
727
|
+
syncNative(parent: number): void {
|
|
728
|
+
const node = this.nativeNode
|
|
729
|
+
if (node.id < 0) node.id = createNode(this.nodeKind)
|
|
730
|
+
if (node.parent !== parent) {
|
|
731
|
+
nodeParent(node.id, parent)
|
|
732
|
+
node.parent = parent
|
|
733
|
+
}
|
|
734
|
+
if (node.visible !== this.visible || node.frustumCulled !== this.frustumCulled || node.renderOrder !== this.renderOrder) {
|
|
735
|
+
nodeFlags(node.id, this.visible ? 1 : 0, this.frustumCulled ? 1 : 0, this.renderOrder)
|
|
736
|
+
node.visible = this.visible
|
|
737
|
+
node.frustumCulled = this.frustumCulled
|
|
738
|
+
node.renderOrder = this.renderOrder
|
|
739
|
+
}
|
|
740
|
+
if (!this.visible) return
|
|
741
|
+
|
|
742
|
+
this.syncTransform(node)
|
|
743
|
+
this.syncSelf()
|
|
744
|
+
const children = this.children
|
|
745
|
+
for (let i = 0; i < children.length; i++) children[i].syncNative(node.id)
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Double-dispatch hook: subclasses write their own native state.
|
|
749
|
+
syncSelf(): void {
|
|
750
|
+
// base objects carry only a transform
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
// Frees this subtree's native nodes; a later sync recreates them.
|
|
754
|
+
releaseNative(): void {
|
|
755
|
+
const node = this.nativeNode
|
|
756
|
+
if (node.id >= 0) deleteNode(node.id)
|
|
757
|
+
node.reset()
|
|
758
|
+
this.releaseSelf()
|
|
759
|
+
const children = this.children
|
|
760
|
+
for (let i = 0; i < children.length; i++) children[i].releaseNative()
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// Double-dispatch hook: subclasses forget per-node native bindings.
|
|
764
|
+
releaseSelf(): void {
|
|
765
|
+
// nothing bound
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// Double-dispatch hook for Raycaster: objects that can be hit append
|
|
769
|
+
// their nearest hit along the world-space ray.
|
|
770
|
+
raycastSelf(origin: Vector3, direction: Vector3, near: number, far: number, intersects: Intersection[]): void {
|
|
771
|
+
// base objects are never hit
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
private syncTransform(node: NativeNode): void {
|
|
775
|
+
if (!this.matrixAutoUpdate) {
|
|
776
|
+
node.sendMatrix(this.matrix.elements)
|
|
777
|
+
return
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
const p = this.position
|
|
781
|
+
const s = this.scale
|
|
782
|
+
const version = this.rotationState.version
|
|
783
|
+
if (
|
|
784
|
+
node.mode === 1 &&
|
|
785
|
+
node.rotationVersion === version &&
|
|
786
|
+
node.px === p.x && node.py === p.y && node.pz === p.z &&
|
|
787
|
+
node.sx === s.x && node.sy === s.y && node.sz === s.z
|
|
788
|
+
) {
|
|
789
|
+
return
|
|
790
|
+
}
|
|
791
|
+
const q = this.quaternion
|
|
792
|
+
nodeTransform(node.id, p.x, p.y, p.z, q.x, q.y, q.z, q.w, s.x, s.y, s.z)
|
|
793
|
+
node.mode = 1
|
|
794
|
+
node.rotationVersion = version
|
|
795
|
+
node.px = p.x
|
|
796
|
+
node.py = p.y
|
|
797
|
+
node.pz = p.z
|
|
798
|
+
node.sx = s.x
|
|
799
|
+
node.sy = s.y
|
|
800
|
+
node.sz = s.z
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
private rotateOnAxisComponents(x: number, y: number, z: number, angle: number): Object3D {
|
|
804
|
+
const half = angle / 2
|
|
805
|
+
const s = Math.sin(half)
|
|
806
|
+
this.quaternion.multiplyComponents(x * s, y * s, z * s, Math.cos(half))
|
|
807
|
+
return this
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// Moves along an object-space axis: the axis rotated by the quaternion.
|
|
811
|
+
private translateOnAxisComponents(x: number, y: number, z: number, distance: number): Object3D {
|
|
812
|
+
const q = this.quaternion
|
|
813
|
+
const qx = q.x
|
|
814
|
+
const qy = q.y
|
|
815
|
+
const qz = q.z
|
|
816
|
+
const qw = q.w
|
|
817
|
+
const tx = 2 * (qy * z - qz * y)
|
|
818
|
+
const ty = 2 * (qz * x - qx * z)
|
|
819
|
+
const tz = 2 * (qx * y - qy * x)
|
|
820
|
+
const p = this.position
|
|
821
|
+
p.x += (x + qw * tx + qy * tz - qz * ty) * distance
|
|
822
|
+
p.y += (y + qw * ty + qz * tx - qx * tz) * distance
|
|
823
|
+
p.z += (z + qw * tz + qx * ty - qy * tx) * distance
|
|
824
|
+
return this
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
export class Group extends Object3D {}
|
|
829
|
+
|
|
830
|
+
// An object drawn from a geometry and a material (Mesh, Line, Points).
|
|
831
|
+
export class Drawable extends Object3D {
|
|
832
|
+
geometry: BufferGeometry
|
|
833
|
+
material: Material
|
|
834
|
+
private boundGeometry: number
|
|
835
|
+
private boundMaterial: number
|
|
836
|
+
|
|
837
|
+
constructor(geometry: BufferGeometry, material: Material) {
|
|
838
|
+
super()
|
|
839
|
+
this.geometry = geometry
|
|
840
|
+
this.material = material
|
|
841
|
+
this.boundGeometry = -2
|
|
842
|
+
this.boundMaterial = -2
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
syncSelf(): void {
|
|
846
|
+
const geometry = this.geometry.syncNative()
|
|
847
|
+
const material = this.material.syncNative()
|
|
848
|
+
if (geometry === this.boundGeometry && material === this.boundMaterial) return
|
|
849
|
+
|
|
850
|
+
nodeGeometry(this.nativeNode.id, geometry, material)
|
|
851
|
+
this.boundGeometry = geometry
|
|
852
|
+
this.boundMaterial = material
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
releaseSelf(): void {
|
|
856
|
+
this.boundGeometry = -2
|
|
857
|
+
this.boundMaterial = -2
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
export class Mesh extends Drawable {
|
|
862
|
+
constructor(geometry: BufferGeometry, material: Material) {
|
|
863
|
+
super(geometry, material)
|
|
864
|
+
this.nodeKind = NODE_MESH
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
collectSelf(collector: SceneCollector): void {
|
|
868
|
+
collector.addMesh(this)
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
raycastSelf(origin: Vector3, direction: Vector3, near: number, far: number, intersects: Intersection[]): void {
|
|
872
|
+
const node = this.nativeNode.id
|
|
873
|
+
if (node < 0) return
|
|
874
|
+
if (raycast(node, origin.x, origin.y, origin.z, direction.x, direction.y, direction.z, near, far) === 0) return
|
|
875
|
+
|
|
876
|
+
const hit = new Intersection(this)
|
|
877
|
+
hit.distance = rayResult(RAY_DISTANCE)
|
|
878
|
+
hit.point.set(rayResult(RAY_POINT_X), rayResult(RAY_POINT_Y), rayResult(RAY_POINT_Z))
|
|
879
|
+
hit.faceIndex = rayResult(RAY_FACE)
|
|
880
|
+
hit.instanceId = rayResult(RAY_INSTANCE)
|
|
881
|
+
intersects.push(hit)
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// Many copies of one geometry, each placed by its own matrix (and tinted by
|
|
886
|
+
// an optional per-instance colour), drawn in one native call.
|
|
887
|
+
export class InstancedMesh extends Mesh {
|
|
888
|
+
readonly instanceMatrix: InstancedBufferAttribute
|
|
889
|
+
// Empty until setColorAt is first called.
|
|
890
|
+
instanceColor: InstancedBufferAttribute
|
|
891
|
+
count: number
|
|
892
|
+
private readonly sentInstances: ValueCache
|
|
893
|
+
|
|
894
|
+
constructor(geometry: BufferGeometry, material: Material, count: number) {
|
|
895
|
+
super(geometry, material)
|
|
896
|
+
this.count = Math.max(0, Math.floor(count))
|
|
897
|
+
this.instanceMatrix = new InstancedBufferAttribute(new Float32Array(this.count * 16), 16)
|
|
898
|
+
this.instanceColor = new InstancedBufferAttribute(new Float32Array(0), 3)
|
|
899
|
+
this.sentInstances = new ValueCache(3)
|
|
900
|
+
for (let i = 0; i < this.count; i++) {
|
|
901
|
+
const offset = i * 16
|
|
902
|
+
this.instanceMatrix.array[offset] = 1
|
|
903
|
+
this.instanceMatrix.array[offset + 5] = 1
|
|
904
|
+
this.instanceMatrix.array[offset + 10] = 1
|
|
905
|
+
this.instanceMatrix.array[offset + 15] = 1
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
setMatrixAt(index: number, matrix: Matrix4): void {
|
|
910
|
+
const array = this.instanceMatrix.array
|
|
911
|
+
const e = matrix.elements
|
|
912
|
+
const offset = index * 16
|
|
913
|
+
for (let i = 0; i < 16; i++) array[offset + i] = e[i]
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
getMatrixAt(index: number, matrix: Matrix4): void {
|
|
917
|
+
const array = this.instanceMatrix.array
|
|
918
|
+
const e = matrix.elements
|
|
919
|
+
const offset = index * 16
|
|
920
|
+
for (let i = 0; i < 16; i++) e[i] = array[offset + i]
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
setColorAt(index: number, color: Color): void {
|
|
924
|
+
if (this.instanceColor.count === 0) {
|
|
925
|
+
const colors = new Float32Array(this.instanceMatrix.count * 3)
|
|
926
|
+
for (let i = 0; i < colors.length; i++) colors[i] = 1
|
|
927
|
+
this.instanceColor = new InstancedBufferAttribute(colors, 3)
|
|
928
|
+
}
|
|
929
|
+
const offset = index * 3
|
|
930
|
+
this.instanceColor.array[offset] = color.r
|
|
931
|
+
this.instanceColor.array[offset + 1] = color.g
|
|
932
|
+
this.instanceColor.array[offset + 2] = color.b
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
syncSelf(): void {
|
|
936
|
+
super.syncSelf()
|
|
937
|
+
const matrices = this.instanceMatrix.upload()
|
|
938
|
+
const colors = this.instanceColor.upload()
|
|
939
|
+
const count = Math.min(this.count, this.instanceMatrix.count)
|
|
940
|
+
const sent = this.sentInstances
|
|
941
|
+
sent.begin()
|
|
942
|
+
sent.put(0, matrices)
|
|
943
|
+
sent.put(1, colors)
|
|
944
|
+
sent.put(2, count)
|
|
945
|
+
if (sent.dirty) nodeInstances(this.nativeNode.id, matrices, colors, count)
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
releaseSelf(): void {
|
|
949
|
+
super.releaseSelf()
|
|
950
|
+
this.sentInstances.invalidate()
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// A polyline through consecutive vertices.
|
|
955
|
+
export class Line extends Drawable {
|
|
956
|
+
constructor(geometry: BufferGeometry, material: Material) {
|
|
957
|
+
super(geometry, material)
|
|
958
|
+
this.nodeKind = NODE_LINE
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// Independent segments between vertex pairs.
|
|
963
|
+
export class LineSegments extends Line {
|
|
964
|
+
constructor(geometry: BufferGeometry, material: Material) {
|
|
965
|
+
super(geometry, material)
|
|
966
|
+
this.nodeKind = NODE_LINE_SEGMENTS
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// A square per vertex, `material.size` pixels (or world units with
|
|
971
|
+
// sizeAttenuation) across.
|
|
972
|
+
export class Points extends Drawable {
|
|
973
|
+
constructor(geometry: BufferGeometry, material: Material) {
|
|
974
|
+
super(geometry, material)
|
|
975
|
+
this.nodeKind = NODE_POINTS
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// A camera-facing rectangle of scale.x by scale.y world units (its geometry
|
|
980
|
+
// stays empty).
|
|
981
|
+
export class Sprite extends Drawable {
|
|
982
|
+
constructor(material: Material) {
|
|
983
|
+
super(new BufferGeometry(), material)
|
|
984
|
+
this.nodeKind = NODE_SPRITE
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
export const LIGHT_KIND_AMBIENT = LIGHT_AMBIENT
|
|
989
|
+
export const LIGHT_KIND_DIRECTIONAL = LIGHT_DIRECTIONAL
|
|
990
|
+
export const LIGHT_KIND_HEMISPHERE = LIGHT_HEMISPHERE
|
|
991
|
+
export const LIGHT_KIND_POINT = LIGHT_POINT
|
|
992
|
+
|
|
993
|
+
// A Light's fields as last written to the native engine.
|
|
994
|
+
class SentLight {
|
|
995
|
+
valid: boolean
|
|
996
|
+
kind: number
|
|
997
|
+
r: number
|
|
998
|
+
g: number
|
|
999
|
+
b: number
|
|
1000
|
+
intensity: number
|
|
1001
|
+
groundR: number
|
|
1002
|
+
groundG: number
|
|
1003
|
+
groundB: number
|
|
1004
|
+
distance: number
|
|
1005
|
+
decay: number
|
|
1006
|
+
tx: number
|
|
1007
|
+
ty: number
|
|
1008
|
+
tz: number
|
|
1009
|
+
|
|
1010
|
+
constructor() {
|
|
1011
|
+
this.valid = false
|
|
1012
|
+
this.kind = 0
|
|
1013
|
+
this.r = 0
|
|
1014
|
+
this.g = 0
|
|
1015
|
+
this.b = 0
|
|
1016
|
+
this.intensity = 0
|
|
1017
|
+
this.groundR = 0
|
|
1018
|
+
this.groundG = 0
|
|
1019
|
+
this.groundB = 0
|
|
1020
|
+
this.distance = 0
|
|
1021
|
+
this.decay = 0
|
|
1022
|
+
this.tx = 0
|
|
1023
|
+
this.ty = 0
|
|
1024
|
+
this.tz = 0
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
matchesLight(light: Light): boolean {
|
|
1028
|
+
const c = light.color
|
|
1029
|
+
const g = light.groundColor
|
|
1030
|
+
return (
|
|
1031
|
+
this.valid && this.kind === light.lightKind &&
|
|
1032
|
+
this.r === c.r && this.g === c.g && this.b === c.b && this.intensity === light.intensity &&
|
|
1033
|
+
this.groundR === g.r && this.groundG === g.g && this.groundB === g.b &&
|
|
1034
|
+
this.distance === light.distance && this.decay === light.decay
|
|
1035
|
+
)
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
takeLight(light: Light): void {
|
|
1039
|
+
const c = light.color
|
|
1040
|
+
const g = light.groundColor
|
|
1041
|
+
this.kind = light.lightKind
|
|
1042
|
+
this.r = c.r
|
|
1043
|
+
this.g = c.g
|
|
1044
|
+
this.b = c.b
|
|
1045
|
+
this.intensity = light.intensity
|
|
1046
|
+
this.groundR = g.r
|
|
1047
|
+
this.groundG = g.g
|
|
1048
|
+
this.groundB = g.b
|
|
1049
|
+
this.distance = light.distance
|
|
1050
|
+
this.decay = light.decay
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
export class Light extends Object3D {
|
|
1055
|
+
readonly color: Color
|
|
1056
|
+
intensity: number
|
|
1057
|
+
lightKind: number
|
|
1058
|
+
// DirectionalLight aim point (unused sentinel instance for other kinds).
|
|
1059
|
+
readonly target: Object3D
|
|
1060
|
+
// HemisphereLight ground color (white for other kinds).
|
|
1061
|
+
readonly groundColor: Color
|
|
1062
|
+
// PointLight range (0 = unlimited) and falloff exponent.
|
|
1063
|
+
distance: number
|
|
1064
|
+
decay: number
|
|
1065
|
+
private readonly sent: SentLight
|
|
1066
|
+
|
|
1067
|
+
constructor(color: number = 0xffffff, intensity: number = 1) {
|
|
1068
|
+
super()
|
|
1069
|
+
this.color = new Color(color)
|
|
1070
|
+
this.intensity = intensity
|
|
1071
|
+
this.lightKind = LIGHT_KIND_AMBIENT
|
|
1072
|
+
this.target = new Object3D()
|
|
1073
|
+
this.groundColor = new Color(0xffffff)
|
|
1074
|
+
this.distance = 0
|
|
1075
|
+
this.decay = 2
|
|
1076
|
+
this.nodeKind = NODE_LIGHT
|
|
1077
|
+
this.sent = new SentLight()
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
collectSelf(collector: SceneCollector): void {
|
|
1081
|
+
collector.addLight(this)
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
syncSelf(): void {
|
|
1085
|
+
const node = this.nativeNode.id
|
|
1086
|
+
const c = this.color
|
|
1087
|
+
const g = this.groundColor
|
|
1088
|
+
const sent = this.sent
|
|
1089
|
+
if (!sent.matchesLight(this)) {
|
|
1090
|
+
nodeLight(node, this.lightKind, c.r, c.g, c.b, this.intensity, g.r, g.g, g.b, this.distance, this.decay)
|
|
1091
|
+
sent.takeLight(this)
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
const t = this.target.position
|
|
1095
|
+
if (!sent.valid || sent.tx !== t.x || sent.ty !== t.y || sent.tz !== t.z) {
|
|
1096
|
+
nodeLightTarget(node, t.x, t.y, t.z)
|
|
1097
|
+
sent.tx = t.x
|
|
1098
|
+
sent.ty = t.y
|
|
1099
|
+
sent.tz = t.z
|
|
1100
|
+
}
|
|
1101
|
+
sent.valid = true
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
releaseSelf(): void {
|
|
1105
|
+
this.sent.valid = false
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
export class AmbientLight extends Light {}
|
|
1110
|
+
|
|
1111
|
+
// Shines from `position` toward `target` (three.js semantics). The default
|
|
1112
|
+
// position (0, 1, 0) with target at the origin lights from straight above.
|
|
1113
|
+
export class DirectionalLight extends Light {
|
|
1114
|
+
constructor(color: number = 0xffffff, intensity: number = 1) {
|
|
1115
|
+
super(color, intensity)
|
|
1116
|
+
this.lightKind = LIGHT_KIND_DIRECTIONAL
|
|
1117
|
+
this.position.set(0, 1, 0)
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
// Sky colour from above (along `position`, default up) blending to the
|
|
1122
|
+
// ground colour from below.
|
|
1123
|
+
export class HemisphereLight extends Light {
|
|
1124
|
+
constructor(skyColor: number = 0xffffff, groundColor: number = 0xffffff, intensity: number = 1) {
|
|
1125
|
+
super(skyColor, intensity)
|
|
1126
|
+
this.lightKind = LIGHT_KIND_HEMISPHERE
|
|
1127
|
+
this.groundColor.setHex(groundColor)
|
|
1128
|
+
this.position.set(0, 1, 0)
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// Shines in every direction from `position`, fading with distance.
|
|
1133
|
+
export class PointLight extends Light {
|
|
1134
|
+
constructor(color: number = 0xffffff, intensity: number = 1, distance: number = 0, decay: number = 2) {
|
|
1135
|
+
super(color, intensity)
|
|
1136
|
+
this.lightKind = LIGHT_KIND_POINT
|
|
1137
|
+
this.distance = distance
|
|
1138
|
+
this.decay = decay
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
export class Scene extends Object3D {
|
|
1143
|
+
// Hex colour (like `new Color(hex).getHex()`); -1 = don't clear.
|
|
1144
|
+
backgroundColor: number
|
|
1145
|
+
// With a hex colour here the background is a vertical gradient from
|
|
1146
|
+
// backgroundColor (top) to this (bottom); -1 = flat.
|
|
1147
|
+
backgroundBottom: number
|
|
1148
|
+
// Distance fog toward fog.color; a Fog with far <= near is off.
|
|
1149
|
+
fog: Fog
|
|
1150
|
+
private readonly sentEnvironment: ValueCache
|
|
1151
|
+
|
|
1152
|
+
constructor() {
|
|
1153
|
+
super()
|
|
1154
|
+
this.backgroundColor = 0x000000
|
|
1155
|
+
this.backgroundBottom = -1
|
|
1156
|
+
this.fog = new Fog(0x000000, 0, 0)
|
|
1157
|
+
this.sentEnvironment = new ValueCache(9)
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// Writes the background and fog once the scene has its node (after
|
|
1161
|
+
// syncNative); `clear` = false keeps the pixels under the 3D view.
|
|
1162
|
+
syncEnvironment(clear: boolean): void {
|
|
1163
|
+
let background = BACKGROUND_NONE
|
|
1164
|
+
if (clear && this.backgroundColor >= 0) background = this.backgroundBottom >= 0 ? BACKGROUND_GRADIENT : BACKGROUND_FLAT
|
|
1165
|
+
const fog = this.fog
|
|
1166
|
+
const fogMode = fog.mode()
|
|
1167
|
+
const fogColor = fog.color.getHex()
|
|
1168
|
+
const sent = this.sentEnvironment
|
|
1169
|
+
sent.begin()
|
|
1170
|
+
sent.put(0, background)
|
|
1171
|
+
sent.put(1, this.backgroundColor)
|
|
1172
|
+
sent.put(2, this.backgroundBottom)
|
|
1173
|
+
sent.put(3, fogMode)
|
|
1174
|
+
sent.put(4, fogColor)
|
|
1175
|
+
sent.put(5, fog.near)
|
|
1176
|
+
sent.put(6, fog.far)
|
|
1177
|
+
sent.put(7, fog.density)
|
|
1178
|
+
if (!sent.dirty) return
|
|
1179
|
+
|
|
1180
|
+
sceneEnvironment(this.nativeNode.id, background, this.backgroundColor, this.backgroundBottom, fogMode, fogColor, fog.near, fog.far, fog.density)
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
releaseSelf(): void {
|
|
1184
|
+
this.sentEnvironment.invalidate()
|
|
1185
|
+
}
|
|
1186
|
+
}
|