ShaderLab シェーダーは単なるハードウェアシェーダー以上の機能を含み、さまざまなことができます。ShaderLab シェーダーは Material インスペクターに表示されたプロパティの記述、さまざまなグラフィックスハードウェア用の複数のシェーダーの実装、固定関数のハードウェア状態の設定などを行います。実際のプログラマブルシェーダー (頂点シェーダーやフラグメントシェーダーのプログラムなど) は ShaderLab のシェーダーのコンセプトの単なる一部です。導入的な説明は、シェーダー: 頂点とフラグメントプログラム を参照してください。ここでは低レベルなハードウェアシェーダーを シェーダープログラム と呼ぶことにします。
ライティングと相互作用するシェーダーを作成する場合は、サーフェスシェーダー のドキュメントを参照してください。いくつか例を見るには 頂点シェーダーとフラグメントシェーダーの例 を参照してください。このページの残りの部分は、シェーダーが Unity のライト (例えば、特殊効果、ポストプロセスエフェクト など) と相互作用しないことを前提としています。
シェーダープログラムは HLSL 言語 で記述され、シェーダーテキストに “スニペット” を埋め込んで Pass コマンドの任意の場所に置かれます。 通常は以下のようになります。
Pass {
// ...通常の pass 設定...
CGPROGRAM
// このスニペットのコンパイラーディレクティブ。例:
#pragma vertex vert
#pragma fragment frag
// the Cg/HLSL code itself
ENDCG
// ... pass 設定の残り ...
}
HLSL プログラムスニペットは CGPROGRAM と ENDCG のキーワードの間、または、HLSLPROGRAM と ENDHLSL の間に記述します。2 番目の形式では、自動的には HLSLSupport と UnityShaderVariables ビルトインヘッダーファイル は 含まれません 。
スニペットの開始で、コンパイラーディレクティブは #pragma ステートメントではじまります。ディレクティブはどのシェーダー関数を使ってコンパイルするかを示します。
その他のコンパイラーディレクティブ
それぞれのスニペットには最低でも頂点プログラムとフラグメントプログラムが含まれていなければいけません。それゆえに #pragma vertex と #pragma fragment 命令が必要になります。
以下のコンパイル命令は、Unity 5.0 上で実行しても何も発生せず、安全に削除できます。#pragma glsl
、#pragma glsl_no_auto_normalization
、#pragma profileoption
、#pragma fragmentoption
Unity は #pragma ディレクティブのみをシェーダーファイルでサポートし、インクルードファイルではサポートしません。
Unity はいくつかのレンダリング API (例えば Direct3D 11 と OpenGL) をサポートしており、デフォルトではすべてのシェーダープログラムは、サポートされているすべてのレンダラーにコンパイルされます。#pragma only_renderers または #pragma exclude_renderers ディレクティブを使用してどのレンダラーをコンパイルするかを指定できます。これは、一部のプラットフォームでは使用不可であることが分かっているシェーダー言語の機能を、明示的に使用する場合に最も役立ちます。サポートされているレンダラー名は以下のとおりです。
例えば、次の行は、シェーダーを D3D11 モードにのみコンパイルします。
#pragma only_renderers d3d11
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.