babylon.shaderMaterial.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 _matrices = new Array<Matrix>();
  13. private _cachedWorldViewMatrix = new BABYLON.Matrix();
  14. private _renderId: number;
  15. constructor(name: string, scene: Scene, shaderPath: any, options: any) {
  16. super(name, scene);
  17. this._shaderPath = shaderPath;
  18. options.needAlphaBlending = options.needAlphaBlending || false;
  19. options.needAlphaTesting = options.needAlphaTesting || false;
  20. options.attributes = options.attributes || ["position", "normal", "uv"];
  21. options.uniforms = options.uniforms || ["worldViewProjection"];
  22. options.samplers = options.samplers || [];
  23. this._options = options;
  24. }
  25. public needAlphaBlending(): boolean {
  26. return this._options.needAlphaBlending;
  27. }
  28. public needAlphaTesting(): boolean {
  29. return this._options.needAlphaTesting;
  30. }
  31. private _checkUniform(uniformName): void {
  32. if (this._options.uniforms.indexOf(uniformName) === -1) {
  33. this._options.uniforms.push(uniformName);
  34. }
  35. }
  36. public setTexture(name: string, texture: Texture): ShaderMaterial {
  37. if (this._options.samplers.indexOf(name) === -1) {
  38. this._options.samplers.push(name);
  39. }
  40. this._textures[name] = texture;
  41. return this;
  42. }
  43. public setFloat(name: string, value: number): ShaderMaterial {
  44. this._checkUniform(name);
  45. this._floats[name] = value;
  46. return this;
  47. }
  48. public setFloats(name: string, value: number[]): ShaderMaterial {
  49. this._checkUniform(name);
  50. this._floatsArrays[name] = value;
  51. return this;
  52. }
  53. public setColor3(name: string, value: Color3): ShaderMaterial {
  54. this._checkUniform(name);
  55. this._colors3[name] = value;
  56. return this;
  57. }
  58. public setColor4(name: string, value: Color4): ShaderMaterial {
  59. this._checkUniform(name);
  60. this._colors4[name] = value;
  61. return this;
  62. }
  63. public setVector2(name: string, value: Vector2): ShaderMaterial {
  64. this._checkUniform(name);
  65. this._vectors2[name] = value;
  66. return this;
  67. }
  68. public setVector3(name: string, value: Vector3): ShaderMaterial {
  69. this._checkUniform(name);
  70. this._vectors3[name] = value;
  71. return this;
  72. }
  73. public setMatrix(name: string, value: Matrix): ShaderMaterial {
  74. this._checkUniform(name);
  75. this._matrices[name] = value;
  76. return this;
  77. }
  78. public isReady(): boolean {
  79. var scene = this.getScene();
  80. var engine = scene.getEngine();
  81. if (!this.checkReadyOnEveryCall) {
  82. if (this._renderId === scene.getRenderId()) {
  83. return true;
  84. }
  85. }
  86. var previousEffect = this._effect;
  87. this._effect = engine.createEffect(this._shaderPath,
  88. this._options.attributes,
  89. this._options.uniforms,
  90. this._options.samplers,
  91. "", null, this.onCompiled, this.onError);
  92. if (!this._effect.isReady()) {
  93. return false;
  94. }
  95. if (previousEffect !== this._effect) {
  96. scene.resetCachedMaterial();
  97. }
  98. this._renderId = scene.getRenderId();
  99. return true;
  100. }
  101. public bindOnlyWorldMatrix(world: Matrix): void {
  102. var scene = this.getScene();
  103. if (this._options.uniforms.indexOf("world") !== -1) {
  104. this._effect.setMatrix("world", world);
  105. }
  106. if (this._options.uniforms.indexOf("worldView") !== -1) {
  107. world.multiplyToRef(scene.getViewMatrix(), this._cachedWorldViewMatrix);
  108. this._effect.setMatrix("worldView", this._cachedWorldViewMatrix);
  109. }
  110. if (this._options.uniforms.indexOf("worldViewProjection") !== -1) {
  111. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  112. }
  113. }
  114. public bind(world: Matrix): void {
  115. // Std values
  116. this.bindOnlyWorldMatrix(world);
  117. if (this.getScene().getCachedMaterial() !== this) {
  118. if (this._options.uniforms.indexOf("view") !== -1) {
  119. this._effect.setMatrix("view", this.getScene().getViewMatrix());
  120. }
  121. if (this._options.uniforms.indexOf("projection") !== -1) {
  122. this._effect.setMatrix("projection", this.getScene().getProjectionMatrix());
  123. }
  124. if (this._options.uniforms.indexOf("viewProjection") !== -1) {
  125. this._effect.setMatrix("viewProjection", this.getScene().getTransformMatrix());
  126. }
  127. // Texture
  128. for (var name in this._textures) {
  129. this._effect.setTexture(name, this._textures[name]);
  130. }
  131. // Float
  132. for (name in this._floats) {
  133. this._effect.setFloat(name, this._floats[name]);
  134. }
  135. // Float s
  136. for (name in this._floatsArrays) {
  137. this._effect.setArray(name, this._floatsArrays[name]);
  138. }
  139. // Color3
  140. for (name in this._colors3) {
  141. this._effect.setColor3(name, this._colors3[name]);
  142. }
  143. // Color4
  144. for (name in this._colors4) {
  145. var color = this._colors4[name];
  146. this._effect.setFloat4(name, color.r, color.g, color.b, color.a);
  147. }
  148. // Vector2
  149. for (name in this._vectors2) {
  150. this._effect.setVector2(name, this._vectors2[name]);
  151. }
  152. // Vector3
  153. for (name in this._vectors3) {
  154. this._effect.setVector3(name, this._vectors3[name]);
  155. }
  156. // Matrix
  157. for (name in this._matrices) {
  158. this._effect.setMatrix(name, this._matrices[name]);
  159. }
  160. }
  161. super.bind(world, null);
  162. }
  163. public dispose(forceDisposeEffect?: boolean): void {
  164. for (var name in this._textures) {
  165. this._textures[name].dispose();
  166. }
  167. this._textures = [];
  168. super.dispose(forceDisposeEffect);
  169. }
  170. }
  171. }