シェーダーを使って Unity の マテリアルインスペクター でアーティストが設定するパラメーターを定義できます。シェーダーファイル の Properties ブロックで定義します。
Properties { Property [Property ...] }
Properties ブロックを定義します。波括弧 {} の中で以下のように複数のプロパティを定義します。
name ("display name", Range (min, max)) = number
name ("display name", Float) = number
name ("display name", Int) = number
これらはすべて数 (数量) プロパティのデフォルト値を決定します。Range
は、スライダーの最小 ( min ) と最大 ( max ) の範囲を表示します。
name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)
指定した RGBA 成分のデフォルト値をもつカラープロパティや、デフォルト値をもつ 4D ベクトルプロパティを定義します。カラープロパティにはカラーピッカーがついており、色空間にしたがい、必要に応じて調整されます ( シェーダープログラムのプロパティ を参照 )。ベクトルのプロパティは、4 つの数値フィールドで表示されます。
name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}
name ("display name", 3D) = "defaulttexture" {}
これらはそれぞれ、2D テクスチャ、キューブマップ、3D (体積) プロパティを決定します。
シェーダー内の各プロパティは name で参照されます (Unity では、シェーダーのプロパティ名をアンダースコアで始めるのが一般的)。プロパティはマテリアルインスペクター上で display name で表示されます。各プロパティのデフォルト値は等号 (=) のあとに記述します。
後で、シェーダーの固定関数では、括弧で囲ったプロパティネーム [name] でプロパティの値にアクセスできます。例えば、2 つの int プロパティを宣言して (例えば “SrcBlend“ と ”DstBlend”)、マテリアルプロパティで駆動するブレンドモードを作成し、Blend [_SrcBlend] [_DstBlend]
のように Blend コマンド を通してそれらを使用します。
Properties
ブロックにあるシェーダーパラメーターは、マテリアル のデータとしてシリアライズされます。 シェーダープログラム は、実際には、マテリアルに設定されているより多くのパラメーター (行列、ベクトル、float など) を実行時にコードから得ることが可能です。しかし、それらが Properties ブロックに含まれていない場合、値は保存されません。 これは主に、スクリプトコード ( Material.SetFloat や類似の関数を使用) によって完全に操作される値に役立ちます。
プロパティの前に、[] で囲ったオプションの属性を指定することができます。Unity が認識できる属性を使用するか、独自の MaterialPropertyDrawer クラス で属性をどのように マテリアルインスペクター に表示するかを指定します。Unity が認識できる属性は以下の通りです。
[HideInInspector]
- マテリアルインスペクターでプロパティ値を表示しません。[NoScaleOffset]
- 属性のテクスチャプロパティに関して、マテリアルインスペクターの texture tiling/offset 欄を表示しません。[Normal]
- テクスチャプロパティが法線マップであることを示しています。[HDR]
- テクスチャプロパティが High Dramatic Range (HDR) テクスチャであることを示しています。[Gamma]
- Float/Vector プロパティが UI で sRGB 値で指定されており (色とまったく同じ)、使用するカラースペースに応じて変換が必要な可能性があることを示しています。詳しくは、Cg/HLSL でシェーダープロパティを参照するを参照してください。[PerRendererData]
- テクスチャプロパティは、MaterialPropertyBlock の各レンダラーのデータから作られることを示しています。このプロパティで、マテリアルインスペクターのテクスチャフィールドの UI が変わります。// water シェーダーのプロパティ
Properties
{
_WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // スライダー
_ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5
_RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4
_RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // カラー
_ReflectionTex ("Environment Reflection", 2D) = "" {} // テクスチャ
_RefractionTex ("Environment Refraction", 2D) = "" {}
_Fresnel ("Fresnel (A) ", 2D) = "" {}
_BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}
Unity 5 の前は、テクスチャプロパティは、波括弧ブロック内に TexGen CubeReflect
のようなオプションを持つことができました。これらは、テクスチャ座標を生成する固定関数を制御していました。この機能は Unity 5.0 では削除されました。TexGen が必要な場合は、代わりに 頂点シェーダー を記述する必要があります。例は、 シェーダーに固定関数 TexGen を実装する を参照してください。
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.