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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | 424x 424x 424x 424x 424x 424x 424x 424x 424x 424x 423x 422x 421x 420x 419x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 1x 1x 1x 1x 1596x 984x 1x 415x 415x 415x 415x 415x 409x 101x 308x 308x 308x 308x 6x 6x 6x 6x 6x 1x 490x 75x 415x 415x 415x 1x 1x 1x 1x 1x 1x 1x 304x 304x 304x 304x 304x 304x 1x 301x 302x 302x 302x 302x 302x 302x 302x 302x 302x 302x 302x 302x 302x 302x 74x 302x 302x 110x 302x 1x 1753x 1753x 120x 1633x 1328x 305x 305x 305x 305x 304x 304x 304x 304x 3x 301x 1x 302x 302x 302x 302x 302x 1x 720x 297x 720x 415x 720x 720x 720x 720x 720x | import Check from "../Core/Check.js";
import CesiumMath from "../Core/Math.js";
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import PixelFormat from "../Core/PixelFormat.js";
import Texture from "../Renderer/Texture.js";
import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
import TextureWrap from "../Renderer/TextureWrap.js";
import GltfLoaderUtil from "./GltfLoaderUtil.js";
import JobType from "./JobType.js";
import ResourceLoader from "./ResourceLoader.js";
import ResourceLoaderState from "./ResourceLoaderState.js";
import resizeImageToNextPowerOfTwo from "../Core/resizeImageToNextPowerOfTwo.js";
/**
* Loads a glTF texture.
* <p>
* Implements the {@link ResourceLoader} interface.
* </p>
*
* @alias GltfTextureLoader
* @constructor
* @augments ResourceLoader
*
* @param {object} options Object with the following properties:
* @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
* @param {object} options.gltf The glTF JSON.
* @param {object} options.textureInfo The texture info object.
* @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 {SupportedImageFormats} options.supportedImageFormats The supported image formats.
* @param {string} [options.cacheKey] The cache key of the resource.
* @param {boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
*
* @private
*/
function GltfTextureLoader(options) {
options = options ?? Frozen.EMPTY_OBJECT;
const resourceCache = options.resourceCache;
const gltf = options.gltf;
const textureInfo = options.textureInfo;
const gltfResource = options.gltfResource;
const baseResource = options.baseResource;
const supportedImageFormats = options.supportedImageFormats;
const cacheKey = options.cacheKey;
const asynchronous = options.asynchronous ?? true;
//>>includeStart('debug', pragmas.debug);
Check.typeOf.func("options.resourceCache", resourceCache);
Check.typeOf.object("options.gltf", gltf);
Check.typeOf.object("options.textureInfo", textureInfo);
Check.typeOf.object("options.gltfResource", gltfResource);
Check.typeOf.object("options.baseResource", baseResource);
Check.typeOf.object("options.supportedImageFormats", supportedImageFormats);
//>>includeEnd('debug');
const textureId = textureInfo.index;
// imageId is guaranteed to be defined otherwise the GltfTextureLoader
// wouldn't have been created
const imageId = GltfLoaderUtil.getImageIdFromTexture({
gltf: gltf,
textureId: textureId,
supportedImageFormats: supportedImageFormats,
});
this._resourceCache = resourceCache;
this._gltf = gltf;
this._textureInfo = textureInfo;
this._imageId = imageId;
this._gltfResource = gltfResource;
this._baseResource = baseResource;
this._cacheKey = cacheKey;
this._asynchronous = asynchronous;
this._imageLoader = undefined;
this._image = undefined;
this._mipLevels = undefined;
this._texture = undefined;
this._state = ResourceLoaderState.UNLOADED;
this._promise = undefined;
}
Eif (defined(Object.create)) {
GltfTextureLoader.prototype = Object.create(ResourceLoader.prototype);
GltfTextureLoader.prototype.constructor = GltfTextureLoader;
}
Object.defineProperties(GltfTextureLoader.prototype, {
/**
* The cache key of the resource.
*
* @memberof GltfTextureLoader.prototype
*
* @type {string}
* @readonly
* @private
*/
cacheKey: {
get: function () {
return this._cacheKey;
},
},
/**
* The texture.
*
* @memberof GltfTextureLoader.prototype
*
* @type {Texture}
* @readonly
* @private
*/
texture: {
get: function () {
return this._texture;
},
},
});
const scratchTextureJob = new CreateTextureJob();
async function loadResources(loader) {
const resourceCache = loader._resourceCache;
try {
const imageLoader = resourceCache.getImageLoader({
gltf: loader._gltf,
imageId: loader._imageId,
gltfResource: loader._gltfResource,
baseResource: loader._baseResource,
});
loader._imageLoader = imageLoader;
await imageLoader.load();
if (loader.isDestroyed()) {
return;
}
// Now wait for process() to run to finish loading
loader._image = imageLoader.image;
loader._mipLevels = imageLoader.mipLevels;
loader._state = ResourceLoaderState.LOADED;
return loader;
} catch (error) {
Iif (loader.isDestroyed()) {
return;
}
loader.unload();
loader._state = ResourceLoaderState.FAILED;
const errorMessage = "Failed to load texture";
throw loader.getError(errorMessage, error);
}
}
/**
* Loads the resource.
* @returns {Promise<GltfDracoLoader>} A promise which resolves to the loader when the resource loading is completed.
* @private
*/
GltfTextureLoader.prototype.load = async function () {
if (defined(this._promise)) {
return this._promise;
}
this._state = ResourceLoaderState.LOADING;
this._promise = loadResources(this);
return this._promise;
};
function CreateTextureJob() {
this.gltf = undefined;
this.textureInfo = undefined;
this.textureId = undefined;
this.image = undefined;
this.context = undefined;
this.texture = undefined;
}
CreateTextureJob.prototype.set = function (
gltf,
textureInfo,
textureId,
image,
mipLevels,
context,
) {
this.gltf = gltf;
this.textureInfo = textureInfo;
this.textureId = textureId;
this.image = image;
this.mipLevels = mipLevels;
this.context = context;
};
CreateTextureJob.prototype.execute = function () {
this.texture = createTexture(
this.gltf,
this.textureInfo,
this.textureId,
this.image,
this.mipLevels,
this.context,
);
};
function createTexture(
gltf,
textureInfo,
textureId,
image,
mipLevels,
context,
) {
// internalFormat is only defined for CompressedTextureBuffer
const internalFormat = image.internalFormat;
let compressedTextureNoMipmap = false;
Iif (PixelFormat.isCompressedFormat(internalFormat) && !defined(mipLevels)) {
compressedTextureNoMipmap = true;
}
const sampler = GltfLoaderUtil.createSampler({
gltf: gltf,
textureInfo: textureInfo,
compressedTextureNoMipmap: compressedTextureNoMipmap,
});
const minFilter = sampler.minificationFilter;
const wrapS = sampler.wrapS;
const wrapT = sampler.wrapT;
const samplerRequiresMipmap =
minFilter === TextureMinificationFilter.NEAREST_MIPMAP_NEAREST ||
minFilter === TextureMinificationFilter.NEAREST_MIPMAP_LINEAR ||
minFilter === TextureMinificationFilter.LINEAR_MIPMAP_NEAREST ||
minFilter === TextureMinificationFilter.LINEAR_MIPMAP_LINEAR;
// generateMipmap is disallowed for compressed textures. Compressed textures
// can have mipmaps but they must come with the KTX2 instead of generated by
// WebGL. Also note from the KHR_texture_basisu spec:
//
// When a texture refers to a sampler with mipmap minification or when the
// sampler is undefined, the KTX2 image SHOULD contain a full mip pyramid.
//
const generateMipmap = !defined(internalFormat) && samplerRequiresMipmap;
// WebGL 1 requires power-of-two texture dimensions for mipmapping and REPEAT/MIRRORED_REPEAT wrap modes.
const requiresPowerOfTwo =
generateMipmap ||
wrapS === TextureWrap.REPEAT ||
wrapS === TextureWrap.MIRRORED_REPEAT ||
wrapT === TextureWrap.REPEAT ||
wrapT === TextureWrap.MIRRORED_REPEAT;
const nonPowerOfTwo =
!CesiumMath.isPowerOfTwo(image.width) ||
!CesiumMath.isPowerOfTwo(image.height);
const requiresResize = requiresPowerOfTwo && nonPowerOfTwo;
let texture;
Iif (defined(internalFormat)) {
if (
!context.webgl2 &&
PixelFormat.isCompressedFormat(internalFormat) &&
nonPowerOfTwo &&
requiresPowerOfTwo
) {
console.warn(
"Compressed texture uses REPEAT or MIRRORED_REPEAT texture wrap mode and dimensions are not powers of two. The texture may be rendered incorrectly.",
);
}
texture = Texture.create({
id: textureId,
context: context,
source: {
arrayBufferView: image.bufferView, // Only defined for CompressedTextureBuffer
mipLevels: mipLevels,
},
width: image.width,
height: image.height,
pixelFormat: image.internalFormat, // Only defined for CompressedTextureBuffer
sampler: sampler,
});
} else {
if (requiresResize) {
image = resizeImageToNextPowerOfTwo(image);
}
texture = Texture.create({
id: textureId,
context: context,
source: image,
sampler: sampler,
flipY: false,
skipColorSpaceConversion: true,
});
}
if (generateMipmap) {
texture.generateMipmap();
}
return texture;
}
/**
* Processes the resource until it becomes ready.
*
* @param {FrameState} frameState The frame state.
* @returns {boolean} true once all resourced are ready.
* @private
*/
GltfTextureLoader.prototype.process = function (frameState) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("frameState", frameState);
//>>includeEnd('debug');
if (this._state === ResourceLoaderState.READY) {
return true;
}
if (
this._state !== ResourceLoaderState.LOADED &&
this._state !== ResourceLoaderState.PROCESSING
) {
return false;
}
Iif (defined(this._texture)) {
// Already created texture
return false;
}
Iif (!defined(this._image)) {
// Not ready to create texture
return false;
}
this._state = ResourceLoaderState.PROCESSING;
let texture;
if (this._asynchronous) {
const textureJob = scratchTextureJob;
textureJob.set(
this._gltf,
this._textureInfo,
this._cacheKey,
this._image,
this._mipLevels,
frameState.context,
);
const jobScheduler = frameState.jobScheduler;
if (!jobScheduler.execute(textureJob, JobType.TEXTURE)) {
// Job scheduler is full. Try again next frame.
return;
}
texture = textureJob.texture;
} else {
texture = createTexture(
this._gltf,
this._textureInfo,
this._cacheKey,
this._image,
this._mipLevels,
frameState.context,
);
}
// Unload everything except the texture
this.unload();
this._texture = texture;
this._state = ResourceLoaderState.READY;
this._resourceCache.statistics.addTextureLoader(this);
return true;
};
/**
* Unloads the resource.
* @private
*/
GltfTextureLoader.prototype.unload = function () {
if (defined(this._texture)) {
this._texture.destroy();
}
if (defined(this._imageLoader) && !this._imageLoader.isDestroyed()) {
this._resourceCache.unload(this._imageLoader);
}
this._imageLoader = undefined;
this._image = undefined;
this._mipLevels = undefined;
this._texture = undefined;
this._gltf = undefined;
};
export default GltfTextureLoader;
|