위에서 제작한 불 이펙트를 업그레이드 해보기 위한 과정이다. 우선, 확실한 효과를 보기 위하여 불 이미지를 아래와 같이 체크 이미지로 변경하였으며 두 번째 이미지는 코드 내에서_MainTex2 ("Albedo (RGB)", 2D) = "black" {}을 추가하여 검정색 이미지를 출력하도록 하였다.
아래와 같이 코드를 변경하여 c의 uv에 d.r을 더해주어도 아무 변화가 일어나지 않는다. d는 float4(0,0,01)이기 때문이다.
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 d = tex2D(_MainTex2, IN.uv_MainTex2);
fixed4 c = tex2D(_MainTex, IN.uv_MainTex + d.r);
o.Emission = c.rgb;
o.Alpha = c.a;
}
ENDCG
d 텍스쳐의 색상에 따라 변경되는 이미지의 이동 범위
d 텍스쳐의 이미지에 따라 다르게 출력되는 c 텍스쳐의 이미지
위로 흐르도록 코드 변경
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 d = tex2D(_MainTex2, float2(IN.uv_MainTex2.x, IN.uv_MainTex2.y - _Time.y));
fixed4 c = tex2D(_MainTex, IN.uv_MainTex + d.r);
o.Emission = c.rgb;
o.Alpha = c.a;
}
Shader "Custom/Practice_Part5"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
위 코드에서 Texture 한 장을 출력하기 위한 코드를 제외하고 필요 없는 코드를 모두 정리해보면 아래와 같다.
"Albedo (RGB)" : 해당 부분은 Albedo 텍스쳐를 넣는 곳이고, 알파는 사용하지 않고 RGB 채널만 사용하겠다는 의미 2D : 해당 인터페이스가 2D 텍스쳐를 받는 부분이라는 의미 "white" {} : 해당 텍스쳐 인터페이스의 초기 default 값은 흰색 텍스쳐라는 의미
인터페이스를 통해 입력받은 텍스쳐를 변수로 받는 코드
sampler2D _MainTex;
구조체 내부에서 uv를 받아오는 코드
float2 uv_MainTex;
uv : float2이며 텍스쳐는 이 uv 좌표와 text2D를 통해 계산되어야 float4로 출력할 수 있음. uv는 vertex가 가지고 있기 때문에 이렇듯이 우리가 만든 인터페이스가 아닌 vertex 내부의 것을 엔진에게 명령할 때에는 Input 구조체를 사용해야 함.
텍스쳐를 연산하여 컬러를 화면에 출력하는 코드
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
이미지 흑백 전환
흑백 이미지의 두 가지 속성
- R,G,B 모두 동일한 숫자로 이루어짐 - 해당 숫자는 R,G,B 각 요소에 따른 강도의 평균이어야 함
o.Albedo = (c.r+c.g+c.b)/3;
o.Albedo = c.rgb;에서o.Albedo = (c.r+c.g+c.b)/ 3;로 코드를 변화하니 위와 같이 이미지가 흑백으로 변경된 것을 볼 수 있다.
5. text2D float 계열로 분류되지 않는 sampler들 (2D텍스쳐를 받는 인터페이스)
_Name ("display name", 2D) = "name" {options}
색상 출력 및 연산
유니티 자체 스크립트가 아닌 CG 언어를 이용하여 쉐이더를 짜는CGPRGRAM ~ ENDCG영역
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#pragma enable_d3d11_debug_symbols
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
1. 설정 부분 전처리 또는 스니핏(snippet)이라 부름. 쉐이더의 조명 계산 설정이나 세부적인 분기를 정해주는 부분
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
2. Input 구조체 엔진으로부터 받아와야 할 데이터
struct Input{
float2 uv_MainTex;
};
3. 함수 영역 색상이나 이미지가 출력되는 부분
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
o.Albed: 빛의 영향을 받는 색이 출력
o.Emmision: 빛의 영향을 받지 않는 색이 출력위와 같이 변수를 사용하여 출력 색상 변경 또한 가능하다.
색상 입력 받고 출력 ex)properties에_TestColor("TestColor", Color) = (1,1,1,1)을 추가하여 인스펙터에 나타내고,float4 _TestColor;로 변수를 추가한 뒤, surf 함수 내에서o.Albedo = _TestColor.rgb;을 추가
즉, 인터페이스에서 원하는 타입의 변수를 생성한 후, SubShader 코드에 선언하고, surf함수에서 변수를 사용하면 출력 가능