Browse Source

Preload assets

customisations
alemart 9 months ago
parent
commit
862535d825
3 changed files with 45 additions and 16 deletions
  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 View File

17
     {
17
     {
18
         super();
18
         super();
19
 
19
 
20
+        this._assetManager = new AssetManager();
20
         this._objects = { };
21
         this._objects = { };
21
         this._initialized = false;
22
         this._initialized = false;
22
     }
23
     }
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
      * Initialization
106
      * Initialization
90
      * @returns {Promise<void>}
107
      * @returns {Promise<void>}
91
      */
108
      */
145
     _initMagicCircle()
162
     _initMagicCircle()
146
     {
163
     {
147
         // create a magic circle
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
         const magicCircle = BABYLON.MeshBuilder.CreatePlane('magic-circle', {
175
         const magicCircle = BABYLON.MeshBuilder.CreatePlane('magic-circle', {
149
             width: 1,
176
             width: 1,
150
             height: 1,
177
             height: 1,
151
             sideOrientation: BABYLON.Mesh.DOUBLESIDE
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
         magicCircle.rotation.set(-Math.PI / 2, 0, 0);
181
         magicCircle.rotation.set(-Math.PI / 2, 0, 0);
162
         magicCircle.scaling.set(4, 4, 1);
182
         magicCircle.scaling.set(4, 4, 1);
183
+        magicCircle.material = material;
163
 
184
 
164
         // make it a child of ar.root
185
         // make it a child of ar.root
165
         const ar = this.ar;
186
         const ar = this.ar;
171
 
192
 
172
     _initText()
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
         const text = BABYLON.MeshBuilder.CreatePlane('text', {
205
         const text = BABYLON.MeshBuilder.CreatePlane('text', {
175
             width: 1,
206
             width: 1,
176
             height: 1,
207
             height: 1,
177
             sideOrientation: BABYLON.Mesh.DOUBLESIDE
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
         text.position.set(0, 2, 0.5);
211
         text.position.set(0, 2, 0.5);
188
         text.scaling.set(3, 1.5, 1);
212
         text.scaling.set(3, 1.5, 1);
213
+        text.material = material;
189
 
214
 
190
         const ar = this.ar;
215
         const ar = this.ar;
191
         text.parent = ar.root;
216
         text.parent = ar.root;
196
     async _initMage()
221
     async _initMage()
197
     {
222
     {
198
         // load the mage
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
         const mage = gltf.meshes[0];
226
         const mage = gltf.meshes[0];
201
         mage.scaling.set(0.7, 0.7, 0.7);
227
         mage.scaling.set(0.7, 0.7, 0.7);
202
 
228
 
215
 
241
 
216
     async _initCat()
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
         const cat = gltf.meshes[0];
246
         const cat = gltf.meshes[0];
220
         cat.scaling.set(0.7, 0.7, 0.7);
247
         cat.scaling.set(0.7, 0.7, 0.7);
221
 
248
 

+ 1
- 0
demos/hello-babylon/index.html View File

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

+ 1
- 0
demos/hello-babylon/video.html View File

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

Loading…
Cancel
Save