Beispiel #1
0
    void Start()
    {
        ////to get a nice and clear mesh without too many points close to each other
        //make a mesh which has a high autoweld threshold. Too high to be a visual mesh - it will leave gaps

        //work out what edges are on the outside
        MeshFilter meshFilter = GetComponent <MeshFilter>();
        Mesh       mesh       = meshFilter.mesh;

        autoWeldedMesh = new Mesh();
        autoWeldedMesh = mesh;

        autoWeldedMesh = AutoWeld.AutoWeldFunction(autoWeldedMesh, 1, 2000);

        //  meshFilter.mesh = autoWeldedMesh;
        //	outline = BuildManifoldEdges(meshFilter.mesh);

        //use the EdgeHelpers static class to work out the boundary could also be called in BushesForCell?
        boundaryPath = EdgeHelpers.GetEdges(autoWeldedMesh.triangles).FindBoundary().SortEdges();

        //use this new glued emsh to create a ring of edge points
        //fills pointsOnEdge
        CreateListOfEdgePoints(autoWeldedMesh);
        //PathOnEdge(boundaryPath);
    }
Beispiel #2
0
    //private Mesh m_Mesh;
    // Use this for initialization
    void Start()
    {
        enemyMaterials = GetComponent <MeshFilter>();

        Vector3[] vertices     = enemyMaterials.mesh.vertices;
        var       boundaryPath = EdgeHelpers.GetEdges(enemyMaterials.mesh.triangles).FindBoundary().SortEdges();

        Vector3[] yourVectors = new Vector3[boundaryPath.Count];
        for (int i = 0; i < boundaryPath.Count; i++)
        {
            yourVectors[i] = vertices[boundaryPath[i].v2];
            m_MeshVertices.Add(yourVectors[i]);
        }


        // create new colors array where the colors will be created.
        Color[] colors = new Color[m_MeshVertices.Count];
        Debug.Log(m_MeshVertices.Count);
        for (int i = 0; i < m_MeshVertices.Count; i++)
        {
            colors[i] = Color.Lerp(Color.white, Color.clear, m_MeshVertices[i].x);
            //colors[i] = Color.Lerp(Color.white, Color.clear, enemyMaterials.mesh.vertices[i].z);
        }
        //enemyMaterials.mesh.Clear();
        // assign the array of colors to the Mesh.
        enemyMaterials.mesh.colors = colors;
    }
Beispiel #3
0
    List <Vector3> OutsidePath(Mesh weldedMesh)
    {
        List <Vector3> borderPoints = new List <Vector3>();

        List <EdgeHelpers.Edge> boundaryPath = EdgeHelpers.GetEdges(weldedMesh.triangles).FindBoundary().SortEdges();

        Vector3[] vertices = weldedMesh.vertices;

        //now organise these edges in two one sequential vector3 list for mesh to build from
        for (int i = 1; i < boundaryPath.Count; i++)
        {
            borderPoints.Add(vertices[boundaryPath[i].v1]);

            //we need to add the 2nd point the last edge to complete the loop
            if (i == boundaryPath.Count - 1)
            {
                borderPoints.Add(vertices[boundaryPath[i].v2]);
            }

            /*
             *
             * GameObject c = GameObject.CreatePrimitive(PrimitiveType.Cube);
             * c.transform.position = vertices[boundaryPath[i].v1];
             * c.name = "1";
             * Destroy(c, 3);
             *
             * c = GameObject.CreatePrimitive(PrimitiveType.Cube);
             * c.transform.position = vertices[boundaryPath[i].v2];
             * Destroy(c, 3);
             * c.name = "2";
             */
        }

        return(borderPoints);
    }
