본문 바로가기
개발/C#

유니티 C# 지형 자동 생성 Terrain 간단 구현

by SPNK 2023. 3. 31.
반응형
  • 코드 작성
using UnityEngine;

public class AutoTerrainExample : MonoBehaviour
{
    [SerializeField] private TerrainData terrainData;
    [SerializeField] private int resolution = 256;
    [SerializeField] private float scale = 10f;
    [SerializeField] private float heightScale = 5f;

    private void Start()
    {
        terrainData.heightmapResolution = resolution;
        terrainData.size = new Vector3(resolution, heightScale, resolution);
        float[,] heights = new float[resolution, resolution];

        for (int x = 0; x < resolution; x++)
        {
            for (int y = 0; y < resolution; y++)
            {
                float perlin = Mathf.PerlinNoise((float)x / resolution * scale, (float)y / resolution * scale);
                heights[x, y] = perlin * heightScale;
            }
        }

        terrainData.SetHeights(0, 0, heights);
    }
}
반응형

댓글