Jump to content

[Solved] Fragment shader diffuse lighting hemispheric light for custom material


Recommended Posts


I've searched and didn't find an answer, apologize if it was already asked, I'm fairly new to the shader world.

I'm working on a custom material shader for a height ground mesh that gets a different texture depending on elevation , slope ...

I have a prototype working, but now I want to add some light to my scene and I'm struggling to figure out how to replicate the same behaviour as the standard material fragment shader.

I have only one Hemi Light in my scene and I want only diffuse lighting, I tried to do as the Phong example in the CYOS, compute the angle between normal and light vector, and it works, but this is behaving like a PointLight, not HemiLight

Here is the example with StandardMaterial (the lighting I want):


Here is the example with my CustomMaterial:


The shader code: https://github.com/tdurand/3d-experiments/blob/gh-pages/terrain.fragment.fx#L22

Could you point me to some ressources to get the fragment shader behave like the standard material for an HemiLight ? What is exactly the difference between an HemiLight and a point light far away?  I tried to investigate the code of the standard material fragment shader without success for now: https://github.com/BabylonJS/Babylon.js/blob/master/src/Shaders/default.fragment.fx








Link to comment
Share on other sites

Really good, for the record the hemi light is mixing influence of both ground and light source. I could replicate the same effect as in Standard material by setting the ground color to black and the sky to white.

The crucial part of the fragment shader was:

// Compute diffuse weight from normal and light vector
float diffuseWeight = 0.5 * dot(vNormalW, lightVectorW) + 0.5;
// Mix black, white with our diffuse weight
vec3 diffuseColor = mix(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0), diffuseWeight);
// Apply diffuse effect to final color
gl_FragColor = color * vec4(diffuseColor, 1.0);


Thanks a lot

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...