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
}
}
}
Please don’t bump your thread so quickly after your initial post. Not even 24 hours had passed.
Sounds like you’re looking for a billboard, or something similar. Billboards are planes that always face the camera. Sometimes they are constrained on one axis, for things like grass. Have you looked at Billboard shader code?
I know what a billboard is, and no, that is not what I was looking for. I want my 3D model to be “squashed” to 2D, like if you set its scale.z to 0, but always with respect to the camera. If my explanation is not clear, just say it and I can draw something to clarify it
Ahh, I think I have a better understanding of what you’re looking for. But I don’t understand yet what isn’t working as you intended. Your current shader makes the model look as if it has isometric projection, which seems to fit your description.
If you only want to change its depth AFTER normal perspective projection, which changes when it fails the ZTest, keep in mind that for OpenGL, OpenGL ES and PS3, the depth is specified in the -1 to 1 range, while it is 0 to 1 on D3D.
If you want to apply lighting, you can also flatten that by forcing the normals to point along the view direction (towards or away from the camera).