Beispiel #4
0
    /// <summary>
    /// Returns a list of ints. The edge Vertice number of the mesh passed to it.
    /// </summary>
    /// <param name="mesh"></param>
    /// <returns></returns>
    public static List <int> EdgeVertices(Mesh mesh, float threshold)
    {
        //returns a list of ints. Vertices numbers of the outsie of a mesh. Does not "complete the loop"

        List <int> edgeVertices = new List <int>();
        //use the EdgeHelpers static class to work out the boundary could also be called in BushesForCell?
        List <EdgeHelpers.Edge> boundaryPath = EdgeHelpers.GetEdges(mesh.triangles).FindBoundary().SortEdges();

        //use temporary array
        Vector3[] vertices = mesh.vertices;

        //-1, boundary edges completes the loop for us. we don't want any duplicates
        for (int i = 0; i < boundaryPath.Count - 1; i++)
        {
            if (i == 0)
            {
                //add first point regardless

                edgeVertices.Add(boundaryPath[i].v1);
                Vector3 pos = vertices[edgeVertices[0]];
                //check for duplicate on second, add if not
                if (Vector3.Distance(pos, vertices[boundaryPath[i].v2]) >= threshold)
                {
                    edgeVertices.Add(boundaryPath[i].v2);
                }
            }
            else if (i > 0)
            {
                Vector3 pos       = vertices[boundaryPath[i].v2];
                Vector3 lastAdded = vertices[edgeVertices[edgeVertices.Count - 1]];
                //check for leftover duplicate left by autoweld
                //if we find one, we are done, we have made it back to the start of the loop
                if (pos == vertices[edgeVertices[0]])
                {
                    //we have found the first point. Don't add it
                    break;
                }

                //if this does not match the previous entry, add it
                //autowelding the mesh doesn not delete all triangels/vertices that arent used so boundary path can get confused

                //not autowelding at higher threshold- so this may not be necessary
                if (Vector3.Distance(pos, lastAdded) >= threshold)
                {
                    edgeVertices.Add(boundaryPath[i].v2);
                }

                // edgeVertices.Add(boundaryPath[i].v2);//anything using this now? was deform mesh stitch
            }
        }

        return(edgeVertices);
    }
        private static List <Vector3> GetVertices(Mesh mesh)
        {
            var            borders  = EdgeHelpers.GetEdges(mesh.triangles, mesh.vertices).FindBoundary();
            List <Vector3> vertices = new List <Vector3>();

            for (int i = 0; i < borders.Count; i++)
            {
                vertices.Add(borders[i].v1);
                vertices.Add(borders[i].v2);
            }
            vertices = RemoveDuplicate(vertices);

            return(vertices);
        }
Beispiel #6
0
    private void DrawOutline()
    {
        Vector3[]   vertices     = mesh.vertices;
        List <Edge> boundaryPath = EdgeHelpers.GetEdges(mesh.triangles).FindBoundary().SortEdges();

        for (int i = 0; i < boundaryPath.Count; i++)
        {
            Vector3 pos = vertices[boundaryPath[i].v1];
            linePoints.Add(pos);

            // do something with pos
        }
        // linePoints = new List<Vector3>(new HashSet<Vector3>(linePoints));
        linePoints = linePoints.Distinct().ToList();
        // StartCoroutine(activeLine.DrawLineOutline(linePoints));
    }
Beispiel #7
0
    static List <int> GetTris(ushort[] triangles, Vector2[] vertices)
    {
        List <int> result = new List <int>();

        // Near Face
        int num_triangles = triangles.Length;

        for (int i = 0; i < num_triangles; i++)
        {
            result.Add(triangles[i]);
        }

        // Far Face
        for (int i = 0; i < num_triangles; i += 3)
        {
            result.Add(triangles[i + 2] + vertices.Length);
            result.Add(triangles[i + 1] + vertices.Length);
            result.Add(triangles[i] + vertices.Length);
        }

        // Sides of mesh
        int[] tris = Array.ConvertAll(triangles, i => (int)i);
        List <EdgeHelpers.Edge> edges    = EdgeHelpers.GetEdges(tris);
        List <EdgeHelpers.Edge> boundary = EdgeHelpers.FindBoundary(edges).SortEdges();

        for (int i = 0; i < boundary.Count; i++)
        {
            EdgeHelpers.Edge edge = boundary[i];

            int vertex1 = edge.v1;                   // Near #1
            int vertex2 = edge.v2;                   // Near #2
            int vertex3 = edge.v1 + vertices.Length; // Far #1
            int vertex4 = edge.v2 + vertices.Length; // Far #2

            // Triangle 1
            result.Add(vertex1);
            result.Add(vertex3);
            result.Add(vertex2);

            // Triangle 2
            result.Add(vertex2);
            result.Add(vertex3);
            result.Add(vertex4);
        }

        return(result);
    }
