babylon.texture.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. public _samplingMode: number;
  42. private _buffer: any;
  43. private _deleteBuffer: boolean;
  44. constructor(url: string, scene: Scene, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: any = null, deleteBuffer: boolean = false) {
  45. super(scene);
  46. this.name = url;
  47. this.url = url;
  48. this._noMipmap = noMipmap;
  49. this._invertY = invertY;
  50. this._samplingMode = samplingMode;
  51. this._buffer = buffer;
  52. this._deleteBuffer = deleteBuffer;
  53. if (!url) {
  54. return;
  55. }
  56. this._texture = this._getFromCache(url, noMipmap);
  57. if (!this._texture) {
  58. if (!scene.useDelayedTextureLoading) {
  59. this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene, this._samplingMode, onLoad, onError, this._buffer);
  60. if (deleteBuffer) {
  61. delete this._buffer;
  62. }
  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(), this._samplingMode, null, null, this._buffer);
  76. if (this._deleteBuffer) {
  77. delete this._buffer;
  78. }
  79. }
  80. }
  81. private _prepareRowForTextureGeneration(x: number, y: number, z: number, t: Vector3): void {
  82. x -= this.uOffset + 0.5;
  83. y -= this.vOffset + 0.5;
  84. z -= 0.5;
  85. Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  86. t.x *= this.uScale;
  87. t.y *= this.vScale;
  88. t.x += 0.5;
  89. t.y += 0.5;
  90. t.z += 0.5;
  91. }
  92. public getTextureMatrix(): Matrix {
  93. if (
  94. this.uOffset === this._cachedUOffset &&
  95. this.vOffset === this._cachedVOffset &&
  96. this.uScale === this._cachedUScale &&
  97. this.vScale === this._cachedVScale &&
  98. this.uAng === this._cachedUAng &&
  99. this.vAng === this._cachedVAng &&
  100. this.wAng === this._cachedWAng) {
  101. return this._cachedTextureMatrix;
  102. }
  103. this._cachedUOffset = this.uOffset;
  104. this._cachedVOffset = this.vOffset;
  105. this._cachedUScale = this.uScale;
  106. this._cachedVScale = this.vScale;
  107. this._cachedUAng = this.uAng;
  108. this._cachedVAng = this.vAng;
  109. this._cachedWAng = this.wAng;
  110. if (!this._cachedTextureMatrix) {
  111. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  112. this._rowGenerationMatrix = new BABYLON.Matrix();
  113. this._t0 = BABYLON.Vector3.Zero();
  114. this._t1 = BABYLON.Vector3.Zero();
  115. this._t2 = BABYLON.Vector3.Zero();
  116. }
  117. BABYLON.Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  118. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  119. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  120. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  121. this._t1.subtractInPlace(this._t0);
  122. this._t2.subtractInPlace(this._t0);
  123. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  124. this._cachedTextureMatrix.m[0] = this._t1.x; this._cachedTextureMatrix.m[1] = this._t1.y; this._cachedTextureMatrix.m[2] = this._t1.z;
  125. this._cachedTextureMatrix.m[4] = this._t2.x; this._cachedTextureMatrix.m[5] = this._t2.y; this._cachedTextureMatrix.m[6] = this._t2.z;
  126. this._cachedTextureMatrix.m[8] = this._t0.x; this._cachedTextureMatrix.m[9] = this._t0.y; this._cachedTextureMatrix.m[10] = this._t0.z;
  127. return this._cachedTextureMatrix;
  128. }
  129. public getReflectionTextureMatrix(): Matrix {
  130. if (
  131. this.uOffset === this._cachedUOffset &&
  132. this.vOffset === this._cachedVOffset &&
  133. this.uScale === this._cachedUScale &&
  134. this.vScale === this._cachedVScale &&
  135. this.coordinatesMode === this._cachedCoordinatesMode) {
  136. return this._cachedTextureMatrix;
  137. }
  138. if (!this._cachedTextureMatrix) {
  139. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  140. this._projectionModeMatrix = BABYLON.Matrix.Zero();
  141. }
  142. switch (this.coordinatesMode) {
  143. case BABYLON.Texture.SPHERICAL_MODE:
  144. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  145. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  146. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  147. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  148. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  149. break;
  150. case BABYLON.Texture.PLANAR_MODE:
  151. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  152. this._cachedTextureMatrix[0] = this.uScale;
  153. this._cachedTextureMatrix[5] = this.vScale;
  154. this._cachedTextureMatrix[12] = this.uOffset;
  155. this._cachedTextureMatrix[13] = this.vOffset;
  156. break;
  157. case BABYLON.Texture.PROJECTION_MODE:
  158. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  159. this._projectionModeMatrix.m[0] = 0.5;
  160. this._projectionModeMatrix.m[5] = -0.5;
  161. this._projectionModeMatrix.m[10] = 0.0;
  162. this._projectionModeMatrix.m[12] = 0.5;
  163. this._projectionModeMatrix.m[13] = 0.5;
  164. this._projectionModeMatrix.m[14] = 1.0;
  165. this._projectionModeMatrix.m[15] = 1.0;
  166. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  167. break;
  168. default:
  169. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  170. break;
  171. }
  172. return this._cachedTextureMatrix;
  173. }
  174. public clone(): Texture {
  175. var newTexture = new BABYLON.Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY);
  176. // Base texture
  177. newTexture.hasAlpha = this.hasAlpha;
  178. newTexture.level = this.level;
  179. newTexture.wrapU = this.wrapU;
  180. newTexture.wrapV = this.wrapV;
  181. newTexture.coordinatesIndex = this.coordinatesIndex;
  182. newTexture.coordinatesMode = this.coordinatesMode;
  183. // Texture
  184. newTexture.uOffset = this.uOffset;
  185. newTexture.vOffset = this.vOffset;
  186. newTexture.uScale = this.uScale;
  187. newTexture.vScale = this.vScale;
  188. newTexture.uAng = this.uAng;
  189. newTexture.vAng = this.vAng;
  190. newTexture.wAng = this.wAng;
  191. return newTexture;
  192. }
  193. // Statics
  194. public static CreateFromBase64String(data: string, name: string, scene: Scene, noMipmap?: boolean, invertY?: boolean, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null): Texture {
  195. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  196. }
  197. }
  198. }