thinTexture.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { Size } from "../../Maths/math.size.js";
  2. /**
  3. * Base class of all the textures in babylon.
  4. * It groups all the common properties required to work with Thin Engine.
  5. */
  6. export class ThinTexture {
  7. /**
  8. * | Value | Type | Description |
  9. * | ----- | ------------------ | ----------- |
  10. * | 0 | CLAMP_ADDRESSMODE | |
  11. * | 1 | WRAP_ADDRESSMODE | |
  12. * | 2 | MIRROR_ADDRESSMODE | |
  13. */
  14. get wrapU() {
  15. return this._wrapU;
  16. }
  17. set wrapU(value) {
  18. this._wrapU = value;
  19. }
  20. /**
  21. * | Value | Type | Description |
  22. * | ----- | ------------------ | ----------- |
  23. * | 0 | CLAMP_ADDRESSMODE | |
  24. * | 1 | WRAP_ADDRESSMODE | |
  25. * | 2 | MIRROR_ADDRESSMODE | |
  26. */
  27. get wrapV() {
  28. return this._wrapV;
  29. }
  30. set wrapV(value) {
  31. this._wrapV = value;
  32. }
  33. /**
  34. * How a texture is mapped.
  35. * Unused in thin texture mode.
  36. */
  37. get coordinatesMode() {
  38. return 0;
  39. }
  40. /**
  41. * Define if the texture is a cube texture or if false a 2d texture.
  42. */
  43. get isCube() {
  44. if (!this._texture) {
  45. return false;
  46. }
  47. return this._texture.isCube;
  48. }
  49. // eslint-disable-next-line @typescript-eslint/naming-convention
  50. set isCube(value) {
  51. if (!this._texture) {
  52. return;
  53. }
  54. this._texture.isCube = value;
  55. }
  56. /**
  57. * Define if the texture is a 3d texture (webgl 2) or if false a 2d texture.
  58. */
  59. get is3D() {
  60. if (!this._texture) {
  61. return false;
  62. }
  63. return this._texture.is3D;
  64. }
  65. // eslint-disable-next-line @typescript-eslint/naming-convention
  66. set is3D(value) {
  67. if (!this._texture) {
  68. return;
  69. }
  70. this._texture.is3D = value;
  71. }
  72. /**
  73. * Define if the texture is a 2d array texture (webgl 2) or if false a 2d texture.
  74. */
  75. get is2DArray() {
  76. if (!this._texture) {
  77. return false;
  78. }
  79. return this._texture.is2DArray;
  80. }
  81. // eslint-disable-next-line @typescript-eslint/naming-convention
  82. set is2DArray(value) {
  83. if (!this._texture) {
  84. return;
  85. }
  86. this._texture.is2DArray = value;
  87. }
  88. /**
  89. * Get the class name of the texture.
  90. * @returns "ThinTexture"
  91. */
  92. getClassName() {
  93. return "ThinTexture";
  94. }
  95. static _IsRenderTargetWrapper(texture) {
  96. return texture?.shareDepth !== undefined;
  97. }
  98. /**
  99. * Instantiates a new ThinTexture.
  100. * Base class of all the textures in babylon.
  101. * This can be used as an internal texture wrapper in AbstractEngine to benefit from the cache
  102. * @param internalTexture Define the internalTexture to wrap. You can also pass a RenderTargetWrapper, in which case the texture will be the render target's texture
  103. */
  104. constructor(internalTexture) {
  105. this._wrapU = 1;
  106. this._wrapV = 1;
  107. /**
  108. * | Value | Type | Description |
  109. * | ----- | ------------------ | ----------- |
  110. * | 0 | CLAMP_ADDRESSMODE | |
  111. * | 1 | WRAP_ADDRESSMODE | |
  112. * | 2 | MIRROR_ADDRESSMODE | |
  113. */
  114. this.wrapR = 1;
  115. /**
  116. * With compliant hardware and browser (supporting anisotropic filtering)
  117. * this defines the level of anisotropic filtering in the texture.
  118. * The higher the better but the slower. This defaults to 4 as it seems to be the best tradeoff.
  119. */
  120. this.anisotropicFilteringLevel = 4;
  121. /**
  122. * Define the current state of the loading sequence when in delayed load mode.
  123. */
  124. this.delayLoadState = 0;
  125. /** @internal */
  126. this._texture = null;
  127. this._engine = null;
  128. this._cachedSize = Size.Zero();
  129. this._cachedBaseSize = Size.Zero();
  130. /** @internal */
  131. this._initialSamplingMode = 2;
  132. this._texture = ThinTexture._IsRenderTargetWrapper(internalTexture) ? internalTexture.texture : internalTexture;
  133. if (this._texture) {
  134. this._engine = this._texture.getEngine();
  135. }
  136. }
  137. /**
  138. * Get if the texture is ready to be used (downloaded, converted, mip mapped...).
  139. * @returns true if fully ready
  140. */
  141. isReady() {
  142. if (this.delayLoadState === 4) {
  143. this.delayLoad();
  144. return false;
  145. }
  146. if (this._texture) {
  147. return this._texture.isReady;
  148. }
  149. return false;
  150. }
  151. /**
  152. * Triggers the load sequence in delayed load mode.
  153. */
  154. delayLoad() { }
  155. /**
  156. * Get the underlying lower level texture from Babylon.
  157. * @returns the internal texture
  158. */
  159. getInternalTexture() {
  160. return this._texture;
  161. }
  162. /**
  163. * Get the size of the texture.
  164. * @returns the texture size.
  165. */
  166. getSize() {
  167. if (this._texture) {
  168. if (this._texture.width) {
  169. this._cachedSize.width = this._texture.width;
  170. this._cachedSize.height = this._texture.height;
  171. return this._cachedSize;
  172. }
  173. if (this._texture._size) {
  174. this._cachedSize.width = this._texture._size;
  175. this._cachedSize.height = this._texture._size;
  176. return this._cachedSize;
  177. }
  178. }
  179. return this._cachedSize;
  180. }
  181. /**
  182. * Get the base size of the texture.
  183. * It can be different from the size if the texture has been resized for POT for instance
  184. * @returns the base size
  185. */
  186. getBaseSize() {
  187. if (!this.isReady() || !this._texture) {
  188. this._cachedBaseSize.width = 0;
  189. this._cachedBaseSize.height = 0;
  190. return this._cachedBaseSize;
  191. }
  192. if (this._texture._size) {
  193. this._cachedBaseSize.width = this._texture._size;
  194. this._cachedBaseSize.height = this._texture._size;
  195. return this._cachedBaseSize;
  196. }
  197. this._cachedBaseSize.width = this._texture.baseWidth;
  198. this._cachedBaseSize.height = this._texture.baseHeight;
  199. return this._cachedBaseSize;
  200. }
  201. /**
  202. * Get the current sampling mode associated with the texture.
  203. */
  204. get samplingMode() {
  205. if (!this._texture) {
  206. return this._initialSamplingMode;
  207. }
  208. return this._texture.samplingMode;
  209. }
  210. /**
  211. * Update the sampling mode of the texture.
  212. * Default is Trilinear mode.
  213. *
  214. * | Value | Type | Description |
  215. * | ----- | ------------------ | ----------- |
  216. * | 1 | NEAREST_SAMPLINGMODE or NEAREST_NEAREST_MIPLINEAR | Nearest is: mag = nearest, min = nearest, mip = linear |
  217. * | 2 | BILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPNEAREST | Bilinear is: mag = linear, min = linear, mip = nearest |
  218. * | 3 | TRILINEAR_SAMPLINGMODE or LINEAR_LINEAR_MIPLINEAR | Trilinear is: mag = linear, min = linear, mip = linear |
  219. * | 4 | NEAREST_NEAREST_MIPNEAREST | |
  220. * | 5 | NEAREST_LINEAR_MIPNEAREST | |
  221. * | 6 | NEAREST_LINEAR_MIPLINEAR | |
  222. * | 7 | NEAREST_LINEAR | |
  223. * | 8 | NEAREST_NEAREST | |
  224. * | 9 | LINEAR_NEAREST_MIPNEAREST | |
  225. * | 10 | LINEAR_NEAREST_MIPLINEAR | |
  226. * | 11 | LINEAR_LINEAR | |
  227. * | 12 | LINEAR_NEAREST | |
  228. *
  229. * > _mag_: magnification filter (close to the viewer)
  230. * > _min_: minification filter (far from the viewer)
  231. * > _mip_: filter used between mip map levels
  232. *@param samplingMode Define the new sampling mode of the texture
  233. */
  234. updateSamplingMode(samplingMode) {
  235. if (this._texture && this._engine) {
  236. this._engine.updateTextureSamplingMode(samplingMode, this._texture);
  237. }
  238. }
  239. /**
  240. * Release and destroy the underlying lower level texture aka internalTexture.
  241. */
  242. releaseInternalTexture() {
  243. if (this._texture) {
  244. this._texture.dispose();
  245. this._texture = null;
  246. }
  247. }
  248. /**
  249. * Dispose the texture and release its associated resources.
  250. */
  251. dispose() {
  252. if (this._texture) {
  253. this.releaseInternalTexture();
  254. this._engine = null;
  255. }
  256. }
  257. }
  258. //# sourceMappingURL=thinTexture.js.map