- Home /
accessing inverse view matrix
Hi, I'm writing a shader and would like to write my own billboarding, for this I need the inverse view matrix, I'm not sure if unity will let me have this information however.
Is there anyone with shader experience who can give me a hand? Thanks
Answer by Womrwood · Dec 05, 2013 at 12:37 AM
I did a research and I guess that Unity3d does not give you the inverse view matrix, but it gives you the UNITY_MATRIX_V that you can access in your shader, just transpose the upper 3x3 matrix and negate the translation vector to get the inverse.
If you are trying to transform vertices in modelview/world matrix into local space of the object I guess you can use _World2Object.
I'm not sure if I should be multiplying/transposing matrices inside my vertex shader, is that a good idea?
$$anonymous$$y current shader :
Shader "Custom/ParticleShader"
{
Properties
{
_Speed ("Particle Speed", Range (0,10)) = 1
_Height ("Particle Height", Range (0,100)) = 1
}
SubShader
{
Pass
{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _$$anonymous$$ainTex;
float _Speed;
float _Height;
struct v2f {
float4 pos : SV_POSITION;
float3 color : COLOR0;
};
v2f vert (appdata_base v)
{
float t = frac(v.vertex.z + _Speed * _Time);
float s = pow(t, 1);
float3 eyeVector = ObjSpaceViewDir( v.vertex );
float3 upVector = float3(0,1,0);
float3 sideVector = normalize(cross(eyeVector,upVector));
v2f o;
o.pos.x = 20 * s * sin(62.0 * v.vertex.z);
o.pos.z = 20 * s * cos(163.0 * v.vertex.z);
o.pos.y = t * _Height;
o.pos.xyz += 7.8 * (v.vertex.x * sideVector + v.vertex.y * upVector).xyz;
o.pos = mul (UNITY_$$anonymous$$ATRIX_VP, o.pos);
o.color = 1.0 - t;
return o;
}
half4 frag (v2f i) : COLOR
{
return half4 (i.color, 1);
}
ENDCG
}
}
}
No problem in doing it, cg programs have access to a function called transpose that you can use.
Your answer
Follow this Question
Related Questions
UnityObjectToClipPos is inverted? 0 Answers
shader inverse(float4x4) function 2 Answers
Previous Model*View*Projection Matrix 0 Answers
Getting the forward position of an object in a vertex shader 0 Answers