コード例 #1
0
    public void Contract(b_Plane bisectPlane, b_Plane bisectPlane2, float timeToContract = -1.0f)
    {
        ResetLineLookupTable();

        timeToContract *= timerMultiplier;

        // Transform the planes to local space for the mesh
        b_Plane bisectPlaneLocal;

        bisectPlaneLocal.location = this.transform.InverseTransformPoint(bisectPlane.location);
        bisectPlaneLocal.normal   = this.transform.InverseTransformDirection(bisectPlane.normal);
        b_Plane bisectPlaneLocal2;

        bisectPlaneLocal2.location = this.transform.InverseTransformPoint(bisectPlane2.location);
        bisectPlaneLocal2.normal   = this.transform.InverseTransformDirection(bisectPlane2.normal);

        // Transform translation to local space
        Vector3 translation = this.transform.InverseTransformPoint(bisectPlane2.location) - bisectPlaneLocal.location;

        if (AxisAlignedBisection)
        {
            Vector3 dif = new Vector3(translation.x, translation.y, translation.z);
            dif.x = Mathf.Abs(dif.x);
            dif.y = Mathf.Abs(dif.y);
            dif.z = Mathf.Abs(dif.z);

            // X axis aligned
            if (dif.x > dif.y && dif.x > dif.z)
            {
                translation.y = 0;
                translation.z = 0;

                bisectPlaneLocal.normal.y  = 0;
                bisectPlaneLocal.normal.z  = 0;
                bisectPlaneLocal2.normal.y = 0;
                bisectPlaneLocal2.normal.z = 0;

                bisectPlaneLocal2.location = bisectPlaneLocal.location + translation;
            }
            // Y axis aligned
            else if (dif.y > dif.z)
            {
                translation.x = 0;
                translation.z = 0;

                bisectPlaneLocal.normal.x  = 0;
                bisectPlaneLocal.normal.z  = 0;
                bisectPlaneLocal2.normal.x = 0;
                bisectPlaneLocal2.normal.z = 0;

                bisectPlaneLocal2.location = bisectPlaneLocal.location + translation;
            }
            // Z axis aligned
            else
            {
                translation.x = 0;
                translation.y = 0;

                bisectPlaneLocal.normal.x  = 0;
                bisectPlaneLocal.normal.y  = 0;
                bisectPlaneLocal2.normal.x = 0;
                bisectPlaneLocal2.normal.y = 0;

                bisectPlaneLocal2.location = bisectPlaneLocal.location + translation;
            }
        }

        bisectPlaneLocal.uPlane  = new Plane(bisectPlaneLocal.normal, bisectPlaneLocal.location);
        bisectPlaneLocal2.uPlane = new Plane(bisectPlaneLocal2.normal, bisectPlaneLocal2.location);

        Vector3 translationSide  = bisectPlaneLocal.location + translation.normalized;
        Vector3 translationSide2 = bisectPlaneLocal2.location - translation.normalized;

        int triangleLen = triangles.ActiveCount;
        int verticesLen = vertices.ActiveCount;

        HashSet <ActiveNode <bVertex> > verticesToBeTranslated  = new HashSet <ActiveNode <bVertex> >();
        HashSet <ActiveNode <bVertex> > verticesToBeTranslated2 = new HashSet <ActiveNode <bVertex> >();
        List <ActiveNode <bTriangle> >  trianglesInBetween      = new List <ActiveNode <bTriangle> >();
        List <ActiveNode <bTriangle> >  trianglesNotTranslated  = new List <ActiveNode <bTriangle> >();

        ActiveNode <bTriangle> it = triangles.GetRootNode().nextActiveNode;

        for (int i = 0; i < triangleLen; i++)
        {
            bTriangleBisection(bisectPlaneLocal, translationSide, it, verticesToBeTranslated, trianglesNotTranslated);
            foreach (ActiveNode <bTriangle> node in trianglesNotTranslated)
            {
                bTriangleBisection(bisectPlaneLocal2, translationSide2, node, verticesToBeTranslated2, trianglesInBetween, true);
            }

            trianglesNotTranslated.Clear();
            it = it.nextActiveNode;
        }

        verticesToBeTranslated.Clear();

        if (timeToContract <= 0.0f)
        {
            int x = trianglesInBetween.Count;
            for (int i = 0; i < x; i++)
            {
                ActiveNode <bTriangle> node = trianglesInBetween[i];
                ChangebTriangle(node, null, true);
                //triangles.SetActivity(node, false);
            }
            foreach (ActiveNode <bVertex> node in verticesToBeTranslated2)
            {
                bVertex newVert = node.data.GetCopy();
                newVert.vertex -= translation;
                ChangeVertex(node, newVert, node.data);
                //node.data -= translation;
            }

            RegenerateMesh();
            history.PushChanges();
        }
        else
        {
            RegenerateMesh();
            StartCoroutine(ContractTransition(timeToContract, verticesToBeTranslated2, trianglesInBetween, -translation));
        }

        // Propagate through children
        foreach (bMesh mesh in this.GetComponentsInChildren <bMesh>())
        {
            if (mesh != this)
            {
                mesh.Contract(bisectPlane, bisectPlane2, timeToContract);
            }
        }
    }