Shader / URP
Intermediate
Unity 2021.3+ / Unity 6 (URP)
램프 텍스처(Ramp Texture)와 1D 룩업을 활용한 URP 카툰 셀 셰이딩(Toon Shading)URP Cel-Shading (Toon Shader) Using 1D Ramp Texture Lookups
Lambert N·L 조명 값을 연속적인 그라데이션 대신 계단식 셀 램프 텍스처로 매핑하여 셀 애니메이션 음영을 표현하는 URP 커스텀 라이팅 셰이더.Mapping Lambert N·L lighting values through a stepped 1D ramp texture instead of smooth gradients to achieve stylized cel-shaded lighting in URP.
#Shader #URP #Toon #CellShading #HLSL
핵심 개념일반적인 라이팅은 노멀과 광원 벡터의 내적($N \cdot L$)으로 연속적인 음영을 만듭니다. 이 $N \cdot L$ 값을
0.0 ~ 1.0 범위로 리매핑한 뒤 1D 텍스처의 U 좌표로 샘플링하면, 음영 단계를 아티스트가 지정한 램프 텍스처(Ramp Texture) 색상으로 직접 제어할 수 있습니다.Core ConceptStandard lighting computes the dot product ($N \cdot L$) between the surface normal and light direction to produce continuous shading. Remapping $N \cdot L$ into the
0.0–1.0 range and using it as the U coordinate of a 1D lookup texture lets artists directly control shading bands and shadow tint via a Ramp Texture.1. HLSL 램프 조명 계산 로직1. HLSL Ramp Lighting Calculation
Light mainLight = GetMainLight(TransformWorldToShadowCoord(input.positionWS));
float NdotL = dot(normalize(input.normalWS), mainLight.direction);
// -1~1 값을 0~1 UV 좌표로 리매핑 (Half-Lambert)
float rampUV = NdotL * 0.5 + 0.5;
// 1D 램프 텍스처 샘플링 (경계면 계단식 색상)
half3 rampColor = SAMPLE_TEXTURE2D(_RampTex, sampler_RampTex, float2(rampUV, 0.5)).rgb;
half3 finalLight = rampColor * mainLight.color * mainLight.distanceAttenuation;
Light mainLight = GetMainLight(TransformWorldToShadowCoord(input.positionWS));
float NdotL = dot(normalize(input.normalWS), mainLight.direction);
// Remap -1..1 range to 0..1 UV coordinate (Half-Lambert)
float rampUV = NdotL * 0.5 + 0.5;
// Sample 1D ramp texture (stepped shading bands)
half3 rampColor = SAMPLE_TEXTURE2D(_RampTex, sampler_RampTex, float2(rampUV, 0.5)).rgb;
half3 finalLight = rampColor * mainLight.color * mainLight.distanceAttenuation;
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- 램프 텍스처 Wrap Mode 설정:
_RampTex의Wrap Mode가Repeat로 되어 있으면 UV 양 끝단(0.0또는1.0)에서 반대쪽 색상(어두운 그림자 끝에 밝은 하이라이트)이 번져 나오므로 반드시Clamp로 설정해야 합니다. - 그림자 감쇠(
shadowAttenuation) 결합:NdotL에mainLight.shadowAttenuation을 곱한 뒤 램프 텍스처를 조회하거나 별도로 마스킹해야 드리워진 실시간 그림자 영역도 동일한 셀 음영 톤으로 통합됩니다.
- Set Ramp Texture Wrap Mode to
Clamp: If_RampTexusesRepeat, UV values near0.0or1.0will wrap around and bleed the opposite end of the gradient (e.g., bright highlights leaking into deep shadows); always setWrap Mode = Clamp. - Integrating
shadowAttenuation: MultiplyNdotL(or the remapped UV) bymainLight.shadowAttenuationso that cast shadows adopt the same cel-shaded shadow band color.
Half-Lambert 리매핑과 1D 램프 텍스처 룩업을 활용하면 셰이더 코드를 수정하지 않고도 텍스처 교체만으로 캐릭터와 재질별 음영 경계의 색감과 경도를 세밀하게 조정할 수 있습니다.
Combining Half-Lambert remapping with a 1D ramp lookup allows artists to tune shadow colors and edge hardness per material simply by swapping ramp textures.