Ejemplo n.º 1
0
    public void Construct(PlanetShapeGenerator shapeGenerator, PlanetColorGenerator colorGenerator)
    {
        List <Vector3> vertices = new List <Vector3>(mResolution * mResolution);
        List <Vector2> uvs      = new List <Vector2>(mResolution * mResolution);
        List <int>     indices  = new List <int>(mResolution * mResolution * 6);

        Vector3 right   = new Vector3(mUp.y, mUp.z, mUp.x);
        Vector3 forward = Vector3.Cross(right, mUp);

        for (int y = 0; y < mResolution; ++y)
        {
            for (int x = 0; x < mResolution; ++x)
            {
                Vector2 percent       = new Vector2((float)x / (mResolution - 1), (float)y / (mResolution - 1));
                Vector3 pointOnCube   = mUp + (percent.y - 0.5f) * 2f * forward + (percent.x - 0.5f) * 2f * right;
                Vector3 pointOnCircle = pointOnCube.normalized;

                float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnCircle);
                vertices.Add(pointOnCircle * shapeGenerator.GetScaledElevation(unscaledElevation));
                uvs.Add(new Vector2(colorGenerator.CalculateBiomeOnPlanet(pointOnCircle), unscaledElevation));
            }
        }

        for (int y = 0; y < mResolution - 1; ++y)
        {
            for (int x = 0; x < mResolution - 1; ++x)
            {
                int index = x + y * (mResolution);

                indices.Add(index);
                indices.Add(index + mResolution);
                indices.Add(index + 1);

                indices.Add(index + mResolution + 1);
                indices.Add(index + 1);
                indices.Add(index + mResolution);
            }
        }

        mMesh.Clear();
        mMesh.SetVertices(vertices);
        mMesh.SetUVs(0, uvs);
        mMesh.SetTriangles(indices, 0);
        mMesh.RecalculateNormals();
        mMesh.RecalculateTangents();
        mMesh.UploadMeshData(false);

        colorGenerator.SetMinMaxValue(shapeGenerator.VecMinMax);
    }
Ejemplo n.º 2
0
    public void ConstructMesh()
    {
        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution;
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - 0.5f) * 2 * xAxis + (percent.y - 0.5f) * 2 * zAxis;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                float   unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere);
                vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation);
                uv[i].y     = unscaledElevation;


                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        mesh.uv = uv;
    }