babylon.shaderMaterial.js 6.3 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. 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 engine = this.getScene().getEngine();
  85. this._effect = engine.createEffect(this._shaderPath, this._options.attributes, this._options.uniforms, this._options.samplers, "", null, this.onCompiled, this.onError);
  86. if (!this._effect.isReady()) {
  87. return false;
  88. }
  89. return true;
  90. };
  91. ShaderMaterial.prototype.bind = function (world) {
  92. // Std values
  93. if (this._options.uniforms.indexOf("world") !== -1) {
  94. this._effect.setMatrix("world", world);
  95. }
  96. if (this._options.uniforms.indexOf("view") !== -1) {
  97. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  98. }
  99. if (this._options.uniforms.indexOf("worldView") !== -1) {
  100. world.multiplyToRef(this.getScene().getViewMatrix(), this._cachedWorldViewMatrix);
  101. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  102. }
  103. if (this._options.uniforms.indexOf("projection") !== -1) {
  104. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  105. }
  106. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  107. this._effect.setMatrix("worldViewProjection", world.multiply(this.getScene().getTransformMatrix()));
  108. }
  109. for (var name in this._textures) {
  110. this._effect.setTexture(name, this._textures[name]);
  111. }
  112. for (name in this._floats) {
  113. this._effect.setFloat(name, this._floats[name]);
  114. }
  115. for (name in this._floatsArrays) {
  116. this._effect.setArray(name, this._floatsArrays[name]);
  117. }
  118. for (name in this._colors3) {
  119. this._effect.setColor3(name, this._colors3[name]);
  120. }
  121. for (name in this._colors4) {
  122. var color = this._colors4[name];
  123. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  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