Beispiel #8
0
    public List <EdgeHelpers.Edge> GetSideEdges(string side, MeshFilter highLOD)
    {
        List <EdgeHelpers.Edge> boundary    = EdgeHelpers.GetEdges(highLOD.sharedMesh.triangles).FindBoundary();
        List <EdgeHelpers.Edge> returnEdges = new List <EdgeHelpers.Edge>();

        for (int i = 0; i < boundary.Count; i++)
        {
            if (side == "right" && highLOD.sharedMesh.vertices[boundary[i].v1].x >= 0.95 && highLOD.sharedMesh.vertices[boundary[i].v2].x >= 0.95)
            {
                Debug.DrawLine(highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v1]), highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v2]), Color.red, 3);
                returnEdges.Add(boundary[i]);
                continue;
            }

            if (side == "left" && highLOD.sharedMesh.vertices[boundary[i].v1].x <= -0.95 && highLOD.sharedMesh.vertices[boundary[i].v2].x <= -0.95)
            {
                Debug.DrawLine(highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v1]), highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v2]), Color.blue, 3);
                returnEdges.Add(boundary[i]);
                continue;
            }

            if (side == "up" && highLOD.sharedMesh.vertices[boundary[i].v1].z >= 0.95 && highLOD.sharedMesh.vertices[boundary[i].v2].z >= 0.95)
            {
                Debug.DrawLine(highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v1]), highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v2]), Color.magenta, 3);
                returnEdges.Add(boundary[i]);
                continue;
            }
            if (side == "down" && highLOD.sharedMesh.vertices[boundary[i].v1].z <= -0.95 && highLOD.sharedMesh.vertices[boundary[i].v2].z <= -0.95)
            {
                Debug.DrawLine(highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v1]), highLOD.transform.TransformPoint(highLOD.sharedMesh.vertices[boundary[i].v2]), Color.yellow, 3);
                returnEdges.Add(boundary[i]);
                continue;
            }
        }

        return(returnEdges);
    }
    static void UpdatePolygonCollider2D(MeshFilter meshFilter)
    {
        if (meshFilter.sharedMesh == null)
        {
            Debug.LogWarning(meshFilter.gameObject.name + " has no Mesh set on its MeshFilter component!");
            return;
        }

        PolygonCollider2D polygonCollider2D = meshFilter.GetComponent <PolygonCollider2D>();

        polygonCollider2D.pathCount = 1;

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

        meshFilter.sharedMesh.GetVertices(vertices);

        var boundaryPath = EdgeHelpers.GetEdges(meshFilter.sharedMesh.triangles).FindBoundary().SortEdges();

        Vector3[] yourVectors = new Vector3[boundaryPath.Count];
        for (int i = 0; i < boundaryPath.Count; i++)
        {
            yourVectors[i] = vertices[boundaryPath[i].v1];
        }
        List <Vector2> newColliderVertices = new List <Vector2>();

        for (int i = 0; i < yourVectors.Length; i++)
        {
            newColliderVertices.Add(new Vector2(yourVectors[i].x, yourVectors[i].y));
        }

        Vector2[] newPoints = newColliderVertices.Distinct().ToArray();

        EditorUtility.SetDirty(polygonCollider2D);

        polygonCollider2D.SetPath(0, newPoints);
        Debug.Log(meshFilter.gameObject.name + " PolygonCollider2D updated.");
    }
    private void makePolygonCollider2D(Mesh mesh)
    {
        if (mesh == null)
        {
            return;
        }

        PolygonCollider2D polygonCollider2D = gameObject.GetComponent <PolygonCollider2D>();

        if (polygonCollider2D != null)
        {
            polygonCollider2D.pathCount = 1;

            List <Vector3> vertices = new List <Vector3>();
            mesh.GetVertices(vertices);

            var boundaryPath = EdgeHelpers.GetEdges(mesh.triangles).FindBoundary().SortEdges();

            Vector3[] yourVectors = new Vector3[boundaryPath.Count];
            for (int i = 0; i < boundaryPath.Count; i++)
            {
                yourVectors[i] = vertices[boundaryPath[i].v1];
            }

            List <Vector2> newColliderVertices = new List <Vector2>();

            for (int i = 0; i < yourVectors.Length; i++)
            {
                newColliderVertices.Add(new Vector2(yourVectors[i].x, yourVectors[i].y));
            }

            Vector2[] newPoints = newColliderVertices.Distinct().ToArray();


            polygonCollider2D.SetPath(0, newPoints);
        }
    }
