Scripting Support (C#)
advanced scriptingVolumetric Fog & Mist (Built-in) · Scripting Support (C#)
The main scripting class is VolumetricFog (namespace VolumetricFogAndMist). This MonoBehavior attaches to the camera and provides a singleton via VolumetricFog.instance. All fog settings are properties directly on this class.
Access the fog instance with VolumetricFog.instance. Unlike the URP version, fog settings (density, color, etc.) are get/set properties directly on the component - no separate profile object is needed for runtime changes.
VolumetricFog
Getting Started
using VolumetricFogAndMist;
// Access the singleton instance
VolumetricFog fog = VolumetricFog.instance;
// Or get from a specific camera
VolumetricFog fog = myCamera.GetComponent<VolumetricFog>();
// Modify properties directly
fog.density = 0.8f;
fog.color = new Color(0.7f, 0.7f, 0.8f);
fog.windDirection = new Vector3(0.05f, 0, 0);
Singleton & Instances
static VolumetricFog instance read-onlySingleton access. Searches Camera.main first, then all cameras. Returns the first VolumetricFog component found.
Fog of War Methods
static VolumetricFog instance read-onlySingleton access. Searches Camera.main first, then all cameras. Returns the first VolumetricFog component found.
Fog of War Methods
static VolumetricFog instance read-onlySingleton access. Searches Camera.main first, then all cameras. Returns the first VolumetricFog component found.
Fog of War Methods
static VolumetricFog instance read-onlySingleton access. Searches Camera.main first, then all cameras. Returns the first VolumetricFog component found.
Fog of War Methods
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, FoWUpdateMethod updateMethod = BackgroundThread, bool blendAlpha = true)Changes the fog of war alpha at a world position within a circular area. Uses transition duration and smoothness from fog settings.
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, float duration, FoWUpdateMethod updateMethod = BackgroundThread, bool blendAlpha = true)void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, float duration, float smoothness, FoWUpdateMethod updateMethod = BackgroundThread, bool blendAlpha = true)void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, bool blendAlpha, float duration, float smoothness, float restoreDelay, float restoreDuration, float restoreToAlphaValue = 1f, FoWUpdateMethod updateMethod = BackgroundThread)void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, float duration = 0)Changes fog of war opacity within a rectangular bounds area.
void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, float duration, float smoothness)void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, bool blendAlpha, float duration, float smoothness, float restoreDelay, float restoreDuration, float restoreToAlpha = 1f, FoWUpdateMethod updateMethod = BackgroundThread)void SetFogOfWarAlpha(Collider collider, float fogNewAlpha, bool blendAlpha = false, float duration = 0, float smoothness = 0, float restoreDelay = 0, float restoreDuration = 2, float restoreToAlpha = 1f)Changes fog of war opacity within a convex collider shape.
void SetFogOfWarAlpha(GameObject go, float fogNewAlpha, float duration = 0, float restoreDelay = 0, float restoreDuration = 2, float restoreToAlpha = 1f, FoWUpdateMethod updateMethod = BackgroundThread)Changes fog of war opacity within a mesh shape defined by a GameObject.
void ResetFogOfWar(float alpha = 1f)Resets the entire fog of war texture to the given opacity.
void ResetFogOfWarAlpha(Vector3 worldPosition, float radius, float alpha = 1f)Restores fog of war to the given opacity at a specific location.
void ResetFogOfWarAlpha(Bounds bounds, float alpha = 1f)void ResetFogOfWarAlpha(Vector3 position, Vector3 size, float alpha = 1f)void ResetFogOfWarAlpha(Vector3 position, float extentsX, float extentsZ, float alpha = 1f)float GetFogOfWarAlpha(Vector3 worldPosition)Returns the current fog of war alpha at a world position (0 = cleared, 1 = full fog).
void UpdateFogOfWar(bool forceUpload = false)Reloads the current contents of the fog of war texture into the color buffer.
Methods
void UpdateMaterialProperties(bool forceNow = false, bool forceTerrainCaptureUpdate = false)Updates the fog shader material properties. Set forceNow to apply immediately.
void UpdateMaterialPropertiesNow(bool forceTerrainCaptureUpdate = false)Immediately updates the fog shader material properties.
void CheckWaterLevel(bool baseZero)Adjusts the fog baseline height based on the camera position and water level. If baseZero is true, resets the baseline to zero.
void TrackPointLights(bool forceImmediateUpdate = false)Finds and assigns the nearest point lights to the fog. Requires pointLightTrackAuto = true.
Light GetPointLight(int index)Returns the Light component at the given point light index.
Profile Transitions
void SetTargetProfile(VolumetricFogProfile targetProfile, float duration)Transitions to a target fog profile over the specified duration. Requires useFogVolumes = true.
void ClearTargetProfile(float duration)Reverts to the initial profile over the specified duration.
void SetTargetAlpha(float newFogAlpha, float newSkyHazeAlpha, float duration)Transitions fog and sky haze alpha to target values.
void ClearTargetAlpha(float duration)Reverts alpha to original values.
void SetTargetColor(Color newColor, float duration)Transitions fog color to target.
void ClearTargetColor(float duration)Reverts fog color.
void SetTargetLightColor(Color newLightColor, float duration)Transitions light color to target. Disables sunCopyColor.
void ClearTargetLightColor(float duration)Reverts light color.
Static Methods
static VolumetricFog CreateFogArea(Vector3 position, float radius, float height = 16, float fallOff = 1f)Creates a fog area at the given position. Returns the new VolumetricFog instance.
static VolumetricFog CreateFogArea(Vector3 position, Vector3 boxSize)void Load(VolumetricFog fog)Applies this profile's settings to the specified fog instance. This is an instance method on VolumetricFogProfile.
void Save(VolumetricFog fog)Captures the current fog settings from the specified fog instance into this profile. This is an instance method on VolumetricFogProfile.
static void Lerp(VolumetricFogProfile profile1, VolumetricFogProfile profile2, float t, VolumetricFog fog)Interpolates between two profiles at factor t (0-1) and applies the result to the fog instance. Useful for smooth day/night or weather transitions.
Code Examples
Basic Fog Modification
using UnityEngine;
using VolumetricFogAndMist;
public class FogController : MonoBehaviour {
void Start() {
VolumetricFog fog = VolumetricFog.instance;
fog.density = 0.8f;
fog.color = new Color(0.7f, 0.7f, 0.8f);
fog.height = 50f;
fog.windDirection = new Vector3(1, 0, 0);
fog.speed = 0.3f;
}
}
Preset Switching
VolumetricFog fog = VolumetricFog.instance;
// Switch to a preset
fog.preset = FOG_PRESET.SeaClouds;
// Or use a custom profile
fog.profile = myFogProfile;
Profile Transitions
VolumetricFog fog = VolumetricFog.instance;
fog.useFogVolumes = true;
// Smoothly transition to a new profile over 3 seconds
fog.SetTargetProfile(nightProfile, 3f);
// Revert to original over 2 seconds
fog.ClearTargetProfile(2f);
Profile Interpolation
public VolumetricFogProfile daytimeProfile;
public VolumetricFogProfile nightProfile;
void UpdateFogForTimeOfDay(float t) {
VolumetricFogProfile.Lerp(daytimeProfile, nightProfile, t,
VolumetricFog.instance);
}
Fog of War
VolumetricFog fog = VolumetricFog.instance;
// Clear fog at player position
fog.SetFogOfWarAlpha(player.position, radius: 15f, fogNewAlpha: 0f);
// Clear fog within bounds
fog.SetFogOfWarAlpha(new Bounds(center, size), fogNewAlpha: 0f);
// Clear with auto-restore after 5 seconds
fog.SetFogOfWarAlpha(player.position, 10f, fogNewAlpha: 0f,
blendAlpha: true, duration: 0.5f, smoothness: 1f,
restoreDelay: 5f, restoreDuration: 3f);
// Query fog opacity
float alpha = fog.GetFogOfWarAlpha(targetPosition);
// Reset all fog of war
fog.ResetFogOfWar();
Creating Fog Areas at Runtime
// Create a spherical fog area
VolumetricFog fogArea = VolumetricFog.CreateFogArea(
position: transform.position,
radius: 50f,
height: 20f,
fallOff: 2f);
// Create a box fog area
VolumetricFog boxFog = VolumetricFog.CreateFogArea(
position: transform.position,
boxSize: new Vector3(100, 30, 100));
// Remove all fog areas
VolumetricFog.RemoveAllFogAreas();
Saving & Loading Fog of War
VolumetricFog fog = VolumetricFog.instance;
// Save state
Color32[] savedState = fog.fogOfWarTextureData;
// Load state
fog.fogOfWarTextureData = savedState;Suggest an improvement
Help us improve this documentation page.