unity surface shader for artist 02
TRANSCRIPT
02. 기본 Unity Surface Shader 작성
이상윤 http://illu.tistory.com
1주차 과제 확인
Red(1,0,0)
Orange(1, 0.5, 0)
Yellow(1,1,0)
Green(0,1,0)
Blue(0,0,1)
Indigo(0.25, 0, 0.5)
Violet(0.5, 0, 1)
Shader "Study/TintColor" {
Properties {
_TintColor(“색깔”, color) = (1,1,1,1)
}
SubShader {Tags { "RenderType"="Opaque" }
CGPROGRAM#pragma surface surf Lambert
fixed4 _TintColor;
struct Input {
float4 color:COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = _TintColor;
}
ENDCG}
}
Shader "Study/RGBColor" {
Properties {
_Red (“빨간채널”, Range(0,1)) = 1_Green (“초록채널”, Range(0,1)) = 1_Blue (“파랑채널”, Range(0,1)) = 1
}
SubShader {Tags { "RenderType"="Opaque" }
CGPROGRAM#pragma surface surf Lambert
fixed _Red; // fixed _Red, _Green, _Blue;fixed _Green;fixed _Blue;
struct Input {float4 color:COLOR;};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = float3( _Red, _Green, _Blue);}
ENDCG}
}
변수~
Properties에 정의되는 이름~
* 숫자로 시작하면 안됨
* 예약어는 안됨
* 대소문자 구별함
보통 많이 쓰는
_MainTex
_TintColor
_Emission
_Gloss
: 이런 변수명 그냥 씁시다…
스크립트로 이를 제어할때도 이걸 사용함.(그러니까 많이 쓰
는걸 걍 쓰자~!)
이제 메쉬의 UV를 사용해 mapping을 해봅시다.
Shader "Study/Diffuse" {
Properties {
_MainTex(“텍스쳐”, 2D) = “white” {}}
SubShader {Tags { "RenderType"="Opaque" }
CGPROGRAM#pragma surface surf Lambert
sampler2D _MainTex;
struct Input {float4 color:COLOR; // 얘는 vertex color를 사용하는 구조체 선언문입니다.float2 uv_MainTex;};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
}ENDCG}
}
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
이걸 아래처럼.
fixed3 c = tex2D(_MainTex, IN.uv_MainTex).rgb;
o.Albedo = c.rgb;
o.Albedo 출력을 치환해서 처리해봅시다.
텍스쳐를 한장 더 쓸수 있게 하여 두장을 더하고 빼고 곱해봅시다~!
Hint
_MainTex1(“텍스쳐1”, 2D) = “white” {}_MainTex2(“텍스쳐2”, 2D) = “white” {}
c1 + c2 ; c1-c2;
Reverse : 1-(c1+c2)
흑백 이미지를 만들어 봅시다.
fixed3 c = tex2D(_MainTex, IN.uv_MainTex).rgb;
fixed grayTex = (c.r + c.g + c.b) / 3;
o.Albedo = grayTex;
* NTSC 가중치에 따른 공식(National Television System Committee, 국가 텔레비전 시스템 위원회)
(0.299 * R + 0.587 * G + 0.114 * B) - 대마왕님 자료 참조
흑백 이미지를 만들어 봅시다.
o.Emission으로 Emissive effect를 만들어 봅시다.
struct Input {
float2 uv_MainTex;
}
fixed3 c = tex2D(_MainTex, IN.uv_MainTex).rgb;
fixed3 e = tex2D(_MainTex2, IN.uv_MainTex).rgb;
o.Albedo = c.rgb;
o.Emission = e.rgb;
Color를 변경할수 있게 만들어 봅시다.
변수에 Range 타입을 선언해 값의 세기를 변경할 수 있게 해봅시다.
o. Albedo = c;
o. Emission = c * fixed3(1,1,0);
o. Albedo = c.rgb;
o. Emission = e * _EmissionColor;
o. Albedo = c.rgb;
o. Emission = e.rgb;
Vertex color를 활용해 봅니다.
Vertex color는 항상 검은색을 깔고 위에 RGB
Color를 입힌다고 생각하면 됩니다.
struct Input {
fixed4 color:COLOR;
}
fixed3 c = tex2D(_MainTex, IN.uv_MainTex).rgb;
o.Albedo = c.rgb * IN.color.rgb;
Vertex Alpha도 적용해 봅니다.
흐르는 물을 표현할때 알파 텍스쳐와 더불어 활
용하기 좋은 방법입니다.
struct Input {
fixed4 color:COLOR;
}
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb * IN.color.rgb;
o.Alpha = c.a * IN.color.a;
함수를 사용해 봅니다.
Lerp(Linear Interpolation) : 선형 보간법.
Lerp(a, b, f) : (1-f) * a + b * f
a 와 b의 사이의 f 값에 따른 값을 구할때 사용 하는 함수.
Terrain splatting에 가장 기본적으로 사용하는 함수.
A texture + B texture = blending texture.
Texture 두장을 더하고 빼고 했던것을 Vertex color를 이용
해 lerp 함수를 사용해 만들어 봅시다.
fixed3 c1 = tex2D(_MainTex1, IN.uv_MainTex).rgb;
fixed3 c2 = tex2D(_MainTex2, IN.uv_MainTex).rgb;
fixed3 c = lerp (a, b, ? ) ;
o.Albedo = c ;
수고하셨습니다.