babylon.texture.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. var __extends = (this && 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. else {
  48. BABYLON.Tools.SetImmediate(function () {
  49. if (onLoad) {
  50. onLoad();
  51. }
  52. });
  53. }
  54. }
  55. Texture.prototype.delayLoad = function () {
  56. if (this.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  57. return;
  58. }
  59. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  60. this._texture = this._getFromCache(this.url, this._noMipmap, this._samplingMode);
  61. if (!this._texture) {
  62. this._texture = this.getScene().getEngine().createTexture(this.url, this._noMipmap, this._invertY, this.getScene(), this._samplingMode, null, null, this._buffer);
  63. if (this._deleteBuffer) {
  64. delete this._buffer;
  65. }
  66. }
  67. };
  68. Texture.prototype.updateSamplingMode = function (samplingMode) {
  69. if (!this._texture) {
  70. return;
  71. }
  72. this.getScene().getEngine().updateTextureSamplingMode(samplingMode, this._texture);
  73. };
  74. Texture.prototype._prepareRowForTextureGeneration = function (x, y, z, t) {
  75. x -= this.uOffset + 0.5;
  76. y -= this.vOffset + 0.5;
  77. z -= 0.5;
  78. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  79. t.x *= this.uScale;
  80. t.y *= this.vScale;
  81. t.x += 0.5;
  82. t.y += 0.5;
  83. t.z += 0.5;
  84. };
  85. Texture.prototype.getTextureMatrix = function () {
  86. if (this.uOffset === this._cachedUOffset &&
  87. this.vOffset === this._cachedVOffset &&
  88. this.uScale === this._cachedUScale &&
  89. this.vScale === this._cachedVScale &&
  90. this.uAng === this._cachedUAng &&
  91. this.vAng === this._cachedVAng &&
  92. this.wAng === this._cachedWAng) {
  93. return this._cachedTextureMatrix;
  94. }
  95. this._cachedUOffset = this.uOffset;
  96. this._cachedVOffset = this.vOffset;
  97. this._cachedUScale = this.uScale;
  98. this._cachedVScale = this.vScale;
  99. this._cachedUAng = this.uAng;
  100. this._cachedVAng = this.vAng;
  101. this._cachedWAng = this.wAng;
  102. if (!this._cachedTextureMatrix) {
  103. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  104. this._rowGenerationMatrix = new BABYLON.Matrix();
  105. this._t0 = BABYLON.Vector3.Zero();
  106. this._t1 = BABYLON.Vector3.Zero();
  107. this._t2 = BABYLON.Vector3.Zero();
  108. }
  109. BABYLON.Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  110. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  111. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  112. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  113. this._t1.subtractInPlace(this._t0);
  114. this._t2.subtractInPlace(this._t0);
  115. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  116. this._cachedTextureMatrix.m[0] = this._t1.x;
  117. this._cachedTextureMatrix.m[1] = this._t1.y;
  118. this._cachedTextureMatrix.m[2] = this._t1.z;
  119. this._cachedTextureMatrix.m[4] = this._t2.x;
  120. this._cachedTextureMatrix.m[5] = this._t2.y;
  121. this._cachedTextureMatrix.m[6] = this._t2.z;
  122. this._cachedTextureMatrix.m[8] = this._t0.x;
  123. this._cachedTextureMatrix.m[9] = this._t0.y;
  124. this._cachedTextureMatrix.m[10] = this._t0.z;
  125. return this._cachedTextureMatrix;
  126. };
  127. Texture.prototype.getReflectionTextureMatrix = function () {
  128. if (this.uOffset === this._cachedUOffset &&
  129. this.vOffset === this._cachedVOffset &&
  130. this.uScale === this._cachedUScale &&
  131. this.vScale === this._cachedVScale &&
  132. this.coordinatesMode === this._cachedCoordinatesMode) {
  133. return this._cachedTextureMatrix;
  134. }
  135. if (!this._cachedTextureMatrix) {
  136. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  137. this._projectionModeMatrix = BABYLON.Matrix.Zero();
  138. }
  139. this._cachedCoordinatesMode = this.coordinatesMode;
  140. switch (this.coordinatesMode) {
  141. case Texture.SPHERICAL_MODE:
  142. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  143. this._cachedTextureMatrix[0] = -0.5 * this.uScale;
  144. this._cachedTextureMatrix[5] = -0.5 * this.vScale;
  145. this._cachedTextureMatrix[12] = 0.5 + this.uOffset;
  146. this._cachedTextureMatrix[13] = 0.5 + this.vOffset;
  147. break;
  148. case Texture.PLANAR_MODE:
  149. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  150. this._cachedTextureMatrix[0] = this.uScale;
  151. this._cachedTextureMatrix[5] = this.vScale;
  152. this._cachedTextureMatrix[12] = this.uOffset;
  153. this._cachedTextureMatrix[13] = this.vOffset;
  154. break;
  155. case Texture.PROJECTION_MODE:
  156. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  157. this._projectionModeMatrix.m[0] = 0.5;
  158. this._projectionModeMatrix.m[5] = -0.5;
  159. this._projectionModeMatrix.m[10] = 0.0;
  160. this._projectionModeMatrix.m[12] = 0.5;
  161. this._projectionModeMatrix.m[13] = 0.5;
  162. this._projectionModeMatrix.m[14] = 1.0;
  163. this._projectionModeMatrix.m[15] = 1.0;
  164. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  165. break;
  166. default:
  167. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  168. break;
  169. }
  170. return this._cachedTextureMatrix;
  171. };
  172. Texture.prototype.clone = function () {
  173. var newTexture = new Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY, this._samplingMode);
  174. // Base texture
  175. newTexture.hasAlpha = this.hasAlpha;
  176. newTexture.level = this.level;
  177. newTexture.wrapU = this.wrapU;
  178. newTexture.wrapV = this.wrapV;
  179. newTexture.coordinatesIndex = this.coordinatesIndex;
  180. newTexture.coordinatesMode = this.coordinatesMode;
  181. // Texture
  182. newTexture.uOffset = this.uOffset;
  183. newTexture.vOffset = this.vOffset;
  184. newTexture.uScale = this.uScale;
  185. newTexture.vScale = this.vScale;
  186. newTexture.uAng = this.uAng;
  187. newTexture.vAng = this.vAng;
  188. newTexture.wAng = this.wAng;
  189. return newTexture;
  190. };
  191. // Statics
  192. Texture.CreateFromBase64String = function (data, name, scene, noMipmap, invertY, samplingMode, onLoad, onError) {
  193. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  194. if (onLoad === void 0) { onLoad = null; }
  195. if (onError === void 0) { onError = null; }
  196. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  197. };
  198. // Constants
  199. Texture.NEAREST_SAMPLINGMODE = 1;
  200. Texture.BILINEAR_SAMPLINGMODE = 2;
  201. Texture.TRILINEAR_SAMPLINGMODE = 3;
  202. Texture.EXPLICIT_MODE = 0;
  203. Texture.SPHERICAL_MODE = 1;
  204. Texture.PLANAR_MODE = 2;
  205. Texture.CUBIC_MODE = 3;
  206. Texture.PROJECTION_MODE = 4;
  207. Texture.SKYBOX_MODE = 5;
  208. Texture.CLAMP_ADDRESSMODE = 0;
  209. Texture.WRAP_ADDRESSMODE = 1;
  210. Texture.MIRROR_ADDRESSMODE = 2;
  211. return Texture;
  212. })(BABYLON.BaseTexture);
  213. BABYLON.Texture = Texture;
  214. })(BABYLON || (BABYLON = {}));
  215. //# sourceMappingURL=babylon.texture.js.map