babylon.shaderMaterial.js 6.2 KB

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