Mirror Shader

I’m taking another dive into shader land and am already stuck. I’m trying to create a shader that mirrors a sprite along a certain arbitrary line. It works fine when the sprite is far from the line, but once it gets close (by close I mean the vertex program starts outputting overlapping gl_Position) it breaks.

I think it breaks because the fragment program renders the alpha == 0 parts of the sprite instead of the ones with alpha == 1, but I’m not sure how to fix it :smiley:

Could you please post the shader here?

1 Like

sprite.zip (1.1 KB)

This is a simplified case that should be reflecting along the x=640 line (which it kind of does, except when overlap happens).

Edit: a typo on the .material

I am not sure what you want to achieve, but the formula

-abs(position.x - mirror.x) + mirror.x,

is very weird… A reflection, as in a mirror, is a linear map. the abs should not be there. I think you want:

2.0 * mirror.x - position.x

If this is want you want, then we will see the general case. Not much complicated, but you need the formula for a reflection with respect to a line in a general position. :slight_smile:

Ciao!

The positional aspect is exactly what I want, sorry I didn’t explain it well - it’s an object moving along a line but from some point it becomes a reflection.

Maybe shaders aren’t even the best way to go about it, but essentially I want something like a ball bouncing off a wall, but with a more complex and animated sprite, so that it’s a fluid reflecting animation.

1 Like

Now, I understand better. So the shader works when the sprite is far from the reflecting line and does not work when it intersects the line, right?

I believe this is unavoidable, recall that a sprite is a collection of two triangles. When you get close to the line, certain vertexes get reflected while other are not reflected. This results in the sprite being distorted.

I am not sure this is the problem, but you should take into consideration this aspect.

I hope this hep!

1 Like

Thanks! I think the issue is with the sprite not blending with itself (if that makes sense), so the parts of the sprite where alpha is 0 overwrite the parts where alpha is 1 causing the distortion.

Yes, this is what I was trying to explain (but I know nothing about the alpha, so, maybe).

2 Likes