Beispiel #11
0
        void Start()
        {
            if (type != TypePolygon.BOX_COLLIDER)
            {
                mesh = GetComponent <MeshFilter>().sharedMesh;
                Vector3[] vertices = mesh.vertices;
                List <EdgeHelpers.Edge> boundaryPath = EdgeHelpers.GetEdges(mesh.triangles).FindBoundary().SortEdges();
                Vector2 v = new Vector2();

                for (int i = 0; i < boundaryPath.Count; i++)
                {
                    Vector3 offset = transform.position;
                    Vector3 pos    = vertices[boundaryPath[i].v1] + offset;
                    pos = ExtensionMethods.RotatePointAroundPivot(pos, transform.position, transform.rotation);
                    v.x = (float)System.Math.Round(pos.x, 1);
                    v.y = (float)System.Math.Round(pos.z, 1);

                    if (!contour.Contains(v))
                    {
                        contour.Add(v);
                    }
                }

                if (type == TypePolygon.CONCAVE)
                {
                    ch      = new ConcaveHull();
                    contour = ch.CalculateConcaveHull(contour, 3);
                    contour.RemoveAt(contour.Count - 1);
                    contour.Reverse();
                }

                else if (type == TypePolygon.CONVEX)
                {
                    Vector2           position = new Vector2(transform.position.x, transform.position.z);
                    ClockwiseComparer c        = new ClockwiseComparer(position);
                    contour.Sort(c);
                }
            }

            else if (type == TypePolygon.BOX_COLLIDER)
            {
                BoxCollider[] boxColliders = GetComponentsInChildren <BoxCollider>();
                for (int i = 0; i < boxColliders.Length; i++)
                {
                    float minX = boxColliders[i].transform.position.x -
                                 boxColliders[i].size.x * boxColliders[i].transform.lossyScale.x * 0.5f;
                    float minZ = boxColliders[i].transform.position.z -
                                 boxColliders[i].size.z * boxColliders[i].transform.lossyScale.z * 0.5f;
                    float maxX = boxColliders[i].transform.position.x +
                                 boxColliders[i].size.x * boxColliders[i].transform.lossyScale.x * 0.5f;
                    float maxZ = boxColliders[i].transform.position.z +
                                 boxColliders[i].size.z * boxColliders[i].transform.lossyScale.z * 0.5f;


                    Vector3 pos = new Vector3(maxX, transform.position.y, maxZ);
                    pos = ExtensionMethods.RotatePointAroundPivot(pos, transform.position, transform.rotation);
                    contour.Add(new Vector2(pos.x, pos.z));

                    pos = new Vector3(minX, transform.position.y, maxZ);
                    pos = ExtensionMethods.RotatePointAroundPivot(pos, transform.position, transform.rotation);
                    contour.Add(new Vector2(pos.x, pos.z));

                    pos = new Vector3(minX, transform.position.y, minZ);
                    pos = ExtensionMethods.RotatePointAroundPivot(pos, transform.position, transform.rotation);
                    contour.Add(new Vector2(pos.x, pos.z));

                    pos = new Vector3(maxX, transform.position.y, minZ);
                    pos = ExtensionMethods.RotatePointAroundPivot(pos, transform.position, transform.rotation);
                    contour.Add(new Vector2(pos.x, pos.z));
                }
            }

            foreach (var position2d in contour)
            {
                Vector3 p = new Vector3(position2d.x, 0, position2d.y);
                Instantiate(prefab, p, Quaternion.identity, transform);
            }

            if (type != TypePolygon.NONE)
            {
                for (int i = 0; i < contour.Count - 1; i++)
                {
                    Engine.Instance.AddObstacle(new Vector2(contour[i].x, contour[i].y), new Vector2(contour[i + 1].x, contour[i + 1].y));
                }
            }

            else
            {
                Debug.Log("No has establecido el tipo para el poligono: " + name);
            }
        }
