Jump to content
Unity Insider Forum

TerrainData.SetHeights Fehler


Kerugal

Recommended Posts

Hallo zusammen,

ich habe ein Problem mit meinem Gelände und hoffe ihr könnt mir helfen. Wenn ich die Höhen einstellen möchte, funktioniert dies, aber das Gelände wird immer größer angezeigt als das 2D-Array, das ich für die Höhe verwende. In diesem Beispiel sind mein Array und mein Terrain 200x157. Warum habe ich dann diesen Überstand in meiner Darstellung? (Siehe Anhang Bild)

Ich hoffe sehr, dass mir jemand weiterhelfen kann...bin ein wenig am verzweifeln...

    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;
    }





}

 

ArrayTerrain.PNG

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...