Exemplo n.º 1
0
        private void CreateTileBackground(GOTile tile)
        {
            MeshFilter   filter   = tile.gameObject.AddComponent <MeshFilter> ();
            MeshRenderer renderer = tile.gameObject.AddComponent <MeshRenderer> ();

            tile.vertices = new List <Vector3> ();
            IList verts = tile.tileCenter.tileVertices(zoomLevel);

            foreach (Vector3 v in verts)
            {
                tile.vertices.Add(tile.transform.InverseTransformPoint(v));
            }

            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = tile.vertices;
            Mesh mesh = Poly2Mesh.CreateMesh(poly);

            Vector3[] meshverts = mesh.vertices;
            Vector2[] uvs       = new Vector2[meshverts.Length];
            for (int i = 0; i < uvs.Length; i++)
            {
                uvs [i] = new Vector2(meshverts [i].x, meshverts [i].z);
            }
            mesh.uv = uvs;

            filter.sharedMesh = mesh;
            tile.gameObject.AddComponent <MeshCollider> ();

            renderer.material = tileBackground;
        }
Exemplo n.º 2
0
        private void CreateTemporaryMapBackground()
        {
            tempTileBackgorund = new GameObject("Temporary tile background");

            MeshFilter   filter   = tempTileBackgorund.AddComponent <MeshFilter>();
            MeshRenderer renderer = tempTileBackgorund.AddComponent <MeshRenderer>();

            float size = 1000;

            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = new List <Vector3> {
                new Vector3(size, -0.1f, size),
                new Vector3(size, -0.1f, -size),
                new Vector3(-size, -0.1f, -size),
                new Vector3(-size, -0.1f, size)
            };
            Mesh mesh = Poly2Mesh.CreateMesh(poly);

            Vector2[] uvs = new Vector2[mesh.vertices.Length];
            for (int i = 0; i < uvs.Length; i++)
            {
                uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
            }
            mesh.uv = uvs;

            filter.mesh       = mesh;
            renderer.material = tileBackground;
        }
Exemplo n.º 3
0
        private static GameObject GenerateFloor(
            List <Vector2> roomContour,
            Vector3 centroid,
            Material floorMat)
        {
            GameObject   go = new GameObject("floor");
            MeshFilter   mf = go.AddComponent <MeshFilter>();
            MeshRenderer mr = go.AddComponent <MeshRenderer>();

            go.transform.position = centroid / MeasurmentK;
            var polygon   = new Poly2Mesh.Polygon();
            var converter = new Converter <Vector2, Vector3>(Vector2ToVector3);

            polygon.outside = roomContour.ConvertAll(converter);
            var mesh  = Poly2Mesh.CreateMesh(polygon);
            var verts = mesh.vertices;

            for (int i = 0; i < verts.Length; i++)
            {
                verts[i] = (centroid - new Vector3(verts[i].x, verts[i].z, verts[i].y)) / MeasurmentK;
            }
            mesh.vertices = verts;

            mesh.uv = MathUtils.CreatePlaneUVs(mesh);
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            mesh.name             = go.name;
            mf.sharedMesh         = mesh;
            mr.sharedMaterial     = floorMat;
            go.transform.rotation = Quaternion.Euler(0, 180, 0);
            return(go);
        }
Exemplo n.º 4
0
        //Draws a polygon given a list of Vector3 that is a closed shape
        public GameObject dropPolygon(List <Vector3> shape, float height, Material material)
        {
            GameObject polygon = new GameObject("Polygon");

            MeshFilter   filter   = polygon.AddComponent <MeshFilter>();
            MeshRenderer renderer = polygon.AddComponent <MeshRenderer>();

            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = shape;

            Mesh mesh = Poly2Mesh.CreateMesh(poly);

            Vector2[] uvs = new Vector2[mesh.vertices.Length];
            for (int i = 0; i < uvs.Length; i++)
            {
                uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z) * 100;
            }
            mesh.uv = uvs;

            if (height > 0)
            {
                mesh = SimpleExtruder.SliceExtrude(mesh, polygon, height, 4f, 4f, 10f);
            }

            filter.sharedMesh = mesh;
            renderer.material = material;

            polygon.AddComponent <MeshCollider> ();

            return(polygon);
        }
