babylon.texture.ts 8.6 KB

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