새소식

그래픽

[Unity URP Shader] 툰 라이팅 기본

  • -

[출처] Unty Shader Training

https://youtu.be/mlNWoROvavY?si=7XpTECHR_CJ5u62d

 

 

	Properties {   
        _RampTex("RampTexture", 2D) = "White"{}

        _TintColor("Color", color) = (1,1,1,1)
        _Intensity("Intensity", Range(0,1)) = 0.5
        _AmbientColor("AmbientColor",  color) = (1,1,1,1)
        _lightwidth("LightWidth" , Range(0.1, 10)) = 0.5
        
        _lightStep("LightStep" , Float) = 0.5
     } 
     
     ....[생략]
     
	float4 _TintColor;
        float _Intensity;
        float4 _AmbientColor;
        float _lightwidth;
        float _lightStep;

        Texture2D _RampTex;
        SamplerState sampler_RampTex;
     
     ....[생략]
	VertexOutput vert(VertexInput v)
        {
            VertexOutput o;
            float4 positionWS = TransformObjectToHClip(v.vertex.xyz);

            o.normal = TransformObjectToWorldNormal(v.normal);
            o.vertex = positionWS;

            return o;
        }					

        half4 frag(VertexOutput i) : SV_Target
        {
            /*툰 쉐이더 대역폭과 단계로 구분하기
            float3 light = _MainLightPosition.xyz;     
            float4 color = float4(1,1,1,1);

            float NdotL = saturate(dot(light, i.normal));

            //float3 toonLight = NDotL > 0 ? _MainLightColor.rgb : _AmbientColor.rgb;
            //툰 라이팅을 위한 계단식 빛 적용
            float3 toonLight = ceil((NdotL) * _lightwidth) / _lightStep * _MainLightColor.rgb;

            color.rgb *= toonLight;
            return color;
            */
	
    //이미지를 이용한 툰 라이팅[위 결과를 이미지로 처리한것]
            float3 light = _MainLightPosition.xyz;     
            float4 color = float4(1,1,1,1);
            
            //-1 ~ 1 내적값
            float NdotL = (dot(light, i.normal));
            //0~ 1의 범위로 변경
            float halfNdotL = NdotL * 0.5 + 0.5;

            //주변광 적용을 위한 것
            float3 ambient = SampleSH(i.normal);

            //램프 텍스쳐의 x축 기준으로 0~1의 값을 가지고 온다.
            float3 ramp = _RampTex.Sample(sampler_RampTex, float2(halfNdotL, 0));

            //모든 것을 결합
            color.rgb =color.rgb *  ramp + ambient;

            return color;
        }

 

 

# 계단식 표현을 위한 것
# _lightwidth은 표면에 적용되는 빛의 세기와 같고, _lightStep 빛의 세기를 몇분할 할 것인지를 나타냄
#Ceil을 사용함으로서 블렌딩된 색이 아닌, 구역별로 나뉘어진 색이 표현

float3
toonLight = ceil((NdotL) * _lightwidth) / _lightStep * _MainLightColor.rgb
# -1 ~ 1 범위의 라이팅에서 0 ~ 1 범위의 빛과 normal의 내적으로 변환
float
halfNdotL = NdotL * 0.5 + 0.5;

# 모든 주변광 처리를 자동으로 진행
float3 ambient = SampleSH(i.normal);

#램프 텍스쳐를 uv 좌표가 아닌  halfNdotL 로 x축을 매핑함으로서, 오브젝트가 구분된 색이 표현됨.
float3 ramp = _RampTex.Sample(sampler_RampTex, float2(halfNdotL, 0));

 

 

인스펙터

 

 

 

결과

 

01

 

02

Contents

아핫

땡큐하다