babylon.shaderMaterial.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. module BABYLON {
  2. export class ShaderMaterial extends Material {
  3. private _shaderPath: any;
  4. private _options: any;
  5. private _textures = new Array<Texture>();
  6. private _floats = new Array<number>();
  7. private _floatsArrays = {};
  8. private _colors3 = new Array<Color3>();
  9. private _colors4 = new Array<Color4>();
  10. private _vectors2 = new Array<Vector2>();
  11. private _vectors3 = new Array<Vector3>();
  12. private _vectors4 = new Array<Vector4>();
  13. private _matrices = new Array<Matrix>();
  14. private _matrices3x3 = new Array<Float32Array>();
  15. private _matrices2x2 = new Array<Float32Array>();
  16. private _cachedWorldViewMatrix = new Matrix();
  17. private _renderId: number;
  18. constructor(name: string, scene: Scene, shaderPath: any, options: any) {
  19. super(name, scene);
  20. this._shaderPath = shaderPath;
  21. options.needAlphaBlending = options.needAlphaBlending || false;
  22. options.needAlphaTesting = options.needAlphaTesting || false;
  23. options.attributes = options.attributes || ["position", "normal", "uv"];
  24. options.uniforms = options.uniforms || ["worldViewProjection"];
  25. options.samplers = options.samplers || [];
  26. options.defines = options.defines || [];
  27. this._options = options;
  28. }
  29. public needAlphaBlending(): boolean {
  30. return this._options.needAlphaBlending;
  31. }
  32. public needAlphaTesting(): boolean {
  33. return this._options.needAlphaTesting;
  34. }
  35. private _checkUniform(uniformName): void {
  36. if (this._options.uniforms.indexOf(uniformName) === -1) {
  37. this._options.uniforms.push(uniformName);
  38. }
  39. }
  40. public setTexture(name: string, texture: Texture): ShaderMaterial {
  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. public setFloat(name: string, value: number): ShaderMaterial {
  48. this._checkUniform(name);
  49. this._floats[name] = value;
  50. return this;
  51. }
  52. public setFloats(name: string, value: number[]): ShaderMaterial {
  53. this._checkUniform(name);
  54. this._floatsArrays[name] = value;
  55. return this;
  56. }
  57. public setColor3(name: string, value: Color3): ShaderMaterial {
  58. this._checkUniform(name);
  59. this._colors3[name] = value;
  60. return this;
  61. }
  62. public setColor4(name: string, value: Color4): ShaderMaterial {
  63. this._checkUniform(name);
  64. this._colors4[name] = value;
  65. return this;
  66. }
  67. public setVector2(name: string, value: Vector2): ShaderMaterial {
  68. this._checkUniform(name);
  69. this._vectors2[name] = value;
  70. return this;
  71. }
  72. public setVector3(name: string, value: Vector3): ShaderMaterial {
  73. this._checkUniform(name);
  74. this._vectors3[name] = value;
  75. return this;
  76. }
  77. public setVector4(name: string, value: Vector4): ShaderMaterial {
  78. this._checkUniform(name);
  79. this._vectors4[name] = value;
  80. return this;
  81. }
  82. public setMatrix(name: string, value: Matrix): ShaderMaterial {
  83. this._checkUniform(name);
  84. this._matrices[name] = value;
  85. return this;
  86. }
  87. public setMatrix3x3(name: string, value: Float32Array): ShaderMaterial {
  88. this._checkUniform(name);
  89. this._matrices3x3[name] = value;
  90. return this;
  91. }
  92. public setMatrix2x2(name: string, value: Float32Array): ShaderMaterial {
  93. this._checkUniform(name);
  94. this._matrices2x2[name] = value;
  95. return this;
  96. }
  97. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  98. var scene = this.getScene();
  99. var engine = scene.getEngine();
  100. if (!this.checkReadyOnEveryCall) {
  101. if (this._renderId === scene.getRenderId()) {
  102. return true;
  103. }
  104. }
  105. // Instances
  106. var defines = [];
  107. var fallbacks = new EffectFallbacks();
  108. if (useInstances) {
  109. defines.push("#define INSTANCES");
  110. }
  111. for (var index = 0; index < this._options.defines.length; index++) {
  112. defines.push(this._options.defines[index]);
  113. }
  114. // Bones
  115. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  116. defines.push("#define BONES");
  117. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  118. defines.push("#define BONES4");
  119. fallbacks.addFallback(0, "BONES4");
  120. }
  121. // Alpha test
  122. if (engine.getAlphaTesting()) {
  123. defines.push("#define ALPHATEST");
  124. }
  125. var previousEffect = this._effect;
  126. var join = defines.join("\n");
  127. this._effect = engine.createEffect(this._shaderPath,
  128. this._options.attributes,
  129. this._options.uniforms,
  130. this._options.samplers,
  131. join, fallbacks, this.onCompiled, this.onError);
  132. if (!this._effect.isReady()) {
  133. return false;
  134. }
  135. if (previousEffect !== this._effect) {
  136. scene.resetCachedMaterial();
  137. }
  138. this._renderId = scene.getRenderId();
  139. return true;
  140. }
  141. public bindOnlyWorldMatrix(world: Matrix): void {
  142. var scene = this.getScene();
  143. if (this._options.uniforms.indexOf("world") !== -1) {
  144. this._effect.setMatrix("world", world);
  145. }
  146. if (this._options.uniforms.indexOf("worldView") !== -1) {
  147. world.multiplyToRef(scene.getViewMatrix(), this._cachedWorldViewMatrix);
  148. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  149. }
  150. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  151. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  152. }
  153. }
  154. public bind(world: Matrix, mesh?: Mesh): void {
  155. // Std values
  156. this.bindOnlyWorldMatrix(world);
  157. if (this.getScene().getCachedMaterial() !== this) {
  158. if (this._options.uniforms.indexOf("view") !== -1) {
  159. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  160. }
  161. if (this._options.uniforms.indexOf("projection") !== -1) {
  162. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  163. }
  164. if (this._options.uniforms.indexOf("viewProjection") !== -1) {
  165. this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  166. }
  167. // Bones
  168. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  169. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  170. }
  171. // Texture
  172. for (var name in this._textures) {
  173. this._effect.setTexture(name, this._textures[name]);
  174. }
  175. // Float
  176. for (name in this._floats) {
  177. this._effect.setFloat(name, this._floats[name]);
  178. }
  179. // Float s
  180. for (name in this._floatsArrays) {
  181. this._effect.setArray(name, this._floatsArrays[name]);
  182. }
  183. // Color3
  184. for (name in this._colors3) {
  185. this._effect.setColor3(name, this._colors3[name]);
  186. }
  187. // Color4
  188. for (name in this._colors4) {
  189. var color = this._colors4[name];
  190. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  191. }
  192. // Vector2
  193. for (name in this._vectors2) {
  194. this._effect.setVector2(name, this._vectors2[name]);
  195. }
  196. // Vector3
  197. for (name in this._vectors3) {
  198. this._effect.setVector3(name, this._vectors3[name]);
  199. }
  200. // Vector4
  201. for (name in this._vectors4) {
  202. this._effect.setVector4(name, this._vectors4[name]);
  203. }
  204. // Matrix
  205. for (name in this._matrices) {
  206. this._effect.setMatrix(name, this._matrices[name]);
  207. }
  208. // Matrix 3x3
  209. for (name in this._matrices3x3) {
  210. this._effect.setMatrix3x3(name, this._matrices3x3[name]);
  211. }
  212. // Matrix 2x2
  213. for (name in this._matrices2x2) {
  214. this._effect.setMatrix2x2(name, this._matrices2x2[name]);
  215. }
  216. }
  217. super.bind(world, mesh);
  218. }
  219. public clone(name: string): ShaderMaterial {
  220. var newShaderMaterial = new ShaderMaterial(name, this.getScene(), this._shaderPath, this._options);
  221. return newShaderMaterial;
  222. }
  223. public dispose(forceDisposeEffect?: boolean): void {
  224. for (var name in this._textures) {
  225. this._textures[name].dispose();
  226. }
  227. this._textures = [];
  228. super.dispose(forceDisposeEffect);
  229. }
  230. }
  231. }