Shader / URP
Intermediate
Unity 2022.3+ / Unity 6 (URP 2D)
2D 픽셀 스프라이트 노멀맵(Normal Map)과 URP 2D 동적 광원 입체 라이팅2D Pixel Sprite Normal Maps and Dynamic Lighting in URP 2D
2D 도트 스프라이트에 Secondary Texture _NormalMap을 연결하고 URP 2D Point/Spot Light로 실시간 입체 명암과 림라이트를 연산하는 기법.Techniques for binding a Secondary Texture _NormalMap to 2D pixel sprites and computing real-time cel-shaded lighting and rim highlights with URP 2D lights.
#Shader #URP #2D #NormalMap #DeadCells #Lighting
핵심 개념*Dead Cells*처럼 평면적인 2D 픽셀 캐릭터가 횃불이나 주변 광원을 지나갈 때 표면 굴곡에 따라 실시간으로 빛이 반응하도록 만들 수 있습니다. 이는 2D 스프라이트에 노멀맵(Normal Map)을 바인딩하고 URP의 2D 라이팅 파이프라인(
Sprite-Lit-Default)을 활용해 3D 벡터 내적($N \cdot L$) 연산을 수행한 결과입니다.Core ConceptFlat 2D pixel characters, as seen in *Dead Cells*, can react dynamically to torches and surrounding light sources according to their surface contours. This is achieved by binding a Normal Map to the 2D sprite and computing a 3D vector dot product ($N \cdot L$) within the URP 2D lighting pipeline (
Sprite-Lit-Default).1. 스프라이트 에디터 Secondary Texture 설정1. Configuring Secondary Textures in Sprite Editor
좌우로 밀어서 표 전체 내용을 확인하세요
| 단계 | 작업 경로 및 옵션 | 설명 |
|---|---|---|
| 1단계 | Texture Import > Texture Type = Sprite (2D and UI) | 플레이어 스프라이트 시트 기본 임포트 |
| 2단계 | Sprite Editor 열기 > Secondary Textures 선택 | 스프라이트 메타데이터에 추가 텍스처 슬롯 개방 |
| 3단계 | Name: _NormalMap, Texture: 캐릭터_Normal 텍스처 등록 | 픽셀별 법선 벡터 텍스처를 1:1 좌표로 바인딩 |
| 4단계 | 캐릭터 Material = Universal Render Pipeline/2D/Sprite-Lit-Default | 노멀 연산을 지원하는 URP 2D 기본 셰이더 머티리얼 적용 |
Swipe horizontally to view the full table
| Step | Menu Path and Option | Description |
|---|---|---|
| Step 1 | Texture Import > Texture Type = Sprite (2D and UI) | Standard import setting for the player sprite sheet |
| Step 2 | Open Sprite Editor > Select Secondary Textures | Expose additional texture slots in the sprite metadata |
| Step 3 | Name: _NormalMap, Texture: Assign character normal texture | Bind the per-pixel normal map at a 1:1 UV ratio |
| Step 4 | Character Material = Universal Render Pipeline/2D/Sprite-Lit-Default | Apply the default URP 2D lit shader material that supports normal calculations |
2. 커스텀 2D 도트 계단식(Cel-stepped) 노멀 라이트 셰이더2. Custom 2D Cel-Stepped Normal Light Shader
// 2D 픽셀 스프라이트의 동적 광원을 도트 계단식으로 양자화하는 연산 블록
half3 CalculateCel2DLighting(half3 baseColor, half3 normalTS, float3 worldPos, float3 lightPos, half3 lightColor, float lightRange)
{
float3 lightDir = lightPos - worldPos;
float dist = length(lightDir);
if (dist > lightRange) return baseColor;
lightDir = normalize(lightDir);
// 2D 스프라이트 접선 공간 노멀 언패킹
half3 N = normalTS;
half3 L = half3(lightDir.xy, lightDir.z);
// 램버트 도트곱
float NdotL = max(0.0, dot(N, L));
// 연속적인 3D 조명을 3단계 도트 명암으로 양자화
float celFactor = floor(NdotL * 3.0) / 3.0;
// 감쇠 적용
float atten = saturate(1.0 - (dist / lightRange));
half3 finalLight = lightColor * celFactor * atten;
return baseColor * (half3(0.2, 0.2, 0.3) + finalLight); // 환경광 + 동적광
}
// Quantizes dynamic lighting on a 2D pixel sprite into stepped cel-shaded bands
half3 CalculateCel2DLighting(half3 baseColor, half3 normalTS, float3 worldPos, float3 lightPos, half3 lightColor, float lightRange)
{
float3 lightDir = lightPos - worldPos;
float dist = length(lightDir);
if (dist > lightRange) return baseColor;
lightDir = normalize(lightDir);
// Unpack tangent-space normal for the 2D sprite
half3 N = normalTS;
half3 L = half3(lightDir.xy, lightDir.z);
// Lambert dot product
float NdotL = max(0.0, dot(N, L));
// Quantize smooth 3D lighting into 3 discrete shading bands
float celFactor = floor(NdotL * 3.0) / 3.0;
// Apply distance attenuation
float atten = saturate(1.0 - (dist / lightRange));
half3 finalLight = lightColor * celFactor * atten;
return baseColor * (half3(0.2, 0.2, 0.3) + finalLight); // Ambient + dynamic light
}
3. 실전 제작 팁3. Practical Production Tips
- 노멀맵 자동 생성 도구: 스프라이트를 한 장씩 수작업으로 그리는 대신,
SpriteIlluminator나 오픈소스 도구인Laigter를 사용하면 2D 도트 이미지의 외곽선과 명암을 분석해 노멀맵을 빠르게 생성할 수 있습니다. - Z축 깊이(Z-Depth) 배치: 2D 씬에서 조명이 정상적으로 입체 명암을 드리우려면 광원(
Light 2D)의 Transform Z 좌표가 스프라이트보다 약간 앞(카메라 방향, 예:z = -1.0f)에 위치해야 입사각이 형성됩니다.
- Automated Normal Map Generation: Instead of painting normal maps by hand for every frame, tools such as
SpriteIlluminatoror the open-sourceLaigtercan analyze 2D sprite silhouettes and shading to generate normal maps automatically. - Z-Depth Placement: For 2D lights to cast directional shading properly, position the
Light 2DTransform slightly in front of the sprite along the Z-axis (toward the camera, e.g.,z = -1.0f) to establish a valid incident angle.
Secondary Texture 노멀맵 바인딩과 단계별 조명 양자화를 결합하면, 평면 2D 스프라이트 환경에서도 광원 위치에 반응하는 입체 명암을 일관되게 표현할 수 있습니다.
Combining Secondary Texture normal map binding with stepped light quantization allows flat 2D sprites to display consistent directional shading in response to dynamic light sources.