Пример #1
0
        IEnumerator DownloadOSMCo()
        {
            CreateFolders();

            string url = Paths.GetPath(ePATHSPEC.OSMURL);
            //DRect r = MapTools.TileLatLonBounds(new DVector3(TileX, 0, TileZ), ZoomLevel);
            DRect r = MapTools.TileIdToBounds(TileX, TileZ, ZoomLevel);

            url = url.Replace("{x2}", r.Min.z.ToString());
            url = url.Replace("{y2}", r.Min.x.ToString());
            url = url.Replace("{x1}", r.Max.z.ToString());
            url = url.Replace("{y1}", r.Max.x.ToString());

            WWW www = new WWW(url);

            progress = 0;
            Debug.Log(url);
            while (!www.isDone)
            {
                //progress = www.progress;
                progress++;
                this.Repaint();
                yield return(new WaitForSeconds(0.01f));
            }
            progress = 0;
            File.WriteAllText(Paths.GetPath(ePATHSPEC.OSMPath), www.text);
            AssetDatabase.Refresh();
            Done--;
        }
Пример #2
0
        //x,y,z tile index for the zoom level, same as mapzen or bing maps id
        public void SetTileIndex(Vector3 pos, int Zoom)
        {
            ZoomLevel = Zoom;
            TileIndex = DVector3.FromVector3(pos);
            //DRect r = MapFuncs.TileBoundsInMeters(new DVector3(pos.x, 0, pos.z), Zoom);
            //DVector3 c = MapFuncs.TileIdToCenterLatitudeLongitude((int)pos.x, (int)pos.z, Zoom);
            DRect    latlonbounds = MapTools.TileIdToBounds((int)pos.x, (int)pos.z, Zoom);
            DVector3 c            = latlonbounds.Max;

            PositionMeters = MapTools.LatLonToMeters(c.x, c.z);

            TileBoundsMeters = new DRect(MapTools.LatLonToMeters(latlonbounds.Min.x, latlonbounds.Min.z),
                                         MapTools.LatLonToMeters(latlonbounds.Max.x, latlonbounds.Max.z));
            Info = TileBoundsMeters.ToString();
        }
Пример #3
0
        //points in lat lon, form a closed loop
        public static List <List <Vector3> > CreatePolygon(IList geometry, Vector3 Offset)
        {
            List <List <Vector3> > lists = new List <List <Vector3> >();
            List <Vector3>         list  = new List <Vector3>();

            foreach (IList p in geometry)
            {
                double  dx = (double)p[0];
                double  dz = (double)p[1];
                Vector3 v  = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                v -= Offset;
                list.Add(v);
            }
            lists.Add(list);
            return(lists);
        }
Пример #4
0
        IEnumerator GetGeoInfoCo()
        {
            CreateFolders();
            string FullURL  = Paths.GetPath(ePATHSPEC.GEOINFOURL);
            DRect  worldpos = MapTools.TileIdToBounds(TileX, TileZ, ZoomLevel);

            FullURL = FullURL.Replace("{e}", worldpos.Min.z.ToString());
            FullURL = FullURL.Replace("{n}", worldpos.Min.x.ToString());

            FullURL = FullURL.Replace("{w}", worldpos.Max.z.ToString());
            FullURL = FullURL.Replace("{s}", worldpos.Max.x.ToString());
            string outpath = Paths.GetPath(ePATHSPEC.GEOINFOPATH);

            string data = "";

            if (File.Exists(outpath))
            {
                Debug.Log("Cache Hit: " + outpath);
                data = File.ReadAllText(outpath);
            }
            else
            {
                Debug.Log(FullURL);
                WWW www = new WWW(FullURL);

                while (!www.isDone)
                {
                    progress = www.progress;
                    this.Repaint();
                    yield return(new WaitForSeconds(0.01f));
                }
                progress = 1;
                this.Repaint();

                if (string.IsNullOrEmpty(www.error))
                {
                    data = www.text;
                    File.WriteAllText(outpath, data);
                    AssetDatabase.Refresh();
                }
                else
                {
                    Debug.LogError(www.error);
                }
            }
            Done--;
        }
Пример #5
0
        public static List <List <Vector3> > CreateMultiPolygon(IList segments, DVector3 Offset)
        {
            List <List <Vector3> > lists = new List <List <Vector3> >();

            foreach (IList polygon in segments)
            {
                List <Vector3> list = new List <Vector3>();
                foreach (IList p in polygon)
                {
                    double  dx = (double)p[0];
                    double  dz = (double)p[1];
                    Vector3 v  = MapTools.LatLonToMeters((float)dz, (float)dx).ToVector3();
                    v -= Offset.ToVector3();
                    list.Add(v);
                }
                lists.Add(list);
            }

            return(lists);
        }
