Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fastest way to draw an influence map?

Discussion in 'General Graphics' started by Arowx, Feb 28, 2016.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    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:
    1. Sprites and a camera
    2. Texture and blitting of sprites
    3. A custom shader
    4. Lights and a camera
    5. Lights shadows and a camera
    6. Other e.g. arrays, physics casts, distance checks, flood fills
    Any thoughts and suggestions welcome?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    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.
    http://www.alanzucconi.com/2016/01/27/arrays-shaders-heatmaps-in-unity3d/
     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Great link thank you.
     
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,982
    I've never done anything from GPU to CPU so I wonder, how would the AIagent read those values if they're in shader?
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,982
    answer: extract Color[] with GetPixels(output texture)
     
  6. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,982
    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.