ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Unity / C# > URP Pixel Perfect Camera 설정과 Cinemachine 2D 카메라 경계 가두기(Confiner2D)URP Pixel Perfect Camera Setup and Cinemachine 2D Camera Bounds (Confiner2D)
Unity / C# Intermediate Unity 2022.3+ / Unity 6

URP Pixel Perfect Camera 설정과 Cinemachine 2D 카메라 경계 가두기(Confiner2D)URP Pixel Perfect Camera Setup and Cinemachine 2D Camera Bounds (Confiner2D)

픽셀 아트 게임의 텍셀 왜곡과 깜빡임(Shimmering)을 방지하는 PPU 공식, Cinemachine Confiner2D를 활용한 맵 경계 밖 노출 방지 기법.Preventing texel distortion and shimmering in pixel art games using PPU formulas and restricting camera views within level bounds with Cinemachine Confiner2D.

#Unity #2D #Cinemachine #PixelPerfect #URP #Camera
핵심 개념레트로 픽셀 아트 게임에서 카메라 줌이나 이동 시 도트가 1.5픽셀로 찌그러지거나 대각선이 울퉁불퉁 튀는 현상은 비정수 스케일링 때문에 일어납니다. URP의 Pixel Perfect Camera 컴포넌트를 올바르게 구성하고, Cinemachine Confiner2D를 결합하여 카메라가 레벨 맵의 빈 공간을 비추지 않도록 제한하는 구조를 살펴봅니다.
Core ConceptIn retro pixel art games, distorted 1.5-pixel dots or jagged diagonal lines during camera movement and zooming occur due to non-integer scaling. This guide covers how to configure URP's Pixel Perfect Camera component and combine it with Cinemachine Confiner2D to keep the camera within the level boundaries.

1. Pixel Perfect Camera 핵심 설정 공식1. Pixel Perfect Camera Configuration

좌우로 밀어서 표 전체 내용을 확인하세요
옵션 항목권장값 (예: 320×180 타겟)설명 및 주의사항
Assets Pixels Per Unit (PPU)16 (또는 32)프로젝트 내 모든 2D 스프라이트의 PPU 설정값과 동일하게 일치해야 함.
Reference ResolutionX: 320, Y: 180 (16:9)게임이 기준으로 삼을 논리적 레트로 네이티브 해상도.
Upscale Render Texture체크 (True)저해상도로 먼저 렌더링한 후 정수 배율로 디스플레이에 업스케일.
Pixel Snapping체크 (True)스프라이트 렌더러들을 서브픽셀 위치가 아닌 가장 가까운 정수 텍셀에 스냅.
Crop Frame (X / Y)Pillarbox / Letterbox모니터 비율이 16:9가 아닐 때 픽셀 비율을 보존하기 위해 레터박스 생성.
Swipe horizontally to view the full table
OptionRecommended Value (e.g., 320×180 Target)Description and Notes
Assets Pixels Per Unit (PPU)16 (or 32)Must match the PPU setting of all 2D sprites in the project.
Reference ResolutionX: 320, Y: 180 (16:9)Logical retro native resolution used as the base reference.
Upscale Render TextureChecked (True)Renders at the low reference resolution first, then upscales by integer factors to the display.
Pixel SnappingChecked (True)Snaps sprite renderers to the nearest integer texel instead of sub-pixel positions.
Crop Frame (X / Y)Pillarbox / LetterboxAdds letterboxing or pillarboxing to preserve aspect ratio on non-16:9 displays.

2. Cinemachine Confiner2D 런타임 바운드 설정2. Cinemachine Confiner2D Runtime Bounds Setup

CameraConfinerSetup.cs
using UnityEngine;
using Unity.Cinemachine; // Cinemachine v3 (Unity 6) / 구버전은 Cinemachine

public class CameraConfinerSetup : MonoBehaviour
{
    [SerializeField] private CinemachineConfiner2D confiner;
    [SerializeField] private PolygonCollider2D mapBoundary;

    private void Start()
    {
        if (confiner != null && mapBoundary != null)
        {
            // 맵 경계 콜라이더 지정
            confiner.BoundingShape2D = mapBoundary;
            // 내부 버텍스 캐시 무효화 및 재계산
            confiner.InvalidateBoundingShapeCache();
        }
    }

    /// <summary>
    /// 스테이지 전환 시 새로운 룸의 경계로 카메라 가둠 영역 교체
    /// </summary>
    public void SwitchRoomBoundary(PolygonCollider2D newRoomBounds)
    {
        mapBoundary = newRoomBounds;
        confiner.BoundingShape2D = newRoomBounds;
        confiner.InvalidateBoundingShapeCache();
    }
}
CameraConfinerSetup.cs
using UnityEngine;
using Unity.Cinemachine; // Cinemachine v3 (Unity 6) / use Cinemachine in older versions

public class CameraConfinerSetup : MonoBehaviour
{
    [SerializeField] private CinemachineConfiner2D confiner;
    [SerializeField] private PolygonCollider2D mapBoundary;

    private void Start()
    {
        if (confiner != null && mapBoundary != null)
        {
            // Assign the map boundary collider
            confiner.BoundingShape2D = mapBoundary;
            // Invalidate and recalculate internal vertex cache
            confiner.InvalidateBoundingShapeCache();
        }
    }

    /// <summary>
    /// Replace the camera bounding shape when transitioning to a new room
    /// </summary>
    public void SwitchRoomBoundary(PolygonCollider2D newRoomBounds)
    {
        mapBoundary = newRoomBounds;
        confiner.BoundingShape2D = newRoomBounds;
        confiner.InvalidateBoundingShapeCache();
    }
}

3. 픽셀 아트 카메라 주의사항3. Pixel Art Camera Precautions

  • 스프라이트 안티앨리어싱 해제: 스프라이트 텍스처 임포트 시 Filter Mode를 Point (no filter)로 지정해야 바이리니어 블러 없이 선명한 도트가 유지됩니다.
  • 카메라 오소그래픽 사이즈 직접 수정 주의: Pixel Perfect Camera가 활성화되면 Camera.orthographicSize는 컴포넌트에 의해 매 프레임 자동으로 계산·고정되므로, 스크립트에서 사이즈를 직접 수정하면 화면 떨림이 발생할 수 있습니다.
  • Disable Sprite Anti-Aliasing: Set Filter Mode to Point (no filter) in the sprite texture import settings to keep crisp pixel edges without bilinear blur.
  • Avoid Modifying Orthographic Size Directly: When Pixel Perfect Camera is active, Camera.orthographicSize is automatically calculated and locked each frame by the component; modifying it via script can cause screen jitter.

스프라이트의 PPU 규격 통일과 Pixel Perfect Camera, Confiner2D 설정을 함께 적용하면 해상도 환경이 달라져도 일관된 픽셀 비율과 안정적인 카메라 경계를 유지할 수 있습니다.

Standardizing sprite PPU values alongside Pixel Perfect Camera and Confiner2D settings helps maintain consistent pixel proportions and stable camera bounds across different display resolutions.

참고자료 & 관련 링크