Unity3D:如何修复“ TerrainData.SetHeights错误”

时间:2019-06-09 13:13:22

标签: c# unity3d terrain

我的地形有问题。 当我想要设置高度时,它可以工作,但是地形总是显示得比我用于该高度的2D数组大。在此示例中,我的数组和地形为200x157。那么,为什么我的显示器上有这个突出部分?

    using MoonSharp.Interpreter;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System;
using System.Text;
using System.IO;

[MoonSharpUserData]
public class PrefabCreator : MonoBehaviour
{

    /// <summary>
    /// Classmembers for PrefabCreator
    /// </summary>
    private int terrainDepth;
    private int terrainWidth;
    private int terrainHeight;
    private float terrainScale;
    private float maxHeight;


    public void CreateTerrain(int[][] mapData)
    {
        MapScale(mapData);
        Terrain terrain = Resources.Load<Terrain>("TerrainObject") as Terrain;
        Terrain prefabTerrain = Instantiate(terrain, new Vector3(0, 0, 0), Quaternion.identity);
        prefabTerrain.terrainData = GenerateTerrain(prefabTerrain.terrainData, mapData);

    }

    private void MapScale(int[][] mapData)
    {
        terrainWidth = 0;
        terrainHeight = 0;
        terrainDepth = 10;

        maxHeight = 0;

        bool firstRun = true;

        for (int i = 0; i < mapData.GetLength(0); i++)
        {
            terrainHeight++;
            for (int j = 0; j < mapData[i].Length; j++)
            {
                if(mapData[i][j] > maxHeight)
                {
                    maxHeight = mapData[i][j];
                }

                if (firstRun)
                {
                    terrainWidth++;
                }
            }
            firstRun = false;
        }
        Debug.Log("MAXHEIGHT: " + maxHeight);
    }

    private TerrainData GenerateTerrain(TerrainData terrainData, int[][] mapData)
    {

        if(terrainWidth >= terrainHeight)
        {

            terrainData.heightmapResolution = terrainWidth + 1;
        }
        else
        {
            terrainData.heightmapResolution = terrainHeight + 1;
        }
        terrainData.size = new Vector3(terrainWidth, terrainDepth, terrainHeight);

        Debug.Log("Test" + terrainData.heightmapHeight);
        Debug.Log("Test" + terrainData.heightmapWidth);
        Debug.Log("Test" + terrainData.heightmapScale);
        terrainData.SetHeights(0, 0, GenerateHeights(mapData));

        return terrainData;
    }

    private float[,] GenerateHeights(int[][] mapData)
    {
        Debug.Log("Breite " + terrainWidth);
        Debug.Log("Höhe " + terrainHeight);



        float[,] heights = new float[terrainHeight, terrainWidth];

        //Debug.Log("Test" + mapData[2][4]);
        for (int x = 0; x <= terrainHeight - 1; x++)
        {
            //Debug.Log("X = " + x);
            for (int y = 0; y <= terrainWidth - 1; y++)
            {
                //Debug.Log("Y = " + y);
                float value = 0.0f + mapData[x][y];
                heights[x, y] = (value / maxHeight);
                //heights[x, y] = (float)value ;
                Debug.Log(heights[x, y]);
            }
        }

        return heights;
    }





}

请参阅附录中的图片。我不明白为什么突出部分在边缘?

在此先感谢人们

Unity show

0 个答案:

没有答案