forked from maierfelix/3d-code-vizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rokon.js
9852 lines (8998 loc) · 295 KB
/
rokon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var SunriseEngine = (function () {
'use strict';
let uiid = 0;
/**
* Returns a unique number
* @return {Number}
*/
function uid() {
return uiid++;
}
/**
* Loads and resolves a text
* @param {String} path - Path to the text
* @return {Promise}
*/
function loadText(path) {
return new Promise(resolve => {
fetch(path)
.then(resp => resp.text())
.then(resolve);
});
}
/**
* Loads and resolves a binary file
* @param {String} path - Path to the text
* @return {Promise}
*/
/**
* Loads and resolves an image
* @param {String} path - Path to the image
* @return {Promise}
*/
function loadImage(path) {
return new Promise(resolve => {
let img = new Image();
img.onload = () => resolve(img);
img.src = path;
});
}
/**
* Loads an image and resolves it as a canvas
* @param {String} path - Path to the image
* @return {Promise}
*/
/**
* Creates a canvas with the given dimensions
* @param {Number} width
* @param {Number} height
* @return {Canvas2DRenderingContext}
*/
function createCanvasBuffer(width, height) {
let canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext("2d");
return ctx;
}
/**
* Indicates if a number is power of two
* @param {Number} v
* @return {Boolean}
*/
function isPowerOf2(v) {
return (v & (v - 1)) === 0;
}
/**
* Converts radians to degrees
* @param {Number} r
* @return {Number}
*/
/**
* Converts degrees to radians
* @param {Number} d
* @return {Number}
*/
/**
* Clamps number between min, max
* @param {Number} n
* @param {Number} min
* @param {Number} max
* @return {Number}
*/
function clamp(n, min, max) {
return Math.min(Math.max(n, min), max);
}
/**
* Max safe integer
*/
const MAX_INTEGER = Number.MAX_SAFE_INTEGER;
/**
* Creates a new transformation matrix
* @param {Float32Array} translation
* @param {Float32Array} rotation
* @param {Float32Array} scale
* @return {Float32Array}
*/
/**
* Returns the binary data representation of an image
* @param {HTMLImageElement}
* @return {Uint8Array}
*/
function getImageBinaryData(img) {
let width = img.width;
let height = img.height;
let ctx = createCanvasBuffer(width, height);
ctx.drawImage(
img,
0, 0
);
let data = ctx.getImageData(0, 0, width, height).data;
return new Uint8Array(data);
}
/**
* @param {Array} p1
* @param {Array} p2
* @param {Array} p3
* @param {Array} pos
* @return {Number}
*/
function baryCentric(p1, p2, p3, pos) {
let det = (p2[2] - p3[2]) * (p1[0] - p3[0]) + (p3[0] - p2[0]) * (p1[2] - p3[2]);
let l1 = ((p2[2] - p3[2]) * (pos[0] - p3[0]) + (p3[0] - p2[0]) * (pos[1] - p3[2])) / det;
let l2 = ((p3[2] - p1[2]) * (pos[0] - p3[0]) + (p1[0] - p3[0]) * (pos[1] - p3[2])) / det;
let l3 = 1.0 - l1 - l2;
return l1 * p1[1] + l2 * p2[1] + l3 * p3[1];
}
/**
* Calculates the tangents and bitangents of an object
* @param {WebGLObject} obj
* @return {Object} tangents, bitangents
*/
function calculateTangentsBitangents(obj) {
let data = obj.data;
let uvs = data.uvs;
let normals = data.normals;
let indices = data.indices;
let vertices = data.vertices;
let tangents = new Float32Array(vertices.length);
let bitangents = new Float32Array(vertices.length);
for (let ii = 0; ii < indices.length; ii += 3) {
let i0 = indices[ii + 0];
let i1 = indices[ii + 1];
let i2 = indices[ii + 2];
let x_v0 = vertices[i0 * 3 + 0];
let y_v0 = vertices[i0 * 3 + 1];
let z_v0 = vertices[i0 * 3 + 2];
let x_uv0 = uvs[i0 * 2 + 0];
let y_uv0 = uvs[i0 * 2 + 1];
let x_v1 = vertices[i1 * 3 + 0];
let y_v1 = vertices[i1 * 3 + 1];
let z_v1 = vertices[i1 * 3 + 2];
let x_uv1 = uvs[i1 * 2 + 0];
let y_uv1 = uvs[i1 * 2 + 1];
let x_v2 = vertices[i2 * 3 + 0];
let y_v2 = vertices[i2 * 3 + 1];
let z_v2 = vertices[i2 * 3 + 2];
let x_uv2 = uvs[i2 * 2 + 0];
let y_uv2 = uvs[i2 * 2 + 1];
let x_deltaPos1 = x_v1 - x_v0;
let y_deltaPos1 = y_v1 - y_v0;
let z_deltaPos1 = z_v1 - z_v0;
let x_deltaPos2 = x_v2 - x_v0;
let y_deltaPos2 = y_v2 - y_v0;
let z_deltaPos2 = z_v2 - z_v0;
let x_uvDeltaPos1 = x_uv1 - x_uv0;
let y_uvDeltaPos1 = y_uv1 - y_uv0;
let x_uvDeltaPos2 = x_uv2 - x_uv0;
let y_uvDeltaPos2 = y_uv2 - y_uv0;
let rInv = x_uvDeltaPos1 * y_uvDeltaPos2 - y_uvDeltaPos1 * x_uvDeltaPos2;
let r = 1.0 / (Math.abs(rInv < 0.0001) ? 1.0 : rInv);
// tangent
let x_tangent = (x_deltaPos1 * y_uvDeltaPos2 - x_deltaPos2 * y_uvDeltaPos1) * r;
let y_tangent = (y_deltaPos1 * y_uvDeltaPos2 - y_deltaPos2 * y_uvDeltaPos1) * r;
let z_tangent = (z_deltaPos1 * y_uvDeltaPos2 - z_deltaPos2 * y_uvDeltaPos1) * r;
// bitangent
let x_bitangent = (x_deltaPos2 * x_uvDeltaPos1 - x_deltaPos1 * x_uvDeltaPos2) * r;
let y_bitangent = (y_deltaPos2 * x_uvDeltaPos1 - y_deltaPos1 * x_uvDeltaPos2) * r;
let z_bitangent = (z_deltaPos2 * x_uvDeltaPos1 - z_deltaPos1 * x_uvDeltaPos2) * r;
// Gram-Schmidt orthogonalize
//t = glm::normalize(t - n * glm:: dot(n, t));
let x_n0 = normals[i0 * 3 + 0];
let y_n0 = normals[i0 * 3 + 1];
let z_n0 = normals[i0 * 3 + 2];
let x_n1 = normals[i1 * 3 + 0];
let y_n1 = normals[i1 * 3 + 1];
let z_n1 = normals[i1 * 3 + 2];
let x_n2 = normals[i2 * 3 + 0];
let y_n2 = normals[i2 * 3 + 1];
let z_n2 = normals[i2 * 3 + 2];
// tangent
let n0_dot_t = x_tangent * x_n0 + y_tangent * y_n0 + z_tangent * z_n0;
let n1_dot_t = x_tangent * x_n1 + y_tangent * y_n1 + z_tangent * z_n1;
let n2_dot_t = x_tangent * x_n2 + y_tangent * y_n2 + z_tangent * z_n2;
let x_resTangent0 = x_tangent - x_n0 * n0_dot_t;
let y_resTangent0 = y_tangent - y_n0 * n0_dot_t;
let z_resTangent0 = z_tangent - z_n0 * n0_dot_t;
let x_resTangent1 = x_tangent - x_n1 * n1_dot_t;
let y_resTangent1 = y_tangent - y_n1 * n1_dot_t;
let z_resTangent1 = z_tangent - z_n1 * n1_dot_t;
let x_resTangent2 = x_tangent - x_n2 * n2_dot_t;
let y_resTangent2 = y_tangent - y_n2 * n2_dot_t;
let z_resTangent2 = z_tangent - z_n2 * n2_dot_t;
let magTangent0 = Math.sqrt(
x_resTangent0 * x_resTangent0 + y_resTangent0 * y_resTangent0 + z_resTangent0 * z_resTangent0
);
let magTangent1 = Math.sqrt(
x_resTangent1 * x_resTangent1 + y_resTangent1 * y_resTangent1 + z_resTangent1 * z_resTangent1
);
let magTangent2 = Math.sqrt(
x_resTangent2 * x_resTangent2 + y_resTangent2 * y_resTangent2 + z_resTangent2 * z_resTangent2
);
// bitangent
let n0_dot_bt = x_bitangent * x_n0 + y_bitangent * y_n0 + z_bitangent * z_n0;
let n1_dot_bt = x_bitangent * x_n1 + y_bitangent * y_n1 + z_bitangent * z_n1;
let n2_dot_bt = x_bitangent * x_n2 + y_bitangent * y_n2 + z_bitangent * z_n2;
let x_resBitangent0 = x_bitangent - x_n0 * n0_dot_bt;
let y_resBitangent0 = y_bitangent - y_n0 * n0_dot_bt;
let z_resBitangent0 = z_bitangent - z_n0 * n0_dot_bt;
let x_resBitangent1 = x_bitangent - x_n1 * n1_dot_bt;
let y_resBitangent1 = y_bitangent - y_n1 * n1_dot_bt;
let z_resBitangent1 = z_bitangent - z_n1 * n1_dot_bt;
let x_resBitangent2 = x_bitangent - x_n2 * n2_dot_bt;
let y_resBitangent2 = y_bitangent - y_n2 * n2_dot_bt;
let z_resBitangent2 = z_bitangent - z_n2 * n2_dot_bt;
let magBitangent0 = Math.sqrt(
x_resBitangent0 * x_resBitangent0 +
y_resBitangent0 * y_resBitangent0 +
z_resBitangent0 * z_resBitangent0
);
let magBitangent1 = Math.sqrt(
x_resBitangent1 * x_resBitangent1 +
y_resBitangent1 * y_resBitangent1 +
z_resBitangent1 * z_resBitangent1
);
let magBitangent2 = Math.sqrt(
x_resBitangent2 * x_resBitangent2 +
y_resBitangent2 * y_resBitangent2 +
z_resBitangent2 * z_resBitangent2
);
tangents[i0 * 3 + 0] += x_resTangent0 / magTangent0;
tangents[i0 * 3 + 1] += y_resTangent0 / magTangent0;
tangents[i0 * 3 + 2] += z_resTangent0 / magTangent0;
tangents[i1 * 3 + 0] += x_resTangent1 / magTangent1;
tangents[i1 * 3 + 1] += y_resTangent1 / magTangent1;
tangents[i1 * 3 + 2] += z_resTangent1 / magTangent1;
tangents[i2 * 3 + 0] += x_resTangent2 / magTangent2;
tangents[i2 * 3 + 1] += y_resTangent2 / magTangent2;
tangents[i2 * 3 + 2] += z_resTangent2 / magTangent2;
bitangents[i0 * 3 + 0] += x_resBitangent0 / magBitangent0;
bitangents[i0 * 3 + 1] += y_resBitangent0 / magBitangent0;
bitangents[i0 * 3 + 2] += z_resBitangent0 / magBitangent0;
bitangents[i1 * 3 + 0] += x_resBitangent1 / magBitangent1;
bitangents[i1 * 3 + 1] += y_resBitangent1 / magBitangent1;
bitangents[i1 * 3 + 2] += z_resBitangent1 / magBitangent1;
bitangents[i2 * 3 + 0] += x_resBitangent2 / magBitangent2;
bitangents[i2 * 3 + 1] += y_resBitangent2 / magBitangent2;
bitangents[i2 * 3 + 2] += z_resBitangent2 / magBitangent2;
}
let dataIndex = 0;
let debugNormals = new Float32Array(vertices.length * 2);
for (let ii = 0; ii < vertices.length; ii += 3) {
let v0 = vertices[ii + 0];
let v1 = vertices[ii + 1];
let v2 = vertices[ii + 2];
let n0 = normals[ii + 0];
let n1 = normals[ii + 1];
let n2 = normals[ii + 2];
debugNormals[dataIndex + 0] = v0;
debugNormals[dataIndex + 1] = v1;
debugNormals[dataIndex + 2] = v2;
debugNormals[dataIndex + 3] = v0 + n0;
debugNormals[dataIndex + 4] = v1 + n1;
debugNormals[dataIndex + 5] = v2 + n2;
dataIndex += 6;
}
return { tangents, bitangents, debugNormals };
}
/**
* @param {Class} cls
* @param {Array} prot
*/
var extend = function(cls, prot) {
for (let key in prot) {
if (prot[key] instanceof Function) {
if (cls.prototype[key] instanceof Function) {
console.log(`Warning: Overwriting ${cls.name}.prototype.${key}`);
}
cls.prototype[key] = prot[key];
}
}
};
let idx = 0;
const UNKNOWN = ++idx;
const BOOL = ++idx;
const FLOAT = ++idx;
const VEC_2 = ++idx;
const VEC_3 = ++idx;
const VEC_4 = ++idx;
const I_VEC_2 = ++idx;
const I_VEC_3 = ++idx;
const I_VEC_4 = ++idx;
const MAT_3 = ++idx;
const MAT_4 = ++idx;
const UNIFORM = ++idx;
const ATTRIBUTE = ++idx;
const SAMPLER_2D = ++idx;
const SAMPLER_CUBE = ++idx;
/**
* A loader
* @class ObjectLoader
*/
class ObjectLoader {
/**
* @param {WebGLRenderer} renderer
* @param {WebGLObject} object
* @constructor
*/
constructor(renderer, object) {
this.uid = uid();
this.object = object;
this.renderer = renderer;
this.gl = this.renderer.gl;
this.buffers = {
vertices: null,
normals: null,
uvs: null,
tangents: null,
bitangents: null,
indices: null
};
this.ready = false;
this.load();
}
}
/**
* Create all buffers
* @param {Object} data
*/
ObjectLoader.prototype.createBuffers = function(data) {
let gl = this.gl;
let buffers = this.buffers;
if (data.vertices) buffers.vertices = gl.createBuffer();
if (data.normals) buffers.normals = gl.createBuffer();
if (data.uvs) buffers.uvs = gl.createBuffer();
if (data.tangents) buffers.tangents = gl.createBuffer();
if (data.bitangents) buffers.bitangents = gl.createBuffer();
if (data.debugNormals) buffers.debugNormals = gl.createBuffer();
if (data.indices) buffers.indices = gl.createBuffer();
};
/**
* Load everything
*/
ObjectLoader.prototype.load = function() {
let gl = this.gl;
let object = this.object;
let buffers = this.buffers;
let data = object.data;
// calculate other stuff
if (data.indices) {
let result = calculateTangentsBitangents(object);
data.tangents = result.tangents;
data.bitangents = result.bitangents;
data.debugNormals = result.debugNormals;
}
// buffers are not created yet
if (!this.ready) {
this.createBuffers(data);
this.ready = true;
}
if (data.vertices) this.updateBuffer(buffers.vertices, data.vertices, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.normals) this.updateBuffer(buffers.normals, data.normals, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.uvs) this.updateBuffer(buffers.uvs, data.uvs, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.tangents) this.updateBuffer(buffers.tangents, data.tangents, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.bitangents) this.updateBuffer(buffers.bitangents, data.bitangents, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.debugNormals) this.updateBuffer(buffers.debugNormals, data.debugNormals, gl.ARRAY_BUFFER, gl.STATIC_DRAW);
if (data.indices) this.updateBuffer(buffers.indices, data.indices, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW);
};
/**
* Update a buffer
* @param {WebGLBuffer} buffer
* @param {TypedArray} data
* @param {Number} target
* @param {Number} usage
*/
ObjectLoader.prototype.updateBuffer = function(buffer, data, target, usage) {
let gl = this.gl;
gl.bindBuffer(target, buffer);
gl.bufferData(target, data, usage);
gl.bindBuffer(target, null);
};
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var objectReflector = createCommonjsModule(function (module, exports) {
(function (global, factory) {
module.exports = factory();
}(commonjsGlobal, (function () { 'use strict';
var PREFIX = "__$";
/**
* Main class
* @class ObjectReflector
*/
var ObjectReflector = function ObjectReflector(opts) {
if ( opts === void 0 ) opts = {};
this.object = null;
this.children = [];
this.properties = [];
this.processOptions(opts);
};
/**
* Process the passed in options
* Options:
* - object: The target parent object
* - *properties: The properties to link children to
* - *enableChildReflection: Allows linked children to change their parent's linked properties
* @param {Object} opts
*/
ObjectReflector.prototype.processOptions = function(opts) {
if (!opts.hasOwnProperty("object")) {
throw new Error("Expected target object but got nothing");
}
if (opts.object === null) {
throw new Error("Invalid type for object definition! Expect Object but got null");
}
if (typeof opts.object !== "object") {
throw new Error(("Invalid type for object definition! Expect Object but got " + (typeof object)));
}
this.object = opts.object;
this.enableChildReflection = opts.enableChildReflection;
if (opts.hasOwnProperty("properties")) { this.linkProperties(opts.properties); }
};
/**
* Attaches the given properties to the parent object
* @param {Array} properties - The properties to link
* @return {ObjectReflector}
*/
ObjectReflector.prototype.linkProperties = function(properties) {
if (!Array.isArray(properties)) {
throw new Error(("Invalid type for property definitions! Expected Array but got " + (typeof properties)));
}
this.properties = properties;
if (this.properties.length) { this.createObjectPropertyLinks(this.object); }
return this;
};
/**
* Clears everything
* @public
*/
ObjectReflector.prototype.clear = function() {
// clear child reflections
if (this.enableChildReflection) { this.clearChildReflections(); }
// clear parent reflections
this.clearParentReflection();
this.children = [];
};
/**
* Clears the parent reflection
*/
ObjectReflector.prototype.clearParentReflection = function() {
var this$1 = this;
var object = this.object;
var properties = this.properties;
for (var ii = 0; ii < properties.length; ++ii) {
var property = properties[ii];
this$1.clearReflectedObjectProperty(object, property, true);
this$1.clearReflectedObjectProperty(object, PREFIX + property, false);
}
};
/**
* Clears all reflected children
*/
ObjectReflector.prototype.clearChildReflections = function() {
var this$1 = this;
var children = this.children;
for (var ii = 0; ii < children.length; ++ii) {
var child = children[ii];
this$1.unreflectChild(child);
}
};
/**
* Deletes the reflection properties of an reflected object
* @param {Object} object - The object to unreflect
* @param {String} property - The property to unreflect
* @param {Boolean} backup - Backup and restore the original value afterwards
*/
ObjectReflector.prototype.clearReflectedObjectProperty = function(object, property, backup) {
if ( backup === void 0 ) backup = false;
var original = object[property];
if (object.hasOwnProperty(property)) {
Reflect.deleteProperty(object, property);
}
if (backup) { object[property] = original; }
};
/**
* @param {Object} child - The target child to unreflect
* @public
*/
ObjectReflector.prototype.unreflectChild = function(child) {
var this$1 = this;
var properties = this.properties;
for (var ii = 0; ii < properties.length; ++ii) {
var property = properties[ii];
this$1.clearReflectedObjectProperty(child, property, true);
}
var index = this.getChildIndexByChild(child);
if (index > -1) { this.children.splice(index, 1); }
};
/**
* Let the user create a link from object A to object B
* @param {Object} child - The child to link
* @public
*/
ObjectReflector.prototype.createReflection = function(child) {
if (!this.isChildRegistered(child)) {
this.registerChild(child);
this.synchronizeChildState(child);
if (this.enableChildReflection) { this.createChildReflection(child); }
}
};
/**
* Creates reflection property links on a child
* @param {Object} child - The child to reflect
*/
ObjectReflector.prototype.createChildReflection = function(child) {
var parent = this.object;
var properties = this.properties;
var loop = function ( ii ) {
var property = properties[ii];
Object.defineProperty(child, property, {
enumerable: false,
configurable: true,
get: function () { return parent[property]; },
set: function (value) { return parent[PREFIX + property] = value; }
});
};
for (var ii = 0; ii < properties.length; ++ii) loop( ii );
};
/**
* Enables linking of the parent object's given properties
* @param {Object} parent - The parent object to link
*/
ObjectReflector.prototype.createObjectPropertyLinks = function(parent) {
var this$1 = this;
var properties = this.properties;
var loop = function ( ii ) {
var property = properties[ii];
Object.defineProperty(parent, PREFIX + property, {
writable: true,
enumerable: false,
configurable: true,
value: parent[property]
});
Object.defineProperty(parent, property, {
get: function () { return parent[PREFIX + property]; },
set: function (value) { return this$1.updateLinkedChildrenPropertyValues(property, value); }
});
};
for (var ii = 0; ii < properties.length; ++ii) loop( ii );
};
/**
* Registers a child object into the linker
* @param {Object} child - The child to register
*/
ObjectReflector.prototype.registerChild = function(child) {
this.children.push(child);
};
/**
* Checks if a child is already registered into the linker
* @param {Object} child - The child to check
* @return {Boolean}
*/
ObjectReflector.prototype.isChildRegistered = function(child) {
return this.getChildIndexByChild(child) > -1;
};
/**
* Tries to fetch a child from the registered childrens
* @param {Object} target - The target child to search for
* @return {Number} The target child's index
*/
ObjectReflector.prototype.getChildIndexByChild = function(target) {
var children = this.children;
for (var ii = 0; ii < children.length; ++ii) {
var child = children[ii];
if (child === target) { return ii; }
}
return -1;
};
/**
* Synchronizes the child's state with the parent's state
* @param {Object} child - The target child
*/
ObjectReflector.prototype.synchronizeChildState = function(child) {
var parent = this.object;
var properties = this.properties;
for (var ii = 0; ii < properties.length; ++ii) {
var property = properties[ii];
child[property] = parent[property];
}
};
/**
* Updates the linked property values of all children
* @param {String} property - The property to update
* @param {*} value - The value to update with
* @return {*} The updated value
*/
ObjectReflector.prototype.updateLinkedChildrenPropertyValues = function(property, value) {
this.object[PREFIX + property] = value;
var children = this.children;
for (var ii = 0; ii < children.length; ++ii) {
var child = children[ii];
child[property] = value;
}
return value;
};
return ObjectReflector;
})));
});
/**
* Bounding info
* @class BoundingInfo
*/
class BoundingInfo {
/**
* @param {Boolean} scale
* @param {Boolean} rotate
* @param {Boolean} translate
* @constructor
*/
constructor(scale, rotate, translate) {
this.uid = uid();
this.min = vec3.create();
this.max = vec3.create();
this.size = vec3.create();
this.center = vec3.create();
this.radius = 0;
this.updates = 0;
this.helpers = {
scale: vec3.create(),
translate: vec3.create(),
vertex: vec4.create(),
matrix: mat4.create()
};
this.settings = {
scale: scale,
rotate: rotate,
translate: translate
};
}
}
/**
* Box-based bounding
* @class BoundingBox
*/
class BoundingBox {
/**
* @param {WebGLObject} object
* @constructor
*/
constructor(object) {
this.uid = uid();
this.object = object;
this.local = new BoundingInfo(false, false, false);
this.world = new BoundingInfo(true, true, true);
// auto calculate local boundings
this.update(this.local);
}
}
/**
* Updates the boundings
* @param {BoundingInfo} space
* @param {Object} settings
*/
BoundingBox.prototype.update = function(space) {
let object = this.object;
let min = space.min;
let max = space.max;
let size = space.size;
let center = space.center;
let helpers = space.helpers;
let spaceSettings = space.settings;
let isWorldSpace = (space === this.world);
let vertices = object.data.vertices;
let length = (vertices.length / 3) | 0;
let vertex = helpers.vertex;
let mm = MAX_INTEGER;
// we can easily transform local->world space
if (isWorldSpace && (this.local.updates > 0)) {
this.updateWorldSpace();
// fetch local center, min, max one time
} else {
// reset
vec3.set(min, +mm, +mm, +mm);
vec3.set(max, -mm, -mm, -mm);
vec4.set(vertex, 0, 0, 0, 0);
// loop vertices, apply transformations, get min/max
for (let ii = 0; ii < length; ++ii) {
let index = (ii * 3) | 0;
vec4.set(
vertex,
vertices[index + 0],
vertices[index + 1],
vertices[index + 2],
1.0
);
this.applyTransformation(vertex, space, spaceSettings);
let x = vertex[0];
let y = vertex[1];
let z = vertex[2];
// min
if (x < min[0]) min[0] = x;
if (y < min[1]) min[1] = y;
if (z < min[2]) min[2] = z;
// max
if (x > max[0]) max[0] = x;
if (y > max[1]) max[1] = y;
if (z > max[2]) max[2] = z;
}
}
// center
{
vec3.add(center, min, max);
vec3.scale(center, center, 0.5);
space.center = center;
}
// size
{
vec3.subtract(size, max, min);
if (space.settings.scale) {
vec3.scale(size, size, 0.5);
}
space.size = size;
}
// radius
{
let length = vec3.length(size);
space.radius = length;
}
space.updates++;
};
/**
* Apply given transformations to a vertex
* @param {Float32Array} vertex
* @param {BoundingInfo} space
* @param {Object} settings
*/
BoundingBox.prototype.applyTransformation = function(vertex, space, settings) {
let object = this.object;
let helpers = space.helpers;
let scale = object.scale;
let rotate = object.rotate;
let translate = object.translate;
let vScale = helpers.scale;
let vTranslate = helpers.translate;
let mTransform = helpers.matrix;
let deg2Rad = (Math.PI / 180);
mat4.identity(mTransform);
// translate
if (settings.translate) {
mat4.translate(
mTransform,
mTransform,
vec3.set(vTranslate, translate.x, translate.y, translate.z)
);
}
// rotation
if (settings.rotate) {
mat4.rotateX(
mTransform,
mTransform,
rotate.x * deg2Rad
);
mat4.rotateY(
mTransform,
mTransform,
rotate.y * deg2Rad
);
mat4.rotateZ(
mTransform,
mTransform,
rotate.z * deg2Rad
);
}
// scale
if (settings.scale) {
mat4.scale(
mTransform,
mTransform,
vec3.set(vScale, scale.x, scale.y, scale.z)
);
}
vec4.transformMat4(
vertex,
vertex,
mTransform
);
};
/**
* Apply translation only
*/
BoundingBox.prototype.updateWorldSpace = function() {
let world = this.world;
let local = this.local;
let helpers = world.helpers;
// this ensures that all transformations take into account
let settings = { scale: true, rotate: true, translate: true };
let vec = helpers.vertex;
// center
{
vec4.set(vec, local.center[0], local.center[1], local.center[2], 1.0);
this.applyTransformation(vec, world, settings);
world.center[0] = vec[0];
world.center[1] = vec[1];
world.center[2] = vec[2];
}
// min
{
vec4.set(vec, local.min[0], local.min[1], local.min[2], 1.0);
this.applyTransformation(vec, world, settings);
world.min[0] = vec[0];
world.min[1] = vec[1];
world.min[2] = vec[2];
}
// max
{
vec4.set(vec, local.max[0], local.max[1], local.max[2], 1.0);
this.applyTransformation(vec, world, settings);
world.max[0] = vec[0];
world.max[1] = vec[1];
world.max[2] = vec[2];
}
};
/**
* Checks if the local world bounds intersect with another object's world bounds
* @param {WebGLObject} object
* @return {Boolean}
*/
BoundingBox.prototype.intersectsWithObject = function(object) {
let a = this.world;
let b = object.boundings.world;
return (
(a.max[0] > b.min[0]) &&
(a.min[0] < b.max[0]) &&
(a.max[1] > b.min[1]) &&
(a.min[1] < b.max[1]) &&
(a.max[2] > b.min[2]) &&
(a.min[2] < b.max[2])
);
};
/**
* Checks if the local world bounds intersect with a point in world space
* @param {Object} pt
* @return {Boolean}
*/
BoundingBox.prototype.intersectsWithPoint = function(pt) {
let a = this.world;
let b = pt;
return (
(a.max[0] > b.x) &&
(a.min[0] < b.x) &&
(a.max[1] > b.y) &&
(a.min[1] < b.y) &&
(a.max[2] > b.z) &&
(a.min[2] < b.z)
);
};
/**
* Checks if local vertices intersect with a ray in world space
* @param {Object} pt
* @return {Boolean}
*/
BoundingBox.prototype.intersectsWithVertex = function(ray) {
let object = this.object;
let indices = object.data.indices;
let vertices = object.data.vertices;
let length = (indices.length / 3) | 0;
let mModel = object.getModelMatrix();
let pos = vec4.fromValues(ray.x, ray.y, ray.z, 1.0);
// convert ray to local space
vec4.transformMat4(pos, pos, mat4.invert(mat4.create(), mModel));
let v0 = vec3.create();
let v1 = vec3.create();
let v2 = vec3.create();
let centroid = vec3.create();
for (let ii = 0; ii < length; ++ii) {
let i0 = indices[ii + 0];
let i1 = indices[ii + 1];
let i2 = indices[ii + 2];
vec3.set(v0, vertices[i0 + 0], vertices[i0 + 1], vertices[i0 + 2]);