|
资源信息 Tutorial Information
教程名称: | 点击在plane上消除该mesh(发帖教程) |
适用引擎: | Unity3D (适用引擎,为空默认为Unity) |
教程语种: | 中文 |
教程等级: | 1 |
教程格式: | 图文(请用IE9以上浏览器访问本版块) |
教程作者: | 转载自互联网 (如有问题请短消息联系作者或发表回复) |
下载地址: | 无 (兑换积分) |
[code=csharp]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[mesh.triangles.Length - 6];
int i = 0;
int j = 0;
while (j < mesh.triangles.Length)
{
if (j != index1 * 3 && j != index2 * 3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
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[triangles[j]] == v1 && (vertices[triangles[j + 1]] == v2 || vertices[triangles[j + 2]] == v2))
{
return j / 3;
}
else if (vertices[triangles[j]] == v2 && (vertices[triangles[j + 1]] == v1 || vertices[triangles[j + 2]] == v1))
{
return j / 3;
}
else if (vertices[triangles[j + 1]] == v2 && (vertices[triangles[j]] == v1 || vertices[triangles[j + 2]] == v1))
{
return j / 3;
}
else if (vertices[triangles[j + 1]] == v1 && (vertices[triangles[j]] == v2 || vertices[triangles[j + 2]] == 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[oldTriangles[hitTriIdx * 3 + 0]];
Vector3 p1 = vertices[oldTriangles[hitTriIdx * 3 + 1]];
Vector3 p2 = vertices[oldTriangles[hitTriIdx * 3 + 2]];
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[v1], vertices[v2], 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[oldTriangles[index * 3 + 0]];
Vector3 p1 = vertices[oldTriangles[index * 3 + 1]];
Vector3 p2 = vertices[oldTriangles[index * 3 + 2]];
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[mesh.triangles.Length - 3];
int i = 0, j = 0;
while (j < mesh.triangles.Length)
{
if (j != index * 3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
}
[/code]
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|