rawTexture.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { Texture } from "./texture";
  2. import { Constants } from "../../Engines/constants";
  3. import "../../Engines/Extensions/engine.rawTexture";
  4. import { Nullable } from '../../types';
  5. import { ThinEngine } from '../../Engines/thinEngine';
  6. declare type Scene = import("../../scene").Scene;
  7. /**
  8. * Raw texture can help creating a texture directly from an array of data.
  9. * This can be super useful if you either get the data from an uncompressed source or
  10. * if you wish to create your texture pixel by pixel.
  11. */
  12. export class RawTexture extends Texture {
  13. /**
  14. * Instantiates a new RawTexture.
  15. * Raw texture can help creating a texture directly from an array of data.
  16. * This can be super useful if you either get the data from an uncompressed source or
  17. * if you wish to create your texture pixel by pixel.
  18. * @param data define the array of data to use to create the texture
  19. * @param width define the width of the texture
  20. * @param height define the height of the texture
  21. * @param format define the format of the data (RGB, RGBA... Engine.TEXTUREFORMAT_xxx)
  22. * @param sceneOrEngine defines the scene or engine the texture will belong to
  23. * @param generateMipMaps define whether mip maps should be generated or not
  24. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  25. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  26. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  27. */
  28. constructor(data: ArrayBufferView, width: number, height: number,
  29. /**
  30. * Define the format of the data (RGB, RGBA... Engine.TEXTUREFORMAT_xxx)
  31. */
  32. public format: number,
  33. sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, type: number = Constants.TEXTURETYPE_UNSIGNED_INT) {
  34. super(null, sceneOrEngine, !generateMipMaps, invertY);
  35. if (!this._engine) {
  36. return;
  37. }
  38. this._texture = this._engine.createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode, null, type);
  39. this.wrapU = Texture.CLAMP_ADDRESSMODE;
  40. this.wrapV = Texture.CLAMP_ADDRESSMODE;
  41. }
  42. /**
  43. * Updates the texture underlying data.
  44. * @param data Define the new data of the texture
  45. */
  46. public update(data: ArrayBufferView): void {
  47. this._getEngine()!.updateRawTexture(this._texture, data, this._texture!.format, this._texture!.invertY, null, this._texture!.type);
  48. }
  49. /**
  50. * Creates a luminance texture from some data.
  51. * @param data Define the texture data
  52. * @param width Define the width of the texture
  53. * @param height Define the height of the texture
  54. * @param sceneOrEngine defines the scene or engine the texture will belong to
  55. * @param generateMipMaps Define whether or not to create mip maps for the texture
  56. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  57. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  58. * @returns the luminance texture
  59. */
  60. public static CreateLuminanceTexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE): RawTexture {
  61. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_LUMINANCE, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  62. }
  63. /**
  64. * Creates a luminance alpha texture from some data.
  65. * @param data Define the texture data
  66. * @param width Define the width of the texture
  67. * @param height Define the height of the texture
  68. * @param sceneOrEngine defines the scene or engine the texture will belong to
  69. * @param generateMipMaps Define whether or not to create mip maps for the texture
  70. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  71. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  72. * @returns the luminance alpha texture
  73. */
  74. public static CreateLuminanceAlphaTexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE): RawTexture {
  75. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_LUMINANCE_ALPHA, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  76. }
  77. /**
  78. * Creates an alpha texture from some data.
  79. * @param data Define the texture data
  80. * @param width Define the width of the texture
  81. * @param height Define the height of the texture
  82. * @param sceneOrEngine defines the scene or engine the texture will belong to
  83. * @param generateMipMaps Define whether or not to create mip maps for the texture
  84. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  85. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  86. * @returns the alpha texture
  87. */
  88. public static CreateAlphaTexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE): RawTexture {
  89. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_ALPHA, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  90. }
  91. /**
  92. * Creates a RGB texture from some data.
  93. * @param data Define the texture data
  94. * @param width Define the width of the texture
  95. * @param height Define the height of the texture
  96. * @param sceneOrEngine defines the scene or engine the texture will belong to
  97. * @param generateMipMaps Define whether or not to create mip maps for the texture
  98. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  99. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  100. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  101. * @returns the RGB alpha texture
  102. */
  103. public static CreateRGBTexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, type: number = Constants.TEXTURETYPE_UNSIGNED_INT): RawTexture {
  104. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_RGB, sceneOrEngine, generateMipMaps, invertY, samplingMode, type);
  105. }
  106. /**
  107. * Creates a RGBA texture from some data.
  108. * @param data Define the texture data
  109. * @param width Define the width of the texture
  110. * @param height Define the height of the texture
  111. * @param sceneOrEngine defines the scene or engine the texture will belong to
  112. * @param generateMipMaps Define whether or not to create mip maps for the texture
  113. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  114. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  115. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  116. * @returns the RGBA texture
  117. */
  118. public static CreateRGBATexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, type: number = Constants.TEXTURETYPE_UNSIGNED_INT): RawTexture {
  119. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_RGBA, sceneOrEngine, generateMipMaps, invertY, samplingMode, type);
  120. }
  121. /**
  122. * Creates a R texture from some data.
  123. * @param data Define the texture data
  124. * @param width Define the width of the texture
  125. * @param height Define the height of the texture
  126. * @param sceneOrEngine defines the scene or engine the texture will belong to
  127. * @param generateMipMaps Define whether or not to create mip maps for the texture
  128. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  129. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  130. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  131. * @returns the R texture
  132. */
  133. public static CreateRTexture(data: ArrayBufferView, width: number, height: number, sceneOrEngine: Nullable<Scene | ThinEngine>, generateMipMaps: boolean = true, invertY: boolean = false, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, type: number = Constants.TEXTURETYPE_FLOAT): RawTexture {
  134. return new RawTexture(data, width, height, Constants.TEXTUREFORMAT_R, sceneOrEngine, generateMipMaps, invertY, samplingMode, type);
  135. }
  136. }