customMaterial.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { Texture } from "babylonjs/Materials/Textures/texture";
  2. import { Effect } from "babylonjs/Materials/effect";
  3. import { StandardMaterialDefines } from "babylonjs/Materials/standardMaterial";
  4. import { StandardMaterial } from "babylonjs/Materials/standardMaterial";
  5. import { Mesh } from "babylonjs/Meshes/mesh";
  6. import { Scene } from "babylonjs/scene";
  7. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  8. export class CustomShaderStructure {
  9. public FragmentStore: string;
  10. public VertexStore: string;
  11. constructor() { }
  12. }
  13. export class ShaderSpecialParts {
  14. constructor() { }
  15. public Fragment_Begin: string;
  16. public Fragment_Definitions: string;
  17. public Fragment_MainBegin: string;
  18. // diffuseColor
  19. public Fragment_Custom_Diffuse: string;
  20. // lights
  21. public Fragment_Before_Lights: string;
  22. // fog
  23. public Fragment_Before_Fog: string;
  24. // alpha
  25. public Fragment_Custom_Alpha: string;
  26. public Fragment_Before_FragColor: string;
  27. public Vertex_Begin: string;
  28. public Vertex_Definitions: string;
  29. public Vertex_MainBegin: string;
  30. // positionUpdated
  31. public Vertex_Before_PositionUpdated: string;
  32. // normalUpdated
  33. public Vertex_Before_NormalUpdated: string;
  34. // mainEnd
  35. public Vertex_MainEnd: string;
  36. }
  37. export class CustomMaterial extends StandardMaterial {
  38. public static ShaderIndexer = 1;
  39. public CustomParts: ShaderSpecialParts;
  40. _isCreatedShader: boolean;
  41. _createdShaderName: string;
  42. _customUniform: string[];
  43. _newUniforms: string[];
  44. _newUniformInstances: any[];
  45. _newSamplerInstances: Texture[];
  46. _customAttributes: string[];
  47. public FragmentShader: string;
  48. public VertexShader: string;
  49. public AttachAfterBind(mesh: Mesh, effect: Effect) {
  50. for (var el in this._newUniformInstances) {
  51. var ea = el.toString().split('-');
  52. if (ea[0] == 'vec2') {
  53. effect.setVector2(ea[1], this._newUniformInstances[el]);
  54. }
  55. else if (ea[0] == 'vec3') {
  56. effect.setVector3(ea[1], this._newUniformInstances[el]);
  57. }
  58. else if (ea[0] == 'vec4') {
  59. effect.setVector4(ea[1], this._newUniformInstances[el]);
  60. }
  61. else if (ea[0] == 'mat4') {
  62. effect.setMatrix(ea[1], this._newUniformInstances[el]);
  63. }
  64. else if (ea[0] == 'float') {
  65. effect.setFloat(ea[1], this._newUniformInstances[el]);
  66. }
  67. }
  68. for (var el in this._newSamplerInstances) {
  69. var ea = el.toString().split('-');
  70. if (ea[0] == 'sampler2D' && this._newSamplerInstances[el].isReady && this._newSamplerInstances[el].isReady()) {
  71. effect.setTexture(ea[1], this._newSamplerInstances[el]);
  72. }
  73. }
  74. }
  75. public ReviewUniform(name: string, arr: string[]): string[] {
  76. if (name == "uniform") {
  77. for (var ind in this._newUniforms) {
  78. if (this._customUniform[ind].indexOf('sampler') == -1) {
  79. arr.push(this._newUniforms[ind]);
  80. }
  81. }
  82. }
  83. if (name == "sampler") {
  84. for (var ind in this._newUniforms) {
  85. if (this._customUniform[ind].indexOf('sampler') != -1) {
  86. arr.push(this._newUniforms[ind]);
  87. }
  88. }
  89. }
  90. return arr;
  91. }
  92. public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines, attributes?: string[]): string {
  93. if (attributes && this._customAttributes && this._customAttributes.length > 0) {
  94. attributes.push(...this._customAttributes);
  95. }
  96. this.ReviewUniform("uniform", uniforms);
  97. this.ReviewUniform("sampler", samplers);
  98. if (this._isCreatedShader) {
  99. return this._createdShaderName;
  100. }
  101. this._isCreatedShader = false;
  102. CustomMaterial.ShaderIndexer++;
  103. var name: string = "custom_" + CustomMaterial.ShaderIndexer;
  104. var fn_afterBind = this._afterBind.bind(this);
  105. this._afterBind = (m, e) => {
  106. if (!e) {
  107. return;
  108. }
  109. this.AttachAfterBind(m, e);
  110. try { fn_afterBind(m, e); }
  111. catch (e) { }
  112. };
  113. Effect.ShadersStore[name + "VertexShader"] = this.VertexShader
  114. .replace('#define CUSTOM_VERTEX_BEGIN', (this.CustomParts.Vertex_Begin ? this.CustomParts.Vertex_Begin : ""))
  115. .replace('#define CUSTOM_VERTEX_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Vertex_Definitions ? this.CustomParts.Vertex_Definitions : ""))
  116. .replace('#define CUSTOM_VERTEX_MAIN_BEGIN', (this.CustomParts.Vertex_MainBegin ? this.CustomParts.Vertex_MainBegin : ""))
  117. .replace('#define CUSTOM_VERTEX_UPDATE_POSITION', (this.CustomParts.Vertex_Before_PositionUpdated ? this.CustomParts.Vertex_Before_PositionUpdated : ""))
  118. .replace('#define CUSTOM_VERTEX_UPDATE_NORMAL', (this.CustomParts.Vertex_Before_NormalUpdated ? this.CustomParts.Vertex_Before_NormalUpdated : ""))
  119. .replace('#define CUSTOM_VERTEX_MAIN_END', (this.CustomParts.Vertex_MainEnd ? this.CustomParts.Vertex_MainEnd : ""));
  120. Effect.ShadersStore[name + "PixelShader"] = this.FragmentShader
  121. .replace('#define CUSTOM_FRAGMENT_BEGIN', (this.CustomParts.Fragment_Begin ? this.CustomParts.Fragment_Begin : ""))
  122. .replace('#define CUSTOM_FRAGMENT_MAIN_BEGIN', (this.CustomParts.Fragment_MainBegin ? this.CustomParts.Fragment_MainBegin : ""))
  123. .replace('#define CUSTOM_FRAGMENT_DEFINITIONS', (this._customUniform ? this._customUniform.join("\n") : "") + (this.CustomParts.Fragment_Definitions ? this.CustomParts.Fragment_Definitions : ""))
  124. .replace('#define CUSTOM_FRAGMENT_UPDATE_DIFFUSE', (this.CustomParts.Fragment_Custom_Diffuse ? this.CustomParts.Fragment_Custom_Diffuse : ""))
  125. .replace('#define CUSTOM_FRAGMENT_UPDATE_ALPHA', (this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
  126. .replace('#define CUSTOM_FRAGMENT_BEFORE_LIGHTS', (this.CustomParts.Fragment_Before_Lights ? this.CustomParts.Fragment_Before_Lights : ""))
  127. .replace('#define CUSTOM_FRAGMENT_BEFORE_FOG', (this.CustomParts.Fragment_Before_Fog ? this.CustomParts.Fragment_Before_Fog : ""))
  128. .replace('#define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR', (this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : ""));
  129. this._isCreatedShader = true;
  130. this._createdShaderName = name;
  131. return name;
  132. }
  133. constructor(name: string, scene: Scene) {
  134. super(name, scene);
  135. this.CustomParts = new ShaderSpecialParts();
  136. this.customShaderNameResolve = this.Builder;
  137. this.FragmentShader = Effect.ShadersStore["defaultPixelShader"];
  138. this.VertexShader = Effect.ShadersStore["defaultVertexShader"];
  139. }
  140. public AddUniform(name: string, kind: string, param: any): CustomMaterial {
  141. if (!this._customUniform) {
  142. this._customUniform = new Array();
  143. this._newUniforms = new Array();
  144. this._newSamplerInstances = new Array();
  145. this._newUniformInstances = new Array();
  146. }
  147. if (param) {
  148. if (kind.indexOf("sampler") == -1) {
  149. (<any>this._newUniformInstances)[kind + "-" + name] = param;
  150. }
  151. else {
  152. (<any>this._newUniformInstances)[kind + "-" + name] = param;
  153. }
  154. }
  155. this._customUniform.push("uniform " + kind + " " + name + ";");
  156. this._newUniforms.push(name);
  157. return this;
  158. }
  159. public AddAttribute(name: string): CustomMaterial {
  160. if (!this._customAttributes) {
  161. this._customAttributes = [];
  162. }
  163. this._customAttributes.push(name);
  164. return this;
  165. }
  166. public Fragment_Begin(shaderPart: string): CustomMaterial {
  167. this.CustomParts.Fragment_Begin = shaderPart;
  168. return this;
  169. }
  170. public Fragment_Definitions(shaderPart: string): CustomMaterial {
  171. this.CustomParts.Fragment_Definitions = shaderPart;
  172. return this;
  173. }
  174. public Fragment_MainBegin(shaderPart: string): CustomMaterial {
  175. this.CustomParts.Fragment_MainBegin = shaderPart;
  176. return this;
  177. }
  178. public Fragment_Custom_Diffuse(shaderPart: string): CustomMaterial {
  179. this.CustomParts.Fragment_Custom_Diffuse = shaderPart.replace("result", "diffuseColor");
  180. return this;
  181. }
  182. public Fragment_Custom_Alpha(shaderPart: string): CustomMaterial {
  183. this.CustomParts.Fragment_Custom_Alpha = shaderPart.replace("result", "alpha");
  184. return this;
  185. }
  186. public Fragment_Before_Lights(shaderPart: string): CustomMaterial {
  187. this.CustomParts.Fragment_Before_Lights = shaderPart;
  188. return this;
  189. }
  190. public Fragment_Before_Fog(shaderPart: string): CustomMaterial {
  191. this.CustomParts.Fragment_Before_Fog = shaderPart;
  192. return this;
  193. }
  194. public Fragment_Before_FragColor(shaderPart: string): CustomMaterial {
  195. this.CustomParts.Fragment_Before_FragColor = shaderPart.replace("result", "color");
  196. return this;
  197. }
  198. public Vertex_Begin(shaderPart: string): CustomMaterial {
  199. this.CustomParts.Vertex_Begin = shaderPart;
  200. return this;
  201. }
  202. public Vertex_Definitions(shaderPart: string): CustomMaterial {
  203. this.CustomParts.Vertex_Definitions = shaderPart;
  204. return this;
  205. }
  206. public Vertex_MainBegin(shaderPart: string): CustomMaterial {
  207. this.CustomParts.Vertex_MainBegin = shaderPart;
  208. return this;
  209. }
  210. public Vertex_Before_PositionUpdated(shaderPart: string): CustomMaterial {
  211. this.CustomParts.Vertex_Before_PositionUpdated = shaderPart.replace("result", "positionUpdated");
  212. return this;
  213. }
  214. public Vertex_Before_NormalUpdated(shaderPart: string): CustomMaterial {
  215. this.CustomParts.Vertex_Before_NormalUpdated = shaderPart.replace("result", "normalUpdated");
  216. return this;
  217. }
  218. public Vertex_MainEnd(shaderPart: string): CustomMaterial {
  219. this.CustomParts.Vertex_MainEnd = shaderPart;
  220. return this;
  221. }
  222. }
  223. _TypeStore.RegisteredTypes["BABYLON.CustomMaterial"] = CustomMaterial;