I'm attempting to translate a fragment shader into a vertex shader (for mobile optimisation)
As you can see in the image below, the vertices of the center and right edge are buggy. (This is a plane with 11 x 11 vertices)
The UVs are currently mapped from the right, and wrapped around the center (with radial rotation). I'm guessing multiple vertices in the middle half the same value which is creating a hole? And then the right side is cross-fading the first UV values to the final values which creates a stretched effect.
The question is how to override or fix those. (They'll probably be 2 separate problems?)
uniform vec2 _Offset;
uniform float _Multiply;
varying vec4 position;
varying vec4 vert;
varying vec2 tex;
varying vec2 uv;
varying float ar;
#ifdef VERTEX
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec2 p = -1.0 + 2.0 * tex.xy;
float a = atan(p.y,p.x) ;
float r = sqrt(dot(p,p));
uv.x = _Multiply*a/3.1416 * 2.0 + 0.1;
float sx = sin(_Multiply*r+_Offset.x);
float sy = _Multiply*.1*cos(_Offset.y+_Multiply*a);
uv.y = -_Offset.x + sx + sy;
}
#endif
#ifdef FRAGMENT
uniform sampler2D _MainTex;
void main(void)
{
gl_FragColor = texture2D(_MainTex,uv*.5);
}
#endif
ENDGLSL
Okay, essentially rewriting the shader I was able to get expected results. And for advice to newcomers: the higher the number of vertices in your plane, the less pixel distortion will occur.
GLSLPROGRAM
#ifdef VERTEX
varying vec2 position;
uniform float _Multiply;
uniform vec2 _Offset;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec2 tex = vec2(gl_MultiTexCoord0);
vec2 p = tex.xy - 0.5;
float r = length(p);
float a = atan(p.y, p.x);
float sides = _Multiply;
float tau = 2.0 * 3.1416;
a = mod(a, tau/sides);
a = abs(a - tau/sides/2.0);
position = r * vec2(cos(a), sin(a)) + 0.5 + _Offset;
}
#endif
#ifdef FRAGMENT
varying vec2 position;
uniform sampler2D _MainTex;
void main(void)
{
gl_FragColor = texture2D(_MainTex, position);
}
#endif
ENDGLSL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With