public GameObject Generate() { // we use the global GameObject for this town if (TownGlobalObject.townsData[town.coord].TownGameObject == null) { TownGlobalObject.townsData[town.coord].TownGameObject = new GameObject(town.name); } var go = TownGlobalObject.townsData[town.coord].TownGameObject; go.transform.parent = root; child = go.transform; child.transform.localPosition = Vector3.zero; // var bounds = town.GetCityWallsBounds().Expand(100); var geometry = town.GetTownGeometry(options); MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3>(); UnityEngine.Random.InitState(options.Seed.GetHashCode()); if (options.Water) { Waters = new GameObject("Waters"); Waters.transform.parent = child; Waters.transform.localPosition = Vector3.zero; foreach (var water in geometry.Water) { foreach (var vertex in water.Vertices) { vertices.Add(new Vector3(vertex.x, -3f, vertex.y)); } poly = new MeshUtils.Polygon("Water", vertices, 3f, rendererOptions.WaterMaterial, Waters.transform); poly.Transform.localPosition = Vector3.zero; vertices.Clear(); } } else { Waters = null; } BuildingsMesh = new GameObject("BuildingsMesh"); BuildingsMesh.transform.parent = child; BuildingsMesh.transform.localPosition = new Vector3(0, -child.position.y, 0); //if (options.CityDetail) if (true) { Buildings = new GameObject("Floating Map Overlay"); Buildings.transform.parent = child; Buildings.transform.localPosition = Vector3.zero; List <Building> onsite = town.GetTownGeometry(town.Options).Buildings; // 3d buildings //List<Geom.Polygon> shapes = new List<Geom.Polygon>(); //foreach (var building in onsite) //{ // shapes.Add(building.Shape); //} //BuildingPlacer bp = new BuildingPlacer(shapes, 10); //List<Building> myBuilds = bp.PopulateBuildings(); //Debug.Log(myBuilds.Count); // end 3d buildings foreach (var building in onsite) { foreach (var vertex in building.Shape.Vertices) { vertices.Add(new Vector3( ScaleToWorldWithOffset( vertex.x, town.townOffset.x), 950, ScaleToWorldWithOffset( vertex.y, town.townOffset.y) )); } poly = new MeshUtils.Polygon( town.name + "_" + building.Description, vertices.Select(x => new Vector3(x.x, 0, x.z)).ToList(), //0.1f, UnityEngine.Random.Range(20f, 35f), rendererOptions.TownModelsOverlay, BuildingsMesh.transform, true); poly.Transform.localPosition = Vector3.zero; poly.GameObject.AddComponent <LerpToGround>(); poly.GameObject.AddComponent <SpawnAndWriteSigns>().Sign = TownGlobalObjectService.rendererOptions.signPrefab; poly.GameObject.layer = LayerMask.NameToLayer("Ignore Raycast"); Vector3 c = new Vector3(0, 0, 0); foreach (var vertex in vertices) { c += vertex; } c.x = c.x / vertices.Count; c.y = c.y / vertices.Count; c.z = c.z / vertices.Count; poly = new MeshUtils.Polygon( building.Description, vertices, 0.1f, // UnityEngine.Random.Range(5f, 10f), rendererOptions.BuildingMaterial, Buildings.transform, true); // var scrpt = poly.GameObject.AddComponent<FindOverlay>(); // Vector3 thecenter = new Vector3(vertices.Sum(x => x.x), vertices.Sum(x => x.y), vertices.Sum(x => x.z)) / vertices.Count; // scrpt.startingoffset = thecenter; // GameObject thingGo = GameObject.Instantiate<GameObject>(thing, poly.GameObject.transform, true); //// town.Center.x // thingGo.transform.localPosition = new Vector3(0,100,0); //if (options.IOC) //{ // poly.GameObject.layer = LayerMask.NameToLayer("IOC"); // IOClod scrip = poly.GameObject.AddComponent<IOClod>(); // scrip.Occludee = true; // scrip.Static = true; // scrip.Lod1 = 0; // scrip.Lod2 = 0; // scrip.LodMargin = 0; // scrip.LodOnly = false; //} //SignCommands.WriteAndMove( // building.Description, // poly.Transform.position + c, // Quaternion.identity, // poly.GameObject); poly.Transform.localPosition = Vector3.zero; poly.GameObject.layer = LayerMask.NameToLayer(CamMapLayer); vertices.Clear(); } } if (true) { //if (options.Roads) //{ DrawRoads(geometry, null); } //var curpos = GameObject.FindGameObjectWithTag("Player").transform; // Den.Tools.Coord newPositionAsCoord = new Den.Tools.Coord((int)(curpos.position.x * 0.001), (int)(curpos.position.z * 0.001)); // if ( Den.Tools.Coord.Distance((town.coord + town.mapOffset.ToCoord() ), newPositionAsCoord) < 4) // //if (options.Walls) // { // DrawWallSplines(geometry, null); // DrawWalls(geometry, null); // } // else // { Walls = null; //} return(go); }
public GameObject Generate() { var go = new GameObject("Town"); go.transform.parent = root; child = go.transform; child.transform.localPosition = Vector3.zero; var bounds = town.GetCityWallsBounds().Expand(100); var geometry = town.GetTownGeometry(options); MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3> (); UnityEngine.Random.InitState(options.Seed.GetHashCode()); if (options.Water) { Waters = new GameObject("Waters"); Waters.transform.parent = child; Waters.transform.localPosition = Vector3.zero; foreach (var water in geometry.Water) { foreach (var vertex in water.Vertices) { vertices.Add(new Vector3(vertex.x, -3f, vertex.y)); } poly = new MeshUtils.Polygon("Water", vertices, 3f, rendererOptions.WaterMaterial, Waters.transform); poly.Transform.localPosition = Vector3.zero; vertices.Clear(); } } else { Waters = null; } BuildingsMesh = new GameObject("BuildingsMesh"); BuildingsMesh.transform.parent = child; BuildingsMesh.transform.localPosition = Vector3.zero; Buildings = new GameObject("Buildings"); Buildings.transform.parent = child; Buildings.transform.localPosition = Vector3.zero; foreach (var building in geometry.Buildings) { foreach (var vertex in building.Shape.Vertices) { vertices.Add(new Vector3(vertex.x, 0, vertex.y)); } poly = new MeshUtils.Polygon(building.Description + "Base", vertices, 0.1f, rendererOptions.BuildingMaterial, Buildings.transform, true); poly.Transform.localPosition = Vector3.zero; poly = new MeshUtils.Polygon(building.Description, vertices, UnityEngine.Random.Range(2f, 4f), rendererOptions.BuildingMaterial, BuildingsMesh.transform, false); poly.Transform.localPosition = Vector3.zero; vertices.Clear(); } DrawRoads(geometry, null); if (options.Walls) { DrawWalls(geometry, null); } else { Walls = null; } if (options.Overlay) { DrawOverlay(geometry, null); } return(go); }
private void DrawOverlay(TownGeometry geometry, StringBuilder sb) { MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3>(); var overlays = new GameObject("Overlays"); overlays.gameObject.layer = LayerMask.NameToLayer(CamMapLayer); overlays.transform.parent = child; overlays.transform.localPosition = new Vector3(0, 0, 0); // Vector3.zero; foreach (var patch in geometry.Overlay) { if (patch.Water) { continue; } if (patch.Area.GetType() == typeof(EmptyArea)) { continue; } if (patch.Area.GetType() == typeof(FarmArea)) { continue; // here exclude outlying areas. you could put this back. } var type = patch.Area.ToString(); float previousOffset = 900f; foreach (var vertex in patch.Shape.Vertices) { float MovedX = ScaleToWorldWithOffset(vertex.x, town.townOffset.x); float MovedY = ScaleToWorldWithOffset(vertex.y, town.townOffset.y); // MapMagic.Terrains.TerrainTile tileFound = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(MovedX, MovedY); try { var thing = TownGlobalObjectService.MapMagicObjectRef.tiles.FindByWorldPosition(vertex.x, vertex.y); Terrain ter = thing.ActiveTerrain; TerrainData terData = ter.terrainData; previousOffset = terData.GetHeight((int)MovedX, (int)MovedY) + 900; vertices.Add(new Vector3( MovedX, previousOffset, MovedY)); } catch // (Exception ex) { if (patch.IsCityCenter) { previousOffset = 0f; } // Debug.Log(ex.Message); vertices.Add(new Vector3( MovedX, previousOffset, MovedY)); } } float offset = 3f; Material mat; if (patch.HasCastle) { // offset = 3.4f; mat = rendererOptions.CastleGroundMaterial; } else if (patch.IsCityCenter) { // offset = 3.2f; mat = rendererOptions.CityCenterGround; } else if (patch.WithinWalls) { if (type == "Town.RichArea") { mat = rendererOptions.RichArea; } else if (type == "Town.PoorArea") { mat = rendererOptions.PoorArea; } else { mat = rendererOptions.WithinWallsGroundMaterial; } } else { if (!options.Farm && type == "Town.EmptyArea") { mat = rendererOptions.HiddenCity; } else if (options.Farm && type == "Town.FarmArea") { // mat = rendererOptions.FarmArea; mat = rendererOptions.HiddenCity; } else { mat = rendererOptions.OuterCityGroundMaterial; } } poly = new MeshUtils.Polygon(patch.Area.GetType().ToString(), vertices, offset, mat, overlays.transform, false); poly.GameObject.layer = LayerMask.NameToLayer(CamMapLayer); poly.Transform.localPosition = Vector3.zero; if (patch.IsCityCenter) { poly.GameObject.isStatic = false; poly.GameObject.AddComponent <LerpToGround>(); } vertices.Clear(); } }
private void DrawOverlay(TownGeometry geometry, StringBuilder sb) { MeshUtils.Polygon poly; List <Vector3> vertices = new List <Vector3> (); var overlays = new GameObject("Overlays"); overlays.transform.parent = child; overlays.transform.localPosition = Vector3.zero; foreach (var patch in geometry.Overlay) { if (patch.Water) { continue; } var type = patch.Area.ToString(); foreach (var vertex in patch.Shape.Vertices) { vertices.Add(new Vector3(vertex.x, -3f, vertex.y)); } float offset = 3f; Material mat; if (patch.HasCastle) { offset = 3.4f; mat = rendererOptions.CastleGroundMaterial; } else if (patch.IsCityCenter) { offset = 3.2f; mat = rendererOptions.CityCenterGround; } else if (patch.WithinWalls) { if (type == "Town.RichArea") { mat = rendererOptions.RichArea; } else if (type == "Town.PoorArea") { mat = rendererOptions.PoorArea; } else { mat = rendererOptions.WithinWallsGroundMaterial; } } else { if (type == "Town.FarmArea") { mat = rendererOptions.FarmArea; } else { mat = rendererOptions.OuterCityGroundMaterial; } } poly = new MeshUtils.Polygon("Patch", vertices, offset, mat, overlays.transform, true); poly.Transform.localPosition = Vector3.zero; vertices.Clear(); } }