반응형
- 코드 작성
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
public int width = 256;
public int height = 256;
public float scale = 20.0f;
public int octaves = 3;
public float persistence = 0.5f;
public float lacunarity = 2.0f;
public int seed = 0;
private void Start()
{
Terrain terrain = GetComponent<Terrain>();
terrain.terrainData = GenerateTerrain(terrain.terrainData);
}
private TerrainData GenerateTerrain(TerrainData terrainData)
{
terrainData.heightmapResolution = width + 1;
terrainData.size = new Vector3(width, 600, height);
terrainData.SetHeights(0, 0, GenerateHeights());
return terrainData;
}
private float[,] GenerateHeights()
{
float[,] heights = new float[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
heights[x, y] = CalculateHeight(x, y);
}
}
return heights;
}
private float CalculateHeight(int x, int y)
{
float amplitude = 1;
float frequency = 1;
float height = 0;
for (int i = 0; i < octaves; i++)
{
float xCoord = (float)x / width * scale * frequency + seed;
float yCoord = (float)y / height * scale * frequency + seed;
float noise = Mathf.PerlinNoise(xCoord, yCoord);
height += noise * amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return height;
}
}
- width: 지형의 너비
- height: 지형의 높이
- scale: Perlin noise의 크기
- octaves: Perlin noise의 옥타브 수
- persistence: 옥타브별 진폭 감소율
- lacunarity: 옥타브별 주파수 증가율
- seed: Perlin noise의 시드값
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 랭킹 시스템 간단 구현 Unity Rankings (0) | 2023.06.07 |
---|---|
유니티 C# 스태미너 효과 간단 구현 Stamina (0) | 2023.05.18 |
유니티 C# UI로 데미지 출력하기 간단 구현 Damage (0) | 2023.05.18 |
유니티 MVVM 패턴 간단 구현 (0) | 2023.04.23 |
유니티 C# 안드로이드 빌드 후 리소스 텍스트 파일 접근 방법 (0) | 2023.04.23 |
유니티 C# 3D 캐릭터 이동, 점프 간단 구현 (0) | 2023.04.03 |
유니티 C# 두번 터치로 카메라 줌인 간단 구현 Camera Zoom in (0) | 2023.04.02 |
유니티 C# 화면 캡처하기 간단 구현 Screen Capture (0) | 2023.04.01 |
댓글