rawCubeTexture.ts 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { SerializationHelper } from "../../Misc/decorators";
  2. import { EnvironmentTextureTools } from "../../Misc/environmentTextureTools";
  3. import { Nullable } from "../../types";
  4. import { Scene } from "../../scene";
  5. import { SphericalPolynomial } from "../../Maths/sphericalPolynomial";
  6. import { InternalTexture } from "../../Materials/Textures/internalTexture";
  7. import { CubeTexture } from "../../Materials/Textures/cubeTexture";
  8. import { Constants } from "../../Engines/constants";
  9. /**
  10. * Raw cube texture where the raw buffers are passed in
  11. */
  12. export class RawCubeTexture extends CubeTexture {
  13. /**
  14. * Creates a cube texture where the raw buffers are passed in.
  15. * @param scene defines the scene the texture is attached to
  16. * @param data defines the array of data to use to create each face
  17. * @param size defines the size of the textures
  18. * @param format defines the format of the data
  19. * @param type defines the type of the data (like Engine.TEXTURETYPE_UNSIGNED_INT)
  20. * @param generateMipMaps defines if the engine should generate the mip levels
  21. * @param invertY defines if data must be stored with Y axis inverted
  22. * @param samplingMode defines the required sampling mode (like Texture.NEAREST_SAMPLINGMODE)
  23. * @param compression defines the compression used (null by default)
  24. */
  25. constructor(scene: Scene, data: Nullable<ArrayBufferView[]>, size: number,
  26. format: number = Constants.TEXTUREFORMAT_RGBA, type: number = Constants.TEXTURETYPE_UNSIGNED_INT,
  27. generateMipMaps: boolean = false, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  28. compression: Nullable<string> = null) {
  29. super("", scene);
  30. this._texture = scene.getEngine().createRawCubeTexture(data, size, format, type, generateMipMaps, invertY, samplingMode, compression);
  31. }
  32. /**
  33. * Updates the raw cube texture.
  34. * @param data defines the data to store
  35. * @param format defines the data format
  36. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  37. * @param invertY defines if data must be stored with Y axis inverted
  38. * @param compression defines the compression used (null by default)
  39. * @param level defines which level of the texture to update
  40. */
  41. public update(data: ArrayBufferView[], format: number, type: number, invertY: boolean, compression: Nullable<string> = null): void {
  42. this._texture!.getEngine().updateRawCubeTexture(this._texture!, data, format, type, invertY, compression);
  43. }
  44. /**
  45. * Updates a raw cube texture with RGBD encoded data.
  46. * @param data defines the array of data [mipmap][face] to use to create each face
  47. * @param sphericalPolynomial defines the spherical polynomial for irradiance
  48. * @param lodScale defines the scale applied to environment texture. This manages the range of LOD level used for IBL according to the roughness
  49. * @param lodOffset defines the offset applied to environment texture. This manages first LOD level used for IBL according to the roughness
  50. * @returns a promsie that resolves when the operation is complete
  51. */
  52. public updateRGBDAsync(data: ArrayBufferView[][], sphericalPolynomial: Nullable<SphericalPolynomial> = null, lodScale: number = 0.8, lodOffset: number = 0): Promise<void> {
  53. return RawCubeTexture._UpdateRGBDAsync(this._texture!, data, sphericalPolynomial, lodScale, lodOffset);
  54. }
  55. /**
  56. * Clones the raw cube texture.
  57. * @return a new cube texture
  58. */
  59. public clone(): CubeTexture {
  60. return SerializationHelper.Clone(() => {
  61. const scene = this.getScene()!;
  62. const internalTexture = this._texture!;
  63. const texture = new RawCubeTexture(scene, internalTexture._bufferViewArray!, internalTexture.width, internalTexture.format, internalTexture.type,
  64. internalTexture.generateMipMaps, internalTexture.invertY, internalTexture.samplingMode, internalTexture._compression);
  65. if (internalTexture.dataSource === InternalTexture.DATASOURCE_CUBERAW_RGBD) {
  66. texture.updateRGBDAsync(internalTexture._bufferViewArrayArray!, internalTexture._sphericalPolynomial, internalTexture._lodGenerationScale, internalTexture._lodGenerationOffset);
  67. }
  68. return texture;
  69. }, this);
  70. }
  71. /** @hidden */
  72. public static _UpdateRGBDAsync(internalTexture: InternalTexture, data: ArrayBufferView[][], sphericalPolynomial: Nullable<SphericalPolynomial>, lodScale: number, lodOffset: number): Promise<void> {
  73. internalTexture._dataSource = InternalTexture.DATASOURCE_CUBERAW_RGBD;
  74. internalTexture._bufferViewArrayArray = data;
  75. internalTexture._lodGenerationScale = lodScale;
  76. internalTexture._lodGenerationOffset = lodOffset;
  77. internalTexture._sphericalPolynomial = sphericalPolynomial;
  78. return EnvironmentTextureTools.UploadLevelsAsync(internalTexture, data).then(() => {
  79. internalTexture.isReady = true;
  80. });
  81. }
  82. }