Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the decal system through Mesh projection

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to achieve the decal system through Mesh projection, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

In games such as FPS, if the gun hits the corner, you can't simply put a bullet hole patch. But like a sticker, it fits perfectly with the corner of the wall. At this point, it is necessary to implement a decal system to achieve this effect.

There are several different ways to realize the decal system, but here we only consider the implementation principle of the decal system through Mesh projection.

The essence of this approach is to find the Mesh affected by the decal resource in the field of vision, and create a Mesh with the same size textured by the decal resource to cover it, so as to achieve the purpose of decal. Mainly divided into the following two steps to achieve.

1. First find the objects that will be affected, such as sticking bullet holes at the angle between the two walls, then the affected objects are the two walls.

How to find the different requirements of the two walls may also be realized in different ways. To achieve the static embellishment effect through decals in the scene editor, you can create an AABB box of decal resources. If the bullet hole is created dynamically at run time, it can also be achieved by four X-ray tests, in short, there are many ways.

two。 First create a cube with a radius of 0.5 units. in the trimming coordinate system, the decal asset is placed in the yellow0 plane, and the center of the decal resource is the (0,0,0) point of the trimming coordinate system.

There is actually no code operation for this step, which is just a mathematical abstraction. Our goal is to project all the affected triangles onto the yellow0 plane so that the decal texture can be sampled correctly.

3. All the triangles of the affected object Mesh are converted to the coordinate system of the trimmed cube to trim the 8 planes of the cube.

Before trimming, there is a situation that needs to be dealt with, because the triangle has an orientation, which is determined by the face normal (the normal of the triangle in Unity is Cross (v2-v1, v3-v1)). When the normal cannot be shot into the eye in the normal rendering process, it will not be rendered. In the cone coordinate system of Unity, Vector3 (0,0,1) is forward, so the position of the eye is at Vector3 (0,0,1).

The same is true in this trimmed cube, it is impossible to project the texture to the back of a triangle plane, so we need to first judge whether the angle between the normal of the triangle and Vector3 (0,0,1) is less than 90 degrees, only less than 90 degrees can be projected and need to be trimmed.

It appears when trimming, the triangle is completely outside the cube, the triangle is completely inside the cube, and part of the triangle is outside the cube and part is inside the cube. The first two cases are easy to deal with, but the third case may cut a triple solution into two, so you need to pay special attention. The specific cutting algorithm is consistent with the cone cutting algorithm, so I won't repeat it here.

4. Texture sampling, when creating triangles, we need to specify a uv coordinate for each vertex. As mentioned earlier, our way of implementation is to project the trimmed triangles onto the yard0 plane of the trimmed coordinate system, and the projected coordinates are (x, 0, z). Therefore, uv can directly execute u = Lerp (0.0f, 1.0f, x + 0.5f) and v = Lerp (0.0f, 1.0f, z + 0.5f). The reason why 0.5f correction is added here is because the central coordinate of the cube is (0,0,0), which also means that the minimum value of x _ () y _ () Z is-0.5f.

I have said so much and attached the source code. It should be noted that I did not realize this source code, but modified it after I found it on the Internet. after all, I am not so familiar with Unity3d.

Ps. How does a trimmed cube with the same unit adapt to decals of different sizes? In Unity, you can do this by setting the Scale stretch axis, so 3D mathematics is amazing.

Pps. In the process of implementation, it is found that the newly created Mesh cannot be close to the covered Mesh, because at the same depth, the newly created Mesh cannot be guaranteed to render after the covered Mesh, which will likely cause the newly created Mesh and the covered Mesh to overwrite each other. Because after creating the Mesh, you need to float a little bit according to the plane normal to ensure that the Z-Buffer works properly.

The above content is how to implement the decal system through Mesh projection. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 226

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report