Exemplo n.º 5
0
    void GenMeshForUV(Vector2[] parentUV, List <Vector3> myVerts, out Vector3[] allVertsMesh, out Vector2[] allUV, float angle)
    {
        Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
        poly.outside = myVerts;
        Mesh mesh = Poly2Mesh.CreateMesh(poly);

        mesh.uv      = GetUVSplit(parentUV, mesh.vertices, angle);
        allVertsMesh = mesh.vertices;
        allUV        = mesh.uv;
    }
Exemplo n.º 6
0
        private static GameObject GenerateWall(
            Vector3 begin,
            Vector3 end,
            Vector3 center,
            List <List <Vector2> > holes,
            float apartmentHeight,
            Material wallMat,
            int index)
        {
            GameObject   wallGO = new GameObject("wall" + index);
            MeshFilter   wallMf = wallGO.AddComponent <MeshFilter>();
            MeshRenderer wallMr = wallGO.AddComponent <MeshRenderer>();

            float          wallLength  = (Vector2.Distance(begin, end) / 2) / MeasurmentK;
            float          wallHeight  = apartmentHeight / MeasurmentK;
            List <Vector2> wallContour = new List <Vector2>
            {
                new Vector2(-wallLength, 0),
                new Vector2(-wallLength, wallHeight),
                new Vector2(wallLength, wallHeight),
                new Vector2(wallLength, 0)
            };

            var polygon   = new Poly2Mesh.Polygon();
            var converter = new Converter <Vector2, Vector3>(Vector2ToVector3);

            polygon.outside = wallContour.ConvertAll(converter);
            var v3Holes = new List <List <Vector3> >();

            holes.ForEach(x => v3Holes.Add(x.ConvertAll(converter)));
            for (int i = 0; i < v3Holes.Count; i++)
            {
                var hole = v3Holes[i];
                for (int j = 0; j < hole.Count; j++)
                {
                    hole[j] = hole[j] / MeasurmentK;
                }
            }

            polygon.holes = v3Holes;
            var mesh = Poly2Mesh.CreateMesh(polygon);

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            mesh.uv               = MathUtils.CreatePlaneUVs(mesh);
            mesh.name             = wallGO.name;
            wallMf.sharedMesh     = mesh;
            wallMr.sharedMaterial = wallMat;

            var transform = wallGO.transform;

            transform.rotation = Quaternion.Euler(0, -Vector2.SignedAngle(Vector2.right, end - begin), 0);
            transform.position = center / MeasurmentK;
            return(wallGO);
        }
Exemplo n.º 7
0
        public GameObject CreateModel(Layer layer, float height)
        {
            GameObject polygon = new GameObject();

            try {
                Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
                poly.outside = convertedSubject;
                if (clips != null)
                {
                    foreach (IList clipVerts in clips)
                    {
                        poly.holes.Add(CoordsToVerts(clipVerts));
                    }
                }

                MeshFilter filter = polygon.AddComponent <MeshFilter>();
                polygon.AddComponent(typeof(MeshRenderer));
                Mesh mesh = Poly2Mesh.CreateMesh(poly);


                if (mesh)
                {
                    Vector2[] uvs = new Vector2[mesh.vertices.Length];
                    for (int i = 0; i < uvs.Length; i++)
                    {
                        uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
                    }
                    mesh.uv = uvs;

                    mesh2D = Mesh.Instantiate(mesh);

                    if (height > 0)
                    {
                        mesh = SimpleExtruder.Extrude(mesh, polygon, height);
                    }
                }
                filter.sharedMesh = mesh;

                if (layer.useColliders)
                {
                    polygon.AddComponent <MeshCollider>().sharedMesh = mesh;
                }
            }
            catch (Exception ex) {
                Debug.Log("[PolygonHandler] Catched exeption: " + ex);
            }



            return(polygon);
        }
