Unity/Shader
[5.7] Warped Diffuse
ljw4104
2021. 5. 7. 16:14
빛 공식으로 쓰이는 Normal과 Light Vector의 내적을 UV 사용한다.
Ramp라는 텍스쳐의 UV를 ndotl의 값으로 사용한다.
Shader "Custom/Test"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_RampTex ("Ramp Texture", 2D) = "white" {}
_BumpTex ("Bump Texture", 2D) = "bump" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf _Diff noambient
sampler2D _MainTex;
sampler2D _RampTex;
sampler2D _BumpTex;
struct Input
{
float2 uv_MainTex;
float2 uv_RampTex;
float2 uv_BumpTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_BumpTex));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
float4 Lighting_Diff(SurfaceOutput s, float3 lightDir, float atten)
{
float ndotl = dot(s.Normal, lightDir);
float4 ramp = tex2D (_RampTex, float2(ndotl, 0.5));
float4 final;
final.rgb = ramp * s.Albedo;
final.a = s.Alpha;
return final;
}
ENDCG
}
FallBack "Diffuse"
}
Specular 적용
Shader "Custom/Test"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_WarpTex ("Warp Texture", 2D) = "white" {}
_BumpTex ("Bump Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf _Warp noambient
sampler2D _MainTex;
sampler2D _WarpTex;
sampler2D _BumpTex;
struct Input
{
float2 uv_MainTex;
float2 uv_WarpTex;
float2 uv_BumpTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_BumpTex));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
float4 Lighting_Warp(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
{
float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;
float3 h = lightDir + viewDir;
float spec = saturate(dot(s.Normal, h));
float4 ramp = tex2D (_WarpTex, float2(ndotl, spec));
float4 final;
final.rgb = (s.Albedo.rgb * ramp.rgb) + (ramp.rgb * 0.25);
final.a = s.Alpha;
return final;
}
ENDCG
}
FallBack "Diffuse"
}