babylon.texture.js 7.8 KB

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