Can't get normal inside custom fragment shader

How do you get the interpolated vertex normal inside the fragment shader?

This code shows how you can get the uv attribute passed to the fragment code (though it state “normal” it isn’t. it’s just the uv coordinates) - Putting Shader Code Into Babylon.js | Babylon.js Documentation

The same way can be used to pass the normal from the vertex shader

Some of the examples in the CYOS also use normals on the fragment shader Babylon.js - CYOS (babylonjs.com) :smiley:

That’s what I thought. So it just doesn’t work in the playground?

https://playground.babylonjs.com/#YXRI8H#21

You should pass your defines in with NORMAL being set to let the material know you are relying on them: https://playground.babylonjs.com/#YXRI8H#26

3 Likes

Aaah thank you :slight_smile:

Extension to this question - how do you get the forward vector of the view/camera?

I thought it was the third row of the view mat4, but that’s not giving me what I think it should.

You can pass it as uniform:

Hologram shader | Babylon.js Playground (babylonjs.com)

You can also run the same calculations as here - Babylon.js/ray.ts at master · BabylonJS/Babylon.js (github.com) on the shader, but i think the option to pass it to the shader is a viable one.

You can definitely get it from the various view spaces. I was just hoping to save a bit of effort. The way you did it is probably easier in the end. :slight_smile:

I have one last strange thing.

https://playground.babylonjs.com/#YXRI8H#65

It looks like the face normals suddenly flip when going past a threshold. So in one position the fresnel effect stops working, you move the camera and cross a threshold and it starts working, then you return to the broken position and it now works.

I saw this threshold normal-flipping behaviour before in babylon with something else. Is anyone able to explain it please?

Not sure where it is broken could you make a repro on a static model like a sphere or similar ? it is hard to see what is the broken part on this one.