-
Notifications
You must be signed in to change notification settings - Fork 1
/
three.ar.js
2350 lines (1929 loc) · 115 KB
/
three.ar.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
/*
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("three"));
else if(typeof define === 'function' && define.amd)
define(["three"], factory);
else {
var a = typeof exports === 'object' ? factory(require("three")) : factory(root["THREE"]);
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 3);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/* WEBPACK VAR INJECTION */(function(global) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.displayUnsupportedMessage = exports.getRandomPaletteColor = exports.placeObjectAtHit = exports.loadBlocksModel = exports.getARDisplay = exports.isARDisplay = exports.isARKit = exports.isTango = undefined;
var _three = __webpack_require__(0);
var _loaders = __webpack_require__(6);
/*
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var colors = ['#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50', '#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800'].map(function (hex) {
return new _three.Color(hex);
});
var LEARN_MORE_LINK = 'https://developers.google.com/ar/develop/web/getting-started';
var UNSUPPORTED_MESSAGE = 'This augmented reality experience requires\n WebARonARCore or WebARonARKit, experimental browsers from Google\n for Android and iOS. Learn more at the <a href="' + LEARN_MORE_LINK + '">Google Developers site</a>.';
var ARUtils = Object.create(null);
ARUtils.isTango = function (display) {
return display && display.displayName.toLowerCase().includes('tango');
};
var isTango = exports.isTango = ARUtils.isTango;
ARUtils.isARKit = function (display) {
return display && display.displayName.toLowerCase().includes('arkit');
};
var isARKit = exports.isARKit = ARUtils.isARKit;
ARUtils.isARDisplay = function (display) {
return isARKit(display) || isTango(display);
};
var isARDisplay = exports.isARDisplay = ARUtils.isARDisplay;
/**
* Returns a promise that resolves to either to a VRDisplay with
* AR capabilities, or null if no valid AR devices found on the platform.
*
* @return {Promise<VRDisplay?>}
*/
ARUtils.getARDisplay = function () {
return new Promise(function (resolve, reject) {
if (!navigator.getVRDisplays) {
resolve(null);
return;
}
navigator.getVRDisplays().then(function (displays) {
if (!displays && displays.length === 0) {
resolve(null);
return;
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = displays[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var display = _step.value;
if (isARDisplay(display)) {
resolve(display);
return;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
resolve(null);
});
});
};
var getARDisplay = exports.getARDisplay = ARUtils.getARDisplay;
/**
* Takes a path for an OBJ model and optionally a path for an MTL
* texture and returns a promise resolving to a THREE.Mesh loaded with
* the appropriate material. Can be used on downloaded models from Blocks.
*
* NOTE: loading function will remap materials in the .mtl file whose specular,
* diffuse, or ambient contribution is (0, 0, 0) to (1, 1, 1). As well as materials
* whose dissolve is 0 (which becomes an opacity of 0) to 1.
*
* @param {string} objPath
* @param {string} mtlPath
* @return {THREE.Mesh}
*/
ARUtils.loadBlocksModel = function (objPath, mtlPath) {
return new Promise(function (resolve, reject) {
if (!global.THREE || !global.THREE.OBJLoader || !global.THREE.MTLLoader) {
reject(new Error('Must include THREE.OBJLoader and THREE.MTLLoader'));
return;
}
var p = Promise.resolve();
if (mtlPath) {
p = (0, _loaders.loadMtl)(mtlPath);
}
p.then(function (materialCreator) {
if (materialCreator) {
materialCreator.preload();
}
return (0, _loaders.loadObj)(objPath, materialCreator);
}).then(function (obj) {
var model = obj.children[0];
model.geometry.applyMatrix(new _three.Matrix4().makeRotationY(_three.Math.degToRad(-90)));
return model;
}).then(resolve, reject);
});
};
var loadBlocksModel = exports.loadBlocksModel = ARUtils.loadBlocksModel;
var model = new _three.Matrix4();
var tempPos = new _three.Vector3();
var tempQuat = new _three.Quaternion();
var tempScale = new _three.Vector3();
/**
* Takes a THREE.Object3D and a VRHit and positions and optionally orients
* the object according to the transform of the VRHit. Can provide an
* easing value between 0 and 1 corresponding to the lerp between the
* object's current position/orientation, and the position/orientation of the
* hit.
*
* @param {THREE.Object3D} object
* @param {VRHit} hit
* @param {number} easing
* @param {boolean} applyOrientation
*/
ARUtils.placeObjectAtHit = function (object, hit) {
var easing = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var applyOrientation = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
if (!hit || !hit.modelMatrix) {
throw new Error('placeObjectAtHit requires a VRHit object');
}
model.fromArray(hit.modelMatrix);
model.decompose(tempPos, tempQuat, tempScale);
if (easing === 1) {
object.position.copy(tempPos);
if (applyOrientation) {
object.quaternion.copy(tempQuat);
}
} else {
object.position.lerp(tempPos, easing);
if (applyOrientation) {
object.quaternion.slerp(tempQuat, easing);
}
}
};
var placeObjectAtHit = exports.placeObjectAtHit = ARUtils.placeObjectAtHit;
/**
* Returns a random color from the stored palette.
* @return {THREE.Color}
*/
ARUtils.getRandomPaletteColor = function () {
return colors[Math.floor(Math.random() * colors.length)];
};
var getRandomPaletteColor = exports.getRandomPaletteColor = ARUtils.getRandomPaletteColor;
/**
* Injects a DOM element into the current page prompting the user that
* their browser does not support these AR features.
*
* @param {string} customMessage
*/
ARUtils.displayUnsupportedMessage = function (customMessage) {
var element = document.createElement('div');
element.id = 'webgl-error-message';
element.style.fontFamily = 'monospace';
element.style.fontSize = '13px';
element.style.fontWeight = 'normal';
element.style.textAlign = 'center';
element.style.background = '#fff';
element.style.border = '1px solid black';
element.style.color = '#000';
element.style.padding = '1.5em';
element.style.width = '400px';
element.style.margin = '5em auto 0';
element.innerHTML = typeof customMessage === 'string' ? customMessage : UNSUPPORTED_MESSAGE;
document.body.appendChild(element);
};
var displayUnsupportedMessage = exports.displayUnsupportedMessage = ARUtils.displayUnsupportedMessage;
exports.default = ARUtils;
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(2)))
/***/ }),
/* 2 */
/***/ (function(module, exports) {
var g;
// This works in non-strict mode
g = (function() {
return this;
})();
try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
// This works if the window reference is available
if(typeof window === "object")
g = window;
}
// g can still be undefined, but nothing to do about it...
// We return undefined, instead of nothing here, so it's
// easier to handle this case. if(!global) { ...}
module.exports = g;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/* WEBPACK VAR INJECTION */(function(global) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ARView = exports.ARUtils = exports.ARReticle = exports.ARPerspectiveCamera = exports.ARDebug = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /*
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint no-unused-vars: "off" */
var _ARDebug = __webpack_require__(4);
var _ARDebug2 = _interopRequireDefault(_ARDebug);
var _ARPerspectiveCamera = __webpack_require__(9);
var _ARPerspectiveCamera2 = _interopRequireDefault(_ARPerspectiveCamera);
var _ARReticle = __webpack_require__(10);
var _ARReticle2 = _interopRequireDefault(_ARReticle);
var _ARUtils = __webpack_require__(1);
var _ARUtils2 = _interopRequireDefault(_ARUtils);
var _ARView = __webpack_require__(11);
var _ARView2 = _interopRequireDefault(_ARView);
__webpack_require__(15);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// If including three.ar.js as a standalone script tag,
// we'll need to expose these objects directly by attaching
// them on the THREE global
if (_typeof(global.THREE) === 'object') {
global.THREE.ARDebug = _ARDebug2.default;
global.THREE.ARPerspectiveCamera = _ARPerspectiveCamera2.default;
global.THREE.ARReticle = _ARReticle2.default;
global.THREE.ARUtils = _ARUtils2.default;
global.THREE.ARView = _ARView2.default;
}
exports.ARDebug = _ARDebug2.default;
exports.ARPerspectiveCamera = _ARPerspectiveCamera2.default;
exports.ARReticle = _ARReticle2.default;
exports.ARUtils = _ARUtils2.default;
exports.ARView = _ARView2.default;
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(2)))
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var _ARPlanes = __webpack_require__(5);
var _ARPlanes2 = _interopRequireDefault(_ARPlanes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DEFAULTS = {
open: true,
showLastHit: true,
showPoseStatus: true,
showPlanes: false
};
var SUCCESS_COLOR = '#00ff00';
var FAILURE_COLOR = '#ff0077';
var PLANES_POLLING_TIMER = 500;
// A cache to store original native VRDisplay methods
// since WebARonARKit does not provide a VRDisplay.prototype[method],
// and assuming the first time ARDebug proxies a method is the
// 'native' version, this caches the correct method if we proxy a method twice
var cachedVRDisplayMethods = new Map();
/**
* A throttle function to limit number of DOM writes
* in the ARDebug view.
*
* @param {Function} fn
* @param {number} timer
* @param {Object} scope
*
* @return {Function}
*/
function throttle(fn, timer, scope) {
var lastFired = void 0;
var timeout = void 0;
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var current = +new Date();
var until = void 0;
if (lastFired) {
until = lastFired + timer - current;
}
if (until == undefined || until < 0) {
lastFired = current;
fn.apply(scope, args);
} else if (until >= 0) {
clearTimeout(timeout);
timeout = setTimeout(function () {
lastFired = current;
fn.apply(scope, args);
}, until);
}
};
}
/**
* Class for creating a mesh that fires raycasts and lerps
* a 3D object along the surface
*/
var ARDebug = function () {
/**
* @param {VRDisplay} vrDisplay
* @param {THREE.Scene?} scene
* @param {Object} config
* @param {boolean} config.open
* @param {boolean} config.showLastHit
* @param {boolean} config.showPoseStatus
* @param {boolean} config.showPlanes
*/
function ARDebug(vrDisplay, scene, config) {
_classCallCheck(this, ARDebug);
// Make `scene` optional
if (typeof config === 'undefined' && scene && scene.type !== 'Scene') {
config = scene;
scene = null;
}
this.config = Object.assign({}, DEFAULTS, config);
this.vrDisplay = vrDisplay;
this._view = new ARDebugView({ open: this.config.open });
if (this.config.showLastHit && this.vrDisplay.hitTest) {
this._view.addRow('hit-test', new ARDebugHitTestRow(vrDisplay));
}
if (this.config.showPoseStatus && this.vrDisplay.getFrameData) {
this._view.addRow('pose-status', new ARDebugPoseRow(vrDisplay));
}
if (this.config.showPlanes && this.vrDisplay.getPlanes) {
if (!scene) {
console.warn('ARDebug `{ showPlanes: true }` option requires ' + 'passing in a THREE.Scene as the second parameter ' + 'in the constructor.');
} else {
this._view.addRow('show-planes', new ARDebugPlanesRow(vrDisplay, scene));
}
}
}
/**
* Opens the debug panel.
*/
_createClass(ARDebug, [{
key: 'open',
value: function open() {
this._view.open();
}
/**
* Closes the debug panel.
*/
}, {
key: 'close',
value: function close() {
this._view.close();
}
/**
* Returns the root DOM element for the panel.
*
* @return {HTMLElement}
*/
}, {
key: 'getElement',
value: function getElement() {
return this._view.getElement();
}
}]);
return ARDebug;
}();
/**
* An implementation that interfaces with the DOM, used
* by ARDebug
*/
var ARDebugView = function () {
/**
* @param {Object} config
* @param {boolean} config.open
*/
function ARDebugView() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, ARDebugView);
this.rows = new Map();
this.el = document.createElement('div');
this.el.style.backgroundColor = '#333';
this.el.style.padding = '5px';
this.el.style.fontFamily = 'Roboto, Ubuntu, Arial, sans-serif';
this.el.style.color = 'rgb(165, 165, 165)';
this.el.style.position = 'absolute';
this.el.style.right = '20px';
this.el.style.top = '0px';
this.el.style.width = '200px';
this.el.style.fontSize = '12px';
this.el.style.zIndex = 9999;
this._rowsEl = document.createElement('div');
this._rowsEl.style.transitionProperty = 'max-height';
this._rowsEl.style.transitionDuration = '0.5s';
this._rowsEl.style.transitionDelay = '0s';
this._rowsEl.style.transitionTimingFunction = 'ease-out';
this._rowsEl.style.overflow = 'hidden';
this._controls = document.createElement('div');
this._controls.style.fontSize = '13px';
this._controls.style.fontWeight = 'bold';
this._controls.style.paddingTop = '5px';
this._controls.style.textAlign = 'center';
this._controls.style.cursor = 'pointer';
this._controls.addEventListener('click', this.toggleControls.bind(this));
// Initialize the view as open or closed
config.open ? this.open() : this.close();
this.el.appendChild(this._rowsEl);
this.el.appendChild(this._controls);
}
/**
* Toggles between open and close modes.
*/
_createClass(ARDebugView, [{
key: 'toggleControls',
value: function toggleControls() {
if (this._isOpen) {
this.close();
} else {
this.open();
}
}
/**
* Opens the debugging panel.
*/
}, {
key: 'open',
value: function open() {
// Use max-height with large value to transition
// to/from a non-specific height (like auto/100%)
// https://stackoverflow.com/a/8331169
// @TODO investigate a more complete solution with correct timing,
// via something like http://n12v.com/css-transition-to-from-auto/
this._rowsEl.style.maxHeight = '100px';
this._isOpen = true;
this._controls.textContent = 'Close ARDebug';
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this.rows[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _step$value = _slicedToArray(_step.value, 2),
row = _step$value[1];
row.enable();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
/**
* Closes the debugging panel.
*/
}, {
key: 'close',
value: function close() {
this._rowsEl.style.maxHeight = '0px';
this._isOpen = false;
this._controls.textContent = 'Open ARDebug';
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this.rows[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _step2$value = _slicedToArray(_step2.value, 2),
row = _step2$value[1];
row.disable();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
/**
* Returns the ARDebugView root element.
*
* @return {HTMLElement}
*/
}, {
key: 'getElement',
value: function getElement() {
return this.el;
}
/**
* Adds a row to the ARDebugView.
*
* @param {string} id
* @param {ARDebugRow} row
*/
}, {
key: 'addRow',
value: function addRow(id, row) {
this.rows.set(id, row);
if (this._isOpen) {
row.enable();
}
this._rowsEl.appendChild(row.getElement());
}
}]);
return ARDebugView;
}();
/**
* A class that implements features being a row in the ARDebugView.
*/
var ARDebugRow = function () {
/**
* @param {string} title
*/
function ARDebugRow(title) {
_classCallCheck(this, ARDebugRow);
this.el = document.createElement('div');
this.el.style.width = '100%';
this.el.style.borderTop = '1px solid rgb(54, 54, 54)';
this.el.style.borderBottom = '1px solid #14171A';
this.el.style.position = 'relative';
this.el.style.padding = '3px 0px';
this.el.style.overflow = 'hidden';
this._titleEl = document.createElement('span');
this._titleEl.style.fontWeight = 'bold';
this._titleEl.textContent = title;
this._dataEl = document.createElement('span');
this._dataEl.style.position = 'absolute';
this._dataEl.style.left = '40px';
// Create a text element to update so we can avoid
// forced reflows when updating
// https://stackoverflow.com/a/17203046
this._dataElText = document.createTextNode('');
this._dataEl.appendChild(this._dataElText);
this.el.appendChild(this._titleEl);
this.el.appendChild(this._dataEl);
this.update = throttle(this.update, 500, this);
}
/**
* Enables the proxying and inspection functionality of
* this row. Should be implemented by child class.
*/
_createClass(ARDebugRow, [{
key: 'enable',
value: function enable() {
throw new Error('Implement in child class');
}
/**
* Disables the proxying and inspection functionality of
* this row. Should be implemented by child class.
*/
}, {
key: 'disable',
value: function disable() {
throw new Error('Implement in child class');
}
/**
* Returns the ARDebugRow's root element.
*
* @return {HTMLElement}
*/
}, {
key: 'getElement',
value: function getElement() {
return this.el;
}
/**
* Updates the row's value.
*
* @param {string} value
* @param {boolean} isSuccess
*/
}, {
key: 'update',
value: function update(value, isSuccess) {
this._dataElText.nodeValue = value;
this._dataEl.style.color = isSuccess ? SUCCESS_COLOR : FAILURE_COLOR;
}
}]);
return ARDebugRow;
}();
/**
* The ARDebugRow subclass for displaying hit information
* by wrapping `vrDisplay.hitTest` and displaying the results.
*/
var ARDebugHitTestRow = function (_ARDebugRow) {
_inherits(ARDebugHitTestRow, _ARDebugRow);
/**
* @param {VRDisplay} vrDisplay
*/
function ARDebugHitTestRow(vrDisplay) {
_classCallCheck(this, ARDebugHitTestRow);
var _this = _possibleConstructorReturn(this, (ARDebugHitTestRow.__proto__ || Object.getPrototypeOf(ARDebugHitTestRow)).call(this, 'Hit'));
_this.vrDisplay = vrDisplay;
_this._onHitTest = _this._onHitTest.bind(_this);
// Store the native hit test, or proxy the native `hitTest` call with our own
_this._nativeHitTest = cachedVRDisplayMethods.get('hitTest') || _this.vrDisplay.hitTest;
cachedVRDisplayMethods.set('hitTest', _this._nativeHitTest);
_this._didPreviouslyHit = null;
return _this;
}
/**
* Enables the tracking of hit test information.
*/
_createClass(ARDebugHitTestRow, [{
key: 'enable',
value: function enable() {
this.vrDisplay.hitTest = this._onHitTest;
}
/**
* Disables the tracking of hit test information.
*/
}, {
key: 'disable',
value: function disable() {
this.vrDisplay.hitTest = this._nativeHitTest;
}
/**
* @param {VRHit} hit
* @return {string}
*/
}, {
key: '_hitToString',
value: function _hitToString(hit) {
var mm = hit.modelMatrix;
return mm[12].toFixed(2) + ', ' + mm[13].toFixed(2) + ', ' + mm[14].toFixed(2);
}
/**
* @param {number} x
* @param {number} y
* @return {VRHit?}
*/
}, {
key: '_onHitTest',
value: function _onHitTest(x, y) {
var hits = this._nativeHitTest.call(this.vrDisplay, x, y);
var t = (parseInt(performance.now(), 10) / 1000).toFixed(1);
var didHit = hits && hits.length;
this.update((didHit ? this._hitToString(hits[0]) : 'MISS') + ' @ ' + t + 's', didHit);
this._didPreviouslyHit = didHit;
return hits;
}
}]);
return ARDebugHitTestRow;
}(ARDebugRow);
/**
* The ARDebugRow subclass for displaying pose information
* by wrapping `vrDisplay.getFrameData` and displaying the results.
*/
var ARDebugPoseRow = function (_ARDebugRow2) {
_inherits(ARDebugPoseRow, _ARDebugRow2);
/**
* @param {VRDisplay} vrDisplay
*/
function ARDebugPoseRow(vrDisplay) {
_classCallCheck(this, ARDebugPoseRow);
var _this2 = _possibleConstructorReturn(this, (ARDebugPoseRow.__proto__ || Object.getPrototypeOf(ARDebugPoseRow)).call(this, 'Pose'));
_this2.vrDisplay = vrDisplay;
_this2._onGetFrameData = _this2._onGetFrameData.bind(_this2);
// Store the native hit test, or proxy the native `hitTest` call with our own
_this2._nativeGetFrameData = cachedVRDisplayMethods.get('getFrameData') || _this2.vrDisplay.getFrameData;
cachedVRDisplayMethods.set('getFrameData', _this2._nativeGetFrameData);
_this2.update('Looking for position...');
_this2._initialPose = false;
return _this2;
}
/**
* Enables displaying and pulling getFrameData
*/
_createClass(ARDebugPoseRow, [{
key: 'enable',
value: function enable() {
this.vrDisplay.getFrameData = this._onGetFrameData;
}