형식 이름 ( 입력값 ) { }
voidsurf (Input IN, inout SurfaceOutputStandard o) { }
1. void : return 값이 없는 함수 형식을 의미
2. surf : 함수의 이름
3. (Input IN, inout SurfaceOutputStandard o) 함수에서 () 안에 있는 부분은 보통 이 함수에 값을 넣는 입력값의 영역이고 이 부분은 다음과 같이 해석할수 있다. 'input 구조체를 IN이란 이름으로 이 함수 안에 받아들이고, SurfaceOutputStandard라는 구조체를 o 라는 이름으로 이 함수 안에 받기도 하고 집어 넣기도 (input) 하겠다'
4. SurfaceOutputStandard 구조체는 유니티 내부의 다른 include 파일에 정의되어 있음
struct SurfaceOutputStandard
{
fixed3 Albedo; // diffuse color
fixed3 Normal; // tangent space normal, if written
fixed3 Emission;
half Specular; // specular power in 0..1 rangefixed Gloss; // specular intensityfixed Alpha; // alpha for transparencies
};
좌: Albedo (1,0,0) // 우 emission (1,0,0)
Albedo 에 넣으면 조명연산을 추가로 받게되고, Emission에 넣으면 조명 연산을 받지 않아서 '조명과 상관없는 순수한 색상만이 출력 됨'
이전까진 Properties 영역(인터페이스 를 제작하는 영역) 이고, 진짜 쉐이더를 제작하는 영역에 대해서 알아볼 부분임
CGPROGRAM ~ ENDCG로 끝나는 부분은 유니티 스크립트가 아닌 CG 언어를 이용해서 쉐이더를 직접 짜야함
////*주석 : 전처리기(=스니핏) ////**주석 : 구조체 ////***주석 : 함수
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
////*전처리라고 할수도 있고, 스니핏이라고 부르기도함, 쉐이더의 조명계산 설정이나, ////세부적인 분기를 정해주는 부분#pragma surface surf Standard fullforwardshadows#pragma target 3.0////*
sampler2D _MainTex;
////** Input이라는 이름을 가진 구조체, 이 안에 넣는 내용은 엔진으로부터 받아와야 할 데이터들임////{ } 안에 들어 있고, { } 의 끝에 ; 있다는 점을 주의 struct Input
{
float2 uv_MainTex;
};
////**
half _Glossiness;
half _Metallic;
fixed4 _Color;
////*** sur라는 이름을 가진 '함수' 영역, 색상이나 이지미가 출력되는 부분을 말수가 있음////{ } 안에 들어 있고, { } 의 끝에 ; 있다는 점을 주의 voidsurf (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
}
// Float을 받는 인터페이스
{
_Name("display name", Range(min, max)) = number //1. Range
_Name("display name", Float) = number //2. Float
_Name("display name", int) = number //3. int
}
//Float4을 받는 인터페이스
{
_Name("display name", Color) = (number, number, number, number) //1. Color
_Name("display name", Vector) = (number, number, number, number) //2. Vector
}
//기타 Sampler을 받는 인터페이스
{
_Name("display name", 2D) = "name" { option } //1. 2D
_Name("display name", Rect) = "name" { option } //2. Rect
_Name("display name", Cube) = "name" { option } //3. Cube
_Name("display name", 3D) = "name" { option } //4. 3D
}
◇ _Name 이 기능의 <변수명> _을 부이는 건 외부에서 입력받았다는 것을 표시하기 위해 쓰임
변수명은 한글 X 띄어쓰기 X 숫자로 시작 X _이외의 특수문자 X 예약어(color flooat 처럼 미리 시스템에 정의된 명칭) 이용해도 안됨
◇ "display name" 큰 따옴표 안에 글은 순수하게 '화면에 나타나는 글자'이고, 엔진에서는 이 안의 내용을 순순한 글씨로 인식함. 유일하게 한글을 입력도 가동되는 곳이지만, 한글을 권하지 않음.
◇ number 이 쉐이더가 처음 만들어졌을 떄 초기값을 의미함 사용자가 값을 조금이라도 조절한다면 이 값은 이제부터 무시하게됨.
Properties - Range (min, max)
_Name("display name", Range(min, max)) = number
- Range의 의미는 슬라이더 바를 만들겠다는 뜻이고, min, max는 각각 슬라이더 바의 최소 최대값을 의미함.
Properties- Float
_Name("display name", Float) = number
- float은 한자리의 소수점을 입력받는 인터페이스를 만들어 줌. - Range와 동일하게 한자리의 소수점을 입력받지만, 슬라이더 방식이 아닌 직접 값을 입력하는 인터페이스가 생김. - 범위에 제한이 없기 떄문에 입력값의 자유도를 주고 싶거나 결과값 예측이 어려운떄 사용이 좋음
Shader "Custom/NewSurfaceShader01"//쉐이더이름
{
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/////여기서 ENDCG 까지 유니티 스크립트아 아닌 CG언어를 사용해서 쉐이더를 직접 짜는 부분
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;
/* GPU 인스턴싱 주석함
// 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)
*/voidsurf (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"
}