Is there any way to alter the UV within a fragment shader?
I'm working on a shader to do image correction via a grid-based warp, but I've hit a little problem. Within my shader, I have a function that takes in my original UV and returns the warped UV...
...
float2 getWarpedUV (float uv)
{
// This calculates the warped UV. Note that this function can't be inversely mapped...
}
The idea is that I feed in the UV of the original image and it spits out the UV within the final image. So if I feed in (0,0) and it returns (0.25, 0.25), that means the colour at (0.25, 0.25) within my final image should equal the colour at (0,0) within the original image. I mistakenly thought it would be easy to do things this way by using this function within the vertex shader...
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = getWarpedUV(v.uv);
return o;
}
The problem is, the vertex shader only processes the 4 vertices that form the quad of my screen (the shader is an image effect shader). Using the function within the fragment shader yields the incorrect result, as I'm performing the mapping in the wrong direction...
fixed4 frag (v2f i) : SV_Target
{
float2 warpedUV = getWarpedUV(i.uv);
// This is wrong, as the mapping is being applied in the wrong direction.
return tex2D(_MainTex, warpedUV);
}
What I need to do is something akin to the following...
fixed4 frag (v2f i) : SV_Target
{
float2 originalUV = i.uv;
i.uv = getWarpedUV(i.uv);
return tex2D(_MainTex, originalUV);
}
Unfortunately, this doesn't work (and I can kind-of see why). Does anybody know a way that I can get around this without having to somehow create an inverted "getWarpedUV" function (which would be pretty-much impossible to create without incurring some heavy performance penalties)?
Your answer
Follow this Question
Related Questions
Composite Materials/Multiple UVs? 0 Answers
Problem with surface shader using uv2 1 Answer
Unity UV weird 0 Answers
How can i fix/properly use this code ? 0 Answers
Why is this point light square? 1 Answer