Unity:在其他网格周围轻柔地缠绕网格?

给定一个网格(如左边的立方体对象)和另一个自定义球体网格(右边;如果更容易,它可能是另一种形状),Unity&C#中的一个如何在运行时轻柔地将第二个网格包裹在第一个网格周围? 谢谢!

在此处输入图像描述

以下方法,感谢指针的VirtualMethodStudio,采用包装球,然后为其中的每个顶点向内投射光线,并将该顶点调整到生命点:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShrinkWrapSphere : MonoBehaviour { void Start() { Debug.Log("Starting..."); MeshFilter meshFilter = gameObject.GetComponent(); Mesh mesh = meshFilter.mesh; Vector3[] vertices = new Vector3[mesh.vertices.Length]; System.Array.Copy(mesh.vertices, vertices, vertices.Length); for (int i = 0; i < vertices.Length; i++) { Vector3 rayDirection = -mesh.normals[i]; RaycastHit hit; if ( Physics.Raycast( vertices[i], rayDirection, out hit, 100f ) ) { vertices[i] = hit.point * 2f; } else { vertices[i] = Vector3.zero; } } mesh.vertices = vertices; Debug.Log("Done. Vertices count " + vertices.Length); // mesh.RecalculateBounds(); // mesh.RecalculateNormals(); // mesh.RecalculateTangents(); } } 

在此处输入图像描述

然后可以通过该资产的简化function进一步简化所得到的网格。

上面的替代方法是使用Collider.ClosestPoint(vertexPoint)。