
목표:
각 Root가 고유한 반경을 사용하여 고정된 중심 위치에서 꽃을 배치하되, 서로 겹치지 않게 배치한다.
작업 내용:
RootBase 클래스의 CalculateFlowerPositions 메서드를 수정하여, 각 Root가 고유한 반경을 가지도록 설정.
중심 위치를 -7.3, -0.66, -5로 고정하고, baseRadius 값을 조정하여 꽃이 중심에 더 가깝게 배치되도록 설정.
PlaceFlowers 메서드를 통해 꽃이 배치되는 방식 구현.
public virtual void CalculateFlowerPositions()
{
Vector3 terrainCenter = new Vector3(-7.3f, -0.66f, -5f); // 고정된 중심 위치
float baseRadius = brushSize / 6; // 기본 반경 (조금 더 가까운 거리)
float rootRadius = baseRadius * (Array.IndexOf(AutoObjectManager.Instance.roots, this) + 1); // 각 Root에 고유한 반경
int numPositions = 8; // 8개의 위치를 원형으로 배치
HashSet<Vector3> usedPositions = new HashSet<Vector3>(); // 사용된 위치를 저장하는 집합
for (int i = 0; i < numPositions; i++)
{
float angle = Mathf.Deg2Rad * (360f / numPositions * i); // 각도 계산
float x = terrainCenter.x + rootRadius * Mathf.Cos(angle);
float z = terrainCenter.z + rootRadius * Mathf.Sin(angle);
Vector3 newPosition = new Vector3(x, terrainCenter.y, z);
while (usedPositions.Contains(newPosition))
{
angle += Mathf.Deg2Rad * (360f / numPositions); // 각도를 변경하여 위치 재계산
x = terrainCenter.x + rootRadius * Mathf.Cos(angle);
z = terrainCenter.z + rootRadius * Mathf.Sin(angle);
newPosition = new Vector3(x, terrainCenter.y, z);
}
flowerPositions.Add(newPosition);
usedPositions.Add(newPosition); // 위치 저장
}
}
void PlaceFlowers(List<Vector3> flowerPositions, GameObject flowerPrefab, ref int currentFlowerIndex)
{
if (currentFlowerIndex >= flowerPositions.Count)
{
currentFlowerIndex = 0; // 인덱스 초기화
}
Vector3 flowerPosition = flowerPositions[currentFlowerIndex];
for (int i = 0; i < flowersPerPosition; i++)
{
float angle = UnityEngine.Random.Range(0f, 2f * Mathf.PI);
float radius = UnityEngine.Random.Range(0f, brushSize / 6);
Vector3 randomOffset = new Vector3(radius * Mathf.Cos(angle), 0, radius * Mathf.Sin(angle));
Vector3 finalPosition = flowerPosition + randomOffset;
float y = terrain.SampleHeight(finalPosition) + terrain.transform.position.y;
finalPosition.y = y;
GameObject newFlower = Instantiate(flowerPrefab, finalPosition, Quaternion.identity);
newFlower.transform.parent = terrain.transform;
Vector3 terrainNormal = terrain.terrainData.GetInterpolatedNormal(
(finalPosition.x - terrain.transform.position.x) / terrain.terrainData.size.x,
(finalPosition.z - terrain.transform.position.z) / terrain.terrainData.size.z
);
newFlower.transform.up = terrainNormal;
newFlower.isStatic = true;
foreach (var renderer in newFlower.GetComponentsInChildren<Renderer>())
{
if (renderer.sharedMaterial != null)
{
renderer.sharedMaterial.enableInstancing = true;
}
}
}
currentFlowerIndex++;
}
문제 1: 각 Root의 꽃이 같은 위치에서 생성됨
원인: 각 Root가 동일한 중심 위치와 각도를 사용하여 꽃을 배치하기 때문에 발생.
해결 방법: 각 Root에 고유한 반경을 적용하여 중심에서 서로 다른 반경에 꽃을 배치하도록 수정.
문제 2: 꽃이 중심에서 너무 멀리 생성됨
원인: baseRadius 값이 너무 크게 설정됨.
해결 방법: baseRadius 값을 줄여서 꽃이 중심에 더 가깝게 배치되도록 수정 (brushSize / 4 -> brushSize / 6).
오늘의 작업을 통해 각 Root가 고유한 반경을 사용하여 고정된 중심 위치에서 꽃을 배치하게 되었습니다. 이로 인해 꽃이 서로 겹치지 않으면서도 중심에 더 가깝게 배치되도록 수정되었습니다. 이를 통해 각 Root의 배치가 더 자연스럽고 효율적으로 이루어질 수 있게 되었습니다.