Exemple #1
0
    private int PlanebTriangleIntersection(b_Plane plane, bTriangle triangle,
                                           out ActiveNode <bVertex>[] intersection,
                                           bool duplicateIntersectionPoints = false,
                                           bool useSecondLookupTable        = false)
    {
        intersection    = new ActiveNode <bVertex> [3];
        intersection[0] = null; intersection[1] = null; intersection[2] = null;
        int nullCount = 0;

        Vector3 vec;
        float   interp;

        for (int i = 0; i < 3; i++)
        {
            int j = (i + 1) % 3;

            int vi = triangle.GetVertexIndex(i);
            int vj = triangle.GetVertexIndex(j);

            if (LineIntersectLookup(vi, vj, !useSecondLookupTable) == lineLookupBlank)
            {
                nullCount++;
                continue;
            }
            else if (LineIntersectLookup(vi, vj, !useSecondLookupTable) != null)
            {
                intersection[i] = LineIntersectLookup(vi, vj, !useSecondLookupTable);
            }
            else if (PlaneSegmentIntersection(plane, triangle.GetVertex(i), triangle.GetVertex(j), out vec, out interp) == 1)
            {
                bVertex newVert = new bVertex(
                    new Vector3(vec.x, vec.y, vec.z),
                    Vector2.Lerp(triangle.GetUV(i), triangle.GetUV(j), interp)
                    );

                intersection[i] = AddVertex(newVert, true);
                SetLineIntersect(vi, vj, intersection[i], !useSecondLookupTable);

                if (duplicateIntersectionPoints)
                {
                    AddVertex(new bVertex(
                                  new Vector3(vec.x, vec.y, vec.z),
                                  Vector2.Lerp(triangle.GetUV(i), triangle.GetUV(j), interp)),
                              true);
                }
            }
            else
            {
                SetLineIntersect(vi, vj, lineLookupBlank, !useSecondLookupTable);
                nullCount++;
            }
        }

        if (nullCount > 1)
        {
            return(0);
        }

        return(1);
    }
Exemple #2
0
    /*
     *  Helper Functions
     */

    private ActiveNode <bVertex> AddVertex(bVertex vertex, bool active = true)
    {
        ActiveNode <bVertex> ret = vertices.Add(vertex, active);

        history.AddChange(ret, ret.data, false, true);

        return(ret);
    }
Exemple #3
0
    public bVertex GetCopy()
    {
        bVertex newVert = new bVertex();

        newVert.vertex = new Vector3(vertex.x, vertex.y, vertex.z);
        newVert.uv     = new Vector2(uv.x, uv.y);

        return(newVert);
    }
Exemple #4
0
    IEnumerator ContractTransition(float numSeconds, HashSet <ActiveNode <bVertex> > verticesToBeTranslated, List <ActiveNode <bTriangle> > trianglesToRemove, Vector3 translation)
    {
        float endTime     = Time.realtimeSinceStartup + numSeconds;
        float currentTime = Time.realtimeSinceStartup;

        List <Vector3> originalVertices = new List <Vector3>();

        foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
        {
            originalVertices.Add(new Vector3(node.data.vertex.x, node.data.vertex.y, node.data.vertex.z));
        }

        while (currentTime < endTime)
        {
            float t  = 1.0f - ((endTime - currentTime) / numSeconds);
            int   _i = 0;
            foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
            {
                node.data.vertex = Vector3.Lerp(originalVertices[_i], originalVertices[_i] + translation, t);
                _i++;
            }

            RegenerateMesh();

            currentTime = Time.realtimeSinceStartup;
            yield return(new WaitForEndOfFrame());
        }

        int i = 0;

        foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
        {
            bVertex newVert = node.data.GetCopy();
            newVert.vertex   = originalVertices[i] + translation;
            node.data.vertex = originalVertices[i];

            ChangeVertex(node, newVert, node.data);
            //node.data = originalVertices[i] + translation;
            i++;
        }
        int x = trianglesToRemove.Count;

        for (int j = 0; j < x; j++)
        {
            ActiveNode <bTriangle> node = trianglesToRemove[j];
            ChangebTriangle(node, null, true);
            //triangles.SetActivity(node, false);
        }

        RegenerateMesh();
        history.PushChanges();
    }