Exemplo n.º 8
0
        GameObject AddPolygonMesh(GameObject parent, Vector3[] ps, Material material)
        {
            GameObject s = new GameObject("SurfacePolygon");

            s.transform.parent = parent.transform;
            s.AddComponent <MeshRenderer>();
            MeshFilter filter = s.AddComponent <MeshFilter>();

            Poly2Mesh.Polygon polygon = new Poly2Mesh.Polygon();
            polygon.outside = new List <Vector3>(ps);
            filter.mesh     = Poly2Mesh.CreateMesh(polygon);
            ApplyMaterial(s, material);
            return(s);
        }
Exemplo n.º 9
0
        private void CreateTileBackground(GOTile tile)
        {
            MeshFilter   filter   = tile.gameObject.AddComponent <MeshFilter>();
            MeshRenderer renderer = tile.gameObject.AddComponent <MeshRenderer>();

            tile.vertices = tile.tileCenter.tileVertices(zoomLevel);
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = tile.vertices;
            Mesh mesh = Poly2Mesh.CreateMesh(poly);

            Vector2[] uvs = new Vector2[mesh.vertices.Length];
            for (int i = 0; i < uvs.Length; i++)
            {
                uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
            }
            mesh.uv = uvs;

            filter.mesh       = mesh;
            renderer.material = tileBackground;
        }
Exemplo n.º 10
0
        private TexturedMesh CreateSurface(Surface surf)
        {
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = CreateVectorlist(surf.outerRing);
            if (surf.outerringUVs.Count > 0)
            {
                surf.outerringUVs.Reverse();
                poly.outsideUVs = surf.outerringUVs;
            }
            ;
            if (surf.innerRings.Count > 0)
            {
                foreach (var innerring in surf.innerRings)
                {
                    poly.holes.Add(CreateVectorlist(innerring));
                }
            }
            if (surf.innerringUVs.Count > 0)
            {
                surf.innerringUVs.Reverse();
                poly.holesUVs = surf.innerringUVs;
            }

            Mesh submesh = Poly2Mesh.CreateMesh(poly);

            TexturedMesh texturedmesh = new TexturedMesh();

            texturedmesh.mesh = submesh;
            if (surf.surfacetexture != null)
            {
                texturedmesh.TextureName = surf.surfacetexture.path;
                //texturedmesh.texture = LoadTexture(surf.surfacetexture);
            }
            submesh = null;
            return(texturedmesh);
        }
