using System; using System.Collections; using System.Collections.Generic; using CSRUtilities; using UnityEngine; using UnityEngine.Serialization; public class CSRGlobalController : MonoBehaviour { [Serializable] private class CycleData { [SerializeField] private string name = string.Empty; [SerializeField] private Light light = null; [Tooltip("value of -1 infinite repeat")][SerializeField] private int repeat = -1; [SerializeField] private List steps = new List(); public string Name => name; public void ExecuteCycling(MonoBehaviour monoBehaviour) { monoBehaviour.StartCoroutine(InternalExecuteCycling()); } private IEnumerator InternalExecuteCycling() { int counter = 0; while (counter < repeat || repeat == -1) { for (var index = 0; index < steps.Count; index++) { var step = steps[index]; var nextStep = index < steps.Count - 1 ? index + 1 : 0; yield return step.ApplyCycling(steps[nextStep], light); } counter++; } } public object FindState(string stateName) { throw new NotImplementedException(); } public void ApplyShaderValues(int stateIndex, int shaderIndex) { if (stateIndex >= steps.Count){return;} var state = steps[stateIndex]; state.ApplyShaderGroupValues(shaderIndex); state.ApplyLightValues(light); } public void ApplyShaderValues(int stateIndex) { if (stateIndex >= steps.Count){return;} var state = steps[stateIndex]; state.ApplyShaderGroupValues(); state.ApplyLightValues(light); } public CycleStepData GetState(int stateIndex) { return steps[stateIndex]; } } [Serializable] private class CycleStepData { [Serializable] private class AudioData { [SerializeField] private AudioSource audioSource; [SerializeField] private float initialVolume; [SerializeField] private float endVolume; public void Update(float lerpValue) { audioSource.volume = Mathf.Lerp(initialVolume, endVolume, lerpValue); } } [SerializeField] private string name= string.Empty; [Tooltip("The time it would take to transition to the next state")] [FormerlySerializedAs("duration")] [SerializeField] private float transitionTime = 0f; [Tooltip("The time between update of the shader values")] [FormerlySerializedAs("updateInterval")] [SerializeField] private float transitionTimeStep = 0.01f; [Tooltip("The duration of this state")] [FormerlySerializedAs("delay")] [SerializeField] private float time = 0f; [SerializeField] private int repeat = -1; [SerializeField] private List shaderDataList = new List(); [SerializeField] private ParticleSystem[] particles = new ParticleSystem[0]; [SerializeField] private GameObject[] gameObjects = new GameObject[0]; [SerializeField] private AudioData[] audioSources = new AudioData[0]; [SerializeField] private float lightIntensity = 0f; [SerializeField] private Color lightColor = Color.white; public object Name => name; public void ApplyShaderGroupValues(int shaderIndex) { if (shaderIndex >= shaderDataList.Count){return;} var data = shaderDataList[shaderIndex]; data.ApplyGroupValues(); } public IEnumerator ApplyCycling(CycleStepData nextStep, Light light) { EnableParticles(); SetActiveGameObjects(true); foreach (var data in shaderDataList) { data.SetNextCycle(nextStep.shaderDataList); } if (light != null) { light.intensity = lightIntensity; light.color = lightColor; } UpdateAudioDataArray(0f); UpdateShaders(0f); var delayWFS = new WaitForSeconds(time); yield return delayWFS; var startingTime = Time.time; var cyclingWaitForSeconds = new WaitForSeconds(transitionTimeStep); while (Time.time - startingTime < transitionTime * Mathf.Max(repeat,1)) { //var lerpValue = Mathf.InverseLerp(-1f, 1f, Mathf.Sin((Time.time - startingTime) / duration * 2f * Mathf.PI - Mathf.PI * 0.5f)); var lerpValue = Mathf.InverseLerp(0, transitionTime, Time.time - startingTime); UpdateShaders(lerpValue); Update(lerpValue); UpdateAudioDataArray(lerpValue); yield return cyclingWaitForSeconds; } UpdateShaders(1f); UpdateAudioDataArray(1f); OnCompleted(); void Update(float lerpValue) { if (light == null) {return;} light.intensity = Mathf.Lerp(lightIntensity, nextStep.lightIntensity, lerpValue); light.color = Color.Lerp(lightColor, nextStep.lightColor, lerpValue); } } private void UpdateAudioDataArray(float lerpValue) { foreach (var audioData in audioSources) { audioData.Update(lerpValue); } } private void UpdateShaders(float lerpValue) { foreach (var shaderData in shaderDataList) { shaderData.Apply(lerpValue); } } private void SetActiveGameObjects(bool active) { foreach (var gameObject in gameObjects) { gameObject.SetActive(active); } } private void OnCompleted() { DisableParticles(); SetActiveGameObjects(false); } private void EnableParticles() { foreach (var particle in particles) { particle.Play(true); } } private void DisableParticles() { foreach (var particle in particles) { particle.Stop(true, ParticleSystemStopBehavior.StopEmitting); } } public void ApplyShaderGroupValues() { foreach (var shaderData in shaderDataList) { shaderData.ApplyGroupValues(); } } public ShaderData GetShader(int shaderIndex) { return shaderDataList[shaderIndex]; } public void SetShader(int shaderIndex, ShaderData shaderPropertyCopy) { shaderDataList[shaderIndex] = shaderPropertyCopy.DeepCopy(); } public void ApplyLightValues(Light light) { if (light == null){return;} light.intensity = lightIntensity; light.color = lightColor; } } [Serializable] public class ShaderData: IEquatable { public string shaderName = string.Empty; [Header("Affected Materials")] [SerializeField] private List materials = new List(); [SerializeField] private ShaderPropertyGroup shaderPropertyGroups = null; public List Materials => materials; public void AutoPopulateMaterials() { materials.Clear(); var shader = Shader.Find(shaderName); foreach (var renderer in FindObjectsOfType()) { foreach (var material in renderer.sharedMaterials) { if (materials.Contains(material) == false && material.shader == shader) { materials.Add(material); } } } } public void ApplyDefault() { shaderPropertyGroups.ApplyDefault(materials); } public void SetNextCycle(List shaderDataArray) { var data = shaderDataArray.Find(shader => shader.Equals(this)); var group = data?.shaderPropertyGroups; shaderPropertyGroups.SetNextGroupInCycle(group); } public void Apply(float lerpValue) { shaderPropertyGroups.Apply(lerpValue, materials); } public void ApplyGroupValues() { shaderPropertyGroups.ApplyDefault(materials); } public bool Equals(ShaderData other) { return other.shaderName == shaderName && materials.TrueForAll(material => other.materials.Contains(material)); } } [Serializable] private class ShaderPropertyGroup { [SerializeField] private string groupName = string.Empty; [SerializeField] private List properties = new List(); public string Name => groupName; public void ApplyDefault(List materials) { Apply(0f, materials); } public void Apply(float lerpValue, List materials) { foreach (var property in properties) { property.Apply(lerpValue, materials); } } public void SetNextGroupInCycle(ShaderPropertyGroup nextPropertyGroup) { if (nextPropertyGroup == null){ return;} foreach (var property in properties) { var nextProperty = nextPropertyGroup.properties.Find(item => item.Name == property.Name); if (nextProperty != null) { property.SetNextStepValues(nextProperty); } } } } [Serializable] private class ShaderProperty { public string Name => name; [SerializeField] private string name = string.Empty; [SerializeField] private PropertyType type = PropertyType.Float; [SerializeField] private Color colorDefault = Color.white; [SerializeField] private float floatDefault = 0f; [SerializeField] private bool boolean = false; [SerializeField] private string keyword = String.Empty; private ShaderProperty _nextInCycle; private enum PropertyType { Float = 0, Bool = 1, Color = 2 } public void Apply(float lerpValue, List materials) { foreach (var material in materials) { switch (type) { case PropertyType.Float: material.SetFloat(name, Mathf.Lerp(floatDefault, _nextInCycle?.floatDefault ?? floatDefault, lerpValue)); break; case PropertyType.Bool: material.SetInt(name, (lerpValue < 1f? boolean: _nextInCycle?.boolean ?? boolean) ? 1 : 0); if (boolean) { material.EnableKeyword(keyword); } else { material.DisableKeyword(keyword); } break; case PropertyType.Color: material.SetColor(name, Color.Lerp(colorDefault, _nextInCycle?.colorDefault ?? colorDefault, lerpValue)); break; default: throw new ArgumentOutOfRangeException(); } } } public void SetNextStepValues(ShaderProperty shaderProperty) { _nextInCycle = shaderProperty; } } [FormerlySerializedAs("_cycles")] [SerializeField] private List cycles = new List(); public void ApplyShaderProperty(int cycleIndex, int stateIndex, int shaderIndex) { if (cycleIndex >= cycles.Count){return;} var cycle = cycles[cycleIndex]; cycle.ApplyShaderValues(stateIndex, shaderIndex); } public void ApplyShaderProperty(int cycleIndex, int stateIndex) { if (cycleIndex >= cycles.Count){return;} var cycle = cycles[cycleIndex]; cycle.ApplyShaderValues(stateIndex); } private void Start() { foreach (var data in cycles) { data.ExecuteCycling(this); } } public ShaderData GetShaderData(int cycleIndex, int stateIndex, int shaderIndex) { return cycles[cycleIndex].GetState(stateIndex).GetShader(shaderIndex); } public void SetShaderData(int cycleIndex, int stateIndex, int shaderIndex, ShaderData shaderPropertyCopy) { cycles[cycleIndex].GetState(stateIndex).SetShader(shaderIndex, shaderPropertyCopy); } }