Exemple #5
0
    private void ChangeVertex(ActiveNode <bVertex> node, bVertex newVal, bVertex oldVal, bool activityToggle = false)
    {
        history.AddChange(node, oldVal, activityToggle);

        if (newVal != null)
        {
            node.data = newVal;
        }

        if (activityToggle)
        {
            vertices.SetActivity(node, !node.IsActive());
        }
    }
Exemple #6
0
    public void AddChange(ActiveNode <bVertex> node, bVertex oldData, bool activityToggle, bool newNode = false)
    {
        if (MaxCount <= 0)
        {
            return;
        }

        Vertex_Change v;

        v.node           = node;
        v.oldData        = oldData.GetCopy();
        v.activityToggle = activityToggle;
        v.newNode        = newNode;

        runningVertexChanges.Add(v);
    }
Exemple #7
0
    IEnumerator ExpandTransition(float numSeconds, HashSet <ActiveNode <bVertex> > verticesToBeTranslated, Vector3 translation)
    {
        float endTime     = Time.realtimeSinceStartup + numSeconds;
        float currentTime = Time.realtimeSinceStartup;

        List <Vector3> originalVertices = new List <Vector3>();

        foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
        {
            originalVertices.Add(new Vector3(node.data.vertex.x, node.data.vertex.y, node.data.vertex.z));
        }

        while (currentTime < endTime)
        {
            float t  = 1.0f - ((endTime - currentTime) / numSeconds);
            int   _i = 0;
            foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
            {
                node.data.vertex = Vector3.Lerp(originalVertices[_i], originalVertices[_i] + translation, t);
                _i++;
            }

            RegenerateMesh();

            currentTime = Time.realtimeSinceStartup;
            yield return(new WaitForEndOfFrame());
        }

        int i = 0;

        foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
        {
            bVertex newVert = node.data.GetCopy();
            newVert.vertex   = originalVertices[i] + translation;
            node.data.vertex = originalVertices[i];

            ChangeVertex(node, newVert, node.data);
            //node.data = originalVertices[i] + translation;
            i++;
        }

        RegenerateMesh();
        history.PushChanges();
    }
Exemple #8
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);
            }
        }
    }
Exemple #9
0
    public void Expand(b_Plane bisectPlane, b_Plane bisectPlane2, float timeToExpand = -1.0f)
    {
        ResetLineLookupTable();

        timeToExpand *= 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);

        // 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;
            }
            // Y axis aligned
            else if (dif.y > dif.z)
            {
                translation.x = 0;
                translation.z = 0;

                bisectPlaneLocal.normal.x = 0;
                bisectPlaneLocal.normal.z = 0;
            }
            // Z axis aligned
            else
            {
                translation.x = 0;
                translation.y = 0;

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

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

        Vector3 translationSide = bisectPlaneLocal.location + translation;

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

        HashSet <ActiveNode <bVertex> > verticesToBeTranslated = new HashSet <ActiveNode <bVertex> >();

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

        for (int i = 0; i < triangleLen; i++)
        {
            bTriangleBisection(bisectPlaneLocal, translationSide, it, verticesToBeTranslated);
            it = it.nextActiveNode;
        }


        if (timeToExpand <= 0.0f)
        {
            foreach (ActiveNode <bVertex> node in verticesToBeTranslated)
            {
                bVertex newVert = node.data.GetCopy();
                newVert.vertex += translation;
                ChangeVertex(node, newVert, node.data);
                //node.data += translation;
            }
            RegenerateMesh();
            history.PushChanges();
        }
        else
        {
            RegenerateMesh();
            StartCoroutine(ExpandTransition(timeToExpand, verticesToBeTranslated, translation));
            RegenerateMesh();
        }

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