How To Get A Pixel Color From A Model

Hello!

I have been trying to prototype a light-based stealth mechanic for a project I am working on.

The idea is that the player in the dark would not be visible, while in the light would be visible. I would like to read the light value from the model / mesh to be as accurate as possible. Preferably sample a few points to determine how visible the player is. I need this value so enemies can detect if the player is in the light long enough to be spotted.

I know there are other ways of doing this:

  • Using Collisions and detecting if the player is in a visible area
  • Creating a table and storing light positions / ranges then checking player brightness with proximity

Before I explore other options, I would prefer to use the actual alpha color value stored in the model / mesh since that seems like the most straight forward.

Is this something that is possible?
Is there a better or easier way of doing this?

The lighting I am using is the simple 2d light by britzl

This is what I am thinking of.

In Darkness:

Semi illuminated:

Fully lit:

Any ideas or code I could use would be extremely helpful.

Thank you!

I implemented a rough version of this using a table of all light positions + sizes then determining if the player is overlapping with any of the light sources. I am not happy with this solution and am still looking for a way to read a pixel color directly.

I was looking at this forum post: How to load a texture from a matrix of colors - Questions - Defold game engine forum

Is there a way I can turn a render target such as “self.light_rt” into a texture then read the color value?