babylon.texture.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var Texture = (function (_super) {
  9. __extends(Texture, _super);
  10. function Texture(url, scene, noMipmap, invertY, samplingMode, onLoad, onError, buffer, deleteBuffer) {
  11. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  12. if (onLoad === void 0) { onLoad = null; }
  13. if (onError === void 0) { onError = null; }
  14. if (buffer === void 0) { buffer = null; }
  15. if (deleteBuffer === void 0) { deleteBuffer = false; }
  16. _super.call(this, scene);
  17. this.uOffset = 0;
  18. this.vOffset = 0;
  19. this.uScale = 1.0;
  20. this.vScale = 1.0;
  21. this.uAng = 0;
  22. this.vAng = 0;
  23. this.wAng = 0;
  24. this.name = url;
  25. this.url = url;
  26. this._noMipmap = noMipmap;
  27. this._invertY = invertY;
  28. this._samplingMode = samplingMode;
  29. this._buffer = buffer;
  30. this._deleteBuffer = deleteBuffer;
  31. if (!url) {
  32. return;
  33. }
  34. this._texture = this._getFromCache(url, noMipmap, samplingMode);
  35. if (!this._texture) {
  36. if (!scene.useDelayedTextureLoading) {
  37. this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene, this._samplingMode, onLoad, onError, this._buffer);
  38. if (deleteBuffer) {
  39. delete this._buffer;
  40. }
  41. }
  42. else {
  43. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  44. this._delayedOnLoad = onLoad;
  45. this._delayedOnError = onError;
  46. }
  47. }
  48. else {
  49. BABYLON.Tools.SetImmediate(function () {
  50. if (onLoad) {
  51. onLoad();
  52. }
  53. });
  54. }
  55. }
  56. Texture.prototype.delayLoad = function () {
  57. if (this.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  58. return;
  59. }
  60. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  61. this._texture = this._getFromCache(this.url, this._noMipmap, this._samplingMode);
  62. if (!this._texture) {
  63. this._texture = this.getScene().getEngine().createTexture(this.url, this._noMipmap, this._invertY, this.getScene(), this._samplingMode, this._delayedOnLoad, this._delayedOnError, this._buffer);
  64. if (this._deleteBuffer) {
  65. delete this._buffer;
  66. }
  67. }
  68. };
  69. Texture.prototype.updateSamplingMode = function (samplingMode) {
  70. if (!this._texture) {
  71. return;
  72. }
  73. this.getScene().getEngine().updateTextureSamplingMode(samplingMode, this._texture);
  74. };
  75. Texture.prototype._prepareRowForTextureGeneration = function (x, y, z, t) {
  76. x *= this.uScale;
  77. y *= this.vScale;
  78. x -= 0.5 * this.uScale;
  79. y -= 0.5 * this.vScale;
  80. z -= 0.5;
  81. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);
  82. t.x += 0.5 * this.uScale + this.uOffset;
  83. t.y += 0.5 * this.vScale + this.vOffset;
  84. t.z += 0.5;
  85. };
  86. Texture.prototype.getTextureMatrix = function () {
  87. if (this.uOffset === this._cachedUOffset &&
  88. this.vOffset === this._cachedVOffset &&
  89. this.uScale === this._cachedUScale &&
  90. this.vScale === this._cachedVScale &&
  91. this.uAng === this._cachedUAng &&
  92. this.vAng === this._cachedVAng &&
  93. this.wAng === this._cachedWAng) {
  94. return this._cachedTextureMatrix;
  95. }
  96. this._cachedUOffset = this.uOffset;
  97. this._cachedVOffset = this.vOffset;
  98. this._cachedUScale = this.uScale;
  99. this._cachedVScale = this.vScale;
  100. this._cachedUAng = this.uAng;
  101. this._cachedVAng = this.vAng;
  102. this._cachedWAng = this.wAng;
  103. if (!this._cachedTextureMatrix) {
  104. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  105. this._rowGenerationMatrix = new BABYLON.Matrix();
  106. this._t0 = BABYLON.Vector3.Zero();
  107. this._t1 = BABYLON.Vector3.Zero();
  108. this._t2 = BABYLON.Vector3.Zero();
  109. }
  110. BABYLON.Matrix.RotationYawPitchRollToRef(this.vAng, this.uAng, this.wAng, this._rowGenerationMatrix);
  111. this._prepareRowForTextureGeneration(0, 0, 0, this._t0);
  112. this._prepareRowForTextureGeneration(1.0, 0, 0, this._t1);
  113. this._prepareRowForTextureGeneration(0, 1.0, 0, this._t2);
  114. this._t1.subtractInPlace(this._t0);
  115. this._t2.subtractInPlace(this._t0);
  116. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  117. this._cachedTextureMatrix.m[0] = this._t1.x;
  118. this._cachedTextureMatrix.m[1] = this._t1.y;
  119. this._cachedTextureMatrix.m[2] = this._t1.z;
  120. this._cachedTextureMatrix.m[4] = this._t2.x;
  121. this._cachedTextureMatrix.m[5] = this._t2.y;
  122. this._cachedTextureMatrix.m[6] = this._t2.z;
  123. this._cachedTextureMatrix.m[8] = this._t0.x;
  124. this._cachedTextureMatrix.m[9] = this._t0.y;
  125. this._cachedTextureMatrix.m[10] = this._t0.z;
  126. return this._cachedTextureMatrix;
  127. };
  128. Texture.prototype.getReflectionTextureMatrix = function () {
  129. if (this.uOffset === this._cachedUOffset &&
  130. this.vOffset === this._cachedVOffset &&
  131. this.uScale === this._cachedUScale &&
  132. this.vScale === this._cachedVScale &&
  133. this.coordinatesMode === this._cachedCoordinatesMode) {
  134. return this._cachedTextureMatrix;
  135. }
  136. if (!this._cachedTextureMatrix) {
  137. this._cachedTextureMatrix = BABYLON.Matrix.Zero();
  138. this._projectionModeMatrix = BABYLON.Matrix.Zero();
  139. }
  140. this._cachedCoordinatesMode = this.coordinatesMode;
  141. switch (this.coordinatesMode) {
  142. case Texture.PLANAR_MODE:
  143. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  144. this._cachedTextureMatrix[0] = this.uScale;
  145. this._cachedTextureMatrix[5] = this.vScale;
  146. this._cachedTextureMatrix[12] = this.uOffset;
  147. this._cachedTextureMatrix[13] = this.vOffset;
  148. break;
  149. case Texture.PROJECTION_MODE:
  150. BABYLON.Matrix.IdentityToRef(this._projectionModeMatrix);
  151. this._projectionModeMatrix.m[0] = 0.5;
  152. this._projectionModeMatrix.m[5] = -0.5;
  153. this._projectionModeMatrix.m[10] = 0.0;
  154. this._projectionModeMatrix.m[12] = 0.5;
  155. this._projectionModeMatrix.m[13] = 0.5;
  156. this._projectionModeMatrix.m[14] = 1.0;
  157. this._projectionModeMatrix.m[15] = 1.0;
  158. this.getScene().getProjectionMatrix().multiplyToRef(this._projectionModeMatrix, this._cachedTextureMatrix);
  159. break;
  160. default:
  161. BABYLON.Matrix.IdentityToRef(this._cachedTextureMatrix);
  162. break;
  163. }
  164. return this._cachedTextureMatrix;
  165. };
  166. Texture.prototype.clone = function () {
  167. var newTexture = new Texture(this._texture.url, this.getScene(), this._noMipmap, this._invertY, this._samplingMode);
  168. // Base texture
  169. newTexture.hasAlpha = this.hasAlpha;
  170. newTexture.level = this.level;
  171. newTexture.wrapU = this.wrapU;
  172. newTexture.wrapV = this.wrapV;
  173. newTexture.coordinatesIndex = this.coordinatesIndex;
  174. newTexture.coordinatesMode = this.coordinatesMode;
  175. // Texture
  176. newTexture.uOffset = this.uOffset;
  177. newTexture.vOffset = this.vOffset;
  178. newTexture.uScale = this.uScale;
  179. newTexture.vScale = this.vScale;
  180. newTexture.uAng = this.uAng;
  181. newTexture.vAng = this.vAng;
  182. newTexture.wAng = this.wAng;
  183. return newTexture;
  184. };
  185. Texture.prototype.serialize = function () {
  186. if (!this.name) {
  187. return null;
  188. }
  189. var serializationObject = _super.prototype.serialize.call(this);
  190. serializationObject.uOffset = this.uOffset;
  191. serializationObject.vOffset = this.vOffset;
  192. serializationObject.uScale = this.uScale;
  193. serializationObject.vScale = this.vScale;
  194. serializationObject.uAng = this.uAng;
  195. serializationObject.vAng = this.vAng;
  196. serializationObject.wAng = this.wAng;
  197. return serializationObject;
  198. };
  199. // Statics
  200. Texture.CreateFromBase64String = function (data, name, scene, noMipmap, invertY, samplingMode, onLoad, onError) {
  201. if (samplingMode === void 0) { samplingMode = Texture.TRILINEAR_SAMPLINGMODE; }
  202. if (onLoad === void 0) { onLoad = null; }
  203. if (onError === void 0) { onError = null; }
  204. return new Texture("data:" + name, scene, noMipmap, invertY, samplingMode, onLoad, onError, data);
  205. };
  206. Texture.Parse = function (parsedTexture, scene, rootUrl) {
  207. if (parsedTexture.isCube) {
  208. return BABYLON.CubeTexture.Parse(parsedTexture, scene, rootUrl);
  209. }
  210. if (!parsedTexture.name && !parsedTexture.isRenderTarget) {
  211. return null;
  212. }
  213. var texture;
  214. if (parsedTexture.mirrorPlane) {
  215. texture = new BABYLON.MirrorTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  216. texture._waitingRenderList = parsedTexture.renderList;
  217. texture.mirrorPlane = BABYLON.Plane.FromArray(parsedTexture.mirrorPlane);
  218. }
  219. else if (parsedTexture.isRenderTarget) {
  220. texture = new BABYLON.RenderTargetTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene);
  221. texture._waitingRenderList = parsedTexture.renderList;
  222. }
  223. else {
  224. if (parsedTexture.base64String) {
  225. texture = Texture.CreateFromBase64String(parsedTexture.base64String, parsedTexture.name, scene);
  226. }
  227. else {
  228. texture = new Texture(rootUrl + parsedTexture.name, scene);
  229. }
  230. }
  231. texture.name = parsedTexture.name;
  232. texture.hasAlpha = parsedTexture.hasAlpha;
  233. texture.getAlphaFromRGB = parsedTexture.getAlphaFromRGB;
  234. texture.level = parsedTexture.level;
  235. texture.coordinatesIndex = parsedTexture.coordinatesIndex;
  236. texture.coordinatesMode = parsedTexture.coordinatesMode;
  237. texture.uOffset = parsedTexture.uOffset;
  238. texture.vOffset = parsedTexture.vOffset;
  239. texture.uScale = parsedTexture.uScale;
  240. texture.vScale = parsedTexture.vScale;
  241. texture.uAng = parsedTexture.uAng;
  242. texture.vAng = parsedTexture.vAng;
  243. texture.wAng = parsedTexture.wAng;
  244. texture.wrapU = parsedTexture.wrapU;
  245. texture.wrapV = parsedTexture.wrapV;
  246. // Animations
  247. if (parsedTexture.animations) {
  248. for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
  249. var parsedAnimation = parsedTexture.animations[animationIndex];
  250. texture.animations.push(BABYLON.Animation.Parse(parsedAnimation));
  251. }
  252. }
  253. return texture;
  254. };
  255. // Constants
  256. Texture.NEAREST_SAMPLINGMODE = 1;
  257. Texture.BILINEAR_SAMPLINGMODE = 2;
  258. Texture.TRILINEAR_SAMPLINGMODE = 3;
  259. Texture.EXPLICIT_MODE = 0;
  260. Texture.SPHERICAL_MODE = 1;
  261. Texture.PLANAR_MODE = 2;
  262. Texture.CUBIC_MODE = 3;
  263. Texture.PROJECTION_MODE = 4;
  264. Texture.SKYBOX_MODE = 5;
  265. Texture.INVCUBIC_MODE = 6;
  266. Texture.EQUIRECTANGULAR_MODE = 7;
  267. Texture.FIXED_EQUIRECTANGULAR_MODE = 8;
  268. Texture.CLAMP_ADDRESSMODE = 0;
  269. Texture.WRAP_ADDRESSMODE = 1;
  270. Texture.MIRROR_ADDRESSMODE = 2;
  271. return Texture;
  272. }(BABYLON.BaseTexture));
  273. BABYLON.Texture = Texture;
  274. })(BABYLON || (BABYLON = {}));