babylon.shaderMaterial.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 ShaderMaterial = (function (_super) {
  10. __extends(ShaderMaterial, _super);
  11. function ShaderMaterial(name, scene, shaderPath, options) {
  12. _super.call(this, name, scene);
  13. this._textures = new Array();
  14. this._floats = new Array();
  15. this._floatsArrays = {};
  16. this._colors3 = new Array();
  17. this._colors4 = new Array();
  18. this._vectors2 = new Array();
  19. this._vectors3 = new Array();
  20. this._matrices = new Array();
  21. this._cachedWorldViewMatrix = new BABYLON.Matrix();
  22. this._shaderPath = shaderPath;
  23. options.needAlphaBlending = options.needAlphaBlending || false;
  24. options.needAlphaTesting = options.needAlphaTesting || false;
  25. options.attributes = options.attributes || ["position", "normal", "uv"];
  26. options.uniforms = options.uniforms || ["worldViewProjection"];
  27. options.samplers = options.samplers || [];
  28. this._options = options;
  29. }
  30. ShaderMaterial.prototype.needAlphaBlending = function () {
  31. return this._options.needAlphaBlending;
  32. };
  33. ShaderMaterial.prototype.needAlphaTesting = function () {
  34. return this._options.needAlphaTesting;
  35. };
  36. ShaderMaterial.prototype._checkUniform = function (uniformName) {
  37. if (this._options.uniforms.indexOf(uniformName) === -1) {
  38. this._options.uniforms.push(uniformName);
  39. }
  40. };
  41. ShaderMaterial.prototype.setTexture = function (name, texture) {
  42. if (this._options.samplers.indexOf(name) === -1) {
  43. this._options.samplers.push(name);
  44. }
  45. this._textures[name] = texture;
  46. return this;
  47. };
  48. ShaderMaterial.prototype.setFloat = function (name, value) {
  49. this._checkUniform(name);
  50. this._floats[name] = value;
  51. return this;
  52. };
  53. ShaderMaterial.prototype.setFloats = function (name, value) {
  54. this._checkUniform(name);
  55. this._floatsArrays[name] = value;
  56. return this;
  57. };
  58. ShaderMaterial.prototype.setColor3 = function (name, value) {
  59. this._checkUniform(name);
  60. this._colors3[name] = value;
  61. return this;
  62. };
  63. ShaderMaterial.prototype.setColor4 = function (name, value) {
  64. this._checkUniform(name);
  65. this._colors4[name] = value;
  66. return this;
  67. };
  68. ShaderMaterial.prototype.setVector2 = function (name, value) {
  69. this._checkUniform(name);
  70. this._vectors2[name] = value;
  71. return this;
  72. };
  73. ShaderMaterial.prototype.setVector3 = function (name, value) {
  74. this._checkUniform(name);
  75. this._vectors3[name] = value;
  76. return this;
  77. };
  78. ShaderMaterial.prototype.setMatrix = function (name, value) {
  79. this._checkUniform(name);
  80. this._matrices[name] = value;
  81. return this;
  82. };
  83. ShaderMaterial.prototype.isReady = function () {
  84. var scene = this.getScene();
  85. var engine = scene.getEngine();
  86. if (!this.checkReadyOnEveryCall) {
  87. if (this._renderId === scene.getRenderId()) {
  88. return true;
  89. }
  90. }
  91. var previousEffect = this._effect;
  92. this._effect = engine.createEffect(this._shaderPath, this._options.attributes, this._options.uniforms, this._options.samplers, "", null, this.onCompiled, this.onError);
  93. if (!this._effect.isReady()) {
  94. return false;
  95. }
  96. if (previousEffect !== this._effect) {
  97. scene.resetCachedMaterial();
  98. }
  99. this._renderId = scene.getRenderId();
  100. return true;
  101. };
  102. ShaderMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  103. var scene = this.getScene();
  104. if (this._options.uniforms.indexOf("world") !== -1) {
  105. this._effect.setMatrix("world", world);
  106. }
  107. if (this._options.uniforms.indexOf("worldView") !== -1) {
  108. world.multiplyToRef(scene.getViewMatrix(), this._cachedWorldViewMatrix);
  109. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  110. }
  111. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  112. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  113. }
  114. };
  115. ShaderMaterial.prototype.bind = function (world) {
  116. // Std values
  117. this.bindOnlyWorldMatrix(world);
  118. if (this.getScene().getCachedMaterial() !== this) {
  119. if (this._options.uniforms.indexOf("view") !== -1) {
  120. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  121. }
  122. if (this._options.uniforms.indexOf("projection") !== -1) {
  123. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  124. }
  125. if (this._options.uniforms.indexOf("viewProjection") !== -1) {
  126. this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  127. }
  128. for (var name in this._textures) {
  129. this._effect.setTexture(name, this._textures[name]);
  130. }
  131. for (name in this._floats) {
  132. this._effect.setFloat(name, this._floats[name]);
  133. }
  134. for (name in this._floatsArrays) {
  135. this._effect.setArray(name, this._floatsArrays[name]);
  136. }
  137. for (name in this._colors3) {
  138. this._effect.setColor3(name, this._colors3[name]);
  139. }
  140. for (name in this._colors4) {
  141. var color = this._colors4[name];
  142. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  143. }
  144. for (name in this._vectors2) {
  145. this._effect.setVector2(name, this._vectors2[name]);
  146. }
  147. for (name in this._vectors3) {
  148. this._effect.setVector3(name, this._vectors3[name]);
  149. }
  150. for (name in this._matrices) {
  151. this._effect.setMatrix(name, this._matrices[name]);
  152. }
  153. }
  154. _super.prototype.bind.call(this, world, null);
  155. };
  156. ShaderMaterial.prototype.dispose = function (forceDisposeEffect) {
  157. for (var name in this._textures) {
  158. this._textures[name].dispose();
  159. }
  160. this._textures = [];
  161. _super.prototype.dispose.call(this, forceDisposeEffect);
  162. };
  163. return ShaderMaterial;
  164. })(BABYLON.Material);
  165. BABYLON.ShaderMaterial = ShaderMaterial;
  166. })(BABYLON || (BABYLON = {}));
  167. //# sourceMappingURL=babylon.shaderMaterial.js.map