Scripting Support (C#)
advanced scriptingDynamic Fog & Mist 2 (Built-in) · Scripting Support (C#)
Control Dynamic Fog & Mist 2 (Built-in) at runtime using the DynamicFogAndMist namespace. The fog is an image effect attached to the camera. Access it via the singleton DynamicFog.instance or GetComponent.
using DynamicFogAndMist;
DynamicFog fog = DynamicFog.instance;
fog.alpha = 0.8f;
fog.color = Color.gray;
fog.UpdateMaterialProperties();
DynamicFog
MonoBehavior image effect attached to the camera. Inherits from DynamicFogBase. All fog appearance properties are set directly on the component (not through a profile). Access the singleton via DynamicFog.instance.
General Settings
static DynamicFog instance read-only
Singleton reference to the active DynamicFog component in the scene.
Camera fogCamera read-only
The camera this fog effect is attached to.
DynamicFogProfile profile
Gets or sets a profile. Setting this property loads the profile settings into the fog component. Use profiles to save and restore fog configurations.
bool profileSync
When enabled, profile settings are reloaded every time the component is enabled.
FOG_PRESET preset
Built-in fog preset (Clear, Mist, WindyMist, GroundFog, Fog, HeavyFog, SandStorm, Custom). Setting a preset overwrites all appearance properties with predefined values.
FOG_TYPE effectType
Fog rendering mode. Different types use different shaders optimized for desktop or mobile platforms. Changing this at runtime recreates the fog material.
GameObject sun
Reference to the directional light used for fog lighting and scattering calculations.
bool useFogVolumes
Enables FogVolume trigger support. Must be true for SetTargetProfile, SetTargetAlpha, and SetTargetColors transitions to work.
Fog Appearance
float alpha
Overall fog opacity. Range 0 (invisible) to 1 (fully opaque).
Color color
Primary fog color.
Color color2
Secondary fog color used for the gradient effect.
float noiseStrength
Strength of the noise pattern applied to the fog. Range 0 to 1.
float noiseScale
Scale of the noise texture. Range 0.01 to 1. Smaller values create larger noise features.
bool enableDithering
Enables dithering to reduce color banding artifacts.
float ditherStrength
Dithering intensity. Range 0 to 0.3.
Distance & Height
float distance
Starting distance of the fog from the camera. Range 0 to 0.999.
float distanceFallOff
Fog density falloff with distance. Range 0.0001 to 5.
float maxDistance
Maximum fog rendering distance. Range 0 to 1.2. Fog is invisible beyond this distance.
float maxDistanceFallOff
Falloff at maximum distance. Range 0.0001 to 0.5.
float height
Height of the fog layer above the baseline.
float maxHeight
Maximum height of the fog. Range 0 to 500.
float heightFallOff
Fog density falloff with height. Range 0.0001 to 1. Used in orthogonal fog types.
float baselineHeight
Baseline height where fog starts in world units.
bool clipUnderBaseline
When enabled, fog is not rendered below the baseline height.
bool useXZDistance
When enabled, uses only XZ-plane distance for fog calculations (ignoring vertical component). Useful for top-down or isometric views.
Animation
float turbulence
Turbulence intensity of the fog animation. Range 0 to 15.
float speed
Fog movement speed. Range 0 to 5.
Vector3 windDirection
Wind direction vector for fog movement.
Sky Haze
float skyHaze
Sky haze intensity. Controls the haze effect at the horizon and sky.
float skySpeed
Sky noise animation speed. Range 0 to 1.
float skyNoiseStrength
Sky noise strength. Range 0 to 1.
float skyAlpha
Sky haze opacity. Range 0 to 1.
Light Scattering
float scattering
Light scattering intensity when looking towards the sun. Range 0 to 1.
Color scatteringColor
Tint color for the light scattering effect.
bool skyboxBlending
Enables skybox blending mode for better integration with the skybox.
Fog of War
bool fogOfWarEnabled
Enables the Fog of War system.
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. Must be a power of 2.
VR
bool useSinglePassStereoRenderingMatrix
Enable for VR single-pass stereo rendering support.
Methods
void UpdateMaterialProperties()
Applies all property changes to the fog material. Call this after modifying fog properties at runtime.
string GetCurrentPresetName()
Returns the name of the current fog preset as a string.
void CheckPreset()
Applies the current preset values to all fog properties. Call this after setting preset to apply its values.
Transitions
void SetTargetProfile(DynamicFogProfile targetProfile, float duration)
Smoothly transitions all fog settings to match the target profile over the specified duration. Requires useFogVolumes = true.
void ClearTargetProfile(float duration)
Smoothly transitions back to the original fog settings before SetTargetProfile was called.
void SetTargetAlpha(float newFogAlpha, float newSkyHazeAlpha, float duration)
Smoothly transitions fog opacity and sky haze alpha to the target values.
void ClearTargetAlpha(float duration)
Restores the original fog and sky haze alpha values.
void SetTargetColors(Color color1, Color color2, float duration)
Smoothly transitions both fog colors to the target values.
void ClearTargetColors(float duration)
Restores the original fog colors.
Fog of War Methods
float GetFogOfWarAlpha(Vector3 worldPosition)
Returns the current fog alpha at a world position (0 = clear, 1 = full fog).
void SetFogOfWarAlpha(Vector3 worldPosition, float radius, float fogNewAlpha)
Sets the fog alpha within a circular area. Alpha 0 = fully clear, 1 = full fog.
void ResetFogOfWar()
Resets the entire Fog of War texture to full opacity.
DynamicFogProfile
ScriptableObject for saving and loading fog configurations. Create via Create > Dynamic Fog Profile in the Project panel. All fields mirror the corresponding DynamicFog properties.
Methods
void Load(DynamicFogBase fog)
Applies all profile settings to the specified fog instance.
DynamicFogProfile profile = Resources.Load<DynamicFogProfile>("MyProfile");
profile.Load(DynamicFog.instance);
void Save(DynamicFogBase fog)
Captures the current fog settings into this profile.
static void Lerp(DynamicFogProfile p1, DynamicFogProfile p2, float t, DynamicFogBase fog)
Interpolates between two profiles and applies the result to the fog instance. t is clamped 0–1 where 0 = p1 and 1 = p2.
// Smooth transition between two profiles
DynamicFogProfile.Lerp(profileA, profileB, t, DynamicFog.instance);
FogVolume
Trigger-based component that smoothly transitions fog settings when a collider enters its volume. Add to a GameObject with a trigger Collider.
bool enableProfileTransition
Enables full profile transition on trigger enter.
DynamicFogProfile targetProfile
The profile to transition to when a collider enters.
bool enableAlphaTransition
Enables fog and sky haze alpha transition on trigger enter.
float targetFogAlpha
Target fog alpha when inside the volume.
float targetSkyHazeAlpha
Target sky haze alpha when inside the volume.
bool enableFogColorTransition
Enables fog color transition on trigger enter.
Color targetFogColor1
Target primary fog color when inside the volume.
Color targetFogColor2
Target secondary fog color when inside the volume.
float transitionDuration
Duration of the transition in seconds.
Collider targetCollider
If set, only this specific collider triggers the volume. If null, any collider triggers it.
DynamicFog targetFog
The fog component to affect. If null, uses DynamicFog.instance.
Code Examples
Basic Runtime Control
using UnityEngine;
using DynamicFogAndMist;
public class FogController : MonoBehaviour {
void Start() {
DynamicFog fog = DynamicFog.instance;
fog.alpha = 0.7f;
fog.color = new Color(0.6f, 0.65f, 0.7f);
fog.color2 = new Color(0.5f, 0.55f, 0.6f);
fog.turbulence = 5f;
fog.speed = 0.3f;
fog.windDirection = new Vector3(1f, 0f, 0.5f);
fog.UpdateMaterialProperties();
}
}
Apply Preset at Runtime
using UnityEngine;
using DynamicFogAndMist;
public class PresetSwitcher : MonoBehaviour {
public void SetHeavyFog() {
DynamicFog fog = DynamicFog.instance;
fog.preset = FOG_PRESET.HeavyFog;
fog.CheckPreset();
}
public void SetClear() {
DynamicFog fog = DynamicFog.instance;
fog.preset = FOG_PRESET.Clear;
fog.CheckPreset();
}
}
Smooth Profile Transition
using UnityEngine;
using DynamicFogAndMist;
public class FogTransition : MonoBehaviour {
public DynamicFogProfile stormProfile;
public float duration = 3f;
void Start() {
// Enable fog volumes for transitions
DynamicFog.instance.useFogVolumes = true;
}
public void EnterStorm() {
DynamicFog.instance.SetTargetProfile(stormProfile, duration);
}
public void ExitStorm() {
DynamicFog.instance.ClearTargetProfile(duration);
}
}
Lerp Between Profiles
using UnityEngine;
using DynamicFogAndMist;
public class DayNightFog : MonoBehaviour {
public DynamicFogProfile dayProfile;
public DynamicFogProfile nightProfile;
void Update() {
// Blend based on time of day (0 = day, 1 = night)
float t = Mathf.PingPong(Time.time * 0.1f, 1f);
DynamicFogProfile.Lerp(dayProfile, nightProfile, t, DynamicFog.instance);
}
}
Fog of War — Clear Around Player
using UnityEngine;
using DynamicFogAndMist;
public class FogOfWarController : MonoBehaviour {
public float clearRadius = 10f;
void Update() {
DynamicFog fog = DynamicFog.instance;
fog.SetFogOfWarAlpha(transform.position, clearRadius, 0f);
}
public void ResetAll() {
DynamicFog.instance.ResetFogOfWar();
}
public void CheckVisibility(Vector3 pos) {
float alpha = DynamicFog.instance.GetFogOfWarAlpha(pos);
if (alpha < 0.1f) {
Debug.Log("Area is visible");
}
}
}
Color and Alpha Transitions
using UnityEngine;
using DynamicFogAndMist;
public class FogEffects : MonoBehaviour {
void Start() {
DynamicFog.instance.useFogVolumes = true;
}
public void FadeToRed() {
DynamicFog fog = DynamicFog.instance;
fog.SetTargetColors(
new Color(0.8f, 0.2f, 0.1f),
new Color(0.5f, 0.1f, 0.05f),
2f
);
fog.SetTargetAlpha(1f, 0.5f, 2f);
}
public void RestoreNormal() {
DynamicFog fog = DynamicFog.instance;
fog.ClearTargetColors(2f);
fog.ClearTargetAlpha(2f);
}
}Suggest an improvement
Help us improve this documentation page.