Exemplo n.º 11
0
        public GameObject BuildPolygon(GOLayer layer, float height)
        {
            if (feature.convertedGeometry.Count == 2 && feature.convertedGeometry[0].Equals(feature.convertedGeometry[1]))
            {
                return(null);
            }
            List <Vector3> clean = feature.convertedGeometry.Distinct().ToList();

            if (clean == null || clean.Count <= 2)
            {
                return(null);
            }

            GameObject polygon = new GameObject();

            Profiler.BeginSample("[GoMap] Start poly2mesh");
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = feature.convertedGeometry;
            if (feature.clips != null)
            {
                foreach (IList clipVerts in feature.clips)
                {
                    poly.holes.Add(GOFeature.CoordsToVerts(clipVerts, true));
                }
            }
            Profiler.EndSample();

            MeshFilter filter = polygon.AddComponent <MeshFilter>();

            meshRenderer = polygon.AddComponent <MeshRenderer>();

            Profiler.BeginSample("[GoMap] Create polygon mesh");
            try {
                mesh = Poly2Mesh.CreateMesh(poly);
            } catch {
            }
            Profiler.EndSample();


            if (mesh)
            {
                Profiler.BeginSample("[GoMap] Set polygon UV");
                Vector2[] uvs      = new Vector2[mesh.vertices.Length];
                Vector3[] vertices = mesh.vertices;
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
                }
                mesh.uv = uvs;
                Profiler.EndSample();

                Profiler.BeginSample("[GoMap] instantiate mesh 2D");
                mesh2D = Mesh.Instantiate(mesh);
                Profiler.EndSample();

                Profiler.BeginSample("[GoMap] polygon extrusion");
                if (height > 0)
                {
                    mesh = SimpleExtruder.SliceExtrude(mesh, polygon, height, 4f, 4f, 10f);
//					mesh = SimpleExtruder.Extrude (mesh, polygon, height);
                }
                Profiler.EndSample();
            }


            filter.sharedMesh = mesh;

            if (layer.useColliders && mesh != null && feature.convertedGeometry.Count() > 2)
            {
                polygon.AddComponent <MeshCollider>().sharedMesh = mesh;
            }


            return(polygon);
        }
        public GameObject BuildPolygon(string name, GOLayer layer, float height)
        {
            //GameObject polygon = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "Building_CornerHouse_A"));
            GameObject polygon = new GameObject();

            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = feature.convertedGeometry;
            if (feature.clips != null)
            {
                foreach (IList clipVerts in feature.clips)
                {
                    poly.holes.Add(GOFeature.CoordsToVerts(clipVerts));
                }
            }

            MeshFilter filter = polygon.AddComponent <MeshFilter>();

            polygon.AddComponent(typeof(MeshRenderer));

            try {
                mesh = Poly2Mesh.CreateMesh(poly);
            } catch {
            }
            if (height > 1)
            {
                if (mesh)
                {
                    Vector2[] uv2d = new Vector2[mesh.vertices.Length];
                    for (int i = 0; i < uv2d.Length; i++)
                    {
                        uv2d[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
                    }
                    var meshlemp = Mesh.Instantiate(mesh);
                    meshlemp.uv = uv2d;
                    mesh2D      = Mesh.Instantiate(meshlemp);

                    mesh.uv = uv2d;
                    if (height > 0)
                    {
                        mesh = SimpleExtruder.Extrude(mesh, polygon, height);
                        Vector2[] uvs3D = new Vector2[mesh.vertices.Length];
                        for (int i = 0; i < uvs3D.Length - 1; i++)
                        {
                            uvs3D[i] = new Vector2(Vector2.Distance(new Vector2(mesh.vertices[i + 1].x, mesh.vertices[i + 1].z), new Vector2(mesh.vertices[i].x, mesh.vertices[i].z)), mesh.vertices[i].y);
                        }
                        // uvs2[uvs2.Length - 1] = new Vector2(Mathf.Sqrt((float)(Math.Pow(mesh.vertices[0].x - mesh.vertices[uvs2.Length - 1].x, 2) + Math.Pow(mesh.vertices[0].z - mesh.vertices[uvs2.Length - 1].z, 2))), mesh.vertices[uvs2.Length - 1].y);
                        uvs3D[uvs3D.Length - 1] = new Vector2(10, mesh.vertices[uvs3D.Length - 1].y);
                        mesh.uv = uvs3D;
                    }
                }
            }
            else
            {
                if (mesh)
                {
                    Vector2[] uvs = new Vector2[mesh.vertices.Length];
                    for (int i = 0; i < uvs.Length; i++)
                    {
                        uvs[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
                    }
                    mesh.uv = uvs;
                    if (height > 0)
                    {
                        mesh = SimpleExtruder.Extrude(mesh, polygon, height);
                    }
                }
            }
            filter.sharedMesh = mesh;

            if (layer.useColliders)
            {
                polygon.AddComponent <MeshCollider>().sharedMesh = mesh;
            }
            return(polygon);
        }
        public GameObject BuildPolygon(string name, Vector3 pos)
        {
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = feature.convertedGeometry;
            if (feature.clips != null)
            {
                foreach (IList clipVerts in feature.clips)
                {
                    poly.holes.Add(GOFeature.CoordsToVerts(clipVerts));
                }
            }
            try
            {
                mesh = Poly2Mesh.CreateMesh(poly);
            }
            catch
            {
            }
            float   d     = 0f;
            Vector3 f_Pos = pos;

            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                var l = Vector2.Distance(pos, mesh.vertices[i]);
                if (d < l)
                {
                    d     = l;
                    f_Pos = mesh.vertices[i];
                }
            }
            float ea_y = GetSC(f_Pos, pos);
            var   sc   = d / 1.414f;

            if (sc > 2f)
            {
                sc = sc / 4;
                GameObject polygon = new GameObject("polygon");
                polygon.transform.localPosition = pos;
                //for (int i = 0;i<2;i++)
                // {
                //    for (int j = 0;j<2;j++)
                //   {
                GameObject item = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "B_10"));
                item.transform.localPosition    = pos;     //new Vector3((pos.x-sc/2)+sc*i,0, (pos.z - sc / 2) + sc * j) ;
                item.transform.localEulerAngles = new Vector3(0, ea_y, 0);
                item.transform.localScale       = Vector3.one * (sc * Global.tilesizeRank);
                item.transform.SetParent(polygon.transform);
                //  }
                //   }
                polygon.transform.Rotate(0, ea_y, 0);
                return(polygon);
            }
            else
            {
                if (sc < 0.8f)
                {
                    sc = 1f;
                }
                GameObject polygon = GameObject.Instantiate((GameObject)Resources.Load("Prefabs/CityBuildings/" + "B_" + UnityEngine.Random.Range(3, 10)));
                polygon.transform.localPosition    = pos;
                polygon.transform.localEulerAngles = new Vector3(0, ea_y, 0);
                polygon.transform.localScale       = Vector3.one * (sc * Global.tilesizeRank);
                return(polygon);
            }
        }
