Shaders Dynamic Spotlight Shader?

Divinik

Member
So the game I'm working on has enemies that have a visible cone-shaped line of sight. I want to implement a shader that makes it so that if part of the spotlight sprite is either colliding with a solid object, or a solid object is between the pixel and the cone's parent object, that part of the sprite will not be drawn.

I'm extremely new at shaders and basically know nothing lol, so any help would be greatly appreciated.
 
R

robproctor83

Guest
Well, this is a very complex problem with different methods of approach. Essentially what your trying to do is something like ray collision, where you draw a line in some direction until it collides with some external object. The math behind is is rather simple, but mastering the process is complicated. You have a few options. You could either use an existing asset or build your own. You can follow an official blog from YoYo on how to do it here

https://www.yoyogames.com/blog/419/realtime-2d-lighting-in-gamemaker-studio-2-part-1
https://www.yoyogames.com/blog/420/realtime-2d-lighting-in-gamemaker-studio-2-part-2

Alternatively you could download an existing asset from the Market.
FG Lighting: https://marketplace.yoyogames.com/assets/8059/fg-lighting
Fast Lights: https://marketplace.yoyogames.com/assets/4772/fast-lights

Now, depending on what you want to do, you may need to reverse engineer what is going on and adapt it to fit your needs. For instance, this requires you to darken your room while then only lightening the area you want to show as the "cone" effect. If you want to draw a cone effect without darkening the room, for example. So keep that in mind.

Another idea, maybe less effective but simpler and more efficient is to use the collision_line function in conjunction with a little math. Let's say you have a cone sprite, it's a fixed size, say 50px wide by 200px long. Then, you can use collision_line from the start of the cone to the end long ways to check if anything is colliding at the center. Then, if there is a collision you can reduce the length of the cone by the difference of the colliding object position and the cones end position. Rather than just do one line though, you should probably do three, one of the left edge, one on the right edge and one in the middle. Then you need to split the sprite into 3 slivers, so something like 17wide x 200long and then you would need to draw them back together in code. Then you would do the same thing but for each sliver instead of the whole thing at once and that way you could simulate the effect of say the edge of the cone getting caught on an object.

https://docs.yoyogames.com/source/dadiospice/002_reference/movement and collisions/collisions/collision_line.html

Best of luck, again this is no small feat. There are a lot of posts online about this, so I'm sure you will find a way.
 
Top