Ejemplo n.º 1
0
        private void CreateBuildings(JSONObject mapData, Vector2 worldCenter)
        {
            //filter to just polygons
            foreach (var geo in mapData["features"].list.Where(x => x["geometry"]["type"].str == "Polygon"))
            {
                var buildIngNameOBJ = geo["properties"]["name"]; //.str == "G1"
                var buildingNameStr = "";
                if (!ReferenceEquals(buildIngNameOBJ, null))
                {
                    buildingNameStr = buildIngNameOBJ.str;
                    if (buildingNameStr == "G1")
                    {
                        Debug.Log("Stop");
                    }
                }

                //convert and add points
                var l = new List <Vector3>();
                for (int i = 0; i < geo["geometry"]["coordinates"][0].list.Count - 1; i++)
                {
                    var c  = geo["geometry"]["coordinates"][0].list[i];
                    var bm = GM.LatLonToMeters(c[1].f, c[0].f);
                    var pm = new Vector2(bm.x - Rect.center.x, bm.y - Rect.center.y);
                    l.Add(pm.ToVector3xz());
                }
                //make them buildings
                try
                {
                    var center = l.Aggregate((acc, cur) => acc + cur) / l.Count;
                    if (!BuildingDictionary.ContainsKey(center))
                    {
                        var bh = new BuildingHolder(center, l);
                        for (int i = 0; i < l.Count; i++)
                        {
                            l[i] = l[i] - bh.Center;
                        }
                        BuildingDictionary.Add(center, bh);

                        var m = bh.CreateModel(BuildingMaterial);
                        m.name                    = "building " + buildingNameStr;
                        m.transform.parent        = this.transform;
                        center                    = new Vector3(center.x, center.y, center.z);
                        m.transform.localPosition = center;
                    }
                }
                catch (Exception ex)
                {
                    Debug.Log(ex);
                }
            }
        }
Ejemplo n.º 2
0
        private void CreateBuildings(JSONObject mapData, Vector2 worldCenter)
        {
            //filter to just polygons
            foreach (var geo in mapData["features"].list.Where(x => x["geometry"]["type"].str == "Polygon"))
            {
                //convert and add points

                var geocenterx = new List <float> ();
                var geocentery = new List <float> ();

                var l = new List <Vector3>();
                for (int i = 0; i < geo["geometry"]["coordinates"][0].list.Count - 1; i++)
                {
                    var c = geo["geometry"]["coordinates"][0].list[i];
                    geocenterx.Add(c [1].f);
                    geocentery.Add(c [0].f);
                    var bm = GM.LatLonToMeters(c[1].f, c[0].f);

                    //c[1].f====>33
                    var pm = new Vector2(bm.x - Rect.center.x, bm.y - Rect.center.y);
                    l.Add(pm.ToVector3xz());
                }


                try
                {
                    var center = l.Aggregate((acc, cur) => acc + cur) / l.Count;

                    float centerx = geocenterx.Sum() / geocenterx.Count;
                    float centery = geocentery.Sum() / geocentery.Count;



                    if (!BuildingDictionary.ContainsKey(center))
                    {
                        var bh = new BuildingHolder(center, l);
                        for (int i = 0; i < l.Count; i++)
                        {
                            l[i] = l[i] - bh.Center;
                        }
                        BuildingDictionary.Add(center, bh);

                        var m = bh.CreateModel();


                        if (geo["properties"]["name"] != null)
                        {
                            m.name = geo["properties"]["name"].ToString().Split('"')[1];
                        }
                        else
                        {
                            m.name = "Unknown_Building";
                        }
                        var st = m.gameObject.AddComponent <Storage>();
                        st.lat = centerx;
                        st.lng = centery;

                        //Debug.Log(m.name + " --- [" +centerx +"," + centery +"]");

                        if (geo["properties"]["height"] != null)
                        {
                            float heighttemp = float.Parse(geo["properties"]["height"].ToString());
                            m.transform.localScale = new Vector3(1, heighttemp / 16, 1);
                        }

                        m.transform.parent        = this.transform;
                        center                    = new Vector3(center.x, center.y, center.z);
                        m.transform.localPosition = center;


                        m.AddComponent <MeshCollider>();
                        m.tag = "Collider";


                        if (geo["properties"]["addr_housenumber"] != null)
                        {
                            st.adress = (geo["properties"]["addr_housenumber"].ToString().Split('\"')[1] + " " + geo["properties"]["addr_street"].ToString().Split('\"')[1]);
                        }

                        //check

//						Collider[] hitC = Physics.OverlapSphere (m.transform.position, 30);
//						for(int i = 0; i < hitC.Length;i++){
//							if(hitC[i].name != "Plane"){
//								GameObject.Destroy(m);
//								break;
//							}else{
//								Debug.Log(hitC[i].name);
//							}
//						}
                    }
                }
                catch (Exception ex)
                {
                    //Debug.Log(ex);
                }
            }
        }