Skip to content

Commit 060a176

Browse files
committed
fix: load asset container for scene @visibleAsAsset
1 parent 4e5b23f commit 060a176

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

tools/src/decorators/apply.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export function applyDecorators(scene: Scene, object: any, script: any, instance
223223

224224
switch (assetType) {
225225
case "json":
226+
case "scene":
226227
instance[propertyKey] = data;
227228
break;
228229

tools/src/loading/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function loadScene(rootUrl: any, sceneFilename: string, scene: Scen
132132
}
133133

134134
if (!options?.skipAssetsPreload) {
135-
await _preloadScriptsAssets(scene, rootUrl, scriptsMap, options);
135+
await _preloadScriptsAssets(rootUrl, scene);
136136
}
137137

138138
options?.onProgress?.(1);

tools/src/loading/script.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import { Scene } from "@babylonjs/core/scene";
33
import { Observer } from "@babylonjs/core/Misc/observable";
44
import { PointerInfo } from "@babylonjs/core/Events/pointerEvents";
55
import { KeyboardInfo } from "@babylonjs/core/Events/keyboardEvents";
6+
import { LoadAssetContainerAsync } from "@babylonjs/core/Loading/sceneLoader";
7+
import { SceneLoaderFlags } from "@babylonjs/core/Loading/sceneLoaderFlags";
68
import { IParticleSystem } from "@babylonjs/core/Particles/IParticleSystem";
79

810
import { IScript } from "../script";
911

1012
import { applyDecorators } from "../decorators/apply";
1113

12-
import { isAnyParticleSystem, isNode, isScene } from "../tools/guards";
14+
import { isAnyParticleSystem, isMesh, isNode, isScene } from "../tools/guards";
1315

14-
import { loadScene, SceneLoaderOptions, ScriptMap } from "./loader";
16+
import { ScriptMap } from "./loader";
1517

1618
/**
1719
* Defines the cache of all
@@ -21,7 +23,7 @@ export const scriptAssetsCache = new Map<string, any>();
2123
/**
2224
* @internal
2325
*/
24-
export async function _preloadScriptsAssets(scene: Scene, rootUrl: string, scriptsMap: ScriptMap, options?: SceneLoaderOptions) {
26+
export async function _preloadScriptsAssets(rootUrl: string, scene: Scene) {
2527
const nodes = [scene, ...scene.transformNodes, ...scene.meshes, ...scene.lights, ...scene.cameras];
2628

2729
const scripts = nodes
@@ -61,9 +63,19 @@ export async function _preloadScriptsAssets(scene: Scene, rootUrl: string, scrip
6163
const filename = key.split("/").pop()!;
6264
const sceneFilename = filename.replace(".scene", ".babylon");
6365

64-
scriptAssetsCache.set(key, "done");
66+
// Load asset container
67+
const container = await LoadAssetContainerAsync(`${rootUrl}${sceneFilename}`, scene, {
68+
pluginExtension: ".babylon",
69+
});
6570

66-
await loadScene(rootUrl, sceneFilename, scene, scriptsMap, options);
71+
// Ensure all meshes perform their delay state check
72+
if (SceneLoaderFlags.ForceFullSceneLoadingForIncremental) {
73+
scene.meshes.forEach((m) => isMesh(m) && m._checkDelayState());
74+
}
75+
76+
container.addAllToScene();
77+
78+
scriptAssetsCache.set(key, container);
6779
} else {
6880
const response = await fetch(`${rootUrl}${key}`);
6981
const data = await response.json();

tools/test/decorators/apply.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ jest.mock("@babylonjs/core/Particles/Node/nodeParticleSystemSet", () => ({
120120
},
121121
}));
122122

123+
jest.mock("@babylonjs/core/Loading/sceneLoaderFlags", () => ({
124+
SceneLoaderFlags: {
125+
ForceFullSceneLoadingForIncremental: 1,
126+
},
127+
}));
128+
jest.mock("@babylonjs/core/Loading/sceneLoader", () => ({
129+
LoadAssetContainerAsync: jest.fn(),
130+
}));
131+
123132
import { Node } from "@babylonjs/core/node";
124133
import { Scene } from "@babylonjs/core/scene";
125134
import { Sound } from "@babylonjs/core/Audio/sound";

tools/test/loading/script.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ jest.mock("@babylonjs/core/Particles/Node/nodeParticleSystemSet", () => ({
1818
jest.mock("@babylonjs/gui/2D/advancedDynamicTexture", () => ({
1919
AdvancedDynamicTexture: class {},
2020
}));
21+
jest.mock("@babylonjs/core/Loading/sceneLoaderFlags", () => ({
22+
SceneLoaderFlags: {
23+
ForceFullSceneLoadingForIncremental: 1,
24+
},
25+
}));
26+
jest.mock("@babylonjs/core/Loading/sceneLoader", () => ({
27+
LoadAssetContainerAsync: jest.fn(),
28+
}));
2129

2230
import { applyScriptOnObject } from "../../src/loading/script";
2331

0 commit comments

Comments
 (0)