In the Built-in Render Pipeline, the Standard Shader has some extra requirements if you want to modify materials at runtime.
This is because - behind the scenes - the Standard Shader is actually many different shaders rolled into one. These different types of shader are called Shader Variants and can be thought of as all the different possible combinations of the shader’s features, when activated or not activated.
例えば、 法線マップ(Normal Map)(Bump mapping) をマテリアルに適用したい場合は、 Normal Mapping をサポートしているシェーダーの variant をアクティブにします。さらに Heightmap も適用したいのであれば、 Normal Mapping と Height Mapping をサポートしているシェーダーの variant をアクティブにします。
This is a good system, because it means that if you use the Standard Shader, but do not use a Normal Map in a certain material, you are not incurring the performance cost of running the Normal Map shader code - because you are running a variant of the shader with that code omitted. It also means that if you never use a certain feature combination (such as HeightMap & Emissive together), that variant is completely omitted from your build - and in practice you will typically only use a very small number of the possible variants of the Standard Shader.
Unity avoids simply including every possible shader variant in your build, because this would be a very large number, some tens of thousands! This high number is a result not only of each possible combination of features available in the material Inspector, but also there are variants of each feature combination for differing rendering scenarios such as whether or not HDR is being used, lightmaps, GI, fog, etc. Including all of these would cause slow loading, high memory consumption, and increase your build size and build time.
そうならないように、Unity はプロジェクトで使われているマテリアルアセットを調査して、どのバリアントが使われているかを追跡します。プロジェクトに含まれたスタンダードシェーダーのバリアントは、ビルドにも含まれることになります。
スタンダードシェーダーの使用で、スクリプトからマテリアルにアクセスするときに2つの問題があります。
If you use scripting to change a material that would cause it to use a different variant of the Standard Shader, you must enable that variant by using the EnableKeyword function. A different variant would be required if you start using a shader feature that was not initially in use by the material. For example assigning a Normal Map to a Material that did not have one, or setting the Emissive level to a value greater than zero, when it was previously zero.
スタンダードシェーダーの機能を有効にするための特定キーワードは以下のとおりです。
キーワード | 機能 |
---|---|
_NORMALMAP | 法線マップ |
_ALPHATEST_ON | 透明度の “Cut out” レンダリングモード |
_ALPHABLEND_ON | 透明度の “Fade” レンダリングモード |
_ALPHAPREMULTIPLY_ON | 透明度の “Transparent” レンダリングモード |
_EMISSION | 放出色 または 放出マップ |
_PARALLAXMAP | ハイトマップ |
_DETAIL_MULX2 | 補助的 “詳細” マップ (アルベド&法線マップ) |
_METALLICGLOSSMAP | メタリックワークフローの メタリック/スムースネスマップ |
_SPECGLOSSMAP | スペキュラーワークフローの スペキュラー/スムースネスマップ |
上記のキーワードによるスクリプトからのマテリアルの変更は、エディター上で実行中でも問題無く行う事ができます。
ですが、ビルドに含むバリアントを割り出すために Unity がチェックするのは、プロジェクトで使われているマテリアルだけなので、実行中にスクリプトから一時的に変更した だけ のバリアントは含まれません。
これは、例えばスクリプトからマテリアルの _PARALLAXMAP キーワードを有効にしているのに、プロジェクト内に一致する機能の組合せを持つマテリアルが無かった場合、視差マッピングは、たとえエディター上では機能しているように見えたとしても、最終的なビルドでは有効にならない、という事です。なぜなら、バリアントが要求されたように見えず、ビルドから省かれてしまうからです。
このため、そのタイプのマテリアルを少なくとも一つ、アセットに用意して、 Unity にそのシェーダーバリアントを使う事を明示する必要があります。そのマテリアルは シーンで使われていなくてはなりません 。もしくは、 ランタイム時にリソースを読み込んで おくという方法もあります。そうしないと Unity からは、まだ使っていないように見えるため、ビルドから除外されてしまいます。
上記の手順を済ませておけば、実行中にスタンダードシェーダーを使っているマテリアルを思い通りに変更することが可能になります。
シェーダーバリアントの詳細について興味があり、自分で書く場合はどうしたらよいか知りたいのであれば、 複数のシェーダープログラムのバリアントを作る を読んでください。
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.