jf_knight
Member
If you're familiar with photo editing smartphone apps, you may have seen the "Tilt-shift" edit option. I'd like to recreate that effect.
I'm primarily having problems with how the shader handles the mouse input; the mouse x and y move a non-blurred circle with the surrounding area is blurred.
I feel the problem is in the step event, or the "vec2 mouse" line on the shader code.
I'd appreciate any help or guidance.
obj_image: CREATE EVENT
obj_image: STEP EVENT
obj_image: DRAW EVENT
Here is the shader code:
vertex:
Fragment:
I'm primarily having problems with how the shader handles the mouse input; the mouse x and y move a non-blurred circle with the surrounding area is blurred.
I feel the problem is in the step event, or the "vec2 mouse" line on the shader code.
I'd appreciate any help or guidance.
obj_image: CREATE EVENT
Code:
mousex_coor_UNI = shader_get_uniform(shd_edit_tilt_shift, "MouseX");
mousey_coor_UNI = shader_get_uniform(shd_edit_tilt_shift, "MouseY");
Code:
mousex_coor_POSITION = 1 - abs(((mouse_x / window_get_width()) - .5) * 2);
mousey_coor_POSITION = 1 - abs(((mouse_y / window_get_height()) - .5) * 2);
obj_image: DRAW EVENT
Code:
shader_set(shd_dof);
shader_set_uniform_f(mousex_coor_UNI, (mousex_coor_POSITION))
shader_set_uniform_f(mousey_coor_UNI, (mousey_coor_POSITION));
draw_self();
shader_reset();
Here is the shader code:
vertex:
Code:
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
varying vec2 v_texcoord;
varying vec4 v_vColour;
void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
v_texcoord = in_TextureCoord;
v_vColour = in_Colour;
}
Code:
uniform vec2 resolution;
const float PI = 3.1415926535897932384626433832795;
const float DoublePI = 2.0 * PI;
const float CircleSize = 1.0;
const float SmoothPadding = 0.33;
const float Distance = 0.04;
const float DistanceSteps = 8.0;
const float RadialSteps = 8.0;
const float RadialOffset = 0.5;
const float KernelPower = 1.0;
uniform float MouseX;
uniform float MouseY;
vec4 tiltshift (vec4 colors)
{
vec2 uv = v_texcoord;
float StepSize = Distance / DistanceSteps;
vec3 BlurColor = vec3(0);
vec2 SubUV = uv;
float CurDistance = 0.0;
vec2 CurOffset = vec2(0);
float SubOffset = 0.0;
float KernelDistance = 0.0;
float AccumulatedDistance = 0.0;
for(int i = 0; i < int(DistanceSteps); i++)
{
CurDistance += StepSize;
for (int j = 0; j < int(RadialSteps); j++)
{
SubOffset += 1.0;
CurOffset.x = cos(DoublePI * (SubOffset / RadialSteps));
CurOffset.y = sin(DoublePI * (SubOffset / RadialSteps));
SubUV.x = uv.x + CurOffset.x * CurDistance;
SubUV.y = uv.y + CurOffset.y * CurDistance;
KernelDistance = pow(CurDistance, KernelPower);
BlurColor += texture2D(gm_BaseTexture, SubUV).rgb * KernelDistance;
AccumulatedDistance += KernelDistance;
}
SubOffset += RadialOffset;
}
BlurColor /= AccumulatedDistance;
vec2 mouse = vec2(mousex, mousey);
//float AspectRatio = resolution.x / resolution.y;
float AspectRatio = 1.0;
vec2 VisionCenter = mouse.xy;
vec2 v = uv - VisionCenter;
v.x = v.x * AspectRatio;
float CirleRadius = CircleSize / 2.0;
float SmoothRadius = CirleRadius * SmoothPadding;
float CircleMask = smoothstep(CirleRadius, CirleRadius - SmoothRadius, length(v));
BlurColor = mix(BlurColor.rgb, colors.rgb, CircleMask);
colors = vec4(BlurColor, 1.0);
return colors;
}
void main( )
{
vec2 uv = v_texcoord;
vec4 colors = texture2D(gm_BaseTexture, uv);
gl_FragColor = tiltshift(colors);
}