Unity Planet Generator颜色不起作用

时间:2019-01-11 16:31:37

标签: c# unity3d visual-studio-2017

我正在遵循本教程:https://www.youtube.com/watch?v=LyV7cEQyZMk为我的游戏生成一个星球。我已经起床到6:08

由于某种原因,行星的颜色不会根据其应有的类型而改变,只是白色。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum Type
{
    Water, Ice, Volcano, Snow, Grass, Forest, Desert, Rocky
}

[CreateAssetMenu()]
public class PlanetSettings : ScriptableObject
{
    public float Size;
    public Type type;
}

public class TerrainFace
{
    Mesh mesh;
    int resolution;
    Vector3 localUp;
    Vector3 axisA;
    Vector3 axisB;

    Vector3[] vertices;
    int[] triangles;

    PlanetSettings settings;

    Vector3 CalculatePointOnPlanet(Vector3 p)
    {
        return p * settings.Size;
    }

    public TerrainFace(Mesh mesh, int resolution, Vector3 localUp, PlanetSettings settings)
    {
        this.mesh = mesh;
        this.resolution = resolution;
        this.localUp = localUp;
        this.settings = settings;

        axisA = new Vector3(localUp.y, localUp.z, localUp.x);
        axisB = Vector3.Cross(localUp, axisA);
    }

    public void ConstructMesh()
    {
        vertices = new Vector3[resolution * resolution];
        triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int triIndex = 0;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int index = x + y * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
                vertices[index] = CalculatePointOnPlanet(pointOnUnitSphere);

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

                    triangles[triIndex + 3] = index;
                    triangles[triIndex + 4] = index + 1;
                    triangles[triIndex + 5] = index + resolution + 1;
                    triIndex += 6;
                }
            }
        }

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

public class PlanetGenerator : MonoBehaviour
{
    [Range(2, 256)]
    public int Resolution = 10;

    public PlanetSettings Settings;

    [SerializeField, HideInInspector]
    MeshFilter[] meshFilters;
    TerrainFace[] terrainFaces;

    Color c;

    private void OnValidate()
    {
        GeneratePlanet();
    }

    void Initialize()
    {
        switch (Settings.type)
        {
            case Type.Desert:
                c = new Color(244, 176, 66, 255);
                break;
            case Type.Forest:
                c = new Color(9, 119, 22, 255);
                break;
            case Type.Grass:
                c = new Color(21, 193, 41, 255);
                break;
            case Type.Ice:
                c = new Color(105, 206, 239, 255);
                break;
            case Type.Rocky:
                c = new Color(104, 65, 6, 255);
                break;
            case Type.Snow:
                c = new Color(204, 226, 229, 255);
                break;
            case Type.Volcano:
                c = new Color(160, 16, 16, 255);
                break;
            case Type.Water:
                c = new Color(26, 69, 178, 255);
                break;
            default:
                c = new Color();
                break;
        }

        if (meshFilters == null || meshFilters.Length == 0)
            meshFilters = new MeshFilter[6];
        terrainFaces = new TerrainFace[6];

        Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };

        for (int i = 0; i < 6; i++)
        {
            if (meshFilters[i] == null)
            {
                GameObject meshObj = new GameObject("mesh");
                meshObj.transform.parent = transform;

                meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard"));
                meshFilters[i] = meshObj.AddComponent<MeshFilter>();
                meshFilters[i].sharedMesh = new Mesh();
            }

            terrainFaces[i] = new TerrainFace(meshFilters[i].sharedMesh, Resolution, directions[i], Settings);
        }
    }

    public void GeneratePlanet()
    {
        Initialize();
        GenerateMesh();
        GenerateColor();
    }

    void GenerateMesh()
    {
        foreach (TerrainFace face in terrainFaces)
        {
            face.ConstructMesh();
        }
    }

    void GenerateColor()
    {
        foreach (MeshFilter m in meshFilters)
        {
            m.GetComponent<MeshRenderer>().sharedMaterial.color = c;
        }
    }
}

大小部分有效,只是颜色无效。我应该看到一个深红色的球体。我一直在尝试不同的方法来发现问题,我想我将其范围缩小到了sharedmaterial.color,但我不确定。

1 个答案:

答案 0 :(得分:0)

好吧,您没有他在5分钟左右更改了TerrainFace对象及其构造函数的代码。