babylon.shaderMaterial.js 6.1 KB

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