"Reprint" Summary of vertex mesh deformation control based on average normal

Source: Internet
Author: User

Original link: http://blog.csdn.net/haohan_meng/article/details/22640991

The warp feature of the vertex mesh, which has a case in the technology showcase demo in the Unity official store. Oneself is in this case the foundation carries on the study. The function of the case is all with JS realization, own leaf out, changed to C #, and some of the core code of the working principle of the analysis and study, finally formed this summary. Some of the shortcomings are welcome to be criticized.

See the title of the article, we may think, to complete the mesh model deformation, what are the required elements? In a nutshell, there are two: vertices and normals. Where vertices refer to the coordinate position of each vertex of the mesh model (the model's own coordinate system); normals refer to the normals of each vertex (the unit vector that represents the vertex direction). They all belong to the category of the vertex attribute of the mesh model. With the basic elements of these two variants, we can begin to transform the model. Before beginning to realize, let us to the model deformation of basic routines have a general understanding, this good let us retrace, targeted.

When we are morphing the mesh model, we usually do it in the following steps:

1. Select the trigger condition. The so-called trigger condition refers to the precondition of triggering the deformation of mesh model, which is commonly used for collision between objects and mouse clicks.

2, the vertex filter. We need to filter the vertices after the triggering conditions trigger the deformation. Vertex filtering refers to filtering all the vertices of a mesh model by setting certain conditions to get all the vertices we need to modify. This is a very important step, the core of this step is the vertex filtering algorithm, the algorithm to be based on the different filter criteria to determine.

3, calculate the change factor. After the vertex filtering, we already know which vertices need to be modified. The so-called change factor, is to tell us, how these vertices to change the position, how to modify. For the vertices that need to be modified, select the appropriate algorithm to calculate their respective change factors. This is also a one-step core operation.

4, complete the vertex deformation, so that the changes take effect. In this step, we need to apply the calculated change factor to the filtered vertices, and make these changes effective, so as to achieve the deformation effect of the vertex mesh.

Next, let us according to the above summary of the 4 steps, combined with code, to achieve the specific vertex mesh deformation function.

In this example, we use a mouse click to make the model deform (concave or convex) in a way. Therefore, the trigger condition in the first article is the mouse click.

First, in the update function to detect whether the left mouse button is pressed, if not pressed, then directly back out, no subsequent processing, otherwise, continue to execute backwards.

Collision detection is performed by emitting a ray from the position of the mouse. If there is a collision, record the relevant collision information (fill in the raycasthit structure) and get the mesh filter of the mesh model encountered. It then calculates the position of the collision point in the model's own coordinate space. In this case, our trigger condition section can be counted as complete. Before entering the vertex filter, there is another crucial concept that is not introduced-what is the average normal method. This part of the code:

So what is the average normal? The normals of the vertices, which describe the direction of each vertex. The deformation of our vertex meshes is essentially a change in the position of the vertices. So how does the vertex position change, which requires a direction to describe the change in position. The average normals we ask for represent a common direction of change for all vertices that change position. With this average normal, we know which direction the vertex is going to move, and how much to move, which will be explained later.

In the first reading of this section of the code to find the average normal, the mind has always been a doubt, that is why the average normal can be approximated to represent the direction of the vertex change? The answer is that this is a simulation of reality. Read a paper on the internet on the automatic generation of automotive panels based on the average normal method of stamping direction of the summary, which describes the surface force on the object, based on the average normal to automatically obtain the direction of the press (force direction, that is, the deformation direction of the model) method. Just see here, just let me to mean normal meaning enlightened, originally this is completely the simulation to the reality situation. So how do we get the average normals, which allows us to go into the second part, vertex filtering, to parse the Deformmesh function in the code.

Vertex filtering, we use the method of calculating the distance from each vertex to the collision point. Set a deformation radius in advance, the vertex within this radius is the point that we want to modify, and vice versa. The distance here is compared to the way we use squares to reduce overhead. When we have obtained all the vertices that need to be modified, the next is to calculate their average normals, that is, to determine the direction of the deformation. The average normal is calculated by using the normals of each vertex to obtain the vector and the way, and with the weighted average, the weight factor here is the vertex to the collision point of the distance, because the closer to the collision point, the greater the impact, then the final result of the average normal is greater. In the process of calculating the weighting factor by distance, a linear calculation is used, as shown in the Linearfalloff function in the code. Well, finally, we standardize the calculated average normals, because we only need its direction, and standardization will help our calculations. Code:

Next, with each vertex position changing direction, we also ask for the above mentioned distance, that is, in the direction of the average normal to move how much, that is, to find the third step of the change factor. The change factor of each vertex is essentially a weight, because the closer the vertex is to the collision point, the greater the deformation distance is, the greater the weight. The weights here, we use the Gaussian fuzzy way, through the Gaussian distribution of the two-dimensional density function, to calculate the probability of each vertex size, and this probability as a weight to be used. However, there is a problem here, it is not solved, that is, the code of the Gaussian calculation formula, the Gaussian distribution of the density function is converted, because it is not the standard notation of the density function. In addition, for Gauss Fuzzy algorithm, oneself also prepares to tidy up a summary, its use is really too big. Code:

Finally, we just assign the modified vertex to the mesh vertex array, and then perform the fourth step of the use modification to take effect, complete the deformation effect.

Summary write some hasty, the wrong place very welcome everybody's criticism, to oneself has not understood the question, also must continue to explore. Look out of the window, or haze, this early morning, is a new day:)

"Reprint" Summary of vertex mesh deformation control based on average normal

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.