Beispiel #1
0
    public static void GenerateGrass(MeshData meshData, Texture2D texture, MeshData grassColourMeshData, Texture2D grassColourTexture)
    {
        Vector3[] vertices  = meshData.vertices;
        Vector2[] uvs       = meshData.uvs;
        Vector2[] uvsColour = grassColourMeshData.uvs;

        float grassRotationBias = 50f;

        //GameObject MapGenerator = GameObject.Find("MapGenerator");
        //Color standard = MapGenerator.GetComponent<MapGenerator>().regions[2].colour;

        for (int i = 0; i < 5; i++)
        {
            for (int v = 0; v < vertices.Length; v++)
            {
                GrassMeshData grassMeshData = GrassMeshGenerator.GenerateGrassMesh(meshData, v, 0.4f, 0.8f, 0.2f, 2.0f, 0.25f);

                GameObject grass = new GameObject("GrassBlade");

                grass.transform.position = RandomizePosition(vertices[v]);

                Vector3 euler = grass.transform.eulerAngles;


                euler.y = RandomizeRotation(grassRotationBias);


                grass.transform.eulerAngles = euler;
                grass.transform.parent      = GameObject.Find("GrassContainer").transform;

                grass.AddComponent <MeshFilter>();
                grass.AddComponent <MeshRenderer>();
                grass.GetComponent <MeshFilter>().mesh = grassMeshData.CreateMesh();
                MeshRenderer rend = grass.GetComponent <MeshRenderer>();

                Material fieldMaterial = (Material)Resources.Load("Material/FieldTexture", typeof(Material));

                rend.material             = fieldMaterial;
                rend.material.mainTexture = SimulateLighting(fieldMaterial, uvs, v);

                Color standard       = texture.GetPixelBilinear(uvs[v].x, uvs[v].y);
                Color colourMapPixel = grassColourTexture.GetPixelBilinear(uvsColour[v].x, uvsColour[v].y);
                rend.material.color = RandomizeColor(standard, colourMapPixel);

                //Yellow
                // 169 188 34

                /*
                 * green 96 128 56
                 *
                 *
                 *
                 *
                 */
            }
        }
    }
    public static GrassMeshData GenerateGrassMesh(MeshData mesh, int vindex, float heightModifier, float heightIncreaseModifier, float leaningModifier, float leaningIncreaseModifier, float width)
    {
        GrassMeshData meshData = new GrassMeshData(7);

        Vector3 position = new Vector3(0f, 0f, 0f);

        float uvX            = 0f;
        float uvY            = 0f;
        float uvYFirstClimb  = 0.33f;
        float uvYSecondClimb = 0.66f;
        float uvYThirdClimb  = 1.0f;


        int vertexIndex = 0;

        meshData.vertices[vertexIndex] = new Vector3(position.x, position.y, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvY);
        vertexIndex++;
        meshData.vertices[vertexIndex] = new Vector3(position.x + width, position.y, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvY);


        vertexIndex++;
        float bending = position.x + leaningModifier;
        float height  = position.y + heightModifier;

        meshData.vertices[vertexIndex] = new Vector3(bending, height, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvYFirstClimb);
        vertexIndex++;
        meshData.vertices[vertexIndex] = new Vector3(bending + width / 1.5f, height, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvYFirstClimb);


        meshData.AddTriangle(vertexIndex - 3, vertexIndex - 2, vertexIndex - 1);
        meshData.AddTriangle(vertexIndex - 1, vertexIndex - 2, vertexIndex);
        meshData.AddTriangle(vertexIndex - 2, vertexIndex - 3, vertexIndex - 1);
        meshData.AddTriangle(vertexIndex - 2, vertexIndex - 1, vertexIndex);


        vertexIndex++;
        bending = position.x + leaningModifier * leaningIncreaseModifier;
        height  = position.y + heightModifier + heightModifier * heightIncreaseModifier;

        meshData.vertices[vertexIndex] = new Vector3(bending, height, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvYSecondClimb);
        vertexIndex++;
        meshData.vertices[vertexIndex] = new Vector3(bending + width / 2f, height, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvYSecondClimb);


        meshData.AddTriangle(vertexIndex - 3, vertexIndex - 2, vertexIndex - 1);
        meshData.AddTriangle(vertexIndex - 1, vertexIndex - 2, vertexIndex);
        meshData.AddTriangle(vertexIndex - 2, vertexIndex - 3, vertexIndex - 1);
        meshData.AddTriangle(vertexIndex - 2, vertexIndex - 1, vertexIndex);


        vertexIndex++;
        bending = position.x + leaningModifier * leaningIncreaseModifier * leaningIncreaseModifier;
        height  = position.y + heightModifier + heightModifier + heightModifier * heightIncreaseModifier * heightIncreaseModifier;

        meshData.vertices[vertexIndex] = new Vector3(bending, height, position.z);
        meshData.uvs[vertexIndex]      = new Vector2(uvX, uvYThirdClimb);

        meshData.AddTriangle(vertexIndex - 2, vertexIndex - 1, vertexIndex);
        meshData.AddTriangle(vertexIndex - 1, vertexIndex - 2, vertexIndex);

        return(meshData);
    }