- Home /
UnityObjectToClipPos is inverted?
I upgraded untiy and an old shader and one of the core cg functions used by unity got its values inverted. To illustrate I assigned one of the values to the red channel on a test shader:
The code is the same on both versions, the offending line/function is:
o.vertex = UnityObjectToClipPos(v.vertex);
This is apparently expected behaviour in newer versions, but it breaks the shader. A solution would be to somehow invert the return values of UnityObjectToClipPos, but I have no idea on how to do it, I haven't found a similar function for it and the documentation seems sparse, any help deeply appreciated!
UPDATE: I managed to solve the original issue by just finding a newer version of the shader I was using. My situation was unlikely but here are the details if anybody has the same issue in the future.
The shader is the FXWater4Simple from the standard assets, I was using the 5.4 and didn't think they would actually update it. Now I have the 2018.1.9f2 version of the shader (last time they updated the standard assets apparently) and everything seems fine.
In regards to the original question, there was another line in the code which was important:
o.pos = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.pos);
which now is:
o.pos = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeNonStereoScreenPos(o.pos);
That's the only change I've noticed in the code. The problem is still a mystery to me because now I can't seem to replicate the issue by reverting the code, which spells doom and makes me believe maybe the problem is something else and it will eventually come back.
UnityObjectToClipPos transforms mesh vertices from object space to clip space such that they can be displayed on screen. To invert the line you've provided would flip the entire mesh. The reason it may be inverted is because some platforms (and some rendering systems) have different definitions for clip space; on some the z-axis goes from [-1,1] and on others it goes from [0,1]. Likewise, the y-axis is also flipped in some cases. Could you post where and how you actually use clip space coordinates? If you really need it to be consistent, you can do the raw multiplication yourself;
o.vertex = mul (UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, float4 (o.vertex.xyz, 1.0));
Thanks for the explanation, the original shader I was using is the FXWater4Simple from the standard assets, I just managed to find a solution to the problem, though my original question is still a mystery to me, I've updated the original post with details if you're interested. Otherwise here's the test shader I was using on the screenshots:
Shader "Test/$$anonymous$$VPTest"
{
Properties
{
_$$anonymous$$ainTex ("Texture", 2D) = "white" {}
}
SubShader
{
//Tags { "Queue" = "Transparent"}
//Blend SrcAlpha One$$anonymous$$inusSrcAlpha
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 screenPos : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _$$anonymous$$ainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_$$anonymous$$ainTex,i.uv) ;
col = float4(i.vertex.y,1.0f,1.0f,1.0f)/500 ;
return col;
}
ENDCG
}
}
}
I'm not sure if this issue still exists, but if this is about image effect shaders you may want to have a look at my answer over here and read the comments below the answer. Unity has some strange flipping going on in certain edge cases.
Your second image looks actually right since clipspace, normalized device coordinates and screenspace usually goes from bottom to top. That's true for almost all rendering contexts. Usually the only exception is GUI which most of the time has it's origin at the top left and goes downwards.
Hey thank you, I just managed to sort the original issue out though, updated the post with details. The original is a water shader, which I'm not sure how similar they are to image effects in regards to working with cameras.
Your answer
Follow this Question
Related Questions
Dissolve Shader Problem 1 Answer
Flat Shading / No Vertex Interpolation 2 Answers
Unlit Transparent With Color Support 1 Answer
create a texture with shader code? ( without using a texture ) 1 Answer