Scripting Support (C#)

advanced scripting

Volumetric Lights 2 (URP) · Scripting Support (C#)

This page covers the Universal Render Pipeline (URP) version. See also the Built-in version.

The main scripting class is VolumetricLight (namespace VolumetricLights). This MonoBehavior attaches to any GameObject with a Light component. Settings can optionally be stored in a VolumetricLightProfile ScriptableObject for sharing across multiple lights.

Use profile to assign a shared profile and set profileSync = true to keep settings synchronized. Changes to the profile automatically update all lights referencing it. Without a profile, modify fields directly on the component.

VolumetricLight

General

void SetBounds(Bounds bounds)

Sets the custom bounds for the volumetric light in world space. If boundsInLocalSpace is enabled, the bounds center is adjusted relative to the light position.

Events

event PropertiesChangedEvent OnPropertiesChanged(VolumetricLight volumetricLight)

Instance event fired when the volumetric light properties are updated. Subscribe to react to property changes.

Static Members

static List<VolumetricLight> volumetricLights

Static list of all active VolumetricLight instances in the scene. Lights are added on enable and removed on disable.

void PropertiesChangedEvent(VolumetricLight volumetricLight)

Methods

void UpdateMaterialProperties()

Schedules an update of the volumetric light material properties. Call this after modifying light settings at runtime.

VolumetricLightProfile

A ScriptableObject containing the same settings fields as VolumetricLight. Create via Unity menu Assets > Create > Volumetric Light Profile. Assign to one or more lights and set profileSync = true to share settings.

void ApplyTo(VolumetricLight vl)

Copies all profile settings to the specified VolumetricLight instance and calls UpdateMaterialProperties().

void LoadFrom(VolumetricLight vl)

Copies all settings from the specified VolumetricLight instance into this profile.

Code Examples

Basic Configuration

using UnityEngine;
using VolumetricLights;

public class LightController : MonoBehaviour {
    VolumetricLight vl;

    void Start() {
        vl = GetComponent<VolumetricLight>();
        vl.density = 0.5f;
        vl.mediumAlbedo = new Color(1f, 0.9f, 0.8f);
        vl.brightness = 1.5f;
        vl.noiseStrength = 0.8f;
        vl.windDirection = new Vector3(0.05f, 0.01f, 0);
        vl.UpdateMaterialProperties();
    }
}

Shadow Occlusion

VolumetricLight vl = GetComponent<VolumetricLight>();
vl.enableShadows = true;
vl.shadowIntensity = 0.8f;
vl.shadowBakeInterval = ShadowBakeInterval.EveryFrame;
vl.shadowResolution = ShadowResolution._512;
vl.ScheduleShadowCapture();
vl.UpdateMaterialProperties();

Dust Particles

VolumetricLight vl = GetComponent<VolumetricLight>();
vl.enableDustParticles = true;
vl.dustBrightness = 0.8f;
vl.dustMinSize = 0.005f;
vl.dustMaxSize = 0.03f;
vl.dustWindSpeed = 1.5f;
vl.UpdateMaterialProperties();

Auto-Toggle for Performance

VolumetricLight vl = GetComponent<VolumetricLight>();
vl.autoToggle = true;
vl.distanceStartDimming = 50f;
vl.distanceDeactivation = 80f;
vl.autoToggleCheckInterval = 0.5f;

Using Profiles

public VolumetricLightProfile torchProfile;
public VolumetricLightProfile lanternProfile;

void SetupTorch(VolumetricLight vl) {
    torchProfile.ApplyTo(vl);
}

void SaveCurrentSettings(VolumetricLight vl) {
    lanternProfile.LoadFrom(vl);
}

Iterating All Active Lights

foreach (VolumetricLight vl in VolumetricLight.volumetricLights) {
    vl.brightness *= 0.5f;
    vl.UpdateMaterialProperties();
}
Was this page helpful?