#pragma shader_feature는 #pragma multi_compile과 매우 유사하다
이를 선택하는 기준은, 런타임에서 C#으로 변경할 여지를 줄 것인가? 말것인가?에 판별하면 좋다.
Multi_Compile는 런타임에 C#으로 변경을 줄 수 있다, 그의 반대는 Shader_feature이다.
Shader_Feature와 Multi_Compile의 특징을 알아보자
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ALPHAPREMULTIPLY_ON
#pragma shader_feature _NORMALMAP
#pragma shader_feature _EMISSION
#pragma shader_feature _RECEIVE_SHADOWS_OFF
#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
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 (김한용)