知識0からのUnityShader勉強

知識0からのUnityShader勉強

UnityのShaderをメインとして、0から学んでいくブログです。

【UnityShader】ShaderLabのプロパティ属性 【2】#78

前回の成果

ShaderLabのプロパティの属性について理解した。

soramamenatan.hatenablog.com


今回やること

前回に引き続き、ShaderLabのプロパティの属性について解説します。


Toggle

対象のプロパティがチェックボックスになり、ONのときは1をOFFのときは0を返します。
プロパティがintもしくは、floatの時に使用できます。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    [Toggle] _ToggleFlag ("Toggle Flag", int) = 0
}

f:id:soramamenatan:20201101091613p:plain

また、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

f:id:soramamenatan:20201101092932p:plain

ToggleをOn

f:id:soramamenatan:20201101092935p:plain

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は、定義されているキーワード全てのバリアントを生成します。

詳しくはこちらのサイト様を参考にしてください。

light11.hatenadiary.com


MaterialToggle

上記のToggleと同じ使い方をすることができます。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    [MaterialToggle] _MaterialToggleFlag ("MaterialToggle Flag", int) = 0
}

Toggleとの違いは不明でしたが、PixelSnapでMaterialToggleを使用するケースが多いようです。
PixelSnapとは、ドット絵をタイリングした際に出てきてしまう不要な線を消したりすることが出来る機能となります。
プロパティとは話がズレてしまうので、割愛させていただきます。
詳しくは以下サイト様が分かりやすかったです。

ekulabo.com


Enum

値を選択できるプルダウンを表示します。
プロパティがintもしくは、floatの時に使用できます。
また、最大7つまで設定することができます。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    [Enum (ZERO, 0, ONE, 1, TWO, 2)] _Value ("Value", int) = 0
}

f:id:soramamenatan:20201101100356p:plain

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
    }
}

f:id:soramamenatan:20201101100359p:plain


KeywordEnum

基本的にEnumと同じになります。
こちらは最大9つまで設定することができます。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    [KeywordEnum (RED, GREEN, BLUE)] _Color ("Color", float) = 0
}

f:id:soramamenatan:20201101102440p:plain

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を指定

f:id:soramamenatan:20201101102449p:plain

GREENを指定

f:id:soramamenatan:20201101102446p:plain

BLUEを指定

f:id:soramamenatan:20201101102443p:plain


IntRange

指定された範囲のint型のスライダーを表示します。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    _RangeValue (" Range Value", Range(0, 1)) = 0
    [IntRange] _IntRangeValue ("Int Range Value", Range(0, 1)) = 0
}

f:id:soramamenatan:20201101104119g:plain

比較用に、通常のRangeも添付します。

f:id:soramamenatan:20201101104128g:plain


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
}

f:id:soramamenatan:20201101105129p:plain

比較用に値の動きも添付します。

[PowerSlider(0.1)]

f:id:soramamenatan:20201101105133g:plain

[PowerSlider(1.0)]

f:id:soramamenatan:20201101105145g:plain

[PowerSlider(2.0)]

f:id:soramamenatan:20201101105154g:plain


Header

プロパティの上にヘッダーを表示します。

Properties {
    _MainTex ("Texture", 2D) = "white" {}
    [Header(Hoge Massage)]
    _Bar ("Bar", int) = 0
    [Header(Fuga Massage)]
    _Doo ("Foo", int) = 0
}

f:id:soramamenatan:20201101105633p:plain


ソースコード

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
        }
    }
}

コピペ用等に一覧のソースコードを添付します。

今回は以上となります。
ここまでご視聴ありがとうございました。


参考サイト様

qiita.com

baba-s.hatenablog.com

docs.unity3d.com