본문 바로가기

공부/테크니컬 아티스트를 위한 유니티 쉐이더 스타트업

[Unity Shader] 기본 Shader 구성 파악하기

728x90
반응형

 

  • 기본적으로 Unity에서 StandardSurface Shader를 생성하면 나오는 형태는 아래와 같다.
Shader "Custom/StandardSurface"
{
    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"
}

 

 

 


 

 

Shader를 통해 메테리얼을 만들었을 때 Inspector 창에서는 다음과 같다.

Shader "Custom/StandardSurface"
  • Shader이름을 나타낸다.
  • /를 통해 하위 내용을 나열할 수 있다.

 

 

    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
    }
  • 말그대로 ShaderProperties들을 선언 및 정의하는 부분이다.
  • _Color, _MainTex ... 등은 Shader 코드 내에서 사용되는 변수명이다. 실제 Shader 코드에서는 해당 이름으로 접근 및 참조한다.
  • "Color", "Albedo (RGB)" ... 등은 실제 변수명은 아니지만 Inspector창에서 보여지는 이름이다.
  • Color, 2D ... 등은 쉽게 말하면 해당 변수의 자료형을 나타낸 모습이고, = 으로 표시된 모습은 기본값이라고 이해하면 편하다.

 

        struct Input
        {
            float2 uv_MainTex;
        };
  • Input 구조체유니티 엔진 내부로부터 받는 데이터들을 가져오기 위해 사용한다.
  • 외부 인터페이스에서 받는 값이 아닌(Properties..) 유니티 내부로부터 받는 데이터 Input 구조체 안에서 선언되어야 한다.
  • Vertex 내부에 있는 UV에 접근하기 위해서는 다음과 같이 Input 구조체에서 uv_MainTex를 선언하고 참조해야한다.

 

        sampler2D _MainTex;
        
        ...
        
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
  • CGPROGRAMENDCG 사이에서 함수가 아닌 곳에서는 다음과 같이 사용할 변수들을 선언할 수 있다.
  • 다음 변수들을 선언해두고 함수에서 사용한다.

 

        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;
        }
  • Shader 코드에서 Shader가 실제로 어떤 방식으로 동작하게끔 만드는 함수 부분이다.
  • Shader의 색상이나 텍스처 등의 처리를 하는 부분

 

 

728x90