private static void GenerateHills(ICollection <Polygon> polygons, IList <Vector3> vertices, PolySet landPolys, Color32 landColor, Color32 sideColor) { // Grab additional polygons to generate hills, but only from the set of polygons that are land. PolySet hillPolys = PolySet.RemoveEdges(landPolys); PolySet insetSides = Inset(polygons, vertices, hillPolys, 0.03f); PolySet.ApplyColor(insetSides, landColor); insetSides.ApplyAmbientOcclusionTerm(0.0f, 1.0f); PolySet extrudeSides = Extrude(polygons, vertices, hillPolys, 0.05f); PolySet.ApplyColor(extrudeSides, sideColor); //Hills have dark ambient occlusion on the bottom, and light on top. extrudeSides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); }
public PlanetGenerator(GameObject _planet, Material m_GroundMaterial, Material m_OceanMaterial, bool withRocket) { planet = _planet; if (!withRocket) { colorGrass = new Color32(99, 61, 72, 0); colorDirt = new Color32(59, 30, 39, 0); colorOcean = new Color32(26, 67, 92, 0); colorDeepOcean = new Color32(11, 34, 48, 0); } landPolys = new PolySet(); m_Objects = new List <Polygon>(); // Create an icosahedron, subdivide it three times so that we have plenty of polys // to work with. InitAsIcosohedron(); Subdivide(3); // When we begin extruding polygons, we'll need each one to know who its immediate //neighbors are. Calculate that now. CalculateNeighbors(); // By default, everything is colored blue. As we extrude land forms, we'll change their colors to match. foreach (Polygon p in m_Polygons) { p.m_Color = colorOcean; } // Now we build a set of Polygons that will become the land. We do this by generating // randomly sized spheres on the surface of the planet, and adding any Polygon that falls // inside that sphere. // Grab polygons that are inside random spheres. These will be the basis of our planet's continents. for (int i = 0; i < m_NumberOfContinents; i++) { float continentSize = Random.Range(m_ContinentSizeMin, m_ContinentSizeMax); Vector3 center = Random.onUnitSphere; if (i == 0) { center.Set(-1, 0, 0); } PolySet newLand = GetPolysInSphere(center, continentSize, m_Polygons); landPolys.UnionWith(newLand); } // While we're here, let's make a group of oceanPolys. It's pretty simple: Any Polygon that isn't in the landPolys set // must be in the oceanPolys set instead. var oceanPolys = new PolySet(); surfacePolys = new List <Polygon>(); foreach (Polygon poly in m_Polygons) { if (!landPolys.Contains(poly)) { oceanPolys.Add(poly); } else { surfacePolys.Add(poly); } } // Let's create the ocean surface as a separate mesh. // First, let's make a copy of the oceanPolys so we can // still use them to also make the ocean floor later. // Back to land for a while! We start by making it green. =) foreach (Polygon landPoly in landPolys) { landPoly.m_Color = colorGrass; } // The Extrude function will raise the land Polygons up out of the water. // It also generates a strip of new Polygons to connect the newly raised land // back down to the water level. We can color this vertical strip of land brown like dirt. PolySet lowLandSides = Extrude(landPolys, 0.05f); lowLandSides.ApplyColor(colorDirt); lowLandSides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); // Grab additional polygons to generate hills, but only from the set of polygons that are land. PolySet hillPolys = landPolys.RemoveEdges(); PolySet insetLandPolys = Inset(hillPolys, 0.03f); insetLandPolys.ApplyColor(colorGrass); insetLandPolys.ApplyAmbientOcclusionTerm(0.0f, 1.0f); PolySet topLandSides = Extrude(hillPolys, 0.05f); topLandSides.ApplyColor(colorDirt); //Hills have dark ambient occlusion on the bottom, and light on top. topLandSides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); landPolys.UnionWith(lowLandSides); landPolys.UnionWith(insetLandPolys); landPolys.UnionWith(topLandSides); // Time to return to the oceans. PolySet insetOceanPolys = Inset(oceanPolys, 0.05f); insetOceanPolys.ApplyColor(colorOcean); insetOceanPolys.ApplyAmbientOcclusionTerm(1.0f, 0.0f); PolySet highOceanSides = Extrude(oceanPolys, -0.02f); highOceanSides.ApplyColor(colorOcean); highOceanSides.ApplyAmbientOcclusionTerm(0.0f, 1.0f); PolySet insetOceanPolys2 = Inset(oceanPolys, 0.02f); insetOceanPolys2.ApplyColor(colorOcean); insetOceanPolys2.ApplyAmbientOcclusionTerm(1.0f, 0.0f); var deepOceanPolys = oceanPolys.RemoveEdges(); PolySet lowOceanSides = Extrude(deepOceanPolys, -0.05f); lowOceanSides.ApplyColor(colorDeepOcean); deepOceanPolys.ApplyColor(colorDeepOcean); oceanPolys.UnionWith(insetOceanPolys); oceanPolys.UnionWith(highOceanSides); oceanPolys.UnionWith(insetOceanPolys2); oceanPolys.UnionWith(lowOceanSides); PolySet allPolys = new PolySet(oceanPolys); allPolys.UnionWith(landPolys); // Okay, we're done! Let's generate an actual game mesh for this planet. for (int i = 0; i < m_Vertices.Count; i++) { m_Vertices[i] *= 0.1f; m_Vertices[i] += planet.transform.position; } //GenerateMesh(planet, oceanPolys, "Ocean Mesh", m_OceanMaterial); GenerateMesh(planet, allPolys, "Ground Mesh", m_GroundMaterial); }
private static Mesh GetGroundMesh(IList <Polygon> polygons, IList <Vector3> vertices, float continentSizeMin, float continentSizeMax, int numberOfContinents, Color32 landColor, Color32 sideColor, Color32 oceanColor, Color32 deepOceanColor, bool isGenerateHills, bool isGenerateDeepOceans) { // Now we build a set of Polygons that will become the land. We do this by generating // randomly sized spheres on the surface of the planet, and adding any Polygon that falls // inside that sphere. PolySet landPolys = CreateLand(polygons, vertices, continentSizeMin, continentSizeMax, numberOfContinents); // While we're here, let's make a group of oceanPolys. It's pretty simple: Any Polygon that isn't in the landPolys set // must be in the oceanPolys set instead. PolySet oceanPolys = CreateOcean(polygons, landPolys); // Let's create the ocean surface as a separate mesh. // First, let's make a copy of the oceanPolys so we can // still use them to also make the ocean floor later. var oceanSurface = new PolySet(oceanPolys); PolySet sides = Inset(polygons, vertices, oceanSurface, 0.05f); PolySet.ApplyColor(sides, oceanColor); sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); // Time to return to the oceans. sides = Extrude(polygons, vertices, oceanPolys, -0.02f); PolySet.ApplyColor(sides, oceanColor); sides.ApplyAmbientOcclusionTerm(0.0f, 1.0f); sides = Inset(polygons, vertices, oceanPolys, 0.02f); PolySet.ApplyColor(sides, oceanColor); sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); if (isGenerateDeepOceans) { var deepOceanPolys = PolySet.RemoveEdges(oceanPolys); sides = Extrude(polygons, vertices, deepOceanPolys, -0.05f); PolySet.ApplyColor(sides, deepOceanColor); PolySet.ApplyColor(deepOceanPolys, deepOceanColor); } // Back to land for a while! We start by making it green. =) PolySet.ApplyColor(landPolys, landColor); // The Extrude function will raise the land Polygons up out of the water. // It also generates a strip of new Polygons to connect the newly raised land // back down to the water level. We can color this vertical strip of land brown like dirt. sides = Extrude(polygons, vertices, landPolys, 0.05f); PolySet.ApplyColor(sides, sideColor); sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f); if (isGenerateHills) { GenerateHills(polygons, vertices, landPolys, landColor, sideColor); } return(GenerateMesh("Ground Mesh", polygons, vertices)); }