- Home /
_Object2World return identity matrix for Shuriken particles.
Hey people.
This problem surely have been encountered, but I didn't find any answer to it.
I'm writting a shader using a normal texture. The texture give a per-pixel normal value, to have a bumping effect. A rather classic shader simplified for 2D use.
sampler2D _MainTex;
sampler2D _Normal;
struct appdata
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord.xy;
return o;
}
half4 frag(v2f i) : COLOR
{
// Retrieve the color
float4 normalColor = tex2D(_Normal, i.uv);
// Apply the object's alpha
normalColor.a *= tex2D(_MainTex, i.uv).a;
return normalColor;
}
Now, if I try to rotate an object using this shader, the normals will follow the rotation of the object. The light and bump effect look awkward. So to keep the bump independant from the object's transform, I added a matrix multiplication in the fragment code.
half4 frag(v2f i) : COLOR
{
// Retrieve the color
float4 normalColor = tex2D(_Normal, i.uv);
// Convert to normal
float3 N = normalColor.rgb * 2.0 - 1.0;
// Transform the normal
N = mul(_World2Object, float4(N, 0));
// Convert back the result as a color
normalColor.rgb = N * 0.5 + 0.5;
// Apply the object's alpha
normalColor.a *= tex2D(_MainTex, i.uv).a;
return normalColor;
}
It works all greatly.
EXCEPT for the shuriken particles. It is like the _Object2World matrix return the identity for the shuriken's particles.
I suspect the shuriken renderer to transform itself the particles and inject the transformed vertices as a huge single mesh to have better performances, but in this condition, how can I retrieve the Model matrix for each particles?
In such case, it there a way to compute the matrix through the UV's or something else?
I could write my own particle système using game objects as I just want to do simple cloud effects with them, but well, it would be sad to not use something already existing and well implemented. :(