4

I am currently working on an algorithm to calculate visibility in a 2D top-down level.

The level contains

  • actors, with position and "sight" information
  • walls, with start & end positions
  • light sources, with position and range

I have written a fragment shader that renders my scene based on a texture I compose of the informations above.

basic line of sight calculation

Left side: my level rendered, right side: the actual texture that is fed to the shader

  • Red channel = line of sight for actors (in the example just the selected token is an actor)
  • Green channel = light sources with their range and their respective line of sight
  • Blue channel (not present in example) = night-vision of actors (means areas are shaded despite no light present)

I am using Babylonjs and am naively rendering all those calculations one after the other into a DynamicTexture using the canvas 2d context.

Can I use the GPU for some of these calculations (or all?) I am no expert for shader programming and don't know where to start / look.

The code for my line-of-sight calculation can be found here: https://gitlab.com/aiacta/visibility

Jonathan
  • 141
  • 1

1 Answers1

1

In this SIGGRAPH paper is described a similar operation for the visibility calculation from every pixel in the domain (you only need calculate it from actors position), which has been implemented in GPU.

Although it is oriented to fluid dynamics, it may be a starting point to look.

You can see other related shader examples like this one with implicit geometry.

David
  • 301
  • 2
  • 4
  • 1
    Thanks, I will take a look. In the meantime I have made use of https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas with decent results, offloading the heavy work into a WebWorker Thread. – Jonathan Apr 02 '19 at 20:27