babylon.texture.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var Texture = (function (_super) {
  10. __extends(Texture, _super);
  11. function Texture(url, scene, noMipmap, invertY, samplingMode, onLoad, onError, buffer, deleteBuffer) {
  12. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  13. if (onLoad === void 0) { onLoad = null; }
  14. if (onError === void 0) { onError = null; }
  15. if (buffer === void 0) { buffer = null; }
  16. if (deleteBuffer === void 0) { deleteBuffer = false; }
  17. _super.call(this, scene);
  18. this.uOffset = 0;
  19. this.vOffset = 0;
  20. this.uScale = 1.0;
  21. this.vScale = 1.0;
  22. this.uAng = 0;
  23. this.vAng = 0;
  24. this.wAng = 0;
  25. this.name = url;
  26. this.url = url;
  27. this._noMipmap = noMipmap;
  28. this._invertY = invertY;
  29. this._samplingMode = samplingMode;
  30. this._buffer = buffer;
  31. this._deleteBuffer = deleteBuffer;
  32. if (!url) {
  33. return;
  34. }
  35. this._texture = this._getFromCache(url, noMipmap, samplingMode);
  36. if (!this._texture) {
  37. if (!scene.useDelayedTextureLoading) {
  38. this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene, this._samplingMode, onLoad, onError, this._buffer);
  39. if (deleteBuffer) {
  40. delete this._buffer;
  41. }
  42. }
  43. else {
  44. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  45. }
  46. }
  47. }
  48. Texture.prototype.delayLoad = function () {
  49. if (this.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  50. return;
  51. }
  52. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  53. this._texture = this._getFromCache(this.url, this._noMipmap, this._samplingMode);
  54. if (!this._texture) {
  55. this._texture = this.getScene().getEngine().createTexture(this.url, this._noMipmap, this._invertY, this.getScene(), this._samplingMode, null, null, this._buffer);
  56. if (this._deleteBuffer) {
  57. delete this._buffer;
  58. }
  59. }
  60. };
  61. Texture.prototype.updateSamplingMode = function (samplingMode) {
  62. if (!this._texture) {
  63. return;
  64. }
  65. this.getScene().getEngine().updateTextureSamplingMode(samplingMode, this._texture);
  66. };
  67. Texture.prototype._prepareRowForTextureGeneration = function (x, y, z, t) {
  68. x -= this.uOffset + 0.5;
  69. y -= this.vOffset + 0.5;
  70. z -= 0.5;
  71. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  72. t.x *= this.uScale;
  73. t.y *= this.vScale;
  74. t.x += 0.5;
  75. t.y += 0.5;
  76. t.z += 0.5;
  77. };
  78. Texture.prototype.getTextureMatrix = function () {
  79. if (this.uOffset === this._cachedUOffset && this.vOffset === this._cachedVOffset && this.uScale === this._cachedUScale && this.vScale === this._cachedVScale && this.uAng === this._cachedUAng && this.vAng === this._cachedVAng && 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;
  104. this._cachedTextureMatrix.m[1] = this._t1.y;
  105. this._cachedTextureMatrix.m[2] = this._t1.z;
  106. this._cachedTextureMatrix.m[4] = this._t2.x;
  107. this._cachedTextureMatrix.m[5] = this._t2.y;
  108. this._cachedTextureMatrix.m[6] = this._t2.z;
  109. this._cachedTextureMatrix.m[8] = this._t0.x;
  110. this._cachedTextureMatrix.m[9] = this._t0.y;
  111. this._cachedTextureMatrix.m[10] = this._t0.z;
  112. return this._cachedTextureMatrix;
  113. };
  114. Texture.prototype.getReflectionTextureMatrix = function () {
  115. if (this.uOffset === this._cachedUOffset && this.vOffset === this._cachedVOffset && this.uScale === this._cachedUScale && this.vScale === this._cachedVScale && this.coordinatesMode === this._cachedCoordinatesMode) {
  116. return this._cachedTextureMatrix;
  117. }
  118. if (!this._cachedTextureMatrix) {
  119. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  120. this._projectionModeMatrix = BABYLON.Matrix.Zero();
  121. }
  122. this._cachedCoordinatesMode = this.coordinatesMode;
  123. switch (this.coordinatesMode) {
  124. case Texture.SPHERICAL_MODE:
  125. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  126. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  127. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  128. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  129. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  130. break;
  131. case Texture.PLANAR_MODE:
  132. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  133. this._cachedTextureMatrix[0] = this.uScale;
  134. this._cachedTextureMatrix[5] = this.vScale;
  135. this._cachedTextureMatrix[12] = this.uOffset;
  136. this._cachedTextureMatrix[13] = this.vOffset;
  137. break;
  138. case Texture.PROJECTION_MODE:
  139. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  140. this._projectionModeMatrix.m[0] = 0.5;
  141. this._projectionModeMatrix.m[5] = -0.5;
  142. this._projectionModeMatrix.m[10] = 0.0;
  143. this._projectionModeMatrix.m[12] = 0.5;
  144. this._projectionModeMatrix.m[13] = 0.5;
  145. this._projectionModeMatrix.m[14] = 1.0;
  146. this._projectionModeMatrix.m[15] = 1.0;
  147. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  148. break;
  149. default:
  150. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  151. break;
  152. }
  153. return this._cachedTextureMatrix;
  154. };
  155. Texture.prototype.clone = function () {
  156. var newTexture = new Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY, this._samplingMode);
  157. // Base texture
  158. newTexture.hasAlpha = this.hasAlpha;
  159. newTexture.level = this.level;
  160. newTexture.wrapU = this.wrapU;
  161. newTexture.wrapV = this.wrapV;
  162. newTexture.coordinatesIndex = this.coordinatesIndex;
  163. newTexture.coordinatesMode = this.coordinatesMode;
  164. // Texture
  165. newTexture.uOffset = this.uOffset;
  166. newTexture.vOffset = this.vOffset;
  167. newTexture.uScale = this.uScale;
  168. newTexture.vScale = this.vScale;
  169. newTexture.uAng = this.uAng;
  170. newTexture.vAng = this.vAng;
  171. newTexture.wAng = this.wAng;
  172. return newTexture;
  173. };
  174. // Statics
  175. Texture.CreateFromBase64String = function (data, name, scene, noMipmap, invertY, samplingMode, onLoad, onError) {
  176. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  177. if (onLoad === void 0) { onLoad = null; }
  178. if (onError === void 0) { onError = null; }
  179. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  180. };
  181. // Constants
  182. Texture.NEAREST_SAMPLINGMODE = 1;
  183. Texture.BILINEAR_SAMPLINGMODE = 2;
  184. Texture.TRILINEAR_SAMPLINGMODE = 3;
  185. Texture.EXPLICIT_MODE = 0;
  186. Texture.SPHERICAL_MODE = 1;
  187. Texture.PLANAR_MODE = 2;
  188. Texture.CUBIC_MODE = 3;
  189. Texture.PROJECTION_MODE = 4;
  190. Texture.SKYBOX_MODE = 5;
  191. Texture.CLAMP_ADDRESSMODE = 0;
  192. Texture.WRAP_ADDRESSMODE = 1;
  193. Texture.MIRROR_ADDRESSMODE = 2;
  194. return Texture;
  195. })(BABYLON.BaseTexture);
  196. BABYLON.Texture = Texture;
  197. })(BABYLON || (BABYLON = {}));
  198. //# sourceMappingURL=babylon.texture.js.map