I want to draw a temporary light where i touch the screen. Some simple circle.
It may seem trivial for first, however. The idea is to find distance between touch position and fragment position.
To simplify things for now lets assume we use landscape screen (sw > sh).
So basically i draw a 2d fullscreen quad and check every fragment if its in the light radius then compute light intensity based on distance and light radius value: r=1/16 of the screen height so basically r is 1/16 (we dont use any pixel lengths here)
now when its valid for height, for width its not cause theres a difference which we can compute by float aspect = sh/sw; => rx = r * aspect;
ok we have two radiuses for both x and y dimensions.
so basically i should be able to compute two intensities both for x and y by:
float dstY = abs(fragpos.y - lightpos[i].y);
float dstX = abs(fragpos.x - lightpos[i].x) * aspect;
But now the thing is that i somehow don't believe adding these two and dividing by 2 will give me the desired intensity.
float intensityY = clamp(1.0 - dstY / LRadius, 0.0, 1.0);
float intensityX = clamp(1.0 - dstX / LRadius, 0.0, 1.0);
float final_intensity = (intensityX + intensityY) / 2.0; //doesnt seem right like something tells me i need to use some kind of quadric gradient lol thats why i ask
Maybe someone could help me solving that.
cheers
and a code
precision highp float;
varying highp vec2 vert;
#include "$shaders/shader_math.h" yeah nice huh?
uniform vec3 click_bait[10];
uniform int cb_len;
uniform float sw;
uniform float sh;
vec4 light_color;
//remember to perserve aspect ratio
//let height be ('main') relative ratio
uniform float LRadius;
void main()
{
if (cb_len > 0)
{
float total_intensity = 0.0;
float aspect = sh/sw;
if (sh > sw) aspect = sw/sh;
float lradiusX = lradius * aspect;
int cnt = 0;
vec3 frag_pos = vec3(vert.x, vert.y, 0.0);
for (int i=0; i < cb_len; i++)
{
vec3 dst = vec3( abs( frag_pos.x - click_bait[i].x), abs( frag_pos.y - click_bait[i].y), 0.0 );
dst = vec3(clamp(1.0 - dst.x / LRadius, 0.0, 1.0), clamp(1.0 - dst.y / LRadius, 0.0, 1.0), 0.0);
float final_intensity = (dst.x+dst.y) / 2.0;
if (final_intensity > 0.0)
{
cnt = cnt + 1;
total_intensity = total_intensity + final_intensity;
}
}
total_intensity = total_intensity / float(cnt);
gl_FragColor = vec4(light_color.xyz*total_intensity, light_color.w);
}
else
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
}