@1pizzateam/roostr 0.0.0-stage → 0.8.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/dist/roostr.js ADDED
@@ -0,0 +1,3650 @@
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2015 Ludovic CLUBER
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+
24
+ https://github.com/1pizzateam/RoostR.js
25
+ */
26
+
27
+ import { Mat4x3, Vec3, Frustum, Vec2, Utils, Time, Mat4, Ray3, Trigo, Quat, Mat3, Box, Sphere as Sphere$1, Rect } from '@1pizzateam/spock';
28
+ export { Accumulator, Bezier, Box as BoundingBox, Sphere as BoundingSphere, Circ, Frustum, Grid, Mat3, Mat4, Mat4x3, NumArray, Quat, Rand, Ray3 as Ray, Ray2, Rect, RollingAverage, Plane as SpatialPlane, Time, Trigo, Utils, Vec2, Vec3 } from '@1pizzateam/spock';
29
+
30
+ class GLState {
31
+ program = null;
32
+ arrayBuffer = null;
33
+ elementBuffer = null;
34
+ attributeMask = null;
35
+ maxAttributes = 0;
36
+ frameOfProgram = new WeakMap();
37
+ materialOfProgram = new WeakMap();
38
+ materialVersionOfProgram = new WeakMap();
39
+ invalidate() {
40
+ this.program = null;
41
+ this.arrayBuffer = null;
42
+ this.elementBuffer = null;
43
+ this.attributeMask = null;
44
+ }
45
+ reset() {
46
+ this.invalidate();
47
+ this.maxAttributes = 0;
48
+ this.frameOfProgram = new WeakMap();
49
+ this.materialOfProgram = new WeakMap();
50
+ this.materialVersionOfProgram = new WeakMap();
51
+ }
52
+ }
53
+ const states = new WeakMap();
54
+ function getGLState(context) {
55
+ let state = states.get(context);
56
+ if (!state) {
57
+ state = new GLState();
58
+ states.set(context, state);
59
+ }
60
+ return state;
61
+ }
62
+
63
+ class SceneRenderer {
64
+ context;
65
+ blendKey = '';
66
+ blendEnabled = null;
67
+ depthWrite = null;
68
+ constructor(context) {
69
+ this.context = context;
70
+ this.defaultSettings();
71
+ }
72
+ invalidateState() {
73
+ getGLState(this.context).invalidate();
74
+ this.blendKey = '';
75
+ this.blendEnabled = null;
76
+ this.depthWrite = null;
77
+ }
78
+ reset() {
79
+ getGLState(this.context).reset();
80
+ this.blendKey = '';
81
+ this.blendEnabled = null;
82
+ this.depthWrite = null;
83
+ this.defaultSettings();
84
+ }
85
+ defaultSettings() {
86
+ this.setDepthFunc('LEQUAL');
87
+ this.enableDepthTest();
88
+ this.disableBlendMode();
89
+ }
90
+ enable(capability) {
91
+ if (capability === 'BLEND')
92
+ this.blendEnabled = true;
93
+ this.context.enable(this.context[capability]);
94
+ }
95
+ disable(capability) {
96
+ if (capability === 'BLEND')
97
+ this.blendEnabled = false;
98
+ this.context.disable(this.context[capability]);
99
+ }
100
+ setDepthFunc(mode) {
101
+ this.context.depthFunc(this.context[mode]);
102
+ }
103
+ setBlendFunction(sourceFactor, destinationFactor) {
104
+ this.context.blendFunc(this.context[sourceFactor], this.context[destinationFactor]);
105
+ }
106
+ setBlendEquation(mode) {
107
+ this.context.blendEquation(this.context[mode]);
108
+ }
109
+ enableDepthTest() {
110
+ this.enable('DEPTH_TEST');
111
+ this.depthWrite = true;
112
+ this.context.depthMask(true);
113
+ }
114
+ disableDepthTest() {
115
+ this.disable('DEPTH_TEST');
116
+ this.depthWrite = false;
117
+ this.context.depthMask(false);
118
+ }
119
+ enableBlendMode(equation, source, destination) {
120
+ const key = `${equation}|${source}|${destination}`;
121
+ if (key !== this.blendKey) {
122
+ this.setBlendEquation(equation);
123
+ this.setBlendFunction(source, destination);
124
+ this.blendKey = key;
125
+ }
126
+ if (this.blendEnabled !== true)
127
+ this.enable('BLEND');
128
+ }
129
+ disableBlendMode() {
130
+ if (this.blendEnabled !== false)
131
+ this.disable('BLEND');
132
+ }
133
+ setDepthWrite(enabled) {
134
+ if (this.depthWrite === enabled)
135
+ return;
136
+ this.depthWrite = enabled;
137
+ this.context.depthMask(enabled);
138
+ }
139
+ getParameter(parameterName) {
140
+ return this.context.getParameter(parameterName);
141
+ }
142
+ }
143
+
144
+ class SceneGraph {
145
+ model;
146
+ nbModel;
147
+ modelStackTop;
148
+ constructor() {
149
+ this.model = [new Mat4x3()];
150
+ this.model[0].identity();
151
+ this.nbModel = this.model.length;
152
+ this.modelStackTop = 0;
153
+ }
154
+ pushModelMatrix(modelMatrix) {
155
+ this.modelStackTop++;
156
+ if (this.modelStackTop === this.nbModel) {
157
+ this.model.push(new Mat4x3());
158
+ this.nbModel++;
159
+ }
160
+ this.model[this.modelStackTop].copy(modelMatrix);
161
+ }
162
+ popModelMatrix() {
163
+ if (this.modelStackTop > 0)
164
+ this.modelStackTop--;
165
+ }
166
+ getWorldMatrix() {
167
+ return this.model[this.modelStackTop];
168
+ }
169
+ }
170
+
171
+ const scratchAmbient = new Vec3();
172
+ function clearFlatArrays(flatArrays) {
173
+ flatArrays.position.length = 0;
174
+ flatArrays.diffuse.length = 0;
175
+ flatArrays.specular.length = 0;
176
+ flatArrays.constantAttenuation.length = 0;
177
+ flatArrays.linearAttenuation.length = 0;
178
+ flatArrays.quadraticAttenuation.length = 0;
179
+ flatArrays.cutoff.length = 0;
180
+ flatArrays.exponent.length = 0;
181
+ flatArrays.direction.length = 0;
182
+ flatArrays.type.length = 0;
183
+ }
184
+ function ensureLightBuffers(flatArrays, count) {
185
+ if (flatArrays.floatPosition && flatArrays.floatPosition.length === count * 3)
186
+ return;
187
+ flatArrays.floatPosition = new Float32Array(count * 3);
188
+ flatArrays.floatDiffuse = new Float32Array(count * 3);
189
+ flatArrays.floatSpecular = new Float32Array(count * 3);
190
+ flatArrays.floatDirection = new Float32Array(count * 3);
191
+ flatArrays.floatAttenuation = new Float32Array(count * 3);
192
+ flatArrays.floatCutoff = new Float32Array(count);
193
+ flatArrays.floatExponent = new Float32Array(count);
194
+ flatArrays.floatType = new Float32Array(count);
195
+ flatArrays.packedPosition = new Float32Array(count * 4);
196
+ flatArrays.packedDiffuse = new Float32Array(count * 4);
197
+ flatArrays.packedSpecular = new Float32Array(count * 4);
198
+ }
199
+ function pushLight(flatArrays, index, kind, light) {
200
+ const o = index * 3;
201
+ const isSpot = kind === 'spot';
202
+ const isDirectional = kind === 'directional';
203
+ const att = isDirectional ? null : light;
204
+ light.position.toArray(flatArrays.floatPosition, o);
205
+ light.diffuse.toArray(flatArrays.floatDiffuse, o);
206
+ light.specular.toArray(flatArrays.floatSpecular, o);
207
+ if (isDirectional)
208
+ light.direction.toArray(flatArrays.floatDirection, o);
209
+ else if (isSpot)
210
+ light.direction.toArray(flatArrays.floatDirection, o);
211
+ else
212
+ flatArrays.floatDirection.fill(0, o, o + 3);
213
+ const constant = att ? att.constantAttenuation : 1;
214
+ const linear = att ? att.linearAttenuation : 0;
215
+ const quadratic = att ? att.quadraticAttenuation : 0;
216
+ flatArrays.floatAttenuation[o] = constant;
217
+ flatArrays.floatAttenuation[o + 1] = linear;
218
+ flatArrays.floatAttenuation[o + 2] = quadratic;
219
+ flatArrays.floatCutoff[index] = isSpot ? light.cutoff : 0;
220
+ flatArrays.floatExponent[index] = isSpot ? light.exponent : 0;
221
+ flatArrays.floatType[index] = isDirectional ? 0 : isSpot ? 2 : 1;
222
+ const q = index * 4;
223
+ light.position.toArray(flatArrays.packedPosition, q);
224
+ flatArrays.packedPosition[q + 3] = flatArrays.floatType[index];
225
+ light.diffuse.toArray(flatArrays.packedDiffuse, q);
226
+ flatArrays.packedDiffuse[q + 3] = flatArrays.floatCutoff[index];
227
+ light.specular.toArray(flatArrays.packedSpecular, q);
228
+ flatArrays.packedSpecular[q + 3] = flatArrays.floatExponent[index];
229
+ const dir = isDirectional || isSpot ? light.direction : null;
230
+ flatArrays.position.push(light.position.x, light.position.y, light.position.z);
231
+ flatArrays.diffuse.push(light.diffuse.x, light.diffuse.y, light.diffuse.z);
232
+ flatArrays.specular.push(light.specular.x, light.specular.y, light.specular.z);
233
+ if (dir)
234
+ flatArrays.direction.push(dir.x, dir.y, dir.z);
235
+ else
236
+ flatArrays.direction.push(0, 0, 0);
237
+ flatArrays.constantAttenuation.push(constant);
238
+ flatArrays.linearAttenuation.push(linear);
239
+ flatArrays.quadraticAttenuation.push(quadratic);
240
+ flatArrays.cutoff.push(isSpot ? light.cutoff : 0);
241
+ flatArrays.exponent.push(isSpot ? light.exponent : 0);
242
+ flatArrays.type.push(kind);
243
+ }
244
+ function packLights(directionals, points, spots, ambients, flatArrays, maxLights, extraAmbient) {
245
+ clearFlatArrays(flatArrays);
246
+ const total = spots.length + points.length + directionals.length;
247
+ const count = Math.min(total, maxLights);
248
+ ensureLightBuffers(flatArrays, count);
249
+ let index = 0;
250
+ for (let i = 0; i < spots.length && index < count; i++)
251
+ pushLight(flatArrays, index++, 'spot', spots[i]);
252
+ for (let i = 0; i < points.length && index < count; i++)
253
+ pushLight(flatArrays, index++, 'point', points[i]);
254
+ for (let i = 0; i < directionals.length && index < count; i++)
255
+ pushLight(flatArrays, index++, 'directional', directionals[i]);
256
+ const ambient = scratchAmbient.setScalar(0, 0, 0);
257
+ if (extraAmbient)
258
+ ambient.add(extraAmbient);
259
+ for (let i = 0; i < ambients.length; i++)
260
+ ambient.addScaledVector(ambients[i].diffuse, ambients[i].intensity);
261
+ if (!flatArrays.floatAmbient)
262
+ flatArrays.floatAmbient = new Float32Array(3);
263
+ ambient.toArray(flatArrays.floatAmbient);
264
+ flatArrays.ambient = [ambient.x, ambient.y, ambient.z];
265
+ return flatArrays;
266
+ }
267
+
268
+ const MAX_LIGHTS = 8;
269
+ class Lights {
270
+ warnedOverflow = false;
271
+ directionals;
272
+ points;
273
+ spots;
274
+ ambients;
275
+ nbDirectionals;
276
+ nbPoints;
277
+ nbSpots;
278
+ nbAmbients;
279
+ flatArrays;
280
+ constructor() {
281
+ this.directionals = [];
282
+ this.points = [];
283
+ this.spots = [];
284
+ this.ambients = [];
285
+ this.nbDirectionals = 0;
286
+ this.nbPoints = 0;
287
+ this.nbSpots = 0;
288
+ this.nbAmbients = 0;
289
+ this.flatArrays = {
290
+ position: [],
291
+ diffuse: [],
292
+ specular: [],
293
+ constantAttenuation: [],
294
+ linearAttenuation: [],
295
+ quadraticAttenuation: [],
296
+ cutoff: [],
297
+ exponent: [],
298
+ direction: [],
299
+ type: [],
300
+ ambient: [0, 0, 0]
301
+ };
302
+ }
303
+ addLight(light) {
304
+ switch (light.type) {
305
+ case 'directional':
306
+ this.directionals.push(light);
307
+ this.nbDirectionals++;
308
+ break;
309
+ case 'point':
310
+ this.points.push(light);
311
+ this.nbPoints++;
312
+ break;
313
+ case 'spot':
314
+ this.spots.push(light);
315
+ this.nbSpots++;
316
+ break;
317
+ case 'ambient':
318
+ this.ambients.push(light);
319
+ this.nbAmbients++;
320
+ break;
321
+ }
322
+ }
323
+ removeLight(light) {
324
+ let idx;
325
+ switch (light.type) {
326
+ case 'directional':
327
+ idx = this.directionals.indexOf(light);
328
+ if (idx !== -1) {
329
+ this.directionals.splice(idx, 1);
330
+ this.nbDirectionals--;
331
+ return true;
332
+ }
333
+ return false;
334
+ case 'point':
335
+ idx = this.points.indexOf(light);
336
+ if (idx !== -1) {
337
+ this.points.splice(idx, 1);
338
+ this.nbPoints--;
339
+ return true;
340
+ }
341
+ return false;
342
+ case 'spot':
343
+ idx = this.spots.indexOf(light);
344
+ if (idx !== -1) {
345
+ this.spots.splice(idx, 1);
346
+ this.nbSpots--;
347
+ return true;
348
+ }
349
+ return false;
350
+ case 'ambient':
351
+ idx = this.ambients.indexOf(light);
352
+ if (idx !== -1) {
353
+ this.ambients.splice(idx, 1);
354
+ this.nbAmbients--;
355
+ return true;
356
+ }
357
+ return false;
358
+ default:
359
+ return false;
360
+ }
361
+ }
362
+ flatten(extraAmbient) {
363
+ const total = this.nbSpots + this.nbPoints + this.nbDirectionals;
364
+ if (total > MAX_LIGHTS && !this.warnedOverflow) {
365
+ this.warnedOverflow = true;
366
+ console.warn(`Lights: ${total} lights added, only the first ${MAX_LIGHTS} (spots, points, then directionals) are used.`);
367
+ }
368
+ return packLights(this.directionals, this.points, this.spots, this.ambients, this.flatArrays, MAX_LIGHTS, extraAmbient);
369
+ }
370
+ clear() {
371
+ this.directionals = [];
372
+ this.points = [];
373
+ this.spots = [];
374
+ this.ambients = [];
375
+ this.nbDirectionals = 0;
376
+ this.nbPoints = 0;
377
+ this.nbSpots = 0;
378
+ this.nbAmbients = 0;
379
+ clearFlatArrays(this.flatArrays);
380
+ }
381
+ getFlatArray(property) {
382
+ if (this.flatArrays.hasOwnProperty(property))
383
+ return this.flatArrays[property];
384
+ return undefined;
385
+ }
386
+ }
387
+
388
+ function raycastScene(meshes, traversalStamp, ray, precise = false) {
389
+ const hits = [];
390
+ const visit = (mesh) => {
391
+ if (!mesh.isActive())
392
+ return;
393
+ const hit = mesh.raycast(ray, precise);
394
+ if (hit)
395
+ hits.push({ mesh, distance: hit.distance, point: hit.point });
396
+ for (const child of mesh.children)
397
+ visit(child);
398
+ };
399
+ for (const mesh of meshes)
400
+ if (mesh.traversalStamp !== traversalStamp)
401
+ visit(mesh);
402
+ hits.sort((a, b) => a.distance - b.distance);
403
+ return hits;
404
+ }
405
+
406
+ class SceneCollector {
407
+ opaqueList = [];
408
+ blendedList = [];
409
+ scratchFrustum = new Frustum();
410
+ scratchVec = new Vec3();
411
+ collect(meshes, camera, frustum, opaque, blended, stamp) {
412
+ this.opaqueList.length = 0;
413
+ this.blendedList.length = 0;
414
+ const visit = (mesh) => {
415
+ if (!mesh.isActive())
416
+ return;
417
+ const wanted = mesh.blendMode ? blended : opaque;
418
+ if (wanted && !(frustum && !frustum.intersectsSphere(mesh.computeBoundingSphere()))) {
419
+ if (mesh.blendMode) {
420
+ mesh.sortKey = mesh.getWorldPosition(this.scratchVec).getDistance(camera.position, true);
421
+ this.blendedList.push(mesh);
422
+ }
423
+ else
424
+ this.opaqueList.push(mesh);
425
+ }
426
+ for (const child of mesh.children)
427
+ visit(child);
428
+ };
429
+ for (const mesh of meshes)
430
+ if (mesh.traversalStamp !== stamp)
431
+ visit(mesh);
432
+ }
433
+ drawBlended(scene, camera, time, flatLights, frame) {
434
+ const list = this.blendedList;
435
+ if (list.length === 0)
436
+ return 0;
437
+ if (list.length > 1)
438
+ list.sort((a, b) => b.sortKey - a.sortKey);
439
+ const projMatrix = camera.getProjectionMatrix();
440
+ const viewMatrix = camera.getViewMatrix();
441
+ const camPos = camera.position;
442
+ for (const mesh of list) {
443
+ const blend = mesh.blendOptions;
444
+ scene.enableBlendMode(blend.equation, blend.source, blend.destination);
445
+ scene.renderer.setDepthWrite(mesh.depthWrite);
446
+ mesh.renderSelf(projMatrix, viewMatrix, flatLights, time, true, camPos, scene.fog, frame);
447
+ }
448
+ scene.disableBlendMode();
449
+ scene.renderer.setDepthWrite(true);
450
+ return list.length;
451
+ }
452
+ }
453
+
454
+ class Scene {
455
+ meshes;
456
+ nbMeshes;
457
+ lights;
458
+ ambientColor;
459
+ fog;
460
+ enableFrustumCulling;
461
+ context;
462
+ renderer;
463
+ graph;
464
+ collector;
465
+ resolution;
466
+ frame = { id: 0 };
467
+ frameCounter = 0;
468
+ traversalStamp = 0;
469
+ constructor(contextOrOptions) {
470
+ this.meshes = [];
471
+ this.nbMeshes = 0;
472
+ this.lights = new Lights();
473
+ this.ambientColor = new Vec3(0, 0, 0);
474
+ this.fog = null;
475
+ this.enableFrustumCulling = false;
476
+ this.collector = new SceneCollector();
477
+ let ctx = null;
478
+ let opts = null;
479
+ if (contextOrOptions && typeof contextOrOptions.createBuffer === 'function')
480
+ ctx = contextOrOptions;
481
+ else if (contextOrOptions && typeof contextOrOptions === 'object') {
482
+ opts = contextOrOptions;
483
+ if (opts.context)
484
+ ctx = opts.context;
485
+ else if (opts.canvasId)
486
+ ctx = Scene.contextFromCanvas(opts.canvasId);
487
+ if (opts.ambientColor)
488
+ this.setAmbientColor(opts.ambientColor);
489
+ if (opts.fog)
490
+ this.fog = opts.fog;
491
+ if (opts.enableFrustumCulling !== undefined)
492
+ this.enableFrustumCulling = opts.enableFrustumCulling;
493
+ }
494
+ else
495
+ ctx = contextOrOptions;
496
+ if (!ctx)
497
+ throw new Error('Scene requires a WebGL context or a canvasId.');
498
+ this.context = ctx;
499
+ this.resolution = new Vec2(ctx.drawingBufferWidth || 0, ctx.drawingBufferHeight || 0);
500
+ this.renderer = new SceneRenderer(this.context);
501
+ this.graph = new SceneGraph();
502
+ if (opts && this.context) {
503
+ if (opts.viewport) {
504
+ if (opts.viewport instanceof Vec2)
505
+ this.setViewport(opts.viewport);
506
+ else
507
+ this.setViewport(opts.viewport.size, opts.viewport.position);
508
+ }
509
+ if (opts.clearColor)
510
+ this.setClearColor(opts.clearColor);
511
+ if (opts.depthTest !== false && this.context.DEPTH_TEST) {
512
+ this.context.enable(this.context.DEPTH_TEST);
513
+ if (this.context.LEQUAL)
514
+ this.context.depthFunc(this.context.LEQUAL);
515
+ }
516
+ if (opts.cullFace && opts.cullFace !== 'NONE' && this.context.CULL_FACE) {
517
+ this.context.enable(this.context.CULL_FACE);
518
+ const mode = this.context[opts.cullFace] || this.context.BACK;
519
+ if (mode)
520
+ this.context.cullFace(mode);
521
+ }
522
+ if (opts.frontFace && this.context.frontFace) {
523
+ const mode = this.context[opts.frontFace] || this.context.CCW;
524
+ if (mode)
525
+ this.context.frontFace(mode);
526
+ }
527
+ }
528
+ }
529
+ static contextFromCanvas(canvasId) {
530
+ const canvas = typeof canvasId === 'string' ? document.getElementById(canvasId) : canvasId;
531
+ if (!canvas)
532
+ return null;
533
+ return (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'));
534
+ }
535
+ setViewport(size, position) {
536
+ const x = position?.x ?? 0;
537
+ const y = position?.y ?? 0;
538
+ const w = size.x;
539
+ const h = size.y;
540
+ this.resolution.x = w;
541
+ this.resolution.y = h;
542
+ if (this.context && this.context.viewport)
543
+ this.context.viewport(x, y, w, h);
544
+ }
545
+ getSize(target = new Vec2()) {
546
+ target.copy(this.resolution);
547
+ return target;
548
+ }
549
+ setClearColor(color, alpha = 1.0) {
550
+ if (!this.context)
551
+ return;
552
+ this.context.clearColor(Utils.clamp(color.x, 0, 1), Utils.clamp(color.y, 0, 1), Utils.clamp(color.z, 0, 1), Utils.clamp(alpha, 0, 1));
553
+ }
554
+ setSize(size) {
555
+ if (this.context && this.context.canvas) {
556
+ const canvas = this.context.canvas;
557
+ canvas.width = size.x;
558
+ canvas.height = size.y;
559
+ }
560
+ this.setViewport(size);
561
+ }
562
+ clear(color = true, depth = true) {
563
+ if (!this.context)
564
+ return;
565
+ let mask = 0;
566
+ if (color)
567
+ mask |= this.context.COLOR_BUFFER_BIT;
568
+ if (depth)
569
+ mask |= this.context.DEPTH_BUFFER_BIT;
570
+ if (mask)
571
+ this.context.clear(mask);
572
+ }
573
+ addMesh(mesh) {
574
+ if (this.meshes.indexOf(mesh) === -1) {
575
+ this.meshes.push(mesh);
576
+ this.nbMeshes++;
577
+ }
578
+ }
579
+ removeMesh(mesh) {
580
+ const index = this.meshes.indexOf(mesh);
581
+ if (index !== -1) {
582
+ this.meshes.splice(index, 1);
583
+ this.nbMeshes--;
584
+ return true;
585
+ }
586
+ return false;
587
+ }
588
+ addLight(light) {
589
+ this.lights.addLight(light);
590
+ }
591
+ removeLight(light) {
592
+ return this.lights.removeLight(light);
593
+ }
594
+ setAmbientColor(color) {
595
+ this.ambientColor.copy(color);
596
+ return this;
597
+ }
598
+ setFog(color, near, far = 1000) {
599
+ this.fog = {
600
+ color: new Vec3(color.x, color.y, color.z),
601
+ near,
602
+ far
603
+ };
604
+ return this;
605
+ }
606
+ clearFog() {
607
+ this.fog = null;
608
+ return this;
609
+ }
610
+ clearMeshes() {
611
+ this.meshes = [];
612
+ this.nbMeshes = 0;
613
+ }
614
+ clearLights() {
615
+ this.lights.clear();
616
+ }
617
+ restore() {
618
+ this.renderer.reset();
619
+ for (const mesh of this.meshes)
620
+ mesh.restore();
621
+ }
622
+ destroy(destroyMeshes = true) {
623
+ if (destroyMeshes)
624
+ for (const mesh of this.meshes)
625
+ mesh.destroy();
626
+ this.clearMeshes();
627
+ this.clearLights();
628
+ this.clearFog();
629
+ }
630
+ getLightsProperty(property) {
631
+ return this.lights.getFlatArray(property);
632
+ }
633
+ enableBlendMode(equation, source, destination) {
634
+ this.renderer.enableBlendMode(equation, source, destination);
635
+ }
636
+ disableBlendMode() {
637
+ this.renderer.disableBlendMode();
638
+ }
639
+ getRendererBlendMode() {
640
+ return this.renderer.getParameter(this.context.BLEND);
641
+ }
642
+ raycast(ray, precise = false) {
643
+ this.computeWorldMatrices();
644
+ return raycastScene(this.meshes, this.traversalStamp, ray, precise);
645
+ }
646
+ render(camera, time = Time.now()) {
647
+ this.computeWorldMatrices();
648
+ const flatLights = this.lights.flatten(this.ambientColor);
649
+ const frustum = this.enableFrustumCulling
650
+ ? this.collector.scratchFrustum.setFromProjectionViewMatrix(camera.getProjectionViewMatrix())
651
+ : null;
652
+ this.collector.collect(this.meshes, camera, frustum, true, true, this.traversalStamp);
653
+ const frame = this.beginFrame();
654
+ this.disableBlendMode();
655
+ const projMatrix = camera.getProjectionMatrix();
656
+ const viewMatrix = camera.getViewMatrix();
657
+ const camPos = camera.position;
658
+ let renderedCount = 0;
659
+ for (const mesh of this.collector.opaqueList) {
660
+ this.renderer.setDepthWrite(mesh.depthWrite);
661
+ mesh.renderSelf(projMatrix, viewMatrix, flatLights, time, false, camPos, this.fog, frame);
662
+ renderedCount++;
663
+ }
664
+ renderedCount += this.collector.drawBlended(this, camera, time, flatLights, frame);
665
+ this.renderer.setDepthWrite(true);
666
+ return renderedCount;
667
+ }
668
+ markDescendants() {
669
+ const stamp = ++this.traversalStamp;
670
+ const mark = (mesh) => {
671
+ for (const child of mesh.children) {
672
+ child.traversalStamp = stamp;
673
+ mark(child);
674
+ }
675
+ };
676
+ for (const mesh of this.meshes)
677
+ mark(mesh);
678
+ return stamp;
679
+ }
680
+ computeWorldMatrices() {
681
+ const stamp = this.markDescendants();
682
+ for (const mesh of this.meshes)
683
+ if (mesh.traversalStamp !== stamp)
684
+ mesh.computeWorldMatrix(this.graph);
685
+ }
686
+ beginFrame() {
687
+ this.renderer.invalidateState();
688
+ this.frame.id = ++this.frameCounter;
689
+ this.frame.resolution = this.resolution;
690
+ return this.frame;
691
+ }
692
+ renderBlended(camera, time = Time.now(), flatLights = this.lights.flatten(this.ambientColor), frustum = null) {
693
+ this.collector.collect(this.meshes, camera, frustum, false, true, this.markDescendants());
694
+ return this.collector.drawBlended(this, camera, time, flatLights, this.beginFrame());
695
+ }
696
+ }
697
+
698
+ const Shader = {
699
+ create(context, str, type) {
700
+ const shader = context.createShader(context[type]);
701
+ if (!shader)
702
+ return null;
703
+ context.shaderSource(shader, str);
704
+ context.compileShader(shader);
705
+ if (!context.getShaderParameter(shader, context.COMPILE_STATUS)) {
706
+ console.error(`${type} compilation failed:`, context.getShaderInfoLog(shader));
707
+ context.deleteShader(shader);
708
+ return null;
709
+ }
710
+ return shader;
711
+ }
712
+ };
713
+
714
+ const caches = new WeakMap();
715
+ const owners = new WeakMap();
716
+ const Program = {
717
+ create(context, vertexShader, fragmentShader) {
718
+ const program = context.createProgram();
719
+ if (program) {
720
+ const vshader = Shader.create(context, vertexShader, 'VERTEX_SHADER');
721
+ const fshader = Shader.create(context, fragmentShader, 'FRAGMENT_SHADER');
722
+ if (!vshader || !fshader) {
723
+ if (vshader)
724
+ context.deleteShader(vshader);
725
+ if (fshader)
726
+ context.deleteShader(fshader);
727
+ context.deleteProgram(program);
728
+ return null;
729
+ }
730
+ context.attachShader(program, vshader);
731
+ context.attachShader(program, fshader);
732
+ context.linkProgram(program);
733
+ if (!context.getProgramParameter(program, context.LINK_STATUS)) {
734
+ console.error('Program link failed:', context.getProgramInfoLog(program));
735
+ context.deleteShader(vshader);
736
+ context.deleteShader(fshader);
737
+ context.deleteProgram(program);
738
+ return null;
739
+ }
740
+ context.detachShader(program, vshader);
741
+ context.detachShader(program, fshader);
742
+ context.deleteShader(vshader);
743
+ context.deleteShader(fshader);
744
+ }
745
+ return program;
746
+ },
747
+ acquire(context, vertexShader, fragmentShader) {
748
+ let cache = caches.get(context);
749
+ if (!cache) {
750
+ cache = new Map();
751
+ caches.set(context, cache);
752
+ }
753
+ const key = `${vertexShader}\u0000${fragmentShader}`;
754
+ let entry = cache.get(key);
755
+ if (!entry) {
756
+ const program = Program.create(context, vertexShader, fragmentShader);
757
+ if (!program)
758
+ return null;
759
+ entry = { program, key, refs: 0 };
760
+ cache.set(key, entry);
761
+ owners.set(program, entry);
762
+ }
763
+ entry.refs++;
764
+ return entry.program;
765
+ },
766
+ release(context, program) {
767
+ const entry = owners.get(program);
768
+ if (!entry) {
769
+ context.deleteProgram(program);
770
+ return;
771
+ }
772
+ if (--entry.refs > 0)
773
+ return;
774
+ const cache = caches.get(context);
775
+ if (cache?.get(entry.key) === entry)
776
+ cache.delete(entry.key);
777
+ owners.delete(program);
778
+ context.deleteProgram(program);
779
+ },
780
+ sources(program) {
781
+ const entry = owners.get(program);
782
+ if (!entry)
783
+ return null;
784
+ const [vertex, fragment] = entry.key.split('\u0000');
785
+ return { vertex, fragment };
786
+ },
787
+ reset(context) {
788
+ caches.delete(context);
789
+ }
790
+ };
791
+
792
+ class Renderer {
793
+ canvas;
794
+ context;
795
+ isContextLost = false;
796
+ options;
797
+ onContextLostBound;
798
+ onContextRestoredBound;
799
+ constructor(canvasID, options = {}) {
800
+ this.options = options;
801
+ this.onContextLostBound = (e) => {
802
+ e.preventDefault();
803
+ this.isContextLost = true;
804
+ this.options.onContextLost?.();
805
+ };
806
+ this.onContextRestoredBound = () => {
807
+ this.isContextLost = false;
808
+ if (this.context) {
809
+ Program.reset(this.context);
810
+ getGLState(this.context).reset();
811
+ }
812
+ this.defaultSettings();
813
+ this.options.onContextRestored?.();
814
+ };
815
+ this.canvas = typeof canvasID === 'string' ? this.findById(canvasID) : canvasID;
816
+ this.context = null;
817
+ if (this.canvas) {
818
+ const size = options.size ?? new Vec2(1280, 720);
819
+ this.canvas.width = size.x;
820
+ this.canvas.height = size.y;
821
+ const attributes = { alpha: false, ...options.contextAttributes };
822
+ this.context = (this.canvas.getContext("webgl", attributes) ||
823
+ this.canvas.getContext("experimental-webgl", attributes)) || null;
824
+ if (this.context) {
825
+ if (typeof this.canvas.addEventListener === 'function') {
826
+ this.canvas.addEventListener('webglcontextlost', this.onContextLostBound);
827
+ this.canvas.addEventListener('webglcontextrestored', this.onContextRestoredBound);
828
+ }
829
+ this.defaultSettings();
830
+ }
831
+ }
832
+ }
833
+ dispose() {
834
+ if (this.canvas && typeof this.canvas.removeEventListener === 'function') {
835
+ this.canvas.removeEventListener('webglcontextlost', this.onContextLostBound);
836
+ this.canvas.removeEventListener('webglcontextrestored', this.onContextRestoredBound);
837
+ }
838
+ }
839
+ defaultSettings() {
840
+ if (!this.context)
841
+ return;
842
+ this.context.getExtension('OES_standard_derivatives');
843
+ this.setFrontFace('CW');
844
+ this.enable('CULL_FACE');
845
+ this.setCullFace('BACK');
846
+ this.setViewport(new Vec2(this.context.drawingBufferWidth, this.context.drawingBufferHeight));
847
+ this.setClearColor(new Vec3(0.0, 0.0, 0.0), 1.0);
848
+ }
849
+ setFrontFace(mode) {
850
+ if (this.context)
851
+ this.context.frontFace(this.context[mode]);
852
+ }
853
+ enable(capability) {
854
+ if (this.context)
855
+ this.context.enable(this.context[capability]);
856
+ }
857
+ disable(capability) {
858
+ if (this.context)
859
+ this.context.disable(this.context[capability]);
860
+ }
861
+ setCullFace(mode) {
862
+ if (this.context)
863
+ this.context.cullFace(this.context[mode]);
864
+ }
865
+ setViewport(size) {
866
+ if (this.context)
867
+ this.context.viewport(0, 0, size.x, size.y);
868
+ }
869
+ setClearColor(color, alpha = 1.0) {
870
+ if (!this.context)
871
+ return;
872
+ this.context.clearColor(Utils.clamp(color.x, 0, 1), Utils.clamp(color.y, 0, 1), Utils.clamp(color.z, 0, 1), Utils.clamp(alpha, 0, 1));
873
+ }
874
+ clearFrame() {
875
+ if (this.context)
876
+ this.context.clear(this.context.COLOR_BUFFER_BIT | this.context.DEPTH_BUFFER_BIT);
877
+ }
878
+ getContext() {
879
+ return this.context;
880
+ }
881
+ getCanvas() {
882
+ return this.canvas;
883
+ }
884
+ setSize(size) {
885
+ if (this.canvas) {
886
+ this.canvas.width = size.x;
887
+ this.canvas.height = size.y;
888
+ }
889
+ this.setViewport(size);
890
+ }
891
+ getSize(target = new Vec2()) {
892
+ if (this.canvas) {
893
+ target.x = this.canvas.width;
894
+ target.y = this.canvas.height;
895
+ }
896
+ return target;
897
+ }
898
+ findById(id) {
899
+ return document.getElementById(id);
900
+ }
901
+ }
902
+
903
+ class RenderTarget {
904
+ width;
905
+ height;
906
+ context;
907
+ framebuffer;
908
+ texture;
909
+ depthBuffer;
910
+ constructor(size, context) {
911
+ this.width = size.x;
912
+ this.height = size.y;
913
+ this.context = context;
914
+ this.framebuffer = null;
915
+ this.texture = null;
916
+ this.depthBuffer = null;
917
+ this.init();
918
+ }
919
+ restore() {
920
+ this.init();
921
+ }
922
+ init() {
923
+ const gl = this.context;
924
+ this.framebuffer = gl.createFramebuffer();
925
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
926
+ this.texture = gl.createTexture();
927
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
928
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
929
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
930
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
931
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
932
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
933
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
934
+ this.depthBuffer = gl.createRenderbuffer();
935
+ gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthBuffer);
936
+ gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, this.width, this.height);
937
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.depthBuffer);
938
+ gl.bindTexture(gl.TEXTURE_2D, null);
939
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
940
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
941
+ }
942
+ bind() {
943
+ const gl = this.context;
944
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
945
+ gl.viewport(0, 0, this.width, this.height);
946
+ }
947
+ unbind(viewportSize) {
948
+ const gl = this.context;
949
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
950
+ if (viewportSize)
951
+ gl.viewport(0, 0, viewportSize.x, viewportSize.y);
952
+ }
953
+ getSize(target = new Vec2()) {
954
+ target.x = this.width;
955
+ target.y = this.height;
956
+ return target;
957
+ }
958
+ setSize(size) {
959
+ const w = size.x;
960
+ const h = size.y;
961
+ if (this.width === w && this.height === h)
962
+ return;
963
+ this.width = w;
964
+ this.height = h;
965
+ const gl = this.context;
966
+ if (this.texture) {
967
+ gl.bindTexture(gl.TEXTURE_2D, this.texture);
968
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
969
+ gl.bindTexture(gl.TEXTURE_2D, null);
970
+ }
971
+ if (this.depthBuffer) {
972
+ gl.bindRenderbuffer(gl.RENDERBUFFER, this.depthBuffer);
973
+ gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, this.width, this.height);
974
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
975
+ }
976
+ }
977
+ dispose() {
978
+ const gl = this.context;
979
+ if (this.framebuffer) {
980
+ gl.deleteFramebuffer(this.framebuffer);
981
+ this.framebuffer = null;
982
+ }
983
+ if (this.texture) {
984
+ gl.deleteTexture(this.texture);
985
+ this.texture = null;
986
+ }
987
+ if (this.depthBuffer) {
988
+ gl.deleteRenderbuffer(this.depthBuffer);
989
+ this.depthBuffer = null;
990
+ }
991
+ }
992
+ }
993
+
994
+ class Camera {
995
+ viewMatrix;
996
+ projectionMatrix;
997
+ position;
998
+ target;
999
+ up;
1000
+ cachedViewMatrix;
1001
+ cachedProjectionMatrix;
1002
+ projectionViewMatrix = new Mat4();
1003
+ inverseProjectionViewMatrix = new Mat4();
1004
+ isProjectionViewDirty = true;
1005
+ isInverseDirty = true;
1006
+ static _scratchNear = new Vec3();
1007
+ static _scratchFar = new Vec3();
1008
+ static _scratchDir = new Vec3();
1009
+ static _scratchNdc = new Vec3();
1010
+ constructor(position, target, up) {
1011
+ this.viewMatrix = new Mat4x3();
1012
+ this.projectionMatrix = new Mat4();
1013
+ this.viewMatrix.identity();
1014
+ this.projectionMatrix.identity();
1015
+ this.cachedViewMatrix = new Float32Array(16);
1016
+ this.cachedProjectionMatrix = new Float32Array(16);
1017
+ this.viewMatrix.toArray(this.cachedViewMatrix);
1018
+ this.projectionMatrix.toArray(this.cachedProjectionMatrix);
1019
+ this.position = position;
1020
+ this.target = target;
1021
+ this.up = up;
1022
+ }
1023
+ setViewMatrix() {
1024
+ this.viewMatrix.lookAtRH(this.position, this.target, this.up);
1025
+ this.viewMatrix.toArray(this.cachedViewMatrix);
1026
+ this.invalidateProjectionView();
1027
+ }
1028
+ setProjectionMatrix() {
1029
+ this.projectionMatrix.toArray(this.cachedProjectionMatrix);
1030
+ this.invalidateProjectionView();
1031
+ }
1032
+ invalidateProjectionView() {
1033
+ this.isProjectionViewDirty = true;
1034
+ this.isInverseDirty = true;
1035
+ }
1036
+ getProjectionViewMatrix() {
1037
+ if (this.isProjectionViewDirty) {
1038
+ this.projectionViewMatrix.copy(this.projectionMatrix).multiplyAffine(this.viewMatrix);
1039
+ this.isProjectionViewDirty = false;
1040
+ }
1041
+ return this.projectionViewMatrix.toArray();
1042
+ }
1043
+ getInverseProjectionViewMatrix() {
1044
+ if (this.isInverseDirty) {
1045
+ this.getProjectionViewMatrix();
1046
+ this.inverseProjectionViewMatrix.copy(this.projectionViewMatrix).invert();
1047
+ this.isInverseDirty = false;
1048
+ }
1049
+ return this.inverseProjectionViewMatrix;
1050
+ }
1051
+ setPosition(position) {
1052
+ this.position.copy(position);
1053
+ this.setViewMatrix();
1054
+ }
1055
+ setTarget(target) {
1056
+ this.target.copy(target);
1057
+ this.setViewMatrix();
1058
+ }
1059
+ setUp(up) {
1060
+ this.up.copy(up);
1061
+ this.setViewMatrix();
1062
+ }
1063
+ lookAt(target) {
1064
+ this.setTarget(target);
1065
+ }
1066
+ getViewDirection(target = new Vec3()) {
1067
+ return target.subVectors(this.target, this.position).normalize();
1068
+ }
1069
+ getDistance() {
1070
+ return this.position.getDistance(this.target);
1071
+ }
1072
+ dolly(amount) {
1073
+ this.position.lerp(this.target, amount);
1074
+ this.setViewMatrix();
1075
+ }
1076
+ getViewMatrix() {
1077
+ return this.cachedViewMatrix;
1078
+ }
1079
+ getProjectionMatrix() {
1080
+ return this.cachedProjectionMatrix;
1081
+ }
1082
+ getViewMatrixObject() {
1083
+ return this.viewMatrix;
1084
+ }
1085
+ getProjectionMatrixObject() {
1086
+ return this.projectionMatrix;
1087
+ }
1088
+ getRay(ndc, targetRay = new Ray3()) {
1089
+ const inverse = this.getInverseProjectionViewMatrix();
1090
+ const near = inverse.transformPoint(Camera._scratchNdc.setScalar(ndc.x, ndc.y, -1), Camera._scratchNear);
1091
+ const far = inverse.transformPoint(Camera._scratchNdc.setScalar(ndc.x, ndc.y, 1.0), Camera._scratchFar);
1092
+ const dir = Camera._scratchDir.subVectors(far, near).normalize();
1093
+ targetRay.set(near, dir);
1094
+ return targetRay;
1095
+ }
1096
+ }
1097
+
1098
+ class PerspectiveCamera extends Camera {
1099
+ fov;
1100
+ ratio;
1101
+ zNear;
1102
+ zFar;
1103
+ constructor(fov, nearFar = new Vec2(0.1, 1000), context) {
1104
+ super(new Vec3(), new Vec3(), new Vec3(0.0, 1.0, 0.0));
1105
+ this.fov = fov;
1106
+ this.ratio = 0;
1107
+ this.zNear = nearFar.x;
1108
+ this.zFar = nearFar.y;
1109
+ let viewport = null;
1110
+ if (context && typeof context.getParameter === 'function') {
1111
+ const vp = context.getParameter(context.VIEWPORT);
1112
+ if (vp && vp.length >= 4)
1113
+ viewport = new Vec2(vp[2], vp[3]);
1114
+ }
1115
+ this.setProjectionMatrix(viewport);
1116
+ super.setViewMatrix();
1117
+ }
1118
+ setProjectionMatrix(viewport) {
1119
+ if (viewport)
1120
+ this.ratio = viewport.x / Math.max(1, viewport.y);
1121
+ else
1122
+ this.ratio = this.ratio || 1;
1123
+ this.projectionMatrix.perspective(this.fov, this.ratio, this.zNear, this.zFar);
1124
+ super.setProjectionMatrix();
1125
+ }
1126
+ setFov(fov) {
1127
+ this.fov = fov;
1128
+ this.setProjectionMatrix();
1129
+ }
1130
+ setNearFar(nearFar) {
1131
+ this.zNear = nearFar.x;
1132
+ this.zFar = nearFar.y;
1133
+ this.setProjectionMatrix();
1134
+ }
1135
+ getNearFar(target = new Vec2()) {
1136
+ target.x = this.zNear;
1137
+ target.y = this.zFar;
1138
+ return target;
1139
+ }
1140
+ }
1141
+
1142
+ class OrthographicCamera extends Camera {
1143
+ left;
1144
+ right;
1145
+ top;
1146
+ bottom;
1147
+ near;
1148
+ far;
1149
+ constructor(bounds, nearFar = new Vec2(-1e3, 1000)) {
1150
+ super(new Vec3(), new Vec3(), new Vec3(0.0, 1.0, 0.0));
1151
+ this.left = bounds.topLeftCorner.x;
1152
+ this.right = bounds.bottomRightCorner.x;
1153
+ this.bottom = bounds.topLeftCorner.y;
1154
+ this.top = bounds.bottomRightCorner.y;
1155
+ this.near = nearFar.x;
1156
+ this.far = nearFar.y;
1157
+ this.setProjectionMatrix();
1158
+ super.setViewMatrix();
1159
+ }
1160
+ setProjectionMatrix() {
1161
+ this.projectionMatrix.orthographic(this.left, this.right, this.top, this.bottom, this.near, this.far);
1162
+ super.setProjectionMatrix();
1163
+ }
1164
+ setBounds(bounds, nearFar) {
1165
+ this.left = bounds.topLeftCorner.x;
1166
+ this.right = bounds.bottomRightCorner.x;
1167
+ this.bottom = bounds.topLeftCorner.y;
1168
+ this.top = bounds.bottomRightCorner.y;
1169
+ if (nearFar) {
1170
+ this.near = nearFar.x;
1171
+ this.far = nearFar.y;
1172
+ }
1173
+ this.setProjectionMatrix();
1174
+ }
1175
+ getNearFar(target = new Vec2()) {
1176
+ target.x = this.near;
1177
+ target.y = this.far;
1178
+ return target;
1179
+ }
1180
+ }
1181
+
1182
+ class OrbitControls {
1183
+ camera;
1184
+ domElement;
1185
+ distance;
1186
+ targetDistance;
1187
+ azimuth;
1188
+ targetAzimuth;
1189
+ elevation;
1190
+ targetElevation;
1191
+ minDistance;
1192
+ maxDistance;
1193
+ minElevation;
1194
+ maxElevation;
1195
+ rotateSpeed;
1196
+ zoomSpeed;
1197
+ panSpeed;
1198
+ enableDamping;
1199
+ dampingFactor;
1200
+ isDragging;
1201
+ dragMode;
1202
+ previousMouseX;
1203
+ previousMouseY;
1204
+ touchStartDistance;
1205
+ activePointerId = null;
1206
+ appliedPose = new Vec3(NaN, NaN, NaN);
1207
+ appliedTarget = new Vec3(NaN, NaN, NaN);
1208
+ onPointerDownBound;
1209
+ onPointerMoveBound;
1210
+ onPointerUpBound;
1211
+ onWheelBound;
1212
+ onContextMenuBound;
1213
+ onTouchStartBound;
1214
+ onTouchMoveBound;
1215
+ onTouchEndBound;
1216
+ constructor(camera, domElement) {
1217
+ this.camera = camera;
1218
+ this.domElement = domElement;
1219
+ this.minDistance = 0.1;
1220
+ this.maxDistance = 10000;
1221
+ this.minElevation = -Trigo.halfpi + 0.01;
1222
+ this.maxElevation = Trigo.halfpi - 0.01;
1223
+ this.rotateSpeed = 1.0;
1224
+ this.zoomSpeed = 1.0;
1225
+ this.panSpeed = 1.0;
1226
+ this.enableDamping = false;
1227
+ this.dampingFactor = 0.05;
1228
+ this.isDragging = false;
1229
+ this.dragMode = null;
1230
+ this.previousMouseX = 0;
1231
+ this.previousMouseY = 0;
1232
+ this.touchStartDistance = 0;
1233
+ const spherical = OrbitControls._scratchSpherical.subVectors(this.camera.position, this.camera.target).getSpherical(OrbitControls._scratchSpherical);
1234
+ this.distance = spherical.x || 10;
1235
+ this.targetDistance = this.distance;
1236
+ this.azimuth = spherical.y;
1237
+ this.targetAzimuth = this.azimuth;
1238
+ this.elevation = spherical.z;
1239
+ this.targetElevation = this.elevation;
1240
+ this.onPointerDownBound = this.onPointerDown.bind(this);
1241
+ this.onPointerMoveBound = this.onPointerMove.bind(this);
1242
+ this.onPointerUpBound = this.onPointerUp.bind(this);
1243
+ this.onWheelBound = this.onWheel.bind(this);
1244
+ this.onContextMenuBound = (e) => e.preventDefault();
1245
+ this.onTouchStartBound = this.onTouchStart.bind(this);
1246
+ this.onTouchMoveBound = this.onTouchMove.bind(this);
1247
+ this.onTouchEndBound = this.onTouchEnd.bind(this);
1248
+ this.attach();
1249
+ this.update();
1250
+ }
1251
+ attach() {
1252
+ if (!this.domElement || typeof this.domElement.addEventListener !== 'function')
1253
+ return;
1254
+ if (this.domElement.style)
1255
+ this.domElement.style.touchAction = 'none';
1256
+ this.domElement.addEventListener('pointerdown', this.onPointerDownBound);
1257
+ if (typeof window !== 'undefined') {
1258
+ window.addEventListener('pointermove', this.onPointerMoveBound);
1259
+ window.addEventListener('pointerup', this.onPointerUpBound);
1260
+ }
1261
+ this.domElement.addEventListener('wheel', this.onWheelBound, { passive: false });
1262
+ this.domElement.addEventListener('contextmenu', this.onContextMenuBound);
1263
+ this.domElement.addEventListener('touchstart', this.onTouchStartBound, { passive: false });
1264
+ this.domElement.addEventListener('touchmove', this.onTouchMoveBound, { passive: false });
1265
+ this.domElement.addEventListener('touchend', this.onTouchEndBound);
1266
+ this.domElement.addEventListener('touchcancel', this.onTouchEndBound);
1267
+ }
1268
+ dispose() {
1269
+ if (!this.domElement || typeof this.domElement.removeEventListener !== 'function')
1270
+ return;
1271
+ this.domElement.removeEventListener('pointerdown', this.onPointerDownBound);
1272
+ if (typeof window !== 'undefined') {
1273
+ window.removeEventListener('pointermove', this.onPointerMoveBound);
1274
+ window.removeEventListener('pointerup', this.onPointerUpBound);
1275
+ }
1276
+ this.domElement.removeEventListener('wheel', this.onWheelBound);
1277
+ this.domElement.removeEventListener('contextmenu', this.onContextMenuBound);
1278
+ this.domElement.removeEventListener('touchstart', this.onTouchStartBound);
1279
+ this.domElement.removeEventListener('touchmove', this.onTouchMoveBound);
1280
+ this.domElement.removeEventListener('touchend', this.onTouchEndBound);
1281
+ this.domElement.removeEventListener('touchcancel', this.onTouchEndBound);
1282
+ }
1283
+ onPointerDown(e) {
1284
+ if (e.isPrimary === false)
1285
+ return;
1286
+ this.activePointerId = e.pointerId ?? null;
1287
+ this.isDragging = true;
1288
+ this.previousMouseX = e.clientX;
1289
+ this.previousMouseY = e.clientY;
1290
+ if (e.button === 2 || e.shiftKey)
1291
+ this.dragMode = 'pan';
1292
+ else
1293
+ this.dragMode = 'rotate';
1294
+ }
1295
+ onPointerMove(e) {
1296
+ if (!this.isDragging)
1297
+ return;
1298
+ if (this.activePointerId !== null && e.pointerId !== undefined && e.pointerId !== this.activePointerId)
1299
+ return;
1300
+ const deltaX = e.clientX - this.previousMouseX;
1301
+ const deltaY = e.clientY - this.previousMouseY;
1302
+ this.previousMouseX = e.clientX;
1303
+ this.previousMouseY = e.clientY;
1304
+ if (this.dragMode === 'rotate') {
1305
+ const elementHeight = this.domElement.clientHeight || 500;
1306
+ this.targetAzimuth -= (deltaX / elementHeight) * Trigo.pi * this.rotateSpeed;
1307
+ this.targetElevation += (deltaY / elementHeight) * Trigo.pi * this.rotateSpeed;
1308
+ this.targetElevation = Utils.clamp(this.targetElevation, this.minElevation, this.maxElevation);
1309
+ }
1310
+ else if (this.dragMode === 'pan')
1311
+ this.pan(deltaX, deltaY);
1312
+ if (!this.enableDamping) {
1313
+ this.azimuth = this.targetAzimuth;
1314
+ this.elevation = this.targetElevation;
1315
+ this.update();
1316
+ }
1317
+ }
1318
+ onPointerUp() {
1319
+ this.isDragging = false;
1320
+ this.activePointerId = null;
1321
+ this.dragMode = null;
1322
+ }
1323
+ onWheel(e) {
1324
+ e.preventDefault();
1325
+ const factor = e.deltaY > 0 ? 1.1 : (1 / 1.1);
1326
+ this.targetDistance *= Math.pow(factor, this.zoomSpeed);
1327
+ this.targetDistance = Utils.clamp(this.targetDistance, this.minDistance, this.maxDistance);
1328
+ if (!this.enableDamping) {
1329
+ this.distance = this.targetDistance;
1330
+ this.update();
1331
+ }
1332
+ }
1333
+ onTouchStart(e) {
1334
+ if (e.touches.length === 2) {
1335
+ e.preventDefault();
1336
+ this.touchStartDistance = OrbitControls.touchDistance(e.touches);
1337
+ }
1338
+ }
1339
+ onTouchMove(e) {
1340
+ if (e.touches.length === 2) {
1341
+ e.preventDefault();
1342
+ const currentDistance = OrbitControls.touchDistance(e.touches);
1343
+ const factor = this.touchStartDistance / (currentDistance || 1);
1344
+ this.touchStartDistance = currentDistance;
1345
+ this.targetDistance *= factor;
1346
+ this.targetDistance = Utils.clamp(this.targetDistance, this.minDistance, this.maxDistance);
1347
+ if (!this.enableDamping) {
1348
+ this.distance = this.targetDistance;
1349
+ this.update();
1350
+ }
1351
+ }
1352
+ }
1353
+ onTouchEnd(e) {
1354
+ if (e.touches.length < 2)
1355
+ this.touchStartDistance = 0;
1356
+ }
1357
+ static _touchA = new Vec2();
1358
+ static _touchB = new Vec2();
1359
+ static touchDistance(touches) {
1360
+ return OrbitControls._touchA.setScalar(touches[0].clientX, touches[0].clientY)
1361
+ .getDistance(OrbitControls._touchB.setScalar(touches[1].clientX, touches[1].clientY));
1362
+ }
1363
+ static _scratchForward = new Vec3();
1364
+ static _scratchRight = new Vec3();
1365
+ static _scratchUp = new Vec3();
1366
+ static _scratchPan = new Vec3();
1367
+ static _scratchSpherical = new Vec3();
1368
+ pan(deltaX, deltaY) {
1369
+ const factor = (this.distance / 1000) * this.panSpeed;
1370
+ const forward = this.camera.getViewDirection(OrbitControls._scratchForward);
1371
+ const right = OrbitControls._scratchRight.crossVectors(forward, this.camera.up).normalize();
1372
+ const up = OrbitControls._scratchUp.crossVectors(right, forward).normalize();
1373
+ OrbitControls._scratchPan.setScalar(0, 0, 0)
1374
+ .add(right.scale(-deltaX * factor))
1375
+ .add(up.scale(deltaY * factor));
1376
+ this.camera.target.add(OrbitControls._scratchPan);
1377
+ this.camera.position.add(OrbitControls._scratchPan);
1378
+ }
1379
+ update(deltaTime) {
1380
+ if (this.enableDamping) {
1381
+ const t = deltaTime === undefined
1382
+ ? this.dampingFactor
1383
+ : 1 - Math.pow(1 - this.dampingFactor, deltaTime * 60);
1384
+ const turn = Trigo.normalizeRadian(this.targetAzimuth - this.azimuth);
1385
+ this.azimuth = Utils.lerp(this.azimuth, this.azimuth + turn, t);
1386
+ this.elevation = Utils.lerp(this.elevation, this.targetElevation, t);
1387
+ this.distance = Utils.lerp(this.distance, this.targetDistance, t);
1388
+ }
1389
+ const target = this.camera.target;
1390
+ const pose = OrbitControls._scratchSpherical.setScalar(this.distance, this.azimuth, this.elevation);
1391
+ if (pose.equals(this.appliedPose) && target.equals(this.appliedTarget))
1392
+ return;
1393
+ this.appliedPose.copy(pose);
1394
+ this.appliedTarget.copy(target);
1395
+ this.camera.position.setFromSpherical(this.distance, this.azimuth, this.elevation).add(target);
1396
+ this.camera.setViewMatrix();
1397
+ }
1398
+ }
1399
+
1400
+ class Node {
1401
+ position;
1402
+ scale;
1403
+ quaternion;
1404
+ modelMatrix;
1405
+ worldMatrix;
1406
+ normalMatrix;
1407
+ boundingBox;
1408
+ boundingSphere;
1409
+ localBoundingBox;
1410
+ localBoundingSphere;
1411
+ parent = null;
1412
+ children = [];
1413
+ traversalStamp = 0;
1414
+ isBoundsDirty = true;
1415
+ isNormalMatrixDirty = true;
1416
+ isWorldDirty = true;
1417
+ _rotation;
1418
+ isRotationStale = false;
1419
+ isTransformDirty = false;
1420
+ static UP = new Vec3(0, 1, 0);
1421
+ static scratchQuat = new Quat();
1422
+ static scratchVec = new Vec3();
1423
+ constructor() {
1424
+ this.position = new Vec3();
1425
+ this._rotation = new Vec3();
1426
+ this.scale = new Vec3(1.0, 1.0, 1.0);
1427
+ this.quaternion = new Quat();
1428
+ this.modelMatrix = new Mat4x3();
1429
+ this.worldMatrix = new Mat4x3();
1430
+ this.normalMatrix = new Mat3();
1431
+ this.modelMatrix.identity();
1432
+ this.worldMatrix.identity();
1433
+ this.normalMatrix.identity();
1434
+ this.localBoundingBox = new Box();
1435
+ this.localBoundingSphere = new Sphere$1();
1436
+ this.boundingBox = new Box();
1437
+ this.boundingSphere = new Sphere$1();
1438
+ }
1439
+ addChild(node) {
1440
+ if (this.children.indexOf(node) === -1) {
1441
+ if (node.parent && node.parent !== this)
1442
+ node.parent.removeChild(node);
1443
+ this.children.push(node);
1444
+ node.parent = this;
1445
+ node.isWorldDirty = true;
1446
+ }
1447
+ }
1448
+ removeChild(node) {
1449
+ const index = this.children.indexOf(node);
1450
+ if (index !== -1) {
1451
+ this.children.splice(index, 1);
1452
+ node.parent = null;
1453
+ node.isWorldDirty = true;
1454
+ return true;
1455
+ }
1456
+ return false;
1457
+ }
1458
+ markDirty() {
1459
+ this.isTransformDirty = true;
1460
+ this.isBoundsDirty = true;
1461
+ this.isNormalMatrixDirty = true;
1462
+ }
1463
+ get rotation() {
1464
+ if (this.isRotationStale) {
1465
+ this.quaternion.getEuler(this._rotation);
1466
+ this.isRotationStale = false;
1467
+ }
1468
+ return this._rotation;
1469
+ }
1470
+ set rotation(euler) {
1471
+ this.setRotation(euler);
1472
+ }
1473
+ rotationChanged() {
1474
+ this.isRotationStale = true;
1475
+ this.markDirty();
1476
+ }
1477
+ setPosition(position) {
1478
+ this.position.copy(position);
1479
+ this.markDirty();
1480
+ return this;
1481
+ }
1482
+ setRotation(rotation) {
1483
+ this._rotation.copy(rotation);
1484
+ this.quaternion.setFromEuler(this._rotation);
1485
+ this.isRotationStale = false;
1486
+ this.markDirty();
1487
+ return this;
1488
+ }
1489
+ setScale(scale) {
1490
+ this.scale.copy(scale);
1491
+ this.markDirty();
1492
+ return this;
1493
+ }
1494
+ scaleBy(factor) {
1495
+ this.scale.multiply(factor);
1496
+ this.markDirty();
1497
+ return this;
1498
+ }
1499
+ setQuaternion(q) {
1500
+ this.quaternion.copy(q);
1501
+ this.rotationChanged();
1502
+ return this;
1503
+ }
1504
+ slerp(targetQuat, t) {
1505
+ this.quaternion.slerp(targetQuat, t);
1506
+ this.rotationChanged();
1507
+ return this;
1508
+ }
1509
+ rotateX(angle) {
1510
+ this.quaternion.rotateX(angle);
1511
+ this.rotationChanged();
1512
+ return this;
1513
+ }
1514
+ rotateY(angle) {
1515
+ this.quaternion.rotateY(angle);
1516
+ this.rotationChanged();
1517
+ return this;
1518
+ }
1519
+ rotateZ(angle) {
1520
+ this.quaternion.rotateZ(angle);
1521
+ this.rotationChanged();
1522
+ return this;
1523
+ }
1524
+ rotateOnAxis(axis, angle) {
1525
+ this.quaternion.multiply(Node.scratchQuat.setAxisAngle(axis, angle));
1526
+ this.rotationChanged();
1527
+ return this;
1528
+ }
1529
+ translate(translation) {
1530
+ this.position.add(translation);
1531
+ this.markDirty();
1532
+ return this;
1533
+ }
1534
+ lookAt(target, up = Node.UP) {
1535
+ if (this.position.equals(target))
1536
+ return this;
1537
+ this.quaternion.lookAt(this.position, target, up);
1538
+ this.rotationChanged();
1539
+ return this;
1540
+ }
1541
+ updateModelMatrix() {
1542
+ if (!this.isTransformDirty)
1543
+ return false;
1544
+ this.modelMatrix.compose(this.position, this.quaternion, this.scale);
1545
+ this.isTransformDirty = false;
1546
+ this.isBoundsDirty = true;
1547
+ this.isNormalMatrixDirty = true;
1548
+ return true;
1549
+ }
1550
+ computeNormalMatrix() {
1551
+ if (!this.isNormalMatrixDirty)
1552
+ return this.normalMatrix;
1553
+ this.worldMatrix.toNormalMatrix(this.normalMatrix.toArray());
1554
+ this.isNormalMatrixDirty = false;
1555
+ return this.normalMatrix;
1556
+ }
1557
+ getWorldPosition(target = new Vec3()) {
1558
+ return this.worldMatrix.getTranslation(target);
1559
+ }
1560
+ getWorldDirection(target = new Vec3()) {
1561
+ return this.worldMatrix.getColumn(2, target).opposite().normalize();
1562
+ }
1563
+ distanceTo(target) {
1564
+ const targetPos = target instanceof Vec3 ? target : target.getWorldPosition();
1565
+ return this.getWorldPosition(Node.scratchVec).getDistance(targetPos);
1566
+ }
1567
+ isInsideBounds(box) {
1568
+ return box.containsPoint(this.getWorldPosition(Node.scratchVec));
1569
+ }
1570
+ setLocalBounds(box) {
1571
+ this.localBoundingBox.copy(box);
1572
+ this.localBoundingBox.getCenter(Node.scratchVec);
1573
+ this.localBoundingSphere.set(Node.scratchVec, box.min.getDistance(box.max) / 2);
1574
+ this.isBoundsDirty = true;
1575
+ return this;
1576
+ }
1577
+ computeBounds() {
1578
+ if (!this.isBoundsDirty)
1579
+ return;
1580
+ if (!this.localBoundingBox.isEmpty()) {
1581
+ this.boundingBox.copy(this.localBoundingBox).applyMatrix4(this.worldMatrix);
1582
+ this.boundingSphere.copy(this.localBoundingSphere).applyMatrix4(this.worldMatrix);
1583
+ }
1584
+ else {
1585
+ const pos = this.getWorldPosition(Node.scratchVec);
1586
+ this.boundingBox.set(pos, pos);
1587
+ this.boundingSphere.set(pos, 0);
1588
+ }
1589
+ this.isBoundsDirty = false;
1590
+ }
1591
+ computeBoundingBox() {
1592
+ this.computeBounds();
1593
+ return this.boundingBox;
1594
+ }
1595
+ computeBoundingSphere() {
1596
+ this.computeBounds();
1597
+ return this.boundingSphere;
1598
+ }
1599
+ setWorldMatrix(worldMatrix) {
1600
+ this.worldMatrix.copy(worldMatrix).multiply(this.modelMatrix);
1601
+ this.isBoundsDirty = true;
1602
+ this.isNormalMatrixDirty = true;
1603
+ this.isWorldDirty = false;
1604
+ }
1605
+ computeWorldMatrix(graph) {
1606
+ const modelChanged = this.updateModelMatrix();
1607
+ const changed = this.isWorldDirty || modelChanged;
1608
+ if (changed)
1609
+ this.setWorldMatrix(graph.getWorldMatrix());
1610
+ graph.pushModelMatrix(this.worldMatrix);
1611
+ for (const child of this.children) {
1612
+ if (changed)
1613
+ child.isWorldDirty = true;
1614
+ child.computeWorldMatrix(graph);
1615
+ }
1616
+ graph.popModelMatrix();
1617
+ }
1618
+ }
1619
+
1620
+ const scratchInverse = new Mat4x3();
1621
+ const scratchRay = new Ray3();
1622
+ const scratchVec = new Vec3();
1623
+ const scratchDir = new Vec3();
1624
+ const scratchA = new Vec3();
1625
+ const scratchB = new Vec3();
1626
+ const scratchC = new Vec3();
1627
+ const scratchHit = new Vec3();
1628
+ const scratchWorldHit = new Vec3();
1629
+ function getVertex(targetMesh, index, target) {
1630
+ const vertices = targetMesh.vertices;
1631
+ const size = targetMesh.itemSize || 3;
1632
+ const offset = (targetMesh.indices ? targetMesh.indices[index] : index) * size;
1633
+ return target.setScalar(vertices[offset], vertices[offset + 1], size > 2 ? vertices[offset + 2] : 0);
1634
+ }
1635
+ function raycastTriangles(mesh, ray) {
1636
+ const inverse = scratchInverse.copy(mesh.worldMatrix).invertAffine();
1637
+ const localRay = scratchRay.set(inverse.transformPoint(ray.origin, scratchVec), inverse.transformDirection(ray.direction, scratchDir));
1638
+ const strip = mesh.primitive === 'TRIANGLE_STRIP';
1639
+ let best = null;
1640
+ let bestDistance = Infinity;
1641
+ for (const subMesh of mesh.subMeshes) {
1642
+ const end = subMesh.start + subMesh.count;
1643
+ for (let i = subMesh.start; i + 2 < end; i += strip ? 1 : 3) {
1644
+ getVertex(mesh, i, scratchA);
1645
+ if (strip && (i - subMesh.start) % 2 === 1) {
1646
+ getVertex(mesh, i + 2, scratchB);
1647
+ getVertex(mesh, i + 1, scratchC);
1648
+ }
1649
+ else {
1650
+ getVertex(mesh, i + 1, scratchB);
1651
+ getVertex(mesh, i + 2, scratchC);
1652
+ }
1653
+ const local = localRay.intersectTriangle(scratchA, scratchB, scratchC, false, scratchHit);
1654
+ if (!local)
1655
+ continue;
1656
+ const world = mesh.worldMatrix.transformPoint(local, scratchWorldHit);
1657
+ const distance = ray.origin.getDistance(world);
1658
+ if (distance < bestDistance) {
1659
+ bestDistance = distance;
1660
+ best = (best ?? new Vec3()).copy(world);
1661
+ }
1662
+ }
1663
+ }
1664
+ return best ? { distance: bestDistance, point: best } : null;
1665
+ }
1666
+
1667
+ const isPowerOfTwo = (n) => (n & (n - 1)) === 0;
1668
+ function canGenerateMipmaps(context, width, height) {
1669
+ if (typeof WebGL2RenderingContext !== 'undefined' && context instanceof WebGL2RenderingContext)
1670
+ return true;
1671
+ return isPowerOfTwo(width) && isPowerOfTwo(height);
1672
+ }
1673
+ const Texture = {
1674
+ create(source, context, options) {
1675
+ const webGLTexture = context.createTexture();
1676
+ if (!webGLTexture)
1677
+ return null;
1678
+ const flipY = options?.flipY !== undefined ? options.flipY : true;
1679
+ const magFilter = options?.magFilter !== undefined ? options.magFilter : context.LINEAR;
1680
+ const minFilter = options?.minFilter !== undefined ? options.minFilter : context.LINEAR;
1681
+ const wrapS = options?.wrapS !== undefined ? options.wrapS : context.CLAMP_TO_EDGE;
1682
+ const wrapT = options?.wrapT !== undefined ? options.wrapT : context.CLAMP_TO_EDGE;
1683
+ context.bindTexture(context.TEXTURE_2D, webGLTexture);
1684
+ context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, flipY ? 1 : 0);
1685
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, magFilter);
1686
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, minFilter);
1687
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, wrapS);
1688
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, wrapT);
1689
+ if (source instanceof Uint8Array) {
1690
+ const width = options?.size ? options.size.x : 1;
1691
+ const height = options?.size ? options.size.y : 1;
1692
+ context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, width, height, 0, context.RGBA, context.UNSIGNED_BYTE, source);
1693
+ }
1694
+ else
1695
+ context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, source);
1696
+ if (options?.generateMipmaps) {
1697
+ const width = source instanceof Uint8Array ? (options?.size ? options.size.x : 1) : source.width;
1698
+ const height = source instanceof Uint8Array ? (options?.size ? options.size.y : 1) : source.height;
1699
+ if (canGenerateMipmaps(context, width, height))
1700
+ context.generateMipmap(context.TEXTURE_2D);
1701
+ else
1702
+ console.warn('Texture: mipmaps skipped, WebGL1 requires power-of-two dimensions.');
1703
+ }
1704
+ context.bindTexture(context.TEXTURE_2D, null);
1705
+ return webGLTexture;
1706
+ },
1707
+ load(url, context, options) {
1708
+ return new Promise((resolve, reject) => {
1709
+ const img = new Image();
1710
+ img.crossOrigin = 'anonymous';
1711
+ img.onload = () => resolve(Texture.create(img, context, options));
1712
+ img.onerror = (err) => reject(err);
1713
+ img.src = url;
1714
+ });
1715
+ },
1716
+ update(texture, source, context, offset = new Vec2(), size, flipY = true) {
1717
+ context.bindTexture(context.TEXTURE_2D, texture);
1718
+ context.pixelStorei(context.UNPACK_FLIP_Y_WEBGL, flipY ? 1 : 0);
1719
+ const xOffset = offset.x;
1720
+ const yOffset = offset.y;
1721
+ const width = size ? size.x : 1;
1722
+ const height = size ? size.y : 1;
1723
+ if (source instanceof Uint8Array)
1724
+ context.texSubImage2D(context.TEXTURE_2D, 0, xOffset, yOffset, width, height, context.RGBA, context.UNSIGNED_BYTE, source);
1725
+ else
1726
+ context.texSubImage2D(context.TEXTURE_2D, 0, xOffset, yOffset, context.RGBA, context.UNSIGNED_BYTE, source);
1727
+ context.bindTexture(context.TEXTURE_2D, null);
1728
+ }
1729
+ };
1730
+
1731
+ class Uniform {
1732
+ type;
1733
+ value;
1734
+ constructor(type, value) {
1735
+ this.type = type;
1736
+ this.value = value;
1737
+ }
1738
+ }
1739
+ function uploadUniform(context, location, uniform) {
1740
+ const value = uniform.value;
1741
+ const type = uniform.type;
1742
+ if (value instanceof Vec3)
1743
+ context.uniform3f(location, value.x, value.y, value.z);
1744
+ else if (value instanceof Vec2)
1745
+ context.uniform2f(location, value.x, value.y);
1746
+ else if (value instanceof Quat)
1747
+ context.uniform4f(location, value.vector.x, value.vector.y, value.vector.z, value.w);
1748
+ else if (value instanceof Mat4 || value instanceof Mat4x3 || value instanceof Mat3) {
1749
+ if (value instanceof Mat3)
1750
+ context.uniformMatrix3fv(location, false, value.toArray());
1751
+ else
1752
+ context.uniformMatrix4fv(location, false, value.toArray());
1753
+ }
1754
+ else if (type.indexOf('Matrix') !== -1)
1755
+ context[type](location, false, value);
1756
+ else
1757
+ context[type](location, value);
1758
+ }
1759
+
1760
+ const scratchView = new Mat4x3();
1761
+ const scratchCamera = new Vec3();
1762
+ function lightTypeValue(type) {
1763
+ return type === 'directional' ? 0 : type === 'point' ? 1 : 2;
1764
+ }
1765
+ function packVec4(xyz, w) {
1766
+ const count = Math.floor(xyz.length / 3);
1767
+ const packed = new Float32Array(count * 4);
1768
+ for (let i = 0; i < count; i++) {
1769
+ packed[i * 4] = xyz[i * 3];
1770
+ packed[i * 4 + 1] = xyz[i * 3 + 1];
1771
+ packed[i * 4 + 2] = xyz[i * 3 + 2];
1772
+ packed[i * 4 + 3] = w[i] || 0;
1773
+ }
1774
+ return packed;
1775
+ }
1776
+ function uploadFrameUniforms(context, program, projectionMatrix, viewMatrix, lights, time, cameraPosition, fog, frame) {
1777
+ if (program.projectionMatrix)
1778
+ context.uniformMatrix4fv(program.projectionMatrix, false, projectionMatrix);
1779
+ if (program.viewMatrix)
1780
+ context.uniformMatrix4fv(program.viewMatrix, false, viewMatrix);
1781
+ if (program.time)
1782
+ context.uniform1f(program.time, time);
1783
+ if (frame && frame.resolution && program.screenResolution)
1784
+ context.uniform2f(program.screenResolution, frame.resolution.x, frame.resolution.y);
1785
+ if (lights) {
1786
+ if (program.nbLights && lights.type)
1787
+ context.uniform1i(program.nbLights, lights.type.length);
1788
+ if (program.lightPosition && lights.position && lights.position.length > 0)
1789
+ context.uniform3fv(program.lightPosition, lights.floatPosition || new Float32Array(lights.position));
1790
+ if (program.lightDiffuse && lights.diffuse && lights.diffuse.length > 0)
1791
+ context.uniform3fv(program.lightDiffuse, lights.floatDiffuse || new Float32Array(lights.diffuse));
1792
+ if (program.lightSpecular && lights.specular && lights.specular.length > 0)
1793
+ context.uniform3fv(program.lightSpecular, lights.floatSpecular || new Float32Array(lights.specular));
1794
+ if (program.lightDirection && lights.direction && lights.direction.length > 0)
1795
+ context.uniform3fv(program.lightDirection, lights.floatDirection || new Float32Array(lights.direction));
1796
+ if (program.lightAttenuation && (lights.floatAttenuation || lights.constantAttenuation?.length)) {
1797
+ let attArr = lights.floatAttenuation;
1798
+ if (!attArr) {
1799
+ const att = [];
1800
+ for (let k = 0; k < lights.constantAttenuation.length; k++)
1801
+ att.push(lights.constantAttenuation[k] || 0, lights.linearAttenuation[k] || 0, lights.quadraticAttenuation[k] || 0);
1802
+ attArr = new Float32Array(att);
1803
+ }
1804
+ context.uniform3fv(program.lightAttenuation, attArr);
1805
+ }
1806
+ if (program.lightCutoff && lights.cutoff && lights.cutoff.length > 0)
1807
+ context.uniform1fv(program.lightCutoff, lights.floatCutoff || new Float32Array(lights.cutoff));
1808
+ if (program.lightExponent && lights.exponent && lights.exponent.length > 0)
1809
+ context.uniform1fv(program.lightExponent, lights.floatExponent || new Float32Array(lights.exponent));
1810
+ if (program.lightType && (lights.floatType || lights.type?.length)) {
1811
+ let typeArr = lights.floatType;
1812
+ if (!typeArr)
1813
+ typeArr = new Float32Array(lights.type.map(t => t === 'directional' ? 0 : t === 'point' ? 1 : 2));
1814
+ context.uniform1fv(program.lightType, typeArr);
1815
+ }
1816
+ if (program.lightPositionType && lights.position && lights.position.length > 0)
1817
+ context.uniform4fv(program.lightPositionType, lights.packedPosition || packVec4(lights.position, (lights.type || []).map(lightTypeValue)));
1818
+ if (program.lightDiffuseCutoff && lights.diffuse && lights.diffuse.length > 0)
1819
+ context.uniform4fv(program.lightDiffuseCutoff, lights.packedDiffuse || packVec4(lights.diffuse, lights.cutoff || []));
1820
+ if (program.lightSpecularExponent && lights.specular && lights.specular.length > 0)
1821
+ context.uniform4fv(program.lightSpecularExponent, lights.packedSpecular || packVec4(lights.specular, lights.exponent || []));
1822
+ if (program.ambientLight && lights.ambient)
1823
+ context.uniform3fv(program.ambientLight, lights.floatAmbient || new Float32Array(lights.ambient));
1824
+ }
1825
+ if (program.cameraPosition) {
1826
+ let camera = cameraPosition;
1827
+ if (!camera && viewMatrix) {
1828
+ scratchView.toArray().set(viewMatrix);
1829
+ camera = scratchView.invertAffine().getTranslation(scratchCamera);
1830
+ }
1831
+ if (camera)
1832
+ context.uniform3f(program.cameraPosition, camera.x, camera.y, camera.z);
1833
+ }
1834
+ if (program.hasFog) {
1835
+ if (fog) {
1836
+ context.uniform1i(program.hasFog, 1);
1837
+ if (program.fogColor)
1838
+ context.uniform3f(program.fogColor, fog.color.x, fog.color.y, fog.color.z);
1839
+ if (program.fogNear)
1840
+ context.uniform1f(program.fogNear, fog.near);
1841
+ if (program.fogFar)
1842
+ context.uniform1f(program.fogFar, fog.far);
1843
+ }
1844
+ else
1845
+ context.uniform1i(program.hasFog, 0);
1846
+ }
1847
+ }
1848
+
1849
+ class MeshRenderer {
1850
+ context;
1851
+ state;
1852
+ constructor(context) {
1853
+ this.context = context;
1854
+ this.state = getGLState(context);
1855
+ this.defaultSettings();
1856
+ }
1857
+ defaultSettings() {
1858
+ }
1859
+ createBuffer(target, size, drawMethod) {
1860
+ const buffer = this.context.createBuffer();
1861
+ this.bindBuffer(target, buffer);
1862
+ this.context.bufferData(this.context[target], size, this.context[drawMethod]);
1863
+ return buffer;
1864
+ }
1865
+ useProgram(program) {
1866
+ if (this.state.program === program)
1867
+ return;
1868
+ this.state.program = program;
1869
+ this.context.useProgram(program);
1870
+ }
1871
+ bindBuffer(target, buffer) {
1872
+ if (target === 'ARRAY_BUFFER') {
1873
+ if (this.state.arrayBuffer === buffer)
1874
+ return;
1875
+ this.state.arrayBuffer = buffer;
1876
+ }
1877
+ else if (target === 'ELEMENT_ARRAY_BUFFER') {
1878
+ if (this.state.elementBuffer === buffer)
1879
+ return;
1880
+ this.state.elementBuffer = buffer;
1881
+ }
1882
+ this.context.bindBuffer(this.context[target], buffer);
1883
+ }
1884
+ deleteBuffer(buffer) {
1885
+ if (this.state.arrayBuffer === buffer)
1886
+ this.state.arrayBuffer = null;
1887
+ if (this.state.elementBuffer === buffer)
1888
+ this.state.elementBuffer = null;
1889
+ this.context.deleteBuffer(buffer);
1890
+ }
1891
+ setAttributeMask(mask) {
1892
+ const previous = this.state.attributeMask;
1893
+ if (previous === mask)
1894
+ return;
1895
+ if (this.state.maxAttributes === 0)
1896
+ this.state.maxAttributes = Math.min(16, this.context.getParameter(this.context.MAX_VERTEX_ATTRIBS) || 8);
1897
+ const changed = previous === null ? (1 << this.state.maxAttributes) - 1 : previous ^ mask;
1898
+ for (let i = 0; i < this.state.maxAttributes && (changed >> i) !== 0; i++)
1899
+ if ((changed >> i) & 1)
1900
+ if ((mask >> i) & 1)
1901
+ this.context.enableVertexAttribArray(i);
1902
+ else
1903
+ this.context.disableVertexAttribArray(i);
1904
+ this.state.attributeMask = mask;
1905
+ }
1906
+ vertexAttribPointer(index, size, type, normalized, stride, offset) {
1907
+ this.context.vertexAttribPointer(index, size, this.context[type], normalized, stride, offset);
1908
+ }
1909
+ drawElements(primitive, subMesh, indexType) {
1910
+ const mode = typeof primitive === 'number' ? primitive : (this.context[primitive] ?? this.context.TRIANGLES);
1911
+ const type = indexType ?? this.context.UNSIGNED_SHORT;
1912
+ const bytes = type === this.context.UNSIGNED_SHORT ? 2 : 4;
1913
+ this.context.drawElements(mode, subMesh.count, type, subMesh.start * bytes);
1914
+ }
1915
+ drawArrays(primitive, subMesh) {
1916
+ const mode = typeof primitive === 'number' ? primitive : (this.context[primitive] ?? this.context.TRIANGLES);
1917
+ this.context.drawArrays(mode, subMesh.start, subMesh.count);
1918
+ }
1919
+ drawMesh(mesh, projectionMatrix, viewMatrix, lights, time, cameraPosition, fog, frame) {
1920
+ const context = this.context;
1921
+ const state = this.state;
1922
+ for (let i = 0; i < mesh.nbSubMeshes; i++) {
1923
+ const program = mesh.programs[i] || mesh.programs[mesh.nbPrograms - 1];
1924
+ this.useProgram(program);
1925
+ if (!frame || state.frameOfProgram.get(program) !== frame.id) {
1926
+ if (frame)
1927
+ state.frameOfProgram.set(program, frame.id);
1928
+ uploadFrameUniforms(context, program, projectionMatrix, viewMatrix, lights, time, cameraPosition, fog, frame);
1929
+ }
1930
+ const material = mesh.materials[i] || mesh.materials[mesh.materials.length - 1];
1931
+ if (material) {
1932
+ material.updateUniforms();
1933
+ if (state.materialOfProgram.get(program) !== material || state.materialVersionOfProgram.get(program) !== material.version) {
1934
+ state.materialOfProgram.set(program, material);
1935
+ state.materialVersionOfProgram.set(program, material.version);
1936
+ const materialUniforms = material.uniforms;
1937
+ for (const property in materialUniforms)
1938
+ if (materialUniforms.hasOwnProperty(property) && program[property])
1939
+ uploadUniform(context, program[property], materialUniforms[property]);
1940
+ }
1941
+ }
1942
+ let attributeMask = 0;
1943
+ if (mesh.indices)
1944
+ this.bindBuffer('ELEMENT_ARRAY_BUFFER', mesh.indexBuffer);
1945
+ if (program.vertexPosition !== -1 && program.vertexPosition !== undefined && mesh.vertexBuffer) {
1946
+ this.bindBuffer('ARRAY_BUFFER', mesh.vertexBuffer);
1947
+ this.vertexAttribPointer(program.vertexPosition, mesh.itemSize, 'FLOAT', false, 0, 0);
1948
+ attributeMask |= 1 << program.vertexPosition;
1949
+ }
1950
+ if (mesh.normals && program.vertexNormal !== -1 && program.vertexNormal !== undefined) {
1951
+ this.bindBuffer('ARRAY_BUFFER', mesh.normalBuffer);
1952
+ this.vertexAttribPointer(program.vertexNormal, 3, 'FLOAT', false, 0, 0);
1953
+ attributeMask |= 1 << program.vertexNormal;
1954
+ }
1955
+ if (mesh.uvs && program.textureCoord !== -1 && program.textureCoord !== undefined) {
1956
+ this.bindBuffer('ARRAY_BUFFER', mesh.texCoordBuffer);
1957
+ this.vertexAttribPointer(program.textureCoord, 2, 'FLOAT', false, 0, 0);
1958
+ attributeMask |= 1 << program.textureCoord;
1959
+ }
1960
+ this.setAttributeMask(attributeMask);
1961
+ if (mesh.WebGLTexture) {
1962
+ context.activeTexture(context.TEXTURE0);
1963
+ context.bindTexture(context.TEXTURE_2D, mesh.WebGLTexture);
1964
+ if (program.sampler)
1965
+ context.uniform1i(program.sampler, 0);
1966
+ if (program.hasTexture)
1967
+ context.uniform1i(program.hasTexture, 1);
1968
+ }
1969
+ else if (program.hasTexture)
1970
+ context.uniform1i(program.hasTexture, 0);
1971
+ if (program.modelMatrix)
1972
+ context.uniformMatrix4fv(program.modelMatrix, false, mesh.worldMatrix.toArray());
1973
+ if (program.normalMatrix) {
1974
+ mesh.computeNormalMatrix();
1975
+ context.uniformMatrix3fv(program.normalMatrix, false, mesh.normalMatrix.toArray());
1976
+ }
1977
+ for (const property in mesh.customUniforms)
1978
+ if (mesh.customUniforms.hasOwnProperty(property) && program[property])
1979
+ uploadUniform(context, program[property], mesh.customUniforms[property]);
1980
+ this[mesh.drawMethod](mesh.primitive, mesh.subMeshes[i], mesh.indexType);
1981
+ }
1982
+ }
1983
+ }
1984
+
1985
+ class SubMesh {
1986
+ start;
1987
+ count;
1988
+ constructor(start, count) {
1989
+ this.start = start;
1990
+ this.count = count;
1991
+ }
1992
+ }
1993
+
1994
+ class Material {
1995
+ ambient;
1996
+ diffuse;
1997
+ specular;
1998
+ shininess;
1999
+ uniforms;
2000
+ version = 0;
2001
+ constructor(options) {
2002
+ this.ambient = new Vec3(0.5, 0.5, 0.5);
2003
+ this.diffuse = new Vec3(0.6, 0.6, 0.6);
2004
+ this.specular = new Vec3(0.8, 0.8, 0.8);
2005
+ this.shininess = 8.0;
2006
+ this.uniforms = {
2007
+ materialAmbient: new Uniform('uniform3fv', this.ambient.toArray()),
2008
+ materialDiffuse: new Uniform('uniform3fv', this.diffuse.toArray()),
2009
+ materialSpecular: new Uniform('uniform3fv', this.specular.toArray()),
2010
+ materialShininess: new Uniform('uniform1f', this.shininess)
2011
+ };
2012
+ if (options) {
2013
+ if (options.ambient)
2014
+ this.setAmbient(options.ambient);
2015
+ if (options.diffuse)
2016
+ this.setDiffuse(options.diffuse);
2017
+ if (options.specular)
2018
+ this.setSpecular(options.specular);
2019
+ if (options.shininess !== undefined)
2020
+ this.setShininess(options.shininess);
2021
+ }
2022
+ }
2023
+ setAmbient(ambient) {
2024
+ this.ambient.copy(ambient).toArray(this.uniforms.materialAmbient.value);
2025
+ this.version++;
2026
+ }
2027
+ setDiffuse(diffuse) {
2028
+ this.diffuse.copy(diffuse).toArray(this.uniforms.materialDiffuse.value);
2029
+ this.version++;
2030
+ }
2031
+ setSpecular(specular) {
2032
+ this.specular.copy(specular).toArray(this.uniforms.materialSpecular.value);
2033
+ this.version++;
2034
+ }
2035
+ setShininess(shininess) {
2036
+ this.shininess = shininess;
2037
+ this.uniforms.materialShininess.value = this.shininess;
2038
+ this.version++;
2039
+ }
2040
+ updateUniforms() {
2041
+ let changed = this.sync(this.ambient, this.uniforms.materialAmbient);
2042
+ changed = this.sync(this.diffuse, this.uniforms.materialDiffuse) || changed;
2043
+ changed = this.sync(this.specular, this.uniforms.materialSpecular) || changed;
2044
+ if (this.uniforms.materialShininess.value !== this.shininess) {
2045
+ this.uniforms.materialShininess.value = this.shininess;
2046
+ changed = true;
2047
+ }
2048
+ if (changed)
2049
+ this.version++;
2050
+ return changed;
2051
+ }
2052
+ sync(vector, uniform) {
2053
+ const value = uniform.value;
2054
+ if (value[0] === vector.x && value[1] === vector.y && value[2] === vector.z)
2055
+ return false;
2056
+ vector.toArray(value);
2057
+ return true;
2058
+ }
2059
+ }
2060
+
2061
+ const BasicShader = {
2062
+ vertex: `
2063
+ attribute vec3 aVertexPosition;
2064
+ attribute vec2 aTextureCoord;
2065
+
2066
+ uniform mat4 uModelMatrix;
2067
+ uniform mat4 uViewMatrix;
2068
+ uniform mat4 uProjectionMatrix;
2069
+
2070
+ varying vec2 vTextureCoord;
2071
+
2072
+ void main(void) {
2073
+ vTextureCoord = aTextureCoord;
2074
+ gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(aVertexPosition, 1.0);
2075
+ }
2076
+ `,
2077
+ fragment: `
2078
+ precision mediump float;
2079
+
2080
+ varying vec2 vTextureCoord;
2081
+
2082
+ uniform sampler2D uSampler;
2083
+ uniform vec3 uMaterialDiffuse;
2084
+ uniform int uHasTexture;
2085
+
2086
+ void main(void) {
2087
+ vec4 color = vec4(uMaterialDiffuse, 1.0);
2088
+ if (uHasTexture == 1) {
2089
+ color *= texture2D(uSampler, vTextureCoord);
2090
+ }
2091
+ gl_FragColor = color;
2092
+ }
2093
+ `,
2094
+ create(context) {
2095
+ return Program.create(context, BasicShader.vertex, BasicShader.fragment);
2096
+ }
2097
+ };
2098
+
2099
+ const PhongShader = {
2100
+ vertex: `
2101
+ attribute vec3 aVertexPosition;
2102
+ attribute vec3 aVertexNormal;
2103
+ attribute vec2 aTextureCoord;
2104
+
2105
+ uniform mat4 uModelMatrix;
2106
+ uniform mat4 uViewMatrix;
2107
+ uniform mat4 uProjectionMatrix;
2108
+ uniform mat3 uNormalMatrix;
2109
+
2110
+ varying vec3 vWorldPosition;
2111
+ varying vec3 vNormal;
2112
+ varying vec2 vTextureCoord;
2113
+
2114
+ void main(void) {
2115
+ vec4 worldPos = uModelMatrix * vec4(aVertexPosition, 1.0);
2116
+ vWorldPosition = worldPos.xyz;
2117
+ vNormal = normalize(uNormalMatrix * aVertexNormal);
2118
+ vTextureCoord = aTextureCoord;
2119
+ gl_Position = uProjectionMatrix * uViewMatrix * worldPos;
2120
+ }
2121
+ `,
2122
+ fragment: `
2123
+ #ifdef GL_FRAGMENT_PRECISION_HIGH
2124
+ precision highp float;
2125
+ #else
2126
+ precision mediump float;
2127
+ #endif
2128
+
2129
+ varying vec3 vWorldPosition;
2130
+ varying vec3 vNormal;
2131
+ varying vec2 vTextureCoord;
2132
+
2133
+ uniform vec3 uMaterialAmbient;
2134
+ uniform vec3 uMaterialDiffuse;
2135
+ uniform vec3 uMaterialSpecular;
2136
+ uniform float uMaterialShininess;
2137
+
2138
+ uniform vec3 uAmbientLight;
2139
+ uniform vec3 uAmbientColor;
2140
+
2141
+ const int MAX_LIGHTS = 8;
2142
+ uniform int uNbLights;
2143
+ // Packed: (position.xyz, type), (diffuse.rgb, cutoff), (specular.rgb, exponent)
2144
+ uniform vec4 uLightPositionType[MAX_LIGHTS];
2145
+ uniform vec4 uLightDiffuseCutoff[MAX_LIGHTS];
2146
+ uniform vec4 uLightSpecularExponent[MAX_LIGHTS];
2147
+ uniform vec3 uLightDirection[MAX_LIGHTS];
2148
+ uniform vec3 uLightAttenuation[MAX_LIGHTS];
2149
+
2150
+ uniform sampler2D uSampler;
2151
+ uniform int uHasTexture;
2152
+
2153
+ uniform vec3 uCameraPosition;
2154
+
2155
+ uniform vec3 uFogColor;
2156
+ uniform float uFogNear;
2157
+ uniform float uFogFar;
2158
+ uniform int uHasFog;
2159
+
2160
+ void main(void) {
2161
+ vec3 normal = normalize(vNormal);
2162
+ vec3 viewDir = normalize(uCameraPosition - vWorldPosition);
2163
+
2164
+ vec3 totalAmbient = (uAmbientLight + uAmbientColor) * uMaterialAmbient;
2165
+ vec3 totalDiffuse = vec3(0.0);
2166
+ vec3 totalSpecular = vec3(0.0);
2167
+
2168
+ for (int i = 0; i < MAX_LIGHTS; i++) {
2169
+ if (i >= uNbLights) break;
2170
+
2171
+ vec4 positionType = uLightPositionType[i];
2172
+ vec4 diffuseCutoff = uLightDiffuseCutoff[i];
2173
+ vec4 specularExponent = uLightSpecularExponent[i];
2174
+
2175
+ vec3 lightDir;
2176
+ float attenuation = 1.0;
2177
+
2178
+ if (positionType.w < 0.5) {
2179
+ // Directional
2180
+ lightDir = normalize(-uLightDirection[i]);
2181
+ } else {
2182
+ // Point or Spot
2183
+ vec3 lightVec = positionType.xyz - vWorldPosition;
2184
+ float d = length(lightVec);
2185
+ lightDir = normalize(lightVec);
2186
+
2187
+ float c = uLightAttenuation[i].x;
2188
+ float l = uLightAttenuation[i].y;
2189
+ float q = uLightAttenuation[i].z;
2190
+ attenuation = 1.0 / max(c + l * d + q * d * d, 1.0);
2191
+
2192
+ if (positionType.w > 1.5) {
2193
+ // Spot
2194
+ float spotEffect = dot(normalize(uLightDirection[i]), -lightDir);
2195
+ float cutoffCos = cos(radians(diffuseCutoff.w));
2196
+ if (spotEffect > cutoffCos) {
2197
+ attenuation *= pow(spotEffect, max(specularExponent.w, 1.0));
2198
+ } else {
2199
+ attenuation = 0.0;
2200
+ }
2201
+ }
2202
+ }
2203
+
2204
+ float NdotL = max(dot(normal, lightDir), 0.0);
2205
+ totalDiffuse += diffuseCutoff.rgb * uMaterialDiffuse * NdotL * attenuation;
2206
+
2207
+ if (NdotL > 0.0) {
2208
+ vec3 halfDir = normalize(lightDir + viewDir);
2209
+ float NdotH = max(dot(normal, halfDir), 0.0);
2210
+ float spec = pow(NdotH, max(uMaterialShininess, 1.0));
2211
+ totalSpecular += specularExponent.rgb * uMaterialSpecular * spec * attenuation;
2212
+ }
2213
+ }
2214
+
2215
+ vec4 texColor = vec4(1.0);
2216
+ if (uHasTexture == 1) {
2217
+ texColor = texture2D(uSampler, vTextureCoord);
2218
+ }
2219
+
2220
+ vec3 finalColor = (totalAmbient + totalDiffuse) * texColor.rgb + totalSpecular;
2221
+
2222
+ if (uHasFog == 1) {
2223
+ float dist = length(uCameraPosition - vWorldPosition);
2224
+ float fogFactor = clamp((uFogFar - dist) / max(uFogFar - uFogNear, 0.0001), 0.0, 1.0);
2225
+ finalColor = mix(uFogColor, finalColor, fogFactor);
2226
+ }
2227
+
2228
+ gl_FragColor = vec4(finalColor, texColor.a);
2229
+ }
2230
+ `,
2231
+ create(context) {
2232
+ return Program.create(context, PhongShader.vertex, PhongShader.fragment);
2233
+ }
2234
+ };
2235
+
2236
+ const NormalShader = {
2237
+ vertex: `
2238
+ attribute vec3 aVertexPosition;
2239
+ attribute vec3 aVertexNormal;
2240
+
2241
+ uniform mat4 uModelMatrix;
2242
+ uniform mat4 uViewMatrix;
2243
+ uniform mat4 uProjectionMatrix;
2244
+ uniform mat3 uNormalMatrix;
2245
+
2246
+ varying vec3 vNormal;
2247
+
2248
+ void main(void) {
2249
+ vNormal = normalize(uNormalMatrix * aVertexNormal);
2250
+ gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(aVertexPosition, 1.0);
2251
+ }
2252
+ `,
2253
+ fragment: `
2254
+ precision mediump float;
2255
+
2256
+ varying vec3 vNormal;
2257
+
2258
+ void main(void) {
2259
+ gl_FragColor = vec4(vNormal * 0.5 + 0.5, 1.0);
2260
+ }
2261
+ `,
2262
+ create(context) {
2263
+ return Program.create(context, NormalShader.vertex, NormalShader.fragment);
2264
+ }
2265
+ };
2266
+
2267
+ const initializedPrograms = new WeakSet();
2268
+ function ucfirst(string) {
2269
+ return string.charAt(0).toUpperCase() + string.slice(1);
2270
+ }
2271
+ function addProgramAttribute(context, program, name) {
2272
+ program[name] = context.getAttribLocation(program, `a${ucfirst(name)}`);
2273
+ }
2274
+ function addProgramUniform(context, program, name) {
2275
+ const ucName = `u${ucfirst(name)}`;
2276
+ let loc = context.getUniformLocation(program, ucName);
2277
+ if (loc === null)
2278
+ loc = context.getUniformLocation(program, `${ucName}[0]`);
2279
+ program[name] = loc;
2280
+ }
2281
+ function initProgramLocations(context, program, customUniforms, materials) {
2282
+ if (!initializedPrograms.has(program)) {
2283
+ initializedPrograms.add(program);
2284
+ addProgramAttribute(context, program, 'vertexPosition');
2285
+ addProgramAttribute(context, program, 'vertexNormal');
2286
+ addProgramAttribute(context, program, 'textureCoord');
2287
+ addProgramUniform(context, program, 'modelMatrix');
2288
+ addProgramUniform(context, program, 'normalMatrix');
2289
+ addProgramUniform(context, program, 'viewMatrix');
2290
+ addProgramUniform(context, program, 'projectionMatrix');
2291
+ addProgramUniform(context, program, 'time');
2292
+ addProgramUniform(context, program, 'screenResolution');
2293
+ addProgramUniform(context, program, 'sampler');
2294
+ addProgramUniform(context, program, 'hasTexture');
2295
+ addProgramUniform(context, program, 'nbLights');
2296
+ addProgramUniform(context, program, 'lightPosition');
2297
+ addProgramUniform(context, program, 'lightDiffuse');
2298
+ addProgramUniform(context, program, 'lightSpecular');
2299
+ addProgramUniform(context, program, 'lightDirection');
2300
+ addProgramUniform(context, program, 'lightAttenuation');
2301
+ addProgramUniform(context, program, 'lightCutoff');
2302
+ addProgramUniform(context, program, 'lightExponent');
2303
+ addProgramUniform(context, program, 'lightType');
2304
+ addProgramUniform(context, program, 'lightPositionType');
2305
+ addProgramUniform(context, program, 'lightDiffuseCutoff');
2306
+ addProgramUniform(context, program, 'lightSpecularExponent');
2307
+ addProgramUniform(context, program, 'ambientLight');
2308
+ addProgramUniform(context, program, 'ambientColor');
2309
+ addProgramUniform(context, program, 'cameraPosition');
2310
+ addProgramUniform(context, program, 'hasFog');
2311
+ addProgramUniform(context, program, 'fogColor');
2312
+ addProgramUniform(context, program, 'fogNear');
2313
+ addProgramUniform(context, program, 'fogFar');
2314
+ if (program.screenResolution) {
2315
+ context.useProgram(program);
2316
+ const viewport = context.getParameter(context.VIEWPORT);
2317
+ if (viewport && viewport.length >= 4)
2318
+ context.uniform2f(program.screenResolution, viewport[2], viewport[3]);
2319
+ }
2320
+ }
2321
+ for (const property in customUniforms)
2322
+ if (customUniforms.hasOwnProperty(property) && program[property] === undefined)
2323
+ addProgramUniform(context, program, property);
2324
+ for (const mat of materials)
2325
+ for (const property in mat.uniforms)
2326
+ if (mat.uniforms.hasOwnProperty(property) && program[property] === undefined)
2327
+ addProgramUniform(context, program, property);
2328
+ }
2329
+ function syncMaterialUniforms(context, programs, material) {
2330
+ const materialUniforms = material.uniforms;
2331
+ for (const property in materialUniforms)
2332
+ if (materialUniforms.hasOwnProperty(property))
2333
+ for (const program of programs)
2334
+ if (program[property] === undefined)
2335
+ program[property] = context.getUniformLocation(program, `u${ucfirst(property)}`);
2336
+ }
2337
+ function syncCustomUniform(context, programs, name) {
2338
+ for (const program of programs)
2339
+ if (program[name] === undefined)
2340
+ program[name] = context.getUniformLocation(program, `u${ucfirst(name)}`);
2341
+ }
2342
+ function syncTextureSampler(context, programs) {
2343
+ for (const program of programs)
2344
+ if (program.sampler === undefined)
2345
+ program.sampler = context.getUniformLocation(program, 'uSampler');
2346
+ }
2347
+
2348
+ var eDrawMethod;
2349
+ (function (eDrawMethod) {
2350
+ eDrawMethod["drawElements"] = "drawElements";
2351
+ eDrawMethod["drawArrays"] = "drawArrays";
2352
+ })(eDrawMethod || (eDrawMethod = {}));
2353
+ class Mesh extends Node {
2354
+ vertices;
2355
+ indices;
2356
+ normals;
2357
+ uvs;
2358
+ itemSize;
2359
+ subMeshes;
2360
+ nbSubMeshes;
2361
+ primitive;
2362
+ customUniforms;
2363
+ context;
2364
+ renderer;
2365
+ WebGLTexture;
2366
+ vertexBuffer;
2367
+ indexBuffer;
2368
+ normalBuffer;
2369
+ texCoordBuffer;
2370
+ sortKey = 0;
2371
+ ownsTexture = false;
2372
+ textureSource = null;
2373
+ textureOptions;
2374
+ indexType;
2375
+ static scratchHit = new Vec3();
2376
+ active;
2377
+ drawMethod;
2378
+ programs;
2379
+ nbPrograms;
2380
+ materials;
2381
+ blendMode;
2382
+ blendOptions = { equation: 'FUNC_ADD', source: 'SRC_ALPHA', destination: 'ONE' };
2383
+ depthWrite = true;
2384
+ zOrder;
2385
+ constructor(meshOrContext, contextOrMesh) {
2386
+ super();
2387
+ let mesh;
2388
+ let context;
2389
+ if (meshOrContext && ('drawingBufferWidth' in meshOrContext || typeof meshOrContext.createBuffer === 'function')) {
2390
+ context = meshOrContext;
2391
+ mesh = contextOrMesh;
2392
+ }
2393
+ else {
2394
+ mesh = meshOrContext;
2395
+ context = contextOrMesh;
2396
+ }
2397
+ this.vertices = mesh && mesh.vertices ? mesh.vertices : null;
2398
+ this.indices = mesh && mesh.indices ? mesh.indices : null;
2399
+ this.normals = mesh && mesh.normals ? mesh.normals : null;
2400
+ this.uvs = mesh && mesh.uvs ? mesh.uvs : null;
2401
+ this.itemSize = mesh && mesh.itemSize ? mesh.itemSize : null;
2402
+ this.subMeshes = mesh && mesh.subMeshes ? mesh.subMeshes : [];
2403
+ if (this.subMeshes.length === 0 && this.vertices && this.vertices.length > 0)
2404
+ this.subMeshes = [new SubMesh(0, this.indices ? this.indices.length : this.vertices.length / (this.itemSize || 3))];
2405
+ this.nbSubMeshes = this.subMeshes.length;
2406
+ this.primitive = mesh && mesh.primitive ? mesh.primitive : null;
2407
+ this.customUniforms = {};
2408
+ this.context = context;
2409
+ this.renderer = new MeshRenderer(this.context);
2410
+ this.WebGLTexture = null;
2411
+ this.vertexBuffer = null;
2412
+ this.indexBuffer = null;
2413
+ this.normalBuffer = null;
2414
+ this.texCoordBuffer = null;
2415
+ this.indexType = this.context.UNSIGNED_SHORT;
2416
+ this.createBuffers();
2417
+ if (this.vertices && this.vertices.length > 0) {
2418
+ this.localBoundingBox.setFromPoints(this.vertices, this.itemSize || 3);
2419
+ this.localBoundingSphere.setFromPoints(this.vertices, undefined, this.itemSize || 3);
2420
+ }
2421
+ this.active = true;
2422
+ this.drawMethod = this.indices ? eDrawMethod.drawElements : eDrawMethod.drawArrays;
2423
+ this.programs = [];
2424
+ this.nbPrograms = 0;
2425
+ this.materials = [];
2426
+ this.blendMode = false;
2427
+ this.zOrder = 0;
2428
+ }
2429
+ setActive() {
2430
+ this.active = true;
2431
+ }
2432
+ setInactive() {
2433
+ this.active = false;
2434
+ }
2435
+ toggleActive() {
2436
+ this.active = !this.active;
2437
+ return this.active;
2438
+ }
2439
+ isActive() {
2440
+ return this.active;
2441
+ }
2442
+ addProgram(vertexShaderOrProgram, fragmentShaderOrMaterial, material) {
2443
+ if (this.nbPrograms < this.nbSubMeshes) {
2444
+ let program;
2445
+ let mat;
2446
+ if (typeof vertexShaderOrProgram !== 'string') {
2447
+ program = vertexShaderOrProgram;
2448
+ mat = (fragmentShaderOrMaterial instanceof Material) ? fragmentShaderOrMaterial : new Material();
2449
+ }
2450
+ else {
2451
+ program = Program.acquire(this.context, vertexShaderOrProgram, fragmentShaderOrMaterial);
2452
+ mat = (material instanceof Material) ? material : (fragmentShaderOrMaterial instanceof Material ? fragmentShaderOrMaterial : new Material());
2453
+ }
2454
+ if (!program)
2455
+ return false;
2456
+ this.programs.push(program);
2457
+ this.addMaterial(mat);
2458
+ this.createProgram();
2459
+ return true;
2460
+ }
2461
+ return false;
2462
+ }
2463
+ clearPrograms() {
2464
+ for (const program of this.programs)
2465
+ Program.release(this.context, program);
2466
+ this.programs = [];
2467
+ this.nbPrograms = 0;
2468
+ this.materials = [];
2469
+ }
2470
+ addMaterial(material) {
2471
+ if (material && this.materials.length < this.nbSubMeshes) {
2472
+ this.materials.push(material);
2473
+ syncMaterialUniforms(this.context, this.programs, material);
2474
+ return true;
2475
+ }
2476
+ return false;
2477
+ }
2478
+ raycast(ray, precise = false) {
2479
+ if (!this.isActive())
2480
+ return null;
2481
+ const sphere = this.computeBoundingSphere();
2482
+ const hitPoint = ray.intersectSphere(sphere, Mesh.scratchHit);
2483
+ if (!hitPoint)
2484
+ return null;
2485
+ if (precise && this.vertices && (this.primitive === 'TRIANGLES' || this.primitive === 'TRIANGLE_STRIP'))
2486
+ return raycastTriangles(this, ray);
2487
+ return { distance: ray.origin.getDistance(hitPoint), point: new Vec3().copy(hitPoint) };
2488
+ }
2489
+ useBasicMaterial(options) {
2490
+ const mat = new Material();
2491
+ if (options?.color)
2492
+ mat.setDiffuse(options.color);
2493
+ this.addProgram(BasicShader.vertex, BasicShader.fragment, mat);
2494
+ this.applyTextureOption(options?.texture);
2495
+ return this;
2496
+ }
2497
+ usePhongMaterial(options) {
2498
+ const mat = new Material();
2499
+ if (options?.ambient)
2500
+ mat.setAmbient(options.ambient);
2501
+ if (options?.diffuse)
2502
+ mat.setDiffuse(options.diffuse);
2503
+ if (options?.specular)
2504
+ mat.setSpecular(options.specular);
2505
+ if (options?.shininess !== undefined)
2506
+ mat.setShininess(options.shininess);
2507
+ this.addProgram(PhongShader.vertex, PhongShader.fragment, mat);
2508
+ this.applyTextureOption(options?.texture);
2509
+ return this;
2510
+ }
2511
+ useNormalMaterial() {
2512
+ this.addProgram(NormalShader.vertex, NormalShader.fragment, new Material());
2513
+ return this;
2514
+ }
2515
+ applyTextureOption(texture) {
2516
+ if (!texture)
2517
+ return;
2518
+ if (typeof WebGLTexture !== 'undefined' && texture instanceof WebGLTexture) {
2519
+ this.releaseTexture();
2520
+ this.WebGLTexture = texture;
2521
+ this.ownsTexture = false;
2522
+ }
2523
+ else
2524
+ this.setTexture(texture);
2525
+ }
2526
+ releaseTexture() {
2527
+ if (this.WebGLTexture && this.ownsTexture)
2528
+ this.context.deleteTexture(this.WebGLTexture);
2529
+ this.WebGLTexture = null;
2530
+ this.ownsTexture = false;
2531
+ this.textureSource = null;
2532
+ this.textureOptions = undefined;
2533
+ }
2534
+ setTexture(img, options) {
2535
+ const texture = Texture.create(img, this.context, options);
2536
+ if (!texture)
2537
+ return;
2538
+ this.releaseTexture();
2539
+ this.WebGLTexture = texture;
2540
+ this.ownsTexture = true;
2541
+ this.textureSource = img;
2542
+ this.textureOptions = options;
2543
+ syncTextureSampler(this.context, this.programs);
2544
+ }
2545
+ addCustomUniform(name, type, value) {
2546
+ if (!this.customUniforms.hasOwnProperty(name)) {
2547
+ this.customUniforms[name] = new Uniform(type, value);
2548
+ syncCustomUniform(this.context, this.programs, name);
2549
+ }
2550
+ }
2551
+ setCustomUniform(name, value) {
2552
+ if (this.customUniforms.hasOwnProperty(name))
2553
+ this.customUniforms[name].value = value;
2554
+ }
2555
+ createIndexData(indices) {
2556
+ let max = 0;
2557
+ for (let i = 0; i < indices.length; i++)
2558
+ if (indices[i] > max)
2559
+ max = indices[i];
2560
+ if (max > 65535) {
2561
+ if (this.context.getExtension && this.context.getExtension('OES_element_index_uint')) {
2562
+ this.indexType = this.context.UNSIGNED_INT;
2563
+ return new Uint32Array(indices);
2564
+ }
2565
+ console.warn('Mesh: indices exceed 65535 but OES_element_index_uint is unavailable; geometry will be corrupted.');
2566
+ }
2567
+ return indices instanceof Uint16Array ? indices : new Uint16Array(indices);
2568
+ }
2569
+ createBuffers() {
2570
+ this.vertexBuffer = this.vertices
2571
+ ? this.renderer.createBuffer('ARRAY_BUFFER', this.vertices instanceof Float32Array ? this.vertices : new Float32Array(this.vertices), 'STATIC_DRAW')
2572
+ : null;
2573
+ this.indexType = this.context.UNSIGNED_SHORT;
2574
+ this.indexBuffer = this.indices
2575
+ ? this.renderer.createBuffer('ELEMENT_ARRAY_BUFFER', this.createIndexData(this.indices), 'STATIC_DRAW')
2576
+ : null;
2577
+ this.normalBuffer = this.normals
2578
+ ? this.renderer.createBuffer('ARRAY_BUFFER', this.normals instanceof Float32Array ? this.normals : new Float32Array(this.normals), 'STATIC_DRAW')
2579
+ : null;
2580
+ this.texCoordBuffer = this.uvs
2581
+ ? this.renderer.createBuffer('ARRAY_BUFFER', this.uvs instanceof Float32Array ? this.uvs : new Float32Array(this.uvs), 'STATIC_DRAW')
2582
+ : null;
2583
+ }
2584
+ restore() {
2585
+ this.createBuffers();
2586
+ const sources = this.programs.map((program) => Program.sources(program));
2587
+ this.programs = [];
2588
+ this.nbPrograms = 0;
2589
+ sources.forEach((source) => {
2590
+ if (!source)
2591
+ return;
2592
+ const program = Program.acquire(this.context, source.vertex, source.fragment);
2593
+ if (!program)
2594
+ return;
2595
+ this.programs.push(program);
2596
+ this.createProgram();
2597
+ });
2598
+ if (this.programs.length !== sources.length)
2599
+ this.setInactive();
2600
+ if (this.ownsTexture && this.textureSource) {
2601
+ const texture = Texture.create(this.textureSource, this.context, this.textureOptions);
2602
+ this.WebGLTexture = texture;
2603
+ this.ownsTexture = texture !== null;
2604
+ }
2605
+ for (const child of this.children)
2606
+ child.restore();
2607
+ }
2608
+ createProgram() {
2609
+ initProgramLocations(this.context, this.programs[this.nbPrograms], this.customUniforms, this.materials);
2610
+ this.nbPrograms++;
2611
+ }
2612
+ activateBlendMode() {
2613
+ this.blendMode = true;
2614
+ }
2615
+ deactivateBlendMode() {
2616
+ this.blendMode = false;
2617
+ }
2618
+ setBlend(source = 'SRC_ALPHA', destination = 'ONE', equation = 'FUNC_ADD') {
2619
+ this.blendOptions.equation = equation;
2620
+ this.blendOptions.source = source;
2621
+ this.blendOptions.destination = destination;
2622
+ this.blendMode = true;
2623
+ return this;
2624
+ }
2625
+ setDepthWrite(enabled) {
2626
+ this.depthWrite = enabled;
2627
+ return this;
2628
+ }
2629
+ render(projectionMatrix, viewMatrix, lights, time, blendMode, cameraPosition, fog, frame) {
2630
+ if (!this.isActive())
2631
+ return;
2632
+ this.renderSelf(projectionMatrix, viewMatrix, lights, time, blendMode, cameraPosition, fog, frame);
2633
+ for (const child of this.children)
2634
+ child.render(projectionMatrix, viewMatrix, lights, time, blendMode, cameraPosition, fog, frame);
2635
+ }
2636
+ renderSelf(projectionMatrix, viewMatrix, lights, time, blendMode, cameraPosition, fog, frame) {
2637
+ if (!this.isActive() || this.nbPrograms === 0 || this.blendMode !== blendMode)
2638
+ return;
2639
+ this.renderer.drawMesh(this, projectionMatrix, viewMatrix, lights, time, cameraPosition, fog, frame);
2640
+ }
2641
+ destroy() {
2642
+ this.clearPrograms();
2643
+ if (this.vertexBuffer)
2644
+ this.renderer.deleteBuffer(this.vertexBuffer), this.vertexBuffer = null;
2645
+ if (this.indexBuffer)
2646
+ this.renderer.deleteBuffer(this.indexBuffer), this.indexBuffer = null;
2647
+ if (this.normalBuffer)
2648
+ this.renderer.deleteBuffer(this.normalBuffer), this.normalBuffer = null;
2649
+ if (this.texCoordBuffer)
2650
+ this.renderer.deleteBuffer(this.texCoordBuffer), this.texCoordBuffer = null;
2651
+ this.releaseTexture();
2652
+ for (const child of this.children) {
2653
+ child.parent = null;
2654
+ child.destroy();
2655
+ }
2656
+ this.children = [];
2657
+ }
2658
+ }
2659
+
2660
+ class BasicMesh {
2661
+ vertices;
2662
+ indices;
2663
+ normals;
2664
+ uvs;
2665
+ subMeshes;
2666
+ itemSize;
2667
+ primitive;
2668
+ constructor() {
2669
+ this.vertices = null;
2670
+ this.indices = null;
2671
+ this.normals = null;
2672
+ this.uvs = null;
2673
+ this.subMeshes = [];
2674
+ this.itemSize = 3;
2675
+ this.primitive = 'TRIANGLES';
2676
+ }
2677
+ }
2678
+
2679
+ class Line extends BasicMesh {
2680
+ thickness;
2681
+ constructor(vertices = [], thickness = 2.0) {
2682
+ super();
2683
+ this.thickness = (thickness || 2.0) * 0.5;
2684
+ this.itemSize = 3;
2685
+ this.primitive = 'LINE_STRIP';
2686
+ this.setPoints(vertices);
2687
+ }
2688
+ setPoints(vertices) {
2689
+ const flat = [];
2690
+ if (vertices)
2691
+ for (const v of vertices)
2692
+ flat.push(v.x, v.y, v.z);
2693
+ this.vertices = flat;
2694
+ this.subMeshes = [
2695
+ new SubMesh(0, this.vertices.length / this.itemSize)
2696
+ ];
2697
+ return this;
2698
+ }
2699
+ }
2700
+
2701
+ class FullscreenQuad extends BasicMesh {
2702
+ constructor() {
2703
+ super();
2704
+ this.vertices = [1.0, -1,
2705
+ -1, -1,
2706
+ 1.0, 1.0,
2707
+ -1, 1.0
2708
+ ];
2709
+ this.uvs = [1.0, 0.0,
2710
+ 0.0, 0.0,
2711
+ 1.0, 1.0,
2712
+ 0.0, 1.0
2713
+ ];
2714
+ this.subMeshes = [
2715
+ new SubMesh(0, 4)
2716
+ ];
2717
+ this.itemSize = 2;
2718
+ this.primitive = 'TRIANGLE_STRIP';
2719
+ }
2720
+ }
2721
+
2722
+ class Quad extends BasicMesh {
2723
+ constructor(size = new Vec2(2.0, 2.0)) {
2724
+ super();
2725
+ let width;
2726
+ let h;
2727
+ if (size instanceof Rect) {
2728
+ width = size.size.x * 0.5;
2729
+ h = size.size.y * 0.5;
2730
+ }
2731
+ else {
2732
+ width = size.x * 0.5;
2733
+ h = size.y * 0.5;
2734
+ }
2735
+ this.vertices = [width, -h, 0.0,
2736
+ -width, -h, 0.0,
2737
+ width, h, 0.0,
2738
+ -width, h, 0.0
2739
+ ];
2740
+ this.uvs = [1.0, 0.0,
2741
+ 0.0, 0.0,
2742
+ 1.0, 1.0,
2743
+ 0.0, 1.0
2744
+ ];
2745
+ this.subMeshes = [
2746
+ new SubMesh(0, 4)
2747
+ ];
2748
+ this.itemSize = 3;
2749
+ this.primitive = 'TRIANGLE_STRIP';
2750
+ }
2751
+ }
2752
+
2753
+ class MultiQuad extends BasicMesh {
2754
+ quad;
2755
+ constructor(size = new Vec2(2.0, 2.0)) {
2756
+ super();
2757
+ let width;
2758
+ let h;
2759
+ if (size instanceof Rect) {
2760
+ width = size.size.x * 0.5;
2761
+ h = size.size.y * 0.5;
2762
+ }
2763
+ else {
2764
+ width = size.x * 0.5;
2765
+ h = size.y * 0.5;
2766
+ }
2767
+ this.quad = {
2768
+ vertices: new Float32Array([width, -h, 0.0,
2769
+ -width, h, 0.0,
2770
+ -width, -h, 0.0,
2771
+ width, h, 0.0
2772
+ ]),
2773
+ indices: new Int32Array([0, 1, 2,
2774
+ 0, 3, 1
2775
+ ]),
2776
+ uvs: new Float32Array([1.0, 0.0,
2777
+ 0.0, 1.0,
2778
+ 0.0, 0.0,
2779
+ 1.0, 1.0
2780
+ ])
2781
+ };
2782
+ this.vertices = [];
2783
+ this.indices = [];
2784
+ this.uvs = [];
2785
+ this.subMeshes = [
2786
+ new SubMesh(0, 4)
2787
+ ];
2788
+ }
2789
+ createQuads(length) {
2790
+ for (let i = 0; i < length; i++) {
2791
+ this.vertices.push(...this.quad.vertices);
2792
+ this.indices.push(...this.createIndices(i));
2793
+ this.uvs.push(...this.quad.uvs);
2794
+ }
2795
+ if (this.subMeshes.length > 0 && length > 0)
2796
+ this.subMeshes[0].count = this.indices.length;
2797
+ }
2798
+ createIndices(quadIndex) {
2799
+ const indices = [];
2800
+ for (let i = 0; i < 6; i++)
2801
+ indices.push(this.quad.indices[i] + quadIndex * 4);
2802
+ return indices;
2803
+ }
2804
+ }
2805
+
2806
+ class Cube extends BasicMesh {
2807
+ constructor(size = new Vec3(2.0, 2.0, 2.0), flat = false) {
2808
+ super();
2809
+ const halfX = (size ? size.x : 2.0) * 0.5;
2810
+ const halfY = (size ? size.y : 2.0) * 0.5;
2811
+ const halfZ = (size ? size.z : 2.0) * 0.5;
2812
+ const sx = halfX || 1.0;
2813
+ const sy = halfY || 1.0;
2814
+ const sz = halfZ || 1.0;
2815
+ if (flat) {
2816
+ this.vertices = [
2817
+ -sx, -sy, sz, sx, -sy, sz, sx, sy, sz, -sx, sy, sz,
2818
+ sx, -sy, -sz, -sx, -sy, -sz, -sx, sy, -sz, sx, sy, -sz,
2819
+ -sx, sy, sz, sx, sy, sz, sx, sy, -sz, -sx, sy, -sz,
2820
+ -sx, -sy, -sz, sx, -sy, -sz, sx, -sy, sz, -sx, -sy, sz,
2821
+ sx, -sy, sz, sx, -sy, -sz, sx, sy, -sz, sx, sy, sz,
2822
+ -sx, -sy, -sz, -sx, -sy, sz, -sx, sy, sz, -sx, sy, -sz
2823
+ ];
2824
+ this.normals = [
2825
+ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
2826
+ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
2827
+ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
2828
+ 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
2829
+ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
2830
+ -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0
2831
+ ];
2832
+ this.indices = [
2833
+ 0, 1, 2, 0, 2, 3,
2834
+ 4, 5, 6, 4, 6, 7,
2835
+ 8, 9, 10, 8, 10, 11,
2836
+ 12, 13, 14, 12, 14, 15,
2837
+ 16, 17, 18, 16, 18, 19,
2838
+ 20, 21, 22, 20, 22, 23
2839
+ ];
2840
+ this.subMeshes = [
2841
+ new SubMesh(0, 36)
2842
+ ];
2843
+ return;
2844
+ }
2845
+ this.vertices = [sx, -sy, -sz,
2846
+ -sx, -sy, sz,
2847
+ sx, -sy, sz,
2848
+ -sx, sy, sz,
2849
+ sx, sy, -sz,
2850
+ sx, sy, sz,
2851
+ -sx, -sy, -sz,
2852
+ -sx, sy, -sz
2853
+ ];
2854
+ this.indices = [2, 5, 3, 2, 3, 1,
2855
+ 4, 0, 6, 4, 6, 7,
2856
+ 3, 5, 4, 3, 4, 7,
2857
+ 0, 2, 1, 0, 1, 6,
2858
+ 5, 2, 0, 5, 0, 4,
2859
+ 6, 1, 3, 6, 3, 7
2860
+ ];
2861
+ this.normals = [0.5773, -0.5773, -0.5773,
2862
+ -0.5773, -0.5773, 0.5773,
2863
+ 0.5773, -0.5773, 0.5773,
2864
+ -0.5773, 0.5773, 0.5773,
2865
+ 0.5773, 0.5773, -0.5773,
2866
+ 0.5773, 0.5773, 0.5773,
2867
+ -0.5773, -0.5773, -0.5773,
2868
+ -0.5773, 0.5773, -0.5773
2869
+ ];
2870
+ this.subMeshes = [
2871
+ new SubMesh(0, 36)
2872
+ ];
2873
+ }
2874
+ }
2875
+
2876
+ class Sphere extends BasicMesh {
2877
+ constructor(scale = new Vec3(1.0, 1.0, 1.0)) {
2878
+ super();
2879
+ this.vertices = [-0.7071, -0.7071, 0, -0.5449, -0.8315, 0.1084, -0.5556, -0.8315, 0, -0.8315, 0.5556, 0, -0.9061, 0.3827, 0.1802, -0.9239, 0.3827, 0, -0.3753, -0.9239, 0.0747, -0.3827, -0.9239, 0, -0.9619, 0.1951, 0.1913, -0.9808, 0.1951, 0, -0.1913, -0.9808, 0.0381, -0.1951, -0.9808, 0, -0.9808, 0, 0.1951, -1, 0, 0, -0.1951, 0.9808, 0, 0, 1, 0, -0.1913, 0.9808, 0.0381, 0, -1, 0, -0.9619, -0.1951, 0.1913, -0.9808, -0.1951, 0, -0.3753, 0.9239, 0.0747, -0.3827, 0.9239, 0, -0.9239, -0.3827, 0, -0.9061, -0.3827, 0.1802, -0.5556, 0.8315, 0, -0.5449, 0.8315, 0.1084, -0.8155, -0.5556, 0.1622, -0.8315, -0.5556, 0, -0.6935, 0.7071, 0.1379, -0.7071, 0.7071, 0, -0.6935, -0.7071, 0.1379, -0.8155, 0.5556, 0.1622, -0.7682, -0.5556, 0.3182, -0.6533, -0.7071, 0.2706, -0.7682, 0.5556, 0.3182, -0.5133, -0.8315, 0.2126, -0.8536, 0.3827, 0.3536, -0.3536, -0.9239, 0.1464, -0.9061, 0.1951, 0.3753, -0.1802, -0.9808, 0.0747, -0.9239, 0, 0.3827, -0.1802, 0.9808, 0.0747, -0.9061, -0.1951, 0.3753, -0.3536, 0.9239, 0.1464, -0.8536, -0.3827, 0.3536, -0.5133, 0.8315, 0.2126, -0.6533, 0.7071, 0.2706, -0.1622, 0.9808, 0.1084, -0.1622, -0.9808, 0.1084, -0.8155, -0.1951, 0.5449, -0.3182, 0.9239, 0.2126, -0.7682, -0.3827, 0.5133, -0.4619, 0.8315, 0.3087, -0.6913, -0.5556, 0.4619, -0.5879, 0.7071, 0.3928, -0.5879, -0.7071, 0.3928, -0.6913, 0.5556, 0.4619, -0.4619, -0.8315, 0.3087, -0.7682, 0.3827, 0.5133, -0.3182, -0.9239, 0.2126, -0.8155, 0.1951, 0.5449, -0.8315, 0, 0.5556, -0.5879, 0.5556, 0.5879, -0.3928, -0.8315, 0.3928, -0.6533, 0.3827, 0.6533, -0.2706, -0.9239, 0.2706, -0.6935, 0.1951, 0.6935, -0.1379, -0.9808, 0.1379, -0.7071, 0, 0.7071, -0.1379, 0.9808, 0.1379, -0.6935, -0.1951, 0.6935, -0.2706, 0.9239, 0.2706, -0.6533, -0.3827, 0.6533, -0.3928, 0.8315, 0.3928, -0.5879, -0.5556, 0.5879, -0.5, 0.7071, 0.5, -0.5, -0.7071, 0.5, -0.5449, -0.1951, 0.8155, -0.2126, 0.9239, 0.3182, -0.5133, -0.3827, 0.7682, -0.3087, 0.8315, 0.4619, -0.4619, -0.5556, 0.6913, -0.3928, 0.7071, 0.5879, -0.3928, -0.7071, 0.5879, -0.4619, 0.5556, 0.6913, -0.3087, -0.8315, 0.4619, -0.5133, 0.3827, 0.7682, -0.2126, -0.9239, 0.3182, -0.5449, 0.1951, 0.8155, -0.1084, -0.9808, 0.1622, -0.5556, 0, 0.8315, -0.1084, 0.9808, 0.1622, -0.2126, -0.8315, 0.5133, -0.3536, 0.3827, 0.8536, -0.1464, -0.9239, 0.3536, -0.3753, 0.1951, 0.9061, -0.0747, -0.9808, 0.1802, -0.3827, 0, 0.9239, -0.0747, 0.9808, 0.1802, -0.3753, -0.1951, 0.9061, -0.1464, 0.9239, 0.3536, -0.3536, -0.3827, 0.8536, -0.2126, 0.8315, 0.5133, -0.3182, -0.5556, 0.7682, -0.2706, 0.7071, 0.6533, -0.2706, -0.7071, 0.6533, -0.3182, 0.5556, 0.7682, -0.1802, -0.3827, 0.9061, -0.1084, 0.8315, 0.5449, -0.1622, -0.5556, 0.8155, -0.1379, 0.7071, 0.6935, -0.1379, -0.7071, 0.6935, -0.1622, 0.5556, 0.8155, -0.1084, -0.8315, 0.5449, -0.1802, 0.3827, 0.9061, -0.0747, -0.9239, 0.3753, -0.1913, 0.1951, 0.9619, -0.0381, -0.9808, 0.1913, -0.1951, 0, 0.9808, -0.0381, 0.9808, 0.1913, -0.1913, -0.1951, 0.9619, -0.0747, 0.9239, 0.3753, 0, -0.8315, 0.5556, 0, -0.9239, 0.3827, 0, 0.3827, 0.9239, 0, 0.1951, 0.9808, 0, -0.9808, 0.1951, 0, 0, 1, 0, 0.9808, 0.1951, 0, -0.1951, 0.9808, 0, 0.9239, 0.3827, 0, -0.3827, 0.9239, 0, 0.8315, 0.5556, 0, -0.5556, 0.8315, 0, 0.7071, 0.7071, 0, -0.7071, 0.7071, 0, 0.5556, 0.8315, 0.1084, 0.8315, 0.5449, 0.1802, -0.3827, 0.9061, 0.1622, -0.5556, 0.8155, 0.138, 0.7071, 0.6935, 0.138, -0.7071, 0.6935, 0.1622, 0.5556, 0.8155, 0.1084, -0.8315, 0.5449, 0.1802, 0.3827, 0.9061, 0.0747, -0.9239, 0.3753, 0.1913, 0.1951, 0.9619, 0.0381, -0.9808, 0.1913, 0.1951, 0, 0.9808, 0.0381, 0.9808, 0.1913, 0.1913, -0.1951, 0.9619, 0.0747, 0.9239, 0.3753, 0.3536, 0.3827, 0.8536, 0.3753, 0.1951, 0.9061, 0.0747, -0.9808, 0.1802, 0.3827, 0, 0.9239, 0.0747, 0.9808, 0.1802, 0.3753, -0.1951, 0.9061, 0.1464, 0.9239, 0.3536, 0.3536, -0.3827, 0.8536, 0.2126, 0.8315, 0.5133, 0.3182, -0.5556, 0.7682, 0.2706, 0.7071, 0.6533, 0.2706, -0.7071, 0.6533, 0.3182, 0.5556, 0.7682, 0.2126, -0.8315, 0.5133, 0.1464, -0.9239, 0.3536, 0.5133, -0.3827, 0.7682, 0.4619, -0.5556, 0.6913, 0.3928, 0.7071, 0.5879, 0.3928, -0.7071, 0.5879, 0.4619, 0.5556, 0.6913, 0.3087, -0.8315, 0.4619, 0.5133, 0.3827, 0.7682, 0.2126, -0.9239, 0.3182, 0.5449, 0.1951, 0.8155, 0.1084, -0.9808, 0.1622, 0.5556, 0, 0.8315, 0.1084, 0.9808, 0.1622, 0.5449, -0.1951, 0.8155, 0.2126, 0.9239, 0.3182, 0.3087, 0.8315, 0.4619, 0.2706, -0.9239, 0.2706, 0.138, -0.9808, 0.1379, 0.7071, 0, 0.7071, 0.138, 0.9808, 0.1379, 0.6935, -0.1951, 0.6935, 0.2706, 0.9239, 0.2706, 0.6533, -0.3827, 0.6533, 0.3928, 0.8315, 0.3928, 0.5879, -0.5556, 0.5879, 0.5, 0.7071, 0.5, 0.5, -0.7071, 0.5, 0.5879, 0.5556, 0.5879, 0.3928, -0.8315, 0.3928, 0.6533, 0.3827, 0.6533, 0.6935, 0.1951, 0.6935, 0.4619, 0.8315, 0.3087, 0.5879, 0.7071, 0.3928, 0.5879, -0.7071, 0.3928, 0.6913, 0.5556, 0.4619, 0.4619, -0.8315, 0.3087, 0.7682, 0.3827, 0.5133, 0.3182, -0.9239, 0.2126, 0.8155, 0.1951, 0.5449, 0.1622, -0.9808, 0.1084, 0.8315, 0, 0.5556, 0.1622, 0.9808, 0.1084, 0.8155, -0.1951, 0.5449, 0.3182, 0.9239, 0.2126, 0.7682, -0.3827, 0.5133, 0.6913, -0.5556, 0.4619, 0.9239, 0, 0.3827, 0.1802, 0.9808, 0.0747, 0.1802, -0.9808, 0.0747, 0.9061, -0.1951, 0.3753, 0.3536, 0.9239, 0.1464, 0.8536, -0.3827, 0.3536, 0.5133, 0.8315, 0.2126, 0.7682, -0.5556, 0.3182, 0.6533, 0.7071, 0.2706, 0.6533, -0.7071, 0.2706, 0.7682, 0.5556, 0.3182, 0.5133, -0.8315, 0.2126, 0.8536, 0.3827, 0.3536, 0.3536, -0.9239, 0.1464, 0.9061, 0.1951, 0.3753, 0.6935, -0.7071, 0.1379, 0.8155, 0.5556, 0.1622, 0.5449, -0.8315, 0.1084, 0.9061, 0.3827, 0.1802, 0.3753, -0.9239, 0.0747, 0.9619, 0.1951, 0.1913, 0.1913, -0.9808, 0.0381, 0.9808, 0, 0.1951, 0.1913, 0.9808, 0.0381, 0.9619, -0.1951, 0.1913, 0.3753, 0.9239, 0.0747, 0.9061, -0.3827, 0.1802, 0.5449, 0.8315, 0.1084, 0.8155, -0.5556, 0.1622, 0.6935, 0.7071, 0.1379, 0.1951, -0.9808, 0, 0.9808, -0.1951, 0, 0.3827, 0.9239, 0, 0.9239, -0.3827, 0, 0.5556, 0.8315, 0, 0.8315, -0.5556, 0, 0.7071, 0.7071, 0, 0.7071, -0.7071, 0, 0.8315, 0.5556, 0, 0.5556, -0.8315, 0, 0.9239, 0.3827, 0, 0.3827, -0.9239, 0, 0.9808, 0.1951, 0, 1, 0, 0, 0.1951, 0.9808, 0, 0.5449, -0.8315, -0.1084, 0.9061, 0.3827, -0.1802, 0.3753, -0.9239, -0.0747, 0.9619, 0.1951, -0.1913, 0.1913, -0.9808, -0.0381, 0.9808, 0, -0.1951, 0.1913, 0.9808, -0.0381, 0.9619, -0.1951, -0.1913, 0.3753, 0.9239, -0.0747, 0.9061, -0.3827, -0.1802, 0.5449, 0.8315, -0.1084, 0.8155, -0.5556, -0.1622, 0.6935, 0.7071, -0.1379, 0.6935, -0.7071, -0.1379, 0.8155, 0.5556, -0.1622, 0.3536, 0.9239, -0.1464, 0.8536, -0.3827, -0.3536, 0.5133, 0.8315, -0.2126, 0.7682, -0.5556, -0.3182, 0.6533, 0.7071, -0.2706, 0.6533, -0.7071, -0.2706, 0.7682, 0.5556, -0.3182, 0.5133, -0.8315, -0.2126, 0.8536, 0.3827, -0.3536, 0.3536, -0.9239, -0.1464, 0.9061, 0.1951, -0.3753, 0.1802, -0.9808, -0.0747, 0.9239, 0, -0.3827, 0.1802, 0.9808, -0.0747, 0.9061, -0.1951, -0.3753, 0.7682, 0.3827, -0.5133, 0.3182, -0.9239, -0.2126, 0.8155, 0.1951, -0.5449, 0.1622, -0.9808, -0.1084, 0.8315, 0, -0.5556, 0.1622, 0.9808, -0.1084, 0.8155, -0.1951, -0.5449, 0.3182, 0.9239, -0.2126, 0.7682, -0.3827, -0.5133, 0.4619, 0.8315, -0.3087, 0.6913, -0.5556, -0.4619, 0.5879, 0.7071, -0.3928, 0.5879, -0.7071, -0.3928, 0.6913, 0.5556, -0.4619, 0.4619, -0.8315, -0.3087, 0.6533, -0.3827, -0.6533, 0.3928, 0.8315, -0.3928, 0.5879, -0.5556, -0.5879, 0.5, 0.7071, -0.5, 0.5, -0.7071, -0.5, 0.5879, 0.5556, -0.5879, 0.3928, -0.8315, -0.3928, 0.6533, 0.3827, -0.6533, 0.2706, -0.9239, -0.2706, 0.6935, 0.1951, -0.6935, 0.1379, -0.9808, -0.1379, 0.7071, 0, -0.7071, 0.138, 0.9808, -0.1379, 0.6935, -0.1951, -0.6935, 0.2706, 0.9239, -0.2706, 0.3087, -0.8315, -0.4619, 0.2126, -0.9239, -0.3182, 0.5133, 0.3827, -0.7682, 0.5449, 0.1951, -0.8155, 0.1084, -0.9808, -0.1622, 0.5556, 0, -0.8315, 0.1084, 0.9808, -0.1622, 0.5449, -0.1951, -0.8155, 0.2126, 0.9239, -0.3182, 0.5133, -0.3827, -0.7682, 0.3087, 0.8315, -0.4619, 0.4619, -0.5556, -0.6913, 0.3928, 0.7071, -0.5879, 0.3928, -0.7071, -0.5879, 0.4619, 0.5556, -0.6913, 0.2126, 0.8315, -0.5133, 0.3536, -0.3827, -0.8536, 0.3182, -0.5556, -0.7682, 0.2706, 0.7071, -0.6533, 0.2706, -0.7071, -0.6533, 0.3182, 0.5556, -0.7682, 0.2126, -0.8315, -0.5133, 0.3536, 0.3827, -0.8536, 0.1464, -0.9239, -0.3536, 0.3753, 0.1951, -0.9061, 0.0747, -0.9808, -0.1802, 0.3827, 0, -0.9239, 0.0747, 0.9808, -0.1802, 0.3753, -0.1951, -0.9061, 0.1464, 0.9239, -0.3536, 0.1802, 0.3827, -0.9061, 0.1913, 0.1951, -0.9619, 0.0381, -0.9808, -0.1913, 0.1951, 0, -0.9808, 0.0381, 0.9808, -0.1913, 0.1913, -0.1951, -0.9619, 0.0747, 0.9239, -0.3753, 0.1802, -0.3827, -0.9061, 0.1084, 0.8315, -0.5449, 0.1622, -0.5556, -0.8155, 0.1379, 0.7071, -0.6935, 0.1379, -0.7071, -0.6935, 0.1622, 0.5556, -0.8155, 0.1084, -0.8315, -0.5449, 0.0747, -0.9239, -0.3753, 0, -0.3827, -0.9239, 0, -0.5556, -0.8315, 0, 0.8315, -0.5556, 0, 0.7071, -0.7071, 0, -0.7071, -0.7071, 0, 0.5556, -0.8315, 0, -0.8315, -0.5556, 0, 0.3827, -0.9239, 0, -0.9239, -0.3827, 0, 0.1951, -0.9808, 0, -0.9808, -0.1951, 0, 0, -1, 0, 0.9808, -0.1951, 0, -0.1951, -0.9808, 0, 0.9239, -0.3827, -0.0381, -0.9808, -0.1913, -0.1951, 0, -0.9808, -0.0381, 0.9808, -0.1913, -0.1913, -0.1951, -0.9619, -0.0747, 0.9239, -0.3753, -0.1802, -0.3827, -0.9061, -0.1084, 0.8315, -0.5449, -0.1622, -0.5556, -0.8155, -0.1379, 0.7071, -0.6935, -0.1379, -0.7071, -0.6935, -0.1622, 0.5556, -0.8155, -0.1084, -0.8315, -0.5449, -0.1802, 0.3827, -0.9061, -0.0747, -0.9239, -0.3753, -0.1913, 0.1951, -0.9619, -0.2706, -0.7071, -0.6533, -0.3182, 0.5556, -0.7682, -0.2126, -0.8315, -0.5133, -0.3536, 0.3827, -0.8536, -0.1464, -0.9239, -0.3536, -0.3753, 0.1951, -0.9061, -0.0747, -0.9808, -0.1802, -0.3827, 0, -0.9239, -0.0747, 0.9808, -0.1802, -0.3753, -0.1951, -0.9061, -0.1464, 0.9239, -0.3536, -0.3536, -0.3827, -0.8536, -0.2126, 0.8315, -0.5133, -0.3182, -0.5556, -0.7682, -0.2706, 0.7071, -0.6533, -0.1084, 0.9808, -0.1622, -0.1084, -0.9808, -0.1622, -0.5556, 0, -0.8315, -0.5449, -0.1951, -0.8155, -0.2126, 0.9239, -0.3182, -0.5133, -0.3827, -0.7682, -0.3087, 0.8315, -0.4619, -0.4619, -0.5556, -0.6913, -0.3928, 0.7071, -0.5879, -0.3928, -0.7071, -0.5879, -0.4619, 0.5556, -0.6913, -0.3087, -0.8315, -0.4619, -0.5133, 0.3827, -0.7682, -0.2126, -0.9239, -0.3182, -0.5449, 0.1951, -0.8155, -0.5, 0.7071, -0.5, -0.5879, 0.5556, -0.5879, -0.3928, -0.8315, -0.3928, -0.6533, 0.3827, -0.6533, -0.2706, -0.9239, -0.2706, -0.6935, 0.1951, -0.6935, -0.1379, -0.9808, -0.1379, -0.7071, 0, -0.7071, -0.1379, 0.9808, -0.1379, -0.6935, -0.1951, -0.6935, -0.2706, 0.9239, -0.2706, -0.6533, -0.3827, -0.6533, -0.3928, 0.8315, -0.3928, -0.5879, -0.5556, -0.5879, -0.5, -0.7071, -0.5, -0.8155, -0.1951, -0.5449, -0.3182, 0.9239, -0.2126, -0.7682, -0.3827, -0.5133, -0.4619, 0.8315, -0.3087, -0.6913, -0.5556, -0.4619, -0.5879, 0.7071, -0.3928, -0.5879, -0.7071, -0.3928, -0.6913, 0.5556, -0.4619, -0.4619, -0.8315, -0.3087, -0.7682, 0.3827, -0.5133, -0.3182, -0.9239, -0.2126, -0.8155, 0.1951, -0.5449, -0.1622, -0.9808, -0.1084, -0.8315, 0, -0.5556, -0.1622, 0.9808, -0.1084, -0.5133, -0.8315, -0.2126, -0.8536, 0.3827, -0.3536, -0.3536, -0.9239, -0.1464, -0.9061, 0.1951, -0.3753, -0.1802, -0.9808, -0.0747, -0.9239, 0, -0.3827, -0.1802, 0.9808, -0.0747, -0.9061, -0.1951, -0.3753, -0.3536, 0.9239, -0.1464, -0.8536, -0.3827, -0.3536, -0.5133, 0.8315, -0.2126, -0.7682, -0.5556, -0.3182, -0.6533, 0.7071, -0.2706, -0.6533, -0.7071, -0.2706, -0.7682, 0.5556, -0.3182, -0.3753, 0.9239, -0.0747, -0.9061, -0.3827, -0.1802, -0.5449, 0.8315, -0.1084, -0.8155, -0.5556, -0.1622, -0.6935, 0.7071, -0.1379, -0.6935, -0.7071, -0.1379, -0.8155, 0.5556, -0.1622, -0.5449, -0.8315, -0.1084, -0.9061, 0.3827, -0.1802, -0.3753, -0.9239, -0.0747, -0.9619, 0.1951, -0.1913, -0.1913, -0.9808, -0.0381, -0.9808, 0, -0.1951, -0.1913, 0.9808, -0.0381, -0.9619, -0.1951, -0.1913
2880
+ ];
2881
+ this.indices = [0, 1, 2, 3, 4, 5, 2, 6, 7, 5, 8, 9, 7, 10, 11, 9, 12, 13, 14, 15, 16, 17, 11, 10, 13, 18, 19, 14, 20, 21, 22, 18, 23, 24, 20, 25, 22, 26, 27, 24, 28, 29, 0, 26, 30, 29, 31, 3, 30, 32, 33, 28, 34, 31, 30, 35, 1, 31, 36, 4, 1, 37, 6, 4, 38, 8, 6, 39, 10, 8, 40, 12, 16, 15, 41, 17, 10, 39, 18, 40, 42, 16, 43, 20, 23, 42, 44, 20, 45, 25, 23, 32, 26, 25, 46, 28, 41, 15, 47, 17, 39, 48, 40, 49, 42, 41, 50, 43, 42, 51, 44, 43, 52, 45, 32, 51, 53, 45, 54, 46, 33, 53, 55, 46, 56, 34, 33, 57, 35, 34, 58, 36, 35, 59, 37, 38, 58, 60, 37, 48, 39, 38, 61, 40, 54, 62, 56, 55, 63, 57, 56, 64, 58, 57, 65, 59, 58, 66, 60, 59, 67, 48, 61, 66, 68, 47, 15, 69, 17, 48, 67, 61, 70, 49, 47, 71, 50, 49, 72, 51, 50, 73, 52, 53, 72, 74, 52, 75, 54, 53, 76, 55, 68, 77, 70, 69, 78, 71, 70, 79, 72, 71, 80, 73, 74, 79, 81, 73, 82, 75, 74, 83, 76, 62, 82, 84, 63, 83, 85, 62, 86, 64, 65, 85, 87, 64, 88, 66, 65, 89, 67, 68, 88, 90, 69, 15, 91, 17, 67, 89, 83, 92, 85, 84, 93, 86, 87, 92, 94, 88, 93, 95, 87, 96, 89, 88, 97, 90, 91, 15, 98, 17, 89, 96, 90, 99, 77, 91, 100, 78, 77, 101, 79, 78, 102, 80, 81, 101, 103, 80, 104, 82, 83, 103, 105, 84, 104, 106, 99, 107, 101, 100, 108, 102, 103, 107, 109, 104, 108, 110, 105, 109, 111, 104, 112, 106, 105, 113, 92, 106, 114, 93, 92, 115, 94, 95, 114, 116, 96, 115, 117, 95, 118, 97, 98, 15, 119, 17, 96, 117, 97, 120, 99, 98, 121, 100, 115, 122, 123, 116, 124, 125, 117, 123, 126, 116, 127, 118, 119, 15, 128, 17, 117, 126, 120, 127, 129, 119, 130, 121, 120, 131, 107, 121, 132, 108, 109, 131, 133, 108, 134, 110, 109, 135, 111, 110, 136, 112, 113, 135, 122, 112, 124, 114, 130, 137, 132, 133, 138, 139, 132, 140, 134, 133, 141, 135, 136, 140, 142, 122, 141, 143, 136, 144, 124, 122, 145, 123, 125, 144, 146, 126, 145, 147, 125, 148, 127, 128, 15, 149, 17, 126, 147, 127, 150, 129, 130, 149, 151, 129, 138, 131, 146, 152, 153, 145, 154, 147, 146, 155, 148, 149, 15, 156, 17, 147, 154, 148, 157, 150, 149, 158, 151, 150, 159, 138, 137, 158, 160, 139, 159, 161, 137, 162, 140, 139, 163, 141, 140, 164, 142, 141, 165, 143, 142, 152, 144, 145, 165, 166, 161, 167, 168, 160, 169, 162, 161, 170, 163, 164, 169, 171, 165, 170, 172, 164, 173, 152, 165, 174, 166, 153, 173, 175, 166, 176, 154, 153, 177, 155, 156, 15, 178, 17, 154, 176, 157, 177, 179, 156, 180, 158, 157, 167, 159, 158, 181, 160, 176, 182, 183, 175, 184, 177, 178, 15, 185, 17, 176, 183, 177, 186, 179, 178, 187, 180, 179, 188, 167, 180, 189, 181, 167, 190, 168, 181, 191, 169, 168, 192, 170, 169, 193, 171, 172, 192, 194, 171, 195, 173, 172, 182, 174, 175, 195, 196, 191, 197, 198, 190, 199, 192, 193, 198, 200, 194, 199, 201, 193, 202, 195, 194, 203, 182, 196, 202, 204, 183, 203, 205, 196, 206, 184, 185, 15, 207, 17, 183, 205, 186, 206, 208, 185, 209, 187, 186, 210, 188, 187, 197, 189, 190, 210, 211, 204, 212, 206, 207, 15, 213, 17, 205, 214, 206, 215, 208, 207, 216, 209, 208, 217, 210, 197, 216, 218, 211, 217, 219, 197, 220, 198, 211, 221, 199, 198, 222, 200, 201, 221, 223, 200, 224, 202, 201, 225, 203, 204, 224, 226, 203, 214, 205, 219, 227, 221, 220, 228, 222, 221, 229, 223, 222, 230, 224, 223, 231, 225, 226, 230, 232, 214, 231, 233, 226, 234, 212, 213, 15, 235, 17, 214, 233, 212, 236, 215, 213, 237, 216, 215, 238, 217, 216, 239, 218, 219, 238, 240, 218, 241, 220, 17, 233, 242, 234, 243, 236, 235, 244, 237, 236, 245, 238, 239, 244, 246, 240, 245, 247, 241, 246, 248, 240, 249, 227, 241, 250, 228, 229, 249, 251, 228, 252, 230, 229, 253, 231, 232, 252, 254, 233, 253, 242, 232, 255, 234, 235, 15, 256, 249, 257, 251, 250, 258, 252, 251, 259, 253, 254, 258, 260, 253, 261, 242, 254, 262, 255, 256, 15, 263, 17, 242, 261, 255, 264, 243, 256, 265, 244, 243, 266, 245, 244, 267, 246, 247, 266, 268, 248, 267, 269, 247, 270, 249, 250, 269, 271, 263, 272, 265, 264, 273, 266, 265, 274, 267, 268, 273, 275, 269, 274, 276, 268, 277, 270, 271, 276, 278, 270, 279, 257, 271, 280, 258, 259, 279, 281, 260, 280, 282, 261, 281, 283, 260, 284, 262, 263, 15, 285, 17, 261, 283, 264, 284, 286, 278, 287, 280, 279, 288, 281, 282, 287, 289, 283, 288, 290, 282, 291, 284, 285, 15, 292, 17, 283, 290, 284, 293, 286, 285, 294, 272, 286, 295, 273, 272, 296, 274, 275, 295, 297, 274, 298, 276, 275, 299, 277, 278, 298, 300, 279, 299, 301, 293, 302, 295, 294, 303, 296, 297, 302, 304, 298, 303, 305, 297, 306, 299, 300, 305, 307, 299, 308, 301, 300, 309, 287, 301, 310, 288, 289, 309, 311, 288, 312, 290, 291, 311, 313, 292, 15, 314, 17, 290, 312, 291, 315, 293, 292, 316, 294, 310, 317, 318, 311, 319, 320, 310, 321, 312, 311, 322, 313, 314, 15, 323, 17, 312, 321, 313, 324, 315, 314, 325, 316, 315, 326, 302, 316, 327, 303, 304, 326, 328, 305, 327, 329, 304, 330, 306, 307, 329, 331, 306, 317, 308, 307, 319, 309, 325, 332, 327, 328, 333, 334, 329, 332, 335, 330, 334, 336, 331, 335, 337, 330, 338, 317, 331, 339, 319, 318, 338, 340, 320, 339, 341, 321, 340, 342, 320, 343, 322, 323, 15, 344, 17, 321, 342, 322, 345, 324, 323, 346, 325, 324, 333, 326, 341, 347, 348, 340, 349, 342, 341, 350, 343, 344, 15, 351, 17, 342, 349, 343, 352, 345, 346, 351, 353, 345, 354, 333, 346, 355, 332, 334, 354, 356, 335, 355, 357, 334, 358, 336, 337, 357, 359, 336, 360, 338, 337, 347, 339, 340, 360, 361, 356, 362, 363, 357, 364, 365, 356, 366, 358, 357, 367, 359, 360, 366, 368, 359, 369, 347, 361, 368, 370, 348, 369, 371, 361, 372, 349, 348, 373, 350, 351, 15, 374, 17, 349, 372, 352, 373, 375, 353, 374, 376, 352, 362, 354, 353, 364, 355, 370, 377, 372, 371, 378, 373, 374, 15, 379, 17, 372, 377, 375, 378, 380, 376, 379, 381, 375, 382, 362, 376, 383, 364, 363, 382, 384, 365, 383, 385, 363, 386, 366, 367, 385, 387, 366, 388, 368, 367, 389, 369, 370, 388, 390, 371, 389, 391, 384, 392, 386, 385, 393, 387, 386, 394, 388, 387, 395, 389, 390, 394, 396, 391, 395, 397, 390, 398, 377, 391, 399, 378, 379, 15, 400, 17, 377, 398, 378, 401, 380, 379, 402, 381, 380, 403, 382, 381, 404, 383, 384, 403, 405, 385, 404, 406, 400, 15, 407, 17, 398, 408, 401, 409, 410, 400, 411, 402, 401, 412, 403, 404, 411, 413, 405, 412, 414, 406, 413, 415, 405, 416, 392, 393, 415, 417, 392, 418, 394, 393, 419, 395, 396, 418, 420, 397, 419, 421, 396, 408, 398, 397, 409, 399, 417, 422, 423, 416, 424, 418, 417, 425, 419, 418, 426, 420, 421, 425, 427, 420, 428, 408, 421, 429, 409, 407, 15, 430, 17, 408, 428, 410, 429, 431, 407, 432, 411, 410, 433, 412, 411, 434, 413, 414, 433, 435, 413, 422, 415, 414, 436, 416, 429, 437, 431, 430, 438, 432, 431, 439, 433, 432, 440, 434, 435, 439, 441, 434, 442, 422, 435, 443, 436, 423, 442, 444, 424, 443, 445, 423, 446, 425, 424, 447, 426, 427, 446, 448, 426, 449, 428, 429, 448, 450, 430, 15, 451, 17, 428, 449, 443, 452, 445, 444, 453, 446, 445, 454, 447, 448, 453, 455, 447, 456, 449, 448, 457, 450, 451, 15, 458, 17, 449, 456, 450, 459, 437, 438, 458, 460, 437, 461, 439, 440, 460, 462, 441, 461, 463, 442, 462, 464, 441, 465, 443, 444, 464, 466, 458, 467, 460, 459, 468, 461, 460, 469, 462, 463, 468, 470, 462, 471, 464, 463, 472, 465, 466, 471, 473, 465, 474, 452, 466, 475, 453, 452, 476, 454, 455, 475, 477, 456, 476, 478, 455, 479, 457, 458, 15, 480, 17, 456, 478, 457, 481, 459, 475, 3, 5, 474, 7, 476, 477, 5, 9, 476, 11, 478, 479, 9, 13, 480, 15, 14, 17, 478, 11, 479, 19, 481, 467, 14, 21, 481, 22, 468, 469, 21, 24, 468, 27, 470, 471, 24, 29, 470, 0, 472, 473, 29, 3, 472, 2, 474, 0, 30, 1, 3, 31, 4, 2, 1, 6, 5, 4, 8, 7, 6, 10, 9, 8, 12, 13, 12, 18, 14, 16, 20, 22, 19, 18, 24, 21, 20, 22, 23, 26, 24, 25, 28, 0, 27, 26, 29, 28, 31, 30, 26, 32, 28, 46, 34, 30, 33, 35, 31, 34, 36, 1, 35, 37, 4, 36, 38, 6, 37, 39, 8, 38, 40, 18, 12, 40, 16, 41, 43, 23, 18, 42, 20, 43, 45, 23, 44, 32, 25, 45, 46, 40, 61, 49, 41, 47, 50, 42, 49, 51, 43, 50, 52, 32, 44, 51, 45, 52, 54, 33, 32, 53, 46, 54, 56, 33, 55, 57, 34, 56, 58, 35, 57, 59, 38, 36, 58, 37, 59, 48, 38, 60, 61, 54, 75, 62, 55, 76, 63, 56, 62, 64, 57, 63, 65, 58, 64, 66, 59, 65, 67, 61, 60, 66, 61, 68, 70, 47, 69, 71, 49, 70, 72, 50, 71, 73, 53, 51, 72, 52, 73, 75, 53, 74, 76, 68, 90, 77, 69, 91, 78, 70, 77, 79, 71, 78, 80, 74, 72, 79, 73, 80, 82, 74, 81, 83, 62, 75, 82, 63, 76, 83, 62, 84, 86, 65, 63, 85, 64, 86, 88, 65, 87, 89, 68, 66, 88, 83, 105, 92, 84, 106, 93, 87, 85, 92, 88, 86, 93, 87, 94, 96, 88, 95, 97, 90, 97, 99, 91, 98, 100, 77, 99, 101, 78, 100, 102, 81, 79, 101, 80, 102, 104, 83, 81, 103, 84, 82, 104, 99, 120, 107, 100, 121, 108, 103, 101, 107, 104, 102, 108, 105, 103, 109, 104, 110, 112, 105, 111, 113, 106, 112, 114, 92, 113, 115, 95, 93, 114, 96, 94, 115, 95, 116, 118, 97, 118, 120, 98, 119, 121, 115, 113, 122, 116, 114, 124, 117, 115, 123, 116, 125, 127, 120, 118, 127, 119, 128, 130, 120, 129, 131, 121, 130, 132, 109, 107, 131, 108, 132, 134, 109, 133, 135, 110, 134, 136, 113, 111, 135, 112, 136, 124, 130, 151, 137, 133, 131, 138, 132, 137, 140, 133, 139, 141, 136, 134, 140, 122, 135, 141, 136, 142, 144, 122, 143, 145, 125, 124, 144, 126, 123, 145, 125, 146, 148, 127, 148, 150, 130, 128, 149, 129, 150, 138, 146, 144, 152, 145, 166, 154, 146, 153, 155, 148, 155, 157, 149, 156, 158, 150, 157, 159, 137, 151, 158, 139, 138, 159, 137, 160, 162, 139, 161, 163, 140, 162, 164, 141, 163, 165, 142, 164, 152, 145, 143, 165, 161, 159, 167, 160, 181, 169, 161, 168, 170, 164, 162, 169, 165, 163, 170, 164, 171, 173, 165, 172, 174, 153, 152, 173, 166, 174, 176, 153, 175, 177, 157, 155, 177, 156, 178, 180, 157, 179, 167, 158, 180, 181, 176, 174, 182, 175, 196, 184, 177, 184, 186, 178, 185, 187, 179, 186, 188, 180, 187, 189, 167, 188, 190, 181, 189, 191, 168, 190, 192, 169, 191, 193, 172, 170, 192, 171, 193, 195, 172, 194, 182, 175, 173, 195, 191, 189, 197, 190, 211, 199, 193, 191, 198, 194, 192, 199, 193, 200, 202, 194, 201, 203, 196, 195, 202, 183, 182, 203, 196, 204, 206, 186, 184, 206, 185, 207, 209, 186, 208, 210, 187, 209, 197, 190, 188, 210, 204, 226, 212, 206, 212, 215, 207, 213, 216, 208, 215, 217, 197, 209, 216, 211, 210, 217, 197, 218, 220, 211, 219, 221, 198, 220, 222, 201, 199, 221, 200, 222, 224, 201, 223, 225, 204, 202, 224, 203, 225, 214, 219, 240, 227, 220, 241, 228, 221, 227, 229, 222, 228, 230, 223, 229, 231, 226, 224, 230, 214, 225, 231, 226, 232, 234, 212, 234, 236, 213, 235, 237, 215, 236, 238, 216, 237, 239, 219, 217, 238, 218, 239, 241, 234, 255, 243, 235, 256, 244, 236, 243, 245, 239, 237, 244, 240, 238, 245, 241, 239, 246, 240, 247, 249, 241, 248, 250, 229, 227, 249, 228, 250, 252, 229, 251, 253, 232, 230, 252, 233, 231, 253, 232, 254, 255, 249, 270, 257, 250, 271, 258, 251, 257, 259, 254, 252, 258, 253, 259, 261, 254, 260, 262, 255, 262, 264, 256, 263, 265, 243, 264, 266, 244, 265, 267, 247, 245, 266, 248, 246, 267, 247, 268, 270, 250, 248, 269, 263, 285, 272, 264, 286, 273, 265, 272, 274, 268, 266, 273, 269, 267, 274, 268, 275, 277, 271, 269, 276, 270, 277, 279, 271, 278, 280, 259, 257, 279, 260, 258, 280, 261, 259, 281, 260, 282, 284, 264, 262, 284, 278, 300, 287, 279, 301, 288, 282, 280, 287, 283, 281, 288, 282, 289, 291, 284, 291, 293, 285, 292, 294, 286, 293, 295, 272, 294, 296, 275, 273, 295, 274, 296, 298, 275, 297, 299, 278, 276, 298, 279, 277, 299, 293, 315, 302, 294, 316, 303, 297, 295, 302, 298, 296, 303, 297, 304, 306, 300, 298, 305, 299, 306, 308, 300, 307, 309, 301, 308, 310, 289, 287, 309, 288, 310, 312, 291, 289, 311, 291, 313, 315, 292, 314, 316, 310, 308, 317, 311, 309, 319, 310, 318, 321, 311, 320, 322, 313, 322, 324, 314, 323, 325, 315, 324, 326, 316, 325, 327, 304, 302, 326, 305, 303, 327, 304, 328, 330, 307, 305, 329, 306, 330, 317, 307, 331, 319, 325, 346, 332, 328, 326, 333, 329, 327, 332, 330, 328, 334, 331, 329, 335, 330, 336, 338, 331, 337, 339, 318, 317, 338, 320, 319, 339, 321, 318, 340, 320, 341, 343, 322, 343, 345, 323, 344, 346, 324, 345, 333, 341, 339, 347, 340, 361, 349, 341, 348, 350, 343, 350, 352, 346, 344, 351, 345, 352, 354, 346, 353, 355, 334, 333, 354, 335, 332, 355, 334, 356, 358, 337, 335, 357, 336, 358, 360, 337, 359, 347, 340, 338, 360, 356, 354, 362, 357, 355, 364, 356, 363, 366, 357, 365, 367, 360, 358, 366, 359, 367, 369, 361, 360, 368, 348, 347, 369, 361, 370, 372, 348, 371, 373, 352, 350, 373, 353, 351, 374, 352, 375, 362, 353, 376, 364, 370, 390, 377, 371, 391, 378, 375, 373, 378, 376, 374, 379, 375, 380, 382, 376, 381, 383, 363, 362, 382, 365, 364, 383, 363, 384, 386, 367, 365, 385, 366, 386, 388, 367, 387, 389, 370, 368, 388, 371, 369, 389, 384, 405, 392, 385, 406, 393, 386, 392, 394, 387, 393, 395, 390, 388, 394, 391, 389, 395, 390, 396, 398, 391, 397, 399, 378, 399, 401, 379, 400, 402, 380, 401, 403, 381, 402, 404, 384, 382, 403, 385, 383, 404, 401, 399, 409, 400, 407, 411, 401, 410, 412, 404, 402, 411, 405, 403, 412, 406, 404, 413, 405, 414, 416, 393, 406, 415, 392, 416, 418, 393, 417, 419, 396, 394, 418, 397, 395, 419, 396, 420, 408, 397, 421, 409, 417, 415, 422, 416, 436, 424, 417, 423, 425, 418, 424, 426, 421, 419, 425, 420, 426, 428, 421, 427, 429, 410, 409, 429, 407, 430, 432, 410, 431, 433, 411, 432, 434, 414, 412, 433, 413, 434, 422, 414, 435, 436, 429, 450, 437, 430, 451, 438, 431, 437, 439, 432, 438, 440, 435, 433, 439, 434, 440, 442, 435, 441, 443, 423, 422, 442, 424, 436, 443, 423, 444, 446, 424, 445, 447, 427, 425, 446, 426, 447, 449, 429, 427, 448, 443, 465, 452, 444, 466, 453, 445, 452, 454, 448, 446, 453, 447, 454, 456, 448, 455, 457, 450, 457, 459, 438, 451, 458, 437, 459, 461, 440, 438, 460, 441, 439, 461, 442, 440, 462, 441, 463, 465, 444, 442, 464, 458, 480, 467, 459, 481, 468, 460, 467, 469, 463, 461, 468, 462, 469, 471, 463, 470, 472, 466, 464, 471, 465, 472, 474, 466, 473, 475, 452, 474, 476, 455, 453, 475, 456, 454, 476, 455, 477, 479, 457, 479, 481, 475, 473, 3, 474, 2, 7, 477, 475, 5, 476, 7, 11, 479, 477, 9, 479, 13, 19, 467, 480, 14, 481, 19, 22, 469, 467, 21, 468, 22, 27, 471, 469, 24, 470, 27, 0, 473, 471, 29, 472, 0, 2
2882
+ ];
2883
+ this.normals = [-0.7101, -0.704, 0, -0.549, -0.8286, 0.1092, -0.5598, -0.8286, 0, -0.8333, 0.5528, 0, -0.907, 0.3805, 0.1804, -0.9247, 0.3805, 0, -0.3804, -0.9217, 0.0757, -0.3879, -0.9217, 0, -0.9622, 0.1939, 0.1914, -0.981, 0.1939, 0, -0.1971, -0.9796, 0.0392, -0.201, -0.9796, 0, -0.9808, 0, 0.1951, -1, 0, 0, -0.201, 0.9796, 0, 0, 1, 0, -0.1971, 0.9796, 0.0392, 0, -1, 0, -0.9622, -0.1939, 0.1914, -0.981, -0.1939, 0, -0.3804, 0.9217, 0.0757, -0.3879, 0.9217, 0, -0.9247, -0.3805, 0, -0.907, -0.3805, 0.1804, -0.5598, 0.8286, 0, -0.549, 0.8286, 0.1092, -0.8173, -0.5528, 0.1626, -0.8333, -0.5528, 0, -0.6965, 0.704, 0.1385, -0.7101, 0.704, 0, -0.6965, -0.704, 0.1385, -0.8173, 0.5528, 0.1626, -0.7699, -0.5528, 0.3189, -0.6561, -0.704, 0.2717, -0.7699, 0.5528, 0.3189, -0.5171, -0.8286, 0.2142, -0.8544, 0.3805, 0.3539, -0.3583, -0.9217, 0.1484, -0.9063, 0.1939, 0.3754, -0.1856, -0.9796, 0.0769, -0.9239, 0, 0.3827, -0.1856, 0.9796, 0.0769, -0.9063, -0.1939, 0.3754, -0.3583, 0.9217, 0.1484, -0.8544, -0.3805, 0.3539, -0.5171, 0.8286, 0.2142, -0.6561, 0.704, 0.2717, -0.1671, 0.9796, 0.1116, -0.1671, -0.9796, 0.1116, -0.8157, -0.1939, 0.545, -0.3225, 0.9217, 0.2155, -0.7689, -0.3805, 0.5137, -0.4654, 0.8286, 0.311, -0.6929, -0.5528, 0.463, -0.5904, 0.704, 0.3945, -0.5904, -0.704, 0.3945, -0.6929, 0.5528, 0.463, -0.4654, -0.8286, 0.311, -0.7689, 0.3805, 0.5137, -0.3225, -0.9217, 0.2155, -0.8157, 0.1939, 0.545, -0.8314, 0, 0.5556, -0.5893, 0.5528, 0.5893, -0.3958, -0.8286, 0.3958, -0.6539, 0.3805, 0.6539, -0.2743, -0.9217, 0.2743, -0.6937, 0.1939, 0.6937, -0.1421, -0.9796, 0.1421, -0.7071, 0, 0.7071, -0.1421, 0.9796, 0.1421, -0.6937, -0.1939, 0.6937, -0.2743, 0.9217, 0.2743, -0.6539, -0.3805, 0.6539, -0.3958, 0.8286, 0.3958, -0.5893, -0.5528, 0.5893, -0.5021, 0.704, 0.5021, -0.5021, -0.704, 0.5021, -0.545, -0.1939, 0.8157, -0.2155, 0.9217, 0.3225, -0.5137, -0.3805, 0.7689, -0.311, 0.8286, 0.4654, -0.463, -0.5528, 0.6929, -0.3945, 0.704, 0.5904, -0.3945, -0.704, 0.5904, -0.463, 0.5528, 0.6929, -0.311, -0.8286, 0.4654, -0.5137, 0.3805, 0.7689, -0.2155, -0.9217, 0.3225, -0.545, 0.1939, 0.8157, -0.1116, -0.9796, 0.1671, -0.5556, 0, 0.8314, -0.1116, 0.9796, 0.1671, -0.2142, -0.8286, 0.5171, -0.3539, 0.3805, 0.8544, -0.1484, -0.9217, 0.3583, -0.3754, 0.1939, 0.9063, -0.0769, -0.9796, 0.1856, -0.3827, 0, 0.9239, -0.0769, 0.9796, 0.1856, -0.3754, -0.1939, 0.9063, -0.1484, 0.9217, 0.3583, -0.3539, -0.3805, 0.8544, -0.2142, 0.8286, 0.5171, -0.3189, -0.5528, 0.7699, -0.2717, 0.704, 0.6561, -0.2717, -0.704, 0.6561, -0.3189, 0.5528, 0.7699, -0.1804, -0.3805, 0.907, -0.1092, 0.8286, 0.549, -0.1626, -0.5528, 0.8173, -0.1385, 0.704, 0.6965, -0.1385, -0.704, 0.6965, -0.1626, 0.5528, 0.8173, -0.1092, -0.8286, 0.549, -0.1804, 0.3805, 0.907, -0.0757, -0.9217, 0.3804, -0.1914, 0.1939, 0.9622, -0.0392, -0.9796, 0.1971, -0.1951, 0, 0.9808, -0.0392, 0.9796, 0.1971, -0.1914, -0.1939, 0.9622, -0.0757, 0.9217, 0.3804, 0, -0.8286, 0.5598, 0, -0.9217, 0.3879, 0, 0.3805, 0.9247, 0, 0.1939, 0.981, 0, -0.9796, 0.201, 0, 0, 1, 0, 0.9796, 0.201, 0, -0.1939, 0.981, 0, 0.9217, 0.3879, 0, -0.3805, 0.9247, 0, 0.8286, 0.5598, 0, -0.5528, 0.8333, 0, 0.704, 0.7101, 0, -0.704, 0.7101, 0, 0.5528, 0.8333, 0.1092, 0.8286, 0.549, 0.1804, -0.3805, 0.907, 0.1626, -0.5528, 0.8173, 0.1385, 0.704, 0.6965, 0.1385, -0.704, 0.6965, 0.1626, 0.5528, 0.8173, 0.1092, -0.8286, 0.549, 0.1804, 0.3805, 0.907, 0.0757, -0.9217, 0.3804, 0.1914, 0.1939, 0.9622, 0.0392, -0.9796, 0.1971, 0.1951, 0, 0.9808, 0.0392, 0.9796, 0.1971, 0.1914, -0.1939, 0.9622, 0.0757, 0.9217, 0.3804, 0.3539, 0.3805, 0.8544, 0.3754, 0.1939, 0.9063, 0.0769, -0.9796, 0.1856, 0.3827, 0, 0.9239, 0.0769, 0.9796, 0.1856, 0.3754, -0.1939, 0.9063, 0.1484, 0.9217, 0.3583, 0.3539, -0.3805, 0.8544, 0.2142, 0.8286, 0.5171, 0.3189, -0.5528, 0.7699, 0.2717, 0.704, 0.6561, 0.2717, -0.704, 0.6561, 0.3189, 0.5528, 0.7699, 0.2142, -0.8286, 0.5171, 0.1484, -0.9217, 0.3583, 0.5137, -0.3805, 0.7689, 0.463, -0.5528, 0.6929, 0.3945, 0.704, 0.5904, 0.3945, -0.704, 0.5904, 0.463, 0.5528, 0.6929, 0.311, -0.8286, 0.4654, 0.5137, 0.3805, 0.7689, 0.2155, -0.9217, 0.3225, 0.545, 0.1939, 0.8157, 0.1116, -0.9796, 0.1671, 0.5556, 0, 0.8314, 0.1116, 0.9796, 0.1671, 0.545, -0.1939, 0.8157, 0.2155, 0.9217, 0.3225, 0.311, 0.8286, 0.4654, 0.2743, -0.9217, 0.2743, 0.1421, -0.9796, 0.1421, 0.7071, 0, 0.7071, 0.1421, 0.9796, 0.1421, 0.6937, -0.1939, 0.6937, 0.2743, 0.9217, 0.2743, 0.6539, -0.3805, 0.6539, 0.3958, 0.8286, 0.3958, 0.5893, -0.5528, 0.5893, 0.5021, 0.704, 0.5021, 0.5021, -0.704, 0.5021, 0.5893, 0.5528, 0.5893, 0.3958, -0.8286, 0.3958, 0.6539, 0.3805, 0.6539, 0.6937, 0.1939, 0.6937, 0.4654, 0.8286, 0.311, 0.5904, 0.704, 0.3945, 0.5904, -0.704, 0.3945, 0.6929, 0.5528, 0.463, 0.4654, -0.8286, 0.311, 0.7689, 0.3805, 0.5137, 0.3225, -0.9217, 0.2155, 0.8157, 0.1939, 0.545, 0.1671, -0.9796, 0.1116, 0.8314, 0, 0.5556, 0.1671, 0.9796, 0.1116, 0.8157, -0.1939, 0.545, 0.3225, 0.9217, 0.2155, 0.7689, -0.3805, 0.5137, 0.6929, -0.5528, 0.463, 0.9239, 0, 0.3827, 0.1856, 0.9796, 0.0769, 0.1856, -0.9796, 0.0769, 0.9063, -0.1939, 0.3754, 0.3583, 0.9217, 0.1484, 0.8544, -0.3805, 0.3539, 0.5171, 0.8286, 0.2142, 0.7699, -0.5528, 0.3189, 0.6561, 0.704, 0.2717, 0.6561, -0.704, 0.2717, 0.7699, 0.5528, 0.3189, 0.5171, -0.8286, 0.2142, 0.8544, 0.3805, 0.3539, 0.3583, -0.9217, 0.1484, 0.9063, 0.1939, 0.3754, 0.6965, -0.704, 0.1385, 0.8173, 0.5528, 0.1626, 0.549, -0.8286, 0.1092, 0.907, 0.3805, 0.1804, 0.3804, -0.9217, 0.0757, 0.9622, 0.1939, 0.1914, 0.1971, -0.9796, 0.0392, 0.9808, 0, 0.1951, 0.1971, 0.9796, 0.0392, 0.9622, -0.1939, 0.1914, 0.3804, 0.9217, 0.0757, 0.907, -0.3805, 0.1804, 0.549, 0.8286, 0.1092, 0.8173, -0.5528, 0.1626, 0.6965, 0.704, 0.1385, 0.201, -0.9796, 0, 0.981, -0.1939, 0, 0.3879, 0.9217, 0, 0.9247, -0.3805, 0, 0.5598, 0.8286, 0, 0.8333, -0.5528, 0, 0.7101, 0.704, 0, 0.7101, -0.704, 0, 0.8333, 0.5528, 0, 0.5598, -0.8286, 0, 0.9247, 0.3805, 0, 0.3879, -0.9217, 0, 0.981, 0.1939, 0, 1, 0, 0, 0.201, 0.9796, 0, 0.549, -0.8286, -0.1092, 0.907, 0.3805, -0.1804, 0.3804, -0.9217, -0.0757, 0.9622, 0.1939, -0.1914, 0.1971, -0.9796, -0.0392, 0.9808, 0, -0.1951, 0.1971, 0.9796, -0.0392, 0.9622, -0.1939, -0.1914, 0.3804, 0.9217, -0.0757, 0.907, -0.3805, -0.1804, 0.549, 0.8286, -0.1092, 0.8173, -0.5528, -0.1626, 0.6965, 0.704, -0.1385, 0.6965, -0.704, -0.1385, 0.8173, 0.5528, -0.1626, 0.3583, 0.9217, -0.1484, 0.8544, -0.3805, -0.3539, 0.5171, 0.8286, -0.2142, 0.7699, -0.5528, -0.3189, 0.6561, 0.704, -0.2717, 0.6561, -0.704, -0.2717, 0.7699, 0.5528, -0.3189, 0.5171, -0.8286, -0.2142, 0.8544, 0.3805, -0.3539, 0.3583, -0.9217, -0.1484, 0.9063, 0.1939, -0.3754, 0.1856, -0.9796, -0.0769, 0.9239, 0, -0.3827, 0.1856, 0.9796, -0.0769, 0.9063, -0.1939, -0.3754, 0.7689, 0.3805, -0.5137, 0.3225, -0.9217, -0.2155, 0.8157, 0.1939, -0.545, 0.1671, -0.9796, -0.1116, 0.8314, 0, -0.5556, 0.1671, 0.9796, -0.1116, 0.8157, -0.1939, -0.545, 0.3225, 0.9217, -0.2155, 0.7689, -0.3805, -0.5137, 0.4654, 0.8286, -0.311, 0.6929, -0.5528, -0.463, 0.5904, 0.704, -0.3945, 0.5904, -0.704, -0.3945, 0.6929, 0.5528, -0.463, 0.4654, -0.8286, -0.311, 0.6539, -0.3805, -0.6539, 0.3958, 0.8286, -0.3958, 0.5893, -0.5528, -0.5893, 0.5021, 0.704, -0.5021, 0.5021, -0.704, -0.5021, 0.5893, 0.5528, -0.5893, 0.3958, -0.8286, -0.3958, 0.6539, 0.3805, -0.6539, 0.2743, -0.9217, -0.2743, 0.6937, 0.1939, -0.6937, 0.1421, -0.9796, -0.1421, 0.7071, 0, -0.7071, 0.1421, 0.9796, -0.1421, 0.6937, -0.1939, -0.6937, 0.2743, 0.9217, -0.2743, 0.311, -0.8286, -0.4654, 0.2155, -0.9217, -0.3225, 0.5137, 0.3805, -0.7689, 0.545, 0.1939, -0.8157, 0.1116, -0.9796, -0.1671, 0.5556, 0, -0.8314, 0.1116, 0.9796, -0.1671, 0.545, -0.1939, -0.8157, 0.2155, 0.9217, -0.3225, 0.5137, -0.3805, -0.7689, 0.311, 0.8286, -0.4654, 0.463, -0.5528, -0.6929, 0.3945, 0.704, -0.5904, 0.3945, -0.704, -0.5904, 0.463, 0.5528, -0.6929, 0.2142, 0.8286, -0.5171, 0.3539, -0.3805, -0.8544, 0.3189, -0.5528, -0.7699, 0.2717, 0.704, -0.6561, 0.2717, -0.704, -0.6561, 0.3189, 0.5528, -0.7699, 0.2142, -0.8286, -0.5171, 0.3539, 0.3805, -0.8544, 0.1484, -0.9217, -0.3583, 0.3754, 0.1939, -0.9063, 0.0769, -0.9796, -0.1856, 0.3827, 0, -0.9239, 0.0769, 0.9796, -0.1856, 0.3754, -0.1939, -0.9063, 0.1484, 0.9217, -0.3583, 0.1804, 0.3805, -0.907, 0.1914, 0.1939, -0.9622, 0.0392, -0.9796, -0.1971, 0.1951, 0, -0.9808, 0.0392, 0.9796, -0.1971, 0.1914, -0.1939, -0.9622, 0.0757, 0.9217, -0.3804, 0.1804, -0.3805, -0.907, 0.1092, 0.8286, -0.549, 0.1626, -0.5528, -0.8173, 0.1385, 0.704, -0.6965, 0.1385, -0.704, -0.6965, 0.1626, 0.5528, -0.8173, 0.1092, -0.8286, -0.549, 0.0757, -0.9217, -0.3804, 0, -0.3805, -0.9247, 0, -0.5528, -0.8333, 0, 0.8286, -0.5598, 0, 0.704, -0.7101, 0, -0.704, -0.7101, 0, 0.5528, -0.8333, 0, -0.8286, -0.5598, 0, 0.3805, -0.9247, 0, -0.9217, -0.3879, 0, 0.1939, -0.981, 0, -0.9796, -0.201, 0, 0, -1, 0, 0.9796, -0.201, 0, -0.1939, -0.981, 0, 0.9217, -0.3879, -0.0392, -0.9796, -0.1971, -0.1951, 0, -0.9808, -0.0392, 0.9796, -0.1971, -0.1914, -0.1939, -0.9622, -0.0757, 0.9217, -0.3804, -0.1804, -0.3805, -0.907, -0.1092, 0.8286, -0.549, -0.1626, -0.5528, -0.8173, -0.1385, 0.704, -0.6965, -0.1385, -0.704, -0.6965, -0.1626, 0.5528, -0.8173, -0.1092, -0.8286, -0.549, -0.1804, 0.3805, -0.907, -0.0757, -0.9217, -0.3804, -0.1914, 0.1939, -0.9622, -0.2717, -0.704, -0.6561, -0.3189, 0.5528, -0.7699, -0.2142, -0.8286, -0.5171, -0.3539, 0.3805, -0.8544, -0.1484, -0.9217, -0.3583, -0.3754, 0.1939, -0.9063, -0.0769, -0.9796, -0.1856, -0.3827, 0, -0.9239, -0.0769, 0.9796, -0.1856, -0.3754, -0.1939, -0.9063, -0.1484, 0.9217, -0.3583, -0.3539, -0.3805, -0.8544, -0.2142, 0.8286, -0.5171, -0.3189, -0.5528, -0.7699, -0.2717, 0.704, -0.6561, -0.1116, 0.9796, -0.1671, -0.1116, -0.9796, -0.1671, -0.5556, 0, -0.8314, -0.545, -0.1939, -0.8157, -0.2155, 0.9217, -0.3225, -0.5137, -0.3805, -0.7689, -0.311, 0.8286, -0.4654, -0.463, -0.5528, -0.6929, -0.3945, 0.704, -0.5904, -0.3945, -0.704, -0.5904, -0.463, 0.5528, -0.6929, -0.311, -0.8286, -0.4654, -0.5137, 0.3805, -0.7689, -0.2155, -0.9217, -0.3225, -0.545, 0.1939, -0.8157, -0.5021, 0.704, -0.5021, -0.5893, 0.5528, -0.5893, -0.3958, -0.8286, -0.3958, -0.6539, 0.3805, -0.6539, -0.2743, -0.9217, -0.2743, -0.6937, 0.1939, -0.6937, -0.1421, -0.9796, -0.1421, -0.7071, 0, -0.7071, -0.1421, 0.9796, -0.1421, -0.6937, -0.1939, -0.6937, -0.2743, 0.9217, -0.2743, -0.6539, -0.3805, -0.6539, -0.3958, 0.8286, -0.3958, -0.5893, -0.5528, -0.5893, -0.5021, -0.704, -0.5021, -0.8157, -0.1939, -0.545, -0.3225, 0.9217, -0.2155, -0.7689, -0.3805, -0.5137, -0.4654, 0.8286, -0.311, -0.6929, -0.5528, -0.463, -0.5904, 0.704, -0.3945, -0.5904, -0.704, -0.3945, -0.6929, 0.5528, -0.463, -0.4654, -0.8286, -0.311, -0.7689, 0.3805, -0.5137, -0.3225, -0.9217, -0.2155, -0.8157, 0.1939, -0.545, -0.1671, -0.9796, -0.1116, -0.8314, 0, -0.5556, -0.1671, 0.9796, -0.1116, -0.5171, -0.8286, -0.2142, -0.8544, 0.3805, -0.3539, -0.3583, -0.9217, -0.1484, -0.9063, 0.1939, -0.3754, -0.1856, -0.9796, -0.0769, -0.9239, 0, -0.3827, -0.1856, 0.9796, -0.0769, -0.9063, -0.1939, -0.3754, -0.3583, 0.9217, -0.1484, -0.8544, -0.3805, -0.3539, -0.5171, 0.8286, -0.2142, -0.7699, -0.5528, -0.3189, -0.6561, 0.704, -0.2717, -0.6561, -0.704, -0.2717, -0.7699, 0.5528, -0.3189, -0.3804, 0.9217, -0.0757, -0.907, -0.3805, -0.1804, -0.549, 0.8286, -0.1092, -0.8173, -0.5528, -0.1626, -0.6965, 0.704, -0.1385, -0.6965, -0.704, -0.1385, -0.8173, 0.5528, -0.1626, -0.549, -0.8286, -0.1092, -0.907, 0.3805, -0.1804, -0.3804, -0.9217, -0.0757, -0.9622, 0.1939, -0.1914, -0.1971, -0.9796, -0.0392, -0.9808, 0, -0.1951, -0.1971, 0.9796, -0.0392, -0.9622, -0.1939, -0.1914
2884
+ ];
2885
+ this.subMeshes = [
2886
+ new SubMesh(0, 2880)
2887
+ ];
2888
+ if (scale !== undefined) {
2889
+ const sx = scale.x;
2890
+ const sy = scale.y;
2891
+ const sz = scale.z;
2892
+ if (sx !== 1 || sy !== 1 || sz !== 1) {
2893
+ for (let i = 0; i < this.vertices.length; i += 3) {
2894
+ this.vertices[i] *= sx;
2895
+ this.vertices[i + 1] *= sy;
2896
+ this.vertices[i + 2] *= sz;
2897
+ }
2898
+ if (sx !== sy || sy !== sz)
2899
+ for (let i = 0; i < this.normals.length; i += 3) {
2900
+ const nx = this.normals[i] / sx;
2901
+ const ny = this.normals[i + 1] / sy;
2902
+ const nz = this.normals[i + 2] / sz;
2903
+ const len = Math.hypot(nx, ny, nz) || 1.0;
2904
+ this.normals[i] = nx / len;
2905
+ this.normals[i + 1] = ny / len;
2906
+ this.normals[i + 2] = nz / len;
2907
+ }
2908
+ }
2909
+ }
2910
+ }
2911
+ }
2912
+
2913
+ class Hemisphere extends BasicMesh {
2914
+ constructor() {
2915
+ super();
2916
+ this.vertices = [-0.5556, -0.8315, 0, -0.3753, -0.9239, 0.0747, -0.3827, -0.9239, 0, -0.9239, 0.3827, 0, -0.9619, 0.1951, 0.1913, -0.9808, 0.1951, 0, -0.1913, -0.9808, 0.0381, -0.1951, -0.9808, 0, -0.9808, 0, 0.1951, -1, 0, 0, -0.1951, 0.9808, 0, 0, 1, 0, -0.1913, 0.9808, 0.0381, 0, -1, 0, -0.9619, -0.1951, 0.1913, -0.9808, -0.1951, 0, -0.3753, 0.9239, 0.0747, -0.3827, 0.9239, 0, -0.9239, -0.3827, 0, -0.9061, -0.3827, 0.1802, -0.5556, 0.8315, 0, -0.5449, 0.8315, 0.1084, -0.8155, -0.5556, 0.1622, -0.8315, -0.5556, 0, -0.6935, 0.7071, 0.1379, -0.7071, 0.7071, 0, -0.7071, -0.7071, 0, -0.6935, -0.7071, 0.1379, -0.8155, 0.5556, 0.1622, -0.8315, 0.5556, 0, -0.5449, -0.8315, 0.1084, -0.9061, 0.3827, 0.1802, -0.5133, -0.8315, 0.2126, -0.8536, 0.3827, 0.3536, -0.3536, -0.9239, 0.1464, -0.9061, 0.1951, 0.3753, -0.1802, -0.9808, 0.0747, -0.9239, 0, 0.3827, -0.1802, 0.9808, 0.0747, -0.9061, -0.1951, 0.3753, -0.3536, 0.9239, 0.1464, -0.8536, -0.3827, 0.3536, -0.5133, 0.8315, 0.2126, -0.7682, -0.5556, 0.3182, -0.6533, 0.7071, 0.2706, -0.6533, -0.7071, 0.2706, -0.7682, 0.5556, 0.3182, -0.3182, 0.9239, 0.2126, -0.7682, -0.3827, 0.5133, -0.4619, 0.8315, 0.3087, -0.6913, -0.5556, 0.4619, -0.5879, 0.7071, 0.3928, -0.5879, -0.7071, 0.3928, -0.6913, 0.5556, 0.4619, -0.4619, -0.8315, 0.3087, -0.7682, 0.3827, 0.5133, -0.3182, -0.9239, 0.2126, -0.8155, 0.1951, 0.5449, -0.1622, -0.9808, 0.1084, -0.8315, 0, 0.5556, -0.1622, 0.9808, 0.1084, -0.8155, -0.1951, 0.5449, -0.6533, 0.3827, 0.6533, -0.2706, -0.9239, 0.2706, -0.6935, 0.1951, 0.6935, -0.1379, -0.9808, 0.1379, -0.7071, 0, 0.7071, -0.1379, 0.9808, 0.1379, -0.6935, -0.1951, 0.6935, -0.2706, 0.9239, 0.2706, -0.6533, -0.3827, 0.6533, -0.3928, 0.8315, 0.3928, -0.5879, -0.5556, 0.5879, -0.5, 0.7071, 0.5, -0.5, -0.7071, 0.5, -0.5879, 0.5556, 0.5879, -0.3928, -0.8315, 0.3928, -0.5133, -0.3827, 0.7682, -0.3087, 0.8315, 0.4619, -0.4619, -0.5556, 0.6913, -0.3928, 0.7071, 0.5879, -0.3928, -0.7071, 0.5879, -0.4619, 0.5556, 0.6913, -0.3087, -0.8315, 0.4619, -0.5133, 0.3827, 0.7682, -0.2126, -0.9239, 0.3182, -0.5449, 0.1951, 0.8155, -0.1084, -0.9808, 0.1622, -0.5556, 0, 0.8315, -0.1084, 0.9808, 0.1622, -0.5449, -0.1951, 0.8155, -0.2126, 0.9239, 0.3182, -0.2126, -0.8315, 0.5133, -0.1464, -0.9239, 0.3536, -0.3536, 0.3827, 0.8536, -0.3753, 0.1951, 0.9061, -0.0747, -0.9808, 0.1802, -0.3827, 0, 0.9239, -0.0747, 0.9808, 0.1802, -0.3753, -0.1951, 0.9061, -0.1464, 0.9239, 0.3536, -0.3536, -0.3827, 0.8536, -0.2126, 0.8315, 0.5133, -0.3182, -0.5556, 0.7682, -0.2706, 0.7071, 0.6533, -0.2706, -0.7071, 0.6533, -0.3182, 0.5556, 0.7682, -0.1802, -0.3827, 0.9061, -0.1622, -0.5556, 0.8155, -0.1084, 0.8315, 0.5449, -0.1379, 0.7071, 0.6935, -0.1379, -0.7071, 0.6935, -0.1622, 0.5556, 0.8155, -0.1084, -0.8315, 0.5449, -0.1802, 0.3827, 0.9061, -0.0747, -0.9239, 0.3753, -0.1913, 0.1951, 0.9619, -0.0381, -0.9808, 0.1913, -0.1951, 0, 0.9808, -0.0381, 0.9808, 0.1913, -0.1913, -0.1951, 0.9619, -0.0747, 0.9239, 0.3753, 0, -0.9239, 0.3827, 0, -0.9808, 0.1951, 0, 0, 1, 0, 0.9808, 0.1951, 0, -0.1951, 0.9808, 0, 0.9239, 0.3827, 0, -0.3827, 0.9239, 0, 0.8315, 0.5556, 0, -0.5556, 0.8315, 0, 0.7071, 0.7071, 0, -0.7071, 0.7071, 0, 0.5556, 0.8315, 0, -0.8315, 0.5556, 0, 0.3827, 0.9239, 0, 0.1951, 0.9808, 0.138, 0.7071, 0.6935, 0.138, -0.7071, 0.6935, 0.1622, 0.5556, 0.8155, 0.1084, -0.8315, 0.5449, 0.1802, 0.3827, 0.9061, 0.0747, -0.9239, 0.3753, 0.1913, 0.1951, 0.9619, 0.0381, -0.9808, 0.1913, 0.1951, 0, 0.9808, 0.0381, 0.9808, 0.1913, 0.1913, -0.1951, 0.9619, 0.0747, 0.9239, 0.3753, 0.1802, -0.3827, 0.9061, 0.1084, 0.8315, 0.5449, 0.1622, -0.5556, 0.8155, 0.3827, 0, 0.9239, 0.0747, 0.9808, 0.1802, 0.0747, -0.9808, 0.1802, 0.3753, -0.1951, 0.9061, 0.1464, 0.9239, 0.3536, 0.3536, -0.3827, 0.8536, 0.2126, 0.8315, 0.5133, 0.3182, -0.5556, 0.7682, 0.2706, 0.7071, 0.6533, 0.2706, -0.7071, 0.6533, 0.3182, 0.5556, 0.7682, 0.2126, -0.8315, 0.5133, 0.3536, 0.3827, 0.8536, 0.1464, -0.9239, 0.3536, 0.3753, 0.1951, 0.9061, 0.3928, -0.7071, 0.5879, 0.3928, 0.7071, 0.5879, 0.4619, 0.5556, 0.6913, 0.3087, -0.8315, 0.4619, 0.5133, 0.3827, 0.7682, 0.2126, -0.9239, 0.3182, 0.5449, 0.1951, 0.8155, 0.1084, -0.9808, 0.1622, 0.5556, 0, 0.8315, 0.1084, 0.9808, 0.1622, 0.5449, -0.1951, 0.8155, 0.2126, 0.9239, 0.3182, 0.5133, -0.3827, 0.7682, 0.3087, 0.8315, 0.4619, 0.4619, -0.5556, 0.6913, 0.138, 0.9808, 0.1379, 0.138, -0.9808, 0.1379, 0.6935, -0.1951, 0.6935, 0.2706, 0.9239, 0.2706, 0.6533, -0.3827, 0.6533, 0.3928, 0.8315, 0.3928, 0.5879, -0.5556, 0.5879, 0.5, 0.7071, 0.5, 0.5, -0.7071, 0.5, 0.5879, 0.5556, 0.5879, 0.3928, -0.8315, 0.3928, 0.6533, 0.3827, 0.6533, 0.2706, -0.9239, 0.2706, 0.6935, 0.1951, 0.6935, 0.7071, 0, 0.7071, 0.5879, 0.7071, 0.3928, 0.6913, 0.5556, 0.4619, 0.5879, -0.7071, 0.3928, 0.4619, -0.8315, 0.3087, 0.7682, 0.3827, 0.5133, 0.3182, -0.9239, 0.2126, 0.8155, 0.1951, 0.5449, 0.1622, -0.9808, 0.1084, 0.8315, 0, 0.5556, 0.1622, 0.9808, 0.1084, 0.8155, -0.1951, 0.5449, 0.3182, 0.9239, 0.2126, 0.7682, -0.3827, 0.5133, 0.4619, 0.8315, 0.3087, 0.6913, -0.5556, 0.4619, 0.9061, -0.1951, 0.3753, 0.3536, 0.9239, 0.1464, 0.8536, -0.3827, 0.3536, 0.5133, 0.8315, 0.2126, 0.7682, -0.5556, 0.3182, 0.6533, 0.7071, 0.2706, 0.6533, -0.7071, 0.2706, 0.7682, 0.5556, 0.3182, 0.5133, -0.8315, 0.2126, 0.8536, 0.3827, 0.3536, 0.3536, -0.9239, 0.1464, 0.9061, 0.1951, 0.3753, 0.1802, -0.9808, 0.0747, 0.9239, 0, 0.3827, 0.1802, 0.9808, 0.0747, 0.5449, -0.8315, 0.1084, 0.9061, 0.3827, 0.1802, 0.3753, -0.9239, 0.0747, 0.9619, 0.1951, 0.1913, 0.1913, -0.9808, 0.0381, 0.9808, 0, 0.1951, 0.1913, 0.9808, 0.0381, 0.9619, -0.1951, 0.1913, 0.3753, 0.9239, 0.0747, 0.9061, -0.3827, 0.1802, 0.5449, 0.8315, 0.1084, 0.8155, -0.5556, 0.1622, 0.6935, 0.7071, 0.1379, 0.6935, -0.7071, 0.1379, 0.8155, 0.5556, 0.1622, 0.9239, -0.3827, 0, 0.3827, 0.9239, 0, 0.5556, 0.8315, 0, 0.8315, -0.5556, 0, 0.7071, 0.7071, 0, 0.7071, -0.7071, 0, 0.8315, 0.5556, 0, 0.5556, -0.8315, 0, 0.9239, 0.3827, 0, 0.3827, -0.9239, 0, 0.9808, 0.1951, 0, 0.1951, -0.9808, 0, 1, 0, 0, 0.1951, 0.9808, 0, 0.9808, -0.1951, 0
2917
+ ];
2918
+ this.indices = [0, 1, 2, 3, 4, 5, 2, 6, 7, 5, 8, 9, 10, 11, 12, 13, 7, 6, 9, 14, 15, 10, 16, 17, 18, 14, 19, 20, 16, 21, 18, 22, 23, 20, 24, 25, 26, 22, 27, 25, 28, 29, 26, 30, 0, 29, 31, 3, 27, 32, 30, 28, 33, 31, 30, 34, 1, 31, 35, 4, 1, 36, 6, 4, 37, 8, 12, 11, 38, 13, 6, 36, 14, 37, 39, 12, 40, 16, 19, 39, 41, 16, 42, 21, 19, 43, 22, 21, 44, 24, 27, 43, 45, 24, 46, 28, 38, 47, 40, 39, 48, 41, 40, 49, 42, 43, 48, 50, 42, 51, 44, 45, 50, 52, 44, 53, 46, 45, 54, 32, 46, 55, 33, 32, 56, 34, 35, 55, 57, 34, 58, 36, 35, 59, 37, 38, 11, 60, 13, 36, 58, 37, 61, 39, 53, 62, 55, 54, 63, 56, 55, 64, 57, 56, 65, 58, 59, 64, 66, 60, 11, 67, 13, 58, 65, 59, 68, 61, 60, 69, 47, 61, 70, 48, 47, 71, 49, 50, 70, 72, 49, 73, 51, 50, 74, 52, 51, 75, 53, 52, 76, 54, 68, 77, 70, 69, 78, 71, 72, 77, 79, 71, 80, 73, 72, 81, 74, 75, 80, 82, 76, 81, 83, 75, 84, 62, 63, 83, 85, 62, 86, 64, 63, 87, 65, 66, 86, 88, 67, 11, 89, 13, 65, 87, 66, 90, 68, 67, 91, 69, 85, 92, 93, 86, 94, 95, 85, 96, 87, 86, 97, 88, 89, 11, 98, 13, 87, 96, 88, 99, 90, 89, 100, 91, 90, 101, 77, 91, 102, 78, 79, 101, 103, 78, 104, 80, 81, 103, 105, 82, 104, 106, 81, 92, 83, 82, 94, 84, 103, 107, 108, 104, 109, 110, 105, 108, 111, 104, 112, 106, 105, 113, 92, 106, 114, 94, 92, 115, 93, 95, 114, 116, 96, 115, 117, 95, 118, 97, 98, 11, 119, 13, 96, 117, 97, 120, 99, 98, 121, 100, 99, 107, 101, 100, 109, 102, 117, 122, 123, 116, 124, 118, 119, 11, 125, 13, 117, 123, 120, 124, 126, 119, 127, 121, 120, 128, 107, 121, 129, 109, 108, 128, 130, 109, 131, 110, 108, 132, 111, 110, 133, 112, 113, 132, 134, 112, 135, 114, 115, 134, 122, 116, 135, 136, 129, 137, 131, 130, 138, 132, 133, 137, 139, 134, 138, 140, 133, 141, 135, 134, 142, 122, 136, 141, 143, 123, 142, 144, 136, 145, 124, 125, 11, 146, 13, 123, 144, 124, 147, 126, 127, 146, 148, 126, 149, 128, 127, 150, 129, 130, 149, 151, 143, 152, 145, 146, 11, 153, 13, 144, 154, 145, 155, 147, 146, 156, 148, 147, 157, 149, 150, 156, 158, 151, 157, 159, 150, 160, 137, 151, 161, 138, 137, 162, 139, 138, 163, 140, 139, 164, 141, 142, 163, 165, 143, 164, 166, 142, 154, 144, 159, 167, 161, 162, 168, 169, 163, 167, 170, 162, 171, 164, 163, 172, 165, 166, 171, 173, 165, 174, 154, 166, 175, 152, 153, 11, 176, 13, 154, 174, 155, 175, 177, 153, 178, 156, 155, 179, 157, 156, 180, 158, 159, 179, 181, 158, 168, 160, 176, 11, 182, 13, 174, 183, 175, 184, 177, 176, 185, 178, 177, 186, 179, 178, 187, 180, 179, 188, 181, 180, 189, 168, 181, 190, 167, 168, 191, 169, 170, 190, 192, 169, 193, 171, 170, 194, 172, 173, 193, 195, 174, 194, 183, 173, 196, 175, 191, 197, 198, 192, 199, 200, 191, 201, 193, 192, 202, 194, 195, 201, 203, 183, 202, 204, 195, 205, 196, 182, 11, 206, 13, 183, 204, 184, 205, 207, 182, 208, 185, 184, 209, 186, 185, 210, 187, 188, 209, 211, 189, 210, 197, 188, 199, 190, 205, 212, 207, 206, 213, 208, 207, 214, 209, 210, 213, 215, 211, 214, 216, 210, 217, 197, 211, 218, 199, 197, 219, 198, 200, 218, 220, 198, 221, 201, 200, 222, 202, 203, 221, 223, 202, 224, 204, 203, 225, 205, 206, 11, 226, 13, 204, 224, 218, 227, 220, 219, 228, 221, 220, 229, 222, 223, 228, 230, 224, 229, 231, 223, 232, 225, 226, 11, 233, 13, 224, 231, 225, 234, 212, 226, 235, 213, 212, 236, 214, 213, 237, 215, 216, 236, 238, 215, 239, 217, 216, 240, 218, 217, 241, 219, 234, 242, 236, 237, 243, 244, 238, 242, 245, 239, 244, 246, 238, 247, 240, 239, 248, 241, 227, 247, 249, 241, 250, 228, 227, 251, 229, 230, 250, 252, 231, 251, 253, 230, 254, 232, 233, 11, 255, 13, 231, 253, 232, 256, 234, 233, 243, 235, 0, 30, 1, 3, 31, 4, 2, 1, 6, 5, 4, 8, 9, 8, 14, 10, 12, 16, 18, 15, 14, 20, 17, 16, 18, 19, 22, 20, 21, 24, 26, 23, 22, 25, 24, 28, 26, 27, 30, 29, 28, 31, 27, 45, 32, 28, 46, 33, 30, 32, 34, 31, 33, 35, 1, 34, 36, 4, 35, 37, 14, 8, 37, 12, 38, 40, 19, 14, 39, 16, 40, 42, 19, 41, 43, 21, 42, 44, 27, 22, 43, 24, 44, 46, 38, 60, 47, 39, 61, 48, 40, 47, 49, 43, 41, 48, 42, 49, 51, 45, 43, 50, 44, 51, 53, 45, 52, 54, 46, 53, 55, 32, 54, 56, 35, 33, 55, 34, 56, 58, 35, 57, 59, 37, 59, 61, 53, 75, 62, 54, 76, 63, 55, 62, 64, 56, 63, 65, 59, 57, 64, 59, 66, 68, 60, 67, 69, 61, 68, 70, 47, 69, 71, 50, 48, 70, 49, 71, 73, 50, 72, 74, 51, 73, 75, 52, 74, 76, 68, 90, 77, 69, 91, 78, 72, 70, 77, 71, 78, 80, 72, 79, 81, 75, 73, 80, 76, 74, 81, 75, 82, 84, 63, 76, 83, 62, 84, 86, 63, 85, 87, 66, 64, 86, 66, 88, 90, 67, 89, 91, 85, 83, 92, 86, 84, 94, 85, 93, 96, 86, 95, 97, 88, 97, 99, 89, 98, 100, 90, 99, 101, 91, 100, 102, 79, 77, 101, 78, 102, 104, 81, 79, 103, 82, 80, 104, 81, 105, 92, 82, 106, 94, 103, 101, 107, 104, 102, 109, 105, 103, 108, 104, 110, 112, 105, 111, 113, 106, 112, 114, 92, 113, 115, 95, 94, 114, 96, 93, 115, 95, 116, 118, 97, 118, 120, 98, 119, 121, 99, 120, 107, 100, 121, 109, 117, 115, 122, 116, 136, 124, 120, 118, 124, 119, 125, 127, 120, 126, 128, 121, 127, 129, 108, 107, 128, 109, 129, 131, 108, 130, 132, 110, 131, 133, 113, 111, 132, 112, 133, 135, 115, 113, 134, 116, 114, 135, 129, 150, 137, 130, 151, 138, 133, 131, 137, 134, 132, 138, 133, 139, 141, 134, 140, 142, 136, 135, 141, 123, 122, 142, 136, 143, 145, 124, 145, 147, 127, 125, 146, 126, 147, 149, 127, 148, 150, 130, 128, 149, 143, 166, 152, 145, 152, 155, 146, 153, 156, 147, 155, 157, 150, 148, 156, 151, 149, 157, 150, 158, 160, 151, 159, 161, 137, 160, 162, 138, 161, 163, 139, 162, 164, 142, 140, 163, 143, 141, 164, 142, 165, 154, 159, 181, 167, 162, 160, 168, 163, 161, 167, 162, 169, 171, 163, 170, 172, 166, 164, 171, 165, 172, 174, 166, 173, 175, 155, 152, 175, 153, 176, 178, 155, 177, 179, 156, 178, 180, 159, 157, 179, 158, 180, 168, 175, 196, 184, 176, 182, 185, 177, 184, 186, 178, 185, 187, 179, 186, 188, 180, 187, 189, 181, 188, 190, 168, 189, 191, 170, 167, 190, 169, 191, 193, 170, 192, 194, 173, 171, 193, 174, 172, 194, 173, 195, 196, 191, 189, 197, 192, 190, 199, 191, 198, 201, 192, 200, 202, 195, 193, 201, 183, 194, 202, 195, 203, 205, 184, 196, 205, 182, 206, 208, 184, 207, 209, 185, 208, 210, 188, 186, 209, 189, 187, 210, 188, 211, 199, 205, 225, 212, 206, 226, 213, 207, 212, 214, 210, 208, 213, 211, 209, 214, 210, 215, 217, 211, 216, 218, 197, 217, 219, 200, 199, 218, 198, 219, 221, 200, 220, 222, 203, 201, 221, 202, 222, 224, 203, 223, 225, 218, 240, 227, 219, 241, 228, 220, 227, 229, 223, 221, 228, 224, 222, 229, 223, 230, 232, 225, 232, 234, 226, 233, 235, 212, 234, 236, 213, 235, 237, 216, 214, 236, 215, 237, 239, 216, 238, 240, 217, 239, 241, 234, 256, 242, 237, 235, 243, 238, 236, 242, 239, 237, 244, 238, 245, 247, 239, 246, 248, 227, 240, 247, 241, 248, 250, 227, 249, 251, 230, 228, 250, 231, 229, 251, 230, 252, 254, 232, 254, 256, 233, 255, 243
2919
+ ];
2920
+ this.normals = [-0.5589, -0.8274, 0.055, -0.3804, -0.9217, 0.0757, -0.3876, -0.921, 0.0381, -0.9209, 0.3789, 0.0907, -0.9622, 0.1939, 0.1914, -0.9764, 0.193, 0.0962, -0.1971, -0.9796, 0.0392, -0.2009, -0.9794, 0.0198, -0.9808, 0, 0.1951, -0.9952, 0, 0.098, -0.2009, 0.9794, 0.0198, 0, 0.998, 0.063, -0.1971, 0.9796, 0.0392, 0, -0.998, 0.063, -0.9622, -0.1939, 0.1914, -0.9764, -0.193, 0.0962, -0.3804, 0.9217, 0.0757, -0.3876, 0.921, 0.0381, -0.9209, -0.3789, 0.0907, -0.907, -0.3805, 0.1804, -0.5589, 0.8274, 0.055, -0.549, 0.8286, 0.1092, -0.8173, -0.5528, 0.1626, -0.8305, -0.5509, 0.0818, -0.6965, 0.704, 0.1385, -0.7084, 0.7023, 0.0698, -0.7084, -0.7023, 0.0698, -0.6965, -0.704, 0.1385, -0.8173, 0.5528, 0.1626, -0.8305, 0.5509, 0.0818, -0.549, -0.8286, 0.1092, -0.907, 0.3805, 0.1804, -0.5171, -0.8286, 0.2142, -0.8544, 0.3805, 0.3539, -0.3583, -0.9217, 0.1484, -0.9063, 0.1939, 0.3754, -0.1856, -0.9796, 0.0769, -0.9239, 0, 0.3827, -0.1856, 0.9796, 0.0769, -0.9063, -0.1939, 0.3754, -0.3583, 0.9217, 0.1484, -0.8544, -0.3805, 0.3539, -0.5171, 0.8286, 0.2142, -0.7699, -0.5528, 0.3189, -0.6561, 0.704, 0.2717, -0.6561, -0.704, 0.2717, -0.7699, 0.5528, 0.3189, -0.3225, 0.9217, 0.2155, -0.7689, -0.3805, 0.5137, -0.4654, 0.8286, 0.311, -0.6929, -0.5528, 0.463, -0.5904, 0.704, 0.3945, -0.5904, -0.704, 0.3945, -0.6929, 0.5528, 0.463, -0.4654, -0.8286, 0.311, -0.7689, 0.3805, 0.5137, -0.3225, -0.9217, 0.2155, -0.8157, 0.1939, 0.545, -0.1671, -0.9796, 0.1116, -0.8314, 0, 0.5556, -0.1671, 0.9796, 0.1116, -0.8157, -0.1939, 0.545, -0.6539, 0.3805, 0.6539, -0.2743, -0.9217, 0.2743, -0.6937, 0.1939, 0.6937, -0.1421, -0.9796, 0.1421, -0.7071, 0, 0.7071, -0.1421, 0.9796, 0.1421, -0.6937, -0.1939, 0.6937, -0.2743, 0.9217, 0.2743, -0.6539, -0.3805, 0.6539, -0.3958, 0.8286, 0.3958, -0.5893, -0.5528, 0.5893, -0.5021, 0.704, 0.5021, -0.5021, -0.704, 0.5021, -0.5893, 0.5528, 0.5893, -0.3958, -0.8286, 0.3958, -0.5137, -0.3805, 0.7689, -0.311, 0.8286, 0.4654, -0.463, -0.5528, 0.6929, -0.3945, 0.704, 0.5904, -0.3945, -0.704, 0.5904, -0.463, 0.5528, 0.6929, -0.311, -0.8286, 0.4654, -0.5137, 0.3805, 0.7689, -0.2155, -0.9217, 0.3225, -0.545, 0.1939, 0.8157, -0.1116, -0.9796, 0.1671, -0.5556, 0, 0.8314, -0.1116, 0.9796, 0.1671, -0.545, -0.1939, 0.8157, -0.2155, 0.9217, 0.3225, -0.2142, -0.8286, 0.5171, -0.1484, -0.9217, 0.3583, -0.3539, 0.3805, 0.8544, -0.3754, 0.1939, 0.9063, -0.0769, -0.9796, 0.1856, -0.3827, 0, 0.9239, -0.0769, 0.9796, 0.1856, -0.3754, -0.1939, 0.9063, -0.1484, 0.9217, 0.3583, -0.3539, -0.3805, 0.8544, -0.2142, 0.8286, 0.5171, -0.3189, -0.5528, 0.7699, -0.2717, 0.704, 0.6561, -0.2717, -0.704, 0.6561, -0.3189, 0.5528, 0.7699, -0.1804, -0.3805, 0.907, -0.1626, -0.5528, 0.8173, -0.1092, 0.8286, 0.549, -0.1385, 0.704, 0.6965, -0.1385, -0.704, 0.6965, -0.1626, 0.5528, 0.8173, -0.1092, -0.8286, 0.549, -0.1804, 0.3805, 0.907, -0.0757, -0.9217, 0.3804, -0.1914, 0.1939, 0.9622, -0.0392, -0.9796, 0.1971, -0.1951, 0, 0.9808, -0.0392, 0.9796, 0.1971, -0.1914, -0.1939, 0.9622, -0.0757, 0.9217, 0.3804, 0, -0.9217, 0.3879, 0, -0.9796, 0.201, 0, 0, 1, 0, 0.9796, 0.201, 0, -0.1939, 0.981, 0, 0.9217, 0.3879, 0, -0.3805, 0.9247, 0, 0.8286, 0.5598, 0, -0.5528, 0.8333, 0, 0.704, 0.7101, 0, -0.704, 0.7101, 0, 0.5528, 0.8333, 0, -0.8286, 0.5598, 0, 0.3805, 0.9247, 0, 0.1939, 0.981, 0.1385, 0.704, 0.6965, 0.1385, -0.704, 0.6965, 0.1626, 0.5528, 0.8173, 0.1092, -0.8286, 0.549, 0.1804, 0.3805, 0.907, 0.0757, -0.9217, 0.3804, 0.1914, 0.1939, 0.9622, 0.0392, -0.9796, 0.1971, 0.1951, 0, 0.9808, 0.0392, 0.9796, 0.1971, 0.1914, -0.1939, 0.9622, 0.0757, 0.9217, 0.3804, 0.1804, -0.3805, 0.907, 0.1092, 0.8286, 0.549, 0.1626, -0.5528, 0.8173, 0.3827, 0, 0.9239, 0.0769, 0.9796, 0.1856, 0.0769, -0.9796, 0.1856, 0.3754, -0.1939, 0.9063, 0.1484, 0.9217, 0.3583, 0.3539, -0.3805, 0.8544, 0.2142, 0.8286, 0.5171, 0.3189, -0.5528, 0.7699, 0.2717, 0.704, 0.6561, 0.2717, -0.704, 0.6561, 0.3189, 0.5528, 0.7699, 0.2142, -0.8286, 0.5171, 0.3539, 0.3805, 0.8544, 0.1484, -0.9217, 0.3583, 0.3754, 0.1939, 0.9063, 0.3945, -0.704, 0.5904, 0.3945, 0.704, 0.5904, 0.463, 0.5528, 0.6929, 0.311, -0.8286, 0.4654, 0.5137, 0.3805, 0.7689, 0.2155, -0.9217, 0.3225, 0.545, 0.1939, 0.8157, 0.1116, -0.9796, 0.1671, 0.5556, 0, 0.8314, 0.1116, 0.9796, 0.1671, 0.545, -0.1939, 0.8157, 0.2155, 0.9217, 0.3225, 0.5137, -0.3805, 0.7689, 0.311, 0.8286, 0.4654, 0.463, -0.5528, 0.6929, 0.1421, 0.9796, 0.1421, 0.1421, -0.9796, 0.1421, 0.6937, -0.1939, 0.6937, 0.2743, 0.9217, 0.2743, 0.6539, -0.3805, 0.6539, 0.3958, 0.8286, 0.3958, 0.5893, -0.5528, 0.5893, 0.5021, 0.704, 0.5021, 0.5021, -0.704, 0.5021, 0.5893, 0.5528, 0.5893, 0.3958, -0.8286, 0.3958, 0.6539, 0.3805, 0.6539, 0.2743, -0.9217, 0.2743, 0.6937, 0.1939, 0.6937, 0.7071, 0, 0.7071, 0.5904, 0.704, 0.3945, 0.6929, 0.5528, 0.463, 0.5904, -0.704, 0.3945, 0.4654, -0.8286, 0.311, 0.7689, 0.3805, 0.5137, 0.3225, -0.9217, 0.2155, 0.8157, 0.1939, 0.545, 0.1671, -0.9796, 0.1116, 0.8314, 0, 0.5556, 0.1671, 0.9796, 0.1116, 0.8157, -0.1939, 0.545, 0.3225, 0.9217, 0.2155, 0.7689, -0.3805, 0.5137, 0.4654, 0.8286, 0.311, 0.6929, -0.5528, 0.463, 0.9063, -0.1939, 0.3754, 0.3583, 0.9217, 0.1484, 0.8544, -0.3805, 0.3539, 0.5171, 0.8286, 0.2142, 0.7699, -0.5528, 0.3189, 0.6561, 0.704, 0.2717, 0.6561, -0.704, 0.2717, 0.7699, 0.5528, 0.3189, 0.5171, -0.8286, 0.2142, 0.8544, 0.3805, 0.3539, 0.3583, -0.9217, 0.1484, 0.9063, 0.1939, 0.3754, 0.1856, -0.9796, 0.0769, 0.9239, 0, 0.3827, 0.1856, 0.9796, 0.0769, 0.549, -0.8286, 0.1092, 0.907, 0.3805, 0.1804, 0.3804, -0.9217, 0.0757, 0.9622, 0.1939, 0.1914, 0.1971, -0.9796, 0.0392, 0.9808, 0, 0.1951, 0.1971, 0.9796, 0.0392, 0.9622, -0.1939, 0.1914, 0.3804, 0.9217, 0.0757, 0.907, -0.3805, 0.1804, 0.549, 0.8286, 0.1092, 0.8173, -0.5528, 0.1626, 0.6965, 0.704, 0.1385, 0.6965, -0.704, 0.1385, 0.8173, 0.5528, 0.1626, 0.9209, -0.3789, 0.0907, 0.3876, 0.921, 0.0381, 0.5589, 0.8274, 0.055, 0.8305, -0.5509, 0.0818, 0.7084, 0.7023, 0.0698, 0.7084, -0.7023, 0.0698, 0.8305, 0.5509, 0.0818, 0.5589, -0.8274, 0.055, 0.9209, 0.3789, 0.0907, 0.3876, -0.921, 0.0381, 0.9764, 0.193, 0.0962, 0.2009, -0.9794, 0.0198, 0.9952, 0, 0.098, 0.2009, 0.9794, 0.0198, 0.9764, -0.193, 0.0962
2921
+ ];
2922
+ this.subMeshes = [
2923
+ new SubMesh(0, 1440)
2924
+ ];
2925
+ }
2926
+ }
2927
+
2928
+ class Plane extends BasicMesh {
2929
+ constructor(size = new Vec2(1.0, 1.0), segments = new Vec2(1, 1), orientation = 'xz') {
2930
+ super();
2931
+ const w = size.x || 1.0;
2932
+ const h = size.y || 1.0;
2933
+ const gridX = Utils.clamp(Utils.floor(segments.x || 1, 0), 1, Infinity);
2934
+ const gridY = Utils.clamp(Utils.floor(segments.y || 1, 0), 1, Infinity);
2935
+ const orient = orientation || 'xz';
2936
+ const gridX1 = gridX + 1;
2937
+ const gridY1 = gridY + 1;
2938
+ const segmentWidth = w / gridX;
2939
+ const segmentHeight = h / gridY;
2940
+ const vertices = [];
2941
+ const normals = [];
2942
+ const uvs = [];
2943
+ const indices = [];
2944
+ const halfW = w * 0.5;
2945
+ const halfH = h * 0.5;
2946
+ for (let iy = 0; iy < gridY1; iy++) {
2947
+ const y = iy * segmentHeight - halfH;
2948
+ for (let ix = 0; ix < gridX1; ix++) {
2949
+ const x = ix * segmentWidth - halfW;
2950
+ if (orient === 'xz') {
2951
+ vertices.push(x, 0, y);
2952
+ normals.push(0, 1, 0);
2953
+ }
2954
+ else {
2955
+ vertices.push(x, -y, 0);
2956
+ normals.push(0, 0, 1);
2957
+ }
2958
+ uvs.push(ix / gridX, 1.0 - iy / gridY);
2959
+ }
2960
+ }
2961
+ for (let iy = 0; iy < gridY; iy++)
2962
+ for (let ix = 0; ix < gridX; ix++) {
2963
+ const a = ix + gridX1 * iy;
2964
+ const b = ix + gridX1 * (iy + 1);
2965
+ const c = (ix + 1) + gridX1 * (iy + 1);
2966
+ const d = (ix + 1) + gridX1 * iy;
2967
+ indices.push(a, b, d);
2968
+ indices.push(b, c, d);
2969
+ }
2970
+ this.vertices = vertices;
2971
+ this.normals = normals;
2972
+ this.uvs = uvs;
2973
+ this.indices = indices;
2974
+ this.itemSize = 3;
2975
+ this.primitive = 'TRIANGLES';
2976
+ this.subMeshes = [new SubMesh(0, indices.length)];
2977
+ }
2978
+ }
2979
+
2980
+ class Cylinder extends BasicMesh {
2981
+ constructor(radii = new Vec2(1.0, 1.0), height = 1.0, radialSegments = 16, heightSegments = 1, openEnded = false) {
2982
+ super();
2983
+ const rTop = radii.x;
2984
+ const rBottom = radii.y;
2985
+ const h = height;
2986
+ const radSeg = Utils.clamp(Utils.floor(radialSegments, 0), 3, Infinity);
2987
+ const hSeg = Utils.clamp(Utils.floor(heightSegments, 0), 1, Infinity);
2988
+ const open = openEnded;
2989
+ const vertices = [];
2990
+ const normals = [];
2991
+ const uvs = [];
2992
+ const indices = [];
2993
+ let index = 0;
2994
+ const indexGrid = [];
2995
+ const halfH = h * 0.5;
2996
+ const tanTheta = (rBottom - rTop) / h;
2997
+ for (let y = 0; y <= hSeg; y++) {
2998
+ const indexRow = [];
2999
+ const v = y / hSeg;
3000
+ const radius = v * (rBottom - rTop) + rTop;
3001
+ const py = -v * h + halfH;
3002
+ for (let x = 0; x <= radSeg; x++) {
3003
+ const u = x / radSeg;
3004
+ const theta = u * Trigo.twopi;
3005
+ const sinTheta = Trigo.sine(theta);
3006
+ const cosTheta = Trigo.cosine(theta);
3007
+ const px = radius * sinTheta;
3008
+ const pz = radius * cosTheta;
3009
+ vertices.push(px, py, pz);
3010
+ const nx = sinTheta;
3011
+ const ny = tanTheta;
3012
+ const nz = cosTheta;
3013
+ const nLen = Math.hypot(nx, ny, nz) || 1.0;
3014
+ normals.push(nx / nLen, ny / nLen, nz / nLen);
3015
+ uvs.push(u, 1.0 - v);
3016
+ indexRow.push(index++);
3017
+ }
3018
+ indexGrid.push(indexRow);
3019
+ }
3020
+ for (let x = 0; x < radSeg; x++)
3021
+ for (let y = 0; y < hSeg; y++) {
3022
+ const a = indexGrid[y][x];
3023
+ const b = indexGrid[y + 1][x];
3024
+ const c = indexGrid[y + 1][x + 1];
3025
+ const d = indexGrid[y][x + 1];
3026
+ indices.push(a, b, d);
3027
+ indices.push(b, c, d);
3028
+ }
3029
+ if (!open) {
3030
+ if (rTop > 0) {
3031
+ const centerIndexTop = index++;
3032
+ vertices.push(0, halfH, 0);
3033
+ normals.push(0, 1, 0);
3034
+ uvs.push(0.5, 0.5);
3035
+ const topCapStartIndex = index;
3036
+ for (let x = 0; x <= radSeg; x++) {
3037
+ const u = x / radSeg;
3038
+ const theta = u * Trigo.twopi;
3039
+ const px = rTop * Trigo.sine(theta);
3040
+ const pz = rTop * Trigo.cosine(theta);
3041
+ vertices.push(px, halfH, pz);
3042
+ normals.push(0, 1, 0);
3043
+ uvs.push(Trigo.sine(theta) * 0.5 + 0.5, Trigo.cosine(theta) * 0.5 + 0.5);
3044
+ index++;
3045
+ }
3046
+ for (let x = 0; x < radSeg; x++)
3047
+ indices.push(centerIndexTop, topCapStartIndex + x, topCapStartIndex + x + 1);
3048
+ }
3049
+ if (rBottom > 0) {
3050
+ const centerIndexBottom = index++;
3051
+ vertices.push(0, -halfH, 0);
3052
+ normals.push(0, -1, 0);
3053
+ uvs.push(0.5, 0.5);
3054
+ const bottomCapStartIndex = index;
3055
+ for (let x = 0; x <= radSeg; x++) {
3056
+ const u = x / radSeg;
3057
+ const theta = u * Trigo.twopi;
3058
+ const px = rBottom * Trigo.sine(theta);
3059
+ const pz = rBottom * Trigo.cosine(theta);
3060
+ vertices.push(px, -halfH, pz);
3061
+ normals.push(0, -1, 0);
3062
+ uvs.push(Trigo.sine(theta) * 0.5 + 0.5, Trigo.cosine(theta) * 0.5 + 0.5);
3063
+ index++;
3064
+ }
3065
+ for (let x = 0; x < radSeg; x++)
3066
+ indices.push(centerIndexBottom, bottomCapStartIndex + x + 1, bottomCapStartIndex + x);
3067
+ }
3068
+ }
3069
+ this.vertices = vertices;
3070
+ this.normals = normals;
3071
+ this.uvs = uvs;
3072
+ this.indices = indices;
3073
+ this.itemSize = 3;
3074
+ this.primitive = 'TRIANGLES';
3075
+ this.subMeshes = [new SubMesh(0, indices.length)];
3076
+ }
3077
+ }
3078
+
3079
+ class VWing extends BasicMesh {
3080
+ constructor() {
3081
+ super();
3082
+ this.vertices = [0, 0, -7.3975, 0, -0.2707, 0, 1.4678, 0, 0, 0, 0.5653, 1.8374, 0, 1.4694, 0, 0, 1.553, -1.0874, 0, 0.7405, -5.3869, 2.3189, 0.251, 0, 1.4678, 0, 0, 1.6633, 2.6, 1.6982, 6.65, -0.8596, 3.1485, 2.3189, 0.251, 0, 1.9333, -0.2938, -1.9828, 1.4678, 0, 0, 2.3189, 0.251, 0, 1.4678, 0, 0, 1.4678, 0, 0, 1.6148, -0.4246, -2.7743, 1.9333, -0.2938, -1.9828, 1.4678, 0, 0, 1.6148, -0.4246, -2.7743, 1.6148, -0.4246, -2.7743, 1.9333, -0.2938, -1.9828, -1.4678, 0, 0, -2.3189, 0.251, 0, -1.6633, 2.6, 1.6982, -1.4678, 0, 0, -6.65, -0.8596, 3.1485, -1.9333, -0.2938, -1.9828, -2.3189, 0.251, 0, -1.4678, 0, 0, -2.3189, 0.251, 0, -1.4678, 0, 0, -1.4678, 0, 0, -1.6148, -0.4246, -2.7743, -1.9333, -0.2938, -1.9828, -1.6148, -0.4246, -2.7743, -1.4678, 0, 0, -1.6148, -0.4246, -2.7743, -1.9333, -0.2938, -1.9828, 0, -0.2707, 0, 0, 0.5653, 1.8374, 1.4678, 0, 0, 0, -0.2707, 0, -1.4678, 0, 0
3083
+ ];
3084
+ this.indices = [0, 1, 2, 2, 3, 4, 5, 2, 4, 6, 0, 2, 5, 6, 2, 7, 8, 9, 10, 11, 12, 10, 12, 13, 14, 10, 15, 16, 12, 17, 7, 9, 18, 9, 19, 20, 21, 22, 9, 0, 23, 1, 23, 4, 3, 5, 4, 23, 6, 23, 0, 5, 23, 6, 24, 25, 26, 27, 28, 29, 27, 30, 28, 31, 32, 27, 33, 34, 28, 24, 35, 25, 25, 36, 37, 38, 25, 39, 40, 41, 42, 43, 44, 41
3085
+ ];
3086
+ this.normals = [0, -0.1063, -0.9943, 0, -0.9819, 0.1891, 0.9233, -0.2975, 0.2427, 0, 0.0714, 0.9974, 0, 0.9697, 0.2443, 0, 0.9961, -0.088, 0, 0.9478, -0.3187, 0.553, 0.7911, 0.2613, -0.7402, -0.526, 0.4187, -0.0528, 0.7418, 0.6686, 0.7771, -0.1425, 0.6131, 0.553, 0.7911, 0.2613, 0.8324, -0.4718, -0.2906, -0.7402, -0.526, 0.4187, 0.553, 0.7911, 0.2613, -0.7402, -0.526, 0.4187, -0.7402, -0.526, 0.4187, -0.1135, -0.2998, -0.9472, 0.8324, -0.4718, -0.2906, -0.7402, -0.526, 0.4187, -0.1135, -0.2998, -0.9472, -0.1135, -0.2998, -0.9472, 0.8324, -0.4718, -0.2906, -0.9233, -0.2975, 0.2427, -0.553, 0.7911, 0.2613, 0.0528, 0.7418, 0.6686, 0.7402, -0.526, 0.4187, -0.7771, -0.1425, 0.6131, -0.8324, -0.4718, -0.2906, -0.553, 0.7911, 0.2613, 0.7402, -0.526, 0.4187, -0.553, 0.7911, 0.2613, 0.7402, -0.526, 0.4187, 0.7402, -0.526, 0.4187, 0.1135, -0.2998, -0.9472, -0.8324, -0.4718, -0.2906, 0.1135, -0.2998, -0.9472, 0.7402, -0.526, 0.4187, 0.1135, -0.2998, -0.9472, -0.8324, -0.4718, -0.2906, 0, -0.9819, 0.1891, 0, 0.0714, 0.9974, 0.9233, -0.2975, 0.2427, 0, -0.9819, 0.1891, -0.9233, -0.2975, 0.2427
3087
+ ];
3088
+ this.subMeshes = [
3089
+ new SubMesh(0, 78),
3090
+ new SubMesh(78, 6)
3091
+ ];
3092
+ }
3093
+ }
3094
+
3095
+ class Cannon extends BasicMesh {
3096
+ constructor() {
3097
+ super();
3098
+ this.vertices = [0, -0.4, 0.1885, 0.7, 0, -0.9849, 0, -0.4, -0.9849, 0, 0.4, -0.9849, 0.7, 0, 0.4525, 0, 0.4, 0.9293, 0, 0, -0.8408, 0, 0.4, -0.9849, 0, 0, 1.9525, 0, 0.4, 0.9293, 0.7, 0, 0.4525, 0, 0, -0.8408, 0, 0.3, -1.1203, 0.6, 0, -1.1203, 0, 0.11, -2.8486, 0.2351, 0, -3.0108, 0, -0.11, -3.0108, 0, -0.3, -1.1203, 0, 0, -0.8408, 0.7, 0, 0.4525, 0, -0.4, 0.1885, -0.7, 0, -0.9849, -0.7, 0, 0.4525, 0, 0.4, -0.9849, -0.7, 0, 0.4525, 0, 0.4, 0.9293, 0, 0, -0.8408, -0.6, 0, -1.1203, -0.2351, 0, -3.0108, 0, 0, -0.8408, -0.7, 0, 0.4525, 0, -0.4, 0.1885, 0.7, 0, 0.4525, -0.7, 0, 0.4525
3099
+ ];
3100
+ this.indices = [0, 1, 2, 3, 4, 5, 1, 6, 2, 1, 7, 6, 8, 9, 10, 11, 12, 13, 14, 13, 12, 15, 14, 16, 13, 17, 18, 19, 20, 8, 17, 13, 15, 15, 16, 17, 21, 0, 2, 22, 3, 5, 21, 2, 6, 21, 6, 23, 8, 24, 25, 26, 27, 12, 27, 14, 12, 28, 16, 14, 27, 29, 17, 30, 8, 31, 17, 28, 27, 28, 17, 16, 0, 32, 1, 3, 1, 4, 14, 15, 13, 21, 33, 0, 22, 21, 3, 27, 28, 14
3101
+ ];
3102
+ this.normals = [0, -0.9962, 0.0863, 0.7964, 0, -0.6047, 0, -0.7073, -0.7069, 0, 0.7073, -0.7069, 0.9834, -0.0314, 0.1787, 0, 0.973, 0.231, 0, 0, -1, 0, 0.7073, -0.7069, 0, -0.0518, 0.9987, 0, 0.973, 0.231, 0.9834, -0.0314, 0.1787, 0, 0, -1, 0, 0.9499, 0.3124, 0.9369, 0.0022, 0.3496, 0, 0.9436, -0.3311, 0.6731, 0.0686, -0.7363, 0, -0.5683, -0.8228, 0, -0.948, 0.3182, 0, 0, -1, 0.9834, -0.0314, 0.1787, 0, -0.9962, 0.0863, -0.7964, 0, -0.6047, -0.9834, -0.0314, 0.1787, 0, 0.7073, -0.7069, -0.9834, -0.0314, 0.1787, 0, 0.973, 0.231, 0, 0, -1, -0.9369, 0.0022, 0.3496, -0.6731, 0.0686, -0.7363, 0, 0, -1, -0.9834, -0.0314, 0.1787, 0, -0.9962, 0.0863, 0.9834, -0.0314, 0.1787, -0.9834, -0.0314, 0.1787
3103
+ ];
3104
+ this.subMeshes = [
3105
+ new SubMesh(0, 90)
3106
+ ];
3107
+ }
3108
+ }
3109
+
3110
+ class CustomMesh {
3111
+ vertices;
3112
+ indices;
3113
+ normals;
3114
+ uvs;
3115
+ subMeshes;
3116
+ itemSize;
3117
+ primitive;
3118
+ primitives;
3119
+ constructor() {
3120
+ this.vertices = null;
3121
+ this.indices = null;
3122
+ this.normals = null;
3123
+ this.uvs = null;
3124
+ this.subMeshes = [];
3125
+ this.itemSize = 3;
3126
+ this.primitive = 'TRIANGLES';
3127
+ this.primitives = ['POINTS',
3128
+ 'LINE_STRIP',
3129
+ 'LINE_LOOP',
3130
+ 'LINES',
3131
+ 'TRIANGLE_STRIP',
3132
+ 'TRIANGLE_FAN',
3133
+ 'TRIANGLES'
3134
+ ];
3135
+ }
3136
+ static toFloat32(array) {
3137
+ const flat = new Float32Array(array.length * 3);
3138
+ for (let i = 0; i < array.length; i++)
3139
+ array[i].toArray(flat, i * 3);
3140
+ return flat;
3141
+ }
3142
+ setVertices(array) {
3143
+ this.vertices = CustomMesh.toFloat32(array);
3144
+ }
3145
+ setIndices(array) {
3146
+ this.indices = array instanceof Int32Array ? array.slice(0) : new Int32Array(array);
3147
+ }
3148
+ setNormals(array) {
3149
+ this.normals = CustomMesh.toFloat32(array);
3150
+ }
3151
+ setUvs(array) {
3152
+ this.uvs = array instanceof Float32Array ? array.slice(0) : new Float32Array(array);
3153
+ }
3154
+ addSubMeshes(array) {
3155
+ for (let i = 0; i < array.length; i += 2)
3156
+ this.addSubMesh(array[i], array[i + 1]);
3157
+ }
3158
+ addSubMesh(start, count) {
3159
+ this.subMeshes.push(new SubMesh(start, count));
3160
+ }
3161
+ setItemSize(itemSize) {
3162
+ this.itemSize = itemSize;
3163
+ }
3164
+ setPrimitive(primitive) {
3165
+ for (let i = 0; i < this.primitives.length; i++)
3166
+ if (this.primitives[i] === primitive) {
3167
+ this.primitive = primitive;
3168
+ return true;
3169
+ }
3170
+ return false;
3171
+ }
3172
+ static _vA = new Vec3();
3173
+ static _vB = new Vec3();
3174
+ static _vC = new Vec3();
3175
+ static _e1 = new Vec3();
3176
+ static _e2 = new Vec3();
3177
+ static _n = new Vec3();
3178
+ static _sum = new Vec3();
3179
+ static UP = new Vec3(0, 1, 0);
3180
+ static accumulate(normals, offset, n) {
3181
+ CustomMesh._sum.setArray(normals, offset).add(n).toArray(normals, offset);
3182
+ }
3183
+ computeVertexNormals() {
3184
+ if (!this.vertices || this.vertices.length === 0)
3185
+ return null;
3186
+ const vertices = this.vertices;
3187
+ const normals = new Float32Array(vertices.length);
3188
+ const vA = CustomMesh._vA, vB = CustomMesh._vB, vC = CustomMesh._vC;
3189
+ const e1 = CustomMesh._e1, e2 = CustomMesh._e2, n = CustomMesh._n;
3190
+ const faceNormal = (iA, iB, iC) => {
3191
+ vA.setArray(vertices, iA);
3192
+ vB.setArray(vertices, iB);
3193
+ vC.setArray(vertices, iC);
3194
+ return n.crossVectors(e1.subVectors(vB, vA), e2.subVectors(vC, vA));
3195
+ };
3196
+ if (this.indices && this.indices.length > 0)
3197
+ for (let i = 0; i + 2 < this.indices.length; i += 3) {
3198
+ const iA = this.indices[i] * 3;
3199
+ const iB = this.indices[i + 1] * 3;
3200
+ const iC = this.indices[i + 2] * 3;
3201
+ faceNormal(iA, iB, iC);
3202
+ CustomMesh.accumulate(normals, iA, n);
3203
+ CustomMesh.accumulate(normals, iB, n);
3204
+ CustomMesh.accumulate(normals, iC, n);
3205
+ }
3206
+ else
3207
+ for (let i = 0; i + 8 < vertices.length; i += 9) {
3208
+ faceNormal(i, i + 3, i + 6);
3209
+ n.toArray(normals, i);
3210
+ n.toArray(normals, i + 3);
3211
+ n.toArray(normals, i + 6);
3212
+ }
3213
+ for (let i = 0; i < normals.length; i += 3)
3214
+ n.setArray(normals, i).normalize(CustomMesh.UP).toArray(normals, i);
3215
+ this.normals = normals;
3216
+ return this.normals;
3217
+ }
3218
+ }
3219
+
3220
+ const SpriteShader = {
3221
+ vertex: `
3222
+ attribute vec3 aVertexPosition;
3223
+ attribute vec2 aTextureCoord;
3224
+
3225
+ uniform mat4 uModelMatrix;
3226
+ uniform mat4 uViewMatrix;
3227
+ uniform mat4 uProjectionMatrix;
3228
+ uniform float uFrame;
3229
+ uniform float uFrames;
3230
+ uniform float uBillboard;
3231
+ uniform float uDepthBias;
3232
+
3233
+ varying vec2 vTextureCoord;
3234
+ varying float vDepth;
3235
+
3236
+ void main(void) {
3237
+ vTextureCoord = vec2((uFrame + aTextureCoord.x) / uFrames, aTextureCoord.y);
3238
+ vec3 view;
3239
+ if (uBillboard < 0.5) {
3240
+ view = (uViewMatrix * uModelMatrix * vec4(aVertexPosition, 1.0)).xyz;
3241
+ } else {
3242
+ vec3 origin = (uViewMatrix * uModelMatrix * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
3243
+ vec3 up = vec3(0.0, 1.0, 0.0);
3244
+ vec3 right = vec3(1.0, 0.0, 0.0);
3245
+ if (uBillboard > 1.5) {
3246
+ up = normalize((uViewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz);
3247
+ vec3 across = cross(origin, up);
3248
+ if (length(across) > 0.0001)
3249
+ right = normalize(across);
3250
+ }
3251
+ float scaleX = length(uModelMatrix[0].xyz);
3252
+ float scaleY = length(uModelMatrix[1].xyz);
3253
+ view = origin + right * aVertexPosition.x * scaleX + up * aVertexPosition.y * scaleY;
3254
+ }
3255
+ vDepth = -view.z;
3256
+ vec4 clip = uProjectionMatrix * vec4(view, 1.0);
3257
+ // only the depth moves: the picture stays where it is and as big as it is
3258
+ float bias = min(uDepthBias, max(0.0, vDepth - 1.0));
3259
+ if (bias > 0.0) {
3260
+ vec4 nearer = uProjectionMatrix * vec4(view.xy, view.z + bias, 1.0);
3261
+ clip.z = nearer.z / nearer.w * clip.w;
3262
+ }
3263
+ gl_Position = clip;
3264
+ }
3265
+ `,
3266
+ fragment: `
3267
+ precision mediump float;
3268
+
3269
+ uniform sampler2D uSampler;
3270
+ uniform vec3 uMaterialDiffuse;
3271
+ uniform float uAlphaCutoff;
3272
+ uniform int uHasFog;
3273
+ uniform vec3 uFogColor;
3274
+ uniform float uFogNear;
3275
+ uniform float uFogFar;
3276
+
3277
+ varying vec2 vTextureCoord;
3278
+ varying float vDepth;
3279
+
3280
+ void main(void) {
3281
+ vec4 texel = texture2D(uSampler, vTextureCoord);
3282
+ if (texel.a < uAlphaCutoff)
3283
+ discard;
3284
+ vec3 color = texel.rgb * uMaterialDiffuse;
3285
+ if (uHasFog == 1) {
3286
+ float fade = clamp((vDepth - uFogNear) / (uFogFar - uFogNear), 0.0, 1.0);
3287
+ color = mix(color, uFogColor, fade);
3288
+ }
3289
+ gl_FragColor = vec4(color, texel.a);
3290
+ }
3291
+ `,
3292
+ create(context) {
3293
+ return Program.create(context, SpriteShader.vertex, SpriteShader.fragment);
3294
+ }
3295
+ };
3296
+
3297
+ const scratchSpritePos = new Vec3();
3298
+ const scratchSpriteScale = new Vec3();
3299
+ const scratchSpriteNormal = new Vec3();
3300
+ const scratchSpriteUp = new Vec3();
3301
+ const scratchSpriteRight = new Vec3();
3302
+ const scratchSpriteHit = new Vec3();
3303
+ const scratchSpriteC0 = new Vec3();
3304
+ const scratchSpriteC1 = new Vec3();
3305
+ const scratchSpriteC2 = new Vec3();
3306
+ const scratchSpriteC3 = new Vec3();
3307
+ function raycastSprite(sprite, ray) {
3308
+ const worldPos = sprite.getWorldPosition(scratchSpritePos);
3309
+ const scale = sprite.worldMatrix.getScale(scratchSpriteScale);
3310
+ const normal = scratchSpriteNormal;
3311
+ const up = scratchSpriteUp;
3312
+ const right = scratchSpriteRight;
3313
+ if (sprite.billboard === 'spherical') {
3314
+ normal.copy(ray.direction).opposite().normalize();
3315
+ up.setScalar(0, 1, 0);
3316
+ const dotUp = up.dotProduct(normal);
3317
+ up.subtractScaledVector(normal, dotUp);
3318
+ if (up.getMagnitude(true) < 0.0001) {
3319
+ up.setScalar(0, 0, 1);
3320
+ const dotZ = up.dotProduct(normal);
3321
+ up.subtractScaledVector(normal, dotZ);
3322
+ }
3323
+ up.normalize();
3324
+ right.crossVectors(up, normal).normalize();
3325
+ }
3326
+ else {
3327
+ up.setScalar(0, 1, 0);
3328
+ normal.setScalar(-ray.direction.x, 0, -ray.direction.z);
3329
+ if (normal.getMagnitude(true) < 0.0001)
3330
+ normal.setScalar(ray.origin.x - worldPos.x, 0, ray.origin.z - worldPos.z);
3331
+ if (normal.getMagnitude(true) < 0.0001)
3332
+ normal.setScalar(0, 0, 1);
3333
+ else
3334
+ normal.normalize();
3335
+ right.crossVectors(up, normal).normalize();
3336
+ }
3337
+ const hw = (sprite.width / 2) * scale.x;
3338
+ const y0 = (sprite.anchor === 'bottom' ? 0 : -sprite.height / 2) * scale.y;
3339
+ const y1 = (sprite.anchor === 'bottom' ? sprite.height : sprite.height / 2) * scale.y;
3340
+ const c0 = scratchSpriteC0.setScalar(worldPos.x - right.x * hw + up.x * y0, worldPos.y - right.y * hw + up.y * y0, worldPos.z - right.z * hw + up.z * y0);
3341
+ const c1 = scratchSpriteC1.setScalar(worldPos.x + right.x * hw + up.x * y0, worldPos.y + right.y * hw + up.y * y0, worldPos.z + right.z * hw + up.z * y0);
3342
+ const c2 = scratchSpriteC2.setScalar(worldPos.x + right.x * hw + up.x * y1, worldPos.y + right.y * hw + up.y * y1, worldPos.z + right.z * hw + up.z * y1);
3343
+ const c3 = scratchSpriteC3.setScalar(worldPos.x - right.x * hw + up.x * y1, worldPos.y - right.y * hw + up.y * y1, worldPos.z - right.z * hw + up.z * y1);
3344
+ let hit = ray.intersectTriangle(c0, c1, c2, false, scratchSpriteHit);
3345
+ if (!hit)
3346
+ hit = ray.intersectTriangle(c0, c2, c3, false, scratchSpriteHit);
3347
+ if (!hit)
3348
+ return null;
3349
+ return { distance: ray.origin.getDistance(hit), point: new Vec3().copy(hit) };
3350
+ }
3351
+
3352
+ const BILLBOARD_VALUES = { none: 0, spherical: 1, cylindrical: 2 };
3353
+ class Sprite extends Mesh {
3354
+ width;
3355
+ height;
3356
+ frames;
3357
+ anchor;
3358
+ billboard;
3359
+ currentFrame = 0;
3360
+ alphaCutoff = 0.5;
3361
+ depthBias = 0;
3362
+ static scratchSpriteMin = new Vec3();
3363
+ static scratchSpriteMax = new Vec3();
3364
+ static scratchSpriteBox = new Box();
3365
+ static scratchSpriteHit = new Vec3();
3366
+ constructor(context, options) {
3367
+ const size = options.size ?? new Vec2(1, 1);
3368
+ const width = size.x;
3369
+ const height = size.y;
3370
+ const anchor = options.anchor ?? 'center';
3371
+ const billboard = options.billboard ?? 'spherical';
3372
+ const frames = Math.max(1, Math.floor(options.frames ?? 1));
3373
+ const x = width / 2;
3374
+ const y0 = anchor === 'bottom' ? 0 : -height / 2;
3375
+ const y1 = anchor === 'bottom' ? height : height / 2;
3376
+ const geometry = {
3377
+ vertices: [-x, y0, 0, x, y0, 0, x, y1, 0, -x, y0, 0, x, y1, 0, -x, y1, 0],
3378
+ uvs: [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1],
3379
+ indices: null,
3380
+ normals: null,
3381
+ subMeshes: [],
3382
+ itemSize: 3,
3383
+ primitive: 'TRIANGLES'
3384
+ };
3385
+ super(geometry, context);
3386
+ this.width = width;
3387
+ this.height = height;
3388
+ this.frames = frames;
3389
+ this.anchor = anchor;
3390
+ this.billboard = billboard;
3391
+ this.alphaCutoff = options.alphaCutoff ?? 0.5;
3392
+ this.depthBias = options.depthBias ?? 0;
3393
+ const material = options.material ?? new Material({ diffuse: new Vec3(1, 1, 1) });
3394
+ this.addProgram(SpriteShader.vertex, options.fragment ?? SpriteShader.fragment, material);
3395
+ this.addCustomUniform('frame', 'uniform1f', 0);
3396
+ this.addCustomUniform('frames', 'uniform1f', frames);
3397
+ this.addCustomUniform('billboard', 'uniform1f', BILLBOARD_VALUES[billboard]);
3398
+ this.addCustomUniform('alphaCutoff', 'uniform1f', this.alphaCutoff);
3399
+ this.addCustomUniform('depthBias', 'uniform1f', this.depthBias);
3400
+ this.updateLocalBounds();
3401
+ if (options.texture)
3402
+ this.setTexture(options.texture, options.textureOptions);
3403
+ }
3404
+ updateLocalBounds() {
3405
+ const x = this.width / 2;
3406
+ const y0 = this.anchor === 'bottom' ? 0 : -this.height / 2;
3407
+ const y1 = this.anchor === 'bottom' ? this.height : this.height / 2;
3408
+ if (this.billboard !== 'none') {
3409
+ const reach = Math.hypot(x, Math.max(Math.abs(y0), Math.abs(y1)));
3410
+ if (this.billboard === 'spherical') {
3411
+ Sprite.scratchSpriteMin.setScalar(-reach, -reach, -reach);
3412
+ Sprite.scratchSpriteMax.setScalar(reach, reach, reach);
3413
+ }
3414
+ else {
3415
+ Sprite.scratchSpriteMin.setScalar(-x, y0, -x);
3416
+ Sprite.scratchSpriteMax.setScalar(x, y1, x);
3417
+ }
3418
+ }
3419
+ else {
3420
+ Sprite.scratchSpriteMin.setScalar(-x, y0, 0);
3421
+ Sprite.scratchSpriteMax.setScalar(x, y1, 0);
3422
+ }
3423
+ Sprite.scratchSpriteBox.set(Sprite.scratchSpriteMin, Sprite.scratchSpriteMax);
3424
+ this.setLocalBounds(Sprite.scratchSpriteBox);
3425
+ }
3426
+ setFrame(frame) {
3427
+ this.currentFrame = ((Math.floor(frame) % this.frames) + this.frames) % this.frames;
3428
+ this.setCustomUniform('frame', this.currentFrame);
3429
+ return this;
3430
+ }
3431
+ getFrame() {
3432
+ return this.currentFrame;
3433
+ }
3434
+ setFrames(frames) {
3435
+ this.frames = Math.max(1, Math.floor(frames));
3436
+ this.setCustomUniform('frames', this.frames);
3437
+ return this.setFrame(this.currentFrame);
3438
+ }
3439
+ getFrames() {
3440
+ return this.frames;
3441
+ }
3442
+ setBillboard(billboard) {
3443
+ if (this.billboard === billboard)
3444
+ return this;
3445
+ this.billboard = billboard;
3446
+ this.setCustomUniform('billboard', BILLBOARD_VALUES[billboard]);
3447
+ this.updateLocalBounds();
3448
+ return this;
3449
+ }
3450
+ getBillboard() {
3451
+ return this.billboard;
3452
+ }
3453
+ setAlphaCutoff(cutoff) {
3454
+ this.alphaCutoff = cutoff;
3455
+ this.setCustomUniform('alphaCutoff', cutoff);
3456
+ return this;
3457
+ }
3458
+ getAlphaCutoff() {
3459
+ return this.alphaCutoff;
3460
+ }
3461
+ setDepthBias(depthBias) {
3462
+ this.depthBias = depthBias;
3463
+ this.setCustomUniform('depthBias', depthBias);
3464
+ return this;
3465
+ }
3466
+ getDepthBias() {
3467
+ return this.depthBias;
3468
+ }
3469
+ getWidth() {
3470
+ return this.width;
3471
+ }
3472
+ getHeight() {
3473
+ return this.height;
3474
+ }
3475
+ getSize(target = new Vec2()) {
3476
+ target.x = this.width;
3477
+ target.y = this.height;
3478
+ return target;
3479
+ }
3480
+ getAnchor() {
3481
+ return this.anchor;
3482
+ }
3483
+ raycast(ray, precise = false) {
3484
+ if (!this.isActive())
3485
+ return null;
3486
+ if (this.billboard === 'none')
3487
+ return super.raycast(ray, precise);
3488
+ const sphere = this.computeBoundingSphere();
3489
+ const hitSphere = ray.intersectSphere(sphere, Sprite.scratchSpriteHit);
3490
+ if (!hitSphere)
3491
+ return null;
3492
+ if (!precise)
3493
+ return { distance: ray.origin.getDistance(hitSphere), point: new Vec3().copy(hitSphere) };
3494
+ return raycastSprite(this, ray);
3495
+ }
3496
+ }
3497
+
3498
+ class DirectionalLight {
3499
+ position;
3500
+ diffuse;
3501
+ specular;
3502
+ direction;
3503
+ type;
3504
+ constructor(directionOrOptions) {
3505
+ this.position = new Vec3();
3506
+ this.direction = new Vec3(0, -1, 0);
3507
+ this.diffuse = new Vec3(0.6, 0.6, 0.6);
3508
+ this.specular = new Vec3(0.8, 0.8, 0.8);
3509
+ this.type = 'directional';
3510
+ if (directionOrOptions)
3511
+ if (directionOrOptions instanceof Vec3)
3512
+ this.setDirection(directionOrOptions);
3513
+ else {
3514
+ if (directionOrOptions.direction)
3515
+ this.setDirection(directionOrOptions.direction);
3516
+ if (directionOrOptions.diffuse)
3517
+ this.setDiffuse(directionOrOptions.diffuse);
3518
+ if (directionOrOptions.specular)
3519
+ this.setSpecular(directionOrOptions.specular);
3520
+ if (directionOrOptions.position)
3521
+ this.setPosition(directionOrOptions.position);
3522
+ }
3523
+ }
3524
+ setPosition(position) {
3525
+ this.position.copy(position);
3526
+ }
3527
+ setDirection(direction) {
3528
+ this.direction.copy(direction).normalize();
3529
+ }
3530
+ setDiffuse(diffuse) {
3531
+ this.diffuse.copy(diffuse);
3532
+ }
3533
+ setSpecular(specular) {
3534
+ this.specular.copy(specular);
3535
+ }
3536
+ }
3537
+
3538
+ class PointLight extends DirectionalLight {
3539
+ constantAttenuation;
3540
+ linearAttenuation;
3541
+ quadraticAttenuation;
3542
+ constructor(positionOrOptions) {
3543
+ super();
3544
+ this.constantAttenuation = 0;
3545
+ this.linearAttenuation = 1.0;
3546
+ this.quadraticAttenuation = 0;
3547
+ this.type = 'point';
3548
+ if (positionOrOptions)
3549
+ if (positionOrOptions instanceof Vec3)
3550
+ this.setPosition(positionOrOptions);
3551
+ else {
3552
+ if (positionOrOptions.position)
3553
+ this.setPosition(positionOrOptions.position);
3554
+ if (positionOrOptions.diffuse)
3555
+ this.setDiffuse(positionOrOptions.diffuse);
3556
+ if (positionOrOptions.specular)
3557
+ this.setSpecular(positionOrOptions.specular);
3558
+ if (positionOrOptions.constantAttenuation !== undefined)
3559
+ this.setConstantAttenuation(positionOrOptions.constantAttenuation);
3560
+ if (positionOrOptions.linearAttenuation !== undefined)
3561
+ this.setLinearAttenuation(positionOrOptions.linearAttenuation);
3562
+ if (positionOrOptions.quadraticAttenuation !== undefined)
3563
+ this.setQuadraticAttenuation(positionOrOptions.quadraticAttenuation);
3564
+ }
3565
+ }
3566
+ setConstantAttenuation(value) {
3567
+ this.constantAttenuation = value;
3568
+ }
3569
+ setLinearAttenuation(value) {
3570
+ this.linearAttenuation = value;
3571
+ }
3572
+ setQuadraticAttenuation(value) {
3573
+ this.quadraticAttenuation = value;
3574
+ }
3575
+ }
3576
+
3577
+ class SpotLight extends PointLight {
3578
+ cutoff;
3579
+ exponent;
3580
+ direction;
3581
+ constructor(positionOrOptions) {
3582
+ super(positionOrOptions);
3583
+ this.cutoff = 180;
3584
+ this.exponent = 0;
3585
+ this.direction = new Vec3(1.0, 0.0, 0.0);
3586
+ this.type = 'spot';
3587
+ if (positionOrOptions && !(positionOrOptions instanceof Vec3)) {
3588
+ if (positionOrOptions.direction)
3589
+ this.setDirection(positionOrOptions.direction);
3590
+ if (positionOrOptions.cutoff !== undefined)
3591
+ this.setCutoff(positionOrOptions.cutoff);
3592
+ if (positionOrOptions.exponent !== undefined)
3593
+ this.setExponent(positionOrOptions.exponent);
3594
+ }
3595
+ }
3596
+ setCutoff(value) {
3597
+ this.cutoff = value;
3598
+ }
3599
+ setExponent(value) {
3600
+ this.exponent = value;
3601
+ }
3602
+ setDirection(direction) {
3603
+ this.direction.copy(direction);
3604
+ }
3605
+ }
3606
+
3607
+ class AmbientLight {
3608
+ diffuse;
3609
+ intensity;
3610
+ type;
3611
+ constructor(diffuse = new Vec3(1.0, 1.0, 1.0), intensity = 1.0) {
3612
+ this.diffuse = new Vec3().copy(diffuse);
3613
+ this.intensity = intensity;
3614
+ this.type = 'ambient';
3615
+ }
3616
+ setColor(diffuse) {
3617
+ this.diffuse.copy(diffuse);
3618
+ return this;
3619
+ }
3620
+ setIntensity(intensity) {
3621
+ this.intensity = intensity;
3622
+ return this;
3623
+ }
3624
+ getColor(target = new Vec3()) {
3625
+ return target.copy(this.diffuse);
3626
+ }
3627
+ get color() {
3628
+ return this.diffuse;
3629
+ }
3630
+ set color(c) {
3631
+ this.diffuse.copy(c);
3632
+ }
3633
+ }
3634
+
3635
+ function assignVec3(target, xOrVec, y, z) {
3636
+ if (xOrVec instanceof Vec3)
3637
+ return target.copy(xOrVec);
3638
+ if (typeof xOrVec === 'number')
3639
+ return target.setScalar(xOrVec, y, z);
3640
+ return target.setArray(xOrVec);
3641
+ }
3642
+ function assignVec2(target, xOrVec, y) {
3643
+ if (xOrVec instanceof Vec2)
3644
+ return target.copy(xOrVec);
3645
+ if (typeof xOrVec === 'number')
3646
+ return target.setScalar(xOrVec, y);
3647
+ return target.setArray(xOrVec);
3648
+ }
3649
+
3650
+ export { AmbientLight, BasicMesh, BasicShader, Camera, Cannon, Cube, CustomMesh, Cylinder, DirectionalLight, FullscreenQuad, Hemisphere, Lights, Line, MAX_LIGHTS, Material, Mesh, MultiQuad, Node, NormalShader, OrbitControls, OrthographicCamera, PerspectiveCamera, PhongShader, Plane, PointLight, Program, Quad, RenderTarget, Renderer, Scene, Shader, Sphere, SpotLight, Sprite, SpriteShader, SubMesh, Texture, Uniform, VWing, assignVec2, assignVec3, eDrawMethod };