Пример #1
0
        void GenerateGeometry(List <List <Vector3> > segments, GameObject container, IDictionary properties, string type)
        {
            string pname = OSMTools.GetProperty(properties, "name");
            // Debug.Log( "Processing:" + pname);
            //List<Vector2> points = new List<Vector2>();

            GameObject go = new GameObject("building");

            go.transform.parent = container.transform;
            go.layer            = LayerMask.NameToLayer("Buildings");
            go.name             = pname;
            //go.transform.position = segments[0][0];
            go.transform.localPosition = Vector3.zero;
            //go.name = properties["kind"].ToString();
            MassiveFeature f = go.AddComponent <MassiveFeature>();

            f.SetProperties(properties);
            f.SetSegments(segments);
            f.SetType(type);
            //if height it not present assume 1 floor
            height = f.height == 0 ? 5 : f.height;

            CreateRoof(segments, go);
            CreateWalls(segments, go);
            MeshTools.MoveGameObjectToTerrain(TileBoundsMeters, this.transform.position, mc, go, Walls.GetComponent <MeshFilter>().sharedMesh);

            //create roof

            //MeshTools.FitMeshToTerrain(TileBoundsMeters, this.transform.position, mc, msh);
            //MeshTools.MoveMeshToTerrain(TileBoundsMeters, this.transform.position, mc, msh, f.height, go);
        }
Пример #2
0
        void GenerateGeometry(List <List <Vector3> > segments, GameObject container, IDictionary properties, string type)
        {
            List <Vector2> points = new List <Vector2>();

            foreach (List <Vector3> list in segments)
            {
                GameObject go = new GameObject("piece");
                go.transform.parent = container.transform;
                go.layer            = LayerMask.NameToLayer("Water");
                go.name             = properties["kind"].ToString();

                MassiveFeature mf = go.AddComponent <MassiveFeature>();
                mf.SetProperties(properties);
                mf.SetSegments(segments);
                mf.SetType(type);


                for (int i = 0; i < list.Count - 1; i++)
                {
                    Vector3 v1 = list[i];
                    points.Add(new Vector2((float)v1.x, (float)v1.z));
                    //float heightv1 = GetMeshHeight(v1);
                    //GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    //go.transform.parent = container.transform;
                    //go.transform.position = v1.ToVector3();
                    //go.transform.localScale = new Vector3(20, 20, 20);
                }

                Triangulator tr      = new Triangulator(points.ToArray());
                int[]        indices = tr.Triangulate();

                // Create the Vector3 vertices
                Vector3[] vertices = new Vector3[points.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] = new Vector3(points[i].x, 0, points[i].y);
                    float h = GetMeshHeight(vertices[i]);
                    vertices[i].y = h;
                }

                // Create the mesh
                Mesh msh = new Mesh();
                msh.vertices  = vertices;
                msh.triangles = indices;
                msh.RecalculateNormals();
                msh.RecalculateBounds();

                // Set up game object with mesh;
                MeshRenderer r = go.AddComponent <MeshRenderer>();
                r.material          = OceanMaterial;
                r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                MeshFilter filter = go.AddComponent(typeof(MeshFilter)) as MeshFilter;
                filter.mesh = msh;
                go.AddComponent <MeshCollider>();
            }
        }
        public void CreateRoads(string sData, DVector3 PositionMeters)
        {
            TileBoundsMeters = this.transform.parent.GetComponent <MassiveTile>().TileBoundsMeters;
            mc = transform.parent.Find("terrain").GetComponent <MeshCollider>();

            Dictionary <string, object> Data     = (Dictionary <string, object>)MiniJSON.Json.Deserialize(sData);
            List <List <Vector3> >      segments = new List <List <Vector3> >();
            IList features = (IList)Data["features"];

            Roads.Clear();
            foreach (IDictionary feature in features)
            {
                IDictionary geometry   = (IDictionary)feature["geometry"];
                IDictionary properties = (IDictionary)feature["properties"];

                string geotype     = (string)geometry["type"];
                string name        = OSMTools.GetProperty(properties, "name");
                string kind        = OSMTools.GetProperty(properties, "kind");
                string kind_detail = OSMTools.GetProperty(properties, "kind_detail");
                //Debug.Log("Processing:" + name);

                if (geotype == "LineString")
                {
                    Road           rs = new Road(name, this.gameObject, kind, kind_detail);
                    MassiveFeature mf = rs.go.AddComponent <MassiveFeature>();
                    mf.SetProperties(properties);
                    rs.is_bridge = mf.is_bridge;

                    for (int i = 0; i < ((IList)geometry["coordinates"]).Count; i++)
                    {
                        IList   piece = (IList)((IList)geometry["coordinates"])[i];
                        double  dx    = (double)piece[0];
                        double  dz    = (double)piece[1];
                        Vector3 v     = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                        v -= PositionMeters.ToVector3();
                        Node n = new Node(v, name);
                        rs.nodes.Add(n);
                        if (i == 0)
                        {
                            //rs.go.transform.position = v;
                            rs.go.layer = LayerMask.NameToLayer("Roads");
                        }
                    }
                    Roads.Add(rs);
                }


                if (geotype == "MultiLineString")
                {
                    GameObject go = new GameObject();
                    go.transform.parent = this.transform;
                    MassiveFeature mf = go.AddComponent <MassiveFeature>();
                    mf.SetProperties(properties);

                    for (int i = 0; i < ((IList)geometry["coordinates"]).Count; i++)
                    {
                        Road rs = new Road(name, go, kind, kind_detail);
                        rs.is_bridge = mf.is_bridge;
                        go.name      = name;
                        IList segment = (IList)((IList)geometry["coordinates"])[i];
                        for (int j = 0; j < segment.Count; j++)
                        {
                            IList   piece = (IList)segment[j];
                            double  dx    = (double)piece[0];
                            double  dz    = (double)piece[1];
                            Vector3 v     = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                            v -= PositionMeters.ToVector3();
                            Node n = new Node(v, name);
                            rs.nodes.Add(n);
                            if ((i == 0) && (j == 0))
                            {
                                //rs.go.transform.position = v;
                                rs.go.layer = LayerMask.NameToLayer("Roads");
                            }
                        }
                        Roads.Add(rs);
                    }
                }
            }
            //calculate intersections
            CalculateIntersections();
            CreateRoadGeometry();
            //VisualizeNodes();
        }
