コード例 #1
0
    /// <summary>
    /// Change the height of the given vertex and its neighbors with the gaussian bell algroithm
    /// </summary>
    /// <param name="vertexToChange">index of the vertex to change</param>
    /// <param name="heightFactor">factor to change the height</param>
    private void useGaussianBell(int indexOfVertexToChange, float heightFactor)
    {
        int totalSize = DiamondSquareGenerator.getTotalSize(terrain.size);

        float widthFactor = 0.02f;

        Vector3[] tempVertices = terrain.tempVertices;
        Vector3[] vertices     = new Vector3[tempVertices.Length];
        float[,] diffHeights = terrain.tempDiffHeights;
        Color[] colors = new Color[vertices.Length];

        Vector3 vertexToChange = tempVertices[indexOfVertexToChange];

        for (int index = 0, z = 0; z < totalSize; z++)
        {
            for (int x = 0; x < totalSize; x++)
            {
                Vector3 vertex = tempVertices[index];
                Vector3 diff   = vertex - vertexToChange;

                // calculates the distance in x and z direction to between the two vertices
                float dist = Mathf.Sqrt(Mathf.Pow(diff.x, 2) + Mathf.Pow(diff.z, 2));

                // calculate the height for the current vertex with the gaussian bell algorithm
                float diffHeight = Mathf.Exp(-Mathf.Pow(widthFactor * dist, 2)) * heightFactor;
                diffHeights[x, z] += diffHeight;

                float height = vertex.y + diffHeights[x, z];
                vertex.y        = height;
                vertices[index] = vertex;
                colors[index]   = terrain.getColorFromHeight(height);

                index++;
            }
        }

        terrain.tempDiffHeights = diffHeights;
        terrain.updateMesh(vertices, colors);
    }