OpenSourceUnityPackage / FogOfWar

For of war for unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fog of war📦

Fog of war post process effect for unity URP/HDRP

Build states semantic-release: angular License

Version LastActivity

What is it ?

This package allows you to easily integrate the fog of war with your terrain component.
Capture d’écran 2022-06-23 225326

How to use ?

Don't forget to import a URP or HDRP sample depending on your needs.
These will contain resources for integrating Fog of War into your rendering pipeline.
If you need a demo, you can import the demo through the package manager.
Capture d’écran 2022-06-23 213023

URP

For URP, you must first ensure that the URP sample has been imported into your project and of course that the URP package is installed in your project.
Then create a forward renderer data and add the Post process fog of war function to it.
Capture d’écran 2022-06-23 214146

Now create a Universal Render Pipeline asset and link your forward render data to it.

Now you need to go to Project Settings/Graphic and change the scriptable render pipeline asset to your own.
You can of course integrate the fog of war function into your own URP asset.
In Project Settings/Quality, change the render asset to your new asset.
URP is ready, you now need to link your terrain with your asset like this:

    [SerializeField] TerrainFogOfWar[] m_fogTeam1;
    [SerializeField] ForwardRendererData m_rendererData;
    private PostProcessFogOfWarFeature m_fowFeature;
    
    private void OnEnable()
    {
        m_fowFeature = m_rendererData.rendererFeatures.OfType<PostProcessFogOfWarFeature>().FirstOrDefault();
     
        if (m_fowFeature == null)
            return;
         
        m_fowFeature.settings.terrainFogOfWars = m_fogTeam1;
        m_rendererData.SetDirty();
    }

HDRP

To enable the fog of war feature in the HDRP, create a volume profile. Add the Post-processing/custom/fogOfWarPostProcess override.
Now add a volume component to your scene and attach your new profile to it.
Then, you need go into Settings/HDRP Default Settings and add in Custom post process order the fog of war feature in After post process.
Capture d’écran 2022-06-26 221448

The HDRP is ready, you now need to link your terrain to your asset in this way:

    [SerializeField] TerrainFogOfWar[] FogTeam1;
    [SerializeField] Volume postProcessVolume;

    void OnEnable() 
    {
        postProcessVolume.profile.TryGet(out FogOfWarPostProcess fow);
        fow.terrainsFogOfWar.value = FogTeam1;
    }

General

For both URP and HDRP, add a fog of war script to your scene with the same gameObject as your terrain component.
The fog of war script allows you to control the resolution of the textures.
Capture d’écran 2022-06-26 221505 In your game manager, you need to get your script and define your list of entities inherited from IFogOfWarEntity.
For example in the GameManager singleton, each spawn or destroy unit calls these functions:

    public void RegisterUnit(Unit unit)
    {
        terrainFogOfWar.RegisterEntity(unit);
    }
    
    public void UnregisterUnit(Unit unit)
    {
        terrainFogOfWar.UnregisterEntity(unit);
    }

You can also get fog of war data to hide object for example. See example in sample.

Implementation

The fog of war uses a render texture to be drawn.
This renderTexture is filled with a compute shader and is used by the screen space post-processing effect.

About

For of war for unity

License:MIT License


Languages

Language:C# 84.6%Language:ShaderLab 15.4%