给定一个网格(如左侧的立方体对象)和另一个自定义的球状网格(右侧;如果更容易的话,它可以是另一种形状),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<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();
}
}
然后可以通过简化函数进一步简化生成的网格该资产 https://assetstore.unity.com/packages/tools/modeling/krabl-mesh-processors-12079.
上述方法的替代方法是使用 Collider.ClosestPoint(vertexPoint)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)