【UnityShader】ShaderLabのプロパティ属性 【2】#78
前回の成果
ShaderLabのプロパティの属性について理解した。
今回やること
前回に引き続き、ShaderLabのプロパティの属性について解説します。
Toggle
対象のプロパティがチェックボックスになり、ONのときは1をOFFのときは0を返します。
プロパティがintもしくは、floatの時に使用できます。
Properties { _MainTex ("Texture", 2D) = "white" {} [Toggle] _ToggleFlag ("Toggle Flag", int) = 0 }
また、Toggleはプリプロセッサで使用することができます。
Toggleの活用例
Toggleとshader_featureを合わせることにより、プリプロセッサとして使用します。
Shader "Unlit/toggleTest" { Properties { [Toggle] _IsBlack ("Is Black", int) = 0 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma shader_feature _ _ISBLACK_ON #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; }; fixed4 _Color; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { #ifdef _ISBLACK_ON return fixed4(0, 0, 0, 1); #else return fixed4(1, 1, 1, 1); #endif } ENDCG } } }
色を変えるだけのシェーダーになります。
ToggleをOff
ToggleをOn
shader_feature
キーワードを定義することが出来るものになります。
// Hogeをキーワードとして定義
#pragma shader_feature _ Hoge
また、複数個定義することもできます。
// Hoge、Fuga、Bar、Fooをキーワードとして定義
#pragma shader_feature _ Hoge Fuga Bar Foo
似たような挙動をするものとして、multi_compileもあります。
// Hogeをキーワードとして定義
#pragma multi_compile _ Hoge
shader_featureは、実際に使われているバリアントだけを生成します。
multi_compileは、定義されているキーワード全てのバリアントを生成します。
詳しくはこちらのサイト様を参考にしてください。
MaterialToggle
上記のToggleと同じ使い方をすることができます。
Properties { _MainTex ("Texture", 2D) = "white" {} [MaterialToggle] _MaterialToggleFlag ("MaterialToggle Flag", int) = 0 }
Toggleとの違いは不明でしたが、PixelSnapでMaterialToggleを使用するケースが多いようです。
PixelSnapとは、ドット絵をタイリングした際に出てきてしまう不要な線を消したりすることが出来る機能となります。
プロパティとは話がズレてしまうので、割愛させていただきます。
詳しくは以下サイト様が分かりやすかったです。
Enum
値を選択できるプルダウンを表示します。
プロパティがintもしくは、floatの時に使用できます。
また、最大7つまで設定することができます。
Properties { _MainTex ("Texture", 2D) = "white" {} [Enum (ZERO, 0, ONE, 1, TWO, 2)] _Value ("Value", int) = 0 }
namespace名.enum名を指定していすると、C#のenumをプルダウンとして表示することができます。
シェーダー側
Properties { _MainTex ("Texture", 2D) = "white" {} [Enum (ZERO, 0, ONE, 1, TWO, 2)] _Value ("Value", int) = 0 [Enum (EnumTest.COLOR)] _Color ("Color", float) = 0 }
C#側
namespace EnumTest { public enum COLOR { RED, GREEN, BLUE } }
KeywordEnum
基本的にEnumと同じになります。
こちらは最大9つまで設定することができます。
Properties { _MainTex ("Texture", 2D) = "white" {} [KeywordEnum (RED, GREEN, BLUE)] _Color ("Color", float) = 0 }
KeywordEnumは値をキーワードにすることができます。
キーワードにする
Shader "Unlit/keywordEnumTest" { Properties { [KeywordEnum (RED, GREEN, BLUE)] _Color ("Color", float) = 0 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma shader_feature _COLOR_RED _COLOR_GREEN _COLOR_BLUE #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; }; struct v2f { float4 vertex : SV_POSITION; }; fixed4 _Color; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); return o; } fixed4 frag (v2f i) : SV_Target { #ifdef _COLOR_RED return fixed4(1, 0, 0, 1); #elif _COLOR_GREEN return fixed4(0, 1, 0, 1); #elif _COLOR_BLUE return fixed4(0, 0, 1, 1); #else return fixed4(1, 1, 1, 1); #endif } ENDCG } } }
色を変えるだけのシェーダーになります。
Toggleと同じように、shader_featureやmulti_compileを使用します。
またシェーダーキーワードは、プロパティ名_表示名となります。
REDを指定
GREENを指定
BLUEを指定
IntRange
指定された範囲のint型のスライダーを表示します。
Properties { _MainTex ("Texture", 2D) = "white" {} _RangeValue (" Range Value", Range(0, 1)) = 0 [IntRange] _IntRangeValue ("Int Range Value", Range(0, 1)) = 0 }
比較用に、通常のRangeも添付します。
PowerSlider
スライダーを指数関数的にすることができます。
値を1にすることにより、通常のRangeと同じ挙動をします。
各値が0.5に対して、スライダーの位置が違うのがわかると思います。
Properties { _MainTex ("Texture", 2D) = "white" {} [PowerSlider(0.1)] _SliderA ("Slider A", Range(0, 1)) = 0 [PowerSlider(1.0)] _SliderB ("Slider B", Range(0, 1)) = 0 [PowerSlider(2.0)] _SliderC ("Slider C", Range(0, 1)) = 0 }
比較用に値の動きも添付します。
[PowerSlider(0.1)]
[PowerSlider(1.0)]
[PowerSlider(2.0)]
Header
プロパティの上にヘッダーを表示します。
Properties { _MainTex ("Texture", 2D) = "white" {} [Header(Hoge Massage)] _Bar ("Bar", int) = 0 [Header(Fuga Massage)] _Doo ("Foo", int) = 0 }
ソースコード
Shader "Unlit/attribute" { Properties { _MainTex ("Texture", 2D) = "white" {} } // 一覧用 // HideInInspector // inspectorから値を非表示にする // Properties { // _MainTex ("Texture", 2D) = "white" {} // [HideInInspector] _HideValue ("HideValue", int) = 0 // _ShowValue ("ShowValue", int) = 0 // } // NoScaleOffset // ScaleとOffsetを非表示にする // Properties { // [NoScaleOffset]_MainTex ("Texture", 2D) = "white" {} // } // Normal // 法線マップかを判断する // Properties { // [Normal]_MainTex ("Texture", 2D) = "white" {} // } // HDR // HDRテクスチャかを確認する // Properties { // [HDR]_MainTex ("Texture", 2D) = "white" {} // } // Gamma // float,Vectorを色空間変換に適応させる // Properties { // _MainTex ("Texture", 2D) = "white" {} // [Gamma] _GammaFloat ("Gamma Float", float) = 0 // [Gamma] _GammaVector ("Gamma Vector", Vector) = (0, 0, 0, 0) // } // PerRendererData // MaterialPropertyBlockクラスのインスタンス時の値で初期化 // Properties { // [PerRendererData]_MainTex ("Texture", 2D) = "white" {} // } // MainTexture // 対象のプロパティをメインテクスチャとする // Properties { // _MainTex ("Texture", 2D) = "white" {} // [MainTexture]_SubTex1 ("Sub Texture1", 2D) = "white" {} // // MainTextureと見なされない // [MainTexture]_SubTex2 ("Sub Texture2", 2D) = "white" {} // } // MainColor // 対象のプロパティをメインカラーとする // Properties { // _MainTex ("Texture", 2D) = "white" {} // _Color ("Color", Color) = (1, 1, 1, 1) // [MainColor]_SubColor1 ("Sub Color1", Color) = (1, 1, 1, 1) // // MainColorと見なされない // [MainColor]_SubColor2 ("Sub Color2", Color) = (1, 1, 1, 1) // } // Space // 対象のプロパティの上にスペースを入れる // Properties { // _MainTex ("Texture", 2D) = "white" {} // _NotSpace ("Not Space", int) = 0 // [Space] _SpaceA ("Space A", int) = 0 // [Space(50)] _SpaceB ("Space B", int) = 0 // [Space(100)] _SpaceC ("Space C", int) = 0 // } // Toggle // int,floatをチェックボックスにする // Properties { // _MainTex ("Texture", 2D) = "white" {} // [Toggle] _ToggleFlag ("Toggle Flag", int) = 0 // } // MaterialToggle // int,floatをチェックボックスにする // Properties { // _MainTex ("Texture", 2D) = "white" {} // [MaterialToggle] _MaterialToggleFlag ("MaterialToggle Flag", int) = 0 // } // Enum // int,floatを列挙する // Properties { // _MainTex ("Texture", 2D) = "white" {} // [Enum (ZERO, 0, ONE, 1, TWO, 2)] _Value ("Value", int) = 0 // // namespace名.enum名でC#のenumを設定できる // [Enum (EnumTest.COLOR)] _Color ("Color", float) = 0 // } // KeywordEnum // キーワードにすることができるEnum // Properties { // _MainTex ("Texture", 2D) = "white" {} // [KeywordEnum (RED, GREEN, BLUE)] _Color ("Color", float) = 0 // } // IntRange // 指定された範囲のint型のスライダーを表示 // Properties { // _MainTex ("Texture", 2D) = "white" {} // _RangeValue (" Range Value", Range(0, 1)) = 0 // [IntRange] _IntRangeValue ("Int Range Value", Range(0, 1)) = 0 // } // PowerSlider // スライダーを指数関数的にする // Properties { // _MainTex ("Texture", 2D) = "white" {} // [PowerSlider(0.1)] _SliderA ("Slider A", Range(0, 1)) = 0 // [PowerSlider(1.0)] _SliderB ("Slider B", Range(0, 1)) = 0 // [PowerSlider(2.0)] _SliderC ("Slider C", Range(0, 1)) = 0 // } // Header // ヘッダーを表示 // Properties { // _MainTex ("Texture", 2D) = "white" {} // [Header(Hoge Massage)] // _Bar ("Bar", int) = 0 // [Header(Fuga Massage)] // _Doo ("Foo", int) = 0 // } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); return col; } ENDCG } } }
コピペ用等に一覧のソースコードを添付します。
今回は以上となります。
ここまでご視聴ありがとうございました。