Unity에서 라이트 프로브 스크립팅하기
Unity의 라이트 프로브를 자동으로 배치하는 스크립트를 작성하는 방법에 대해 설명합니다. 이를 통해 대규모 환경에서도 효율적으로 라이트 프로브를 위치시킬 수 있습니다.
라이트 프로브란?
라이트 프로브는 씬의 조명을 캡처하여 정적 오브젝트가 옳은 조명을 받을 수 있도록 도와주는 개념입니다. 스크립팅을 통해 라이트 프로브의 배치를 자동화하면 시간과 노력을 절약할 수 있습니다.
스크립트 개요
아래의 C# 스크립트는 라이트 프로브 그룹을 생성하고 특정 규칙에 따라 프로브 위치를 결정합니다.
스크립트 예제
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(LightProbeGroup))]
public class LightProbesTetrahedralGrid : MonoBehaviour
{
public float m_Side = 1.0f;
public float m_Radius = 5.0f;
public float m_InnerRadius = 0.1f;
public float m_Height = 2.0f;
public uint m_Levels = 3;
const float kMinSide = 0.05f;
const float kMinHeight = 0.05f;
const float kMinInnerRadius = 0.1f;
const uint kMinIterations = 4;
public void OnValidate()
{
m_Side = Mathf.Max(kMinSide, m_Side);
m_Height = Mathf.Max(kMinHeight, m_Height);
if (m_InnerRadius < kMinInnerRadius)
{
TriangleProps props = new TriangleProps(m_Side);
m_Radius = Mathf.Max(props.circumscribedCircleRadius + 0.01f, m_Radius);
}
else
{
m_Radius = Mathf.Max(0.1f, m_Radius);
m_InnerRadius = Mathf.Min(m_Radius, m_InnerRadius);
}
}
struct TriangleProps
{
public TriangleProps(float triangleSide)
{
side = triangleSide;
halfSide = side / 2.0f;
height = Mathf.Sqrt(3.0f) * side / 2.0f;
inscribedCircleRadius = Mathf.Sqrt(3.0f) * side / 6.0f;
circumscribedCircleRadius = 2.0f * height / 3.0f;
}
public float side;
public float halfSide;
public float height;
public float inscribedCircleRadius;
public float circumscribedCircleRadius;
};
public void Generate()
{
LightProbeGroup lightProbeGroup = GetComponent<LightProbeGroup>();
List<Vector3> positions = new List<Vector3>();
TriangleProps m_TriangleProps = new TriangleProps(m_Side);
if (m_InnerRadius < kMinInnerRadius)
GenerateCylinder(m_TriangleProps, m_Radius, m_Height, m_Levels, positions);
else
GenerateRing(m_TriangleProps, m_Radius, m_InnerRadius, m_Height, m_Levels, positions);
lightProbeGroup.probePositions = positions.ToArray();
}
// Cylinder 및 Ring 생성 메소드 생략...
}
주요 구조 설명
- TriangleProps 구조체: 삼각형 관련 속성을 계산합니다.
- Generate() 메소드: 라이트 프로브의 위치를 생성합니다. 원통형 또는 링 모양으로 프로브를 배치할 수 있습니다.
- OnValidate() 메소드: 변수의 유효성을 검사하여 잘못된 값이 입력되지 않도록 합니다.
활용 예제
- 모든 방향으로 프로브 배치: Generate() 메소드에서
GenerateCylinder()
와GenerateRing()
메소드를 호출하여 프로브를 원통이나 링 형태로 배치합니다. - 프로브의 반지름 조정: m_Radius와 m_InnerRadius 값을 조정하여 프로브가 배치되는 범위를 제어할 수 있습니다.
- 테스트 및 디버깅: Unity 에디터에서 OnValidate 메소드를 사용하여 각 변수를 실시간으로 조정하고 적용해 볼 수 있습니다.
라이트 프로브 배치 예제
예제 이름 | 설명 | 주요 설정 |
---|---|---|
프로브 원통형 배치 | 원통 내에 프로브 배치 | m_Radius, m_Height, m_Levels 설정 |
프로브 링형 배치 | 링 형태로 프로브 배치 | m_Radius, m_InnerRadius, m_Levels 설정 |
결론
스크립팅을 통해 라이트 프로브를 보다 쉽고 효과적으로 배치할 수 있습니다. 필요에 따라 스크립트를 수정하여 특정 요구에 맞춰 최적화할 수 있습니다. 이러한 기능은 대규모 씬에서 조명을 효율적으로 관리하는 데 매우 유용합니다.