Browse Source

Update three.js plugin and demo

customisations
alemart 11 months ago
parent
commit
c5344f9d68
2 changed files with 40 additions and 40 deletions
  1. 25
    26
      demos/hello-three/demo.js
  2. 15
    14
      plugins/three-with-encantar.js

+ 25
- 26
demos/hello-three/demo.js View File

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

+ 15
- 14
plugins/three-with-encantar.js View File

11
 });
11
 });
12
 
12
 
13
 /**
13
 /**
14
- * Base class for augmented scenes
14
+ * Base class for Augmented Reality experiences
15
  */
15
  */
16
-class ARScene
16
+class ARDemo
17
 {
17
 {
18
     /**
18
     /**
19
      * Start the AR session
19
      * Start the AR session
26
     }
26
     }
27
 
27
 
28
     /**
28
     /**
29
-     * Initialize the augmented scene
29
+     * Initialization
30
      * @abstract
30
      * @abstract
31
      * @param {ARSystem} ar
31
      * @param {ARSystem} ar
32
      * @returns {void | Promise<void> | SpeedyPromise<void>}
32
      * @returns {void | Promise<void> | SpeedyPromise<void>}
37
     }
37
     }
38
 
38
 
39
     /**
39
     /**
40
-     * Update / animate the augmented scene
40
+     * Animation loop
41
      * @abstract
41
      * @abstract
42
      * @param {ARSystem} ar
42
      * @param {ARSystem} ar
43
      * @returns {void}
43
      * @returns {void}
48
     }
48
     }
49
 
49
 
50
     /**
50
     /**
51
-     * Release the augmented scene
51
+     * Release resources
52
      * @param {ARSystem} ar
52
      * @param {ARSystem} ar
53
      * @returns {void}
53
      * @returns {void}
54
      */
54
      */
59
 }
59
 }
60
 
60
 
61
 /**
61
 /**
62
- * Helper for augmenting the scenes with three.js
62
+ * Helper for creating Augmented Reality experiences
63
  */
63
  */
64
 class ARSystem
64
 class ARSystem
65
 {
65
 {
136
 
136
 
137
 /**
137
 /**
138
  * Enchant three.js with encantar.js!
138
  * Enchant three.js with encantar.js!
139
- * @param {ARScene} scene
139
+ * @param {ARDemo} demo
140
  * @returns {Promise<ARSystem>}
140
  * @returns {Promise<ARSystem>}
141
  */
141
  */
142
-function encantar(scene)
142
+function encantar(demo)
143
 {
143
 {
144
     const ar = new ARSystem();
144
     const ar = new ARSystem();
145
 
145
 
148
         ar._frame = frame;
148
         ar._frame = frame;
149
         mix(frame);
149
         mix(frame);
150
 
150
 
151
-        scene.update(ar);
151
+        demo.update(ar);
152
 
152
 
153
         ar._renderer.render(ar._scene, ar._camera);
153
         ar._renderer.render(ar._scene, ar._camera);
154
         ar._session.requestAnimationFrame(animate);
154
         ar._session.requestAnimationFrame(animate);
156
 
156
 
157
     function mix(frame)
157
     function mix(frame)
158
     {
158
     {
159
-        ar._origin.visible = false;
160
-
161
         for(const result of frame.results) {
159
         for(const result of frame.results) {
162
             if(result.tracker.type == 'image-tracker') {
160
             if(result.tracker.type == 'image-tracker') {
163
                 if(result.trackables.length > 0) {
161
                 if(result.trackables.length > 0) {
173
                 }
171
                 }
174
             }
172
             }
175
         }
173
         }
174
+
175
+        ar._origin.visible = false;
176
     }
176
     }
177
 
177
 
178
     function align(projectionMatrix, viewMatrixInverse, modelMatrix)
178
     function align(projectionMatrix, viewMatrixInverse, modelMatrix)
187
 
187
 
188
     return Promise.resolve()
188
     return Promise.resolve()
189
     .then(() => {
189
     .then(() => {
190
-        return scene.startSession(); // Promise or SpeedyPromise
190
+        return demo.startSession(); // Promise or SpeedyPromise
191
     })
191
     })
192
     .then(session => {
192
     .then(session => {
193
 
193
 
197
 
197
 
198
         ar._origin = new THREE.Group();
198
         ar._origin = new THREE.Group();
199
         ar._origin.matrixAutoUpdate = false;
199
         ar._origin.matrixAutoUpdate = false;
200
+        ar._origin.visible = false;
200
         ar._scene.add(ar._origin);
201
         ar._scene.add(ar._origin);
201
 
202
 
202
         ar._root = new THREE.Group();
203
         ar._root = new THREE.Group();
223
 
224
 
224
         return Promise.resolve()
225
         return Promise.resolve()
225
         .then(() => {
226
         .then(() => {
226
-            return scene.init(ar);
227
+            return demo.init(ar);
227
         })
228
         })
228
         .then(() => {
229
         .then(() => {
229
-            session.addEventListener('end', event => { scene.release(ar); });
230
+            session.addEventListener('end', event => { demo.release(ar); });
230
             session.requestAnimationFrame(animate);
231
             session.requestAnimationFrame(animate);
231
             return ar;
232
             return ar;
232
         })
233
         })

Loading…
Cancel
Save