How to generate a normal map from the height Map

Source: Internet
Author: User
Both nvidia and ATI have tools to convert heightmap to normalmap. With normalmap, we can use normalmapping Technology for per pixel lighting computing. How is heightmap converted to normalmap?
In fact, it is not difficult. In 3D Game and computer graphics method, a method is provided to generate a forward graph from a height graph. The idea is to construct the S vector and T vector in the cut space based on the height difference between the pixels in the height graph and the surrounding pixels, and obtain the normal vector from sxt.
If H (I, j) is set to indicate the height value of the (I, j) pixel point on the height map, the tangent vectors in the S and T directions of the tangent space can be expressed:
S (I, j) = (, H (I + 1, J)-H (I-1, j ))
T (I, j) = (0, 1, h (I, j + 1)-H (I, J-1 ))
Normal (I, j) = S (I, j) x T (I, j)
H (I + 1, J)-H (I-1, j) is the height difference along the S direction, that is, the slope of the S direction, H (I, j + 1) -H (I, J-1) is the height difference along the T direction, that is, the T direction of the slope. When the height difference between adjacent pixels is 0, the calculated normal (I, j) = (, 1) is perpendicular to the plane. When there is a height difference, the normal is offset to S or t respectively.
It is also very easy to implement with shader. Vs and PS Code are as follows. The top left is heightmap, And the right is the normalmap generated by the shader below. The normalmap generated by this method is not good enough, there is a sample named normalmapfilter in rendermonkey, which will generate a higher quality normalmap. If you are interested, you can refer to it.
Vs_output main (float4 pos: Position ){
Vs_output out;
// Clean up inaccuracies
POs. XY = sign (Pos. XY );
Out. Pos = float4 (Pos. XY, 0, 1 );
// Image-space
Out. texcoord. x = 0.5*(1 + pos. X );
Out. texcoord. Y = 0.5 * (1-pos. y );
Return out;
}
Float4 main (float2 texcoord: texcoord): Color {
Float2 off = 1.0/heightmapsize;
Float scale = 1;
// Sample teh neighbor
Float S0 = tex2d (heightmap, texcoord + float2 (-off. X, 0). R;
Float S1 = tex2d (heightmap, texcoord + float2 (Off. X, 0). R;
Float S2 = tex2d (heightmap, texcoord + float2 (0,-off. Y). R;
Float S3 = tex2d (heightmap, texcoord + float2 (0, off. Y). R;
Float3 u = float3 (1, 0, S1-S0 );
Float3 v = float3 (0, 1, S3-S2 );
Float3 normal = normalize (scale * Cross (u, v ));
// Pack [-1, 1] into [0, 1]
Return float4 (normal * 0.5 + 0.5, 1 );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.