このページには、ShaderLab コードで Name
ブロックを使用して、Pass (パス) に名前を割り当てるための情報が含まれています。パスの定義については、ShaderLab: パスの定義 を参照してください。シェーダーオブジェクトの仕組み、およびシェーダーオブジェクト、サブシェーダー、パスの関係については、シェーダーの基礎概念 を参照してください。
パスは名前を持つことができます。UsePass
コマンドや、いくつかの C# API では、名前でパスを参照する必要があります。パスの名前は、フレームデバッガー ツールに表示されます。
機能名 | ビルトインレンダーパイプライン | ユニバーサルレンダーパイプライン (URP) | HD レンダーパイプライン (HDRP) | カスタム SRP |
---|---|---|---|---|
ShaderLab: Name ブロック | 可 | 可 | 可 | 可 |
ShaderLab でパスに名前を割り当てるには、Name
ブロックを Pass
ブロックの中に配置します。
シグネチャ | 機能 |
---|---|
Name "<name>" |
パスの名前を設定します。 |
内部的には、Unity は名前を大文字に変換します。ShaderLab コードで名前を参照する場合は、大文字のバリアントを使用する必要があります。例えば、値が “example” の場合、EXAMPLE として参照する必要があります。
同じ SubShader 内の複数のパスが同じ名前の場合、Unity はコードの最初のパスを使用します。
C# スクリプトからパスの名前にアクセスするには、Material.FindPass、Material.GetPassName、ShaderData.Pass.Name などの API を使用します。
ノート: Material.GetShaderPassEnabled と Material.SetShaderPassEnabled は、名前でパスを参照するのではなく、LightMode tag の値を使ってパスを参照します。
このサンプルコードでは、ContainsNamedPass という Shader オブジェクトを作成します。その中に ExampleNamedPass というパスが含まれています。
Shader "Examples/ContainsNamedPass"
{
SubShader
{
Pass
{
Name "ExampleNamedPass"
// パスを定義する残りのコードをここに記述します。
}
}
}
次に、以下の C# コードで、このパスの名前をクエリできます。
using UnityEngine;
public class GetPassName : MonoBehaviour
{
// このスクリプトを MeshRenderer コンポーネントを含むゲームオブジェクトに配置します
void Start() {
// マテリアルを取得します
var material = GetComponent<MeshRenderer>().material;
// マテリアルに割り当てられたシェーダーオブジェクトの
// アクティブなサブシェーダーの最初のパスの名前を取得します
var passName = material.GetPassName(0);
// 名前をコンソールに出力します
Debug.Log(passName);
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.