Selaa lähdekoodia

Preload assets

customisations
alemart 9 kuukautta sitten
vanhempi
commit
862535d825
3 muutettua tiedostoa jossa 45 lisäystä ja 16 poistoa
  1. 43
    16
      demos/hello-babylon/demo.js
  2. 1
    0
      demos/hello-babylon/index.html
  3. 1
    0
      demos/hello-babylon/video.html

+ 43
- 16
demos/hello-babylon/demo.js Näytä tiedosto

@@ -17,6 +17,7 @@ class EnchantedDemo extends ARDemo
17 17
     {
18 18
         super();
19 19
 
20
+        this._assetManager = new AssetManager();
20 21
         this._objects = { };
21 22
         this._initialized = false;
22 23
     }
@@ -86,6 +87,22 @@ class EnchantedDemo extends ARDemo
86 87
     }
87 88
 
88 89
     /**
90
+     * Preload resources before starting the AR session
91
+     * @returns {Promise<void>}
92
+     */
93
+    preload()
94
+    {
95
+        console.log('Preloading assets...');
96
+
97
+        return this._assetManager.preload([
98
+            '../assets/mage.glb',
99
+            '../assets/cat.glb',
100
+            '../assets/magic-circle.png',
101
+            '../assets/it-works.png',
102
+        ]);
103
+    }
104
+
105
+    /**
89 106
      * Initialization
90 107
      * @returns {Promise<void>}
91 108
      */
@@ -145,21 +162,25 @@ class EnchantedDemo extends ARDemo
145 162
     _initMagicCircle()
146 163
     {
147 164
         // create a magic circle
165
+        const material = new BABYLON.StandardMaterial('magic-circle-material');
166
+        const url = this._assetManager.url('magic-circle.png');
167
+
168
+        material.diffuseTexture = new BABYLON.Texture(url);
169
+        material.diffuseTexture.hasAlpha = true;
170
+        material.useAlphaFromDiffuseTexture = true;
171
+        material.diffuseColor.set(0, 0, 0);
172
+        material.emissiveColor.set(1, 1, 1);
173
+        material.unlit = true;
174
+
148 175
         const magicCircle = BABYLON.MeshBuilder.CreatePlane('magic-circle', {
149 176
             width: 1,
150 177
             height: 1,
151 178
             sideOrientation: BABYLON.Mesh.DOUBLESIDE
152 179
         });
153 180
 
154
-        magicCircle.material = new BABYLON.StandardMaterial('magic-circle-material');
155
-        magicCircle.material.diffuseTexture = new BABYLON.Texture('../assets/magic-circle.png');
156
-        magicCircle.material.diffuseTexture.hasAlpha = true;
157
-        magicCircle.material.useAlphaFromDiffuseTexture = true;
158
-        magicCircle.material.diffuseColor.set(0, 0, 0);
159
-        magicCircle.material.emissiveColor.set(1, 1, 1);
160
-        magicCircle.material.unlit = true;
161 181
         magicCircle.rotation.set(-Math.PI / 2, 0, 0);
162 182
         magicCircle.scaling.set(4, 4, 1);
183
+        magicCircle.material = material;
163 184
 
164 185
         // make it a child of ar.root
165 186
         const ar = this.ar;
@@ -171,21 +192,25 @@ class EnchantedDemo extends ARDemo
171 192
 
172 193
     _initText()
173 194
     {
195
+        const material = new BABYLON.StandardMaterial('text-material');
196
+        const url = this._assetManager.url('it-works.png');
197
+
198
+        material.diffuseTexture = new BABYLON.Texture(url);
199
+        material.diffuseTexture.hasAlpha = true;
200
+        material.useAlphaFromDiffuseTexture = true;
201
+        material.diffuseColor.set(0, 0, 0);
202
+        material.emissiveColor.set(1, 1, 1);
203
+        material.unlit = true;
204
+
174 205
         const text = BABYLON.MeshBuilder.CreatePlane('text', {
175 206
             width: 1,
176 207
             height: 1,
177 208
             sideOrientation: BABYLON.Mesh.DOUBLESIDE
178 209
         });
179 210
 
180
-        text.material = new BABYLON.StandardMaterial('text-material');
181
-        text.material.diffuseTexture = new BABYLON.Texture('../assets/it-works.png');
182
-        text.material.diffuseTexture.hasAlpha = true;
183
-        text.material.useAlphaFromDiffuseTexture = true;
184
-        text.material.diffuseColor.set(0, 0, 0);
185
-        text.material.emissiveColor.set(1, 1, 1);
186
-        text.material.unlit = true;
187 211
         text.position.set(0, 2, 0.5);
188 212
         text.scaling.set(3, 1.5, 1);
213
+        text.material = material;
189 214
 
190 215
         const ar = this.ar;
191 216
         text.parent = ar.root;
@@ -196,7 +221,8 @@ class EnchantedDemo extends ARDemo
196 221
     async _initMage()
197 222
     {
198 223
         // load the mage
199
-        const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'mage.glb');
224
+        const file = this._assetManager.file('mage.glb');
225
+        const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '', file);
200 226
         const mage = gltf.meshes[0];
201 227
         mage.scaling.set(0.7, 0.7, 0.7);
202 228
 
@@ -215,7 +241,8 @@ class EnchantedDemo extends ARDemo
215 241
 
216 242
     async _initCat()
217 243
     {
218
-        const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '../assets/', 'cat.glb');
244
+        const file = this._assetManager.file('cat.glb');
245
+        const gltf = await BABYLON.SceneLoader.ImportMeshAsync('', '', file);
219 246
         const cat = gltf.meshes[0];
220 247
         cat.scaling.set(0.7, 0.7, 0.7);
221 248
 

+ 1
- 0
demos/hello-babylon/index.html Näytä tiedosto

@@ -9,6 +9,7 @@
9 9
         <script src="https://cdn.jsdelivr.net/npm/babylonjs@7.29.0/babylon.min.js"></script>
10 10
         <script src="https://cdn.jsdelivr.net/npm/babylonjs-loaders@7.29.0/babylonjs.loaders.min.js"></script>
11 11
         <script src="../../plugins/babylon-with-encantar.js"></script>
12
+        <script src="../../plugins/extras/asset-manager.js"></script>
12 13
         <script src="demo.js"></script>
13 14
     </head>
14 15
     <body>

+ 1
- 0
demos/hello-babylon/video.html Näytä tiedosto

@@ -9,6 +9,7 @@
9 9
         <script src="https://cdn.jsdelivr.net/npm/babylonjs@7.29.0/babylon.min.js"></script>
10 10
         <script src="https://cdn.jsdelivr.net/npm/babylonjs-loaders@7.29.0/babylonjs.loaders.min.js"></script>
11 11
         <script src="../../plugins/babylon-with-encantar.js"></script>
12
+        <script src="../../plugins/extras/asset-manager.js"></script>
12 13
         <script src="demo.js"></script>
13 14
     </head>
14 15
     <body>

Loading…
Peruuta
Tallenna