点击在plane上消除该mesh
using System.Collections;using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Cutting hole in AM esh. made by u3dchina.com
/// </summary>
public class CuttingHoleInAMesh : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f))
{
deleteSquare(hit);
Debug.Log("triangleIndex:" + hit.triangleIndex);
}
}
}
/// <summary>
/// Finds the vertex.
/// </summary>
/// <returns>The vertex.</returns>
/// <param name="v">V.</param>
int findVertexIdx(Vector3 v)
{
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
if (vertices == v)
{
return i;
}
}
return -1;
}
/// <summary>
/// Deletes the square.
/// </summary>
/// <param name="index1">Index1.delete triangle idx1</param>
/// <param name="index2">Index2.delete triangle idx2</param>
void deleteSquare(int index1, int index2)
{
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
int[] oldTriangles = mesh.triangles;
int[] newTriangles = new int;
int i = 0;
int j = 0;
while (j < mesh.triangles.Length)
{
if (j != index1 * 3 && j != index2 * 3)
{
newTriangles = oldTriangles;
newTriangles = oldTriangles;
newTriangles = oldTriangles;
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
Debug.Log("newTriangles len:" + newTriangles.Length);
this.gameObject.AddComponent<MeshCollider>();
}
/// <summary>
/// Finds the triangle.
/// </summary>
/// <returns>The triangle.</returns>
/// <param name="v1">V1.</param>
/// <param name="v2">V2.</param>
/// <param name="notTriIndex">Not tri index.</param>
int findTriangle(Vector3 v1, Vector3 v2, int notTriIndex)
{
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
int j = 0;
while (j < triangles.Length)
{
if (j / 3 != notTriIndex)
{
if (vertices] == v1 && (vertices] == v2 || vertices] == v2))
{
return j / 3;
}
else if (vertices] == v2 && (vertices] == v1 || vertices] == v1))
{
return j / 3;
}
else if (vertices] == v2 && (vertices] == v1 || vertices] == v1))
{
return j / 3;
}
else if (vertices] == v1 && (vertices] == v2 || vertices] == v2))
{
return j / 3;
}
}
j += 3;
}
return -1;
}
/// <summary>
/// Deletes the square.
/// </summary>
/// <param name="hit">Hit.</param>
void deleteSquare(RaycastHit hit)
{
int hitTriIdx = hit.triangleIndex;
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int[] oldTriangles = mesh.triangles;
int i = 0;
while (i < oldTriangles.Length)
{
Debug.Log("oldTriangles i:" + oldTriangles);
i++;
}
i = 0;
while (i < vertices.Length)
{
Debug.Log("vertices i:" + vertices);
i++;
}
Vector3 p0 = vertices];
Vector3 p1 = vertices];
Vector3 p2 = vertices];
float edge1 = Vector3.Distance(p0, p1);
float edge2 = Vector3.Distance(p0, p2);
float edge3 = Vector3.Distance(p1, p2);
//find adjacent edge of triangle
Vector3 shared1;
Vector3 shared2;
if (edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if (edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertexIdx(shared1);
int v2 = findVertexIdx(shared2);
deleteSquare(hitTriIdx, findTriangle(vertices, vertices, hitTriIdx));
}
/// <summary>
/// Deletes the tri.
/// </summary>
/// <param name="hit">Hit.</param>
void deleteTri(RaycastHit hit)
{
int index = hit.triangleIndex;
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int[] oldTriangles = mesh.triangles;
Vector3 p0 = vertices];
Vector3 p1 = vertices];
Vector3 p2 = vertices];
float edge1 = Vector3.Distance(p0, p1);
float edge2 = Vector3.Distance(p0, p2);
float edge3 = Vector3.Distance(p1, p2);
Debug.Log("vertices1:" + p0);
Debug.Log("vertices2:" + p1);
Debug.Log("vertices3:" + p2);
Debug.Log("edge1:" + edge1);
Debug.Log("edge2:" + edge2);
Debug.Log("edge3:" + edge3);
int[] newTriangles = new int;
int i = 0, j = 0;
while (j < mesh.triangles.Length)
{
if (j != index * 3)
{
newTriangles = oldTriangles;
newTriangles = oldTriangles;
newTriangles = oldTriangles;
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
}
你真的叼,逆天就是指的你 很不错 楼主是超人 好帖就是要顶 顶顶多好 LZ真是人才
不错 不错 不错 楼主是超人 难得一见的好帖
页:
[1]