Unity/Shader

[5.3] ShaderLab

ljw4104 2021. 5. 3. 12:20

ShaderLab

  • 유니티에서 Shader를 Control 할 수 있는 언어
  • Properties, SubShader, CG 부분으로 나뉜다.
구분 특징
Properties Inspector 창에 나타나는 속성들을 기재한다.
SubShader 태그를 사용하여 언제 어떻게 렌더링할지 나타낸다.
(실제 Shader코드부분, 여러개가 있을 수 있음.)
CG CG(밖의 Shader언어)를 사용해서 Shader를 구성한다.

 

Properties

  • Inspectors창에 나타나는 속성들을 정의할 수 있다.
  • 여러가지 속성들이 존재한다.
  • = 값, 이부분이 없으면 에러발생. 무조건 기본값을 넣어주어야 한다.
  • 값의 이름은 무조건 _(underbar)로 시작해야된다. 끝에 세미콜론을 붙여서는 안된다.
_변수이름 ("Inspector창에 표시될 이름", 자료형) = 기본값

R, G, B 의 값을 0~1까지 설정하는 Properties

 

SubShader

  • 실질적으로 Shader가 구성되어지는 블록
  • void surf(Input IN, inout SurfaceOutputStandard o) 라는 함수로 실제로 Shader의 값을 조절한다.
  • Properties에서 선언한 속성의 값을 사용하기 위해서는 위에서 선언된 이름과 같게 변수를 선언 후 사용하면 된다.

_R, _G, _B값을 Shader의 Albedo값에 대입

  • Input은 Unity에서 넘어오는 UV데이터
  • SurfaceOutputStandard 구조체 안에 들어있는 멤버
struct SurfaceOutputStandard
{
    fixed3 Albedo;      // base (diffuse or specular) color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Metallic;      // 0=non-metal, 1=metal
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};

결과물

Shader "Custom/Test"
{
	Properties
	{
		_R ("R", Range(0,1)) = 1
		_G ("G", Range(0,1)) = 1
		_B ("B", Range(0,1)) = 1
	}
	SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 200

		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		struct Input
		{
			float2 uv_MainTex;
		};

		float _R;
		float _G;
		float _B;

		void surf(Input IN, inout SurfaceOutputStandard o)
		{
			o.Albedo = float3(_R, _G, _B);
           		//fixed3은 float3으로도 가능하다.
			o.Alpha = 1;
		}
		ENDCG
	}
		FallBack "Diffuse"
}

 

* Unity 내에서의 RGB는 빛의 3원색을 따른다.