abstractEngine.cubeTexture.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { InternalTexture, InternalTextureSource } from "../../Materials/Textures/internalTexture.js";
  2. import { Logger } from "../../Misc/logger.js";
  3. import { LoadImage } from "../../Misc/fileTools.js";
  4. import { RandomGUID } from "../../Misc/guid.js";
  5. import { AbstractEngine } from "../abstractEngine.js";
  6. AbstractEngine.prototype._partialLoadFile = function (url, index, loadedFiles, onfinish, onErrorCallBack = null) {
  7. const onload = (data) => {
  8. loadedFiles[index] = data;
  9. loadedFiles._internalCount++;
  10. if (loadedFiles._internalCount === 6) {
  11. onfinish(loadedFiles);
  12. }
  13. };
  14. const onerror = (request, exception) => {
  15. if (onErrorCallBack && request) {
  16. onErrorCallBack(request.status + " " + request.statusText, exception);
  17. }
  18. };
  19. this._loadFile(url, onload, undefined, undefined, true, onerror);
  20. };
  21. AbstractEngine.prototype._cascadeLoadFiles = function (scene, onfinish, files, onError = null) {
  22. const loadedFiles = [];
  23. loadedFiles._internalCount = 0;
  24. for (let index = 0; index < 6; index++) {
  25. this._partialLoadFile(files[index], index, loadedFiles, onfinish, onError);
  26. }
  27. };
  28. AbstractEngine.prototype._cascadeLoadImgs = function (scene, texture, onfinish, files, onError = null, mimeType) {
  29. const loadedImages = [];
  30. loadedImages._internalCount = 0;
  31. for (let index = 0; index < 6; index++) {
  32. this._partialLoadImg(files[index], index, loadedImages, scene, texture, onfinish, onError, mimeType);
  33. }
  34. };
  35. AbstractEngine.prototype._partialLoadImg = function (url, index, loadedImages, scene, texture, onfinish, onErrorCallBack = null, mimeType) {
  36. const tokenPendingData = RandomGUID();
  37. const onload = (img) => {
  38. loadedImages[index] = img;
  39. loadedImages._internalCount++;
  40. if (scene) {
  41. scene.removePendingData(tokenPendingData);
  42. }
  43. if (loadedImages._internalCount === 6 && onfinish) {
  44. onfinish(texture, loadedImages);
  45. }
  46. };
  47. const onerror = (message, exception) => {
  48. if (scene) {
  49. scene.removePendingData(tokenPendingData);
  50. }
  51. if (onErrorCallBack) {
  52. onErrorCallBack(message, exception);
  53. }
  54. };
  55. LoadImage(url, onload, onerror, scene ? scene.offlineProvider : null, mimeType);
  56. if (scene) {
  57. scene.addPendingData(tokenPendingData);
  58. }
  59. };
  60. AbstractEngine.prototype.createCubeTextureBase = function (rootUrl, scene, files, noMipmap, onLoad = null, onError = null, format, forcedExtension = null, createPolynomials = false, lodScale = 0, lodOffset = 0, fallback = null, beforeLoadCubeDataCallback = null, imageHandler = null, useSRGBBuffer = false) {
  61. const texture = fallback ? fallback : new InternalTexture(this, InternalTextureSource.Cube);
  62. texture.isCube = true;
  63. texture.url = rootUrl;
  64. texture.generateMipMaps = !noMipmap;
  65. texture._lodGenerationScale = lodScale;
  66. texture._lodGenerationOffset = lodOffset;
  67. texture._useSRGBBuffer = !!useSRGBBuffer && this._caps.supportSRGBBuffers && (this.version > 1 || this.isWebGPU || !!noMipmap);
  68. if (texture !== fallback) {
  69. texture.label = rootUrl.substring(0, 60); // default label, can be overriden by the caller
  70. }
  71. if (!this._doNotHandleContextLost) {
  72. texture._extension = forcedExtension;
  73. texture._files = files;
  74. }
  75. const originalRootUrl = rootUrl;
  76. if (this._transformTextureUrl && !fallback) {
  77. rootUrl = this._transformTextureUrl(rootUrl);
  78. }
  79. const rootUrlWithoutUriParams = rootUrl.split("?")[0];
  80. const lastDot = rootUrlWithoutUriParams.lastIndexOf(".");
  81. const extension = forcedExtension ? forcedExtension : lastDot > -1 ? rootUrlWithoutUriParams.substring(lastDot).toLowerCase() : "";
  82. let loader = null;
  83. for (const availableLoader of AbstractEngine._TextureLoaders) {
  84. if (availableLoader.canLoad(extension)) {
  85. loader = availableLoader;
  86. break;
  87. }
  88. }
  89. const onInternalError = (request, exception) => {
  90. if (rootUrl === originalRootUrl) {
  91. if (onError && request) {
  92. onError(request.status + " " + request.statusText, exception);
  93. }
  94. }
  95. else {
  96. // fall back to the original url if the transformed url fails to load
  97. Logger.Warn(`Failed to load ${rootUrl}, falling back to the ${originalRootUrl}`);
  98. this.createCubeTextureBase(originalRootUrl, scene, files, !!noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, texture, beforeLoadCubeDataCallback, imageHandler, useSRGBBuffer);
  99. }
  100. };
  101. if (loader) {
  102. const onloaddata = (data) => {
  103. if (beforeLoadCubeDataCallback) {
  104. beforeLoadCubeDataCallback(texture, data);
  105. }
  106. loader.loadCubeData(data, texture, createPolynomials, onLoad, onError);
  107. };
  108. if (files && files.length === 6) {
  109. if (loader.supportCascades) {
  110. this._cascadeLoadFiles(scene, (images) => onloaddata(images.map((image) => new Uint8Array(image))), files, onError);
  111. }
  112. else {
  113. if (onError) {
  114. onError("Textures type does not support cascades.");
  115. }
  116. else {
  117. Logger.Warn("Texture loader does not support cascades.");
  118. }
  119. }
  120. }
  121. else {
  122. this._loadFile(rootUrl, (data) => onloaddata(new Uint8Array(data)), undefined, undefined, true, onInternalError);
  123. }
  124. }
  125. else {
  126. if (!files || files.length === 0) {
  127. throw new Error("Cannot load cubemap because files were not defined, or the correct loader was not found.");
  128. }
  129. this._cascadeLoadImgs(scene, texture, (texture, imgs) => {
  130. if (imageHandler) {
  131. imageHandler(texture, imgs);
  132. }
  133. }, files, onError);
  134. }
  135. this._internalTexturesCache.push(texture);
  136. return texture;
  137. };
  138. //# sourceMappingURL=abstractEngine.cubeTexture.js.map