- Home /
What is the difference between UNITY_MATRIX_MVP and (Camera.projectionMatrix * Camera.worldToCameraMatrix) * unity_ObjectToWorld ?
Hey, I try to obtain camera MVP matrix via C#Script and set to global Matrix. But the results of calculation are different from the built-in UNITY_MATRIX_MVP. Why ?
using UnityEngine;
using System.Collections;
public class LightProjector : MonoBehaviour {
private Camera _lightCam;
private Shader _replaceSdr;
void Awake () {
_lightCam = GetComponent<Camera>();
_replaceSdr = Shader.Find("Hidden/LightPorject/Replace");
if (_replaceSdr != null)
{
_lightCam.SetReplacementShader(_replaceSdr, "RenderType");
}
}
void Update () {
Shader.SetGlobalMatrix("_LightProjectMatrix", _lightCam.projectionMatrix * _lightCam.worldToCameraMatrix);
}
}
ReplaceShader with UNITY_MATRIX_MVP :
Shader "Hidden/LightPorject/Replace"
{
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 normal : TEXCOORD0;
};
float4x4 _LightProjectMatrix;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.normal = mul(unity_ObjectToWorld, float4(v.normal,0)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(i.normal,1);
return col;
}
ENDCG
}
}
}
ReplaceShader with (Camera.projectionMatrix Camera.worldToCameraMatrix) unity_ObjectToWorld :
Shader "Hidden/LightPorject/Replace"
{
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 normal : TEXCOORD0;
};
float4x4 _LightProjectMatrix;
v2f vert (appdata v)
{
v2f o;
float4 wPos = mul(unity_ObjectToWorld, v.vertex);
o.vertex = mul(_LightProjectMatrix, wPos);
o.normal = mul(unity_ObjectToWorld, float4(v.normal,0)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(i.normal,1);
return col;
}
ENDCG
}
}
}
Comment