I have quite some struggle with a post processing shader that I'm working on.
I use Unity, and I have a shader in which I want to, in screen space as part of post processing, render a sphere that looks like it was drawn in world space.
Now, I've tried some different approaches.
For all attempts, I've given the shader a world position of the sphere,a screen position of the sphere and a camera position named:
uniform float4 _SpherePositionCamera;
uniform float4 _SpherePositionCamera;
uniform float4 _SpherePositionWorld;
That is, the same point in respectively world-space camera-space and screen-space.
I've tried to project the fragment in screen space into camera space using the inverse projection matrix, and tested in the xy coords minus the screen space center of the sphere are of length < 1. This gives a circle on the screen that does not really follow a world space shere very well.
the vertex program of the gc code is
v2f vert (appdata_img v){
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
o.camPos = mul (inverseProjection, float4((o.uv * 2) - 1, _SpherePositionCamera.zw));
return o;
}
And the fragment program is
fixed4 frag (v2f i) : COLOR
{
// Get the original screen color
fixed4 screenColor = tex2D(_MainTex, i.uv);
if ( length(_SpherePositionCamera.xy - i.camPos.xy) < 1 )
{
screenColor = ( screenColor + fixed4(1,0,0,1) ) / 2;
}
return screenColor;
}
I have been working on this for quite some time, so I'm sorry if I'm somewhat failing at explaining the problem, but I hope that someone has some pointer to what I'm missing.
Thanks.
EDIT
I figured a better formulation. I think it might be done by casting a ray from each fragment and testing for intersection with the sphere in world space.
So all I need now is to construct a ray that originates from the near clipping plane and is directed in the "projection" orientation.
could this easily be done?