- Home /
Question by
Lieene-Guo · Dec 02, 2014 at 10:42 AM ·
shaderlabtessellation
How to get a Tangent space to object space Matrix in shader
I'm currently working on a tessellation based shader. And need to calculate vertex extrusion in tessellation vertex modifier by sampling normal map. And it is east to get object to tangent rotation matrix via TANGENT_SPACE_ROTATION unity macro but, inverse is not supported in shader lab. Does anyone have any idea on how to get this Tangent Object matrix? Or maybe I should not sample normal, and use only vertex normal?
struct appdata_sr
{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
};
float3x3 _inverse(float3x3 m)
{
float3x3 minv;
float det = determinant(m);
minv[0, 0] = (m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2]);
minv[0, 1] = (m[0, 2] * m[2, 1] - m[0, 1] * m[2, 2]);
minv[0, 2] = (m[0, 1] * m[1, 2] - m[0, 2] * m[1, 1]);
minv[1, 0] = (m[1, 2] * m[2, 0] - m[1, 0] * m[2, 2]);
minv[1, 1] = (m[0, 0] * m[2, 2] - m[0, 2] * m[2, 0]);
minv[1, 2] = (m[1, 0] * m[0, 2] - m[0, 0] * m[1, 2]);
minv[2, 0] = (m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1]);
minv[2, 1] = (m[2, 0] * m[0, 1] - m[0, 0] * m[2, 1]);
minv[2, 2] = (m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1]);
return minv / det;
}
void SrVert(inout appdata_sr v)//, out Input o)
{
float d = tex2Dlod(_ParallaxMap, float4(v.texcoord.xy,0,0)).r * _Parallax;
TANGENT_SPACE_ROTATION;
rotation=_inverse(rotation);
float3 n = mul(rotation,tex2Dlod(_BumpMap, float4(v.texcoord.xy,0,0)));
n = normalize(n);
v.vertex.xyz += n * d;
}
I find the inverse matrix code from stackflow but it doesn't look right. could any one help?
Comment