Пример #4
0
        public void Generate(string sData, DVector3 PositionMeters, string AssetPath)
        {
            mc = transform.parent.Find("terrain").GetComponent <MeshCollider>();
            TileBoundsMeters = this.transform.parent.GetComponent <MassiveTile>().TileBoundsMeters;
            Data             = (Dictionary <string, object>)MiniJSON.Json.Deserialize(sData);

            IList features = (IList)Data["features"];

            foreach (IDictionary feature in features)
            {
                IDictionary geometry = (IDictionary)feature["geometry"];
                string      geotype  = (string)geometry["type"];

                IDictionary properties = (IDictionary)feature["properties"];
                string      kind       = "";
                string      kinddetail = "";
                string      name       = "_";
                string      id         = "";
                name       = OSMTools.GetProperty(properties, "name");
                kind       = OSMTools.GetProperty(properties, "kind");
                kinddetail = OSMTools.GetProperty(properties, "kind_detail");
                id         = OSMTools.GetProperty(properties, "id");

                //if (name == "_") continue;

                if (!CanWeInclude(AllowKinds, kind))
                {
                    continue;
                }


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

                if (geotype == "LineString" &&
                    CanWeInclude(AllowShapes, "LineString"))
                {
                    List <Vector3> list = new List <Vector3>();
                    foreach (IList piece in (IList)geometry["coordinates"])
                    {
                        double  dx = (double)piece[0];
                        double  dz = (double)piece[1];
                        Vector3 v  = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                        v -= PositionMeters.ToVector3();
                        list.Add(v);
                    }

                    list = OSMTools.InsertPoints(list, InterpolateRoadMeters);

                    segments.Add(list);
                }

                if (geotype == "MultiLineString" &&
                    CanWeInclude(AllowShapes, "MultiLineString"))
                {
                    foreach (IList piecelist in (IList)geometry["coordinates"])
                    {
                        List <Vector3> list = new List <Vector3>();
                        foreach (IList piece in (IList)piecelist)
                        {
                            double  dx = (double)piece[0];
                            double  dz = (double)piece[1];
                            Vector3 v  = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                            v -= PositionMeters.ToVector3();
                            list.Add(v);
                        }
                        list = OSMTools.InsertPoints(list, InterpolateRoadMeters);
                        segments.Add(list);
                    }
                }

                if (segments.Count > 0)
                {
                    MeshData md = new MeshData();
                    Run(segments, md, GetWidthForKind(kind));

                    GameObject go = new GameObject(name);
                    go.layer = LayerMask.NameToLayer("Roads");
                    MassiveFeature f = go.AddComponent <MassiveFeature>();
                    f.SetSegments(segments);
                    f.SetProperties(properties);
                    f.SetType(geotype);
                    f.SetID(id);
                    go.transform.parent        = this.transform;
                    go.transform.localPosition = Vector3.zero;
                    // Create the mesh
                    CreateGameObject(md, go, kind);
                    //WriteAsset(go, AssetPath);
                }
            }

#if UNITY_EDITOR
            AssetDatabase.SaveAssets();
#endif
        }