UnityでShaderの入力パラメータとして行列を渡す

NegativeMind
5 min readJan 29, 2015

--

久しぶりにCG系の話題。かなりニッチな需要だとは思うけど、UnityのShader Labのお作法の話。

UnityのShaderで、入力パラメータ(プロパティ)として渡せるデータ型を調べてたら、なんと行列(Matrix4x4)もいけるらしいと分かったのでメモしておく。
Shader Lab関連の公式ドキュメントには行列の入力についてちゃんと載っていなかったんだけど、フォーラムの方にあった。

こちらが公式ドキュメントの入力パラメータ一覧↓

ShaderLab文法:プロパティ

Properties { Property [Property …] }
プロパティのブロックを定義します。波括弧{}の中で次のように複数のプロパティを定義します。

name (“display name”, Range (min, max)) = number
floatプロパティを定義し、インスペクタ上でスライドバーがminからmaxとして表現。

name (“display name”, Color) = (number,number,number,number)
colorプロパティを定義。

name (“display name”, 2D) = “name” { options }
2Dテクスチャプロパティを定義。

name (“display name”, Rect) = “name” { options }
長方形(2のべき乗でない)テクスチャのプロパティを定義。

name (“display name”, Cube) = “name” { options }
キューブマップテクスチャのプロパティを定義。

name (“display name”, Float) = number
floatプロパティを定義

name (“display name”, Vector) = (number,number,number,number)
4コンポーネント ベクトルのプロパティを定義。

ここに列挙されているのはGUIのInspectorからいじれる入力パラメータのみで、実はScript経由だと他の型もパラメータとして入力できるという話。



そして、フォーラムの以下に行列(Matrix4x4)をプロパティとして渡す方法が載っている。

How to set Matrix as a Property?

Script側

...

public class ObjectBlur : MonoBehaviour {
...
protected void LateUpdate() {
Vector4 currentPosition = transform.position;
currentPosition.w = 1f;
// Calculate ModelView matrices
Matrix4x4 _mv = CameraInfo.ViewMatrix*transform.localToWorldMatrix;
Matrix4x4 _mvPrev = CameraInfo.PrevViewMatrix*m_prevModelMatrix;
// Give material the matrices it needs
[COLOR="red"] m_stretchMaterial.SetMatrix("_mv", _mv);
m_stretchMaterial.SetMatrix("_mvPrev", _mvPrev);
m_stretchMaterial.SetMatrix("_mvInvTrans", _mv.transpose.inverse);
m_stretchMaterial.SetMatrix("_mvpPrev", CameraInfo.PrevViewProjMatrix*m_prevModelMatrix);[/COLOR]
// Record our previous transform
m_prevModelMatrix = transform.localToWorldMatrix;
}

...
}

Shader側

Shader "Hidden/Motion Vectors" {
SubShader {
Tags { "RenderType"="Moving" }
Pass {
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

...

[COLOR="red"] uniform float4x4 _mv;
uniform float4x4 _mvPrev;
uniform float4x4 _mvInvTrans;
uniform float4x4 _mvpPrev;[/COLOR]

...
ENDCG
}
}
...
}

というように、4×4の行列をパラメータとしてShaderに渡したい場合は、ScriptからMaterialクラスのSetMatrix関数を使って行列を渡せるみたい。

Material.SetMatrix

public void SetMatrix(string propertyName, Matrix4x4 matrix);
public void SetMatrix(int nameID, Matrix4x4 matrix);

SetMatrixのサンプルコードがJavascriptオンリーなのはなぜだ。

Originally published at NegativeMindException.

--

--