瀏覽代碼

Update three.js plugin and demo

customisations
alemart 11 月之前
父節點
當前提交
c5344f9d68
共有 2 個檔案被更改,包括 40 行新增40 行删除
  1. 25
    26
      demos/hello-three/demo.js
  2. 15
    14
      plugins/three-with-encantar.js

+ 25
- 26
demos/hello-three/demo.js 查看文件

@@ -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
 }

+ 15
- 14
plugins/three-with-encantar.js 查看文件

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

Loading…
取消
儲存