UNPKG

@babylonjs/loaders

Version:

For usage documentation please visit https://doc.babylonjs.com/features/featuresDeepDive/importers/loadingFileTypes/.

999 lines 42.4 kB
import { Observable } from "@babylonjs/core/Misc/observable.js"; import { Tools } from "@babylonjs/core/Misc/tools.js"; import { RegisterSceneLoaderPlugin } from "@babylonjs/core/Loading/sceneLoader.js"; import { AssetContainer } from "@babylonjs/core/assetContainer.js"; import { Logger } from "@babylonjs/core/Misc/logger.js"; import { DataReader } from "@babylonjs/core/Misc/dataReader.js"; import { GLTFValidation } from "./glTFValidation.js"; import { GLTFFileLoaderMetadata, GLTFMagicBase64Encoded } from "./glTFFileLoader.metadata.js"; import { DecodeBase64UrlToBinary } from "@babylonjs/core/Misc/fileTools.js"; import { RuntimeError, ErrorCodes } from "@babylonjs/core/Misc/error.js"; function readAsync(arrayBuffer, byteOffset, byteLength) { try { return Promise.resolve(new Uint8Array(arrayBuffer, byteOffset, byteLength)); } catch (e) { return Promise.reject(e); } } function readViewAsync(arrayBufferView, byteOffset, byteLength) { try { if (byteOffset < 0 || byteOffset >= arrayBufferView.byteLength) { throw new RangeError("Offset is out of range."); } if (byteOffset + byteLength > arrayBufferView.byteLength) { throw new RangeError("Length is out of range."); } return Promise.resolve(new Uint8Array(arrayBufferView.buffer, arrayBufferView.byteOffset + byteOffset, byteLength)); } catch (e) { return Promise.reject(e); } } /** * Mode that determines the coordinate system to use. */ export var GLTFLoaderCoordinateSystemMode; (function (GLTFLoaderCoordinateSystemMode) { /** * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene. */ GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO"; /** * Sets the useRightHandedSystem flag on the scene. */ GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED"; })(GLTFLoaderCoordinateSystemMode || (GLTFLoaderCoordinateSystemMode = {})); /** * Mode that determines what animations will start. */ export var GLTFLoaderAnimationStartMode; (function (GLTFLoaderAnimationStartMode) { /** * No animation will start. */ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE"; /** * The first animation will start. */ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST"; /** * All animations will start. */ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL"; })(GLTFLoaderAnimationStartMode || (GLTFLoaderAnimationStartMode = {})); /** * Loader state. */ export var GLTFLoaderState; (function (GLTFLoaderState) { /** * The asset is loading. */ GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING"; /** * The asset is ready for rendering. */ GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY"; /** * The asset is completely loaded. */ GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE"; })(GLTFLoaderState || (GLTFLoaderState = {})); /** * This class contains all the concrete (not abstract) glTF options, excluding callbacks. * The purpose of this class is to make it easy to provide a way to mutate the default * loader options (see the GLTFLoaderDefaultOptions instance below) without duplicating * all the options in yet another object. Since this class is instantiated for the default * options object, abstract properties and callbacks are not included, it's more just * flag-type options. */ class GLTFLoaderBaseOptions { constructor() { /** * Defines if the loader should always compute the bounding boxes of meshes and not use the min/max values from the position accessor. Defaults to false. */ this.alwaysComputeBoundingBox = false; /** * Defines if the loader should always compute the nearest common ancestor of the skeleton joints instead of using `skin.skeleton`. Defaults to false. * Set this to true if loading assets with invalid `skin.skeleton` values. */ this.alwaysComputeSkeletonRootNode = false; /** * The animation start mode. Defaults to FIRST. */ this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST; /** * Defines if the loader should compile materials before raising the success callback. Defaults to false. */ this.compileMaterials = false; /** * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false. */ this.compileShadowGenerators = false; /** * The coordinate system mode. Defaults to AUTO. */ this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO; /** * Defines if the loader should create instances when multiple glTF nodes point to the same glTF mesh. Defaults to true. */ this.createInstances = true; /** * If true, load all materials defined in the file, even if not used by any mesh. Defaults to false. */ this.loadAllMaterials = false; /** * Defines if the loader should load morph targets. Defaults to true. */ this.loadMorphTargets = true; /** * Defines if the loader should load node animations. Defaults to true. * NOTE: The animation of this node will still load if the node is also a joint of a skin and `loadSkins` is true. */ this.loadNodeAnimations = true; /** * If true, load only the materials defined in the file. Defaults to false. */ this.loadOnlyMaterials = false; /** * Defines if the loader should load skins. Defaults to true. */ this.loadSkins = true; /** * If true, do not load any materials defined in the file. Defaults to false. */ this.skipMaterials = false; /** * When loading glTF animations, which are defined in seconds, target them to this FPS. Defaults to 60. */ this.targetFps = 60; /** * Defines if the Alpha blended materials are only applied as coverage. * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials. * If true, no extra effects are applied to transparent pixels. */ this.transparencyAsCoverage = false; /** * Defines if the loader should also compile materials with clip planes. Defaults to false. */ this.useClipPlane = false; /** * If true, the loader will derive the name for Babylon textures from the glTF texture name, image name, or image url. Defaults to false. * Note that it is possible for multiple Babylon textures to share the same name when the Babylon textures load from the same glTF texture or image. */ this.useGltfTextureNames = false; /** * Defines if the loader should use range requests when load binary glTF files from HTTP. * Enabling will disable offline support and glTF validator. * Defaults to false. */ this.useRangeRequests = false; /** * If true, load the color (gamma encoded) textures into sRGB buffers (if supported by the GPU), which will yield more accurate results when sampling the texture. Defaults to true. */ this.useSRGBBuffers = true; /** * Defines if the loader should validate the asset. */ this.validate = false; /** * Load the glTF files using the OpenPBR material. * @experimental */ this.useOpenPBR = false; /** * If true, the loader will not use the transmission helper when loading materials with transmission. */ this.dontUseTransmissionHelper = false; } } /** * The default GLTF loader options. * Override the properties of this object to globally change the default loader options. * To specify options for a specific load call, pass those options into the associated load function. */ export const GLTFLoaderDefaultOptions = new GLTFLoaderBaseOptions(); class GLTFLoaderOptions extends GLTFLoaderBaseOptions { constructor() { super(...arguments); /** * Defines options for glTF extensions. */ this.extensionOptions = {}; /** * Function called before loading a url referenced by the asset. * @param url url referenced by the asset * @returns Async url to load */ this.preprocessUrlAsync = (url) => Promise.resolve(url); } // eslint-disable-next-line babylonjs/available copyFrom(options) { if (options) { this.alwaysComputeBoundingBox = options.alwaysComputeBoundingBox ?? this.alwaysComputeBoundingBox; this.alwaysComputeSkeletonRootNode = options.alwaysComputeSkeletonRootNode ?? this.alwaysComputeSkeletonRootNode; this.animationStartMode = options.animationStartMode ?? this.animationStartMode; this.capturePerformanceCounters = options.capturePerformanceCounters ?? this.capturePerformanceCounters; this.compileMaterials = options.compileMaterials ?? this.compileMaterials; this.compileShadowGenerators = options.compileShadowGenerators ?? this.compileShadowGenerators; this.coordinateSystemMode = options.coordinateSystemMode ?? this.coordinateSystemMode; this.createInstances = options.createInstances ?? this.createInstances; this.customRootNode = options.customRootNode; this.extensionOptions = options.extensionOptions ?? this.extensionOptions; this.loadAllMaterials = options.loadAllMaterials ?? this.loadAllMaterials; this.loadMorphTargets = options.loadMorphTargets ?? this.loadMorphTargets; this.loadNodeAnimations = options.loadNodeAnimations ?? this.loadNodeAnimations; this.loadOnlyMaterials = options.loadOnlyMaterials ?? this.loadOnlyMaterials; this.loadSkins = options.loadSkins ?? this.loadSkins; this.loggingEnabled = options.loggingEnabled ?? this.loggingEnabled; this.onCameraLoaded = options.onCameraLoaded; this.onMaterialLoaded = options.onMaterialLoaded; this.onMeshLoaded = options.onMeshLoaded; this.onParsed = options.onParsed; this.onSkinLoaded = options.onSkinLoaded; this.onTextureLoaded = options.onTextureLoaded; this.onValidated = options.onValidated; this.preprocessUrlAsync = options.preprocessUrlAsync ?? this.preprocessUrlAsync; this.skipMaterials = options.skipMaterials ?? this.skipMaterials; this.targetFps = options.targetFps ?? this.targetFps; this.transparencyAsCoverage = options.transparencyAsCoverage ?? this.transparencyAsCoverage; this.useClipPlane = options.useClipPlane ?? this.useClipPlane; this.useGltfTextureNames = options.useGltfTextureNames ?? this.useGltfTextureNames; this.useOpenPBR = options.useOpenPBR ?? this.useOpenPBR; this.useRangeRequests = options.useRangeRequests ?? this.useRangeRequests; this.useSRGBBuffers = options.useSRGBBuffers ?? this.useSRGBBuffers; this.validate = options.validate ?? this.validate; this.dontUseTransmissionHelper = options.dontUseTransmissionHelper ?? this.dontUseTransmissionHelper; } } } /** * File loader for loading glTF files into a scene. */ export class GLTFFileLoader extends GLTFLoaderOptions { /** * Creates a new glTF file loader. * @param options The options for the loader */ constructor(options) { super(); // -------------------- // Begin Common options // -------------------- /** * Raised when the asset has been parsed */ this.onParsedObservable = new Observable(); // -------------- // End V1 options // -------------- /** * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh. * Note that the observable is raised as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...) */ this.onMeshLoadedObservable = new Observable(); /** * Observable raised when the loader creates a skin after parsing the glTF properties of the skin node. * @see https://doc.babylonjs.com/features/featuresDeepDive/importers/glTF/glTFSkinning#ignoring-the-transform-of-the-skinned-mesh * @param node - the transform node that corresponds to the original glTF skin node used for animations * @param skinnedNode - the transform node that is the skinned mesh itself or the parent of the skinned meshes */ this.onSkinLoadedObservable = new Observable(); /** * Observable raised when the loader creates a texture after parsing the glTF properties of the texture. */ this.onTextureLoadedObservable = new Observable(); /** * Observable raised when the loader creates a material after parsing the glTF properties of the material. */ this.onMaterialLoadedObservable = new Observable(); /** * Observable raised when the loader creates a camera after parsing the glTF properties of the camera. */ this.onCameraLoadedObservable = new Observable(); /** * Observable raised when the asset is completely loaded, immediately before the loader is disposed. * For assets with LODs, raised when all of the LODs are complete. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise. */ this.onCompleteObservable = new Observable(); /** * Observable raised when an error occurs. */ this.onErrorObservable = new Observable(); /** * Observable raised after the loader is disposed. */ this.onDisposeObservable = new Observable(); /** * Observable raised after a loader extension is created. * Set additional options for a loader extension in this event. */ this.onExtensionLoadedObservable = new Observable(); /** * Observable raised after validation when validate is set to true. The event data is the result of the validation. */ this.onValidatedObservable = new Observable(); this._loader = null; this._state = null; this._requests = new Array(); /** * Name of the loader ("gltf") */ this.name = GLTFFileLoaderMetadata.name; /** @internal */ this.extensions = GLTFFileLoaderMetadata.extensions; /** * Observable raised when the loader state changes. */ this.onLoaderStateChangedObservable = new Observable(); this._logIndentLevel = 0; this._loggingEnabled = false; /** @internal */ this._log = this._logDisabled; this._capturePerformanceCounters = false; /** @internal */ this._startPerformanceCounter = this._startPerformanceCounterDisabled; /** @internal */ this._endPerformanceCounter = this._endPerformanceCounterDisabled; this.copyFrom(Object.assign({ ...GLTFLoaderDefaultOptions }, options)); } /** * Raised when the asset has been parsed */ set onParsed(callback) { if (this._onParsedObserver) { this.onParsedObservable.remove(this._onParsedObserver); } if (callback) { this._onParsedObserver = this.onParsedObservable.add(callback); } } /** * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh. * Note that the callback is called as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...) */ set onMeshLoaded(callback) { if (this._onMeshLoadedObserver) { this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver); } if (callback) { this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback); } } /** * Callback raised when the loader creates a skin after parsing the glTF properties of the skin node. * @see https://doc.babylonjs.com/features/featuresDeepDive/importers/glTF/glTFSkinning#ignoring-the-transform-of-the-skinned-mesh */ set onSkinLoaded(callback) { if (this._onSkinLoadedObserver) { this.onSkinLoadedObservable.remove(this._onSkinLoadedObserver); } if (callback) { this._onSkinLoadedObserver = this.onSkinLoadedObservable.add((data) => callback(data.node, data.skinnedNode)); } } /** * Callback raised when the loader creates a texture after parsing the glTF properties of the texture. */ set onTextureLoaded(callback) { if (this._onTextureLoadedObserver) { this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver); } if (callback) { this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback); } } /** * Callback raised when the loader creates a material after parsing the glTF properties of the material. */ set onMaterialLoaded(callback) { if (this._onMaterialLoadedObserver) { this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver); } if (callback) { this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback); } } /** * Callback raised when the loader creates a camera after parsing the glTF properties of the camera. */ set onCameraLoaded(callback) { if (this._onCameraLoadedObserver) { this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver); } if (callback) { this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback); } } /** * Callback raised when the asset is completely loaded, immediately before the loader is disposed. * For assets with LODs, raised when all of the LODs are complete. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise. */ set onComplete(callback) { if (this._onCompleteObserver) { this.onCompleteObservable.remove(this._onCompleteObserver); } this._onCompleteObserver = this.onCompleteObservable.add(callback); } /** * Callback raised when an error occurs. */ set onError(callback) { if (this._onErrorObserver) { this.onErrorObservable.remove(this._onErrorObserver); } this._onErrorObserver = this.onErrorObservable.add(callback); } /** * Callback raised after the loader is disposed. */ set onDispose(callback) { if (this._onDisposeObserver) { this.onDisposeObservable.remove(this._onDisposeObserver); } this._onDisposeObserver = this.onDisposeObservable.add(callback); } /** * Callback raised after a loader extension is created. */ set onExtensionLoaded(callback) { if (this._onExtensionLoadedObserver) { this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver); } this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback); } /** * Defines if the loader logging is enabled. */ get loggingEnabled() { return this._loggingEnabled; } set loggingEnabled(value) { if (this._loggingEnabled === value) { return; } this._loggingEnabled = value; if (this._loggingEnabled) { this._log = this._logEnabled; } else { this._log = this._logDisabled; } } /** * Defines if the loader should capture performance counters. */ get capturePerformanceCounters() { return this._capturePerformanceCounters; } set capturePerformanceCounters(value) { if (this._capturePerformanceCounters === value) { return; } this._capturePerformanceCounters = value; if (this._capturePerformanceCounters) { this._startPerformanceCounter = this._startPerformanceCounterEnabled; this._endPerformanceCounter = this._endPerformanceCounterEnabled; } else { this._startPerformanceCounter = this._startPerformanceCounterDisabled; this._endPerformanceCounter = this._endPerformanceCounterDisabled; } } /** * Callback raised after the asset is validated. */ set onValidated(callback) { if (this._onValidatedObserver) { this.onValidatedObservable.remove(this._onValidatedObserver); } this._onValidatedObserver = this.onValidatedObservable.add(callback); } /** * Disposes the loader, releases resources during load, and cancels any outstanding requests. */ dispose() { if (this._loader) { this._loader.dispose(); this._loader = null; } for (const request of this._requests) { request.abort(); } this._requests.length = 0; delete this._progressCallback; this.preprocessUrlAsync = (url) => Promise.resolve(url); this.onMeshLoadedObservable.clear(); this.onSkinLoadedObservable.clear(); this.onTextureLoadedObservable.clear(); this.onMaterialLoadedObservable.clear(); this.onCameraLoadedObservable.clear(); this.onCompleteObservable.clear(); this.onExtensionLoadedObservable.clear(); this.onDisposeObservable.notifyObservers(undefined); this.onDisposeObservable.clear(); } /** * @internal */ loadFile(scene, fileOrUrl, rootUrl, onSuccess, onProgress, useArrayBuffer, onError, name) { if (ArrayBuffer.isView(fileOrUrl)) { this._loadBinary(scene, fileOrUrl, rootUrl, onSuccess, onError, name); return null; } this._progressCallback = onProgress; const fileName = fileOrUrl.name || Tools.GetFilename(fileOrUrl); if (useArrayBuffer) { if (this.useRangeRequests) { if (this.validate) { Logger.Warn("glTF validation is not supported when range requests are enabled"); } const fileRequest = { abort: () => { }, onCompleteObservable: new Observable(), }; const dataBuffer = { readAsync: (byteOffset, byteLength) => { return new Promise((resolve, reject) => { this._loadFile(scene, fileOrUrl, (data) => { resolve(new Uint8Array(data)); }, true, (error) => { reject(error); }, (webRequest) => { webRequest.setRequestHeader("Range", `bytes=${byteOffset}-${byteOffset + byteLength - 1}`); }); }); }, byteLength: 0, }; this._unpackBinaryAsync(new DataReader(dataBuffer)).then((loaderData) => { fileRequest.onCompleteObservable.notifyObservers(fileRequest); onSuccess(loaderData); }, onError ? (error) => onError(undefined, error) : undefined); return fileRequest; } return this._loadFile(scene, fileOrUrl, (data) => { this._validate(scene, new Uint8Array(data, 0, data.byteLength), rootUrl, fileName); this._unpackBinaryAsync(new DataReader({ readAsync: (byteOffset, byteLength) => readAsync(data, byteOffset, byteLength), byteLength: data.byteLength, })).then((loaderData) => { onSuccess(loaderData); }, onError ? (error) => onError(undefined, error) : undefined); }, true, onError); } else { return this._loadFile(scene, fileOrUrl, (data) => { try { this._validate(scene, data, rootUrl, fileName); onSuccess({ json: this._parseJson(data) }); } catch { if (onError) { onError(); } } }, false, onError); } } _loadBinary(scene, data, rootUrl, onSuccess, onError, fileName) { this._validate(scene, new Uint8Array(data.buffer, data.byteOffset, data.byteLength), rootUrl, fileName); this._unpackBinaryAsync(new DataReader({ readAsync: (byteOffset, byteLength) => readViewAsync(data, byteOffset, byteLength), byteLength: data.byteLength, })).then((loaderData) => { onSuccess(loaderData); }, onError ? (error) => onError(undefined, error) : undefined); } /** * @internal */ importMeshAsync(meshesNames, scene, data, rootUrl, onProgress, fileName) { return Promise.resolve().then(() => { this.onParsedObservable.notifyObservers(data); this.onParsedObservable.clear(); this._log(`Loading ${fileName || ""}`); this._loader = this._getLoader(data); return this._loader.importMeshAsync(meshesNames, scene, null, data, rootUrl, onProgress, fileName); }); } /** * @internal */ loadAsync(scene, data, rootUrl, onProgress, fileName) { return Promise.resolve().then(() => { this.onParsedObservable.notifyObservers(data); this.onParsedObservable.clear(); this._log(`Loading ${fileName || ""}`); this._loader = this._getLoader(data); return this._loader.loadAsync(scene, data, rootUrl, onProgress, fileName); }); } /** * @internal */ loadAssetContainerAsync(scene, data, rootUrl, onProgress, fileName) { return Promise.resolve().then(() => { this.onParsedObservable.notifyObservers(data); this.onParsedObservable.clear(); this._log(`Loading ${fileName || ""}`); this._loader = this._getLoader(data); // Prepare the asset container. const container = new AssetContainer(scene); // Get materials/textures when loading to add to container const materials = []; this.onMaterialLoadedObservable.add((material) => { materials.push(material); }); const textures = []; this.onTextureLoadedObservable.add((texture) => { textures.push(texture); }); const cameras = []; this.onCameraLoadedObservable.add((camera) => { cameras.push(camera); }); const morphTargetManagers = []; this.onMeshLoadedObservable.add((mesh) => { if (mesh.morphTargetManager) { morphTargetManagers.push(mesh.morphTargetManager); } }); return this._loader.importMeshAsync(null, scene, container, data, rootUrl, onProgress, fileName).then((result) => { Array.prototype.push.apply(container.geometries, result.geometries); Array.prototype.push.apply(container.meshes, result.meshes); Array.prototype.push.apply(container.particleSystems, result.particleSystems); Array.prototype.push.apply(container.skeletons, result.skeletons); Array.prototype.push.apply(container.animationGroups, result.animationGroups); Array.prototype.push.apply(container.materials, materials); Array.prototype.push.apply(container.textures, textures); Array.prototype.push.apply(container.lights, result.lights); Array.prototype.push.apply(container.transformNodes, result.transformNodes); Array.prototype.push.apply(container.cameras, cameras); Array.prototype.push.apply(container.morphTargetManagers, morphTargetManagers); return container; }); }); } /** * @internal */ canDirectLoad(data) { return GLTFFileLoaderMetadata.canDirectLoad(data); } /** * @internal */ directLoad(scene, data) { if (data.startsWith("base64," + GLTFMagicBase64Encoded) || // this is technically incorrect, but will continue to support for backcompat. data.startsWith(";base64," + GLTFMagicBase64Encoded) || data.startsWith("application/octet-stream;base64," + GLTFMagicBase64Encoded) || data.startsWith("model/gltf-binary;base64," + GLTFMagicBase64Encoded)) { const arrayBuffer = DecodeBase64UrlToBinary(data); this._validate(scene, new Uint8Array(arrayBuffer, 0, arrayBuffer.byteLength)); return this._unpackBinaryAsync(new DataReader({ readAsync: (byteOffset, byteLength) => readAsync(arrayBuffer, byteOffset, byteLength), byteLength: arrayBuffer.byteLength, })); } this._validate(scene, data); return Promise.resolve({ json: this._parseJson(data) }); } /** @internal */ createPlugin(options) { return new GLTFFileLoader(options[GLTFFileLoaderMetadata.name]); } /** * The loader state or null if the loader is not active. */ get loaderState() { return this._state; } /** * Returns a promise that resolves when the asset is completely loaded. * @returns a promise that resolves when the asset is completely loaded. */ whenCompleteAsync() { return new Promise((resolve, reject) => { this.onCompleteObservable.addOnce(() => { resolve(); }); this.onErrorObservable.addOnce((reason) => { reject(reason); }); }); } /** * @internal */ _setState(state) { if (this._state === state) { return; } this._state = state; this.onLoaderStateChangedObservable.notifyObservers(this._state); this._log(GLTFLoaderState[this._state]); } /** * @internal */ _loadFile(scene, fileOrUrl, onSuccess, useArrayBuffer, onError, onOpened) { const request = scene._loadFile(fileOrUrl, onSuccess, (event) => { this._onProgress(event, request); }, true, useArrayBuffer, onError, onOpened); request.onCompleteObservable.add(() => { // Force the length computable to be true since we can guarantee the data is loaded. request._lengthComputable = true; request._total = request._loaded; }); this._requests.push(request); return request; } _onProgress(event, request) { if (!this._progressCallback) { return; } request._lengthComputable = event.lengthComputable; request._loaded = event.loaded; request._total = event.total; let lengthComputable = true; let loaded = 0; let total = 0; for (const request of this._requests) { if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) { return; } lengthComputable = lengthComputable && request._lengthComputable; loaded += request._loaded; total += request._total; } this._progressCallback({ lengthComputable: lengthComputable, loaded: loaded, total: lengthComputable ? total : 0, }); } _validate(scene, data, rootUrl = "", fileName = "") { if (!this.validate) { return; } this._startPerformanceCounter("Validate JSON"); GLTFValidation.ValidateAsync(data, rootUrl, fileName, (uri) => { return this.preprocessUrlAsync(rootUrl + uri).then((url) => { return scene._loadFileAsync(url, undefined, true, true).then((data) => { return new Uint8Array(data, 0, data.byteLength); }); }); }).then((result) => { this._endPerformanceCounter("Validate JSON"); this.onValidatedObservable.notifyObservers(result); this.onValidatedObservable.clear(); }, (reason) => { this._endPerformanceCounter("Validate JSON"); Tools.Warn(`Failed to validate: ${reason.message}`); this.onValidatedObservable.clear(); }); } _getLoader(loaderData) { const asset = loaderData.json.asset || {}; this._log(`Asset version: ${asset.version}`); asset.minVersion && this._log(`Asset minimum version: ${asset.minVersion}`); asset.generator && this._log(`Asset generator: ${asset.generator}`); const version = GLTFFileLoader._parseVersion(asset.version); if (!version) { throw new Error("Invalid version: " + asset.version); } if (asset.minVersion !== undefined) { const minVersion = GLTFFileLoader._parseVersion(asset.minVersion); if (!minVersion) { throw new Error("Invalid minimum version: " + asset.minVersion); } if (GLTFFileLoader._compareVersion(minVersion, { major: 2, minor: 0 }) > 0) { throw new Error("Incompatible minimum version: " + asset.minVersion); } } const createLoaders = { 1: GLTFFileLoader._CreateGLTF1Loader, 2: GLTFFileLoader._CreateGLTF2Loader, }; const createLoader = createLoaders[version.major]; if (!createLoader) { throw new Error("Unsupported version: " + asset.version); } return createLoader(this); } _parseJson(json) { this._startPerformanceCounter("Parse JSON"); this._log(`JSON length: ${json.length}`); const parsed = JSON.parse(json); this._endPerformanceCounter("Parse JSON"); return parsed; } _unpackBinaryAsync(dataReader) { this._startPerformanceCounter("Unpack Binary"); // Read magic + version + length + json length + json format return dataReader.loadAsync(20).then(() => { const Binary = { Magic: 0x46546c67, }; const magic = dataReader.readUint32(); if (magic !== Binary.Magic) { throw new RuntimeError("Unexpected magic: " + magic, ErrorCodes.GLTFLoaderUnexpectedMagicError); } const version = dataReader.readUint32(); if (this.loggingEnabled) { this._log(`Binary version: ${version}`); } const length = dataReader.readUint32(); if (!this.useRangeRequests && length !== dataReader.buffer.byteLength) { Logger.Warn(`Length in header does not match actual data length: ${length} != ${dataReader.buffer.byteLength}`); } let unpacked; switch (version) { case 1: { unpacked = this._unpackBinaryV1Async(dataReader, length); break; } case 2: { unpacked = this._unpackBinaryV2Async(dataReader, length); break; } default: { throw new Error("Unsupported version: " + version); } } this._endPerformanceCounter("Unpack Binary"); return unpacked; }); } _unpackBinaryV1Async(dataReader, length) { const ContentFormat = { JSON: 0, }; const contentLength = dataReader.readUint32(); const contentFormat = dataReader.readUint32(); if (contentFormat !== ContentFormat.JSON) { throw new Error(`Unexpected content format: ${contentFormat}`); } const bodyLength = length - dataReader.byteOffset; const data = { json: this._parseJson(dataReader.readString(contentLength)), bin: null }; if (bodyLength !== 0) { const startByteOffset = dataReader.byteOffset; data.bin = { readAsync: (byteOffset, byteLength) => dataReader.buffer.readAsync(startByteOffset + byteOffset, byteLength), byteLength: bodyLength, }; } return Promise.resolve(data); } _unpackBinaryV2Async(dataReader, length) { const ChunkFormat = { JSON: 0x4e4f534a, BIN: 0x004e4942, }; // Read the JSON chunk header. const chunkLength = dataReader.readUint32(); const chunkFormat = dataReader.readUint32(); if (chunkFormat !== ChunkFormat.JSON) { throw new Error("First chunk format is not JSON"); } // Bail if there are no other chunks. if (dataReader.byteOffset + chunkLength === length) { return dataReader.loadAsync(chunkLength).then(() => { return { json: this._parseJson(dataReader.readString(chunkLength)), bin: null }; }); } // Read the JSON chunk and the length and type of the next chunk. return dataReader.loadAsync(chunkLength + 8).then(() => { const data = { json: this._parseJson(dataReader.readString(chunkLength)), bin: null }; const readAsync = () => { const chunkLength = dataReader.readUint32(); const chunkFormat = dataReader.readUint32(); switch (chunkFormat) { case ChunkFormat.JSON: { throw new Error("Unexpected JSON chunk"); } case ChunkFormat.BIN: { const startByteOffset = dataReader.byteOffset; data.bin = { readAsync: (byteOffset, byteLength) => dataReader.buffer.readAsync(startByteOffset + byteOffset, byteLength), byteLength: chunkLength, }; dataReader.skipBytes(chunkLength); break; } default: { // ignore unrecognized chunkFormat dataReader.skipBytes(chunkLength); break; } } if (dataReader.byteOffset !== length) { return dataReader.loadAsync(8).then(readAsync); } return Promise.resolve(data); }; return readAsync(); }); } static _parseVersion(version) { if (version === "1.0" || version === "1.0.1") { return { major: 1, minor: 0, }; } const match = (version + "").match(/^(\d+)\.(\d+)/); if (!match) { return null; } return { major: parseInt(match[1]), minor: parseInt(match[2]), }; } static _compareVersion(a, b) { if (a.major > b.major) { return 1; } if (a.major < b.major) { return -1; } if (a.minor > b.minor) { return 1; } if (a.minor < b.minor) { return -1; } return 0; } /** * @internal */ _logOpen(message) { this._log(message); this._logIndentLevel++; } /** @internal */ _logClose() { --this._logIndentLevel; } _logEnabled(message) { const spaces = GLTFFileLoader._logSpaces.substring(0, this._logIndentLevel * 2); Logger.Log(`${spaces}${message}`); } _logDisabled(message) { } _startPerformanceCounterEnabled(counterName) { Tools.StartPerformanceCounter(counterName); } _startPerformanceCounterDisabled(counterName) { } _endPerformanceCounterEnabled(counterName) { Tools.EndPerformanceCounter(counterName); } _endPerformanceCounterDisabled(counterName) { } } // ------------------ // End Common options // ------------------ // ---------------- // Begin V1 options // ---------------- /** * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders. * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled. * Defaults to true. * @internal */ GLTFFileLoader.IncrementalLoading = true; /** * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters. * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates. * @internal */ GLTFFileLoader.HomogeneousCoordinates = false; GLTFFileLoader._logSpaces = " "; RegisterSceneLoaderPlugin(new GLTFFileLoader()); //# sourceMappingURL=glTFFileLoader.js.map