babylon.texture.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. module BABYLON {
  2. export class Texture extends BaseTexture {
  3. // Constants
  4. public static NEAREST_SAMPLINGMODE = 1;
  5. public static BILINEAR_SAMPLINGMODE = 2;
  6. public static TRILINEAR_SAMPLINGMODE = 3;
  7. public static EXPLICIT_MODE = 0;
  8. public static SPHERICAL_MODE = 1;
  9. public static PLANAR_MODE = 2;
  10. public static CUBIC_MODE = 3;
  11. public static PROJECTION_MODE = 4;
  12. public static SKYBOX_MODE = 5;
  13. public static CLAMP_ADDRESSMODE = 0;
  14. public static WRAP_ADDRESSMODE = 1;
  15. public static MIRROR_ADDRESSMODE = 2;
  16. // Members
  17. public name: string;
  18. public url: string;
  19. public uOffset = 0;
  20. public vOffset = 0;
  21. public uScale = 1.0;
  22. public vScale = 1.0;
  23. public uAng = 0;
  24. public vAng = 0;
  25. public wAng = 0;
  26. public wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  27. public wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  28. public coordinatesIndex = 0;
  29. public coordinatesMode = BABYLON.Texture.EXPLICIT_MODE;
  30. public anisotropicFilteringLevel = 4;
  31. public animations = new Array<Animation>();
  32. public isRenderTarget = false;
  33. private _noMipmap: boolean;
  34. public _invertY: boolean;
  35. private _rowGenerationMatrix: Matrix;
  36. private _cachedTextureMatrix: Matrix;
  37. private _projectionModeMatrix: Matrix;
  38. private _t0: Vector3;
  39. private _t1: Vector3;
  40. private _t2: Vector3;
  41. public _cachedAnisotropicFilteringLevel: number;
  42. private _cachedUOffset: number;
  43. private _cachedVOffset: number;
  44. private _cachedUScale: number;
  45. private _cachedVScale: number;
  46. private _cachedUAng: number;
  47. private _cachedVAng: number;
  48. private _cachedWAng: number;
  49. private _cachedCoordinatesMode: number;
  50. constructor(url: string, scene: Scene, noMipmap?: boolean, invertY?: boolean) {
  51. super(scene);
  52. this.name = url;
  53. this.url = url;
  54. this._noMipmap = noMipmap;
  55. this._invertY = invertY;
  56. if (!url) {
  57. return;
  58. }
  59. this._texture = this._getFromCache(url, noMipmap);
  60. if (!this._texture) {
  61. if (!scene.useDelayedTextureLoading) {
  62. this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene);
  63. } else {
  64. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  65. }
  66. }
  67. }
  68. public delayLoad(): void {
  69. if (this.delayLoadState != BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  70. return;
  71. }
  72. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  73. this._texture = this._getFromCache(this.url, this._noMipmap);
  74. if (!this._texture) {
  75. this._texture = this.getScene().getEngine().createTexture(this.url, this._noMipmap, this._invertY, this.getScene());
  76. }
  77. }
  78. private _prepareRowForTextureGeneration(x: number, y: number, z: number, t: Vector3): void {
  79. x -= this.uOffset + 0.5;
  80. y -= this.vOffset + 0.5;
  81. z -= 0.5;
  82. Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  83. t.x *= this.uScale;
  84. t.y *= this.vScale;
  85. t.x += 0.5;
  86. t.y += 0.5;
  87. t.z += 0.5;
  88. }
  89. public _computeTextureMatrix(): Matrix {
  90. if (
  91. this.uOffset === this._cachedUOffset &&
  92. this.vOffset === this._cachedVOffset &&
  93. this.uScale === this._cachedUScale &&
  94. this.vScale === this._cachedVScale &&
  95. this.uAng === this._cachedUAng &&
  96. this.vAng === this._cachedVAng &&
  97. this.wAng === this._cachedWAng) {
  98. return this._cachedTextureMatrix;
  99. }
  100. this._cachedUOffset = this.uOffset;
  101. this._cachedVOffset = this.vOffset;
  102. this._cachedUScale = this.uScale;
  103. this._cachedVScale = this.vScale;
  104. this._cachedUAng = this.uAng;
  105. this._cachedVAng = this.vAng;
  106. this._cachedWAng = this.wAng;
  107. if (!this._cachedTextureMatrix) {
  108. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  109. this._rowGenerationMatrix = new BABYLON.Matrix();
  110. this._t0 = BABYLON.Vector3.Zero();
  111. this._t1 = BABYLON.Vector3.Zero();
  112. this._t2 = BABYLON.Vector3.Zero();
  113. }
  114. BABYLON.Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  115. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  116. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  117. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  118. this._t1.subtractInPlace(this._t0);
  119. this._t2.subtractInPlace(this._t0);
  120. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  121. this._cachedTextureMatrix.m[0] = this._t1.x; this._cachedTextureMatrix.m[1] = this._t1.y; this._cachedTextureMatrix.m[2] = this._t1.z;
  122. this._cachedTextureMatrix.m[4] = this._t2.x; this._cachedTextureMatrix.m[5] = this._t2.y; this._cachedTextureMatrix.m[6] = this._t2.z;
  123. this._cachedTextureMatrix.m[8] = this._t0.x; this._cachedTextureMatrix.m[9] = this._t0.y; this._cachedTextureMatrix.m[10] = this._t0.z;
  124. return this._cachedTextureMatrix;
  125. }
  126. public _computeReflectionTextureMatrix(): Matrix {
  127. if (
  128. this.uOffset === this._cachedUOffset &&
  129. this.vOffset === this._cachedVOffset &&
  130. this.uScale === this._cachedUScale &&
  131. this.vScale === this._cachedVScale &&
  132. this.coordinatesMode === this._cachedCoordinatesMode) {
  133. return this._cachedTextureMatrix;
  134. }
  135. if (!this._cachedTextureMatrix) {
  136. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  137. this._projectionModeMatrix = BABYLON.Matrix.Zero();
  138. }
  139. switch (this.coordinatesMode) {
  140. case BABYLON.Texture.SPHERICAL_MODE:
  141. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  142. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  143. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  144. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  145. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  146. break;
  147. case BABYLON.Texture.PLANAR_MODE:
  148. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  149. this._cachedTextureMatrix[0] = this.uScale;
  150. this._cachedTextureMatrix[5] = this.vScale;
  151. this._cachedTextureMatrix[12] = this.uOffset;
  152. this._cachedTextureMatrix[13] = this.vOffset;
  153. break;
  154. case BABYLON.Texture.PROJECTION_MODE:
  155. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  156. this._projectionModeMatrix.m[0] = 0.5;
  157. this._projectionModeMatrix.m[5] = -0.5;
  158. this._projectionModeMatrix.m[10] = 0.0;
  159. this._projectionModeMatrix.m[12] = 0.5;
  160. this._projectionModeMatrix.m[13] = 0.5;
  161. this._projectionModeMatrix.m[14] = 1.0;
  162. this._projectionModeMatrix.m[15] = 1.0;
  163. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  164. break;
  165. default:
  166. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  167. break;
  168. }
  169. return this._cachedTextureMatrix;
  170. }
  171. public clone(): Texture {
  172. var newTexture = new BABYLON.Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY);
  173. // Base texture
  174. newTexture.hasAlpha = this.hasAlpha;
  175. newTexture.level = this.level;
  176. // Texture
  177. newTexture.uOffset = this.uOffset;
  178. newTexture.vOffset = this.vOffset;
  179. newTexture.uScale = this.uScale;
  180. newTexture.vScale = this.vScale;
  181. newTexture.uAng = this.uAng;
  182. newTexture.vAng = this.vAng;
  183. newTexture.wAng = this.wAng;
  184. newTexture.wrapU = this.wrapU;
  185. newTexture.wrapV = this.wrapV;
  186. newTexture.coordinatesIndex = this.coordinatesIndex;
  187. newTexture.coordinatesMode = this.coordinatesMode;
  188. return newTexture;
  189. }
  190. }
  191. }