babylon.texture.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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._texture = this._getFromCache(url, noMipmap);
  8. if (!this._texture) {
  9. this._texture = scene.getEngine().createTexture(url, noMipmap, invertY, scene);
  10. }
  11. // Animations
  12. this.animations = [];
  13. };
  14. BABYLON.Texture.prototype = Object.create(BABYLON.BaseTexture.prototype);
  15. // Constants
  16. BABYLON.Texture.EXPLICIT_MODE = 0;
  17. BABYLON.Texture.SPHERICAL_MODE = 1;
  18. BABYLON.Texture.PLANAR_MODE = 2;
  19. BABYLON.Texture.CUBIC_MODE = 3;
  20. BABYLON.Texture.PROJECTION_MODE = 4;
  21. BABYLON.Texture.SKYBOX_MODE = 5;
  22. BABYLON.Texture.CLAMP_ADDRESSMODE = 0;
  23. BABYLON.Texture.WRAP_ADDRESSMODE = 1;
  24. BABYLON.Texture.MIRROR_ADDRESSMODE = 2;
  25. // Members
  26. BABYLON.Texture.prototype.uOffset = 0;
  27. BABYLON.Texture.prototype.vOffset = 0;
  28. BABYLON.Texture.prototype.uScale = 1.0;
  29. BABYLON.Texture.prototype.vScale = 1.0;
  30. BABYLON.Texture.prototype.uAng = 0;
  31. BABYLON.Texture.prototype.vAng = 0;
  32. BABYLON.Texture.prototype.wAng = 0;
  33. BABYLON.Texture.prototype.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  34. BABYLON.Texture.prototype.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  35. BABYLON.Texture.prototype.coordinatesIndex = 0;
  36. BABYLON.Texture.prototype.coordinatesMode = BABYLON.Texture.EXPLICIT_MODE;
  37. // Methods
  38. BABYLON.Texture.prototype._prepareRowForTextureGeneration = function (t) {
  39. var matRot = BABYLON.Matrix.RotationYawPitchRoll(this.vAng, this.uAng, this.wAng);
  40. t.x -= this.uOffset + 0.5;
  41. t.y -= this.vOffset + 0.5;
  42. t.z -= 0.5;
  43. t = BABYLON.Vector3.TransformCoordinates(t, matRot);
  44. t.x *= this.uScale;
  45. t.y *= this.vScale;
  46. t.x += 0.5;
  47. t.y += 0.5;
  48. t.z += 0.5;
  49. return t;
  50. };
  51. BABYLON.Texture.prototype._computeTextureMatrix = function () {
  52. if (
  53. this.uOffset === this._cachedUOffset &&
  54. this.vOffset === this._cachedVOffset &&
  55. this.uScale === this._cachedUScale &&
  56. this.vScale === this._cachedVScale &&
  57. this.uAng === this._cachedUAng &&
  58. this.vAng === this._cachedVAng &&
  59. this.wAng === this._cachedWAng) {
  60. return this._cachedTextureMatrix;
  61. }
  62. this._cachedUOffset = this.uOffset;
  63. this._cachedVOffset = this.vOffset;
  64. this._cachedUScale = this.uScale;
  65. this._cachedVScale = this.vScale;
  66. this._cachedUAng = this.uAng;
  67. this._cachedVAng = this.vAng;
  68. this._cachedWAng = this.wAng;
  69. var t0 = new BABYLON.Vector3(0, 0, 0);
  70. var t1 = new BABYLON.Vector3(1.0, 0, 0);
  71. var t2 = new BABYLON.Vector3(0, 1.0, 0);
  72. var matTemp = BABYLON.Matrix.Identity();
  73. t0 = this._prepareRowForTextureGeneration(t0);
  74. t1 = this._prepareRowForTextureGeneration(t1);
  75. t2 = this._prepareRowForTextureGeneration(t2);
  76. t1 = t1.subtract(t0);
  77. t2 = t2.subtract(t0);
  78. matTemp.m[0] = t1.x; matTemp.m[1] = t1.y; matTemp.m[2] = t1.z;
  79. matTemp.m[4] = t2.x; matTemp.m[5] = t2.y; matTemp.m[6] = t2.z;
  80. matTemp.m[8] = t0.x; matTemp.m[9] = t0.y; matTemp.m[10] = t0.z;
  81. this._cachedTextureMatrix = matTemp;
  82. return matTemp;
  83. };
  84. BABYLON.Texture.prototype._computeReflectionTextureMatrix = function () {
  85. if (
  86. this.uOffset === this._cachedUOffset &&
  87. this.vOffset === this._cachedVOffset &&
  88. this.uScale === this._cachedUScale &&
  89. this.vScale === this._cachedVScale &&
  90. this.coordinatesMode === this._cachedCoordinatesMode) {
  91. return this._cachedTextureMatrix;
  92. }
  93. var matrix = BABYLON.Matrix.Identity();
  94. switch (this.coordinatesMode) {
  95. case BABYLON.Texture.SPHERICAL_MODE:
  96. matrix.m[0] = -0.5 * this.uScale;
  97. matrix.m[5] = -0.5 * this.vScale;
  98. matrix.m[12] = 0.5 + this.uOffset;
  99. matrix.m[13] = 0.5 + this.vOffset;
  100. break;
  101. case BABYLON.Texture.PLANAR_MODE:
  102. matrix.m[0] = this.uScale;
  103. matrix.m[5] = this.vScale;
  104. matrix.m[12] = this.uOffset;
  105. matrix.m[13] = this.vOffset;
  106. break;
  107. case BABYLON.Texture.PROJECTION_MODE:
  108. matrix.m[0] = 0.5;
  109. matrix.m[5] = -0.5;
  110. matrix.m[10] = 0.0;
  111. matrix.m[12] = 0.5;
  112. matrix.m[13] = 0.5;
  113. matrix.m[14] = 1.0;
  114. matrix.m[15] = 1.0;
  115. matrix = this._scene.getProjectionMatrix().multiply(matrix);
  116. break;
  117. }
  118. this._cachedTextureMatrix = matrix;
  119. return matrix;
  120. };
  121. })();