3 dsmaxsdk export vertex tangent, binormal, normal method

Source: Internet
Author: User
Note: This method is slightly improved based on the method used by cryengine.

Take a look.

Final Effect

Display the TBN of the vertex (T, B, and n are displayed with the R, G, and B color axes respectively)

Normal texture used

The specific method is as follows:

1. Export the position point3 POs, texture coordinate point2 UV, and corresponding index value of all triangle vertices of the mesh. The export method is omitted. Note: The number of positions numpos and the number of texture coordinates numuv are usually not equal. The number of indexes is numfaces * 3. 2. Traverse each triangle surface of the mesh, and calculate TBN of each triangle surface using three POs and three Uvs of the triangle surface. The calculation method is as follows: Set the three vertices of a triangle to point3 POS [3], and their respective texture coordinates are point2 UV [3]. The two sides are point3 edgea = POS [1]-pos [0]; point3 edgeb = POS [2]-pos [0 ]; Algorithm Line point3 nor = fnormalize (edgea ^ edgeb); Calculate float deltau1 = uvw [1]. x-uvw [0]. x; float deltau2 = uvw [2]. x-uvw [0]. x; float deltav1 = uvw [1]. y-uvw [0]. y; float deltav2 = uvw [2]. y-uvw [0]. y; float DIV = (deltau1 * deltav2-deltau2 * deltav1); Calculate tan and bin Based on the DIV value, and use the Triangle Area as the weight. Point3 tan = point3 (1.0f, 0.0f, 0.0f); point3 bin = point3 (0.0f, 1.0f, 0.0f); If (Div! = 0.0) {float areaw = fabsf (DIV); // Triangle Area * 2 float a = deltav2/div; float B =-deltav1/div; float c =-deltau2/div; float d = deltau1/div; tag = fnormalize (edgea * A + edgeb * B) * areaw; bin = fnormalize (edgea * C + edgeb * D) * areaw;} 3. traverse each triangle in the mesh, and calculate the normal of each vertex using the N and POS of the three vertices and the smooth group information. the method is to group and add all the triangle faces that share the vertex Based on the smoothing group information (multiple groups may exist), and to solve the L-type problem, we need to multiply the weight by the area normal with the angle between the two edges of the vertex in the triangle. If only the normal line is exported, the process ends. Note that the number of vertices, numvernor, is likely to be greater than the number of positions, numpos. In other words, the vertices have been split Based on the smoothing group. 4. Traverse each triangle in the mesh to calculate the T and B of each vertex in the triangle. The calculation method is the same as that of the normal. (You need to determine whether to split the vertex Based on vertex T, B, and UV) vertbn. tan and vertbn. bin is initially point3 (0, 0), vertbn. nor is the vertex normal calculated in step 1. You still need to use the angle as the weight during calculation. Note that the number of T, B, and N obtained after this step is equal, and> = numvernor in step 1. 4.1 If a UV image exists, the method for splitting the vertex to determine whether the UV image exists is: The TBN hand-oriented bool faceparity = dotprod (facetbn. tan ^ facetbn. bin, facetbn. nor)> 0.0f; the hand direction bool verparity of the vertex TBN = dotprod (vertbn. tan ^ vertbn. bin, vertbn. nor)> 0.0f ;. If faceparity! = Verparity: If a UV image exists, the vertex must be split. 4.2 If the UV rotation angle is greater than 90, the splitting vertex determination method is point3 vrefuv = vertbn. tan + vertbn. bin; point3 vrothalf = vrefuv-facetbn. nor * dotprod (facetbn. nor, vrefuv); If dotprod (facetbn. tag + facetbn. BIN), vrothalf) <= 0.0f, the UV rotation angle is greater than 90 degrees, and the vertex needs to be split 5. the vertex TBN is processed by using the thinking method orthogonal, and the hand direction of TBN is recorded to T. w. Projected U and V to the plane where N is located, and then unitized to obtain the final vertbn. tan and vertbn. bin. multiply by vertbn. tan and vertbn. bin calculates the new newnor and becomes a unit. Check the angle between the new line and the normal calculated in step 2, that is, the hand direction of the final vertex TBN. If (dotprod (newnor, vertbn. Nor) <0.0f) {newnor * =-1.0f; vertbn. Tan. W =-1.0f;

}

 

The following two images are useful: UV and line chart.

UV graphs can be used to intuitively check whether the exported vertex TBN is correct. The check method is to check whether the T axis and B axis of the vertex are consistent with the arrow direction of U and V in the UV diagram respectively. If they are consistent, they are correct.

The line chart can check the UV image and UV rotation problems. The check method is to set the light to check whether the illumination effect is correct, that is, whether there are obvious seams.

 

Below are some model tests.

The final effect of a patch with a UV Image

Model vertices, faces, and UV Distribution

Vertex TBN (note that the T axis and B axis are in the same direction as U and V respectively)

 

Normal texture used

The final effect of the teapot in 3dmax

Teapot vertices TBN

Close-up at the top of the tea pot cover with UV Rotation

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.