Пример #6
0
        //TODO: Skip OBJ step and generate directly to .asset
        void GenerateMeshTerrain()
        {
            Done++;
            Debug.Log("Generating Mesh Terrain for " + TileX + "," + TileZ);
            AssetDatabase.Refresh();
            GetContainers();
            CreateFolders();

            Material m = new Material(Shader.Find("Standard"));

            m.EnableKeyword("_BUMPMAP");
            //_DETAIL_MULX2
            m.color = Color.white;
            m.SetFloat("_SpecularHighlights", 0f);
            m.SetFloat("_GlossyReflections", 0f);

            Texture2D t2 = (Texture2D)AssetDatabase.LoadAssetAtPath <Texture2D>(Paths.GetPath(ePATHSPEC.TEXTUREPATH));

            m.mainTexture = t2;
            Texture2D n1 = (Texture2D)AssetDatabase.LoadAssetAtPath <Texture2D>(Paths.GetPath(ePATHSPEC.NORMALMAPPATH));

            m.SetTexture("_BumpMap", n1);
            m.SetFloat("_Glossiness", 0);

            AssetDatabase.CreateAsset(m, Paths.GetPath(ePATHSPEC.MATERIALPATH));
            AssetDatabase.Refresh();

            //TerrainSettings tset = TerrainProto.GetComponent<TerrainSettings>();

            string hpath = Paths.GetPath(ePATHSPEC.HEIGHTMAPCONTPATH);

            Debug.Log("Instantiating HeightmapCont:" + TileX + "," + TileZ);
            HeightMap = Instantiate(AssetDatabase.LoadAssetAtPath <Texture2D>(hpath));

            string outpath  = Paths.GetPath(ePATHSPEC.MESHOBJPATH);
            DRect  TileSize = MapTools.TileBoundsInMeters(new DVector3(TileX, 0, TileZ), ZoomLevel);
            //ExportOBJ.ExportFromHeight(HeightMap, outpath, TileSize);
            //ExportOBJ.ExportMapZenHeight(HeightMap, outpath, TileSize.Size);
            DVector3 TileLatLon = MapTools.TileToWorldPos(new DVector3(TileX, 0, TileZ), ZoomLevel);

            TerrainContainer = new GameObject();
            MassiveTile tile = TerrainContainer.AddComponent <MassiveTile>();

            ExportOBJ.ExportMapZenHeight(tile, HeightMap, outpath, TileSize.Size, TileLatLon);

            AssetDatabase.Refresh();
            AssetDatabase.ImportAsset(Paths.GetPath(ePATHSPEC.MESHOBJPATH));
            GameObject asset = (GameObject)AssetDatabase.LoadMainAssetAtPath(Paths.GetPath(ePATHSPEC.MESHOBJPATH));
            GameObject obj   = Instantiate(asset);

            //Swap OBJ for .Asset to preserve modifications
            Transform  tr = obj.transform.Find("default");
            MeshFilter mf = tr.GetComponent <MeshFilter>();
            Mesh       nm = Instantiate(mf.sharedMesh);

            AssetDatabase.CreateAsset(nm, Paths.GetPath(ePATHSPEC.MESHASSETPATH));
            AssetDatabase.ImportAsset(Paths.GetPath(ePATHSPEC.MESHASSETPATH));
            AssetDatabase.Refresh();
            Mesh masset = (Mesh)AssetDatabase.LoadMainAssetAtPath(Paths.GetPath(ePATHSPEC.MESHASSETPATH));

            GameObject ma  = new GameObject("terrain");
            MeshFilter tmf = ma.AddComponent <MeshFilter>();

            tmf.mesh = masset;
            ma.AddComponent <MeshRenderer>();
            ma.transform.parent = TerrainContainer.transform;

            GameObject.DestroyImmediate(obj);
            AssetDatabase.Refresh();

            //set position
            Vector3  tm  = MapTools.TileToMeters(TileX, TileZ, ZoomLevel).ToVector3();
            Vector3  sp  = MapTools.TileToMeters((int)StartPostion.x, (int)StartPostion.z, ZoomLevel).ToVector3();
            DVector3 dif = new DVector3(TileX, 0, TileZ) - StartPostion;

            TerrainContainer.transform.position = new Vector3(tm.x - sp.x, 0, sp.z - tm.z);

            TerrainContainer.name = TileX + "_" + "0" + "_" + TileZ + "_" + ZoomLevel;
            GameObject   TerGo = TerrainContainer.transform.Find("terrain").gameObject;
            MeshRenderer mr    = TerGo.GetComponent <MeshRenderer>();

            TerGo.AddComponent <MeshCollider>();
            TerGo.layer = LayerMask.NameToLayer("Terrain");

            tile.ZoomLevel = ZoomLevel;
            tile.SetTileIndex(new Vector3(TileX, 0, TileZ), ZoomLevel);

            Material[] temp = new Material[1];
            //temp[0] = Instantiate((Material)AssetDatabase.LoadAssetAtPath<Material>(path + ".mat"));
            temp[0]      = (Material)AssetDatabase.LoadAssetAtPath <Material>(Paths.GetPath(ePATHSPEC.MATERIALPATH));
            temp[0].name = "mat_" + TileX + "_" + TileZ;
            //TextureTools.Bilinear(t2, 512, 512);
            temp[0].mainTexture = t2;
            //temp[1] = Instantiate((Material)AssetDatabase.LoadAssetAtPath<Material>(path + ".mat"));
            //temp[1].name = "hello";
            mr.sharedMaterials = temp;
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Generated Mesh Terrain");
            Done--;
        }
        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();
        }
Пример #8
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
        }