How to create a shader that highlights the edges of a CollisionShape2D and CollisionPolygon2D?

Godot Version



I am not very experienced when it comes to shaders and I don’t know how to acces the properties of a CollisionShape2D from a shader. I want to implement a feature to communicate to the player when he cannot leave a specific area in my game. For example, when the player enters a boss room I want to prevent the player from exiting the room and I want to communicate this restriction to the player. I think some sort of barrier shader is a good approach for this. I want to create this shader based on a CollisionShape2D but it should also work for the CollisionPolygon2D class.

How can I implement a very basic prototype of this feature that only shows a black border around the edges of a collision shape like so? In the future, I want to create a custom animation for this barrier etc so the debug color does not suffice.

1 Like

It’s a pity that there are no answers (

Any reason why you won’t simply use an additional Sprite node that you show while the CollisionShape2D is active and hide when it gets disabled?

Using a shader for this seems like overkill to me.

At the basic level, you can add a MeshInstance2D node to your actor that has the CollisionShape2D and if you stick to primitive shapes, you can match the MeshInstance2D visual mesh to the CollisionShape2D shape (or any other primitives).

In the below, the collision shape in blue wouldn’t be visible in game.

Collision setup for the Circle

Visual mesh setup for the Circle

What kind of sucks is that the even Mesh2D just has 3D meshes to draw, so as you can see with settings, you will have to set a height of 20 on the sphere mesh to make it visible from 2D, but I haven’t done 2D enough to know if that is just how I have my 2D scene setup.

If you wanted to get more funky with the Collision shape, like a convex or concave polygon setup, I think that and the array mesh option on the MeshInstance2D mesh node will accept a PackedVector2Array (through an array mesh) to describe the mesh shape, so you can match the collision polygon shape to it (or vice versa).

Then the MeshInstance2D can have any material you want on it to do further design things.