개발의 시작 TIL(Today I Learned)

오늘의 TIL (Today I Learned) 포스트 프로세싱(Post-Processing)

human-Novice 2024. 12. 26. 20:49

Title: 오늘의 TIL (Today I Learned)

포스트 프로세싱(Post-Processing)

배운 내용:

  1. 포스트 프로세싱의 개념:
    • 렌더링이 끝난 후 화면에 추가적인 시각 효과를 적용하는 기술.
    • 게임의 비주얼을 더욱 풍부하고 현실감 있게 만드는 데 사용됨.
  2. 주요 포스트 프로세싱 효과:
    • Bloom: 밝은 부분을 빛나게 하는 효과.
    • Depth of Field: 특정 초점 거리에 있는 대상만 선명하게 보이게 하는 효과.
    • Motion Blur: 빠르게 움직이는 대상에 흐릿한 효과를 주는 기술.
    • Ambient Occlusion: 주변 환경에 따라 그림자를 더해 입체감을 높이는 기법.
    • Color Grading: 색상을 조정하여 전체적인 분위기를 조절하는 작업.
    • Lens Distortion: 렌즈 왜곡 효과로, 카메라 렌즈를 통한 비틀림이나 곡률 효과를 추가.
    • Film Grain: 필름의 입자를 추가하여, 오래된 영화나 특유의 질감을 표현.

적용 과정:

  1. 유니티 프로젝트에 포스트 프로세싱 패키지 추가:
    • Package Manager를 통해 포스트 프로세싱 패키지 설치.
  2. 포스트 프로세싱 볼륨 설정:
    • 게임 오브젝트에 Post-Processing Volume 컴포넌트 추가.
    • 원하는 효과들을 Volume Profile에 추가하고 설정.

결과:

  • 게임의 비주얼 퀄리티가 향상됨.
  • 다양한 포스트 프로세싱 효과를 적용하여 게임의 분위기와 몰입감을 높임.
 
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class GlobalVolumeController : MonoBehaviour
{
    public GameObject globalVolumeObject;
    private Volume volume;
    private Dictionary<System.Type, VolumeComponent> components = new Dictionary<System.Type, VolumeComponent>();

    void Start()
    {
        if (globalVolumeObject.TryGetComponent<Volume>(out volume))
        {
            var profile = volume.profile;

            // VolumeProfile의 모든 컴포넌트 가져오기
            foreach (var component in profile.components)
            {
                components[component.GetType()] = component;
            }
        }
    }

    // 특정 효과 가져오기
    public T GetEffect<T>() where T : VolumeComponent
    {
        if (components.TryGetValue(typeof(T), out var component))
        {
            return component as T;
        }
        return null;
    }

    // 효과의 속성 변경
    public void SetVignetteIntensity(float intensity)   // 화면 가장 자리를 어둡게 해주는 효과
    {
        var vignette = GetEffect<Vignette>();
        if (vignette != null)
        {
            vignette.intensity.value = intensity;
        }
    }
    public void SetLensDistortionIntensity(float intensity)     // 시야를 렌즈를 쓴것 처럼 굴절 효과
    {
        var lensDistortion = GetEffect<LensDistortion>();
        if (lensDistortion != null)
        {
            lensDistortion.intensity.value = intensity;
        }
    }
    public void SetFilmGrainIntensity(float intensity)     // 시야에 노이즈 발생 효과
    {
        var lensDistortion = GetEffect<FilmGrain>();
        if (lensDistortion != null)
        {
            lensDistortion.intensity.value = intensity;
        }
    }
}

글로벌 볼륨 오브젝트를 씬에 추가함으로써 포스트 프로세싱을 사용할 수 있게 되었지만, 코드 상으로 유동적으로 조작하는 부분이 필요했습니다. 각 효과를 따로 받아와야 했기 때문에, 볼륨의 컴포넌트를 가져와 딕셔너리에 효과들을 저장하고, 각 효과의 속성에 접근하는 메서드를 만들어서 사용하고자 했습니다.