Sign in to follow this  

Samples for CustomMaterial

Recommended Posts

hi i start write some documentation here for custom material ( its realy easy for me to make it here but i can move it in documentation after that complete )

definition : Custom Material inherited from stable version of Standard material (a carbon Copy of BABYLON.StandarMaterial)

when we need CustomMaterial ? : any time you wanna make any custom option but you can't manage that in standard material . 

vertex shader definitions : 

  1. position(readonly)  or positionUpdated(vec3) : local position per vertex
  2. normal(readonly) or NormalUpdated(vec3) : local normal

fragment shader definitions :

  1. vPositionW( readonly) : world position per pixel
  2. vNormalW(read only) 
  3. vDiffuseUV(vec2) : (readonly) : defined uv attribute append when you have DiffuseTexture
  4. diffuseColor(vec3) you can manage that with Fragment_Custom_Diffuse : for control Diffuse color   **** that mixed with diffuseTexture if you add any texture
  5. alpha(float) you can manage that from Fragment_Custom_alpha : for control transparency
  6. color(vec4) last result after attached all effect (light fog shadow ... ) you can manage that in Fragment_before_FragColor 


methods : 


.material = new BABYLON.CustomMaterial("name",scene);

Demo :  You can define customMaterial exactly like StandardMaterial and you have all property and methods in custom material too


  1. simple (float,vec2, vec3,vec4,...)  demo 
  2. sampler without UV  with Define UV
  3. array array3 , float


material.Fragment_Begin( string)

#extension GL_OES_standard_derivatives : enable

for define any new extension or include any shader (not find any requirement for make sample)


material.Fragment_Definitions( string)

void main(void) { 

this define before main  you can define any varying or global function before main

demo : make varying for simple noise used vertex data to add normal

demo : change vertex use definition function


void main(void) { 
vNormalW = vNormalW_helper; 

demo : correct normal for back face


you most find your result red and green and blue (witch any way you like ) and set it in diffuseColor or result

*** result (vec3) in this method replaced (one time) by diffuseColor 

set texture  setTexture use diffuseColor


you most find your alpha and set it to alpha variable or in result

*** result (vec3) in this method replaced (one time) byalpha 

demo manage transparency


before the last result you have chance to manage your result your final color available in color variable and you most be set it color after your changes

*** result (vec4) in this method replaced (one time) by color 

demo result with and without light effect


for define any new extension or include any shader (not find any requirement for make sample)


this define before main  you can define any varying or global function before main


same as Fragment_Main


localPosition = positionUpdated; 

you most change positionUpdated in here for change last vertex result


#ifdef NORMAL 
localNormal = normalUpdated; 

you most change normalUpdated in here for change last vertex normal result

related samples

Update Uniform demo


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.