Shader / URP
Advanced
Unity 2022.3+ / Unity 6 (URP)
3D 픽셀 아트 카메라의 픽셀 떨림(Pixel Creep) 방지와 서브픽셀 스냅(Texel Snapping)Preventing Pixel Creep with Subpixel Camera Texel Snapping
저해상도 픽셀 렌더링에서 카메라 이동 시 도트 외곽선이 울렁이는 Shimmering 문제를 월드 텍셀 그리드 스냅과 디스플레이 UV 미세 오프셋으로 해결하는 핵심 기법.Resolving pixel shimmering during camera movement in low-resolution 3D pixel art rendering by snapping the camera to the world texel grid and applying a subpixel UV offset on blit.
#Shader #URP #PixelArt #TexelSnapping #CameraSmoothing #t3ssel8r
핵심 개념3D를 저해상도 RenderTexture로 렌더링할 때 흔히 겪는 문제는 '픽셀 기어오름(Pixel Creep / Shimmering)'입니다. 카메라가 서브픽셀(예: 0.1픽셀) 단위로 이동하면 래스터라이저가 3D 기하학을 샘플링하는 위치가 매 프레임 달라져 픽셀 경계가 자글거리게 됩니다. 이를 해결하기 위해 카메라 월드 위치는 정확한 텍셀 단위로 절삭(Snap)하고, 잘려 나간 소수점 오프셋만큼 최종 화면의 UV를 반대로 미세 이동시키는 것이 *t3ssel8r*가 정리한 핵심 기법입니다.
Core ConceptA common artifact when rendering 3D scenes into a low-resolution RenderTexture is Pixel Creep (Shimmering). When the camera moves by fractional pixel increments (e.g., 0.1 pixels), the rasterizer samples 3D geometry at shifting positions each frame, causing pixel edges to Crawl. As popularized by *t3ssel8r*, the core technique is to snap the camera's world position to exact texel increments while shifting the final display UV by the discarded fractional offset.
1. 텍셀 스냅 + 서브픽셀 오프셋 카메라 스크립트1. Texel-Snapped + Subpixel Offset Camera Script
using UnityEngine;
using UnityEngine.UI;
public class TexelSnappedCamera : MonoBehaviour
{
[SerializeField] private Transform followTarget;
[SerializeField] private Material fullScreenBlitMaterial;
[SerializeField] private float pixelsPerUnit = 16f;
private Camera _cam;
private static readonly int SubpixelOffsetProperty = Shader.PropertyToID("_SubpixelOffset");
private void Awake()
{
_cam = GetComponent<Camera>();
}
private void LateUpdate()
{
if (followTarget == null) return;
Vector3 realPosition = followTarget.position;
float unitsPerPixel = 1f / pixelsPerUnit;
// 1. 카메라의 실제 목표 위치를 텍셀 그리드로 스냅(절삭)
float snappedX = Mathf.Floor(realPosition.x / unitsPerPixel) * unitsPerPixel;
float snappedY = Mathf.Floor(realPosition.y / unitsPerPixel) * unitsPerPixel;
// 2. 절삭 과정에서 버려진 서브픽셀(0.0 ~ 1.0 텍셀) 오프셋 산출
float subpixelX = (realPosition.x - snappedX) / unitsPerPixel;
float subpixelY = (realPosition.y - snappedY) / unitsPerPixel;
// 3. 카메라는 도트 형태가 깨지지 않도록 스냅된 위치에 배치
transform.position = new Vector3(snappedX, snappedY, transform.position.z);
// 4. 잘려 나간 소수점 오프셋을 최종 풀스크린 셰이더로 전달
if (fullScreenBlitMaterial != null)
{
fullScreenBlitMaterial.SetVector(SubpixelOffsetProperty, new Vector4(subpixelX, subpixelY, 0, 0));
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class TexelSnappedCamera : MonoBehaviour
{
[SerializeField] private Transform followTarget;
[SerializeField] private Material fullScreenBlitMaterial;
[SerializeField] private float pixelsPerUnit = 16f;
private Camera _cam;
private static readonly int SubpixelOffsetProperty = Shader.PropertyToID("_SubpixelOffset");
private void Awake()
{
_cam = GetComponent<Camera>();
}
private void LateUpdate()
{
if (followTarget == null) return;
Vector3 realPosition = followTarget.position;
float unitsPerPixel = 1f / pixelsPerUnit;
// 1. Snap the camera target position to the world texel grid
float snappedX = Mathf.Floor(realPosition.x / unitsPerPixel) * unitsPerPixel;
float snappedY = Mathf.Floor(realPosition.y / unitsPerPixel) * unitsPerPixel;
// 2. Calculate the discarded fractional subpixel offset (0.0 to 1.0 texels)
float subpixelX = (realPosition.x - snappedX) / unitsPerPixel;
float subpixelY = (realPosition.y - snappedY) / unitsPerPixel;
// 3. Place the camera at the snapped position to keep pixel shapes stable
transform.position = new Vector3(snappedX, snappedY, transform.position.z);
// 4. Pass the fractional offset to the full-screen blit shader
if (fullScreenBlitMaterial != null)
{
fullScreenBlitMaterial.SetVector(SubpixelOffsetProperty, new Vector4(subpixelX, subpixelY, 0, 0));
}
}
}
2. 서브픽셀 UV 오프셋 보정 풀스크린 셰이더2. Subpixel UV Offset Compensation Full-Screen Shader
Shader "Custom/SubpixelBlit"
{
Properties
{
_MainTex ("Source Low-Res Texture", 2D) = "white" {}
_SubpixelOffset ("Subpixel Offset (X, Y)", Vector) = (0, 0, 0, 0)
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
ZWrite Off ZTest Always Cull Off
Pass
{
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex_Point);
float4 _MainTex_TexelSize;
float4 _SubpixelOffset;
Varyings Vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.uv = input.uv;
return output;
}
half4 Frag(Varyings input) : SV_Target
{
// 소수점 오프셋만큼 UV를 이동시켜 서브픽셀 스크롤 보정
float2 uvOffset = _SubpixelOffset.xy * _MainTex_TexelSize.xy;
float2 smoothedUV = input.uv + uvOffset;
// 포인트 필터링된 저해상도 도트 컬러 샘플링
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex_Point, smoothedUV);
return col;
}
ENDHLSL
}
}
}
Shader "Custom/SubpixelBlit"
{
Properties
{
_MainTex ("Source Low-Res Texture", 2D) = "white" {}
_SubpixelOffset ("Subpixel Offset (X, Y)", Vector) = (0, 0, 0, 0)
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
ZWrite Off ZTest Always Cull Off
Pass
{
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex_Point);
float4 _MainTex_TexelSize;
float4 _SubpixelOffset;
Varyings Vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.uv = input.uv;
return output;
}
half4 Frag(Varyings input) : SV_Target
{
// Shift UVs by the fractional offset for smooth subpixel scrolling
float2 uvOffset = _SubpixelOffset.xy * _MainTex_TexelSize.xy;
float2 smoothedUV = input.uv + uvOffset;
// Sample point-filtered low-resolution pixel color
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex_Point, smoothedUV);
return col;
}
ENDHLSL
}
}
}
3. 결과 및 비교3. Results and Comparison
- 스냅 미적용: 카메라 이동 시 3D 모델의 1픽셀 외곽선이나 세부 형태가 매 프레임 굵어졌다 얇아지는 Shimmering 현상이 발생합니다.
- 단순 위치 스냅만 적용: 외곽선 왜곡은 줄어들지만, 카메라가 1텍셀(
1 / PPU유닛) 단위로 끊기며 이동하여 화면 스크롤이 거칠어집니다. - 스냅 + 서브픽셀 UV 보정: 도트 내부 래스터 왜곡을 억제하면서 모니터 주사율에 맞춘 부드러운 카메라 스크롤을 동시에 유지합니다.
- Without Snapping: 1-pixel outlines and fine details on 3D models fluctuate in thickness each frame during camera movement, causing noticeable shimmering.
- Position Snapping Only: Internal pixel distortion is eliminated, but the camera steps in discrete 1-texel (
1 / PPUunit) increments, resulting in choppy scrolling. - Snapping + Subpixel UV Compensation: Suppresses raster distortion within pixel art while maintaining smooth camera scrolling at the display's native refresh rate.
월드 공간의 텍셀 그리드 스냅과 화면 공간의 서브픽셀 UV 오프셋을 분리해 처리하면, 3D 픽셀 아트 파이프라인에서 픽셀 형태 안정성과 부드러운 카메라 이동을 함께 확보할 수 있습니다.
Decoupling world-space texel snapping from screen-space subpixel UV offsetting ensures both stable pixel silhouettes and smooth camera motion in 3D pixel-art pipelines.