본문 바로가기

Unity/Shader

2. 기초적인 서피스 셰이더 제작

Project -> Assets 우클릭 -> Create -> Shader -> Standard Surface Shader

를 생성하면 기본 셰이더 코드가 작성된 셰이더 파일이 생성됩니다.

Shader "Custom/BasicSurfaceShader"
{
    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"
}

 

위 코드에서 LOD 부분은 이 쉐이더의 환경 설정에 따른 옵션 값에 관련된 내용입니다.

Level Of Detail을 의미합니다. 환경설정에서 상 중 하 옵션과 연관이 있는 부분입니다.

 

#Pragma target 3.0은 쉐이더 모델 3.0 이상에서만 돌아가게 함으로써 더 복잡한 코드를 쓸 수 있게 제한을 풀어주는 기능입니다.

 

 

아래는 최소한으로 필요한 것들만 남긴 코드입니다.

Shader "NewSurfaceShader01"
{
    Properties
    {
		_Red("Red", Range(0,1)) = 0
		_Green("Green", Range(0,1)) = 0
		_Blue("Blue", Range(0,1)) = 0
		_Bright("Brightness", Range(-1,1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows

        struct Input
        {
			float4 color : COLOR;
        };

        float _Red;
		float _Green;
		float _Blue;
		float _Bright;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            o.Albedo = float3(_Red, _Green, _Blue) + _Bright;
            o.Alpha = 1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

많이 간결해지고 보기 편해진 쉐이더 코드 입니다.

 

아래는 기본적인 텍스처를 출력하는 쉐이더 코드 입니다.

Shader "Custom/TexShader"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows

        sampler2D _MainTex;

		
        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}