Fastest way to draw an influence map?

OK people have pointed out to me that an influence map could be the ideal way to attract and repel NPC characters.

So I’m thinking with two main factions I can use one texture and it’s RGBA components as separate layers 2 per faction one for attraction and one repulsion.

E.g. a gradient circle would represent a threat, the size varying between enemies.

Only I need to update these quickly and do frequent lookups e.g. 60 fps also looking for good WebGL solution.

So what would be the optimal approach:

  • Sprites and a camera
  • Texture and blitting of sprites
  • A custom shader
  • Lights and a camera
  • Lights shadows and a camera
  • Other e.g. arrays, physics casts, distance checks, flood fills

Any thoughts and suggestions welcome?

The fastest way is going to depend on the number of enemies you think you’re going to have on screen at once and the size of those areas compared to the screen. A lower number the easiest way will be using a shader and passing an vector array to the shader every frame. A higher number then using a particle system or sprites and a camera with a render texture rendering a basic distance field and a shader to display it is going to be the better option. With the render texture method you can drop the resolution if you expect lots of them overlapping the screen. You can actually use a surprisingly low resolution render texture and still get nice smooth gradients or outlines.

Blitting isn’t a great option as it means a full texture pass for every range.
Any option using lights will be more expensive than a custom shader or the sprite / render texture option.
The more you can do on the GPU the better, so any mostly CPU based method is going to be slow as you have to get the you make texture over to the GPU. Better to create it there to begin with.

If you want to try the shader method, here’s an implementation to start with.

3 Likes

Great link thank you.

I’ve never done anything from GPU to CPU so I wonder, how would the AIagent read those values if they’re in shader?

answer: extract Color[ ] with GetPixels(output texture)

Not sure you are on the right track, you might loose all you gained by doing this.

Depending on your platform this might be interesting

Thanks for the pointer. They say that GetPixels is performant, it’s the whole cpu/gpu interaction that causes stalls and at 0.6ms I’m ok because I can combine the textures in one texturearray, or maybe I’m misunderstanding what they’re saying.