Exemplo n.º 14
0
        private TexturedMesh CreateSurface(Surface surf, GameObject parentGameobject = null)
        {
            Poly2Mesh.Polygon poly = new Poly2Mesh.Polygon();
            poly.outside = CreateVectorlist(surf.outerRing, Origin);
            if (surf.outerringUVs.Count > 0)
            {
                poly.outsideUVs = surf.outerringUVs;
            }
            ;
            if (surf.innerRings.Count > 0)
            {
                foreach (var innerring in surf.innerRings)
                {
                    poly.holes.Add(CreateVectorlist(innerring, Origin));
                }
            }
            if (surf.innerringUVs.Count > 0)
            {
                poly.holesUVs = surf.innerringUVs;
            }

            Mesh submesh = Poly2Mesh.CreateMesh(poly);

            if (parentGameobject != null)
            {
                GameObject go = new GameObject();

                for (int i = 0; i < surf.semantics.Count; i++)
                {
                    if (surf.semantics[i].name == "type")
                    {
                        go.name = surf.semantics[i].value;
                        i       = surf.semantics.Count;
                    }
                }


                if (submesh != null)
                {
                    go.transform.parent = parentGameobject.transform;
                    go.AddComponent <MeshFilter>().sharedMesh       = submesh;
                    go.AddComponent <MeshRenderer>().sharedMaterial = Defaultmaterial;
                    ObjectProperties op = go.AddComponent <ObjectProperties>();
                    op.semantics = surf.semantics;
                    if (surf.surfacetexture != null && useTextures == true)
                    {
                        Material mat = new Material(Defaultmaterial);
                        mat.color = new Color(1, 1, 1);
                        mat.SetTexture("_MainTex", LoadTexture(surf.surfacetexture));
                        go.GetComponent <MeshRenderer>().material = mat;
                    }
                }
            }
            else
            {
                TexturedMesh texturedmesh = new TexturedMesh();
                for (int i = 0; i < surf.semantics.Count; i++)
                {
                    if (surf.semantics[i].name == "name")
                    {
                        texturedmesh.IdentifierString = surf.semantics[i].value;
                    }
                }
                texturedmesh.mesh = submesh;
                if (surf.surfacetexture != null)
                {
                    texturedmesh.TextureName = surf.surfacetexture.path;
                    texturedmesh.texture     = LoadTexture(surf.surfacetexture);
                }

                return(texturedmesh);
            }
            return(null);
        }