babylon.shaderMaterial.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.ShaderMaterial = function (name, scene, shaderPath, options) {
  5. this.name = name;
  6. this.id = name;
  7. this._shaderPath = shaderPath;
  8. options.needAlphaBlending = options.needAlphaBlending || false;
  9. options.needAlphaTesting = options.needAlphaTesting || false;
  10. options.attributes = options.attributes || ["position", "normal", "uv"];
  11. options.uniforms = options.uniforms || ["worldViewProjection"];
  12. options.samplers = options.samplers || [];
  13. this._options = options;
  14. this._scene = scene;
  15. scene.materials.push(this);
  16. this._textures = [];
  17. this._floats = [];
  18. this._floatsArrays = [];
  19. this._colors3 = [];
  20. this._colors4 = [];
  21. this._vectors2 = [];
  22. this._vectors3 = [];
  23. this._vectors4 = [];
  24. this._matrices = [];
  25. };
  26. BABYLON.ShaderMaterial.prototype = Object.create(BABYLON.Material.prototype);
  27. // Properties
  28. BABYLON.ShaderMaterial.prototype.needAlphaBlending = function () {
  29. return this._options.needAlphaBlending;
  30. };
  31. BABYLON.ShaderMaterial.prototype.needAlphaTesting = function () {
  32. return this._options.needAlphaTesting;
  33. };
  34. // Methods
  35. BABYLON.ShaderMaterial.prototype._checkUniform = function (uniformName) {
  36. if (this._options.uniforms.indexOf(uniformName) === -1) {
  37. this._options.uniforms.push(uniformName);
  38. }
  39. };
  40. BABYLON.ShaderMaterial.prototype.setTexture = function (name, texture) {
  41. if (this._options.samplers.indexOf(name) === -1) {
  42. this._options.samplers.push(name);
  43. }
  44. this._textures[name] = texture;
  45. return this;
  46. };
  47. BABYLON.ShaderMaterial.prototype.setFloat = function (name, value) {
  48. this._checkUniform(name);
  49. this._floats[name] = value;
  50. return this;
  51. };
  52. BABYLON.ShaderMaterial.prototype.setFloats = function (name, value) {
  53. this._checkUniform(name);
  54. this._floatsArrays[name] = value;
  55. return this;
  56. };
  57. BABYLON.ShaderMaterial.prototype.setColor3 = function (name, value) {
  58. this._checkUniform(name);
  59. this._colors3[name] = value;
  60. return this;
  61. };
  62. BABYLON.ShaderMaterial.prototype.setColor4 = function (name, value) {
  63. this._checkUniform(name);
  64. this._colors4[name] = value;
  65. return this;
  66. };
  67. BABYLON.ShaderMaterial.prototype.setVector2 = function (name, value) {
  68. this._checkUniform(name);
  69. this._vectors2[name] = value;
  70. return this;
  71. };
  72. BABYLON.ShaderMaterial.prototype.setVector3 = function (name, value) {
  73. this._checkUniform(name);
  74. this._vectors3[name] = value;
  75. return this;
  76. };
  77. BABYLON.ShaderMaterial.prototype.setVector4 = function (name, value) {
  78. this._checkUniform(name);
  79. this._vectors4[name] = value;
  80. return this;
  81. };
  82. BABYLON.ShaderMaterial.prototype.setMatrix = function (name, value) {
  83. this._checkUniform(name);
  84. this._matrices[name] = value;
  85. return this;
  86. };
  87. BABYLON.ShaderMaterial.prototype.isReady = function (mesh) {
  88. var engine = this._scene.getEngine();
  89. this._effect = engine.createEffect(this._shaderPath,
  90. this._options.attributes,
  91. this._options.uniforms,
  92. this._options.samplers,
  93. "", null, this.onCompiled, this.onError);
  94. if (!this._effect.isReady()) {
  95. return false;
  96. }
  97. return true;
  98. };
  99. BABYLON.ShaderMaterial.prototype.bind = function (world, mesh) {
  100. // Std values
  101. if (this._options.uniforms.indexOf("world") !== -1) {
  102. this._effect.setMatrix("world", world);
  103. }
  104. if (this._options.uniforms.indexOf("view") !== -1) {
  105. this._effect.setMatrix("view", this._scene.getViewMatrix());
  106. }
  107. if (this._options.uniforms.indexOf("worldView") !== -1) {
  108. this._effect.setMatrix("worldView", world.multiply(this._scene.getViewMatrix()));
  109. }
  110. if (this._options.uniforms.indexOf("projection") !== -1) {
  111. this._effect.setMatrix("projection", this._scene.getProjectionMatrix());
  112. }
  113. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  114. this._effect.setMatrix("worldViewProjection", world.multiply(this._scene.getTransformMatrix()));
  115. }
  116. // Texture
  117. for (var name in this._textures) {
  118. this._effect.setTexture(name, this._textures[name]);
  119. }
  120. // Float
  121. for (name in this._floats) {
  122. this._effect.setFloat(name, this._floats[name]);
  123. }
  124. // Float s
  125. for (name in this._floatsArrays) {
  126. this._effect.setArray(name, this._floatsArrays[name]);
  127. }
  128. // Color3
  129. for (name in this._colors3) {
  130. this._effect.setColor3(name, this._colors3[name]);
  131. }
  132. // Color4
  133. for (name in this._colors4) {
  134. this._effect.setColor4(name, this._colors4[name]);
  135. }
  136. // Vector2
  137. for (name in this._vectors2) {
  138. this._effect.setVector2(name, this._vectors2[name]);
  139. }
  140. // Vector3
  141. for (name in this._vectors3) {
  142. this._effect.setVector3(name, this._vectors3[name]);
  143. }
  144. // Vector4
  145. for (name in this._vectors4) {
  146. this._effect.setVector4(name, this._vectors4[name]);
  147. }
  148. // Matrix
  149. for (name in this._matrices) {
  150. this._effect.setMatrix(name, this._matrices[name]);
  151. }
  152. };
  153. BABYLON.ShaderMaterial.prototype.dispose = function (forceDisposeEffect) {
  154. for (var name in this._textures) {
  155. this._textures[name].dispose();
  156. }
  157. this._textures = [];
  158. this.baseDispose(forceDisposeEffect);
  159. };
  160. })();