Bläddra i källkod

Store a reference to ARSystem in ARDemo

customisations
alemart 9 månader sedan
förälder
incheckning
3393c3716f
4 ändrade filer med 93 tillägg och 51 borttagningar
  1. 18
    14
      demos/hello-babylon/demo.js
  2. 20
    14
      demos/hello-three/demo.js
  3. 28
    12
      plugins/babylon-with-encantar.js
  4. 27
    11
      plugins/three-with-encantar.js

+ 18
- 14
demos/hello-babylon/demo.js Visa fil

@@ -87,10 +87,9 @@ class EnchantedDemo extends ARDemo
87 87
 
88 88
     /**
89 89
      * Initialization
90
-     * @param {ARSystem} ar
91 90
      * @returns {Promise<void>}
92 91
      */
93
-    async init(ar)
92
+    async init()
94 93
     {
95 94
         // Do not automatically play an animation when loading GLTF models
96 95
         BABYLON.SceneLoader.OnPluginActivatedObservable.add(loader => {
@@ -100,16 +99,17 @@ class EnchantedDemo extends ARDemo
100 99
         });
101 100
 
102 101
         // Change the point of view - slightly
102
+        const ar = this.ar;
103 103
         ar.root.position.y = -0.8;
104 104
 
105 105
         // Initialize objects
106
-        this._initLight(ar);
107
-        this._initText(ar);
108
-        this._initMagicCircle(ar);
106
+        this._initLight();
107
+        this._initText();
108
+        this._initMagicCircle();
109 109
 
110 110
         await Promise.all([
111
-            this._initMage(ar),
112
-            this._initCat(ar),
111
+            this._initMage(),
112
+            this._initCat(),
113 113
         ]);
114 114
 
115 115
         // done!
@@ -118,11 +118,11 @@ class EnchantedDemo extends ARDemo
118 118
 
119 119
     /**
120 120
      * Animation loop
121
-     * @param {ARSystem} ar
122 121
      * @returns {void}
123 122
      */
124
-    update(ar)
123
+    update()
125 124
     {
125
+        const ar = this.ar;
126 126
         const delta = ar.session.time.delta; // given in seconds
127 127
 
128 128
         this._animateMagicCircle(delta);
@@ -132,7 +132,7 @@ class EnchantedDemo extends ARDemo
132 132
     // ------------------------------------------------------------------------
133 133
 
134 134
 
135
-    _initLight(ar)
135
+    _initLight()
136 136
     {
137 137
         const light = new BABYLON.HemisphericLight('light', BABYLON.Vector3.Up());
138 138
         light.intensity = 1.0;
@@ -140,7 +140,7 @@ class EnchantedDemo extends ARDemo
140 140
         light.specular.set(0, 0, 0);
141 141
     }
142 142
 
143
-    _initMagicCircle(ar)
143
+    _initMagicCircle()
144 144
     {
145 145
         // create a magic circle
146 146
         const magicCircle = BABYLON.MeshBuilder.CreatePlane('magic-circle', {
@@ -160,13 +160,14 @@ class EnchantedDemo extends ARDemo
160 160
         magicCircle.scaling.set(4, 4, 1);
161 161
 
162 162
         // make it a child of ar.root
163
+        const ar = this.ar;
163 164
         magicCircle.parent = ar.root;
164 165
 
165 166
         // save a reference
166 167
         this._objects.magicCircle = magicCircle;
167 168
     }
168 169
 
169
-    _initText(ar)
170
+    _initText()
170 171
     {
171 172
         const text = BABYLON.MeshBuilder.CreatePlane('text', {
172 173
             width: 1,
@@ -184,12 +185,13 @@ class EnchantedDemo extends ARDemo
184 185
         text.position.set(0, 2, 0.5);
185 186
         text.scaling.set(3, 1.5, 1);
186 187
 
188
+        const ar = this.ar;
187 189
         text.parent = ar.root;
188 190
 
189 191
         this._objects.text = text;
190 192
     }
191 193
 
192
-    async _initMage(ar)
194
+    async _initMage()
193 195
     {
194 196
         // load the mage
195 197
         const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'mage.glb');
@@ -202,13 +204,14 @@ class EnchantedDemo extends ARDemo
202 204
             anim.play(true);
203 205
 
204 206
         // make the mage a child of ar.root
207
+        const ar = this.ar;
205 208
         mage.parent = ar.root;
206 209
 
207 210
         // save a reference
208 211
         this._objects.mage = mage;
209 212
     }
210 213
 
211
-    async _initCat(ar)
214
+    async _initCat()
212 215
     {
213 216
         const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'cat.glb');
214 217
         const cat = gltf.meshes[0];
@@ -218,6 +221,7 @@ class EnchantedDemo extends ARDemo
218 221
         if(anim)
219 222
             anim.play(true);
220 223
 
224
+        const ar = this.ar;
221 225
         cat.parent = ar.root;
222 226
 
223 227
         this._objects.cat = cat;

+ 20
- 14
demos/hello-three/demo.js Visa fil

@@ -177,11 +177,12 @@ class EnchantedDemo extends ARDemo
177 177
 
178 178
     /**
179 179
      * Initialization
180
-     * @param {ARSystem} ar
181 180
      * @returns {void}
182 181
      */
183
-    init(ar)
182
+    init()
184 183
     {
184
+        const ar = this.ar;
185
+
185 186
         // Change the point of view. All virtual objects are descendants of
186 187
         // ar.root, a node that is automatically aligned to the physical scene.
187 188
         // Adjusting ar.root will adjust all virtual objects.
@@ -189,11 +190,11 @@ class EnchantedDemo extends ARDemo
189 190
         ar.root.position.set(0, -0.8, 0);
190 191
 
191 192
         // Initialize objects
192
-        this._initLight(ar);
193
-        this._initText(ar);
194
-        this._initMagicCircle(ar);
195
-        this._initMage(ar);
196
-        this._initCat(ar);
193
+        this._initLight();
194
+        this._initText();
195
+        this._initMagicCircle();
196
+        this._initMage();
197
+        this._initCat();
197 198
 
198 199
         // done!
199 200
         this._initialized = true;
@@ -201,11 +202,11 @@ class EnchantedDemo extends ARDemo
201 202
 
202 203
     /**
203 204
      * Animation loop
204
-     * @param {ARSystem} ar
205 205
      * @returns {void}
206 206
      */
207
-    update(ar)
207
+    update()
208 208
     {
209
+        const ar = this.ar;
209 210
         const delta = ar.session.time.delta; // given in seconds
210 211
 
211 212
         // animate the objects of the scene
@@ -218,15 +219,16 @@ class EnchantedDemo extends ARDemo
218 219
     // ------------------------------------------------------------------------
219 220
 
220 221
 
221
-    _initLight(ar)
222
+    _initLight()
222 223
     {
223 224
         const ambientLight = new THREE.AmbientLight(0xffffff);
224 225
         ambientLight.intensity = 1.5;
225 226
 
227
+        const ar = this.ar;
226 228
         ar.scene.add(ambientLight);
227 229
     }
228 230
 
229
-    _initMagicCircle(ar)
231
+    _initMagicCircle()
230 232
     {
231 233
         // create a magic circle
232 234
         const magicCircle = Utils.createImagePlane('../assets/magic-circle.png');
@@ -235,13 +237,14 @@ class EnchantedDemo extends ARDemo
235 237
         magicCircle.scale.set(4, 4, 1);
236 238
 
237 239
         // make it a child of ar.root
240
+        const ar = this.ar;
238 241
         ar.root.add(magicCircle);
239 242
 
240 243
         // save a reference
241 244
         this._objects.magicCircle = magicCircle;
242 245
     }
243 246
 
244
-    _initText(ar)
247
+    _initText()
245 248
     {
246 249
         const text = Utils.createImagePlane('../assets/it-works.png');
247 250
         text.material.transparent = true;
@@ -250,12 +253,13 @@ class EnchantedDemo extends ARDemo
250 253
         text.scale.set(3, 1.5, 1);
251 254
         text.rotateX(Math.PI / 2);
252 255
 
256
+        const ar = this.ar;
253 257
         ar.root.add(text);
254 258
 
255 259
         this._objects.text = text;
256 260
     }
257 261
 
258
-    _initMage(ar)
262
+    _initMage()
259 263
     {
260 264
         // load the mage
261 265
         const gltf = this._objects.gltf.mage;
@@ -267,6 +271,7 @@ class EnchantedDemo extends ARDemo
267 271
         mageAction.play();
268 272
 
269 273
         // make the mage a child of ar.root
274
+        const ar = this.ar;
270 275
         ar.root.add(mage);
271 276
 
272 277
         // save references
@@ -274,7 +279,7 @@ class EnchantedDemo extends ARDemo
274 279
         this._objects.mageAction = mageAction;
275 280
     }
276 281
 
277
-    _initCat(ar)
282
+    _initCat()
278 283
     {
279 284
         const gltf = this._objects.gltf.cat;
280 285
         const cat = gltf.scene;
@@ -283,6 +288,7 @@ class EnchantedDemo extends ARDemo
283 288
         const catAction = Utils.createAnimationAction(gltf, 'Cheer');
284 289
         catAction.play();
285 290
 
291
+        const ar = this.ar;
286 292
         ar.root.add(cat);
287 293
 
288 294
         this._objects.cat = cat;

+ 28
- 12
plugins/babylon-with-encantar.js Visa fil

@@ -17,8 +17,8 @@ class ARDemo
17 17
 {
18 18
     /**
19 19
      * Start the AR session
20
-     * @abstract
21 20
      * @returns {Promise<Session> | SpeedyPromise<Session>}
21
+     * @abstract
22 22
      */
23 23
     startSession()
24 24
     {
@@ -27,32 +27,29 @@ class ARDemo
27 27
 
28 28
     /**
29 29
      * Initialization
30
-     * @abstract
31
-     * @param {ARSystem} ar
32 30
      * @returns {void | Promise<void> | SpeedyPromise<void>}
31
+     * @abstract
33 32
      */
34
-    init(ar)
33
+    init()
35 34
     {
36 35
         throw new Error('Abstract method');
37 36
     }
38 37
 
39 38
     /**
40 39
      * Animation loop
41
-     * @abstract
42
-     * @param {ARSystem} ar
43 40
      * @returns {void}
41
+     * @abstract
44 42
      */
45
-    update(ar)
43
+    update()
46 44
     {
47 45
         throw new Error('Abstract method');
48 46
     }
49 47
 
50 48
     /**
51 49
      * Release resources
52
-     * @param {ARSystem} ar
53 50
      * @returns {void}
54 51
      */
55
-    release(ar)
52
+    release()
56 53
     {
57 54
         // optional implementation
58 55
     }
@@ -66,6 +63,23 @@ class ARDemo
66 63
         // optional implementation
67 64
         return Promise.resolve();
68 65
     }
66
+
67
+    /**
68
+     * A reference to the ARSystem
69
+     * @returns {ARSystem | null}
70
+     */
71
+    get ar()
72
+    {
73
+        return this._ar;
74
+    }
75
+
76
+    /**
77
+     * Constructor
78
+     */
79
+    constructor()
80
+    {
81
+        this._ar = null;
82
+    }
69 83
 }
70 84
 
71 85
 /**
@@ -244,7 +258,7 @@ function encantar(demo)
244 258
         mix(frame);
245 259
 
246 260
         ar._engine.beginFrame();
247
-        demo.update(ar);
261
+        demo.update();
248 262
         ar._scene.render(false);
249 263
         ar._engine.endFrame();
250 264
 
@@ -307,6 +321,8 @@ function encantar(demo)
307 321
     .then(() => demo.startSession()) // Promise or SpeedyPromise
308 322
     .then(session => {
309 323
 
324
+        demo._ar = ar;
325
+
310 326
         ar._session = session;
311 327
 
312 328
         ar._engine = new BABYLON.Engine(session.viewport.canvas, false, {
@@ -352,10 +368,10 @@ function encantar(demo)
352 368
 
353 369
         return Promise.resolve()
354 370
         .then(() => {
355
-            return demo.init(ar);
371
+            return demo.init();
356 372
         })
357 373
         .then(() => {
358
-            session.addEventListener('end', event => { demo.release(ar); });
374
+            session.addEventListener('end', event => { demo.release(); });
359 375
             session.requestAnimationFrame(animate);
360 376
             return ar;
361 377
         })

+ 27
- 11
plugins/three-with-encantar.js Visa fil

@@ -27,32 +27,29 @@ class ARDemo
27 27
 
28 28
     /**
29 29
      * Initialization
30
-     * @abstract
31
-     * @param {ARSystem} ar
32 30
      * @returns {void | Promise<void> | SpeedyPromise<void>}
31
+     * @abstract
33 32
      */
34
-    init(ar)
33
+    init()
35 34
     {
36 35
         throw new Error('Abstract method');
37 36
     }
38 37
 
39 38
     /**
40 39
      * Animation loop
41
-     * @abstract
42
-     * @param {ARSystem} ar
43 40
      * @returns {void}
41
+     * @abstract
44 42
      */
45
-    update(ar)
43
+    update()
46 44
     {
47 45
         throw new Error('Abstract method');
48 46
     }
49 47
 
50 48
     /**
51 49
      * Release resources
52
-     * @param {ARSystem} ar
53 50
      * @returns {void}
54 51
      */
55
-    release(ar)
52
+    release()
56 53
     {
57 54
         // optional implementation
58 55
     }
@@ -66,6 +63,23 @@ class ARDemo
66 63
         // optional implementation
67 64
         return Promise.resolve();
68 65
     }
66
+
67
+    /**
68
+     * A reference to the ARSystem
69
+     * @returns {ARSystem | null}
70
+     */
71
+    get ar()
72
+    {
73
+        return this._ar;
74
+    }
75
+
76
+    /**
77
+     * Constructor
78
+     */
79
+    constructor()
80
+    {
81
+        this._ar = null;
82
+    }
69 83
 }
70 84
 
71 85
 /**
@@ -237,7 +251,7 @@ function encantar(demo)
237 251
         ar._frame = frame;
238 252
         mix(frame);
239 253
 
240
-        demo.update(ar);
254
+        demo.update();
241 255
 
242 256
         ar._renderer.render(ar._scene, ar._camera);
243 257
         ar._session.requestAnimationFrame(animate);
@@ -289,6 +303,8 @@ function encantar(demo)
289 303
     .then(() => demo.startSession()) // Promise or SpeedyPromise
290 304
     .then(session => {
291 305
 
306
+        demo._ar = ar;
307
+
292 308
         ar._session = session;
293 309
 
294 310
         ar._scene = new THREE.Scene();
@@ -324,10 +340,10 @@ function encantar(demo)
324 340
 
325 341
         return Promise.resolve()
326 342
         .then(() => {
327
-            return demo.init(ar);
343
+            return demo.init();
328 344
         })
329 345
         .then(() => {
330
-            session.addEventListener('end', event => { demo.release(ar); });
346
+            session.addEventListener('end', event => { demo.release(); });
331 347
             session.requestAnimationFrame(animate);
332 348
             return ar;
333 349
         })

Laddar…
Avbryt
Spara