Scripting Support (C#)
advanced scriptingDynamic Fog & Mist 2 (URP) · Scripting Support (C#)
Control Dynamic Fog & Mist 2 at runtime using the DynamicFogAndMist2 namespace. The fog system uses two main classes: DynamicFog (the MonoBehavior component placed on a volume object) and DynamicFogProfile (a ScriptableObject that holds all visual settings).
using DynamicFogAndMist2;
DynamicFog fog = GetComponent<DynamicFog>();
fog.profile.density = 0.5f;
fog.UpdateMaterialProperties();
DynamicFog
MonoBehavior component attached to a fog volume GameObject. Manages rendering, sub-volumes, fade, and Fog of War.
General Settings
DynamicFogProfile profile
Reference to the DynamicFogProfile ScriptableObject that defines all visual fog settings. Changing this swaps the entire fog appearance.
bool showBoundary
Shows the volume boundary gizmo in the scene view for debugging purposes.
bool allowRotation
When enabled, the fog volume supports rotation of the GameObject. When disabled, rotation is ignored for better performance.
Fade
bool enableFade
Enables distance-based fading of the fog volume. When enabled, the fog fades out as the fade controller moves away.
float fadeDistance
Maximum distance from the fade controller at which the fog is fully visible. Beyond this distance the fog fades out.
Transform fadeController
The transform used as the reference point for distance fading. Typically the player or main camera. If null, the main camera is used.
Sub-Volumes
bool enableSubVolumes
Enables sub-volume support. When the fade controller enters a DynamicFogSubVolume, the fog profile blends to the sub-volume's profile.
List<DynamicFogSubVolume> subVolumes
Optional list of allowed sub-volumes. If empty, all sub-volumes in the scene are considered. Use this to filter which sub-volumes affect this fog.
Fog of War
bool enableFogOfWar
Enables the Fog of War system, which allows clearing or restoring fog in specific world areas at runtime.
bool fogOfWarShowBoundaries
Shows the Fog of War area boundaries in the scene view for debugging.
Vector3 fogOfWarCenter
World-space center of the Fog of War area.
Vector3 fogOfWarSize
Size of the Fog of War area in world units.
int fogOfWarTextureSize
Resolution of the internal Fog of War texture. Higher values allow finer control but use more memory. Must be a power of 2.
float fogOfWarRestoreDelay
Default delay in seconds before cleared fog starts restoring. Used when no explicit restore delay is specified in method calls.
float fogOfWarRestoreDuration
Default duration in seconds for the fog restoration animation. Used when no explicit restore duration is specified.
float fogOfWarSmoothness
Default smoothness for the edges of cleared fog areas. Higher values create softer transitions. Used when not specified in method calls.
bool fogOfWarBlur
Enables blur on the Fog of War texture for smoother visual transitions between cleared and fogged areas.
Texture2D fogOfWarTexture property
Gets or sets the Fog of War texture. The texture must be square. Setting this replaces the internal texture with a custom one.
Color32[] fogOfWarTextureData property
Direct access to the Fog of War pixel data. Read the array, modify pixels, write it back, then call UpdateFogOfWar(true) to apply changes. The alpha channel controls fog visibility (255 = full fog, 0 = clear).
Methods
void UpdateMaterialProperties()
Applies any changes made to the fog profile or component properties to the material. Call this after modifying profile fields at runtime.
void UpdateFogOfWar(bool forceUpload = false)
Uploads Fog of War texture changes to the GPU. Call with true after modifying fogOfWarTextureData directly.
void ReloadFogOfWarTexture()
Recreates the internal Fog of War texture. Call this after changing fogOfWarTextureSize at runtime.
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha)
Instantly sets the fog alpha within a circular area. Alpha 0 = fully clear, 1 = full fog.
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, float duration)
Sets the fog alpha within a circular area with an animated transition over the specified duration in seconds.
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, float duration, float smoothness)
Sets the fog alpha with animated transition and edge smoothness control.
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha, bool blendAlpha, float duration, float smoothness, float restoreDelay, float restoreDuration)
Full-control overload. All parameters are explicit:
blendAlpha — if true, blends with existing alpha instead of replacing
duration — transition time in seconds
smoothness — edge softness
restoreDelay — seconds before fog starts restoring
restoreDuration — seconds for the restoration animation
void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, float duration)
Sets the fog alpha within a rectangular area defined by Bounds, with animated transition.
void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, float duration, float smoothness)
Sets the fog alpha within a Bounds area with transition and edge smoothness.
void SetFogOfWarAlpha(Bounds bounds, float fogNewAlpha, bool blendAlpha, float duration, float smoothness, float restoreDelay, float restoreDuration)
Full-control Bounds overload with all parameters explicit.
void ResetFogOfWarAlpha(Vector3 worldPosition, float radius)
Resets the fog to full opacity within a circular area (equivalent to setting alpha to 1).
void ResetFogOfWarAlpha(Bounds bounds)
Resets fog to full opacity within a rectangular Bounds area.
void ResetFogOfWarAlpha(Vector3 position, Vector3 size)
Resets fog to full opacity within a rectangular area defined by center position and size.
void ResetFogOfWarAlpha(Vector3 position, float extentsX, float extentsZ)
Resets fog to full opacity within a rectangular area defined by center position and XZ extents.
void ResetFogOfWar(byte alpha = 255)
Resets the entire Fog of War texture. Default alpha 255 restores full fog everywhere. Use alpha 0 to clear all fog.
float GetFogOfWarAlpha(Vector3 worldPosition)
Returns the current fog alpha value at a world position (0 = clear, 1 = full fog). Useful for gameplay logic that depends on fog visibility.
DynamicFogProfile
ScriptableObject that holds all visual fog settings. Create profiles in the Project panel via Create > Dynamic Fog Profile. Multiple fog volumes can share the same profile.
Density & Range
float baseAltitude
Base altitude of the fog in world units. Fog appears above this height up to maxHeight.
bool baseAltitudeClip
When enabled, base altitude acts as a hard clipping plane preventing fog from rendering below it. Default: false.
float maxHeight
Maximum height of the fog relative to the base altitude.
float densityLinear
Linear density multiplier. Controls overall fog thickness using a linear falloff curve.
float densityExponential
Exponential density factor. Higher values create denser fog that concentrates near the ground.
float distanceMax
Maximum rendering distance for the fog in world units.
bool useXYZDistance
When enabled, uses full 3D distance for fog calculations. When disabled, uses only XZ-plane distance (ignoring vertical component).
Colors & Lighting
Color tintColor
Base tint color of the fog. The alpha channel controls overall fog opacity.
float brightness
Overall brightness multiplier for the fog color.
bool useSunColor
When enabled, the fog color is tinted by the directional light (sun) color.
float lightDiffusionPower
Power of the light diffusion effect when looking towards the sun. Higher values create a more concentrated glow.
float lightDiffusionIntensity
Intensity of the light diffusion glow effect.
bool useVerticalGradient
Enables a vertical color gradient on the fog instead of a uniform color.
Gradient verticalGradient
The gradient used for vertical fog coloring when useVerticalGradient is enabled. The gradient maps from base altitude (left) to max height (right).
float dithering
Dithering intensity to reduce color banding artifacts. Recommended range 0–1.
Noise & Animation
float scale
Scale of the noise pattern. Smaller values create larger, more spread-out noise features.
float shift
Vertical shift of the noise pattern. Range -1 to 1. Adjusts the noise baseline position.
float turbulence
Amount of turbulence in the noise pattern. Higher values create more chaotic fog shapes.
float speed
Animation speed of the fog movement.
Vector3 direction
Wind direction for fog animation. Each axis controls movement along that world axis.
float noiseColorBlend
Blend amount between the main tint color and the noise color. Range 0 (only tint) to 1 (only noise color).
Color noiseColor
Secondary color applied to the noise pattern, blended according to noiseColorBlend.
float noiseDistanceAtten
Attenuates noise with distance from the camera. Range 0 (no attenuation) to 1 (noise fades completely at distance). Helps reduce visual noise at far distances.
Rendering
bool boxProjection
Enables box projection for the fog volume. When enabled, the fog is projected within the volume bounds rather than extending infinitely.
bool depthClip
Enables depth clipping so the fog is occluded by scene geometry. When disabled, the fog renders on top of everything within the volume.
int renderQueue
Render queue for the fog material. Default 3099 (transparent range). Adjust to control rendering order relative to other transparent objects.
int sortingLayerID
Sorting layer ID for the fog renderer. Controls rendering order with other sorting layers.
int sortingOrder
Sorting order within the sorting layer for the fog renderer.
Events
event OnSettingsChanged onSettingsChanged()
Fired whenever any profile setting is modified. Subscribe to this event to react to fog configuration changes.
fog.profile.onSettingsChanged += () => {
Debug.Log("Fog settings changed");
};
Methods
void ValidateSettings()
Validates and clamps all profile settings to valid ranges. Called automatically by the inspector but can be called manually after bulk changes via script.
void Lerp(DynamicFogProfile p1, DynamicFogProfile p2, float t)
Interpolates all settings between two profiles. t is clamped 0–1 where 0 = p1 and 1 = p2. The result is stored in the current profile instance.
// Blend between two profiles over time
DynamicFogProfile result = ScriptableObject.CreateInstance<DynamicFogProfile>();
result.Lerp(profileA, profileB, Mathf.PingPong(Time.time, 1f));
fog.profile = result;
fog.UpdateMaterialProperties();
Code Examples
Basic Setup
using UnityEngine;
using DynamicFogAndMist2;
public class FogController : MonoBehaviour {
public DynamicFog fog;
void Start() {
// Modify fog appearance at runtime
fog.profile.densityLinear = 0.3f;
fog.profile.tintColor = new Color(0.8f, 0.85f, 0.9f, 0.6f);
fog.profile.speed = 0.5f;
fog.profile.direction = new Vector3(1f, 0f, 0.5f);
fog.UpdateMaterialProperties();
}
}
Swap Profiles at Runtime
using UnityEngine;
using DynamicFogAndMist2;
public class WeatherSystem : MonoBehaviour {
public DynamicFog fog;
public DynamicFogProfile clearWeather;
public DynamicFogProfile stormWeather;
public void SetStorm() {
fog.profile = stormWeather;
fog.UpdateMaterialProperties();
}
public void SetClear() {
fog.profile = clearWeather;
fog.UpdateMaterialProperties();
}
}
Smooth Profile Transitions
using UnityEngine;
using DynamicFogAndMist2;
public class FogTransition : MonoBehaviour {
public DynamicFog fog;
public DynamicFogProfile fromProfile;
public DynamicFogProfile toProfile;
public float transitionDuration = 3f;
DynamicFogProfile blendProfile;
float t;
void Start() {
blendProfile = ScriptableObject.CreateInstance<DynamicFogProfile>();
}
void Update() {
if (t < 1f) {
t += Time.deltaTime / transitionDuration;
blendProfile.Lerp(fromProfile, toProfile, t);
fog.profile = blendProfile;
fog.UpdateMaterialProperties();
}
}
public void StartTransition() {
t = 0f;
}
}
Fog of War — Clear Around Player
using UnityEngine;
using DynamicFogAndMist2;
public class FogOfWarController : MonoBehaviour {
public DynamicFog fog;
public float clearRadius = 10f;
public float clearDuration = 0.5f;
public float restoreDelay = 5f;
public float restoreDuration = 3f;
void Update() {
// Clear fog around player with auto-restore
fog.SetFogOfWarAlpha(
transform.position,
clearRadius,
0f, // fully clear
false, // replace (don't blend)
clearDuration,
0.8f, // smooth edges
restoreDelay,
restoreDuration
);
}
}
Fog of War — Read and Modify Texture Directly
using UnityEngine;
using DynamicFogAndMist2;
public class FogOfWarDirect : MonoBehaviour {
public DynamicFog fog;
public void ClearHorizontalStripe(int y, int height) {
Color32[] data = fog.fogOfWarTextureData;
int size = fog.fogOfWarTextureSize;
for (int row = y; row < Mathf.Min(y + height, size); row++) {
for (int col = 0; col < size; col++) {
int idx = row * size + col;
data[idx] = new Color32(data[idx].r, data[idx].g, data[idx].b, 0);
}
}
fog.fogOfWarTextureData = data;
fog.UpdateFogOfWar(true);
}
public void CheckVisibility(Vector3 worldPos) {
float alpha = fog.GetFogOfWarAlpha(worldPos);
if (alpha < 0.1f) {
Debug.Log("Position is visible (fog cleared)");
}
}
}
Distance Fade with Player
using UnityEngine;
using DynamicFogAndMist2;
public class FogFade : MonoBehaviour {
public DynamicFog fog;
public Transform player;
void Start() {
fog.enableFade = true;
fog.fadeDistance = 50f;
fog.fadeController = player;
}
}Suggest an improvement
Help us improve this documentation page.