Solve the problem of Unity Vertex Shader rendering loss

In the past few days, the artist has given me a scene model that uses vertex animation. Because the vertex movement is relatively large, sometimes when the camera moves, you will find that the model is not rendered.

Analyzed it carefully


Find the reason is


Because the Bounds of the Renderer using this shader are not actually updated according to the shader's vertex animation changes,

As a result, the Bounds of this model have left the camera's view frustum and were eliminated.


The way to solve this problem is also relatively simple, which is to manually enlarge the Bounds corresponding to the mesh in the MeshFilter so that the movement of the vertex animation is always limited to the Bounds.


Remember to change the Bounds of the mesh variable in MeshFilter, not the Bounds in MeshRenderer.


code show as below


        var ren = this.GetComponent<MeshRenderer>();
        var mesh = this.GetComponent<MeshFilter>();
        float Height = ren.material.GetFloat("_Height"); //This is the attribute in the shader that controls the vertex displacement
        Bounds bound = mesh.mesh.bounds;
        Bounds newBounds = new Bounds(bound.center, new Vector3(bound.size.x, bound.size.y + Height * 2, bound.size.z));
        mesh.mesh. bounds = newBounds;

Guess you like

Origin blog.csdn.net/qq_28784217/article/details/78822232