babylon.texture.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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._prepareRowForTextureGeneration = function (x, y, z, t) {
  62. x -= this.uOffset + 0.5;
  63. y -= this.vOffset + 0.5;
  64. z -= 0.5;
  65. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  66. t.x *= this.uScale;
  67. t.y *= this.vScale;
  68. t.x += 0.5;
  69. t.y += 0.5;
  70. t.z += 0.5;
  71. };
  72. Texture.prototype.getTextureMatrix = function () {
  73. 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) {
  74. return this._cachedTextureMatrix;
  75. }
  76. this._cachedUOffset = this.uOffset;
  77. this._cachedVOffset = this.vOffset;
  78. this._cachedUScale = this.uScale;
  79. this._cachedVScale = this.vScale;
  80. this._cachedUAng = this.uAng;
  81. this._cachedVAng = this.vAng;
  82. this._cachedWAng = this.wAng;
  83. if (!this._cachedTextureMatrix) {
  84. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  85. this._rowGenerationMatrix = new BABYLON.Matrix();
  86. this._t0 = BABYLON.Vector3.Zero();
  87. this._t1 = BABYLON.Vector3.Zero();
  88. this._t2 = BABYLON.Vector3.Zero();
  89. }
  90. BABYLON.Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  91. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  92. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  93. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  94. this._t1.subtractInPlace(this._t0);
  95. this._t2.subtractInPlace(this._t0);
  96. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  97. this._cachedTextureMatrix.m[0] = this._t1.x;
  98. this._cachedTextureMatrix.m[1] = this._t1.y;
  99. this._cachedTextureMatrix.m[2] = this._t1.z;
  100. this._cachedTextureMatrix.m[4] = this._t2.x;
  101. this._cachedTextureMatrix.m[5] = this._t2.y;
  102. this._cachedTextureMatrix.m[6] = this._t2.z;
  103. this._cachedTextureMatrix.m[8] = this._t0.x;
  104. this._cachedTextureMatrix.m[9] = this._t0.y;
  105. this._cachedTextureMatrix.m[10] = this._t0.z;
  106. return this._cachedTextureMatrix;
  107. };
  108. Texture.prototype.getReflectionTextureMatrix = function () {
  109. if (this.uOffset === this._cachedUOffset && this.vOffset === this._cachedVOffset && this.uScale === this._cachedUScale && this.vScale === this._cachedVScale && 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. this._cachedCoordinatesMode = this.coordinatesMode;
  117. switch (this.coordinatesMode) {
  118. case BABYLON.Texture.SPHERICAL_MODE:
  119. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  120. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  121. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  122. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  123. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  124. break;
  125. case BABYLON.Texture.PLANAR_MODE:
  126. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  127. this._cachedTextureMatrix[0] = this.uScale;
  128. this._cachedTextureMatrix[5] = this.vScale;
  129. this._cachedTextureMatrix[12] = this.uOffset;
  130. this._cachedTextureMatrix[13] = this.vOffset;
  131. break;
  132. case BABYLON.Texture.PROJECTION_MODE:
  133. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  134. this._projectionModeMatrix.m[0] = 0.5;
  135. this._projectionModeMatrix.m[5] = -0.5;
  136. this._projectionModeMatrix.m[10] = 0.0;
  137. this._projectionModeMatrix.m[12] = 0.5;
  138. this._projectionModeMatrix.m[13] = 0.5;
  139. this._projectionModeMatrix.m[14] = 1.0;
  140. this._projectionModeMatrix.m[15] = 1.0;
  141. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  142. break;
  143. default:
  144. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  145. break;
  146. }
  147. return this._cachedTextureMatrix;
  148. };
  149. Texture.prototype.clone = function () {
  150. var newTexture = new BABYLON.Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY, this._samplingMode);
  151. // Base texture
  152. newTexture.hasAlpha = this.hasAlpha;
  153. newTexture.level = this.level;
  154. newTexture.wrapU = this.wrapU;
  155. newTexture.wrapV = this.wrapV;
  156. newTexture.coordinatesIndex = this.coordinatesIndex;
  157. newTexture.coordinatesMode = this.coordinatesMode;
  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. return newTexture;
  167. };
  168. // Statics
  169. Texture.CreateFromBase64String = function (data, name, scene, noMipmap, invertY, samplingMode, onLoad, onError) {
  170. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  171. if (onLoad === void 0) { onLoad = null; }
  172. if (onError === void 0) { onError = null; }
  173. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  174. };
  175. // Constants
  176. Texture.NEAREST_SAMPLINGMODE = 1;
  177. Texture.BILINEAR_SAMPLINGMODE = 2;
  178. Texture.TRILINEAR_SAMPLINGMODE = 3;
  179. Texture.EXPLICIT_MODE = 0;
  180. Texture.SPHERICAL_MODE = 1;
  181. Texture.PLANAR_MODE = 2;
  182. Texture.CUBIC_MODE = 3;
  183. Texture.PROJECTION_MODE = 4;
  184. Texture.SKYBOX_MODE = 5;
  185. Texture.CLAMP_ADDRESSMODE = 0;
  186. Texture.WRAP_ADDRESSMODE = 1;
  187. Texture.MIRROR_ADDRESSMODE = 2;
  188. return Texture;
  189. })(BABYLON.BaseTexture);
  190. BABYLON.Texture = Texture;
  191. })(BABYLON || (BABYLON = {}));
  192. //# sourceMappingURL=babylon.texture.js.map