239 lines
8.6 KiB
C#
239 lines
8.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[System.Serializable]
|
|
public class EmisssiveRendererData
|
|
{
|
|
public Renderer renderer;
|
|
public float maxIntensity;
|
|
}
|
|
|
|
public class LightsTransitionController : MonoBehaviour
|
|
{
|
|
public List<EmisssiveRendererData> flickeringRenderersData; // List of Lights and their maxIntensity
|
|
|
|
[SerializeField] private float transitionDuration = 2.0f; // Duration of light transition
|
|
[SerializeField] private float timeBetweenLights = 1.0f; // Time between lights
|
|
[SerializeField] private float initialIntensity = 0.0f; // Initial intensity of the lights
|
|
[SerializeField] private float targetIntensity = 1.0f; // Target intensity of the lights
|
|
[SerializeField] private float startIntensity = 1.0f;
|
|
|
|
[SerializeField] private GameObject[] objectsWithLights; // List of objects with lights
|
|
[SerializeField] private List<LampRenderersGroup> lampRenderersGroup;
|
|
public string targetLayer = "Emissive";
|
|
|
|
private int currentObjectIndex = 0; // Index of the current object
|
|
private int currentLampRendererGroupIndex = 0;
|
|
[SerializeField] private List<Light> allLights;
|
|
[SerializeField] private Light[] currentObjectLights; // Lights in the current object
|
|
[SerializeField] private LampRenderersGroup[] currentLampRenderersGroup;
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; i < objectsWithLights.Length; i++)
|
|
{
|
|
allLights.AddRange(objectsWithLights[i].GetComponentsInChildren<Light>());
|
|
}
|
|
|
|
for (int i = 0; i < objectsWithLights.Length; i++)
|
|
{
|
|
lampRenderersGroup.Add(objectsWithLights[i].GetComponentInChildren<LampRenderersGroup>());
|
|
}
|
|
|
|
|
|
//StartCoroutine(TransitionLights());
|
|
}
|
|
|
|
public void LightsIntensityZero()
|
|
{
|
|
foreach (var light in allLights)
|
|
{
|
|
light.intensity = 0f;
|
|
}
|
|
for (int i = 0; i < lampRenderersGroup.Count; i++)
|
|
{
|
|
foreach (var lampCapLODRenderers in lampRenderersGroup[i].lampCapLODRenderers)
|
|
{
|
|
lampCapLODRenderers.material.SetFloat("_EmissiveExposureWeight", 3);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Debugging
|
|
//private void Update()
|
|
//{
|
|
// if (Input.GetKeyDown(KeyCode.Alpha0))
|
|
// {
|
|
// lampRenderersGroup.Clear();
|
|
// StartLightsTransition();
|
|
// }
|
|
//}
|
|
#endregion
|
|
|
|
public void StartLightsTransition()
|
|
{
|
|
lampRenderersGroup.Clear();
|
|
for (int i = 0; i < objectsWithLights.Length; i++)
|
|
{
|
|
lampRenderersGroup.Add(objectsWithLights[i].GetComponentInChildren<LampRenderersGroup>());
|
|
}
|
|
|
|
|
|
StartCoroutine(TransitionLights());
|
|
}
|
|
|
|
private IEnumerator TransitionLights()
|
|
{
|
|
while (true)
|
|
{
|
|
// Transition lights of the previous object
|
|
if (currentObjectLights != null)
|
|
{
|
|
yield return StartCoroutine(FadeOutLights(currentObjectLights, currentLampRenderersGroup));
|
|
}
|
|
|
|
// Transition lights of the current object
|
|
currentObjectLights = objectsWithLights[currentObjectIndex].GetComponentsInChildren<Light>();
|
|
|
|
currentLampRenderersGroup[0] = lampRenderersGroup[currentLampRendererGroupIndex];
|
|
|
|
|
|
////I added:
|
|
//foreach (Renderer objectsWithALLRenderes in objectsWithRenderers)
|
|
//{
|
|
// if (objectsWithALLRenderes.gameObject.layer == LayerMask.NameToLayer(targetLayer))
|
|
// {
|
|
// currentObjectRenderers.Add(objectsWithALLRenderes); //So it's full of the taget layer renderes.
|
|
// }
|
|
//}
|
|
|
|
yield return StartCoroutine(TransitionObjectLights(currentObjectLights, currentLampRenderersGroup));
|
|
|
|
// Wait before moving to the next object
|
|
yield return new WaitForSeconds(timeBetweenLights);
|
|
|
|
// Move to the next object
|
|
currentObjectIndex = (currentObjectIndex + 1) % objectsWithLights.Length;
|
|
currentLampRendererGroupIndex = (currentLampRendererGroupIndex + 1) % lampRenderersGroup.Count;
|
|
}
|
|
}
|
|
|
|
private IEnumerator TransitionObjectLights(Light[] lights, LampRenderersGroup[] renderers)
|
|
{
|
|
foreach (Light light in lights)
|
|
{
|
|
// Transition current light
|
|
foreach (var renderer in renderers)
|
|
{
|
|
yield return StartCoroutine(TransitionLight(light, renderer));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private IEnumerator TransitionLight(Light light, LampRenderersGroup objectEmissiveRenderer)
|
|
{
|
|
//float flickeringIntensity;
|
|
|
|
float elapsedTime = 0.0f;
|
|
float startIntensity = light.intensity;
|
|
|
|
while (elapsedTime < transitionDuration)
|
|
{
|
|
light.intensity = Mathf.Lerp(startIntensity, targetIntensity, elapsedTime / transitionDuration);
|
|
#region Old Test
|
|
//flickeringIntensity = light.intensity;
|
|
//// Adjust emission intensities of all renderers based on flickering intensity
|
|
//foreach (EmisssiveRendererData data in flickeringRenderersData)
|
|
//{
|
|
// Material[] materialsArray = data.renderer.materials; // Get all materials of the current Renderer
|
|
|
|
// foreach (var lightBulbRendMaterial in materialsArray)
|
|
// {
|
|
// int rendererIndex = flickeringRenderersData.IndexOf(data);
|
|
// //float originalEmissionIntensity = originalRendererIntensities[rendererIndex];
|
|
// flickeringIntensity = targetIntensity;
|
|
// float flickerEmissionIntensity = 0f * flickeringIntensity * data.maxIntensity;
|
|
// lightBulbRendMaterial.SetFloat("_EmissiveExposureWeight", flickerEmissionIntensity);
|
|
// }
|
|
// data.renderer.materials = materialsArray;
|
|
//}
|
|
#endregion
|
|
float emissiveIntensity = Mathf.Lerp(2f, 0f, elapsedTime / transitionDuration);
|
|
//objectEmissiveRenderer.material.SetFloat("_EmissiveExposureWeight", emissiveIntensity);
|
|
foreach (var lampCapLODRenderers in objectEmissiveRenderer.lampCapLODRenderers)
|
|
{
|
|
lampCapLODRenderers.material.SetFloat("_EmissiveExposureWeight", emissiveIntensity);
|
|
}
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure the intensity is set to the target value
|
|
light.intensity = targetIntensity;
|
|
}
|
|
|
|
private IEnumerator FadeOutLights(Light[] lights, LampRenderersGroup[] objectEmissiveRenderer)
|
|
{
|
|
float elapsedTime = 0.0f;
|
|
float[] startIntensities = new float[lights.Length];
|
|
|
|
// Record the current intensities
|
|
for (int i = 0; i < lights.Length; i++)
|
|
{
|
|
startIntensities[i] = lights[i].intensity;
|
|
}
|
|
|
|
while (elapsedTime < transitionDuration)
|
|
{
|
|
for (int i = 0; i < lights.Length; i++)
|
|
{
|
|
lights[i].intensity = Mathf.Lerp(startIntensities[i], initialIntensity, elapsedTime / transitionDuration);
|
|
}
|
|
|
|
float emissiveIntensity = Mathf.Lerp(0f, 3f, elapsedTime / transitionDuration);
|
|
//objectEmissiveRenderer.material.SetFloat("_EmissiveExposureWeight", emissiveIntensity);
|
|
for (int i = 0; i < objectEmissiveRenderer.Length; i++)
|
|
{
|
|
|
|
|
|
foreach (var lampCapLODRenderers in objectEmissiveRenderer[i].lampCapLODRenderers)
|
|
{
|
|
lampCapLODRenderers.material.SetFloat("_EmissiveExposureWeight", emissiveIntensity);
|
|
}
|
|
}
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure the intensities are set to the initial value
|
|
for (int i = 0; i < lights.Length; i++)
|
|
{
|
|
lights[i].intensity = initialIntensity;
|
|
}
|
|
}
|
|
|
|
public void ResetLights()
|
|
{
|
|
StopAllCoroutines();
|
|
// Iterate over all lights in the scene
|
|
foreach (Light light in allLights)
|
|
{
|
|
// Reset light intensity to initial value
|
|
light.intensity = startIntensity;
|
|
}
|
|
|
|
// Iterate over all lamp renderers groups
|
|
foreach (LampRenderersGroup lampRendererGroup in lampRenderersGroup)
|
|
{
|
|
// Reset emissive exposure weight to initial value for each renderer in the group
|
|
foreach (Renderer renderer in lampRendererGroup.lampCapLODRenderers)
|
|
{
|
|
renderer.material.SetFloat("_EmissiveExposureWeight", initialIntensity);
|
|
}
|
|
}
|
|
}
|
|
}
|