Accessing fog properties
All properties shown in the inspector can be set or modified at runtime using scripting.
First you need to get a reference to the main script which can be done easily using:
using VolumetricFogAndMist2; … VolumetricFog fog = volumeObject.GetComponent<VolumetricFog>;
(where volumeObject is the gameobject of the fog volume)
Then, you can change any profile property like the fog color or wind speed/direction:
fog.profile.albedo = new Color(1,0,0); fog.profile.windDirection = Vector3.right; ... fog.UpdateMaterialProperties(); // call this method to apply changes
Remember to call UpdateMaterialPropertes() method to apply any change.
You can also access the properties using the “settings” accessor which will create an internal copy of the profile the first time you use it so any change to the properties will only affect that fog instance. Example:
fog.settings.albedo = new Color(1,0,0);
Creating fog volumes at runtime
Use this code to create a new fog volume using code:
GameObject fogVolume = VolumetricFogManager.CreateFogVolume(“New Fog Volume”);
Creating fog voids at runtime
Use this code to create a new fog void using code:
GameObject fogVoid = VolumetricFogManager.CreateFogVoid(“New Fog Void”);
Fog of War methods
The following fog of war methods are available:
Property that returns or set the fog of war texture.
Property that returns an array of Color32 with contents of the fog of war texture. Can be used to modify the fog of war texture directly. Call UpdateFogOfWar method to apply modifications to the array.
void UpdateFogOfWar(bool forceUpload = false)
Updates any ongoing fog of war transition (if any) and submits any change to the fog of war contents to the GPU (forceUpload must be used if the array content obtained with the fogOfWarTextureData property if modified directly). Usually you don’t need to use this method unless you handle the contents of the fog of war directly.
General reset function which removes any previous change to fog of war transparency.
Restores fog immediately at a given position with radius or bounds.
Returns the opacity (0-1) of the fog at a given world position.
Changes the transparency of the fog in an area. This method is overloaded and can accept different paremeters and options:
A world position and a radius, or a bounds object or a world position and size.
Desired alpha and specify if this value replaces the existing transparency or if it blends with it (blendAlpha parameter).
The duration of the transition (pass 0 to set the alpha immediately).
The smoothness/harshness of the border (by default it uses the global setting in Fog Of War section which is 1f).
The restore delay and duration (by default it uses the global settings in Fog Of War section). Pass 0 to restore delay to make the change permanent.
Terrain Fit And Dynamic Terrain Changes
When using the Terrain Fit feature, Volumetric Fog will automatically capture the terrain heightmap when some terrain fit related option or fog volume position changes.
If your terrain is procedurally generated or you make changes to its heightmap at runtime, you may use the following calls to manually perform a terrain fit update of the VolumetricFog component:
fog.UpdateMaterialProperties(forceTerrainCaptureUpdate = true);