コード例 #1
0
    private void OnCollisionEnter(Collision collision)
    {
        startPos = collision.contacts[0].point;

        cutMesh = collision.gameObject.GetComponent <CutMesh>();

        Debug.Log("HIT");
    }
コード例 #2
0
    private void OnCollisionEnter(Collision collision)
    {
        startPos = collision.contacts[0].point;

        cutMesh = collision.gameObject.GetComponent <CutMesh>();

        if (cutMesh != null)
        {
            if (speed >= 0.5)
            {
                cutMesh.Create();
            }

            Debug.Log("HIT");
        }
    }
コード例 #3
0
        public void MakeGameobject(CutMesh original)
        {
            GameObject = new GameObject(original.name);
            GameObject.transform.position   = original.transform.position;
            GameObject.transform.rotation   = original.transform.rotation;
            GameObject.transform.localScale = original.transform.localScale;

            var mesh = new Mesh();

            mesh.name = original.GetComponent <MeshFilter>().mesh.name;

            mesh.vertices = Vertices;
            mesh.normals  = Normals;
            mesh.uv       = UV;
            for (var i = 0; i < Triangles.Length; i++)
            {
                mesh.SetTriangles(Triangles[i], i, true);
            }
            Bounds = mesh.bounds;

            var renderer = GameObject.AddComponent <MeshRenderer>();

            renderer.materials = original.GetComponent <MeshRenderer>().materials;

            var filter = GameObject.AddComponent <MeshFilter>();

            filter.mesh = mesh;

            var collider = GameObject.AddComponent <MeshCollider>();

            collider.convex = true;

            var rigidbody   = GameObject.AddComponent <Rigidbody>();
            var meshDestroy = GameObject.AddComponent <CutMesh>();

            meshDestroy.CutCascades  = original.CutCascades;
            meshDestroy.ExplodeForce = original.ExplodeForce;
        }
コード例 #4
0
    private Vector3 _pos2; // planeとmeshの交点その2

    public void Cut(Plane plane, CutMesh _cutMesh)
    {
        var group1ObjPosList = new List <Vector3>();
        var group1ObjTriList = new List <int>();

        var group2ObjPosList = new List <Vector3>();
        var group2ObjTriList = new List <int>();

        // 色々必要になってしまった
        var meshTriangles = _cutMesh.Mesh.triangles;
        var meshVertices  = _cutMesh.Mesh.vertices;
        var meshNormals   = _cutMesh.Mesh.normals;
        var meshPos       = _cutMesh.transform.position;
        var meshScale     = _cutMesh.transform.localScale;

        for (var i = 0; i < meshTriangles.Length; i += 3)
        {
            var group1PosList = new List <Vector3>();
            var group1TriList = new List <int>();
            var group2PosList = new List <Vector3>();
            var group2TriList = new List <int>();

            var idx0 = meshTriangles[i];
            var idx1 = meshTriangles[i + 1];
            var idx2 = meshTriangles[i + 2];

            var verts = new List <Vector3>();

            // 頂点位置をscaleやpositionに合わせてしっかり計算しないとおかしくなる
            // あれ、もうmatrixで計算したほうがいい?
            var v1 = Vector3.Scale(meshVertices[idx0], meshScale) + meshPos;
            var v2 = Vector3.Scale(meshVertices[idx1], meshScale) + meshPos;
            var v3 = Vector3.Scale(meshVertices[idx2], meshScale) + meshPos;

            verts.Add(v1);
            verts.Add(v2);
            verts.Add(v3);

            // そのポリゴンの法線を計算しておく
            var normal = Vector3.Cross(meshVertices[idx2] - meshVertices[idx0], meshVertices[idx1] - meshVertices[idx0]);


            CheckPlaneSide(plane, verts, group1PosList, group2PosList); // 1.グループ分け

            if (group1PosList.Count > 0 && group2PosList.Count > 0)
            {
                CalcCrossPoint(plane, group1PosList, group2PosList); // 2.planeとの交点を求める

                // 3.両方のグループともに交点を入れる
                group1PosList.Add(_pos1);
                group1PosList.Add(_pos2);

                group2PosList.Add(_pos1);
                group2PosList.Add(_pos2);
            }

            if (group1PosList.Count > 0)
            {
                var tris1  = CreateTriangles(group1PosList, normal);
                var triIdx = group1ObjPosList.Count;

                group1ObjPosList.AddRange(group1PosList);

                // 二つめ以降ならidxがずれることに注意
                foreach (var triI in tris1)
                {
                    group1ObjTriList.Add(triI + triIdx);
                }
            }

            if (group2PosList.Count > 0)
            {
                var tris2  = CreateTriangles(group2PosList, normal);
                var triIdx = group2ObjPosList.Count;

                group2ObjPosList.AddRange(group2PosList);

                // 二つめ以降ならidxがずれることに注意
                foreach (var triI in tris2)
                {
                    group2ObjTriList.Add(triI + triIdx);
                }
            }
        }

        // 4.2つのグループに分けたオブジェクトを作成する
        CreateCutObj(group1ObjPosList, group1ObjTriList);
        CreateCutObj(group2ObjPosList, group2ObjTriList);



        _cutMesh.gameObject.SetActive(false); // 5.元となるオブジェクトを非表示にする
    }