I am making some custom terrain shaders with strumpy's editor and I want to be able to create normals based on my blend mask. Does anyone know how I can turn grayscale data into basic normal mapping info? I have seen someone do this in unreal but I can't remember where.
Replies
Off the top of my head... for CG/HLSL;
float heightmap = your height map value;
float3 normal;
normal.x = ddx(heightmap);
normal.y = ddy(heightmap);
normal.z = sqrt(1 - normal.x*normal.x - normal.y * normal.y); // Reconstruct z component to get a unit normal.
For OpenGL, I think the functions are dFdx and dFdy.
DirectX11 has ddx_fine and ddy_fine which I think give more accurate results.
That'll give you the normal in, ehr... screen space I think?
Otherwise you can sample the heightmap multiple times, offsetting it by a pixel and working out the difference from those values. That should get you it in whatever space the base normal is in.
Nicked frm here; http://www.gamedev.net/topic/594781-hlsl-height-map-to-normal-algorithm/
Here's the multi-sample method in a stripped-down surface shader.
I had to change the handedness from the code above to match Unity (changing the sign of the operation when sampling the e and w values from + to - and vice versa).
Using the base normal map and the height map I had used to generate a detail normal from.
ddx_fine might give better results in DX11, but it looks like crap in DX9.