我有一个脚本将一个立方体分成四个相等的三角形棱柱,GameObject Panel是破碎的原始对象。每个棱镜都是通过脚本完成的,它通过将顶点和三角形分配给空白网格,原始对象被破坏。
// 2D Points which will connect to the center to form triangles
List<Vector2> shatterPoints = new List<Vector2> {new Vector2(-0.5f, -0.5f),
new Vector2(-0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, -0.5f)};
// Generate each fragment
for (int i = 0; i < shatterPoints.Count; i++) {
GameObject Fragment = new GameObject("Empty"); // Create game object
Fragment.AddComponent<MeshFilter>();
Fragment.AddComponent<MeshRenderer>();
Vector3 point1 = shatterPoints[i]; // Grab the two edge points for this fragment
Vector3 point2;
if (i + 1 == shatterPoints.Count) {
point2 = shatterPoints[0];
} else {
point2 = shatterPoints[i + 1];
}
Mesh FragMesh = new Mesh(); // Create the mesh
FragMesh.vertices = new Vector3[] {point1 + Vector3.forward * 0.5f,
point2 + Vector3.forward * 0.5f, new Vector3(0f, 0f, 0.5f),
new Vector3(0f, 0f, -0.5f), point2 - Vector3.forward * 0.5f,
point1 - Vector3.forward * 0.5f};
FragMesh.triangles = new int[] {1,0,2, 5,4,3, 0,5,3, 3,2,0, 4,5,0, 0,1,4, 1,2,3, 3,4,1};
// Copy important properties from the original piece to the fragment
Fragment.GetComponent<MeshFilter>().mesh = FragMesh;
Fragment.transform.position = Panel.transform.position;
Fragment.transform.rotation = Panel.transform.rotation;
Fragment.transform.localScale = Panel.transform.localScale;
Fragment.GetComponent<Renderer>().material = Panel.GetComponent<Renderer>().material;
FragMesh.RecalculateNormals();
// Add force here later (and a rigidbody while you're at it)
}
Destroy(Panel);
以下是粉碎前后立方体的图片:
Cube after shattering and manual displacement of fragments
虽然我重新计算了脚本中每个片段的法线,但是光照似乎不正确。我是程序网格生成的新手,所以任何人都能对此有所了解吗? (没有双关语)感谢你的时间。
答案 0 :(得分:0)
您看到的atrifacts是由于所有新创建的网格的平滑组相同。肯定的方法是为每张脸创造单独的verecies,而不是团结不会试图平滑边缘,