customMaterial.ts 11 KB

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