Beispiel #12
0
    //Import the obj model
    public IEnumerator importModel(ModelData modelData)
    {
        string modelPath = modelData.ModelUri;

        modelPlaced = false;
        modelBrowserWindow.SetActive(false);
        selectedModel = OBJLoader.LoadOBJFile(modelPath, modelShader, out offset);
        if (selectedModel.transform.childCount > 0)
        {
            //selectedModel.GetComponentsInChildren<Transform>()[1].Translate(-offset); //Looks in the parent as well
            pivotAdjuster.adjustPivot(selectedModel.GetComponentsInChildren <Transform>()[1]);
            selectedModel.GetComponentsInChildren <Transform>()[1].localPosition = Vector3.zero;
        }
        else
        {
            //selectedModel.transform.Translate(-offset);
            pivotAdjuster.adjustPivot(selectedModel.transform);
            selectedModel.transform.position = Vector3.zero;
        }

        //Calculating mesh size
        Renderer rend     = selectedModel.GetComponentsInChildren <Renderer>()[0];
        float    diameter = rend.bounds.extents.magnitude * 2;
        float    scale    = targetScale / diameter;                            //Calculates model scale to make it's diameter match the target scale (makes all model the same size)

        selectedModel.transform.localScale = new Vector3(scale, scale, scale); //x is negative because obj's flipped on x-axis apparently

        //Calculating orientation
        Mesh    modelMesh = selectedModel.GetComponentsInChildren <MeshFilter>()[0].mesh;
        Vector3 edgeSum   = Vector3.zero;

        Vector3[] vertices = modelMesh.vertices;
        List <EdgeHelpers.Edge> boundaryPath = EdgeHelpers.GetEdges(modelMesh.triangles).FindBoundary().SortEdges();

        for (int i = 0; i < boundaryPath.Count; i++)
        {
            Vector3 pos = vertices[boundaryPath[i].v1];
            edgeSum = edgeSum + pos;
        }
        Transform modelTransform = selectedModel.GetComponentsInChildren <Transform>()[0];
        Vector3   up             = edgeSum - modelTransform.position;

        Debug.DrawLine(modelTransform.position, up, Color.red, 1000f);
        modelTransform.rotation = Quaternion.FromToRotation(up, Vector3.up);

        //Demo Rotation
        if (modelData.Name == "Tobasco")
        {
            Debug.Log("Tobasco rotating");
            modelTransform.rotation = Quaternion.Euler(-46.527f, 66.417f, -118.493f);
        }
        else if (modelData.Name == "Houses")
        {
            Debug.Log("Houses rotating");
            modelTransform.rotation = Quaternion.Euler(-87.038f, 143.324f, -92.64101f);
        }
        else if (modelData.Name == "Salt Shaker")
        {
            Debug.Log("Salt Shaker rotating");
            modelTransform.rotation = Quaternion.Euler(49.763f, 51.159f, 92.40301f);
        }

        modelSelected = true;
        yield return(null);
    }