equiRectangularCubeTexture.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { PanoramaToCubeMapTools } from "../../Misc/HighDynamicRange/panoramaToCubemap.js";
  2. import { BaseTexture } from "./baseTexture.js";
  3. import { Texture } from "./texture.js";
  4. import { Tools } from "../../Misc/tools.js";
  5. import "../../Engines/Extensions/engine.rawTexture.js";
  6. import { LoadImage } from "../../Misc/fileTools.js";
  7. /**
  8. * This represents a texture coming from an equirectangular image supported by the web browser canvas.
  9. */
  10. export class EquiRectangularCubeTexture extends BaseTexture {
  11. /**
  12. * Instantiates an EquiRectangularCubeTexture from the following parameters.
  13. * @param url The location of the image
  14. * @param scene The scene the texture will be used in
  15. * @param size The cubemap desired size (the more it increases the longer the generation will be)
  16. * @param noMipmap Forces to not generate the mipmap if true
  17. * @param gammaSpace Specifies if the texture will be used in gamma or linear space
  18. * (the PBR material requires those textures in linear space, but the standard material would require them in Gamma space)
  19. * @param onLoad — defines a callback called when texture is loaded
  20. * @param onError — defines a callback called if there is an error
  21. * @param supersample — defines if texture must be supersampled (default: false)
  22. */
  23. constructor(url, scene, size, noMipmap = false, gammaSpace = true, onLoad = null, onError = null, supersample = false) {
  24. super(scene);
  25. this._onLoad = null;
  26. this._onError = null;
  27. if (!url) {
  28. throw new Error("Image url is not set");
  29. }
  30. this._coordinatesMode = Texture.CUBIC_MODE;
  31. this.name = url;
  32. this.url = url;
  33. this._size = size;
  34. this._supersample = supersample;
  35. this._noMipmap = noMipmap;
  36. this.gammaSpace = gammaSpace;
  37. this._onLoad = onLoad;
  38. this._onError = onError;
  39. this.hasAlpha = false;
  40. this.isCube = true;
  41. this._texture = this._getFromCache(url, this._noMipmap, undefined, undefined, undefined, this.isCube);
  42. if (!this._texture) {
  43. if (!scene.useDelayedTextureLoading) {
  44. this._loadImage(() => this._loadTexture(), this._onError);
  45. }
  46. else {
  47. this.delayLoadState = 4;
  48. }
  49. }
  50. else if (onLoad) {
  51. if (this._texture.isReady) {
  52. Tools.SetImmediate(() => onLoad());
  53. }
  54. else {
  55. this._texture.onLoadedObservable.add(onLoad);
  56. }
  57. }
  58. }
  59. /**
  60. * Load the image data, by putting the image on a canvas and extracting its buffer.
  61. * @param loadTextureCallback
  62. * @param onError
  63. */
  64. _loadImage(loadTextureCallback, onError) {
  65. const scene = this.getScene();
  66. if (!scene) {
  67. return;
  68. }
  69. // Create texture before loading
  70. const texture = scene
  71. .getEngine()
  72. .createRawCubeTexture(null, this._size, 4, scene.getEngine().getCaps().textureFloat ? 1 : 7, this._noMipmap, false, 3);
  73. texture.generateMipMaps = !this._noMipmap;
  74. scene.addPendingData(texture);
  75. texture.url = this.url;
  76. texture.isReady = false;
  77. scene.getEngine()._internalTexturesCache.push(texture);
  78. this._texture = texture;
  79. const canvas = document.createElement("canvas");
  80. LoadImage(this.url, (image) => {
  81. this._width = image.width;
  82. this._height = image.height;
  83. canvas.width = this._width;
  84. canvas.height = this._height;
  85. const ctx = canvas.getContext("2d");
  86. ctx.drawImage(image, 0, 0);
  87. const imageData = ctx.getImageData(0, 0, image.width, image.height);
  88. this._buffer = imageData.data.buffer;
  89. canvas.remove();
  90. loadTextureCallback();
  91. }, (_, e) => {
  92. scene.removePendingData(texture);
  93. if (onError) {
  94. onError(`${this.getClassName()} could not be loaded`, e);
  95. }
  96. }, scene ? scene.offlineProvider : null);
  97. }
  98. /**
  99. * Convert the image buffer into a cubemap and create a CubeTexture.
  100. */
  101. _loadTexture() {
  102. const scene = this.getScene();
  103. const callback = () => {
  104. const imageData = this._getFloat32ArrayFromArrayBuffer(this._buffer);
  105. // Extract the raw linear data.
  106. const data = PanoramaToCubeMapTools.ConvertPanoramaToCubemap(imageData, this._width, this._height, this._size, this._supersample);
  107. const results = [];
  108. // Push each faces.
  109. for (let i = 0; i < 6; i++) {
  110. const dataFace = data[EquiRectangularCubeTexture._FacesMapping[i]];
  111. results.push(dataFace);
  112. }
  113. return results;
  114. };
  115. if (!scene) {
  116. return;
  117. }
  118. const faceDataArrays = callback();
  119. const texture = this._texture;
  120. scene.getEngine().updateRawCubeTexture(texture, faceDataArrays, texture.format, texture.type, texture.invertY);
  121. texture.isReady = true;
  122. scene.removePendingData(texture);
  123. texture.onLoadedObservable.notifyObservers(texture);
  124. texture.onLoadedObservable.clear();
  125. if (this._onLoad) {
  126. this._onLoad();
  127. }
  128. }
  129. /**
  130. * Convert the ArrayBuffer into a Float32Array and drop the transparency channel.
  131. * @param buffer The ArrayBuffer that should be converted.
  132. * @returns The buffer as Float32Array.
  133. */
  134. _getFloat32ArrayFromArrayBuffer(buffer) {
  135. const dataView = new DataView(buffer);
  136. const floatImageData = new Float32Array((buffer.byteLength * 3) / 4);
  137. let k = 0;
  138. for (let i = 0; i < buffer.byteLength; i++) {
  139. // We drop the transparency channel, because we do not need/want it
  140. if ((i + 1) % 4 !== 0) {
  141. floatImageData[k++] = dataView.getUint8(i) / 255;
  142. }
  143. }
  144. return floatImageData;
  145. }
  146. /**
  147. * Get the current class name of the texture useful for serialization or dynamic coding.
  148. * @returns "EquiRectangularCubeTexture"
  149. */
  150. getClassName() {
  151. return "EquiRectangularCubeTexture";
  152. }
  153. /**
  154. * Create a clone of the current EquiRectangularCubeTexture and return it.
  155. * @returns A clone of the current EquiRectangularCubeTexture.
  156. */
  157. clone() {
  158. const scene = this.getScene();
  159. if (!scene) {
  160. return this;
  161. }
  162. const newTexture = new EquiRectangularCubeTexture(this.url, scene, this._size, this._noMipmap, this.gammaSpace);
  163. // Base texture
  164. newTexture.level = this.level;
  165. newTexture.wrapU = this.wrapU;
  166. newTexture.wrapV = this.wrapV;
  167. newTexture.coordinatesIndex = this.coordinatesIndex;
  168. newTexture.coordinatesMode = this.coordinatesMode;
  169. return newTexture;
  170. }
  171. }
  172. /** The six faces of the cube. */
  173. EquiRectangularCubeTexture._FacesMapping = ["right", "left", "up", "down", "front", "back"];
  174. //# sourceMappingURL=equiRectangularCubeTexture.js.map