|
@@ -6,11 +6,11 @@
|
6
|
6
|
(function() {
|
7
|
7
|
|
8
|
8
|
/**
|
9
|
|
- * Utilities for the Demo scene
|
|
9
|
+ * Utilities for the Demo
|
10
|
10
|
*/
|
11
|
|
-class DemoUtils
|
|
11
|
+class Utils
|
12
|
12
|
{
|
13
|
|
- async loadGLTF(filepath, yAxisIsUp = true)
|
|
13
|
+ static async loadGLTF(filepath, yAxisIsUp = true)
|
14
|
14
|
{
|
15
|
15
|
const loader = new THREE.GLTFLoader();
|
16
|
16
|
const gltf = await loader.loadAsync(filepath);
|
|
@@ -22,7 +22,7 @@ class DemoUtils
|
22
|
22
|
return gltf;
|
23
|
23
|
}
|
24
|
24
|
|
25
|
|
- createAnimationAction(gltf, name = null, loop = THREE.LoopRepeat)
|
|
25
|
+ static createAnimationAction(gltf, name = null, loop = THREE.LoopRepeat)
|
26
|
26
|
{
|
27
|
27
|
const mixer = new THREE.AnimationMixer(gltf.scene);
|
28
|
28
|
const clips = gltf.animations;
|
|
@@ -42,7 +42,7 @@ class DemoUtils
|
42
|
42
|
return action;
|
43
|
43
|
}
|
44
|
44
|
|
45
|
|
- createImagePlane(imagepath)
|
|
45
|
+ static createImagePlane(imagepath)
|
46
|
46
|
{
|
47
|
47
|
const texture = new THREE.TextureLoader().load(imagepath);
|
48
|
48
|
const geometry = new THREE.PlaneGeometry(1, 1);
|
|
@@ -55,13 +55,13 @@ class DemoUtils
|
55
|
55
|
return mesh;
|
56
|
56
|
}
|
57
|
57
|
|
58
|
|
- switchToFrontView(ar)
|
|
58
|
+ static switchToFrontView(ar)
|
59
|
59
|
{
|
60
|
60
|
// top view is the default
|
61
|
61
|
ar.root.rotation.set(-Math.PI / 2, 0, 0);
|
62
|
62
|
}
|
63
|
63
|
|
64
|
|
- referenceImageName(ar)
|
|
64
|
+ static referenceImageName(ar)
|
65
|
65
|
{
|
66
|
66
|
if(ar.frame === null)
|
67
|
67
|
return null;
|
|
@@ -80,9 +80,9 @@ class DemoUtils
|
80
|
80
|
}
|
81
|
81
|
|
82
|
82
|
/**
|
83
|
|
- * Demo scene
|
|
83
|
+ * Augmented Reality Demo
|
84
|
84
|
*/
|
85
|
|
-class DemoScene extends ARScene
|
|
85
|
+class EnchantedDemo extends ARDemo
|
86
|
86
|
{
|
87
|
87
|
/**
|
88
|
88
|
* Constructor
|
|
@@ -91,7 +91,6 @@ class DemoScene extends ARScene
|
91
|
91
|
{
|
92
|
92
|
super();
|
93
|
93
|
|
94
|
|
- this._utils = new DemoUtils();
|
95
|
94
|
this._objects = { };
|
96
|
95
|
}
|
97
|
96
|
|
|
@@ -160,7 +159,7 @@ class DemoScene extends ARScene
|
160
|
159
|
}
|
161
|
160
|
|
162
|
161
|
/**
|
163
|
|
- * Initialize the augmented scene
|
|
162
|
+ * Initialization
|
164
|
163
|
* @param {ARSystem} ar
|
165
|
164
|
* @returns {Promise<void>}
|
166
|
165
|
*/
|
|
@@ -169,7 +168,7 @@ class DemoScene extends ARScene
|
169
|
168
|
// Change the point of view. All virtual objects are descendants of
|
170
|
169
|
// ar.root, a node that is automatically aligned to the physical scene.
|
171
|
170
|
// Adjusting ar.root will adjust all virtual objects.
|
172
|
|
- this._utils.switchToFrontView(ar);
|
|
171
|
+ Utils.switchToFrontView(ar);
|
173
|
172
|
ar.root.position.set(0, -0.5, 0);
|
174
|
173
|
|
175
|
174
|
// initialize objects
|
|
@@ -184,7 +183,7 @@ class DemoScene extends ARScene
|
184
|
183
|
}
|
185
|
184
|
|
186
|
185
|
/**
|
187
|
|
- * Update / animate the augmented scene
|
|
186
|
+ * Animation loop
|
188
|
187
|
* @param {ARSystem} ar
|
189
|
188
|
* @returns {void}
|
190
|
189
|
*/
|
|
@@ -212,13 +211,13 @@ class DemoScene extends ARScene
|
212
|
211
|
|
213
|
212
|
_initMagicCircle(ar)
|
214
|
213
|
{
|
215
|
|
- // load the object
|
216
|
|
- const magicCircle = this._utils.createImagePlane('../assets/magic-circle.png');
|
|
214
|
+ // create a magic circle
|
|
215
|
+ const magicCircle = Utils.createImagePlane('../assets/magic-circle.png');
|
217
|
216
|
magicCircle.material.transparent = true;
|
218
|
217
|
magicCircle.material.opacity = 1;
|
219
|
218
|
magicCircle.scale.set(4, 4, 1);
|
220
|
219
|
|
221
|
|
- // add the object to the scene
|
|
220
|
+ // make it a child of ar.root
|
222
|
221
|
ar.root.add(magicCircle);
|
223
|
222
|
|
224
|
223
|
// save a reference
|
|
@@ -227,7 +226,7 @@ class DemoScene extends ARScene
|
227
|
226
|
|
228
|
227
|
_initText(ar)
|
229
|
228
|
{
|
230
|
|
- const text = this._utils.createImagePlane('../assets/it-works.png');
|
|
229
|
+ const text = Utils.createImagePlane('../assets/it-works.png');
|
231
|
230
|
text.material.transparent = true;
|
232
|
231
|
text.material.opacity = 1;
|
233
|
232
|
text.position.set(0, -0.5, 2);
|
|
@@ -242,15 +241,15 @@ class DemoScene extends ARScene
|
242
|
241
|
async _initMage(ar)
|
243
|
242
|
{
|
244
|
243
|
// load the mage
|
245
|
|
- const gltf = await this._utils.loadGLTF('../assets/mage.glb');
|
|
244
|
+ const gltf = await Utils.loadGLTF('../assets/mage.glb');
|
246
|
245
|
const mage = gltf.scene;
|
247
|
246
|
mage.scale.set(0.7, 0.7, 0.7);
|
248
|
247
|
|
249
|
248
|
// prepare the animation of the mage
|
250
|
|
- const mageAction = this._utils.createAnimationAction(gltf, 'Idle');
|
|
249
|
+ const mageAction = Utils.createAnimationAction(gltf, 'Idle');
|
251
|
250
|
mageAction.play();
|
252
|
251
|
|
253
|
|
- // add the mage to the scene
|
|
252
|
+ // make the mage a child of ar.root
|
254
|
253
|
ar.root.add(mage);
|
255
|
254
|
|
256
|
255
|
// save references
|
|
@@ -260,11 +259,11 @@ class DemoScene extends ARScene
|
260
|
259
|
|
261
|
260
|
async _initCat(ar)
|
262
|
261
|
{
|
263
|
|
- const gltf = await this._utils.loadGLTF('../assets/cat.glb');
|
|
262
|
+ const gltf = await Utils.loadGLTF('../assets/cat.glb');
|
264
|
263
|
const cat = gltf.scene;
|
265
|
264
|
cat.scale.set(0.7, 0.7, 0.7);
|
266
|
265
|
|
267
|
|
- const catAction = this._utils.createAnimationAction(gltf, 'Cheer');
|
|
266
|
+ const catAction = Utils.createAnimationAction(gltf, 'Cheer');
|
268
|
267
|
catAction.play();
|
269
|
268
|
|
270
|
269
|
ar.root.add(cat);
|
|
@@ -299,7 +298,7 @@ class DemoScene extends ARScene
|
299
|
298
|
|
300
|
299
|
_onTargetFound(referenceImage)
|
301
|
300
|
{
|
302
|
|
- // change the scene based on the scanned image
|
|
301
|
+ // change the scene based on the tracked image
|
303
|
302
|
switch(referenceImage.name) {
|
304
|
303
|
case 'mage':
|
305
|
304
|
this._objects.mage.visible = true;
|
|
@@ -323,17 +322,17 @@ class DemoScene extends ARScene
|
323
|
322
|
}
|
324
|
323
|
|
325
|
324
|
/**
|
326
|
|
- * Enchant the scene
|
|
325
|
+ * Start the Demo
|
327
|
326
|
* @returns {void}
|
328
|
327
|
*/
|
329
|
328
|
function main()
|
330
|
329
|
{
|
331
|
|
- const scene = new DemoScene();
|
|
330
|
+ const demo = new EnchantedDemo();
|
332
|
331
|
|
333
|
332
|
if(typeof encantar === 'undefined')
|
334
|
333
|
throw new Error(`Can't find the three.js plugin for encantar.js`);
|
335
|
334
|
|
336
|
|
- encantar(scene).catch(error => {
|
|
335
|
+ encantar(demo).catch(error => {
|
337
|
336
|
alert(error.message);
|
338
|
337
|
});
|
339
|
338
|
}
|