@onerjs/core 8.51.4 → 8.51.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Animations/animatorAvatar.d.ts +2 -0
- package/Animations/animatorAvatar.js +163 -94
- package/Animations/animatorAvatar.js.map +1 -1
- package/Engines/constants.d.ts +10 -0
- package/Engines/constants.js +10 -0
- package/Engines/constants.js.map +1 -1
- package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js +6 -3
- package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js.map +1 -1
- package/Materials/PBR/openpbrMaterial.d.ts +8 -0
- package/Materials/PBR/openpbrMaterial.js +16 -0
- package/Materials/PBR/openpbrMaterial.js.map +1 -1
- package/Materials/PBR/pbrBaseMaterial.d.ts +1 -0
- package/Materials/PBR/pbrBaseMaterial.js +8 -0
- package/Materials/PBR/pbrBaseMaterial.js.map +1 -1
- package/Materials/Textures/Procedurals/proceduralTexture.d.ts +6 -0
- package/Materials/Textures/Procedurals/proceduralTexture.js +3 -1
- package/Materials/Textures/Procedurals/proceduralTexture.js.map +1 -1
- package/Materials/Textures/index.d.ts +1 -0
- package/Materials/Textures/index.js +1 -0
- package/Materials/Textures/index.js.map +1 -1
- package/Materials/Textures/textureMerger.js +1 -0
- package/Materials/Textures/textureMerger.js.map +1 -1
- package/Materials/Textures/textureProcessor.d.ts +315 -0
- package/Materials/Textures/textureProcessor.js +792 -0
- package/Materials/Textures/textureProcessor.js.map +1 -0
- package/Materials/material.d.ts +24 -0
- package/Materials/material.js +39 -0
- package/Materials/material.js.map +1 -1
- package/Materials/standardMaterial.d.ts +1 -0
- package/Materials/standardMaterial.js +6 -0
- package/Materials/standardMaterial.js.map +1 -1
- package/Physics/v2/characterController.d.ts +104 -7
- package/Physics/v2/characterController.js +355 -7
- package/Physics/v2/characterController.js.map +1 -1
- package/Shaders/ShadersInclude/bumpFragment.js +3 -3
- package/Shaders/ShadersInclude/bumpFragment.js.map +1 -1
- package/Shaders/ShadersInclude/bumpFragmentMainFunctions.js +5 -1
- package/Shaders/ShadersInclude/bumpFragmentMainFunctions.js.map +1 -1
- package/Shaders/ShadersInclude/defaultFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/defaultFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/defaultUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/defaultUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/defaultVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/defaultVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrAmbientOcclusionData.js +1 -1
- package/Shaders/ShadersInclude/openpbrAmbientOcclusionData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrBackgroundTransmission.js +1 -1
- package/Shaders/ShadersInclude/openpbrBackgroundTransmission.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrBaseLayerData.js +14 -14
- package/Shaders/ShadersInclude/openpbrBaseLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrCoatLayerData.js +6 -6
- package/Shaders/ShadersInclude/openpbrCoatLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrDirectLighting.js +1 -1
- package/Shaders/ShadersInclude/openpbrDirectLighting.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js +1 -1
- package/Shaders/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/openpbrFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrFuzzLayerData.js +3 -3
- package/Shaders/ShadersInclude/openpbrFuzzLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrNormalMapFragment.js +4 -4
- package/Shaders/ShadersInclude/openpbrNormalMapFragment.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrSubsurfaceLayerData.js +7 -3
- package/Shaders/ShadersInclude/openpbrSubsurfaceLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrThinFilmLayerData.js +2 -2
- package/Shaders/ShadersInclude/openpbrThinFilmLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrTransmissionLayerData.js +5 -5
- package/Shaders/ShadersInclude/openpbrTransmissionLayerData.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/openpbrUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/openpbrVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/openpbrVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrBlockFinalUnlitComponents.js +1 -1
- package/Shaders/ShadersInclude/pbrBlockFinalUnlitComponents.js.map +1 -1
- package/Shaders/ShadersInclude/pbrBlockLightmapInit.js +1 -1
- package/Shaders/ShadersInclude/pbrBlockLightmapInit.js.map +1 -1
- package/Shaders/ShadersInclude/pbrFragmentDeclaration.js +3 -0
- package/Shaders/ShadersInclude/pbrFragmentDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrHelperFunctions.js +4 -0
- package/Shaders/ShadersInclude/pbrHelperFunctions.js.map +1 -1
- package/Shaders/ShadersInclude/pbrUboDeclaration.js +1 -1
- package/Shaders/ShadersInclude/pbrUboDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/pbrVertexDeclaration.js +1 -1
- package/Shaders/ShadersInclude/pbrVertexDeclaration.js.map +1 -1
- package/Shaders/ShadersInclude/textureRepetitionFunctions.d.ts +5 -0
- package/Shaders/ShadersInclude/textureRepetitionFunctions.js +52 -0
- package/Shaders/ShadersInclude/textureRepetitionFunctions.js.map +1 -0
- package/Shaders/default.fragment.d.ts +1 -0
- package/Shaders/default.fragment.js +8 -6
- package/Shaders/default.fragment.js.map +1 -1
- package/Shaders/geometry.fragment.js +3 -3
- package/Shaders/geometry.fragment.js.map +1 -1
- package/Shaders/openpbr.fragment.d.ts +1 -0
- package/Shaders/openpbr.fragment.js +4 -2
- package/Shaders/openpbr.fragment.js.map +1 -1
- package/Shaders/pbr.fragment.d.ts +1 -0
- package/Shaders/pbr.fragment.js +24 -22
- package/Shaders/pbr.fragment.js.map +1 -1
- package/Shaders/textureProcessor.fragment.d.ts +5 -0
- package/Shaders/textureProcessor.fragment.js +156 -0
- package/Shaders/textureProcessor.fragment.js.map +1 -0
- package/ShadersWGSL/ShadersInclude/bumpFragment.js +3 -3
- package/ShadersWGSL/ShadersInclude/bumpFragment.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/bumpFragmentMainFunctions.js +5 -1
- package/ShadersWGSL/ShadersInclude/bumpFragmentMainFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/defaultUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/defaultUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrAmbientOcclusionData.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrAmbientOcclusionData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBackgroundTransmission.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBackgroundTransmission.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrBaseLayerData.js +15 -15
- package/ShadersWGSL/ShadersInclude/openpbrBaseLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrCoatLayerData.js +7 -7
- package/ShadersWGSL/ShadersInclude/openpbrCoatLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrDirectLighting.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrDirectLighting.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrEnvironmentLighting.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrFuzzLayerData.js +4 -4
- package/ShadersWGSL/ShadersInclude/openpbrFuzzLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js +3 -3
- package/ShadersWGSL/ShadersInclude/openpbrIblFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrNormalMapFragment.js +4 -4
- package/ShadersWGSL/ShadersInclude/openpbrNormalMapFragment.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrSubsurfaceLayerData.js +7 -3
- package/ShadersWGSL/ShadersInclude/openpbrSubsurfaceLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrThinFilmLayerData.js +2 -2
- package/ShadersWGSL/ShadersInclude/openpbrThinFilmLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrTransmissionLayerData.js +6 -6
- package/ShadersWGSL/ShadersInclude/openpbrTransmissionLayerData.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/openpbrUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockFinalUnlitComponents.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockFinalUnlitComponents.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockLightmapInit.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrBlockLightmapInit.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrHelperFunctions.js +4 -0
- package/ShadersWGSL/ShadersInclude/pbrHelperFunctions.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/pbrUboDeclaration.js +1 -1
- package/ShadersWGSL/ShadersInclude/pbrUboDeclaration.js.map +1 -1
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.d.ts +5 -0
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.js +52 -0
- package/ShadersWGSL/ShadersInclude/textureRepetitionFunctions.js.map +1 -0
- package/ShadersWGSL/default.fragment.d.ts +1 -0
- package/ShadersWGSL/default.fragment.js +8 -6
- package/ShadersWGSL/default.fragment.js.map +1 -1
- package/ShadersWGSL/geometry.fragment.js +3 -3
- package/ShadersWGSL/geometry.fragment.js.map +1 -1
- package/ShadersWGSL/openpbr.fragment.d.ts +1 -0
- package/ShadersWGSL/openpbr.fragment.js +5 -3
- package/ShadersWGSL/openpbr.fragment.js.map +1 -1
- package/ShadersWGSL/openpbr.vertex.js +1 -1
- package/ShadersWGSL/openpbr.vertex.js.map +1 -1
- package/ShadersWGSL/pbr.fragment.d.ts +1 -0
- package/ShadersWGSL/pbr.fragment.js +24 -22
- package/ShadersWGSL/pbr.fragment.js.map +1 -1
- package/ShadersWGSL/textureProcessor.fragment.d.ts +5 -0
- package/ShadersWGSL/textureProcessor.fragment.js +161 -0
- package/ShadersWGSL/textureProcessor.fragment.js.map +1 -0
- package/SmartAssets/index.d.ts +2 -0
- package/SmartAssets/index.js +2 -0
- package/SmartAssets/index.js.map +1 -0
- package/SmartAssets/smartAssetManager.d.ts +156 -0
- package/SmartAssets/smartAssetManager.js +531 -0
- package/SmartAssets/smartAssetManager.js.map +1 -0
- package/SmartAssets/smartAssetSerializer.d.ts +61 -0
- package/SmartAssets/smartAssetSerializer.js +97 -0
- package/SmartAssets/smartAssetSerializer.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { type Scene } from "../scene.js";
|
|
2
|
+
import { type AssetContainer } from "../assetContainer.js";
|
|
3
|
+
import { type Node } from "../node.js";
|
|
4
|
+
import { type Material } from "../Materials/material.js";
|
|
5
|
+
import { type BaseTexture } from "../Materials/Textures/baseTexture.js";
|
|
6
|
+
import { type AnimationGroup } from "../Animations/animationGroup.js";
|
|
7
|
+
import { Observable, type Observer } from "../Misc/observable.js";
|
|
8
|
+
import { type ISerializedSmartAssetEntry, type ISerializedSmartAssetMap } from "./smartAssetSerializer.js";
|
|
9
|
+
/**
|
|
10
|
+
* Stateful handle for a scene's smart asset registry.
|
|
11
|
+
*
|
|
12
|
+
* Smart asset behavior is exposed through module-level functions rather than
|
|
13
|
+
* class methods so callers can import only the operations they need.
|
|
14
|
+
*/
|
|
15
|
+
export type SmartAssetManager = {
|
|
16
|
+
/**
|
|
17
|
+
* The scene this manager is attached to.
|
|
18
|
+
*/
|
|
19
|
+
readonly scene: Scene;
|
|
20
|
+
/**
|
|
21
|
+
* Fires when the smart asset registry or loaded asset state changes.
|
|
22
|
+
*/
|
|
23
|
+
readonly onChangedObservable: Observable<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Optional callback invoked when an asset cannot be found at its registered URL.
|
|
26
|
+
* Return a new URL or File to retry loading, or null to skip the asset.
|
|
27
|
+
*/
|
|
28
|
+
onAssetNotFound: ((key: string, expectedUrl: string) => Promise<string | File | null>) | null;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Optional registration data that helps select the correct loader when the URL
|
|
32
|
+
* does not contain a usable file extension, such as blob-backed local files.
|
|
33
|
+
*/
|
|
34
|
+
type SmartAssetRegistrationOptions = Pick<ISerializedSmartAssetEntry, "type" | "extension" | "metadata">;
|
|
35
|
+
/**
|
|
36
|
+
* Optional load-time configuration. Includes the persistable {@link SmartAssetRegistrationOptions}
|
|
37
|
+
* fields (type, extension, metadata) plus a transient `reloadSource` callback used by
|
|
38
|
+
* {@link ReloadSmartAssetAsync} to fetch fresh bytes from disk for blob-backed assets.
|
|
39
|
+
*/
|
|
40
|
+
export type SmartAssetLoadOptions = SmartAssetRegistrationOptions & {
|
|
41
|
+
/**
|
|
42
|
+
* Optional callback invoked by {@link ReloadSmartAssetAsync} to obtain a fresh File
|
|
43
|
+
* for the asset. Use this when the registered URL is a `blob:` URL backed by a
|
|
44
|
+
* `FileSystemFileHandle` so Reload can re-read the underlying file from disk.
|
|
45
|
+
*/
|
|
46
|
+
readonly reloadSource?: () => Promise<File>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Returns the SmartAssetManager attached to the given scene, creating and
|
|
50
|
+
* attaching one if none exists.
|
|
51
|
+
* @param scene - The scene to look up or attach a manager to.
|
|
52
|
+
* @returns The existing or newly created SmartAssetManager.
|
|
53
|
+
*/
|
|
54
|
+
export declare function GetSmartAssetManager(scene: Scene): SmartAssetManager;
|
|
55
|
+
/**
|
|
56
|
+
* Adds an observer that is notified whenever a SmartAssetManager is created.
|
|
57
|
+
* @param callback - The callback to invoke with each newly created manager.
|
|
58
|
+
* @returns The observer registration.
|
|
59
|
+
*/
|
|
60
|
+
export declare function AddSmartAssetManagerCreatedObserver(callback: (manager: SmartAssetManager) => void): Observer<SmartAssetManager>;
|
|
61
|
+
/**
|
|
62
|
+
* Registers a smart asset entry mapping a key to a URL.
|
|
63
|
+
* @param scene - The scene whose smart asset registry to update.
|
|
64
|
+
* @param key - Unique string identifier for this asset.
|
|
65
|
+
* @param url - URL or path to the asset file.
|
|
66
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
67
|
+
*/
|
|
68
|
+
export declare function RegisterSmartAsset(scene: Scene, key: string, url: string, options?: SmartAssetRegistrationOptions): void;
|
|
69
|
+
/**
|
|
70
|
+
* Removes a key from the registry. If the asset is loaded, it is unloaded first.
|
|
71
|
+
* @param scene - The scene that owns the smart asset.
|
|
72
|
+
* @param key - The key to remove.
|
|
73
|
+
* @returns A promise that resolves when the asset has been unloaded and removed.
|
|
74
|
+
*/
|
|
75
|
+
export declare function RemoveSmartAssetAsync(scene: Scene, key: string): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns all registered key-to-URL mappings.
|
|
78
|
+
* @param scene - The scene whose smart asset registry to read.
|
|
79
|
+
* @returns A read-only map of keys to URLs.
|
|
80
|
+
*/
|
|
81
|
+
export declare function GetAllSmartAssets(scene: Scene): ReadonlyMap<string, string>;
|
|
82
|
+
/**
|
|
83
|
+
* Loads a scene-file asset by key.
|
|
84
|
+
* @param scene - The scene to load the asset into.
|
|
85
|
+
* @param key - The key to load.
|
|
86
|
+
* @param url - Optional URL. If provided, the key is registered first.
|
|
87
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
88
|
+
* @returns A promise resolving to the loaded AssetContainer.
|
|
89
|
+
*/
|
|
90
|
+
export declare function LoadSmartAssetAsync(scene: Scene, key: string, url?: string, options?: SmartAssetLoadOptions): Promise<AssetContainer>;
|
|
91
|
+
/**
|
|
92
|
+
* Loads all registered assets concurrently.
|
|
93
|
+
* @param scene - The scene whose registered assets to load.
|
|
94
|
+
* @returns A promise resolving to loaded scene-file containers.
|
|
95
|
+
*/
|
|
96
|
+
export declare function LoadAllSmartAssetsAsync(scene: Scene): Promise<AssetContainer[]>;
|
|
97
|
+
/**
|
|
98
|
+
* Loads a standalone texture by key.
|
|
99
|
+
* @param scene - The scene to load the texture into.
|
|
100
|
+
* @param key - The key to load.
|
|
101
|
+
* @param url - Optional URL. If provided, the key is registered first.
|
|
102
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
103
|
+
* @returns A promise resolving to the loaded texture.
|
|
104
|
+
*/
|
|
105
|
+
export declare function LoadSmartAssetTextureAsync(scene: Scene, key: string, url?: string, options?: SmartAssetLoadOptions): Promise<BaseTexture>;
|
|
106
|
+
/**
|
|
107
|
+
* Unloads a loaded asset while keeping the key registered.
|
|
108
|
+
* @param scene - The scene whose smart asset to unload.
|
|
109
|
+
* @param key - The key to unload.
|
|
110
|
+
* @returns A promise that resolves once the asset has been unloaded.
|
|
111
|
+
*/
|
|
112
|
+
export declare function UnloadSmartAssetAsync(scene: Scene, key: string): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Unloads and re-loads an asset.
|
|
115
|
+
* @param scene - The scene whose smart asset to reload.
|
|
116
|
+
* @param key - The key to reload.
|
|
117
|
+
* @returns A promise resolving to the newly loaded AssetContainer or BaseTexture.
|
|
118
|
+
*/
|
|
119
|
+
export declare function ReloadSmartAssetAsync(scene: Scene, key: string): Promise<AssetContainer | BaseTexture>;
|
|
120
|
+
/**
|
|
121
|
+
* Finds which smart asset key owns a scene object.
|
|
122
|
+
* @param scene - The scene whose registry to search.
|
|
123
|
+
* @param object - A scene object.
|
|
124
|
+
* @returns The key, or undefined if the object is not tracked.
|
|
125
|
+
*/
|
|
126
|
+
export declare function FindSmartAssetKeyForObject(scene: Scene, object: Node | Material | BaseTexture | AnimationGroup): string | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* Serializes the registry to a JSON-compatible document.
|
|
129
|
+
* If a baseUrl is provided, asset URLs are stored relative to it for portability.
|
|
130
|
+
* @param scene - The scene whose registry to serialize.
|
|
131
|
+
* @param baseUrl - Optional base URL for making asset paths relative.
|
|
132
|
+
* @returns A serialized asset map document.
|
|
133
|
+
*/
|
|
134
|
+
export declare function SerializeSmartAssetManagerMap(scene: Scene, baseUrl?: string): ISerializedSmartAssetMap;
|
|
135
|
+
/**
|
|
136
|
+
* Loads an asset map from a URL, File, or pre-parsed JSON object.
|
|
137
|
+
* @param scene - The scene to load assets into.
|
|
138
|
+
* @param source - A URL string, File object, or pre-parsed ISerializedSmartAssetMap.
|
|
139
|
+
* @param rootUrl - Optional root URL for resolving relative asset paths.
|
|
140
|
+
* @returns A promise that resolves after the map has been loaded and all registered assets have been attempted.
|
|
141
|
+
*/
|
|
142
|
+
export declare function LoadSmartAssetMapAsync(scene: Scene, source: string | File | ISerializedSmartAssetMap, rootUrl?: string): Promise<void>;
|
|
143
|
+
/**
|
|
144
|
+
* Disposes the manager, unloading all assets and detaching it from its scene.
|
|
145
|
+
* Safe to call multiple times; subsequent calls are no-ops. Automatically invoked when the
|
|
146
|
+
* owning scene is disposed.
|
|
147
|
+
* @param manager - The smart asset manager state.
|
|
148
|
+
*/
|
|
149
|
+
export declare function DisposeSmartAssetManager(manager: SmartAssetManager): void;
|
|
150
|
+
/**
|
|
151
|
+
* Returns the set of file extensions (including the leading dot) that {@link LoadAllSmartAssetsAsync}
|
|
152
|
+
* treats as standalone textures.
|
|
153
|
+
* @returns A read-only set of texture file extensions.
|
|
154
|
+
*/
|
|
155
|
+
export declare function GetSmartAssetTextureExtensions(): ReadonlySet<string>;
|
|
156
|
+
export {};
|
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { Texture } from "../Materials/Textures/texture.js";
|
|
2
|
+
import { CubeTexture } from "../Materials/Textures/cubeTexture.js";
|
|
3
|
+
import { HDRCubeTexture } from "../Materials/Textures/hdrCubeTexture.js";
|
|
4
|
+
import { Observable } from "../Misc/observable.js";
|
|
5
|
+
import { Logger } from "../Misc/logger.js";
|
|
6
|
+
import { LoadAssetContainerAsync } from "../Loading/sceneLoader.js";
|
|
7
|
+
import { GetExtensionFromUrl } from "../Misc/urlTools.js";
|
|
8
|
+
import { DeserializeSmartAssetMap, IsAbsoluteOrSpecialUrl, MakeRelative, ResolveAssetUrl, ReadJsonSourceAsync, } from "./smartAssetSerializer.js";
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
+
const SMART_ASSET_MANAGER_KEY = Symbol.for("babylonjs:smartAssetManager");
|
|
11
|
+
const SmartAssetManagerInternals = new WeakMap();
|
|
12
|
+
const OnSmartAssetManagerCreatedObservable = new Observable();
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new SmartAssetManager state object and attaches it to the scene.
|
|
15
|
+
*
|
|
16
|
+
* Internal: callers should use {@link GetSmartAssetManager} which returns the
|
|
17
|
+
* existing manager when one is already attached.
|
|
18
|
+
* @param scene - The scene this manager operates on.
|
|
19
|
+
* @returns The created smart asset manager state.
|
|
20
|
+
*/
|
|
21
|
+
function CreateSmartAssetManager(scene) {
|
|
22
|
+
const manager = {
|
|
23
|
+
scene,
|
|
24
|
+
onChangedObservable: new Observable(),
|
|
25
|
+
onAssetNotFound: null,
|
|
26
|
+
};
|
|
27
|
+
const internal = {
|
|
28
|
+
urls: new Map(),
|
|
29
|
+
options: new Map(),
|
|
30
|
+
containers: new Map(),
|
|
31
|
+
objectToKeyMap: new WeakMap(),
|
|
32
|
+
textureKeys: new Set(),
|
|
33
|
+
reloadSources: new Map(),
|
|
34
|
+
blobUrls: new Map(),
|
|
35
|
+
sceneDisposeObserver: null,
|
|
36
|
+
};
|
|
37
|
+
SmartAssetManagerInternals.set(manager, internal);
|
|
38
|
+
if (!scene.metadata) {
|
|
39
|
+
scene.metadata = {};
|
|
40
|
+
}
|
|
41
|
+
scene.metadata[SMART_ASSET_MANAGER_KEY] = manager;
|
|
42
|
+
// Auto-dispose when the scene is disposed so the manager doesn't outlive it.
|
|
43
|
+
internal.sceneDisposeObserver = scene.onDisposeObservable.add(() => DisposeSmartAssetManager(manager));
|
|
44
|
+
OnSmartAssetManagerCreatedObservable.notifyObservers(manager);
|
|
45
|
+
return manager;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns the SmartAssetManager attached to the given scene, creating and
|
|
49
|
+
* attaching one if none exists.
|
|
50
|
+
* @param scene - The scene to look up or attach a manager to.
|
|
51
|
+
* @returns The existing or newly created SmartAssetManager.
|
|
52
|
+
*/
|
|
53
|
+
export function GetSmartAssetManager(scene) {
|
|
54
|
+
const existing = scene.metadata?.[SMART_ASSET_MANAGER_KEY];
|
|
55
|
+
if (existing) {
|
|
56
|
+
return existing;
|
|
57
|
+
}
|
|
58
|
+
return CreateSmartAssetManager(scene);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Adds an observer that is notified whenever a SmartAssetManager is created.
|
|
62
|
+
* @param callback - The callback to invoke with each newly created manager.
|
|
63
|
+
* @returns The observer registration.
|
|
64
|
+
*/
|
|
65
|
+
export function AddSmartAssetManagerCreatedObserver(callback) {
|
|
66
|
+
// Wrap so the EventState second-arg from Observable.add isn't passed through to the caller.
|
|
67
|
+
return OnSmartAssetManagerCreatedObservable.add((manager) => callback(manager));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Registers a smart asset entry mapping a key to a URL.
|
|
71
|
+
* @param scene - The scene whose smart asset registry to update.
|
|
72
|
+
* @param key - Unique string identifier for this asset.
|
|
73
|
+
* @param url - URL or path to the asset file.
|
|
74
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
75
|
+
*/
|
|
76
|
+
export function RegisterSmartAsset(scene, key, url, options) {
|
|
77
|
+
const manager = GetSmartAssetManager(scene);
|
|
78
|
+
const internal = GetSmartAssetInternals(manager);
|
|
79
|
+
RevokeManagedBlobUrl(internal, key, url);
|
|
80
|
+
internal.urls.set(key, url);
|
|
81
|
+
TrackManagedBlobUrl(internal, key, url);
|
|
82
|
+
if (options) {
|
|
83
|
+
const existingOptions = internal.options.get(key);
|
|
84
|
+
internal.options.set(key, { ...existingOptions, ...options });
|
|
85
|
+
if (options.type === "texture") {
|
|
86
|
+
internal.textureKeys.add(key);
|
|
87
|
+
}
|
|
88
|
+
else if (options.type !== undefined) {
|
|
89
|
+
internal.textureKeys.delete(key);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
manager.onChangedObservable.notifyObservers();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Removes a key from the registry. If the asset is loaded, it is unloaded first.
|
|
96
|
+
* @param scene - The scene that owns the smart asset.
|
|
97
|
+
* @param key - The key to remove.
|
|
98
|
+
* @returns A promise that resolves when the asset has been unloaded and removed.
|
|
99
|
+
*/
|
|
100
|
+
export async function RemoveSmartAssetAsync(scene, key) {
|
|
101
|
+
const manager = GetSmartAssetManager(scene);
|
|
102
|
+
const internal = GetSmartAssetInternals(manager);
|
|
103
|
+
if (internal.containers.has(key)) {
|
|
104
|
+
await UnloadSmartAssetAsync(scene, key);
|
|
105
|
+
}
|
|
106
|
+
for (const tex of [...scene.textures]) {
|
|
107
|
+
if (internal.objectToKeyMap.get(tex) === key) {
|
|
108
|
+
internal.objectToKeyMap.delete(tex);
|
|
109
|
+
tex.dispose();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
internal.urls.delete(key);
|
|
113
|
+
internal.options.delete(key);
|
|
114
|
+
internal.textureKeys.delete(key);
|
|
115
|
+
internal.reloadSources.delete(key);
|
|
116
|
+
RevokeManagedBlobUrl(internal, key);
|
|
117
|
+
manager.onChangedObservable.notifyObservers();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns all registered key-to-URL mappings.
|
|
121
|
+
* @param scene - The scene whose smart asset registry to read.
|
|
122
|
+
* @returns A read-only map of keys to URLs.
|
|
123
|
+
*/
|
|
124
|
+
export function GetAllSmartAssets(scene) {
|
|
125
|
+
return GetSmartAssetInternals(GetSmartAssetManager(scene)).urls;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Loads a scene-file asset by key.
|
|
129
|
+
* @param scene - The scene to load the asset into.
|
|
130
|
+
* @param key - The key to load.
|
|
131
|
+
* @param url - Optional URL. If provided, the key is registered first.
|
|
132
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
133
|
+
* @returns A promise resolving to the loaded AssetContainer.
|
|
134
|
+
*/
|
|
135
|
+
export async function LoadSmartAssetAsync(scene, key, url, options) {
|
|
136
|
+
const manager = GetSmartAssetManager(scene);
|
|
137
|
+
const internal = GetSmartAssetInternals(manager);
|
|
138
|
+
const previousUrl = internal.urls.get(key);
|
|
139
|
+
const { reloadSource, ...registrationOptions } = options ?? {};
|
|
140
|
+
if (url) {
|
|
141
|
+
RegisterSmartAsset(scene, key, url, registrationOptions);
|
|
142
|
+
}
|
|
143
|
+
if (reloadSource) {
|
|
144
|
+
internal.reloadSources.set(key, reloadSource);
|
|
145
|
+
}
|
|
146
|
+
const resolvedUrl = internal.urls.get(key);
|
|
147
|
+
if (!resolvedUrl) {
|
|
148
|
+
throw new Error(`SmartAssetManager: Key "${key}" is not registered. Provide a URL to auto-register.`);
|
|
149
|
+
}
|
|
150
|
+
const existing = internal.containers.get(key);
|
|
151
|
+
if (existing) {
|
|
152
|
+
if (url && url !== previousUrl) {
|
|
153
|
+
// URL changed — drop the stale container before fetching the new one
|
|
154
|
+
// so callers don't get a surprise cached return for an updated URL.
|
|
155
|
+
await UnloadSmartAssetAsync(scene, key);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return existing;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return await LoadSmartAssetSceneFileAsync(manager, key, resolvedUrl, internal.options.get(key)?.extension);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Loads all registered assets concurrently.
|
|
165
|
+
* @param scene - The scene whose registered assets to load.
|
|
166
|
+
* @returns A promise resolving to loaded scene-file containers.
|
|
167
|
+
*/
|
|
168
|
+
export async function LoadAllSmartAssetsAsync(scene) {
|
|
169
|
+
const manager = GetSmartAssetManager(scene);
|
|
170
|
+
const internal = GetSmartAssetInternals(manager);
|
|
171
|
+
const scenePromises = [];
|
|
172
|
+
const texturePromises = [];
|
|
173
|
+
for (const [key, url] of Array.from(internal.urls)) {
|
|
174
|
+
if (internal.containers.has(key)) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const options = internal.options.get(key);
|
|
178
|
+
if (internal.textureKeys.has(key) || options?.type === "texture" || IsTextureUrl(url) || IsTextureExtension(options?.extension)) {
|
|
179
|
+
const textureLoadAsync = async () => {
|
|
180
|
+
try {
|
|
181
|
+
await LoadSmartAssetTextureAsync(scene, key);
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
Logger.Warn(`SmartAssetManager: Texture "${key}" could not be loaded — skipping.`);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
texturePromises.push(textureLoadAsync());
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const sceneLoadAsync = async () => {
|
|
191
|
+
try {
|
|
192
|
+
return await LoadSmartAssetAsync(scene, key);
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
Logger.Warn(`SmartAssetManager: Asset "${key}" could not be loaded — skipping.`);
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
scenePromises.push(sceneLoadAsync());
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
await Promise.all(texturePromises);
|
|
203
|
+
const results = await Promise.all(scenePromises);
|
|
204
|
+
return results.filter((r) => r !== null);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Loads a standalone texture by key.
|
|
208
|
+
* @param scene - The scene to load the texture into.
|
|
209
|
+
* @param key - The key to load.
|
|
210
|
+
* @param url - Optional URL. If provided, the key is registered first.
|
|
211
|
+
* @param options - Optional loader hints and metadata for this asset.
|
|
212
|
+
* @returns A promise resolving to the loaded texture.
|
|
213
|
+
*/
|
|
214
|
+
export async function LoadSmartAssetTextureAsync(scene, key, url, options) {
|
|
215
|
+
const manager = GetSmartAssetManager(scene);
|
|
216
|
+
const internal = GetSmartAssetInternals(manager);
|
|
217
|
+
const previousUrl = internal.urls.get(key);
|
|
218
|
+
const { reloadSource, ...registrationOptions } = options ?? {};
|
|
219
|
+
if (url) {
|
|
220
|
+
RegisterSmartAsset(scene, key, url, { ...registrationOptions, type: registrationOptions.type ?? "texture" });
|
|
221
|
+
}
|
|
222
|
+
if (reloadSource) {
|
|
223
|
+
internal.reloadSources.set(key, reloadSource);
|
|
224
|
+
}
|
|
225
|
+
internal.textureKeys.add(key);
|
|
226
|
+
// Mirror LoadSmartAssetAsync: if a tracked texture already exists for this key,
|
|
227
|
+
// return it on a same-URL call (cache hit) or dispose it before reload on URL change.
|
|
228
|
+
// Without this guard, calling LoadSmartAssetTextureAsync twice with the same URL
|
|
229
|
+
// would create duplicate Texture objects in scene.textures.
|
|
230
|
+
const existingTextures = [];
|
|
231
|
+
for (const tex of scene.textures) {
|
|
232
|
+
if (internal.objectToKeyMap.get(tex) === key) {
|
|
233
|
+
existingTextures.push(tex);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (existingTextures.length > 0) {
|
|
237
|
+
if (url && previousUrl !== undefined && url !== previousUrl) {
|
|
238
|
+
for (const tex of existingTextures) {
|
|
239
|
+
internal.objectToKeyMap.delete(tex);
|
|
240
|
+
tex.dispose();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
return existingTextures[0];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const resolvedUrl = internal.urls.get(key);
|
|
248
|
+
if (!resolvedUrl) {
|
|
249
|
+
throw new Error(`SmartAssetManager: Key "${key}" is not registered. Provide a URL to auto-register.`);
|
|
250
|
+
}
|
|
251
|
+
const extensionHint = internal.options.get(key)?.extension;
|
|
252
|
+
let texture;
|
|
253
|
+
try {
|
|
254
|
+
texture = await CreateAndLoadTextureAsync(manager, resolvedUrl, extensionHint);
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
const fallback = await ResolveNotFoundAsync(manager, key, resolvedUrl);
|
|
258
|
+
if (!fallback) {
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
texture = await CreateAndLoadTextureAsync(manager, fallback.url, fallback.extensionHint ?? extensionHint);
|
|
262
|
+
}
|
|
263
|
+
internal.objectToKeyMap.set(texture, key);
|
|
264
|
+
manager.onChangedObservable.notifyObservers();
|
|
265
|
+
return texture;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Unloads a loaded asset while keeping the key registered.
|
|
269
|
+
* @param scene - The scene whose smart asset to unload.
|
|
270
|
+
* @param key - The key to unload.
|
|
271
|
+
* @returns A promise that resolves once the asset has been unloaded.
|
|
272
|
+
*/
|
|
273
|
+
export async function UnloadSmartAssetAsync(scene, key) {
|
|
274
|
+
const manager = GetSmartAssetManager(scene);
|
|
275
|
+
const internal = GetSmartAssetInternals(manager);
|
|
276
|
+
const container = internal.containers.get(key);
|
|
277
|
+
if (container) {
|
|
278
|
+
container.removeAllFromScene();
|
|
279
|
+
container.dispose();
|
|
280
|
+
internal.containers.delete(key);
|
|
281
|
+
manager.onChangedObservable.notifyObservers();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
for (const tex of [...scene.textures]) {
|
|
285
|
+
if (internal.objectToKeyMap.get(tex) === key) {
|
|
286
|
+
internal.objectToKeyMap.delete(tex);
|
|
287
|
+
tex.dispose();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
manager.onChangedObservable.notifyObservers();
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Unloads and re-loads an asset.
|
|
294
|
+
* @param scene - The scene whose smart asset to reload.
|
|
295
|
+
* @param key - The key to reload.
|
|
296
|
+
* @returns A promise resolving to the newly loaded AssetContainer or BaseTexture.
|
|
297
|
+
*/
|
|
298
|
+
export async function ReloadSmartAssetAsync(scene, key) {
|
|
299
|
+
const internal = GetSmartAssetInternals(GetSmartAssetManager(scene));
|
|
300
|
+
const reloadSource = internal.reloadSources.get(key);
|
|
301
|
+
if (reloadSource) {
|
|
302
|
+
try {
|
|
303
|
+
const freshFile = await reloadSource();
|
|
304
|
+
const blobUrl = URL.createObjectURL(freshFile);
|
|
305
|
+
RegisterSmartAsset(scene, key, blobUrl, { extension: GetExtensionFromUrl(freshFile.name) || internal.options.get(key)?.extension });
|
|
306
|
+
}
|
|
307
|
+
catch (e) {
|
|
308
|
+
Logger.Warn(`SmartAssetManager: reloadSource callback failed for "${key}": ${e}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
await UnloadSmartAssetAsync(scene, key);
|
|
312
|
+
if (internal.textureKeys.has(key)) {
|
|
313
|
+
return await LoadSmartAssetTextureAsync(scene, key);
|
|
314
|
+
}
|
|
315
|
+
return await LoadSmartAssetAsync(scene, key);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Finds which smart asset key owns a scene object.
|
|
319
|
+
* @param scene - The scene whose registry to search.
|
|
320
|
+
* @param object - A scene object.
|
|
321
|
+
* @returns The key, or undefined if the object is not tracked.
|
|
322
|
+
*/
|
|
323
|
+
export function FindSmartAssetKeyForObject(scene, object) {
|
|
324
|
+
return GetSmartAssetInternals(GetSmartAssetManager(scene)).objectToKeyMap.get(object);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Serializes the registry to a JSON-compatible document.
|
|
328
|
+
* If a baseUrl is provided, asset URLs are stored relative to it for portability.
|
|
329
|
+
* @param scene - The scene whose registry to serialize.
|
|
330
|
+
* @param baseUrl - Optional base URL for making asset paths relative.
|
|
331
|
+
* @returns A serialized asset map document.
|
|
332
|
+
*/
|
|
333
|
+
export function SerializeSmartAssetManagerMap(scene, baseUrl) {
|
|
334
|
+
const internal = GetSmartAssetInternals(GetSmartAssetManager(scene));
|
|
335
|
+
const assets = {};
|
|
336
|
+
for (const [key, registeredUrl] of Array.from(internal.urls)) {
|
|
337
|
+
let url = registeredUrl;
|
|
338
|
+
if (baseUrl && !IsAbsoluteOrSpecialUrl(url)) {
|
|
339
|
+
url = MakeRelative(url, baseUrl);
|
|
340
|
+
}
|
|
341
|
+
const options = internal.options.get(key);
|
|
342
|
+
assets[key] = { url, ...options, ...(internal.textureKeys.has(key) ? { type: "texture" } : {}) };
|
|
343
|
+
}
|
|
344
|
+
return { version: 1, assets };
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Loads an asset map from a URL, File, or pre-parsed JSON object.
|
|
348
|
+
* @param scene - The scene to load assets into.
|
|
349
|
+
* @param source - A URL string, File object, or pre-parsed ISerializedSmartAssetMap.
|
|
350
|
+
* @param rootUrl - Optional root URL for resolving relative asset paths.
|
|
351
|
+
* @returns A promise that resolves after the map has been loaded and all registered assets have been attempted.
|
|
352
|
+
*/
|
|
353
|
+
export async function LoadSmartAssetMapAsync(scene, source, rootUrl) {
|
|
354
|
+
let resolvedRootUrl = rootUrl ?? "";
|
|
355
|
+
if (typeof source === "string" && !rootUrl) {
|
|
356
|
+
const { Tools } = await import("../Misc/tools.js");
|
|
357
|
+
resolvedRootUrl = Tools.GetFolderPath(source);
|
|
358
|
+
}
|
|
359
|
+
const raw = await ReadJsonSourceAsync(source);
|
|
360
|
+
const doc = DeserializeSmartAssetMap(raw);
|
|
361
|
+
for (const [key, entry] of Object.entries(doc.assets)) {
|
|
362
|
+
const resolved = resolvedRootUrl ? ResolveAssetUrl(entry.url, resolvedRootUrl) : entry.url;
|
|
363
|
+
RegisterSmartAsset(scene, key, resolved, { type: entry.type, extension: entry.extension, metadata: entry.metadata });
|
|
364
|
+
}
|
|
365
|
+
await LoadAllSmartAssetsAsync(scene);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Registers an externally loaded AssetContainer under a key.
|
|
369
|
+
* @param manager - The smart asset manager state.
|
|
370
|
+
* @param key - The key to associate with the container.
|
|
371
|
+
* @param container - The loaded AssetContainer.
|
|
372
|
+
*/
|
|
373
|
+
function TrackLoadedSmartAssetContainer(manager, key, container) {
|
|
374
|
+
const internal = GetSmartAssetInternals(manager);
|
|
375
|
+
internal.containers.set(key, container);
|
|
376
|
+
TrackSmartAssetContainerObjects(manager, key, container);
|
|
377
|
+
manager.onChangedObservable.notifyObservers();
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Disposes the manager, unloading all assets and detaching it from its scene.
|
|
381
|
+
* Safe to call multiple times; subsequent calls are no-ops. Automatically invoked when the
|
|
382
|
+
* owning scene is disposed.
|
|
383
|
+
* @param manager - The smart asset manager state.
|
|
384
|
+
*/
|
|
385
|
+
export function DisposeSmartAssetManager(manager) {
|
|
386
|
+
const internal = SmartAssetManagerInternals.get(manager);
|
|
387
|
+
if (!internal) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
SmartAssetManagerInternals.delete(manager);
|
|
391
|
+
if (internal.sceneDisposeObserver) {
|
|
392
|
+
manager.scene.onDisposeObservable.remove(internal.sceneDisposeObserver);
|
|
393
|
+
internal.sceneDisposeObserver = null;
|
|
394
|
+
}
|
|
395
|
+
for (const container of Array.from(internal.containers.values())) {
|
|
396
|
+
container.removeAllFromScene();
|
|
397
|
+
container.dispose();
|
|
398
|
+
}
|
|
399
|
+
for (const tex of [...manager.scene.textures]) {
|
|
400
|
+
if (internal.objectToKeyMap.get(tex) !== undefined) {
|
|
401
|
+
tex.dispose();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
internal.urls.clear();
|
|
405
|
+
internal.options.clear();
|
|
406
|
+
internal.textureKeys.clear();
|
|
407
|
+
internal.reloadSources.clear();
|
|
408
|
+
internal.containers.clear();
|
|
409
|
+
for (const blobUrl of Array.from(internal.blobUrls.values())) {
|
|
410
|
+
URL.revokeObjectURL(blobUrl);
|
|
411
|
+
}
|
|
412
|
+
internal.blobUrls.clear();
|
|
413
|
+
manager.onChangedObservable.clear();
|
|
414
|
+
if (manager.scene.metadata) {
|
|
415
|
+
delete manager.scene.metadata[SMART_ASSET_MANAGER_KEY];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
function GetSmartAssetInternals(manager) {
|
|
419
|
+
const internal = SmartAssetManagerInternals.get(manager);
|
|
420
|
+
if (!internal) {
|
|
421
|
+
throw new Error("SmartAssetManager: Unknown manager state.");
|
|
422
|
+
}
|
|
423
|
+
return internal;
|
|
424
|
+
}
|
|
425
|
+
function RevokeManagedBlobUrl(internal, key, replacementUrl) {
|
|
426
|
+
const blobUrl = internal.blobUrls.get(key);
|
|
427
|
+
if (!blobUrl || blobUrl === replacementUrl) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
URL.revokeObjectURL(blobUrl);
|
|
431
|
+
internal.blobUrls.delete(key);
|
|
432
|
+
}
|
|
433
|
+
function TrackManagedBlobUrl(internal, key, url) {
|
|
434
|
+
if (url.startsWith("blob:")) {
|
|
435
|
+
internal.blobUrls.set(key, url);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function CreateAndLoadTextureAsync(manager, url, extensionHint) {
|
|
439
|
+
return await new Promise((resolve, reject) => {
|
|
440
|
+
const ext = (extensionHint || GetExtensionFromUrl(url)).toLowerCase();
|
|
441
|
+
const onError = (message, exception) => {
|
|
442
|
+
const err = exception instanceof Error ? exception : new Error(message ?? `SmartAssetManager: failed to load texture from "${url}".`);
|
|
443
|
+
reject(err);
|
|
444
|
+
};
|
|
445
|
+
let texture;
|
|
446
|
+
const onLoad = () => resolve(texture);
|
|
447
|
+
if (ext === ".hdr") {
|
|
448
|
+
// HDR equirectangular files require HDRCubeTexture — CubeTexture's .hdr
|
|
449
|
+
// loader explicitly throws ".hdr not supported in Cube." so we can't
|
|
450
|
+
// route HDRs through the generic CubeTexture path.
|
|
451
|
+
texture = new HDRCubeTexture(url, manager.scene, 256, false, true, false, false, onLoad, onError);
|
|
452
|
+
}
|
|
453
|
+
else if (ext === ".env" || ext === ".dds") {
|
|
454
|
+
texture = new CubeTexture(url, manager.scene, null, false, null, onLoad, onError, undefined, ext === ".env");
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
texture = new Texture(url, manager.scene, undefined, undefined, undefined, onLoad, onError);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
async function ResolveNotFoundAsync(manager, key, expectedUrl) {
|
|
462
|
+
if (!manager.onAssetNotFound) {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
const resolution = await manager.onAssetNotFound(key, expectedUrl);
|
|
466
|
+
if (resolution === null || resolution === undefined) {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
if (typeof resolution === "string") {
|
|
470
|
+
RegisterSmartAsset(manager.scene, key, resolution);
|
|
471
|
+
return { url: resolution, extensionHint: GetSmartAssetInternals(manager).options.get(key)?.extension };
|
|
472
|
+
}
|
|
473
|
+
const blobUrl = URL.createObjectURL(resolution);
|
|
474
|
+
const extensionHint = GetExtensionFromUrl(resolution.name) || undefined;
|
|
475
|
+
RegisterSmartAsset(manager.scene, key, blobUrl, { extension: extensionHint });
|
|
476
|
+
return { url: blobUrl, extensionHint };
|
|
477
|
+
}
|
|
478
|
+
async function LoadSmartAssetSceneFileAsync(manager, key, url, extensionHint) {
|
|
479
|
+
const loadAsync = async (loadUrl, extensionHint) => {
|
|
480
|
+
const container = await LoadAssetContainerAsync(loadUrl, manager.scene, { pluginExtension: extensionHint });
|
|
481
|
+
container.addAllToScene();
|
|
482
|
+
TrackLoadedSmartAssetContainer(manager, key, container);
|
|
483
|
+
return container;
|
|
484
|
+
};
|
|
485
|
+
try {
|
|
486
|
+
return await loadAsync(url, extensionHint);
|
|
487
|
+
}
|
|
488
|
+
catch (error) {
|
|
489
|
+
const fallback = await ResolveNotFoundAsync(manager, key, url);
|
|
490
|
+
if (fallback) {
|
|
491
|
+
try {
|
|
492
|
+
return await loadAsync(fallback.url, fallback.extensionHint);
|
|
493
|
+
}
|
|
494
|
+
catch (retryError) {
|
|
495
|
+
Logger.Warn(`SmartAssetManager: Asset "${key}" could not be loaded from fallback "${fallback.url}".`);
|
|
496
|
+
throw retryError;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
Logger.Warn(`SmartAssetManager: Asset "${key}" could not be loaded from "${url}".`);
|
|
500
|
+
throw error;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function TrackSmartAssetContainerObjects(manager, key, container) {
|
|
504
|
+
const internal = GetSmartAssetInternals(manager);
|
|
505
|
+
for (const collection of [container.meshes, container.materials, container.textures, container.animationGroups, container.lights, container.cameras]) {
|
|
506
|
+
for (const obj of collection) {
|
|
507
|
+
internal.objectToKeyMap.set(obj, key);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const TextureExtensions = new Set([".png", ".jpg", ".jpeg", ".bmp", ".tga", ".gif", ".webp", ".env", ".hdr", ".dds", ".ktx", ".ktx2", ".basis"]);
|
|
512
|
+
/**
|
|
513
|
+
* Returns the set of file extensions (including the leading dot) that {@link LoadAllSmartAssetsAsync}
|
|
514
|
+
* treats as standalone textures.
|
|
515
|
+
* @returns A read-only set of texture file extensions.
|
|
516
|
+
*/
|
|
517
|
+
export function GetSmartAssetTextureExtensions() {
|
|
518
|
+
return TextureExtensions;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Returns true if the URL points to a standalone texture file.
|
|
522
|
+
* @param url - The URL to check.
|
|
523
|
+
* @returns True if the URL has a texture file extension.
|
|
524
|
+
*/
|
|
525
|
+
function IsTextureUrl(url) {
|
|
526
|
+
return TextureExtensions.has(GetExtensionFromUrl(url));
|
|
527
|
+
}
|
|
528
|
+
function IsTextureExtension(extension) {
|
|
529
|
+
return extension !== undefined && TextureExtensions.has(extension.toLowerCase());
|
|
530
|
+
}
|
|
531
|
+
//# sourceMappingURL=smartAssetManager.js.map
|