Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 1666x 1666x 1666x 1666x 1666x 1666x 1666x 1666x 1665x 1664x 1663x 1663x 1663x 1663x 1663x 1663x 1663x 1663x 1663x 1663x 1x 1x 1x 1x 5008x 27367x 1x 1776x 114x 1662x 1662x 95x 95x 1567x 1091x 1091x 476x 476x 476x 476x 469x 4x 465x 7x 2x 5x 465x 8x 8x 8x 8x 1651x 1638x 13x 13x 10x 8x 8x 8x 6x 6x 4x 4x 11x 9x 1651x 1651x 1780x 1780x 111x 111x 111x 1651x 1647x 1647x 1776x 1776x 1297x 1297x 1297x 1297x 1647x 1651x 1651x 1651x 1651x 1647x 1647x 1647x 1647x 1647x 1647x 1647x 98x 1646x 1646x 1646x 5x 2x 3x 1556x 1556x 1187x 369x 1556x 1x 1657x 1657x 1657x 1295x 1657x 1657x 1x 446x | import Check from "../Core/Check.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
import getMagic from "../Core/getMagic.js";
import isDataUri from "../Core/isDataUri.js";
import Resource from "../Core/Resource.js";
import RuntimeError from "../Core/RuntimeError.js";
import addDefaults from "./GltfPipeline/addDefaults.js";
import addPipelineExtras from "./GltfPipeline/addPipelineExtras.js";
import ForEach from "./GltfPipeline/ForEach.js";
import parseGlb from "./GltfPipeline/parseGlb.js";
import removePipelineExtras from "./GltfPipeline/removePipelineExtras.js";
import updateVersion from "./GltfPipeline/updateVersion.js";
import usesExtension from "./GltfPipeline/usesExtension.js";
import ResourceLoader from "./ResourceLoader.js";
import ResourceLoaderState from "./ResourceLoaderState.js";
import ModelUtility from "./Model/ModelUtility.js";
/**
* Loads a glTF JSON from a glTF or glb.
* <p>
* Implements the {@link ResourceLoader} interface.
* </p>
*
* @alias GltfJsonLoader
* @constructor
* @augments ResourceLoader
*
* @param {object} options Object with the following properties:
* @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
* @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
* @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
* @param {Uint8Array} [options.typedArray] The typed array containing the glTF contents.
* @param {object} [options.gltfJson] The parsed glTF JSON contents.
* @param {string} [options.cacheKey] The cache key of the resource.
*
* @private
*/
function GltfJsonLoader(options) {
options = options ?? Frozen.EMPTY_OBJECT;
const resourceCache = options.resourceCache;
const gltfResource = options.gltfResource;
const baseResource = options.baseResource;
const typedArray = options.typedArray;
const gltfJson = options.gltfJson;
const cacheKey = options.cacheKey;
//>>includeStart('debug', pragmas.debug);
Check.typeOf.func("options.resourceCache", resourceCache);
Check.typeOf.object("options.gltfResource", gltfResource);
Check.typeOf.object("options.baseResource", baseResource);
//>>includeEnd('debug');
this._resourceCache = resourceCache;
this._gltfResource = gltfResource;
this._baseResource = baseResource;
this._typedArray = typedArray;
this._gltfJson = gltfJson;
this._cacheKey = cacheKey;
this._gltf = undefined;
this._bufferLoaders = [];
this._state = ResourceLoaderState.UNLOADED;
this._promise = undefined;
}
Eif (defined(Object.create)) {
GltfJsonLoader.prototype = Object.create(ResourceLoader.prototype);
GltfJsonLoader.prototype.constructor = GltfJsonLoader;
}
Object.defineProperties(GltfJsonLoader.prototype, {
/**
* The cache key of the resource.
*
* @memberof GltfJsonLoader.prototype
*
* @type {string}
* @readonly
* @private
*/
cacheKey: {
get: function () {
return this._cacheKey;
},
},
/**
* The glTF JSON.
*
* @memberof GltfJsonLoader.prototype
*
* @type {object}
* @readonly
* @private
*/
gltf: {
get: function () {
return this._gltf;
},
},
});
/**
* Loads the resource.
* @returns {Promise<GltfJsonLoader>} A promise which resolves to the loader when the resource loading is completed.
* @private
*/
GltfJsonLoader.prototype.load = async function () {
if (defined(this._promise)) {
return this._promise;
}
this._state = ResourceLoaderState.LOADING;
if (defined(this._gltfJson)) {
this._promise = processGltfJson(this, this._gltfJson);
return this._promise;
}
if (defined(this._typedArray)) {
this._promise = processGltfTypedArray(this, this._typedArray);
return this._promise;
}
this._promise = loadFromUri(this);
return this._promise;
};
async function loadFromUri(gltfJsonLoader) {
let typedArray;
try {
const arrayBuffer = await gltfJsonLoader._fetchGltf();
if (gltfJsonLoader.isDestroyed()) {
return;
}
typedArray = new Uint8Array(arrayBuffer);
} catch (error) {
if (gltfJsonLoader.isDestroyed()) {
return;
}
handleError(gltfJsonLoader, error);
}
return processGltfTypedArray(gltfJsonLoader, typedArray);
}
function handleError(gltfJsonLoader, error) {
gltfJsonLoader.unload();
gltfJsonLoader._state = ResourceLoaderState.FAILED;
const errorMessage = `Failed to load glTF: ${gltfJsonLoader._gltfResource.url}`;
throw gltfJsonLoader.getError(errorMessage, error);
}
async function upgradeVersion(gltfJsonLoader, gltf) {
if (
defined(gltf.asset) &&
gltf.asset.version === "2.0" &&
!usesExtension(gltf, "KHR_techniques_webgl") &&
!usesExtension(gltf, "KHR_materials_common")
) {
return Promise.resolve();
}
// Load all buffers into memory. updateVersion will read and in some cases modify
// the buffer data, which it accesses from buffer.extras._pipeline.source
const promises = [];
ForEach.buffer(gltf, function (buffer) {
if (
!defined(buffer.extras._pipeline.source) && // Ignore uri if this buffer uses the glTF 1.0 KHR_binary_glTF extension
defined(buffer.uri)
) {
const resource = gltfJsonLoader._baseResource.getDerivedResource({
url: buffer.uri,
});
const resourceCache = gltfJsonLoader._resourceCache;
const bufferLoader = resourceCache.getExternalBufferLoader({
resource: resource,
});
gltfJsonLoader._bufferLoaders.push(bufferLoader);
promises.push(
bufferLoader.load().then(function () {
Iif (bufferLoader.isDestroyed()) {
return;
}
buffer.extras._pipeline.source = bufferLoader.typedArray;
}),
);
}
});
await Promise.all(promises);
updateVersion(gltf);
}
function decodeDataUris(gltf) {
const promises = [];
ForEach.buffer(gltf, function (buffer) {
const bufferUri = buffer.uri;
if (
!defined(buffer.extras._pipeline.source) && // Ignore uri if this buffer uses the glTF 1.0 KHR_binary_glTF extension
defined(bufferUri) &&
isDataUri(bufferUri)
) {
delete buffer.uri; // Delete the data URI to keep the cached glTF JSON small
promises.push(
Resource.fetchArrayBuffer(bufferUri).then(function (arrayBuffer) {
buffer.extras._pipeline.source = new Uint8Array(arrayBuffer);
}),
);
}
});
return Promise.all(promises);
}
function loadEmbeddedBuffers(gltfJsonLoader, gltf) {
const promises = [];
ForEach.buffer(gltf, function (buffer, bufferId) {
const source = buffer.extras._pipeline.source;
if (defined(source) && !defined(buffer.uri)) {
const resourceCache = gltfJsonLoader._resourceCache;
const bufferLoader = resourceCache.getEmbeddedBufferLoader({
parentResource: gltfJsonLoader._gltfResource,
bufferId: bufferId,
typedArray: source,
});
gltfJsonLoader._bufferLoaders.push(bufferLoader);
promises.push(bufferLoader.load());
}
});
return Promise.all(promises);
}
async function processGltfJson(gltfJsonLoader, gltf) {
try {
addPipelineExtras(gltf);
await decodeDataUris(gltf);
await upgradeVersion(gltfJsonLoader, gltf);
addDefaults(gltf);
await loadEmbeddedBuffers(gltfJsonLoader, gltf);
removePipelineExtras(gltf);
const version = gltf.asset.version;
Iif (version !== "1.0" && version !== "2.0") {
throw new RuntimeError(`Unsupported glTF version: ${version}`);
}
const extensionsRequired = gltf.extensionsRequired;
if (defined(extensionsRequired)) {
ModelUtility.checkSupportedExtensions(extensionsRequired);
}
gltfJsonLoader._gltf = gltf;
gltfJsonLoader._state = ResourceLoaderState.READY;
return gltfJsonLoader;
} catch (error) {
if (gltfJsonLoader.isDestroyed()) {
return;
}
handleError(gltfJsonLoader, error);
}
}
async function processGltfTypedArray(gltfJsonLoader, typedArray) {
let gltf;
try {
if (getMagic(typedArray) === "glTF") {
gltf = parseGlb(typedArray);
} else {
gltf = getJsonFromTypedArray(typedArray);
}
} catch (error) {
if (gltfJsonLoader.isDestroyed()) {
return;
}
handleError(gltfJsonLoader, error);
}
return processGltfJson(gltfJsonLoader, gltf);
}
/**
* Unloads the resource.
* @private
*/
GltfJsonLoader.prototype.unload = function () {
const bufferLoaders = this._bufferLoaders;
const bufferLoadersLength = bufferLoaders.length;
for (let i = 0; i < bufferLoadersLength; ++i) {
bufferLoaders[i] =
!bufferLoaders[i].isDestroyed() &&
this._resourceCache.unload(bufferLoaders[i]);
}
this._bufferLoaders.length = 0;
this._gltf = undefined;
};
/**
* Exposed for testing
*
* @private
*/
GltfJsonLoader.prototype._fetchGltf = function () {
return this._gltfResource.fetchArrayBuffer();
};
export default GltfJsonLoader;
|