equiRectangularCubeTexture.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { PanoramaToCubeMapTools } from '../../Misc/HighDynamicRange/panoramaToCubemap';
  2. import { Engine } from "../../Engines/engine";
  3. import { BaseTexture } from './baseTexture';
  4. import { Texture } from './texture';
  5. import { Scene } from "../../scene";
  6. import { Nullable } from "../../types";
  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. /** The six faces of the cube. */
  12. private static _FacesMapping = ['right', 'left', 'up', 'down', 'front', 'back'];
  13. private _noMipmap: boolean;
  14. private _onLoad: Nullable<() => void> = null;
  15. private _onError: Nullable<() => void> = null;
  16. /** The size of the cubemap. */
  17. private _size: number;
  18. /** The buffer of the image. */
  19. private _buffer: ArrayBuffer;
  20. /** The width of the input image. */
  21. private _width: number;
  22. /** The height of the input image. */
  23. private _height: number;
  24. /** The URL to the image. */
  25. public url: string;
  26. /** The texture coordinates mode. As this texture is stored in a cube format, please modify carefully. */
  27. public coordinatesMode = Texture.CUBIC_MODE;
  28. /**
  29. * Instantiates an EquiRectangularCubeTexture from the following parameters.
  30. * @param url The location of the image
  31. * @param scene The scene the texture will be used in
  32. * @param size The cubemap desired size (the more it increases the longer the generation will be)
  33. * @param noMipmap Forces to not generate the mipmap if true
  34. * @param gammaSpace Specifies if the texture will be used in gamma or linear space
  35. * (the PBR material requires those textures in linear space, but the standard material would require them in Gamma space)
  36. * @param onLoad — defines a callback called when texture is loaded
  37. * @param onError — defines a callback called if there is an error
  38. */
  39. constructor(
  40. url: string,
  41. scene: Scene,
  42. size: number,
  43. noMipmap: boolean = false,
  44. gammaSpace: boolean = true,
  45. onLoad: Nullable<() => void> = null,
  46. onError: Nullable<(message?: string, exception?: any) => void> = null
  47. ) {
  48. super(scene);
  49. if (!url) {
  50. throw new Error('Image url is not set');
  51. }
  52. this.name = url;
  53. this.url = url;
  54. this._size = size;
  55. this._noMipmap = noMipmap;
  56. this.gammaSpace = gammaSpace;
  57. this._onLoad = onLoad;
  58. this._onError = onError;
  59. this.hasAlpha = false;
  60. this.isCube = true;
  61. this._texture = this._getFromCache(url, this._noMipmap);
  62. if (!this._texture) {
  63. if (!scene.useDelayedTextureLoading) {
  64. this.loadImage(this.loadTexture.bind(this));
  65. } else {
  66. this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
  67. }
  68. }
  69. }
  70. /**
  71. * Load the image data, by putting the image on a canvas and extracting its buffer.
  72. */
  73. private loadImage(loadTextureCallback: () => void): void {
  74. const canvas = document.createElement('canvas');
  75. const image = new Image();
  76. image.addEventListener('load', () => {
  77. this._width = image.width;
  78. this._height = image.height;
  79. canvas.width = this._width;
  80. canvas.height = this._height;
  81. const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
  82. ctx.drawImage(image, 0, 0);
  83. const imageData = ctx.getImageData(0, 0, image.width, image.height);
  84. this._buffer = imageData.data.buffer as ArrayBuffer;
  85. canvas.remove();
  86. loadTextureCallback();
  87. });
  88. image.src = this.url;
  89. }
  90. /**
  91. * Convert the image buffer into a cubemap and create a CubeTexture.
  92. */
  93. private loadTexture(): void {
  94. const scene = this.getScene();
  95. const callback = (): Nullable<ArrayBufferView[]> => {
  96. const imageData = this.getFloat32ArrayFromArrayBuffer(this._buffer);
  97. // Extract the raw linear data.
  98. const data = PanoramaToCubeMapTools.ConvertPanoramaToCubemap(imageData, this._width, this._height, this._size);
  99. const results = [];
  100. // Push each faces.
  101. for (let i = 0; i < 6; i++) {
  102. const dataFace = (data as any)[EquiRectangularCubeTexture._FacesMapping[i]];
  103. results.push(dataFace);
  104. }
  105. return results;
  106. };
  107. if (!scene) {
  108. return;
  109. }
  110. this._texture = scene
  111. .getEngine()
  112. .createRawCubeTextureFromUrl(
  113. this.url,
  114. scene,
  115. this._size,
  116. Engine.TEXTUREFORMAT_RGB,
  117. scene.getEngine().getCaps().textureFloat
  118. ? Engine.TEXTURETYPE_FLOAT
  119. : Engine.TEXTURETYPE_UNSIGNED_INTEGER,
  120. this._noMipmap,
  121. callback,
  122. null,
  123. this._onLoad,
  124. this._onError
  125. );
  126. }
  127. /**
  128. * Convert the ArrayBuffer into a Float32Array and drop the transparency channel.
  129. * @param buffer The ArrayBuffer that should be converted.
  130. * @returns The buffer as Float32Array.
  131. */
  132. private getFloat32ArrayFromArrayBuffer(buffer: ArrayBuffer): Float32Array {
  133. const dataView = new DataView(buffer);
  134. const floatImageData = new Float32Array((buffer.byteLength * 3) / 4);
  135. let k = 0;
  136. for (let i = 0; i < buffer.byteLength; i++) {
  137. // We drop the transparency channel, because we do not need/want it
  138. if ((i + 1) % 4 !== 0) {
  139. floatImageData[k++] = dataView.getUint8(i) / 255;
  140. }
  141. }
  142. return floatImageData;
  143. }
  144. /**
  145. * Get the current class name of the texture useful for serialization or dynamic coding.
  146. * @returns "EquiRectangularCubeTexture"
  147. */
  148. public getClassName(): string {
  149. return "EquiRectangularCubeTexture";
  150. }
  151. /**
  152. * Create a clone of the current EquiRectangularCubeTexture and return it.
  153. * @returns A clone of the current EquiRectangularCubeTexture.
  154. */
  155. public clone(): EquiRectangularCubeTexture {
  156. const scene = this.getScene();
  157. if (!scene) {
  158. return this;
  159. }
  160. const newTexture = new EquiRectangularCubeTexture(this.url, scene, this._size, this._noMipmap, this.gammaSpace);
  161. // Base texture
  162. newTexture.level = this.level;
  163. newTexture.wrapU = this.wrapU;
  164. newTexture.wrapV = this.wrapV;
  165. newTexture.coordinatesIndex = this.coordinatesIndex;
  166. newTexture.coordinatesMode = this.coordinatesMode;
  167. return newTexture;
  168. }
  169. }