rawTexture.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Texture } from "./texture.js";
  2. import "../../Engines/Extensions/engine.rawTexture.js";
  3. /**
  4. * Raw texture can help creating a texture directly from an array of data.
  5. * This can be super useful if you either get the data from an uncompressed source or
  6. * if you wish to create your texture pixel by pixel.
  7. */
  8. export class RawTexture extends Texture {
  9. /**
  10. * Instantiates a new RawTexture.
  11. * Raw texture can help creating a texture directly from an array of data.
  12. * This can be super useful if you either get the data from an uncompressed source or
  13. * if you wish to create your texture pixel by pixel.
  14. * @param data define the array of data to use to create the texture (null to create an empty texture)
  15. * @param width define the width of the texture
  16. * @param height define the height of the texture
  17. * @param format define the format of the data (RGB, RGBA... Engine.TEXTUREFORMAT_xxx)
  18. * @param sceneOrEngine defines the scene or engine the texture will belong to
  19. * @param generateMipMaps define whether mip maps should be generated or not
  20. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  21. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  22. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  23. * @param creationFlags specific flags to use when creating the texture (1 for storage textures, for eg)
  24. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  25. */
  26. constructor(data, width, height,
  27. /**
  28. * Define the format of the data (RGB, RGBA... Engine.TEXTUREFORMAT_xxx)
  29. */
  30. format, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3, type = 0, creationFlags, useSRGBBuffer) {
  31. super(null, sceneOrEngine, !generateMipMaps, invertY, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, creationFlags);
  32. this.format = format;
  33. if (!this._engine) {
  34. return;
  35. }
  36. if (!this._engine._caps.textureFloatLinearFiltering && type === 1) {
  37. samplingMode = 1;
  38. }
  39. if (!this._engine._caps.textureHalfFloatLinearFiltering && type === 2) {
  40. samplingMode = 1;
  41. }
  42. this._texture = this._engine.createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode, null, type, creationFlags ?? 0, useSRGBBuffer ?? false);
  43. this.wrapU = Texture.CLAMP_ADDRESSMODE;
  44. this.wrapV = Texture.CLAMP_ADDRESSMODE;
  45. }
  46. /**
  47. * Updates the texture underlying data.
  48. * @param data Define the new data of the texture
  49. */
  50. update(data) {
  51. this._getEngine().updateRawTexture(this._texture, data, this._texture.format, this._texture.invertY, null, this._texture.type, this._texture._useSRGBBuffer);
  52. }
  53. /**
  54. * Creates a luminance texture from some data.
  55. * @param data Define the texture data
  56. * @param width Define the width of the texture
  57. * @param height Define the height of the texture
  58. * @param sceneOrEngine defines the scene or engine the texture will belong to
  59. * @param generateMipMaps Define whether or not to create mip maps for the texture
  60. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  61. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  62. * @returns the luminance texture
  63. */
  64. static CreateLuminanceTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3) {
  65. return new RawTexture(data, width, height, 1, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  66. }
  67. /**
  68. * Creates a luminance alpha texture from some data.
  69. * @param data Define the texture data
  70. * @param width Define the width of the texture
  71. * @param height Define the height of the texture
  72. * @param sceneOrEngine defines the scene or engine the texture will belong to
  73. * @param generateMipMaps Define whether or not to create mip maps for the texture
  74. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  75. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  76. * @returns the luminance alpha texture
  77. */
  78. static CreateLuminanceAlphaTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3) {
  79. return new RawTexture(data, width, height, 2, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  80. }
  81. /**
  82. * Creates an alpha texture from some data.
  83. * @param data Define the texture data
  84. * @param width Define the width of the texture
  85. * @param height Define the height of the texture
  86. * @param sceneOrEngine defines the scene or engine the texture will belong to
  87. * @param generateMipMaps Define whether or not to create mip maps for the texture
  88. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  89. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  90. * @returns the alpha texture
  91. */
  92. static CreateAlphaTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3) {
  93. return new RawTexture(data, width, height, 0, sceneOrEngine, generateMipMaps, invertY, samplingMode);
  94. }
  95. /**
  96. * Creates a RGB texture from some data.
  97. * @param data Define the texture data
  98. * @param width Define the width of the texture
  99. * @param height Define the height of the texture
  100. * @param sceneOrEngine defines the scene or engine the texture will belong to
  101. * @param generateMipMaps Define whether or not to create mip maps for the texture
  102. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  103. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  104. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  105. * @param creationFlags specific flags to use when creating the texture (1 for storage textures, for eg)
  106. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  107. * @returns the RGB alpha texture
  108. */
  109. static CreateRGBTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3, type = 0, creationFlags = 0, useSRGBBuffer = false) {
  110. return new RawTexture(data, width, height, 4, sceneOrEngine, generateMipMaps, invertY, samplingMode, type, creationFlags, useSRGBBuffer);
  111. }
  112. /**
  113. * Creates a RGBA texture from some data.
  114. * @param data Define the texture data
  115. * @param width Define the width of the texture
  116. * @param height Define the height of the texture
  117. * @param sceneOrEngine defines the scene or engine the texture will belong to
  118. * @param generateMipMaps Define whether or not to create mip maps for the texture
  119. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  120. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  121. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  122. * @param creationFlags specific flags to use when creating the texture (1 for storage textures, for eg)
  123. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  124. * @returns the RGBA texture
  125. */
  126. static CreateRGBATexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3, type = 0, creationFlags = 0, useSRGBBuffer = false) {
  127. return new RawTexture(data, width, height, 5, sceneOrEngine, generateMipMaps, invertY, samplingMode, type, creationFlags, useSRGBBuffer);
  128. }
  129. /**
  130. * Creates a RGBA storage texture from some data.
  131. * @param data Define the texture data
  132. * @param width Define the width of the texture
  133. * @param height Define the height of the texture
  134. * @param sceneOrEngine defines the scene or engine the texture will belong to
  135. * @param generateMipMaps Define whether or not to create mip maps for the texture
  136. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  137. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  138. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  139. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  140. * @returns the RGBA texture
  141. */
  142. static CreateRGBAStorageTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = 3, type = 0, useSRGBBuffer = false) {
  143. return new RawTexture(data, width, height, 5, sceneOrEngine, generateMipMaps, invertY, samplingMode, type, 1, useSRGBBuffer);
  144. }
  145. /**
  146. * Creates a R texture from some data.
  147. * @param data Define the texture data
  148. * @param width Define the width of the texture
  149. * @param height Define the height of the texture
  150. * @param sceneOrEngine defines the scene or engine the texture will belong to
  151. * @param generateMipMaps Define whether or not to create mip maps for the texture
  152. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  153. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  154. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  155. * @returns the R texture
  156. */
  157. static CreateRTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = Texture.TRILINEAR_SAMPLINGMODE, type = 1) {
  158. return new RawTexture(data, width, height, 6, sceneOrEngine, generateMipMaps, invertY, samplingMode, type);
  159. }
  160. /**
  161. * Creates a R storage texture from some data.
  162. * @param data Define the texture data
  163. * @param width Define the width of the texture
  164. * @param height Define the height of the texture
  165. * @param sceneOrEngine defines the scene or engine the texture will belong to
  166. * @param generateMipMaps Define whether or not to create mip maps for the texture
  167. * @param invertY define if the data should be flipped on Y when uploaded to the GPU
  168. * @param samplingMode define the texture sampling mode (Texture.xxx_SAMPLINGMODE)
  169. * @param type define the format of the data (int, float... Engine.TEXTURETYPE_xxx)
  170. * @returns the R texture
  171. */
  172. static CreateRStorageTexture(data, width, height, sceneOrEngine, generateMipMaps = true, invertY = false, samplingMode = Texture.TRILINEAR_SAMPLINGMODE, type = 1) {
  173. return new RawTexture(data, width, height, 6, sceneOrEngine, generateMipMaps, invertY, samplingMode, type, 1);
  174. }
  175. }
  176. //# sourceMappingURL=rawTexture.js.map