Getting world normal in fragment shader

I was under wrong impression that ‘normal’ variable in mid of Lightning.frag shader represents world-space normal - but this seems to be not a case. Is there any easy way to get world-space normal in there? Even in vertex shader, it seems that only usable matrix goes immediately from model to view space.

Reason why I want to use it is because Skyrim has trivial but effective snow shader - it seems to be just take configurable max angle and covers everything less then that angle from up vector with snow. But up vector has to be world space…

http://hub.jmonkeyengine.org/javadoc/com/jme3/shader/UniformBinding.html

You will have to add it to your j3md file so that it’s set but the NormalMatrixInverse may give you what you want… though it may also take it straight to model space (likely).

Since you have to modify the shader anyway, your best bet might be to set a world space normal varying in the vertex shader and then just access that from the .frag. Probably with the world matrix and then remove the translation.

It occurs to me that another even cheaper way to do this would be to have an “up” uniform that is the up vector rotated into view space. Then you can just use the normals as they are without additional calculation and you only have to set that “up” uniform just once per frame.

1 Like

I have done a quick test by multiplying up vector with normal matrix in vert shader and it worked - as long as I was not using normal map, just vertex normals.

It seems that normal.xyz is in tangent space when I use normal maps… and I need it in view space using that approach;)

Seems that I will need to convert up-vector to tangent space in vertex shader instead to view space.
[java]
vUp = normalize(g_NormalMatrix *vec3(0,0,1))*tbnMat;
[/java]
seems to do the trick, not that I really understand all the implications :wink:

(I’m in z-up world, so it is 0,0,1, not 0,1,0; on top of that model is not rotated, so I get away with using model->view transform for now, instead of world->view one - will have to fix it)

[video]www.youtube.com/watch?v=FkMCwG7Dako&hd=1[/video]

Quick demo of dynamic level of snow. It will look better with real snow texture instead of just white color, but it is work in progress.

Looks nice. I plan to do something similar in Mythruna someday… though I will do a similar effect to my “dirt/noise” stuff and use a simple noise texture-based fractal to partially filter the snow to give it variation.

I’m going to do the same thing for moss, also. Someday.

Someday I may even write a post about texture-based fractal noise and combining it with existing textures. Neat effects.