babylon.texture.js 8.0 KB

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