-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual_logic_scene1.js
2104 lines (1669 loc) · 72 KB
/
visual_logic_scene1.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
/**
* Generated by Verge3D Puzzles v.3.8.1
* Wed Jan 12 2022 18:18:01 GMT+0800 (中国标准时间)
* Prefer not editing this file as your changes may get overridden once Puzzles are saved.
* Check out https://www.soft8soft.com/docs/manual/en/introduction/Using-JavaScript.html
* for the information on how to add your own JavaScript to Verge3D apps.
*/
'use strict';
(function() {
// global variables/constants used by puzzles' functions
var LIST_NONE = '<none>';
var _pGlob = {};
_pGlob.objCache = {};
_pGlob.fadeAnnotations = true;
_pGlob.pickedObject = '';
_pGlob.hoveredObject = '';
_pGlob.mediaElements = {};
_pGlob.loadedFile = '';
_pGlob.states = [];
_pGlob.percentage = 0;
_pGlob.openedFile = '';
_pGlob.xrSessionAcquired = false;
_pGlob.xrSessionCallbacks = [];
_pGlob.screenCoords = new v3d.Vector2();
_pGlob.intervalTimers = {};
_pGlob.AXIS_X = new v3d.Vector3(1, 0, 0);
_pGlob.AXIS_Y = new v3d.Vector3(0, 1, 0);
_pGlob.AXIS_Z = new v3d.Vector3(0, 0, 1);
_pGlob.MIN_DRAG_SCALE = 10e-4;
_pGlob.SET_OBJ_ROT_EPS = 1e-8;
_pGlob.vec2Tmp = new v3d.Vector2();
_pGlob.vec2Tmp2 = new v3d.Vector2();
_pGlob.vec3Tmp = new v3d.Vector3();
_pGlob.vec3Tmp2 = new v3d.Vector3();
_pGlob.vec3Tmp3 = new v3d.Vector3();
_pGlob.vec3Tmp4 = new v3d.Vector3();
_pGlob.eulerTmp = new v3d.Euler();
_pGlob.eulerTmp2 = new v3d.Euler();
_pGlob.quatTmp = new v3d.Quaternion();
_pGlob.quatTmp2 = new v3d.Quaternion();
_pGlob.colorTmp = new v3d.Color();
_pGlob.mat4Tmp = new v3d.Matrix4();
_pGlob.planeTmp = new v3d.Plane();
_pGlob.raycasterTmp = new v3d.Raycaster();
var PL = v3d.PL = v3d.PL || {};
// a more readable alias for PL (stands for "Puzzle Logic")
v3d.puzzles = PL;
PL.procedures = PL.procedures || {};
PL.execInitPuzzles = function(options) {
// always null, should not be available in "init" puzzles
var appInstance = null;
// app is more conventional than appInstance (used in exec script and app templates)
var app = null;
var _initGlob = {};
_initGlob.percentage = 0;
_initGlob.output = {
initOptions: {
fadeAnnotations: true,
useBkgTransp: false,
preserveDrawBuf: false,
useCompAssets: false,
useFullscreen: true,
useCustomPreloader: false,
preloaderStartCb: function() {},
preloaderProgressCb: function() {},
preloaderEndCb: function() {},
}
}
// provide the container's id to puzzles that need access to the container
_initGlob.container = options !== undefined && 'container' in options
? options.container : "";
var PROC = {
};
// utility functions envoked by the HTML puzzles
function getElements(ids, isParent) {
var elems = [];
if (Array.isArray(ids) && ids[0] != 'CONTAINER' && ids[0] != 'WINDOW' &&
ids[0] != 'DOCUMENT' && ids[0] != 'BODY' && ids[0] != 'QUERYSELECTOR') {
for (var i = 0; i < ids.length; i++)
elems.push(getElement(ids[i], isParent));
} else {
elems.push(getElement(ids, isParent));
}
return elems;
}
function getElement(id, isParent) {
var elem;
if (Array.isArray(id) && id[0] == 'CONTAINER') {
if (appInstance !== null) {
elem = appInstance.container;
} else if (typeof _initGlob !== 'undefined') {
// if we are on the initialization stage, we still can have access
// to the container element
var id = _initGlob.container;
if (isParent) {
elem = parent.document.getElementById(id);
} else {
elem = document.getElementById(id);
}
}
} else if (Array.isArray(id) && id[0] == 'WINDOW') {
if (isParent)
elem = parent;
else
elem = window;
} else if (Array.isArray(id) && id[0] == 'DOCUMENT') {
if (isParent)
elem = parent.document;
else
elem = document;
} else if (Array.isArray(id) && id[0] == 'BODY') {
if (isParent)
elem = parent.document.body;
else
elem = document.body;
} else if (Array.isArray(id) && id[0] == 'QUERYSELECTOR') {
if (isParent)
elem = parent.document.querySelector(id);
else
elem = document.querySelector(id);
} else {
if (isParent)
elem = parent.document.getElementById(id);
else
elem = document.getElementById(id);
}
return elem;
}
// setHTMLElemStyle puzzle
function setHTMLElemStyle(prop, value, ids, isParent) {
var elems = getElements(ids, isParent);
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
if (!elem || !elem.style)
continue;
elem.style[prop] = value;
}
}
// setHTMLElemAttribute puzzle
function setHTMLElemAttribute(attr, value, ids, isParent) {
var elems = getElements(ids, isParent);
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
if (!elem) continue;
if (attr === 'style') {
// NOTE: setting an attribute 'style' instead of a property 'style'
// fixes IE11 wrong behavior
elem.setAttribute(attr, value);
} else if ((attr == 'href' || attr == 'src') && value instanceof Promise) {
// resolve promise value for url-based attributes
value.then(function(response) {
elem[attr] = response;
});
} else {
elem[attr] = value;
}
}
}
// setTimeout puzzle
function registerSetTimeout(timeout, callback) {
window.setTimeout(callback, 1000 * timeout);
}
// initSettings puzzle
_initGlob.output.initOptions.fadeAnnotations = false;
_initGlob.output.initOptions.useBkgTransp = true;
_initGlob.output.initOptions.preserveDrawBuf = false;
_initGlob.output.initOptions.useCompAssets = false;
_initGlob.output.initOptions.useFullscreen = false;
// initPreloader puzzle
_initGlob.output.initOptions.useCustomPreloader = true;
_initGlob.output.initOptions.preloaderStartCb = function() {
_initGlob.percentage = 0;
(function() {
setHTMLElemStyle('animation', 'show 0.2s 0s linear 1', 'simple-preloader-background', false);
setHTMLElemStyle('animationFillMode', 'forwards', 'simple-preloader-background', false);
})();
};
_initGlob.output.initOptions.preloaderProgressCb = function(percentage) {
_initGlob.percentage = percentage;
(function() {
setHTMLElemAttribute('innerHTML', String(Math.round(_initGlob.percentage)) + '%', 'preloader-text', false);
setHTMLElemStyle('width', String(Math.round(_initGlob.percentage)) + '%', 'simple-preloader-line', false);
})();
};
_initGlob.output.initOptions.preloaderEndCb = function() {
_initGlob.percentage = 100;
(function() {
setHTMLElemStyle('animation', 'hide 0.5s 0s linear 1 ', 'simple-preloader-background', false);
setHTMLElemStyle('animationFillMode', 'forwards', 'simple-preloader-background', false);
registerSetTimeout(0.5, function() {
setHTMLElemStyle('display', 'none', 'simple-preloader-background', false);
});
})();
};
return _initGlob.output;
}
PL.init = function(appInstance, initOptions) {
// app is more conventional than appInstance (used in exec script and app templates)
var app = appInstance;
initOptions = initOptions || {};
if ('fadeAnnotations' in initOptions) {
_pGlob.fadeAnnotations = initOptions.fadeAnnotations;
}
this.procedures["ReturnCamMatrix"] = ReturnCamMatrix;
this.procedures["SetCamQuaternion"] = SetCamQuaternion;
this.procedures["SetCameraRotation"] = SetCameraRotation;
this.procedures["GetCameraRotation"] = GetCameraRotation;
this.procedures["SetCameraPosition"] = SetCameraPosition;
this.procedures["GetCameraPosition"] = GetCameraPosition;
this.procedures["SetTargetPosition"] = SetTargetPosition;
this.procedures["GetTargetPosition"] = GetTargetPosition;
this.procedures["SetEviorColor"] = SetEviorColor;
this.procedures["HideObjectsInCollection"] = HideObjectsInCollection;
this.procedures["HideObjects"] = HideObjects;
this.procedures["resetCamera"] = resetCamera;
this.procedures["ShowObjectsInCollection"] = ShowObjectsInCollection;
this.procedures["ShowObjects"] = ShowObjects;
this.procedures["HideTemperatureFilter"] = HideTemperatureFilter;
this.procedures["SetParticleT1"] = SetParticleT1;
this.procedures["GetObjectByCollectionName"] = GetObjectByCollectionName;
this.procedures["ShowTemperatureFilter"] = ShowTemperatureFilter;
this.procedures["SetTemperature"] = SetTemperature;
this.procedures["SetParticleT2"] = SetParticleT2;
this.procedures["SetAirFlowSpeed"] = SetAirFlowSpeed;
this.procedures["Ani_ShowAirFlow"] = Ani_ShowAirFlow;
this.procedures["animateFlowH2O"] = animateFlowH2O;
this.procedures["animateFlowPM25"] = animateFlowPM25;
this.procedures["Ani_HideAirFlow"] = Ani_HideAirFlow;
this.procedures["animateCollection"] = animateCollection;
this.procedures["animateFlowCO2"] = animateFlowCO2;
this.procedures["Ani_ShowH2OV0"] = Ani_ShowH2OV0;
this.procedures["Ani_ShowCO2V0"] = Ani_ShowCO2V0;
this.procedures["Ani_ShowPM25V0"] = Ani_ShowPM25V0;
this.procedures["Ani_HideH2OV0"] = Ani_HideH2OV0;
this.procedures["Ani_HideCO2V0"] = Ani_HideCO2V0;
this.procedures["Ani_HidePM25V0"] = Ani_HidePM25V0;
this.procedures["Ani_ShowH2OV3"] = Ani_ShowH2OV3;
this.procedures["Ani_ShowCO2V3"] = Ani_ShowCO2V3;
this.procedures["Ani_ShowPM25V3"] = Ani_ShowPM25V3;
this.procedures["Ani_HideH2OV3"] = Ani_HideH2OV3;
this.procedures["Ani_HideCO2V3"] = Ani_HideCO2V3;
this.procedures["Ani_HidePM25V3"] = Ani_HidePM25V3;
var PROC = {
"ReturnCamMatrix": ReturnCamMatrix,
"SetCamQuaternion": SetCamQuaternion,
"SetCameraRotation": SetCameraRotation,
"GetCameraRotation": GetCameraRotation,
"SetCameraPosition": SetCameraPosition,
"GetCameraPosition": GetCameraPosition,
"SetTargetPosition": SetTargetPosition,
"GetTargetPosition": GetTargetPosition,
"SetEviorColor": SetEviorColor,
"HideObjectsInCollection": HideObjectsInCollection,
"HideObjects": HideObjects,
"resetCamera": resetCamera,
"ShowObjectsInCollection": ShowObjectsInCollection,
"ShowObjects": ShowObjects,
"HideTemperatureFilter": HideTemperatureFilter,
"SetParticleT1": SetParticleT1,
"GetObjectByCollectionName": GetObjectByCollectionName,
"ShowTemperatureFilter": ShowTemperatureFilter,
"SetTemperature": SetTemperature,
"SetParticleT2": SetParticleT2,
"SetAirFlowSpeed": SetAirFlowSpeed,
"Ani_ShowAirFlow": Ani_ShowAirFlow,
"animateFlowH2O": animateFlowH2O,
"animateFlowPM25": animateFlowPM25,
"Ani_HideAirFlow": Ani_HideAirFlow,
"animateCollection": animateCollection,
"animateFlowCO2": animateFlowCO2,
"Ani_ShowH2OV0": Ani_ShowH2OV0,
"Ani_ShowCO2V0": Ani_ShowCO2V0,
"Ani_ShowPM25V0": Ani_ShowPM25V0,
"Ani_HideH2OV0": Ani_HideH2OV0,
"Ani_HideCO2V0": Ani_HideCO2V0,
"Ani_HidePM25V0": Ani_HidePM25V0,
"Ani_ShowH2OV3": Ani_ShowH2OV3,
"Ani_ShowCO2V3": Ani_ShowCO2V3,
"Ani_ShowPM25V3": Ani_ShowPM25V3,
"Ani_HideH2OV3": Ani_HideH2OV3,
"Ani_HideCO2V3": Ani_HideCO2V3,
"Ani_HidePM25V3": Ani_HidePM25V3,
};
var Matrix, CameraInfos, FloatColor, CollectionName, ObjectList, CO2Speed, ToValue, LastValue, AirFlowSpeed, HOSpeed, PMSpeed, COSpeed, CamMtrix, H2OSpeed, PM25Speed;
// utility function envoked by almost all V3D-specific puzzles
// filter off some non-mesh types
function notIgnoredObj(obj) {
return obj.type !== 'AmbientLight' &&
obj.name !== '' &&
!(obj.isMesh && obj.isMaterialGeneratedMesh) &&
!obj.isAuxClippingMesh;
}
// utility function envoked by almost all V3D-specific puzzles
// find first occurence of the object by its name
function getObjectByName(objName) {
var objFound;
var runTime = _pGlob !== undefined;
objFound = runTime ? _pGlob.objCache[objName] : null;
if (objFound && objFound.name === objName)
return objFound;
appInstance.scene.traverse(function(obj) {
if (!objFound && notIgnoredObj(obj) && (obj.name == objName)) {
objFound = obj;
if (runTime) {
_pGlob.objCache[objName] = objFound;
}
}
});
return objFound;
}
// utility function envoked by almost all V3D-specific puzzles
// retrieve all objects on the scene
function getAllObjectNames() {
var objNameList = [];
appInstance.scene.traverse(function(obj) {
if (notIgnoredObj(obj))
objNameList.push(obj.name)
});
return objNameList;
}
// utility function envoked by almost all V3D-specific puzzles
// retrieve all objects which belong to the group
function getObjectNamesByGroupName(targetGroupName) {
var objNameList = [];
appInstance.scene.traverse(function(obj){
if (notIgnoredObj(obj)) {
var groupNames = obj.groupNames;
if (!groupNames)
return;
for (var i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i];
if (groupName == targetGroupName) {
objNameList.push(obj.name);
}
}
}
});
return objNameList;
}
// utility function envoked by almost all V3D-specific puzzles
// process object input, which can be either single obj or array of objects, or a group
function retrieveObjectNames(objNames) {
var acc = [];
retrieveObjectNamesAcc(objNames, acc);
return acc.filter(function(name) {
return name;
});
}
function retrieveObjectNamesAcc(currObjNames, acc) {
if (typeof currObjNames == "string") {
acc.push(currObjNames);
} else if (Array.isArray(currObjNames) && currObjNames[0] == "GROUP") {
var newObj = getObjectNamesByGroupName(currObjNames[1]);
for (var i = 0; i < newObj.length; i++)
acc.push(newObj[i]);
} else if (Array.isArray(currObjNames) && currObjNames[0] == "ALL_OBJECTS") {
var newObj = getAllObjectNames();
for (var i = 0; i < newObj.length; i++)
acc.push(newObj[i]);
} else if (Array.isArray(currObjNames)) {
for (var i = 0; i < currObjNames.length; i++)
retrieveObjectNamesAcc(currObjNames[i], acc);
}
}
// show and hide puzzles
function changeVis(objSelector, bool) {
var objNames = retrieveObjectNames(objSelector);
for (var i = 0; i < objNames.length; i++) {
var objName = objNames[i]
if (!objName)
continue;
var obj = getObjectByName(objName);
if (!obj)
continue;
obj.visible = bool;
}
}
// Describe this function...
function ReturnCamMatrix() {
var VARS = Object.defineProperties({}, {
"Matrix": { get: function() { return Matrix; }, set: function(val) { Matrix = val; } },
"CameraInfos": { get: function() { return CameraInfos; }, set: function(val) { CameraInfos = val; } },
"FloatColor": { get: function() { return FloatColor; }, set: function(val) { FloatColor = val; } },
"CollectionName": { get: function() { return CollectionName; }, set: function(val) { CollectionName = val; } },
"ObjectList": { get: function() { return ObjectList; }, set: function(val) { ObjectList = val; } },
"HOSpeed": { get: function() { return HOSpeed; }, set: function(val) { HOSpeed = val; } },
"ToValue": { get: function() { return ToValue; }, set: function(val) { ToValue = val; } },
"LastValue": { get: function() { return LastValue; }, set: function(val) { LastValue = val; } },
"AirFlowSpeed": { get: function() { return AirFlowSpeed; }, set: function(val) { AirFlowSpeed = val; } },
"PMSpeed": { get: function() { return PMSpeed; }, set: function(val) { PMSpeed = val; } },
"COSpeed": { get: function() { return COSpeed; }, set: function(val) { COSpeed = val; } },
"CamMtrix": { get: function() { return CamMtrix; }, set: function(val) { CamMtrix = val; } },
"CO2Speed": { get: function() { return CO2Speed; }, set: function(val) { CO2Speed = val; } },
"H2OSpeed": { get: function() { return H2OSpeed; }, set: function(val) { H2OSpeed = val; } },
"PM25Speed": { get: function() { return PM25Speed; }, set: function(val) { PM25Speed = val; } },
});
Function('app', 'v3d', 'VARS', 'PROC', (('// Built-in variables: app, v3d, VARS, PROC' + '\n' +
'let cam=app.scene.getObjectByName(\'MasterCamera\')' + '\n' +
'// console.log(cam)' + '\n' +
'let qu = new THREE.Quaternion();' + '\n' +
'cam.getWorldQuaternion(qu)' + '\n' +
'VARS[\'CamMtrix\']=qu')))(appInstance, v3d, VARS, PROC);
return CamMtrix;
}
// Describe this function...
function SetCamQuaternion(Matrix) {
CamMtrix = Matrix;
var VARS = Object.defineProperties({}, {
"Matrix": { get: function() { return Matrix; }, set: function(val) { Matrix = val; } },
"CameraInfos": { get: function() { return CameraInfos; }, set: function(val) { CameraInfos = val; } },
"FloatColor": { get: function() { return FloatColor; }, set: function(val) { FloatColor = val; } },
"CollectionName": { get: function() { return CollectionName; }, set: function(val) { CollectionName = val; } },
"ObjectList": { get: function() { return ObjectList; }, set: function(val) { ObjectList = val; } },
"HOSpeed": { get: function() { return HOSpeed; }, set: function(val) { HOSpeed = val; } },
"ToValue": { get: function() { return ToValue; }, set: function(val) { ToValue = val; } },
"LastValue": { get: function() { return LastValue; }, set: function(val) { LastValue = val; } },
"AirFlowSpeed": { get: function() { return AirFlowSpeed; }, set: function(val) { AirFlowSpeed = val; } },
"PMSpeed": { get: function() { return PMSpeed; }, set: function(val) { PMSpeed = val; } },
"COSpeed": { get: function() { return COSpeed; }, set: function(val) { COSpeed = val; } },
"CamMtrix": { get: function() { return CamMtrix; }, set: function(val) { CamMtrix = val; } },
"CO2Speed": { get: function() { return CO2Speed; }, set: function(val) { CO2Speed = val; } },
"H2OSpeed": { get: function() { return H2OSpeed; }, set: function(val) { H2OSpeed = val; } },
"PM25Speed": { get: function() { return PM25Speed; }, set: function(val) { PM25Speed = val; } },
});
Function('app', 'v3d', 'VARS', 'PROC', (('// Built-in variables: app, v3d, VARS, PROC' + '\n' +
'let cam=app.scene.getObjectByName(\'MasterCamera\')' + '\n' +
'let tar=app.scene.getObjectByName(\'CameraTarget\')' + '\n' +
'// console.log(cam)' + '\n' +
'cam.matrixAutoUpdate = false;' + '\n' +
'' + '\n' +
'let qu = new THREE.Quaternion();' + '\n' +
'qu.x=VARS[\'CamMtrix\']._x' + '\n' +
'qu.y=VARS[\'CamMtrix\']._y' + '\n' +
'qu.z=VARS[\'CamMtrix\']._z' + '\n' +
'qu.w=VARS[\'CamMtrix\']._w' + '\n' +
'' + '\n' +
'console.log(JSON.stringify(qu))' + '\n' +
'cam.setRotationFromQuaternion(qu)' + '\n' +
'cam.updateMatrix();' + '\n' +
'cam.matrixAutoUpdate = true;' + '\n' +
'cam.lookAt(tar.position[0],tar.position[1],tar.position[2])' + '\n' +
'')))(appInstance, v3d, VARS, PROC);
}
/**
* Retrieve coordinate system from the loaded scene
*/
function getCoordSystem() {
var scene = appInstance.scene;
if (scene && "v3d" in scene.userData && "coordSystem" in scene.userData.v3d) {
return scene.userData.v3d.coordSystem;
} else {
// COMPAT: <2.17, consider replacing to 'Y_UP_RIGHT' for scenes with unknown origin
return 'Z_UP_RIGHT';
}
}
/**
* Transform coordinates from one space to another
* Can be used with Vector3 or Euler.
*/
function coordsTransform(coords, from, to, noSignChange) {
if (from == to)
return coords;
var y = coords.y, z = coords.z;
if (from == 'Z_UP_RIGHT' && to == 'Y_UP_RIGHT') {
coords.y = z;
coords.z = noSignChange ? y : -y;
} else if (from == 'Y_UP_RIGHT' && to == 'Z_UP_RIGHT') {
coords.y = noSignChange ? z : -z;
coords.z = y;
} else {
console.error('coordsTransform: Unsupported coordinate space');
}
return coords;
}
/**
* Verge3D euler rotation to Blender/Max shortest.
* 1) Convert from intrinsic rotation (v3d) to extrinsic XYZ (Blender/Max default
* order) via reversion: XYZ -> ZYX
* 2) swizzle ZYX->YZX
* 3) choose the shortest rotation to resemble Blender's behavior
*/
var eulerV3DToBlenderShortest = function() {
var eulerTmp = new v3d.Euler();
var eulerTmp2 = new v3d.Euler();
var vec3Tmp = new v3d.Vector3();
return function(euler, dest) {
var eulerBlender = eulerTmp.copy(euler).reorder('YZX');
var eulerBlenderAlt = eulerTmp2.copy(eulerBlender).makeAlternative();
var len = eulerBlender.toVector3(vec3Tmp).lengthSq();
var lenAlt = eulerBlenderAlt.toVector3(vec3Tmp).lengthSq();
dest.copy(len < lenAlt ? eulerBlender : eulerBlenderAlt);
return coordsTransform(dest, 'Y_UP_RIGHT', 'Z_UP_RIGHT');
}
}();
function RotationInterface() {
/**
* For user manipulations use XYZ extrinsic rotations (which
* are the same as ZYX intrinsic rotations)
* - Blender/Max/Maya use extrinsic rotations in the UI
* - XYZ is the default option, but could be set from
* some order hint if exported
*/
this._userRotation = new v3d.Euler(0, 0, 0, 'ZYX');
this._actualRotation = new v3d.Euler();
}
Object.assign(RotationInterface, {
initObject: function(obj) {
if (obj.userData.v3d.puzzles === undefined) {
obj.userData.v3d.puzzles = {}
}
if (obj.userData.v3d.puzzles.rotationInterface === undefined) {
obj.userData.v3d.puzzles.rotationInterface = new RotationInterface();
}
var rotUI = obj.userData.v3d.puzzles.rotationInterface;
rotUI.updateFromObject(obj);
return rotUI;
}
});
Object.assign(RotationInterface.prototype, {
updateFromObject: function(obj) {
var SYNC_ROT_EPS = 1e-8;
if (!this._actualRotation.equalsEps(obj.rotation, SYNC_ROT_EPS)) {
this._actualRotation.copy(obj.rotation);
this._updateUserRotFromActualRot();
}
},
getActualRotation: function(euler) {
return euler.copy(this._actualRotation);
},
setUserRotation: function(euler) {
// don't copy the order, since it's fixed to ZYX for now
this._userRotation.set(euler.x, euler.y, euler.z);
this._updateActualRotFromUserRot();
},
getUserRotation: function(euler) {
return euler.copy(this._userRotation);
},
_updateUserRotFromActualRot: function() {
var order = this._userRotation.order;
this._userRotation.copy(this._actualRotation).reorder(order);
},
_updateActualRotFromUserRot: function() {
var order = this._actualRotation.order;
this._actualRotation.copy(this._userRotation).reorder(order);
}
});
// setObjTransform puzzle
function setObjTransform(objSelector, isWorldSpace, mode, vector, offset){
var x = vector[0];
var y = vector[1];
var z = vector[2];
var objNames = retrieveObjectNames(objSelector);
function setObjProp(obj, prop, val) {
if (!offset) {
obj[mode][prop] = val;
} else {
if (mode != "scale")
obj[mode][prop] += val;
else
obj[mode][prop] *= val;
}
}
var inputsUsed = _pGlob.vec3Tmp.set(Number(x !== ''), Number(y !== ''),
Number(z !== ''));
var coords = _pGlob.vec3Tmp2.set(x || 0, y || 0, z || 0);
if (mode === 'rotation') {
// rotations are specified in degrees
coords.multiplyScalar(v3d.MathUtils.DEG2RAD);
}
var coordSystem = getCoordSystem();
coordsTransform(inputsUsed, coordSystem, 'Y_UP_RIGHT', true);
coordsTransform(coords, coordSystem, 'Y_UP_RIGHT', mode === 'scale');
for (var i = 0; i < objNames.length; i++) {
var objName = objNames[i];
if (!objName) continue;
var obj = getObjectByName(objName);
if (!obj) continue;
if (isWorldSpace && obj.parent) {
obj.matrixWorld.decomposeE(obj.position, obj.rotation, obj.scale);
if (inputsUsed.x) setObjProp(obj, "x", coords.x);
if (inputsUsed.y) setObjProp(obj, "y", coords.y);
if (inputsUsed.z) setObjProp(obj, "z", coords.z);
obj.matrixWorld.composeE(obj.position, obj.rotation, obj.scale);
obj.matrix.multiplyMatrices(_pGlob.mat4Tmp.copy(obj.parent.matrixWorld).invert(), obj.matrixWorld);
obj.matrix.decompose(obj.position, obj.quaternion, obj.scale);
} else if (mode === 'rotation' && coordSystem == 'Z_UP_RIGHT') {
// Blender/Max coordinates
// need all the rotations for order conversions, especially if some
// inputs are not specified
var euler = eulerV3DToBlenderShortest(obj.rotation, _pGlob.eulerTmp);
coordsTransform(euler, coordSystem, 'Y_UP_RIGHT');
if (inputsUsed.x) euler.x = offset ? euler.x + coords.x : coords.x;
if (inputsUsed.y) euler.y = offset ? euler.y + coords.y : coords.y;
if (inputsUsed.z) euler.z = offset ? euler.z + coords.z : coords.z;
/**
* convert from Blender/Max default XYZ extrinsic order to v3d XYZ
* intrinsic with reversion (XYZ -> ZYX) and axes swizzling (ZYX -> YZX)
*/
euler.order = "YZX";
euler.reorder(obj.rotation.order);
obj.rotation.copy(euler);
} else if (mode === 'rotation' && coordSystem == 'Y_UP_RIGHT') {
// Maya coordinates
// Use separate rotation interface to fix ambiguous rotations for Maya,
// might as well do the same for Blender/Max.
var rotUI = RotationInterface.initObject(obj);
var euler = rotUI.getUserRotation(_pGlob.eulerTmp);
// TODO(ivan): this probably needs some reasonable wrapping
if (inputsUsed.x) euler.x = offset ? euler.x + coords.x : coords.x;
if (inputsUsed.y) euler.y = offset ? euler.y + coords.y : coords.y;
if (inputsUsed.z) euler.z = offset ? euler.z + coords.z : coords.z;
rotUI.setUserRotation(euler);
rotUI.getActualRotation(obj.rotation);
} else {
if (inputsUsed.x) setObjProp(obj, "x", coords.x);
if (inputsUsed.y) setObjProp(obj, "y", coords.y);
if (inputsUsed.z) setObjProp(obj, "z", coords.z);
}
obj.updateMatrixWorld(true);
}
}
// getObjTransform puzzle
function getObjTransform(objName, isWorldSpace, mode, coord) {
if (!objName)
return;
var obj = getObjectByName(objName);
if (!obj)
return;
var coordSystem = getCoordSystem();
var transformVal;
if (isWorldSpace && obj.parent) {
if (mode === 'position') {
transformVal = coordsTransform(obj.getWorldPosition(_pGlob.vec3Tmp), 'Y_UP_RIGHT',
coordSystem, mode === 'scale');
} else if (mode === 'rotation') {
transformVal = coordsTransform(obj.getWorldEuler(_pGlob.eulerTmp, 'XYZ'), 'Y_UP_RIGHT',
coordSystem, mode === 'scale');
} else if (mode === 'scale') {
transformVal = coordsTransform(obj.getWorldScale(_pGlob.vec3Tmp), 'Y_UP_RIGHT',
coordSystem, mode === 'scale');
}
} else if (mode === 'rotation' && coordSystem == 'Z_UP_RIGHT') {
transformVal = eulerV3DToBlenderShortest(obj.rotation,
_pGlob.eulerTmp);
} else if (mode === 'rotation' && coordSystem == 'Y_UP_RIGHT') {
// Maya coordinates
// Use separate rotation interface to fix ambiguous rotations for Maya,
// might as well do the same for Blender/Max.
var rotUI = RotationInterface.initObject(obj);
transformVal = rotUI.getUserRotation(_pGlob.eulerTmp);
} else {
transformVal = coordsTransform(obj[mode].clone(), 'Y_UP_RIGHT',
coordSystem, mode === 'scale');
}
if (mode === 'rotation') {
transformVal.x = v3d.MathUtils.radToDeg(transformVal.x);
transformVal.y = v3d.MathUtils.radToDeg(transformVal.y);
transformVal.z = v3d.MathUtils.radToDeg(transformVal.z);
}
if (coord == 'xyz') {
// remove order component for Euler vectors
return transformVal.toArray().slice(0, 3);
} else {
return transformVal[coord];
}
}
// Describe this function...
function SetCameraRotation(CameraInfos) {
console.log(CameraInfos);
setObjTransform('MasterCamera', false, 'rotation', [CameraInfos[0], CameraInfos[1], CameraInfos[2]], false);
console.log(getObjTransform('MasterCamera', false, 'rotation', 'xyz'));
}
// Describe this function...
function GetCameraRotation() {
return getObjTransform('MasterCamera', true, 'rotation', 'xyz');
}
// Describe this function...
function SetCameraPosition(CameraInfos) {
setObjTransform('MasterCamera', true, 'position', [CameraInfos[0], CameraInfos[1], CameraInfos[2]], false);
}
// Describe this function...
function GetCameraPosition() {
return getObjTransform('MasterCamera', false, 'position', 'xyz');
}
// tweenCamera puzzle
function tweenCamera(posOrObj, targetOrObj, duration, doSlot, movementType) {
var camera = appInstance.getCamera();
if (Array.isArray(posOrObj)) {
var worldPos = _pGlob.vec3Tmp.fromArray(posOrObj);
worldPos = coordsTransform(worldPos, getCoordSystem(), 'Y_UP_RIGHT');
} else if (posOrObj) {
var posObj = getObjectByName(posOrObj);
if (!posObj) return;
var worldPos = posObj.getWorldPosition(_pGlob.vec3Tmp);
} else {
// empty input means: don't change the position
var worldPos = camera.getWorldPosition(_pGlob.vec3Tmp);
}
if (Array.isArray(targetOrObj)) {
var worldTarget = _pGlob.vec3Tmp2.fromArray(targetOrObj);
worldTarget = coordsTransform(worldTarget, getCoordSystem(), 'Y_UP_RIGHT');
} else {
var targObj = getObjectByName(targetOrObj);
if (!targObj) return;
var worldTarget = targObj.getWorldPosition(_pGlob.vec3Tmp2);
}
duration = Math.max(0, duration);
if (appInstance.controls && appInstance.controls.tween) {
// orbit and flying cameras
if (!appInstance.controls.inTween) {
appInstance.controls.tween(worldPos, worldTarget, duration, doSlot,
movementType);
}
} else {
// TODO: static camera, just position it for now
if (camera.parent) {
camera.parent.worldToLocal(worldPos);
}
camera.position.copy(worldPos);
camera.lookAt(worldTarget);
doSlot();
}
}
// Describe this function...
function SetTargetPosition(CameraInfos) {
setObjTransform('AAAAAAA', true, 'position', [CameraInfos[0], CameraInfos[1], CameraInfos[2]], false);
tweenCamera('', 'AAAAAAA', 1, function() {}, 0);
}
// Describe this function...
function GetTargetPosition() {
setObjTransform('AAAAAAA', true, 'position', [getObjTransform('CameraTarget', true, 'position', 'x'), getObjTransform('CameraTarget', true, 'position', 'y'), getObjTransform('CameraTarget', true, 'position', 'z')], false);
return getObjTransform('AAAAAAA', true, 'position', 'xyz');
}
// featureAvailable puzzle
function featureAvailable(feature) {
var userAgent = window.navigator.userAgent;
var platform = window.navigator.platform;
switch (feature) {
case 'LINUX':
return /Linux/.test(platform);
case 'WINDOWS':
return ['Win32', 'Win64', 'Windows', 'WinCE'].indexOf(platform) !== -1;
case 'MACOS':
return (['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'].indexOf(platform) !== -1 && !v3d.Detector.checkIOS());
case 'IOS':
return v3d.Detector.checkIOS();
case 'ANDROID':
return /Android/i.test(userAgent);
case 'MOBILE':
return (/Android|webOS|BlackBerry/i.test(userAgent) || v3d.Detector.checkIOS());
case 'CHROME':
// Chromium based
return (!!window.chrome && !/Edge/.test(navigator.userAgent));
case 'FIREFOX':
return /Firefox/.test(navigator.userAgent);
case 'IE':
return /Trident/.test(navigator.userAgent);
case 'EDGE':
return /Edge/.test(navigator.userAgent);
case 'SAFARI':
return (/Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent));
case 'TOUCH':
return !!(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
case 'RETINA':
return window.devicePixelRatio >= 2;
case 'HDR':
return appInstance.useHDR;
case 'WEBAUDIO':
return v3d.Detector.checkWebAudio();
case 'WEBGL2':
var canvas = document.createElement('canvas');
var gl = canvas.getContext('webgl2')
return !!gl;
case 'WOOCOMMERCE':
var woo_fun = window.parent.v3d_woo_get_product_info || window.parent.parent.v3d_woo_get_product_info;
return !!woo_fun;
case 'DO_NOT_TRACK':
if (navigator.doNotTrack == '1' || window.doNotTrack == '1')
return true;
else
return false;
default:
return false;
}
}
function setScreenScale(factor) {
// already have maximum pixel ratio in HiDPI mode
if (!appInstance.useHiDPIRenderPass)
appInstance.renderer.setPixelRatio(factor);
if (appInstance.postprocessing)
appInstance.postprocessing.composer.setPixelRatio(factor);
// to update possible post-processing passes
appInstance.onResize();
}
// getVectorValue puzzle
function getVectorValue(vector, value) {
var vector = _pGlob.vec3Tmp.fromArray(vector);
switch (value) {
case 'X':
return vector.x;
case 'Y':
return vector.y;
case 'Z':
return vector.z;
case 'IS_ZERO':
return Boolean(vector.length() <= Number.EPSILON);
case 'LENGTH':
return vector.length();
case 'NEGATED':
return [-vector.x, -vector.y, -vector.z];
case 'NORMALIZED':
return vector.normalize().toArray();
default:
console.error('get value from vector: Wrong value');
return;
}
};
// createVector puzzle
function createVector(x, y, z) {