babylon.texture.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, samplingMode);
  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 = Engine.DELAYLOADSTATE_NOTLOADED;
  65. }
  66. } else {
  67. Tools.SetImmediate(() => {
  68. if (onLoad) {
  69. onLoad();
  70. }
  71. });
  72. }
  73. }
  74. public delayLoad(): void {
  75. if (this.delayLoadState !== Engine.DELAYLOADSTATE_NOTLOADED) {
  76. return;
  77. }
  78. this.delayLoadState = Engine.DELAYLOADSTATE_LOADED;
  79. this._texture = this._getFromCache(this.url, this._noMipmap, this._samplingMode);
  80. if (!this._texture) {
  81. this._texture = this.getScene().getEngine().createTexture(this.url, this._noMipmap, this._invertY, this.getScene(), this._samplingMode, null, null, this._buffer);
  82. if (this._deleteBuffer) {
  83. delete this._buffer;
  84. }
  85. }
  86. }
  87. public updateSamplingMode(samplingMode: number): void {
  88. if (!this._texture) {
  89. return;
  90. }
  91. this.getScene().getEngine().updateTextureSamplingMode(samplingMode, this._texture);
  92. }
  93. private _prepareRowForTextureGeneration(x: number, y: number, z: number, t: Vector3): void {
  94. x -= this.uOffset + 0.5;
  95. y -= this.vOffset + 0.5;
  96. z -= 0.5;
  97. Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  98. t.x *= this.uScale;
  99. t.y *= this.vScale;
  100. t.x += 0.5;
  101. t.y += 0.5;
  102. t.z += 0.5;
  103. }
  104. public getTextureMatrix(): Matrix {
  105. if (
  106. this.uOffset === this._cachedUOffset &&
  107. this.vOffset === this._cachedVOffset &&
  108. this.uScale === this._cachedUScale &&
  109. this.vScale === this._cachedVScale &&
  110. this.uAng === this._cachedUAng &&
  111. this.vAng === this._cachedVAng &&
  112. this.wAng === this._cachedWAng) {
  113. return this._cachedTextureMatrix;
  114. }
  115. this._cachedUOffset = this.uOffset;
  116. this._cachedVOffset = this.vOffset;
  117. this._cachedUScale = this.uScale;
  118. this._cachedVScale = this.vScale;
  119. this._cachedUAng = this.uAng;
  120. this._cachedVAng = this.vAng;
  121. this._cachedWAng = this.wAng;
  122. if (!this._cachedTextureMatrix) {
  123. this._cachedTextureMatrix = Matrix.Zero();
  124. this._rowGenerationMatrix = new Matrix();
  125. this._t0 = Vector3.Zero();
  126. this._t1 = Vector3.Zero();
  127. this._t2 = Vector3.Zero();
  128. }
  129. Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  130. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  131. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  132. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  133. this._t1.subtractInPlace(this._t0);
  134. this._t2.subtractInPlace(this._t0);
  135. Matrix.IdentityToRef(this._cachedTextureMatrix);
  136. this._cachedTextureMatrix.m[0] = this._t1.x; this._cachedTextureMatrix.m[1] = this._t1.y; this._cachedTextureMatrix.m[2] = this._t1.z;
  137. this._cachedTextureMatrix.m[4] = this._t2.x; this._cachedTextureMatrix.m[5] = this._t2.y; this._cachedTextureMatrix.m[6] = this._t2.z;
  138. this._cachedTextureMatrix.m[8] = this._t0.x; this._cachedTextureMatrix.m[9] = this._t0.y; this._cachedTextureMatrix.m[10] = this._t0.z;
  139. return this._cachedTextureMatrix;
  140. }
  141. public getReflectionTextureMatrix(): Matrix {
  142. if (
  143. this.uOffset === this._cachedUOffset &&
  144. this.vOffset === this._cachedVOffset &&
  145. this.uScale === this._cachedUScale &&
  146. this.vScale === this._cachedVScale &&
  147. this.coordinatesMode === this._cachedCoordinatesMode) {
  148. return this._cachedTextureMatrix;
  149. }
  150. if (!this._cachedTextureMatrix) {
  151. this._cachedTextureMatrix = Matrix.Zero();
  152. this._projectionModeMatrix = Matrix.Zero();
  153. }
  154. this._cachedCoordinatesMode = this.coordinatesMode;
  155. switch (this.coordinatesMode) {
  156. case Texture.SPHERICAL_MODE:
  157. Matrix.IdentityToRef(this._cachedTextureMatrix);
  158. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  159. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  160. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  161. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  162. break;
  163. case Texture.PLANAR_MODE:
  164. Matrix.IdentityToRef(this._cachedTextureMatrix);
  165. this._cachedTextureMatrix[0] = this.uScale;
  166. this._cachedTextureMatrix[5] = this.vScale;
  167. this._cachedTextureMatrix[12] = this.uOffset;
  168. this._cachedTextureMatrix[13] = this.vOffset;
  169. break;
  170. case Texture.PROJECTION_MODE:
  171. Matrix.IdentityToRef(this._projectionModeMatrix);
  172. this._projectionModeMatrix.m[0] = 0.5;
  173. this._projectionModeMatrix.m[5] = -0.5;
  174. this._projectionModeMatrix.m[10] = 0.0;
  175. this._projectionModeMatrix.m[12] = 0.5;
  176. this._projectionModeMatrix.m[13] = 0.5;
  177. this._projectionModeMatrix.m[14] = 1.0;
  178. this._projectionModeMatrix.m[15] = 1.0;
  179. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  180. break;
  181. default:
  182. Matrix.IdentityToRef(this._cachedTextureMatrix);
  183. break;
  184. }
  185. return this._cachedTextureMatrix;
  186. }
  187. public clone(): Texture {
  188. var newTexture = new Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY, this._samplingMode);
  189. // Base texture
  190. newTexture.hasAlpha = this.hasAlpha;
  191. newTexture.level = this.level;
  192. newTexture.wrapU = this.wrapU;
  193. newTexture.wrapV = this.wrapV;
  194. newTexture.coordinatesIndex = this.coordinatesIndex;
  195. newTexture.coordinatesMode = this.coordinatesMode;
  196. // Texture
  197. newTexture.uOffset = this.uOffset;
  198. newTexture.vOffset = this.vOffset;
  199. newTexture.uScale = this.uScale;
  200. newTexture.vScale = this.vScale;
  201. newTexture.uAng = this.uAng;
  202. newTexture.vAng = this.vAng;
  203. newTexture.wAng = this.wAng;
  204. return newTexture;
  205. }
  206. // Statics
  207. 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 {
  208. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  209. }
  210. }
  211. }