GltfTextureLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import Check from "../Core/Check.js";
  2. import CesiumMath from "../Core/Math.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defined from "../Core/defined.js";
  5. import PixelFormat from "../Core/PixelFormat.js";
  6. import Texture from "../Renderer/Texture.js";
  7. import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
  8. import TextureWrap from "../Renderer/TextureWrap.js";
  9. import GltfLoaderUtil from "./GltfLoaderUtil.js";
  10. import JobType from "./JobType.js";
  11. import ResourceLoader from "./ResourceLoader.js";
  12. import ResourceLoaderState from "./ResourceLoaderState.js";
  13. import resizeImageToNextPowerOfTwo from "../Core/resizeImageToNextPowerOfTwo.js";
  14. /**
  15. * Loads a glTF texture.
  16. * <p>
  17. * Implements the {@link ResourceLoader} interface.
  18. * </p>
  19. *
  20. * @alias GltfTextureLoader
  21. * @constructor
  22. * @augments ResourceLoader
  23. *
  24. * @param {object} options Object with the following properties:
  25. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  26. * @param {object} options.gltf The glTF JSON.
  27. * @param {object} options.textureInfo The texture info object.
  28. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  29. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  30. * @param {SupportedImageFormats} options.supportedImageFormats The supported image formats.
  31. * @param {string} [options.cacheKey] The cache key of the resource.
  32. * @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.
  33. *
  34. * @private
  35. */
  36. function GltfTextureLoader(options) {
  37. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  38. const resourceCache = options.resourceCache;
  39. const gltf = options.gltf;
  40. const textureInfo = options.textureInfo;
  41. const gltfResource = options.gltfResource;
  42. const baseResource = options.baseResource;
  43. const supportedImageFormats = options.supportedImageFormats;
  44. const cacheKey = options.cacheKey;
  45. const asynchronous = defaultValue(options.asynchronous, true);
  46. //>>includeStart('debug', pragmas.debug);
  47. Check.typeOf.func("options.resourceCache", resourceCache);
  48. Check.typeOf.object("options.gltf", gltf);
  49. Check.typeOf.object("options.textureInfo", textureInfo);
  50. Check.typeOf.object("options.gltfResource", gltfResource);
  51. Check.typeOf.object("options.baseResource", baseResource);
  52. Check.typeOf.object("options.supportedImageFormats", supportedImageFormats);
  53. //>>includeEnd('debug');
  54. const textureId = textureInfo.index;
  55. // imageId is guaranteed to be defined otherwise the GltfTextureLoader
  56. // wouldn't have been created
  57. const imageId = GltfLoaderUtil.getImageIdFromTexture({
  58. gltf: gltf,
  59. textureId: textureId,
  60. supportedImageFormats: supportedImageFormats,
  61. });
  62. this._resourceCache = resourceCache;
  63. this._gltf = gltf;
  64. this._textureInfo = textureInfo;
  65. this._imageId = imageId;
  66. this._gltfResource = gltfResource;
  67. this._baseResource = baseResource;
  68. this._cacheKey = cacheKey;
  69. this._asynchronous = asynchronous;
  70. this._imageLoader = undefined;
  71. this._image = undefined;
  72. this._mipLevels = undefined;
  73. this._texture = undefined;
  74. this._state = ResourceLoaderState.UNLOADED;
  75. this._promise = undefined;
  76. }
  77. if (defined(Object.create)) {
  78. GltfTextureLoader.prototype = Object.create(ResourceLoader.prototype);
  79. GltfTextureLoader.prototype.constructor = GltfTextureLoader;
  80. }
  81. Object.defineProperties(GltfTextureLoader.prototype, {
  82. /**
  83. * The cache key of the resource.
  84. *
  85. * @memberof GltfTextureLoader.prototype
  86. *
  87. * @type {string}
  88. * @readonly
  89. * @private
  90. */
  91. cacheKey: {
  92. get: function () {
  93. return this._cacheKey;
  94. },
  95. },
  96. /**
  97. * The texture.
  98. *
  99. * @memberof GltfTextureLoader.prototype
  100. *
  101. * @type {Texture}
  102. * @readonly
  103. * @private
  104. */
  105. texture: {
  106. get: function () {
  107. return this._texture;
  108. },
  109. },
  110. });
  111. const scratchTextureJob = new CreateTextureJob();
  112. async function loadResources(loader) {
  113. const resourceCache = loader._resourceCache;
  114. try {
  115. const imageLoader = resourceCache.getImageLoader({
  116. gltf: loader._gltf,
  117. imageId: loader._imageId,
  118. gltfResource: loader._gltfResource,
  119. baseResource: loader._baseResource,
  120. });
  121. loader._imageLoader = imageLoader;
  122. await imageLoader.load();
  123. if (loader.isDestroyed()) {
  124. return;
  125. }
  126. // Now wait for process() to run to finish loading
  127. loader._image = imageLoader.image;
  128. loader._mipLevels = imageLoader.mipLevels;
  129. loader._state = ResourceLoaderState.LOADED;
  130. return loader;
  131. } catch (error) {
  132. if (loader.isDestroyed()) {
  133. return;
  134. }
  135. loader.unload();
  136. loader._state = ResourceLoaderState.FAILED;
  137. const errorMessage = "Failed to load texture";
  138. throw loader.getError(errorMessage, error);
  139. }
  140. }
  141. /**
  142. * Loads the resource.
  143. * @returns {Promise<GltfDracoLoader>} A promise which resolves to the loader when the resource loading is completed.
  144. * @private
  145. */
  146. GltfTextureLoader.prototype.load = async function () {
  147. if (defined(this._promise)) {
  148. return this._promise;
  149. }
  150. this._state = ResourceLoaderState.LOADING;
  151. this._promise = loadResources(this);
  152. return this._promise;
  153. };
  154. function CreateTextureJob() {
  155. this.gltf = undefined;
  156. this.textureInfo = undefined;
  157. this.image = undefined;
  158. this.context = undefined;
  159. this.texture = undefined;
  160. }
  161. CreateTextureJob.prototype.set = function (
  162. gltf,
  163. textureInfo,
  164. image,
  165. mipLevels,
  166. context
  167. ) {
  168. this.gltf = gltf;
  169. this.textureInfo = textureInfo;
  170. this.image = image;
  171. this.mipLevels = mipLevels;
  172. this.context = context;
  173. };
  174. CreateTextureJob.prototype.execute = function () {
  175. this.texture = createTexture(
  176. this.gltf,
  177. this.textureInfo,
  178. this.image,
  179. this.mipLevels,
  180. this.context
  181. );
  182. };
  183. function createTexture(gltf, textureInfo, image, mipLevels, context) {
  184. // internalFormat is only defined for CompressedTextureBuffer
  185. const internalFormat = image.internalFormat;
  186. let compressedTextureNoMipmap = false;
  187. if (PixelFormat.isCompressedFormat(internalFormat) && !defined(mipLevels)) {
  188. compressedTextureNoMipmap = true;
  189. }
  190. const sampler = GltfLoaderUtil.createSampler({
  191. gltf: gltf,
  192. textureInfo: textureInfo,
  193. compressedTextureNoMipmap: compressedTextureNoMipmap,
  194. });
  195. const minFilter = sampler.minificationFilter;
  196. const wrapS = sampler.wrapS;
  197. const wrapT = sampler.wrapT;
  198. const samplerRequiresMipmap =
  199. minFilter === TextureMinificationFilter.NEAREST_MIPMAP_NEAREST ||
  200. minFilter === TextureMinificationFilter.NEAREST_MIPMAP_LINEAR ||
  201. minFilter === TextureMinificationFilter.LINEAR_MIPMAP_NEAREST ||
  202. minFilter === TextureMinificationFilter.LINEAR_MIPMAP_LINEAR;
  203. // generateMipmap is disallowed for compressed textures. Compressed textures
  204. // can have mipmaps but they must come with the KTX2 instead of generated by
  205. // WebGL. Also note from the KHR_texture_basisu spec:
  206. //
  207. // When a texture refers to a sampler with mipmap minification or when the
  208. // sampler is undefined, the KTX2 image SHOULD contain a full mip pyramid.
  209. //
  210. const generateMipmap = !defined(internalFormat) && samplerRequiresMipmap;
  211. // WebGL 1 requires power-of-two texture dimensions for mipmapping and REPEAT/MIRRORED_REPEAT wrap modes.
  212. const requiresPowerOfTwo =
  213. generateMipmap ||
  214. wrapS === TextureWrap.REPEAT ||
  215. wrapS === TextureWrap.MIRRORED_REPEAT ||
  216. wrapT === TextureWrap.REPEAT ||
  217. wrapT === TextureWrap.MIRRORED_REPEAT;
  218. const nonPowerOfTwo =
  219. !CesiumMath.isPowerOfTwo(image.width) ||
  220. !CesiumMath.isPowerOfTwo(image.height);
  221. const requiresResize = requiresPowerOfTwo && nonPowerOfTwo;
  222. let texture;
  223. if (defined(internalFormat)) {
  224. if (
  225. !context.webgl2 &&
  226. PixelFormat.isCompressedFormat(internalFormat) &&
  227. nonPowerOfTwo &&
  228. requiresPowerOfTwo
  229. ) {
  230. console.warn(
  231. "Compressed texture uses REPEAT or MIRRORED_REPEAT texture wrap mode and dimensions are not powers of two. The texture may be rendered incorrectly."
  232. );
  233. }
  234. texture = Texture.create({
  235. context: context,
  236. source: {
  237. arrayBufferView: image.bufferView, // Only defined for CompressedTextureBuffer
  238. mipLevels: mipLevels,
  239. },
  240. width: image.width,
  241. height: image.height,
  242. pixelFormat: image.internalFormat, // Only defined for CompressedTextureBuffer
  243. sampler: sampler,
  244. });
  245. } else {
  246. if (requiresResize) {
  247. image = resizeImageToNextPowerOfTwo(image);
  248. }
  249. texture = Texture.create({
  250. context: context,
  251. source: image,
  252. sampler: sampler,
  253. flipY: false,
  254. skipColorSpaceConversion: true,
  255. });
  256. }
  257. if (generateMipmap) {
  258. texture.generateMipmap();
  259. }
  260. return texture;
  261. }
  262. /**
  263. * Processes the resource until it becomes ready.
  264. *
  265. * @param {FrameState} frameState The frame state.
  266. * @returns {boolean} true once all resourced are ready.
  267. * @private
  268. */
  269. GltfTextureLoader.prototype.process = function (frameState) {
  270. //>>includeStart('debug', pragmas.debug);
  271. Check.typeOf.object("frameState", frameState);
  272. //>>includeEnd('debug');
  273. if (this._state === ResourceLoaderState.READY) {
  274. return true;
  275. }
  276. if (
  277. this._state !== ResourceLoaderState.LOADED &&
  278. this._state !== ResourceLoaderState.PROCESSING
  279. ) {
  280. return false;
  281. }
  282. if (defined(this._texture)) {
  283. // Already created texture
  284. return false;
  285. }
  286. if (!defined(this._image)) {
  287. // Not ready to create texture
  288. return false;
  289. }
  290. this._state = ResourceLoaderState.PROCESSING;
  291. let texture;
  292. if (this._asynchronous) {
  293. const textureJob = scratchTextureJob;
  294. textureJob.set(
  295. this._gltf,
  296. this._textureInfo,
  297. this._image,
  298. this._mipLevels,
  299. frameState.context
  300. );
  301. const jobScheduler = frameState.jobScheduler;
  302. if (!jobScheduler.execute(textureJob, JobType.TEXTURE)) {
  303. // Job scheduler is full. Try again next frame.
  304. return;
  305. }
  306. texture = textureJob.texture;
  307. } else {
  308. texture = createTexture(
  309. this._gltf,
  310. this._textureInfo,
  311. this._image,
  312. this._mipLevels,
  313. frameState.context
  314. );
  315. }
  316. // Unload everything except the texture
  317. this.unload();
  318. this._texture = texture;
  319. this._state = ResourceLoaderState.READY;
  320. this._resourceCache.statistics.addTextureLoader(this);
  321. return true;
  322. };
  323. /**
  324. * Unloads the resource.
  325. * @private
  326. */
  327. GltfTextureLoader.prototype.unload = function () {
  328. if (defined(this._texture)) {
  329. this._texture.destroy();
  330. }
  331. if (defined(this._imageLoader) && !this._imageLoader.isDestroyed()) {
  332. this._resourceCache.unload(this._imageLoader);
  333. }
  334. this._imageLoader = undefined;
  335. this._image = undefined;
  336. this._mipLevels = undefined;
  337. this._texture = undefined;
  338. this._gltf = undefined;
  339. };
  340. export default GltfTextureLoader;