Need help to fix normal in shader

Hi everyone, I hope you are all great and having a good time !!

I am currently working on this hair simulator using PBRCustomMaterial: https://playground.babylonjs.com/#LVJG7H#381

It works great as I have the movement I want, but now I have an issue with the PBR rendering of the hair. In the playground above, we are supposed to see blue hair as the albedo is settled (Line 271). PBR seems broken, as we have weird results when playing with roughness and metallic parameters. If we remove the additional fragment and vertex shader, it looks ok, so the issue must come from the shader code.

From what I found, this is maybe the normal calculation (Lines 372-378) that is causing the issue. Could someone help me solve this? I guess this is a subject for @Evgeni_Popov ?

Thanks a lot ! Pichou

1 Like

It’s cuz you forgor to take environment lighting into account and have no actual lights in your scene

it seems the huge chunk that’s commented out has some useful stuff lol

2 Likes

Environment irradiance has been commented out in the vertex shader. As you only use an environment map, lighting comes from environment irradiance + radiance, and only irradiance is impacted by the albedo color. So, if it’s 0, your model won’t have the right color.

You can re-add the code that computes irradiance this way (lines 394-398):

Alternatively, you can force irradiance to be computed in the fragment shader, but it’s more GPU intensive (line 176):

(@Heaust-ops beat me to it!)

2 Likes

Thank you both for your responses and solutions!

At first, it felt a bit counterintuitive to add more code, since I assumed that custom materials would work out of the box — we’re only adding an additional visual effect.
But I now understand that since we’re modifying the mesh’s shape, recalculations are indeed necessary.

Now, I have three different solutions, and I don’t see major differences between them.
So my question is: which one is the most optimized? (Aside from your last one, @Evgeni_Popov, as you’ve already pointed out it’s not.)

:hugs:

1 Like

well I didn’t test mine against edge cases nor gave it much thought, just cp pasted the relevant lines from the source.

I’d suggest go with Evgeni_Popov’s 1st solution it looks more complete : )

since I assumed that custom materials would work out of the box

they do, you commented that part out :sweat_smile:


the environment irradiance snippet already exists in this block

1 Like

Hmm okay. To be honest, I started this hair simulation from that project/playground without fully understanding everything behind it:

I now see the reason for those commented-out sections.
So, would there be a better way to run the hair simulation without having to comment out all the PBR-related code?
I tried removing it, but then it stopped working, which makes sense, since the shader is modifying all the vertex properties.

1 Like

ig you can do this,

but then you’re redoing those position and world calculations, which I mean who knows maybe the compiler is smart enough to optimize that for us.
just benchmark it and if you see no difference then that’s fiiiiiiiiiiiiine

otherwise the current way you’re doing things is good enough, just
console.log(stringMat.VertexShader);
and check if you’re commenting out smth important and add that back in if you are : P

1 Like

Ok I see, thanks again for all your explanations and help.
This was exactly what I was looking for, and in addition, I learned new stuff about shaders!
:saluting_face:

1 Like

lesgoooo : D

2 Likes

I think the compiler should be able to optimize the code, however the code that calculates irradiance will use the default normal, not the new one (not sure if the difference is visible, though).

2 Likes