Shader_feature와 Multi_Compile의 차이

셰이더

목록 보기
2/3

소개

#pragma shader_feature#pragma multi_compile매우 유사하다

이를 선택하는 기준은, 런타임에서 C#으로 변경할 여지를 줄 것인가? 말것인가?에 판별하면 좋다.

Multi_Compile는 런타임에 C#으로 변경을 줄 수 있다, 그의 반대는 Shader_feature이다.

특징

Shader_Feature와 Multi_Compile의 특징을 알아보자

  • Shader Feature : 사용되지 않으면 게임 빌드에 코드가 포함되지 않는다.
    즉, Material에서 노말맵을 쓸거냐 안쓸거냐, Emission 연산을 할거냐 안할거냐는 것 처럼 Material 옵션등에 사용
#pragma shader_feature _ALPHATEST_ON

#pragma shader_feature _ALPHAPREMULTIPLY_ON

#pragma shader_feature _NORMALMAP

#pragma shader_feature _EMISSION

#pragma shader_feature _RECEIVE_SHADOWS_OFF
  • Multi Compile : 라이트가 있냐? 셰도우가 있냐? 등등의 전체 설정일때 주로 적용한다.
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS

#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE

#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS

#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS

#pragma multi_compile _ _SHADOWS_SOFT

#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE

Multi_Compile을 사용한 예시

Shader "UnityShaderTutorial/shader_variants" {
    Properties {
    } 
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vertexShader
            #pragma fragment fragmentShader

            #pragma multi_compile RED GREEN BLUE //핵심
            //#pragma shader_featulre RED GREEN BLUE
            
            float4 vertexShader (float4 vertex : POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(vertex);
            }

            fixed4 fragmentShader () : SV_Target
            {
                fixed4 col = fixed4 (0, 0, 0, 1);
                # ifdef RED
                    col = fixed4 (1, 0, 0, 1);
                # elif GREEN
                    col = fixed4 (0, 1, 0, 1);
                # elif BLUE
                    col = fixed4 (0, 0, 1, 1);
                # endif
                return col;
            }
            ENDCG
        }
    }
}

이 셰이더는 multi_compile로 RED, GREEN, BLUE를 선언했다.

그리고 각각 ifdef를 통해 셰이더를 변경하고 있다.

로컬 키워드 방식

using UnityEngine;
using UnityEngine.Rendering;

public class MaterialKeywordExample : MonoBehaviour
{
    public Material material;
    private LocalKeyword exampleFeatureKeyword;

    void Start()
    {
        // Get the instance of the Shader class that this material uses
        var shader = material.shader;

        // Create and cache the LocalKeyword
        exampleFeatureKeyword = new LocalKeyword(shader, "EXAMPLE_FEATURE_ON");
    }

    public void EnableExampleFeature()
    {
        material.EnableKeyword(exampleFeatureKeyword);
    }

    public void DisableExampleFeature()
    {
        material.DisableKeyword(exampleFeatureKeyword);
    }
}

글로벌 키워드 방식

public class GlobalKeywordExample : MonoBehaviour
{
    private GlobalKeyword exampleFeatureKeyword;

    private void Start()
    {
        var exampleFeatureKeyword = GlobalKeyword.Create("EXAMPLE_FEATURE_ON");
    }

    public void EnableExampleFeature()
    {
        Shader.EnableKeyword(exampleFeatureKeyword);
    }

    public void DisableExampleFeature()
    {
        Shader.DisableKeyword(exampleFeatureKeyword);
    }
}

후원

이 내용이 맛있었다면 저에게 맛있는 커피를 후원해주세요! ☕

토스 익명 송금 : 후원하기
계좌 송금 : 토스뱅크 1000-0586-4766 (김한용)

profile
유니티를 통한 스페셜 테크닉만 다루는 독특한 개발자

0개의 댓글