NPR : 비사실적(non-photorealistic) 렌더링
현재 단계에선 1,2만 사용
cull front 로 뒤집힌 모델 생성
cull back (기본값)으로 정상적인 모델 생성
이하 쉐이더 파일 코드
Shader "Apply/TwoPassShader"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
_OutlineWeight ("Outline Weight", Range(1, 20)) = 5
}
SubShader
{
Tags { "RenderType"="Opaque" }
cull front
CGPROGRAM
#pragma surface surf Nolight noambient vertex:vert noshadow
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
fixed4 _OutlineColor;
float _OutlineWeight;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutput o)
{
}
float4 LightingNolight(SurfaceOutput s, float3 lightDir, float atten)
{
return _OutlineColor;
}
void vert(inout appdata_full v)
{
v.vertex.xyz = v.vertex.xyz + v.normal.xyz * (_OutlineWeight / 1000);
}
ENDCG
cull back
CGPROGRAM
#pragma surface surf Lambert noambient
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 diffTex = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = diffTex.rgb;
o.Alpha = diffTex.a;
}
ENDCG
}
FallBack "Diffuse"
}
Lambert 붙이기
이하 전체 소스
Shader "Apply/FresnelOutlineShader"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
_OutlineWeight ("Outline Weight", Range(0.1, 0.4)) = 0.3
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Fresnel fullforwardshadows
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex;
fixed4 _OutlineColor;
float _OutlineWeight;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 diffColor = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = diffColor.rgb;
o.Alpha = diffColor.a;
}
float4 LightingFresnel(inout SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
{
// Lambert
float fNDotL = saturate(dot(s.Normal, lightDir)) * 0.5 + 0.5;
// Diffuse
float4 diffColor;
diffColor.rgb = s.Albedo * fNDotL * _LightColor0.rgb * atten;
// Outline
float lim = abs(dot(s.Normal, viewDir));
if(lim > _OutlineWeight)
{
lim = 1;
}
else
{
lim = -1;
}
float4 result;
result.rgb = diffColor.rgb * lim;
result.a = s.Alpha;
return result;
}
ENDCG
}
FallBack "Diffuse"
}
카툰 효과를 내기 위해 그림자를 그라데이션이 아닌 층으로 만들기
Shader "Apply/ToonShader"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpTex ("Bump Map", 2D) = "bump" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Toon noambient
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
float2 uv_BumpTex;
};
sampler2D _MainTex;
sampler2D _BumpTex;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 diffColor = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = diffColor.rgb;
o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_BumpTex));
o.Alpha = diffColor.a;
}
float4 LightingToon(SurfaceOutput s, float3 lightDir, float atten)
{
float3 diffColor = s.Albedo * _LightColor0.rgb * atten;
float nDotL = dot(s.Normal, lightDir) * 0.5 + 0.5;
nDotL = nDotL * 5;
nDotL = ceil(nDotL) / 5;
float4 result;
result.rgb = nDotL * diffColor;
result.a = s.Alpha;
return result;
}
ENDCG
}
FallBack "Diffuse"
}