- Home /
Flatten mesh shader
Hi, I want to flat an object in its z with respect to the camera. I'll explain it a bit more: What I want is that a 3D mesh appears as a "plane" in the x-y plane (z axis) in whatever rotation it is. Think of it as if you smash a 3D object against the screen to get a squashed plane out of it. What I tried is setting its Z to something constant in modelview coords, and then convert it to projection, but it isn't working... What am I doing wrong?
Shader "Test/FlattenMesh" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float3 color : COLOR0;
};
v2f vert (appdata_base v)
{
float4 origin = mul (UNITY_MATRIX_MV, float4(0,0,0,1));
v2f o;
o.pos = mul (UNITY_MATRIX_MV, v.vertex);
o.pos.z = origin.z;
o.pos = mul (UNITY_MATRIX_P, o.pos);
return o;
}
half4 frag (v2f i) : COLOR
{
return half4 (i.color, 1);
}
ENDCG
}
}
}
Answer by DMGregory · May 27, 2014 at 05:39 PM
You're applying the flattening in view space, so this will always flatten the object in the camera's z axis (ie. it will become a flat billboard facing the camera). Is that what you want?
If you'd prefer to flatten in world space, replace UNITY_MATRIX_MV with _Object2World and UNITY_MATRIX_P with UNITY_MATRIX_VP
If you want to flatten in the object's local z axis, do:
o.pos = mul(UNITY_MATRIX_MVP, v.vertex * float4(1, 1, 0, 1));
to zero out the local z offset of each vertex.
Nice! It worked flatten it in world space, that is exactly the behaviour I was expecting! Thank you so much!
I'm trying to do the exact same thing but in a surface shader and am having little success.
D$$anonymous$$G, could you please go through the "view space" solution you touched upon?
Your answer
Follow this Question
Related Questions
Vertex lit vs diffuse shading with no shadows? 0 Answers
Foliage shader advices ? 0 Answers
Grab texture from Vertex Shader 0 Answers
How to force the compilation of a shader in Unity? 5 Answers
Shining shader 1 Answer