LoginSignup
176

More than 5 years have passed since last update.

[Unity] シェーダで使える定義済値

Last updated at Posted at 2014-09-19

Unityのシェーダを書いていて、定義済の値を毎回調べているのでいい加減メモ。
ちなみにドキュメントはこちら

Programmable Shaders で使用する場合は UnityCG.cginc file をインクルードする必要があります。

変換

TYPE NAME DESCRIPTION
float4x4 UNITY_MATRIX_MVP 現在のモデルビュー行列×射影行列 (model*view*projection)
float4x4 UNITY_MATRIX_MV 現在のモデルビュー行列
float4x4 UNITY_MATRIX_V 現在のビュー行列
float4x4 UNITY_MATRIX_P 現在の投影行列
float4x4 UNITY_MATRIX_VP 現在のビュー行列×射影行列
float4x4 UNITY_MATRIX_T_MV モデルビュー行列の転置行列
float4x4 UNITY_MATRIX_IT_MV モデルビュー行列の逆行列の転置行列
float4x4 UNITY_MATRIX_TEXTURE0 to UNITY_MATRIX_TEXTURE3 テクスチャの転置行列
float4x4 _Object2World 現在のモデル行列
float4x4 _World2Object 現在のモデル行列の逆行列
float3 _WorldSpaceCameraPos ワールド座標系のカメラの位置
float4 unity_Scale xyz コンポーネントを使用せず、.w が均一に拡大・縮小するスケールを保持しています

ベクトル

TYPE NAME DESCRIPTION
float4 UNITY_LIGHTMODEL_AMBIENT Current ambient color

ライティング

シンプルなShadelabでは、次のプロパティにアクセスするには末尾に0を付与、すなわちライトのModel×LightColorは_ModelLightColor0 となります。
Cgシェーダでは、ひとつの要素をもった配列としてアクセスできるため、Cgでの相当する表現は_ModelLightColor[0] です。

NAME TYPE VALUE
_ModelLightColor float4 マテリアルのModel×Light color。
_SpecularLightColor float4 マテリアルのSpecular×Light color。
_ObjectSpaceLightPos float4 オブジェクト座標系でのライトの位置。wコンポーネントが 0 の場合は指向性ライト、1 の場合はその他のライト。
_Light2World float4x4 ライトからワールド座標系の行列。
_World2Light float4x4 ワールドからライト座標系の行列。
_Object2Light float4x4 オブジェクトからライト座標系の行列。

その他

TYPE NAME DESCRIPTION
float4 _Time 時間 (t/20、t、t×2、t×3)、シェーダの中でアニメーションするのに使用。
float4 _SinTime 時間のサイン関数: (t/8、t/4、t/2、t)
float4 _CosTime 時間のコサイン関数: (t/8、t/4、t/2、t)
float4 unity_DeltaTime デルタ時間: (dt、1/dt、smoothDt、1/smoothDt)
float4 _ProjectionParams x は 1.0 または -1.0、反転した射影行列で現在レンダリングしている場合は負の値。
y は カメラのNear Plane
z は カメラのFar Plane
w は 1/Far Plane。
float4 _ScreenParams x は 現在のレンダリングターゲットのピクセル幅
y は 現在のレンダリングターゲットのピクセル高さ
z は 1.0 + 1.0/幅
w は 1.0 + 1.0/高さ

Platform difference helpers

環境の違いを吸収してくれるいくつかの定義値があります。
以下はドキュメントの引用です。

Direct use of these platform macros is discouraged, since it’s not very future proof. For example, if you’re writing a shader that checks for D3D9, then maybe in the future the check should be extended to include D3D11. Instead, Unity defines several helper macros (in HLSLSupport.cginc) to help with that.

  • UNITY_ATTEN_CHANNEL - which channel of light attenuation texture contains the data; used in per-pixel lighting code. Defined to either ‘r’ or ‘a’.
  • UNITY_HALF_TEXEL_OFFSET - defined on platforms that need a half-texel offset adjustment in mapping texels to pixels (e.g. Direct3D 9).
  • UNITY_UV_STARTS_AT_TOP - always defined with value or 1 or 0; value of one is on platforms where texture V coordinate is zero at “top of the texture”. - Direct3D-like platforms use value of 1; OpenGL-like platforms use value of 0.
  • UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE - defined if a platform might emulate shadow maps or depth textures by manually rendering depth into a texture.
  • UNITY_PROJ_COORD(a) - given a 4-component vector, return a texture coordinate suitable for projected texture reads. On most platforms this returns the given value directly.
  • UNITY_NEAR_CLIP_VALUE - defined to the value of near clipping plane; Direct3D-like platforms use 0.0 while OpenGL-like platforms use –1.0.
  • UNITY_COMPILER_CG, UNITY_COMPILER_HLSL or UNITY_COMPILER_HLSL2GLSL determine which underlying shader compiler is used; use in case of subtle syntax differences force you to write different shader code.
  • UNITY_CAN_COMPILE_TESSELLATION - defined when the shader compiler “understands” tessellation shader HLSL syntax (currently only D3D11).
  • UNITY_INITIALIZE_OUTPUT(type,name) - initialize variable name of given type to zero.
  • UNITY_COMPILER_HLSL, UNITY_COMPILER_HLSL2GLSL, UNITY_COMPILER_CG - indicates which shader compiler is being used to compile shaders. Respectively, Microsoft’s HLSL (used for DX11, Xbox360, WinRT), HLSL to GLSL translator (used for iOS/Android and desktop OpenGL when #pragma glsl), and NVIDIA’s Cg (used for D3D9 and non-GLSL desktop OpenGL targets). Use this if you run into very specific corner case shader syntax handling differences between the compilers, and want to write different code for each compiler.

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
176