Apologies if the is RTFM (or RTF Forum maybe), but I’m having some trouble synthesizing the correct projection matrix for a custom vertex shader. I am very new to Unity (only started poking around a couple of days ago), so I am probably doing something stupid.
I am rendering all geometry with custom shaders (DX11). I also have custom property blocks per object. As best I can, I have verified that everything in the pipeline works, so long as I use UNITY_MATRIX_MVP. But as soon as I try to make my own MVP matrix, things go south.
Enclosed are two pictures - one good and one bad. Very sorry the post is long - wanted to be as complete as possible.
Here is the vertex and fragment shader:
float _test;
float4x4 _MVP;
v2f vert( appdata_base v ) {
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); //GOOD!
//o.pos = mul( _MVP, v.vertex ); //BAD!!!
return o;
}
half4 frag (v2f input) : COLOR {
half4 vOutput;
vOutput.rgb =_test;
vOutput.a = 1;
return vOutput;
}
And here is the (likely terrible) script code on the camera to seed the _MVP matrix:
function OnPostRender() {
...
var allRenderers : Renderer[] = FindObjectsOfType(Renderer) as Renderer[];
var testColor = 0.0f;
for (var curRenderer : Renderer in allRenderers) {
var propertyBlock : MaterialPropertyBlock = new MaterialPropertyBlock();
var mModel : Matrix4x4 = curRenderer.transform.localToWorldMatrix;
var mView : Matrix4x4 = Camera.main.worldToCameraMatrix;
var mProj : Matrix4x4 = Camera.main.projectionMatrix;
var mMVP : Matrix4x4 = mProj * mView * mModel;
propertyBlock.AddMatrix ("_MVP", mMVP);
propertyBlock.AddFloat ("_test", testColor);
testColor += .05;
curRenderer.SetPropertyBlock(propertyBlock);
}
cam.RenderWithShader (shader, "RenderType");
}
It looks as if the projection matrix is horked, but in some non-obvious way? Or maybe there is a mystery scale on Y? I am afraid I am so new to Unity I don’t even know how to inspect script values (that’s on tomorrow’s plate). I wonder if Unity has an internal representation of the projection matrix that is not what DX is expecting? Or maybe I just screwed something else up??
Thanks!
Ben

