Author Topic: use TGS for 'fog of war'?  (Read 451 times)

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
use TGS for 'fog of war'?
« on: August 06, 2019, 04:29:35 PM »
Is there a way to apply a 'fog' texture to the entire TGS grid, which is placed elevated above the terrain and then selectively clear the cells to expose the terrain under the grid as the player moves.  This would be used for a minimap/worldmap, not for the player camera.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #1 on: August 07, 2019, 12:50:15 AM »
Hi,
Yes, you could enable "Transparent Background" and color cells to make those areas opaque, then clear them or use a transparent color to reveal what's behind.
Another option would be to use Dynamic or Volumetric Fog & Mist assets, as they include fog of war functionality already.
Regards

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #2 on: August 08, 2019, 06:02:33 AM »
Thanks for the response.  I'm already using the TGS hex grid for management of data and game objects, but not visually, so it seemed like a good candidate to implement fog of war in a top down world map view.  I am looking at your fog asset for teh main player view.  Is there are info on the amount of processor load it adds?

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #3 on: August 08, 2019, 06:03:43 AM »
Is there a way to apply a texture to all cells,  or does each have to be set individually?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #4 on: August 08, 2019, 06:29:04 AM »
You need to texture all cells in order to remove some cells and see-through.
To use the same texture covering the entire grid, use the Canvas texture property and then texture all cells passing the same texture. TGS will compute the UV for the cell region.
There’s a demo scene which let you texture some cells as part of a bigger texture.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #5 on: August 08, 2019, 06:30:19 AM »
Thanks for the response.  I'm already using the TGS hex grid for management of data and game objects, but not visually, so it seemed like a good candidate to implement fog of war in a top down world map view.  I am looking at your fog asset for teh main player view.  Is there are info on the amount of processor load it adds?

If you need the fastest solution for fog of war, use the Fog of War plane prefab included in Dynamic Fog & Mist. This asset includes a demo scene with it.

EDIT: however, the best looking effect can be achieved using Volumetric Fog & Mist. This fog of war effect is used in MarZ Tactical Base Defense: https://assetstore.unity.com/g/marz-tactical-base-defense
« Last Edit: August 08, 2019, 07:02:49 AM by Kronnect »

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #6 on: August 09, 2019, 07:15:50 AM »
Is there any way to provide a new function to set a texture property which is applied to all cells at startup or a single call to set a texture to all cells?  I am running a 100x100 grid and this would be 10,000 calls to set cell textures over the whole grid.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #7 on: August 09, 2019, 07:45:27 AM »
If you're using TGS as a 2D grid, you can assign a texture to the material of TGS itself (the albedo texture of CanvasBackground material).
However, that will make the background visible and opaque so it'll defeat the purpose of using TGS as a fog of war solution.

Since the idea is to clear individual cells at will, you need to color/texture all cells at start. It can take some time but once it does, any further change is very fast as TGS caches the generated cell geometry.

(as commented before, an alternate solution for fog of war feature is to use the fog assets).

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #8 on: August 13, 2019, 08:13:26 PM »
Is it possible to adapt the cell texture rendering so that if you use a solid color, we get a significant perfomance improvment? 

I have implemented a top down fog image as the canvas texture and applied it to each cell, then clear them using CellGetNeighboursInRange... but the performance is not good.  It would be a worthwhile tradeoff to me to have a solid color for the texture to get a performance improvement.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #9 on: August 14, 2019, 05:24:00 AM »
Hi,
When you call CellSetColor(cellIndex, Color) API, it already uses a shader that only writes color and does not read from any texture.
In any case, reading a single texture should not impact performance. Can you post a screenshot of the profiler and the detail of some spike?


lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #10 on: August 14, 2019, 06:54:49 AM »
I was using CellHideRegionSurface to clear the 'fog', and the frame rate was over 30ms when showing the map.  I switched to CellSetColor  to 'clear' and the frame rate dropped to ~20ms.  You can see by the profiler that the bulk of that time is rendering.  With the map off and in normal player view (the left side of the profiler plot), the grid is not being rendered and the frame rate is less than 10ms.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #11 on: August 14, 2019, 07:18:53 AM »
Are you using an opaque or transparent color? If you use an opaque color, it might improve.
In any case, if you can send me a repro I will take a look.

lledden

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #12 on: August 14, 2019, 12:50:17 PM »
I think the frame rate is acceptable for now, however, I have a related question.  This application of the grid is primarily for data storage and various calculations, and it's only viewing function is in the world map for fog of war.  I have a separate camera for that map display and I'm using layers to control what appears on the map.  I set TGS to the minimap layer but it remains visible in the normal view.   I also set the camera property to be the minimap camera.

Does TGS not support the camera layer functionality, or is there a different way to handle that?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: use TGS for 'fog of war'?
« Reply #13 on: August 16, 2019, 07:56:11 AM »
Yes, it should.
By default, TGS and its children use the UI layer but you can use any layer.
I have added a second camera to one of the demo scenes and changed the Culling Mask of the second camera so it excludes the layer used by TGS and it works as expected. TGS is not visible in the second camera.
Is it what you refer?