最佳答案
以下方法,感谢 VirtualMethodStudio 的指针,采用包装球体,然后为其中的每个顶点向内转换光线,并将该顶点调整到命中点:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShrinkWrapSphere : MonoBehaviour {
void Start() {
Debug.Log("Starting...");
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
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();
}
}
然后可以通过 this asset 的简化功能进一步简化生成的网格。 .
上面的替代方法是使用 Collider.ClosestPoint(vertexPoint)。
关于c# - 统一: Wrap mesh softly around other mesh?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022070/