How to create a glow effect around a Sprite/Image GameObject?

Hey everyone!

I am currently trying to create an outline for some sprites in my game.

After searching Google, I decided to implement it using two sprites, one above the other and the “bottom” one scaled up a bit and tintFilled with white.
It works perfectly for square/rectangular shaped but is very ugly when I use a “normal” sprite with transparency.

See the difference:

I noticed that a devlog (https://phaser.io/phaser3/devlog/109) mentions a glow shader which is what I’d like to use but it doesn’t seem to have been released as of today.

Do you guys know how I could solve my issue?

Thanks in advance!
Telokis

2 Likes

How many different sprites do you have? A simple way to do this would be to use two different png files as animation frames. One with the white outline and one without. Then, just switch between them as needed.

Isn’t that more or less what I’ve attempted above?
Regarding the number of sprites, I expected to be able to do it for more or less any interactive sprite so it could be a lot.

Here is the relevant code:

Then, to use it
image
(Same for disabling it)

Yes, very similar. But I meant to use two different files. When you create your graphics (in photoshop or whatever), create one with an outline and one without.

But, looking at your code, you can also try setOrigin(0.5) so the images scale from the center. You will need to position based on their center too.

I think that would be too much sprites to handle. Thanks for the suggestion, though.

The setOrigin(0.5) changes nothing at all, sadly.

Am I allowed to bump this thread?
I’m not asking for code or anything, I’d just like to have a slight idea regarding the process I could use.
I don’t expect someone to solve this issue for me.

The other option is to write your own shader. There’s an intro to shaders in Phaser 3 here. And, I’m sure there are examples of glow shaders on the web that you can adapt to your needs.

1 Like

Try to simplify your code.
Just make 2 sprites that’s overlapse each-other and scale one with outline effect.

Yes, I read this tutorial but it seems to describe camera-based shaders. I have no experience working with them at all but I’m not sure this would allow me to make specific sprites glow. Do you think it would?

Yes, that’s the code I’ve shown above.

As an alternative, I would also be interested in knowing how to “brighten” my sprite dynamically on hover. It may be simpler.

Create a copy of your sprite, use setTintFill like you did earlier, and then just set the alpha of the copy to something like 0.5.

Thanks for the suggestion!
That would work but I don’t like having to move two sprites at once.
I could use a container (and I tried) but it isn’t interactive so I would need to make my sprite interactive but use the container for the x,y coordinates, not very practical.

Why not create a new class called HighlightSprite or something, and have it extend the Sprite class. Inside that class, have it create a second sprite as described above to be your highlight. That way, it still acts as a Sprite, just with an additional sprite on it.

That’s a good workaround. Do you know if I can do it so that it’s easily integrated with Phaser? Do I still need to recreate and register all factories or is there an easier way?

Ok, I’m very sad because I just noticed that alpha is ignored when using setTintFill… The sprite is always 100% opaque and completely hides my sprite…

Are you sure about that? I myself haven’t experimented with tint fills too much, but I modified one of the examples (this one) and alpha appeared to function normally with tint fills, even when using different values for each vertex.

Seems like the issue has been fixed. I’m still in 3.11 for the moment, though.
Anyway, I’m not very happy with the idea of doubling the amount of sprites/images just to achieve this effect. I switched to a setTint(0x999999) which is far from ideal but will do the job until a white tint is possible. I opened an issue there to try and get a feature added.

Thanks for all answers, it’s been helpful!