babylon.shaderMaterial.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. __.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 (mesh, useInstances) {
  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. // Instances
  92. var defines = [];
  93. var fallbacks = new BABYLON.EffectFallbacks();
  94. if (useInstances) {
  95. defines.push("#define INSTANCES");
  96. }
  97. // Bones
  98. if (mesh && mesh.useBones) {
  99. defines.push("#define BONES");
  100. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  101. defines.push("#define BONES4");
  102. fallbacks.addFallback(0, "BONES4");
  103. }
  104. // Alpha test
  105. if (engine.getAlphaTesting()) {
  106. defines.push("#define ALPHATEST");
  107. }
  108. var previousEffect = this._effect;
  109. var join = defines.join("\n");
  110. this._effect = engine.createEffect(this._shaderPath, this._options.attributes, this._options.uniforms, this._options.samplers, join, fallbacks, this.onCompiled, this.onError);
  111. if (!this._effect.isReady()) {
  112. return false;
  113. }
  114. if (previousEffect !== this._effect) {
  115. scene.resetCachedMaterial();
  116. }
  117. this._renderId = scene.getRenderId();
  118. return true;
  119. };
  120. ShaderMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  121. var scene = this.getScene();
  122. if (this._options.uniforms.indexOf("world") !== -1) {
  123. this._effect.setMatrix("world", world);
  124. }
  125. if (this._options.uniforms.indexOf("worldView") !== -1) {
  126. world.multiplyToRef(scene.getViewMatrix(), this._cachedWorldViewMatrix);
  127. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  128. }
  129. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  130. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  131. }
  132. };
  133. ShaderMaterial.prototype.bind = function (world, mesh) {
  134. // Std values
  135. this.bindOnlyWorldMatrix(world);
  136. if (this.getScene().getCachedMaterial() !== this) {
  137. if (this._options.uniforms.indexOf("view") !== -1) {
  138. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  139. }
  140. if (this._options.uniforms.indexOf("projection") !== -1) {
  141. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  142. }
  143. if (this._options.uniforms.indexOf("viewProjection") !== -1) {
  144. this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  145. }
  146. // Bones
  147. if (mesh && mesh.useBones) {
  148. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  149. }
  150. // Texture
  151. for (var name in this._textures) {
  152. this._effect.setTexture(name, this._textures[name]);
  153. }
  154. // Float
  155. for (name in this._floats) {
  156. this._effect.setFloat(name, this._floats[name]);
  157. }
  158. // Float s
  159. for (name in this._floatsArrays) {
  160. this._effect.setArray(name, this._floatsArrays[name]);
  161. }
  162. // Color3
  163. for (name in this._colors3) {
  164. this._effect.setColor3(name, this._colors3[name]);
  165. }
  166. // Color4
  167. for (name in this._colors4) {
  168. var color = this._colors4[name];
  169. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  170. }
  171. // Vector2
  172. for (name in this._vectors2) {
  173. this._effect.setVector2(name, this._vectors2[name]);
  174. }
  175. // Vector3
  176. for (name in this._vectors3) {
  177. this._effect.setVector3(name, this._vectors3[name]);
  178. }
  179. // Matrix
  180. for (name in this._matrices) {
  181. this._effect.setMatrix(name, this._matrices[name]);
  182. }
  183. }
  184. _super.prototype.bind.call(this, world, mesh);
  185. };
  186. ShaderMaterial.prototype.dispose = function (forceDisposeEffect) {
  187. for (var name in this._textures) {
  188. this._textures[name].dispose();
  189. }
  190. this._textures = [];
  191. _super.prototype.dispose.call(this, forceDisposeEffect);
  192. };
  193. return ShaderMaterial;
  194. })(BABYLON.Material);
  195. BABYLON.ShaderMaterial = ShaderMaterial;
  196. })(BABYLON || (BABYLON = {}));
  197. //# sourceMappingURL=babylon.shaderMaterial.js.map