0

我正在开发一个统一的实时销毁插件,但就网格操作而言,我遇到了一些问题。

我正在尝试做的事情:

通过移除 3d 网格的正面和背面从网格中切出一个孔。

示例图片

示例图片

我在做什么:

我正在删除部分原始网格(此时它只是一个三角形)克隆它,并将其放入另一个网格(只是背景信息)。

我的做什么/看起来像什么

我的做什么/看起来像什么

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class destroy : MonoBehaviour 
{

    public GameObject ttt;

    // Use this for initialization
    void Start () {

    }


    void deleteTri(int index)  // what is index? index is hit.triangleIndex the triangle that the mouse hit, this number is relative to the top rightmost triangle 
    {
        GameObject clone = Instantiate(this.gameObject);
        Destroy(clone.GetComponent<distroy>());
        Destroy(clone.GetComponent<MeshCollider>());


        Destroy(this.gameObject.GetComponent<MeshCollider>()); // removes prevouse render of shape 
         Mesh mesh = transform.GetComponent<MeshFilter>().mesh; // gets mesh (oop)


        int[] oldTriangles = mesh.triangles;  // current total number of triangles
        int[] oldTriangles2 = mesh.triangles;  

        int[] newTriangles = new int[mesh.triangles.Length - 3];
        int[] otherTriangles = new int[mesh.triangles.Length - 3];

        int i = 0;
        int j = 0;
        int j2 = 0;
        int i2 = 0;
        while (j < mesh.triangles.Length)  // makeing a new list of all the vector points in the mesh when it finds the mesh you have made it skips those three vector points and finishes the list this then removes that point from the mesh 
        {
            if(j != index*3)
            {
                newTriangles[i++] = oldTriangles[j++];
                newTriangles[i++] = oldTriangles[j++];
                newTriangles[i++] = oldTriangles[j++];
            }
            else
            {
                 j += 3;
            }


            // this is for making the second mesh and is an inverse of the original code 
            if (j2 == index * 3)
            {
                otherTriangles[i2++] = oldTriangles2[j2++];
                otherTriangles[i2++] = oldTriangles2[j2++];
                otherTriangles[i2++] = oldTriangles2[j2++];
            }
            else
            {
               j2 += 3;
            }

        }
        transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;  //says this is the new mesh 
        this.gameObject.AddComponent<MeshCollider>(); // renders the mesh 

        clone.transform.GetComponent<MeshFilter>().mesh.triangles = otherTriangles;  //says this is the new mesh 
        clone.gameObject.AddComponent<MeshCollider>(); // renders the mesh 




    }



    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Ray ray2 = Camera.main.ViewportPointToRay(ttt.transform.position);

            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                deleteTri(hit.triangleIndex);
               // print(hit.triangleIndex);
               // print(ray2.hit.triangleIndex);
                //print(ray.triangleIndex);
            }
        }
    }
}

我当前代码的问题在于它仅适用于平面网格而不适用于 3d 网格。这是因为光线投射在碰到某物时会停止,我需要程序检测两边的 triangleIndex,然后将其提供/返回给我。我迷路了请帮助!

4

0 回答 0