Back to Babylon Js

Custom Materials

packages/dev/materials/src/custom/readme.md

9.5.25.6 KB
Original Source

Custom Materials

CustomMaterial is a StandardMaterial with some customizable Options

PBRCustomMaterial is a PBRMaterial with some customizable Options

1- manage part of current shader

CustomMaterial or PBRCustomMaterial put some part of shader in special part of current shader and make new shader in ShaderStore shaders struct :

[ Begin ]

. // includes extensions varyng uniforms attributes special functions

[ Definations ]

void main(){

  [ Main Begin ]

  .
  .
  .

  [  ]

}

method : SelectVersion(ver:string)

Custom materials for now Supported just ver 3.0.0 of BabylonJs and this is default of currentVersion for now Add other old version in Progress %

method : AddUniform(name:string,kind:string,param:any):CustomMaterial

for append dynamic setting and manage </br> this method Add Unforn in bouth of shaders(Fragment and Vertex) </br> usage : new CustomMaterial(...).AddUniform('time','float') </br> : new CustomMaterial(...).AddUniform('direction','vec3',new BABYLON.Vector3(0.,0.,0.)) </br> : new CustomMaterial(...).AddUniform('txt1','sampler2D', new BABYLON.Texture("path",scene))

method : Fragment_Begin(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in start of main function in fragment shader </br> usage : new CustomMaterial(...).Fragment_Begin('vec3 direction = vec3(0.);')

method : Fragment_Definations(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in befor of the main function in fragment shader </br> you can define your varyng and functions from this </br> usage : new CustomMaterial(...).Fragment_Definations('float func1(vec4 param1){ return param1.x;}') </br>

  • dont try use uniform with this function because uniforms need to add buffers

method : Fragment_MainBegin(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in start place of the main function in fragment shader

CustomMaterial Only method : Fragment_Custom_Diffuse(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append after diffuseColor is defined of the main function in fragment shader </br> usage : new CustomMaterial(...).Fragment_Custom_Diffuse('diffuseColor = vec3(sin(vPositionW.x));') </br> : new CustomMaterial(...).Fragment_Custom_Diffuse('result = vec3(sin(vPositionW.x));') </br>

  • diffuseColor is vec3 variable </br>
  • you can use result (vec3) too that replaced by diffuseColor

PBRCustomMaterial Only method : Fragment_Custom_Albedo(shaderPart:string):PBRCustomMaterial

shaderPart is Shader Structure append after surfaceAlbedo is defined of the main function in fragment shader </br> usage : new PBRCustomMaterial(...).Fragment_Custom_Albedo('surfaceAlbedo = vec3(sin(vPositionW.x));') </br> : new PBRCustomMaterial(...).Fragment_Custom_Albedo('result = vec3(sin(vPositionW.x));') </br>

  • surfaceAlbedo is vec3 variable </br>
  • you can use result (vec3) too that replaced by surfaceAlbedo

method : Fragment_Custom_Alpha(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append when material need a alpha parameter in fragment shader </br>

method : Fragment_Before_FragColor(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append before gl_FragColor wanna be set in fragment shader </br>

  • color is vec4 parameter be set in last part to gl_fragcolor
  • you can use result (vec4) too
  • this part your last chance for change the frag color

method : Vertex_Begin(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in start of main function in vertex shader </br> usage : new CustomMaterial(...).Vertex_Begin('vec3 direction = vec3(0.);')

method : Vertex_Definations(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in befor of the main function in vertex shader </br> you can define your varyng and functions from this </br> usage : new CustomMaterial(...).Vertex_Definations('float func1(vec4 param1){ return param1.x;}') </br>

  • dont try use uniform with this function because uniforms need to add buffers </br>
  • for connect any information between vertex and fragment part in shader you can define varying you need make that in both of definition part

method : Vertex_MainBegin(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append in start place of the main function in vertex shader

method : Vertex_Befor_PositionUpdated(shaderPart:string):CustomMaterial{

shaderPart is Shader Structure append after positionUpdated is defined of the main function in vertex shader </br> usage : new CustomMaterial(...).Vertex_Befor_PositionUpdated('positionUpdated = positionUpdated;') </br> : new CustomMaterial(...).Vertex_Befor_PositionUpdated('result = positionUpdated * 1.5 ;') </br>

  • positionUpdated is vec3 variable </br>
  • you can use result (vec3) too that replaced by positionUpdated
  • you can use 'normal' attribute in this part too

method : Vertex_Befor_NormalUpdated(shaderPart:string):CustomMaterial

shaderPart is Shader Structure append after normalUpdated is defined of the main function in vertex shader </br> usage : new CustomMaterial(...).Vertex_Befor_NormalUpdated('normalUpdated = normalUpdated;') </br> : new CustomMaterial(...).Vertex_Befor_NormalUpdated('result = normalUpdated ;') </br>

  • normalUpdated is vec3 variable </br>
  • you can use result (vec3) too that replaced by normalUpdated
  • you can use positionUpdated too in this part