Page 1 of 1

Shader Issue

Posted: Sat Apr 04, 2015 1:37 am
by jonbprime
I'm trying to create a shader that will warp my output by by scaling the x axis based on the y position of the pixel. It's working but the sides of the output image are curved instead of straight? Any ideas what I'm doing wrong? Here is the shader and the input and output images are attached.


vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
{
// We need to calculate the pixel that will end up "here" after the transformation
// so we need to find the pixel that when scaled by x will end up here.
// Note: Texture Y is inverted so 1,1 is upper right

// We have a linear scale in as Y changes. Scale goes from 2 to 1
float x_scale = (2.0 - (texture_coords.y));

texture_coords.x = ((texture_coords.x - .5) * x_scale) + .5;

// If the pixel is off the screen just use black
if(texture_coords.x < 0 || texture_coords.x >= 1){
return vec4(0.0,0.0,0.0,1.0);
}
else
{
return Texel(texture,texture_coords);//This is the current pixel
}
}

Re: Shader Issue

Posted: Sat Apr 04, 2015 5:13 am
by T-Bone
As far as I can tell, the problem doesn't seem to be the actual shader but rather when you use it. If you want the white square to stay a square, draw it without the shader, and only use the shader for drawing the red circle.

Re: Shader Issue

Posted: Sat Apr 04, 2015 9:43 am
by Doctory
great eggs

Re: Shader Issue

Posted: Sat Apr 04, 2015 1:04 pm
by jonbprime
So the problem is not that the white square is warped, the problem is that the edges of the white square are curved (non-linear). Based on my shader it seems like the edges of the square should be linear, not slightly curved?

Re: Shader Issue

Posted: Mon Apr 06, 2015 8:51 pm
by jonbprime
Anyone else have an idea why the edge of the white box is curved instead of straight?

Re: Shader Issue

Posted: Tue Apr 07, 2015 8:56 am
by Lugen
While the formula looks linear to me it's apparent by the result that it isn't, so I guess you simply need to figure out a different solution to get the values you want.
Perhaps something like this.

Code: Select all

uv.x = uv.x + (uv.x - 0.5 * 2) * uv.y * 0.25
Add uv.x with a re-scale of uv.x from 0 to 1 to -1 to 1 then multiply with uv.y and a target scale of the top.
Haven't tested this, so if it doesn't work I probably used a wrong number somewhere.