|
17 | 17 | import '@babylonjs/core/Materials/Textures/Loaders/basisTextureLoader'; |
18 | 18 | import '@babylonjs/core/Materials/Textures/Loaders/ktxTextureLoader'; |
19 | 19 | import '@babylonjs/loaders/glTF'; |
20 | | -import { AssetContainer } from '@babylonjs/core/assetContainer'; |
21 | | -import { SceneLoader } from '@babylonjs/core/Loading/sceneLoader'; |
| 20 | +import { LoadAssetContainerAsync, LoadAssetContainerOptions } from '@babylonjs/core/Loading/sceneLoader'; |
22 | 21 | import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh'; |
23 | 22 | import { IDisposable } from '@babylonjs/core/scene'; |
24 | | -import { GLTFFileLoader, GLTFLoaderAnimationStartMode } from '@babylonjs/loaders/glTF/glTFFileLoader'; |
| 23 | +import { GLTFLoaderAnimationStartMode } from '@babylonjs/loaders/glTF/glTFFileLoader'; |
25 | 24 |
|
26 | 25 | import { ObservableManager } from '../manager/observableManager'; |
27 | 26 | import { SceneManager } from '../manager/sceneManager'; |
@@ -74,64 +73,63 @@ export class ModelLoader implements IDisposable { |
74 | 73 | throw Error('No glTF content or url provided'); |
75 | 74 | } |
76 | 75 |
|
77 | | - let rootUrl = url; |
78 | | - let contents = ''; |
79 | 76 | let pluginExtension = '.glb'; |
80 | 77 | let isBase64 = false; |
81 | 78 | let isBlobUrl = false; |
82 | 79 | const extensionRegEx = /\.(glb|gltf|babylon|obj|stl)$/; |
83 | 80 |
|
84 | 81 | if (url.startsWith('data:')) { |
85 | | - rootUrl = ''; |
86 | | - contents = url; |
87 | 82 | isBase64 = true; |
88 | 83 | } else if (url.startsWith('blob:')) { |
89 | 84 | isBlobUrl = true; |
90 | 85 | } else if (extensionRegEx.test(url.toLowerCase())) { |
91 | 86 | pluginExtension = url.toLowerCase().match(extensionRegEx)?.at(0) ?? pluginExtension; |
92 | 87 | } |
93 | 88 |
|
94 | | - SceneLoader.OnPluginActivatedObservable.addOnce(function (loader) { |
95 | | - if (loader.name === 'gltf' && loader instanceof GLTFFileLoader) { |
96 | | - // Use HTTP range requests to load the glTF binary (GLB) in parts. |
97 | | - loader.useRangeRequests = !isBase64 && !isBlobUrl; |
| 89 | + const loadAssetContainerOptions: LoadAssetContainerOptions = { |
| 90 | + pluginOptions: { |
| 91 | + gltf: { |
| 92 | + // Use HTTP range requests to load the glTF binary (GLB) in parts. |
| 93 | + useRangeRequests: !isBase64 && !isBlobUrl, |
| 94 | + extensionOptions: { |
| 95 | + /* For debugging MSFT_lod extension */ |
| 96 | + // MSFT_lod: { |
| 97 | + // maxLODsToLoad: 1, |
| 98 | + // }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + pluginExtension: pluginExtension, |
| 103 | + }; |
| 104 | + |
| 105 | + if (disableAnimation) { |
| 106 | + loadAssetContainerOptions.pluginOptions!.gltf!.animationStartMode = GLTFLoaderAnimationStartMode.NONE; |
| 107 | + } |
98 | 108 |
|
99 | | - if (disableAnimation) { |
100 | | - loader.animationStartMode = GLTFLoaderAnimationStartMode.NONE; |
101 | | - } |
| 109 | + if (options?.targetFps) { |
| 110 | + loadAssetContainerOptions.pluginOptions!.gltf!.targetFps = options.targetFps; |
| 111 | + } |
102 | 112 |
|
103 | | - if (options?.targetFps) { |
104 | | - loader.targetFps = options.targetFps; |
105 | | - } |
106 | | - } |
107 | | - }); |
| 113 | + /* For debugging MSFT_lod extension */ |
| 114 | + // SceneLoader.OnPluginActivatedObservable.addOnce(function (loader) { |
| 115 | + // if (loader.name === 'gltf' && loader instanceof GLTFFileLoader) { |
| 116 | + // loader.loggingEnabled = true; |
| 117 | + // } |
| 118 | + // }); |
108 | 119 |
|
109 | 120 | if (this._sceneManager.config.sceneConfig?.useLoadingUI) { |
110 | 121 | this._sceneManager.scene.getEngine().displayLoadingUI(); |
111 | 122 | } |
112 | 123 |
|
113 | | - const sceneLoaderAsyncResult = await SceneLoader.ImportMeshAsync( |
114 | | - '', |
115 | | - rootUrl, |
116 | | - contents, |
117 | | - this._sceneManager.scene, |
118 | | - undefined, |
119 | | - pluginExtension, |
120 | | - ); |
| 124 | + const container = await LoadAssetContainerAsync(url, this._sceneManager.scene, loadAssetContainerOptions); |
| 125 | + |
| 126 | + // Add everything from the container into the scene |
| 127 | + container.addAllToScene(); |
121 | 128 |
|
122 | 129 | if (this._sceneManager.config.sceneConfig?.useLoadingUI) { |
123 | 130 | this._sceneManager.scene.getEngine().hideLoadingUI(); |
124 | 131 | } |
125 | 132 |
|
126 | | - const container = new AssetContainer(this._sceneManager.scene); |
127 | | - container.meshes = sceneLoaderAsyncResult.meshes; |
128 | | - container.lights = sceneLoaderAsyncResult.lights; |
129 | | - container.geometries = sceneLoaderAsyncResult.geometries; |
130 | | - container.skeletons = sceneLoaderAsyncResult.skeletons; |
131 | | - container.particleSystems = sceneLoaderAsyncResult.particleSystems; |
132 | | - container.animationGroups = sceneLoaderAsyncResult.animationGroups; |
133 | | - container.transformNodes = sceneLoaderAsyncResult.transformNodes; |
134 | | - |
135 | 133 | // Avoid frustum clipping for all meshes |
136 | 134 | container.meshes.forEach((mesh: AbstractMesh) => { |
137 | 135 | mesh.alwaysSelectAsActiveMesh = true; |
|
0 commit comments