【发布时间】:2018-06-25 11:09:20
【问题描述】:
【问题讨论】:
【问题讨论】:
以下方法,感谢 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)。
【讨论】: