babylon.shaderMaterial.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. this._checkUniform(name);
  42. this._textures[name] = texture;
  43. return this;
  44. };
  45. BABYLON.ShaderMaterial.prototype.setFloat = function (name, value) {
  46. this._checkUniform(name);
  47. this._floats[name] = value;
  48. return this;
  49. };
  50. BABYLON.ShaderMaterial.prototype.setFloats = function (name, value) {
  51. this._checkUniform(name);
  52. this._floatsArrays[name] = value;
  53. return this;
  54. };
  55. BABYLON.ShaderMaterial.prototype.setColor3 = function (name, value) {
  56. this._checkUniform(name);
  57. this._colors3[name] = value;
  58. return this;
  59. };
  60. BABYLON.ShaderMaterial.prototype.setColor4 = function (name, value) {
  61. this._checkUniform(name);
  62. this._colors4[name] = value;
  63. return this;
  64. };
  65. BABYLON.ShaderMaterial.prototype.setVector2 = function (name, value) {
  66. this._checkUniform(name);
  67. this._vectors2[name] = value;
  68. return this;
  69. };
  70. BABYLON.ShaderMaterial.prototype.setVector3 = function (name, value) {
  71. this._checkUniform(name);
  72. this._vectors3[name] = value;
  73. return this;
  74. };
  75. BABYLON.ShaderMaterial.prototype.setVector4 = function (name, value) {
  76. this._checkUniform(name);
  77. this._vectors4[name] = value;
  78. return this;
  79. };
  80. BABYLON.ShaderMaterial.prototype.setMatrix = function (name, value) {
  81. this._checkUniform(name);
  82. this._matrices[name] = value;
  83. return this;
  84. };
  85. BABYLON.ShaderMaterial.prototype.isReady = function (mesh) {
  86. var engine = this._scene.getEngine();
  87. this._effect = engine.createEffect(this._shaderPath,
  88. this._options.attributes,
  89. this._options.uniforms,
  90. this._options.samplers,
  91. "");
  92. if (!this._effect.isReady()) {
  93. return false;
  94. }
  95. return true;
  96. };
  97. BABYLON.ShaderMaterial.prototype.bind = function (world, mesh) {
  98. // Std values
  99. if (this._options.uniforms.indexOf("world") !== -1) {
  100. this._effect.setMatrix("world", world);
  101. }
  102. if (this._options.uniforms.indexOf("view") !== -1) {
  103. this._effect.setMatrix("view", this._scene.getViewMatrix());
  104. }
  105. if (this._options.uniforms.indexOf("projection") !== -1) {
  106. this._effect.setMatrix("projection", this._scene.getProjectionMatrix());
  107. }
  108. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  109. this._effect.setMatrix("worldViewProjection", world.multiply(this._scene.getTransformMatrix()));
  110. }
  111. // Texture
  112. for (var name in this._textures) {
  113. this._effect.setTexture(name, this._textures[name]);
  114. }
  115. // Float
  116. for (name in this._floats) {
  117. this._effect.setFloat(name, this._floats[name]);
  118. }
  119. // Float s
  120. for (name in this._floatsArrays) {
  121. this._effect.setArray(name, this._floatsArrays[name]);
  122. }
  123. // Color3
  124. for (name in this._colors3) {
  125. this._effect.setColor3(name, this._colors3[name]);
  126. }
  127. // Color4
  128. for (name in this._colors4) {
  129. this._effect.setColor4(name, this._colors4[name]);
  130. }
  131. // Vector2
  132. for (name in this._vectors2) {
  133. this._effect.setVector2(name, this._vectors2[name]);
  134. }
  135. // Vector3
  136. for (name in this._vectors3) {
  137. this._effect.setVector3(name, this._vectors3[name]);
  138. }
  139. // Vector4
  140. for (name in this._vectors4) {
  141. this._effect.setVector4(name, this._vectors4[name]);
  142. }
  143. // Matrix
  144. for (name in this._matrices) {
  145. this._effect.setMatrix(name, this._matrices[name]);
  146. }
  147. };
  148. BABYLON.ShaderMaterial.prototype.dispose = function () {
  149. for (var name in this._textures) {
  150. this._textures[name].dispose();
  151. }
  152. this._textures = [];
  153. this.baseDispose();
  154. };
  155. })();