thinTexture.ts 8.6 KB

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