Version: 2021.2
言語: 日本語
シェーダーキーワード
マテリアルインスペクターでのシェーダーキーワードの使用

C# スクリプトによるシェーダーキーワードの使用

ランタイムにシェーダーキーワードを有効または無効にすることができます。シェーダーキーワードを有効または無効にすると、Unity はレンダリングに適切なシェーダーバリアントを使用します。

ランタイムにシェーダーバリアントを変更すると、パフォーマンスに影響を与えることがあります。キーワードの変更により、そのバリアントを初めて使用する必要がある場合、グラフィックスドライバーがシェーダープログラムを準備する間に不具合が発生する可能性があります。これは、大規模または複雑なシェーダーの場合や、グローバルなキーワードの状態変更が複数のシェーダーに影響する場合に、特に問題となります。これを避けるためには、シェーダーのロードと事前準備の方法でキーワードバリアントを考慮するようにしてください。詳細については、シェーダーのロード を参照してください。

How shader keywords are represented in C#

Unity では、 ローカルシェーダーキーワードグローバルシェーダーのキーワード があります。

  • Local shader keywords represent the keywords that you declared in your shader or compute shader source files. When you enable or disable local shader keywords, they affect an individual shader or compute shader.
  • Global shader keywords act as overrides for local shader keywords. When you enable or disable global shader keywords, they can affect multiple shaders and compute shaders at the same time.

ローカルシェーダーキーワード

シェーダーソースファイル内でシェーダーキーワードを宣言する場合、Unity はこれを C# で LocalKeyword 構造体で表現します。これを ローカルシェーダーキーワード と呼びます。

The isOverridable property of a LocalKeyword indicates whether the keyword was declared with a global or local scope in the source file. It is true if the keyword was declared with global scope, and false if it was declared with local scope. This value determines whether this local keyword can be overridden by a global keyword with the same name.

Unity stores all local shader keywords that affect a shader or compute shader in a LocalKeywordSpace struct. For a graphics shader, you can access this with Shader.keywordSpace. For a compute shader, you can access this with ComputeShader-keywordSpace.

グローバルシェーダーキーワード

In addition to the local shader keywords that you declared in your source files, Unity maintains a separate list of global shader keywords. Global shader keywords act as overrides for local shader keywords, and can affect multiple shaders and compute shaders at the same time.

Unity はグローバルシェーダーキーワードを GlobalKeyword 構造体で表します。

グローバルシェーダーキーワードを設定すると、同じシェーダーキーワードを多数のマテリアルやコンピュートシェーダーに対して有効化または無効化する必要がある場合に便利なことがあります。しかしこれは、以下のような不都合をもたらす可能性があります。

  • キーワードのグローバル状態を設定すると、シェーダーが誤って同じ名前のキーワードを定義した場合に、意図しない結果になる可能性があります。キーワードをローカルスコープで宣言するか、衝突の可能性を低減する方法でキーワードを命名することで、これを防ぐことができます。
  • 新しい GlobalKeyword を作成すると、Unity は、この時点でロードされている全てのシェーダーおよびコンピュートシェーダーに関して、グローバルキーワード空間とローカルキーワード空間の間の内部マッピングを更新します。この操作は CPU に高い負荷をかける場合があります。この操作の影響を軽減するには、できるだけ、アプリケーションの開始後すぐに (アプリケーションのロード中に) 全てのグローバルキーワードを作成してください。

Determining whether a shader keyword is enabled or disabled

When a global and local shader keyword with the same name have different states, Unity uses the isOverridable property of a LocalKeyword to determine whether the keyword is enabled or disabled for an individual material or compute shader.

  • isOverridabletrue の場合: 同じ名前のグローバルキーワードが存在し、それが有効になっている場合、Unity は、グローバルキーワードの状態を使用します。それ以外の場合は、ローカルキーワードの状態を使用します。
  • isOverridablefalse の場合: Unity は常にローカルキーワードの状態を使用します。

したがって、個々のマテリアルやコンピュートシェーダーに関してシェーダーキーワードが有効か無効か把握するには、isOverridable プロパティの状態と、グローバルキーワードやローカルキーワードの状態を調査する必要があります。

以下の例は、特定のマテリアルについて、Unity がキーワードを有効と見なすか無効と見なすか確認する方法を示しています。

using UnityEngine;
using UnityEngine.Rendering;

public class KeywordExample : MonoBehaviour
{
    public Material material;

    void Start()
    {
        CheckShaderKeywordState();
    }

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

        // Get all the local keywords that affect the Shader
        var keywordSpace = shader.keywordSpace;

        // Iterate over the local keywords
        foreach (var localKeyword in keywordSpace.keywords)
        {
            // If the local keyword is overridable (i.e., it was declared with a global scope),
            // and a global keyword with the same name exists and is enabled,
            // then Unity uses the global keyword state
            if (localKeyword.isOverridable && Shader.IsKeywordEnabled(localKeyword.name))
            {
                Debug.Log("Local keyword with name of " + localKeyword.name + " is overridden by a global keyword, and is enabled");
            }
            // Otherwise, Unity uses the local keyword state
            else
            {
                var state = material.IsKeywordEnabled(localKeyword) ? "enabled" : "disabled";
                Debug.Log("Local keyword with name of " + localKeyword.name + " is " + state);
            }            
        }
    }
}

To check whether a local keyword is enabled for a graphics shader, use Material.IsKeywordEnabled or Material.enabledKeywords. For a compute shader, use ComputeShader.IsKeywordEnabled, or [ComputeShader.enabledKeywords].

To check whether a global keyword is enabled, use Shader.IsKeywordEnabled or Shader.enabledGlobalKeywords or ComputeShader.enabledKeywords.

シェーダーキーワードの有効化と無効化

グラフィックスシェーダーのローカルシェーダーキーワードを有効化または無効化するには、Material.SetKeywordMaterial.EnableKeyword、または Material.DisableKeyword を使用してください。コンピュートシェーダーには、ComputeShader.SetKeywordComputeShader.EnableKeyword、または ComputeShader.DisableKeyword を使用してください。

グローバルシェーダーキーワードを有効化または無効化するには、Shader.EnableKeywordShader.DisableKeyword を使用してください。

コマンドバッファでグローバルキーワードを有効化または無効化するには、CommandBuffer.EnableKeywordCommandBuffer.DisableKeyword を使用してください。

ランタイムにキーワードのセットを管理する

シェーダーをオーサリングする際は、キーワードをセットで宣言します。セットには、互いに排他的なキーワードが含まれます。

ランタイムでは、Unity はこれらのセットの概念を持ちません。Unity では、任意のキーワードを個別に有効または無効にすることができ、あるキーワードを有効または無効にしても、他のキーワードの状態には影響しません。つまり、同じセットから複数のキーワードを有効にしたり、セット内のすべてのキーワードを無効にしたりすることが可能です。

セット内の複数のキーワードが有効になっている場合や、セット内のキーワードが 1 つも有効になっていない場合、Unity は “十分に適合する” と考えられる (1 つの) バリアントを選択します。正確に何が起こるかについては保証されておらず、意図しない結果になる可能性があります。キーワードの状態を慎重に管理して、このような状況を避けることが最善の策です。

シェーダーキーワード
マテリアルインスペクターでのシェーダーキーワードの使用