pbrCustomMaterial.ts 11 KB

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