babylon.texture.js 8.0 KB

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