Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 기초
- String
- 프로그래밍
- 시작해요 언리얼 2022
- github
- 문제풀이
- 재귀
- Material
- dfs
- Unity
- W3Schools
- Unreal Engine 5
- 백준
- Class
- 파이썬
- guide
- loop
- c++
- UE5
- dynamic
- 오류
- w3school
- Programming
- Tutorial
- parameter
- C#
- Algorithm
- DP
- Basic
- python
Archives
- Today
- Total
행복한 개구리
Unity Shader 21.05.07. 수업내용 - Wraped Diffuse 본문
사용한 Ramp텍스쳐. 흰색이 필요해서 사진 위쪽에 그림판으로 흰색을 만들어서 사용했다.
**핵심 구현기법은 빛공식으로 쓰이는 Normal과 Light Vector의 내적을 UV로 사용한다.
** 주의 : wrap를 할 때 쓰는 텍스쳐의 경우는 Input에서 UV좌표를 구하지 않고 쓴다. -> 텍스쳐의 UV좌표를 그대로 써야하는게 아니라 조정해서 써야하기때문에 위 코드를 보면 tex2D(램프텍스쳐, 좌표값)이라고 사용된다.
여기에 알베도까지 적용시킨다면 이런 모습이 된다.
Shader "Custom/Wrap"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_RampTex ("RampTexture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Wrap noambient
sampler2D _RampTex;
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
float4 LightingWrap(SurfaceOutput s, float3 lightDir, float attten)
{
float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;
float4 ramp = tex2D(_RampTex, float2(ndotl, 0.5));
float4 final;
final.rgb = s.Albedo * ramp.rgb;
final.a = s.Alpha;
return final;
}
ENDCG
}
FallBack "Diffuse"
}
참고로 옵션에서의 Wrap는 내장함수가 아니라 그냥 이름이다.
오늘 배운 내용까지 합쳐보았다.
Shader "Custom/Test"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_RampTex ("RampTexture", 2D) = "white" {}
_BumpTex ("BumpTexture", 2D) = "bump" {}
_Thickness ("Thickness", Float) = 0
_LineColor ("LineColor", Color) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Cull Front
CGPROGRAM
#pragma surface surf _Test vertex:vert noambient noshadow
float _Thickness;
float4 _LineColor;
void vert(inout appdata_full v)
{
v.vertex.xyz = v.vertex.xyz + v.vertex.xyz * _Thickness;
}
struct Input{float2 uv_MainTex;};
void surf (Input IN, inout SurfaceOutput o){}
float4 Lighting_Test(SurfaceOutput s, float3 lightDir, float atten)
{
return _LineColor;
}
ENDCG
Cull Back
CGPROGRAM
#pragma surface surf _Test
sampler2D _MainTex;
sampler2D _RampTex;
sampler2D _BumpTex;
struct Input
{
float2 uv_BumpTex;
float2 uv_MainTex;
};
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_Test(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
{
float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;
float3 hVec = normalize(lightDir + viewDir);
float spec = dot(hVec, s.Normal);
float4 ramp = tex2D(_RampTex, float2(ndotl, spec));
float4 final;
final.rgb = s.Albedo.rgb * ramp.rgb;
final.a = s.Alpha;
return final;
}
ENDCG
}
FallBack "Diffuse"
}
Bump와 Wrap모두 사용하였고, 2pack을 사용하여 외곽선을 잡았다.
코드를 보다보니 Ramp텍스쳐를 적용할 때, UV좌표값에 U에는 ndotl, V에는 spec을 적용시키니 둘의 특성이 모두 드러나는 듯 하다.
'Unity > 수업내용' 카테고리의 다른 글
Unity Shader 21.05.10. 수업내용 - Alpha Blending (0) | 2021.05.10 |
---|---|
Unity Shader 21.05.07. 수업내용 - Cube Map (0) | 2021.05.07 |
Unity Shader 21.05.07. 수업내용 - 빛효과 단계별 적용 (0) | 2021.05.07 |
Unity Shader 21.05.07. 수업내용 - 외곽선 적용 (0) | 2021.05.07 |
Unity Shader 21.05.06. - BlinnPhong HalfVector (0) | 2021.05.06 |