Skip to content

Instantly share code, notes, and snippets.

@PeturDarriPeturs
Last active December 17, 2023 00:44
Show Gist options
  • Save PeturDarriPeturs/0233360372a4e53562d06cafcccaa12c to your computer and use it in GitHub Desktop.
Save PeturDarriPeturs/0233360372a4e53562d06cafcccaa12c to your computer and use it in GitHub Desktop.
Unlimited custom directional lights using RenderSettings.ambientProbe.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
[ExecuteAlways]
public class CustomAmbientLights : MonoBehaviour
{
[ColorUsage(false, true)]
public Color ambientLight;
public List<CustomLight> lights = new List<CustomLight>();
private void Update()
{
UpdateLights();
}
public void UpdateLights()
{
SphericalHarmonicsL2 probe = new SphericalHarmonicsL2();
probe.AddAmbientLight(ambientLight);
foreach (CustomLight light in lights)
{
Quaternion rotation = Quaternion.Euler(light.rotation);
Vector3 direction = rotation * Vector3.forward;
probe.AddDirectionalLight(direction, light.color, light.intensity);
}
RenderSettings.ambientMode = AmbientMode.Custom;
RenderSettings.ambientProbe = probe;
}
[Serializable]
public class CustomLight
{
public Vector3 rotation;
[ColorUsage(false, true)]
public Color color;
public float intensity = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment