Beispiel #1
0
    public void Start()
    {
        // Create an icosahedron, subdivide it three times so that we have plenty of polys
        // to work with.

        InitAsIcosohedron();
        Subdivide(4);

        // 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.

        Color32 colorOcean     = new Color32(0, 80, 220, 0);
        Color32 colorGrass     = new Color32(0, 220, 0, 0);
        Color32 colorDirt      = new Color32(180, 140, 20, 0);
        Color32 colorDeepOcean = new Color32(0, 40, 110, 0);

        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.

        PolySet landPolys = new PolySet();
        PolySet sides;

        // 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);

            PolySet newLand = GetPolysInSphere(Random.onUnitSphere, continentSize, m_Polygons);

            landPolys.UnionWith(newLand);
        }

        m_landPolys = landPolys;

        // 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();

        foreach (Polygon poly in m_Polygons)
        {
            if (!landPolys.Contains(poly))
            {
                oceanPolys.Add(poly);
            }
        }

        m_oceanPolys = oceanPolys;

        // 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);

        sides = Inset(oceanSurface, 0.05f);
        sides.ApplyColor(colorOcean);
        sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f);

        if (m_OceanMesh != null)
        {
            Destroy(m_OceanMesh);
        }

        m_OceanMesh = GenerateMesh("Ocean Surface", m_OceanMaterial);

        // Back to land for a while! We start by making it green. =)

        //landPolys.ApplyRandomClanColors(colorBlueClan, colorRedClan);
        landPolys.ApplyColor(colorGrass);
        landPolys.ApplyTerritory(Territory.Neutral);
        // 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(landPolys, 0.05f);

        sides.ApplyColor(colorDirt);
        sides.ApplyTerritory(Territory.Cliff);

        sides.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();
         *
         * sides = Inset(hillPolys, 0.03f);
         * sides.ApplyRandomClanColors(colorBlueClan, colorRedClan);
         * sides.ApplyAmbientOcclusionTerm(0.0f, 1.0f);
         *
         * sides = Extrude(hillPolys, 0.05f);
         * sides.ApplyColor(colorDirt);
         *
         * //Hills have dark ambient occlusion on the bottom, and light on top.
         * sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f);
         */
        // Time to return to the oceans.

        sides = Extrude(oceanPolys, -0.02f);
        sides.ApplyColor(colorOcean);
        sides.ApplyTerritory(Territory.Ocean);
        sides.ApplyAmbientOcclusionTerm(0.0f, 1.0f);

        sides = Inset(oceanPolys, 0.02f);
        sides.ApplyColor(colorOcean);
        sides.ApplyTerritory(Territory.Ocean);
        sides.ApplyAmbientOcclusionTerm(1.0f, 0.0f);

        var deepOceanPolys = oceanPolys.RemoveEdges();

        sides = Extrude(deepOceanPolys, -0.05f);
        sides.ApplyColor(colorDeepOcean);
        sides.ApplyTerritory(Territory.Ocean);

        deepOceanPolys.ApplyColor(colorDeepOcean);
        deepOceanPolys.ApplyTerritory(Territory.Ocean);


        // Okay, we're done! Let's generate an actual game mesh for this planet.

        if (m_GroundMesh != null)
        {
            Destroy(m_GroundMesh);
        }

        m_GroundMesh = GenerateMesh("Ground Mesh", m_GroundMaterial);
        m_GroundMesh.AddComponent <ClickableTriangles>();
    }