Last active
December 17, 2023 00:44
-
-
Save PeturDarriPeturs/0233360372a4e53562d06cafcccaa12c to your computer and use it in GitHub Desktop.
Unlimited custom directional lights using RenderSettings.ambientProbe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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