| . | |
| using System; |
| |
| using System.Collections.Generic; |
| |
| using UnityEngine; |
| |
| |
| |
| public class MaterialFader : MonoBehaviour |
| |
| { |
| |
| private void Awake() |
| |
| { |
| |
| if (this.m_renderers == null) |
| |
| { |
| |
| this.m_renderers = new List<Renderer>(); |
| |
| Renderer[] componentsInChildren = base.GetComponentsInChildren<Renderer>(); |
| |
| if (componentsInChildren != null) |
| |
| { |
| |
| this.m_renderers.AddRange(componentsInChildren); |
| |
| } |
| |
| Renderer component = base.GetComponent<Renderer>(); |
| |
| if (component != null) |
| |
| { |
| |
| this.m_renderers.Add(component); |
| |
| } |
| |
| } |
| |
| if (this.m_renderers.Count == 0) |
| |
| { |
| |
| ZLog.LogError("No MeshRenderer components assigned to MaterialFader!"); |
| |
| } |
| |
| foreach (MaterialFader.FadeProperty fadeProperty in this.m_fadeProperties) |
| |
| { |
| |
| fadeProperty.Initalize(this.m_renderers[0].material); |
| |
| } |
| |
| this.m_propertyBlock = new MaterialPropertyBlock(); |
| |
| if (this.m_renderers[0].HasPropertyBlock()) |
| |
| { |
| |
| this.m_renderers[0].GetPropertyBlock(this.m_propertyBlock); |
| |
| } |
| |
| this.m_started = this.m_triggerOnAwake; |
| |
| } |
| |
| |
| |
| public void TriggerFade() |
| |
| { |
| |
| if (this.m_started) |
| |
| { |
| |
| foreach (MaterialFader.FadeProperty fadeProperty in this.m_fadeProperties) |
| |
| { |
| |
| fadeProperty.Reset(); |
| |
| } |
| |
| } |
| |
| this.m_started = true; |
| |
| } |
| |
| |
| |
| private void Update() |
| |
| { |
| |
| if (!this.m_started) |
| |
| { |
| |
| return; |
| |
| } |
| |
| foreach (MaterialFader.FadeProperty fadeProperty in this.m_fadeProperties) |
| |
| { |
| |
| fadeProperty.Update(Time.deltaTime, ref this.m_propertyBlock); |
| |
| } |
| |
| foreach (Renderer renderer in this.m_renderers) |
| |
| { |
| |
| renderer.SetPropertyBlock(this.m_propertyBlock); |
| |
| } |
| |
| } |
| |
| |
| |
| public List<Renderer> m_renderers; |
| |
| |
| |
| public bool m_triggerOnAwake = true; |
| |
| |
| |
| public List<MaterialFader.FadeProperty> m_fadeProperties; |
| |
| |
| |
| private MaterialPropertyBlock m_propertyBlock; |
| |
| |
| |
| private bool m_started; |
| |
| |
| |
| [Serializable] |
| |
| public class FadeProperty |
| |
| { |
| |
| public void Initalize(Material mat) |
| |
| { |
| |
| this.m_shaderID = Shader.PropertyToID(this.m_propertyName); |
| |
| this.m_originalMaterial = mat; |
| |
| } |
| |
| |
| |
| public void Reset() |
| |
| { |
| |
| this.m_fadeTimer = 0f; |
| |
| this.m_finished = false; |
| |
| this.m_startedFade = false; |
| |
| } |
| |
| |
| |
| private void GetMaterialValues(MaterialPropertyBlock propertyBlock) |
| |
| { |
| |
| this.m_startedFade = true; |
| |
| if (propertyBlock.HasProperty(this.m_shaderID)) |
| |
| { |
| |
| switch (this.m_propertyType) |
| |
| { |
| |
| case MaterialFader.PropertyType.Float: |
| |
| this.m_startFloatValue = propertyBlock.GetFloat(this.m_shaderID); |
| |
| return; |
| |
| case MaterialFader.PropertyType.Color: |
| |
| this.m_startColorValue = propertyBlock.GetColor(this.m_shaderID); |
| |
| return; |
| |
| case MaterialFader.PropertyType.SingleVectorChannel: |
| |
| this.m_startVectorValue = propertyBlock.GetVector(this.m_shaderID); |
| |
| this.m_startFloatValue = this.m_startVectorValue[(int)this.m_vectorChannel]; |
| |
| return; |
| |
| case MaterialFader.PropertyType.Vector3: |
| |
| this.m_startVectorValue = propertyBlock.GetVector(this.m_shaderID); |
| |
| return; |
| |
| default: |
| |
| return; |
| |
| } |
| |
| } |
| |
| else |
| |
| { |
| |
| switch (this.m_propertyType) |
| |
| { |
| |
| case MaterialFader.PropertyType.Float: |
| |
| this.m_startFloatValue = this.m_originalMaterial.GetFloat(this.m_shaderID); |
| |
| return; |
| |
| case MaterialFader.PropertyType.Color: |
| |
| this.m_startColorValue = this.m_originalMaterial.GetColor(this.m_shaderID); |
| |
| return; |
| |
| case MaterialFader.PropertyType.SingleVectorChannel: |
| |
| this.m_startVectorValue = this.m_originalMaterial.GetVector(this.m_shaderID); |
| |
| this.m_startFloatValue = this.m_startVectorValue[(int)this.m_vectorChannel]; |
| |
| return; |
| |
| case MaterialFader.PropertyType.Vector3: |
| |
| this.m_startVectorValue = this.m_originalMaterial.GetVector(this.m_shaderID); |
| |
| return; |
| |
| default: |
| |
| return; |
| |
| } |
| |
| } |
| |
| } |
| |
| |
| |
| public void Update(float delta, ref MaterialPropertyBlock propertyBlock) |
| |
| { |
| |
| this.m_fadeTimer += delta; |
| |
| if (this.m_finished || this.m_fadeTimer < this.m_delay) |
| |
| { |
| |
| return; |
| |
| } |
| |
| if (!this.m_startedFade) |
| |
| { |
| |
| this.GetMaterialValues(propertyBlock); |
| |
| } |
| |
| float num = Mathf.Clamp01((this.m_fadeTimer - this.m_delay) / this.m_fadeTime); |
| |
| num = this.m_animationCurve.Evaluate(num); |
| |
| switch (this.m_propertyType) |
| |
| { |
| |
| case MaterialFader.PropertyType.Float: |
| |
| propertyBlock.SetFloat(this.m_shaderID, Mathf.Lerp(this.m_startFloatValue, this.m_finalFloatValue, num)); |
| |
| break; |
| |
| case MaterialFader.PropertyType.Color: |
| |
| propertyBlock.SetColor(this.m_shaderID, Color.Lerp(this.m_startColorValue, this.m_finalColorValue, num)); |
| |
| break; |
| |
| case MaterialFader.PropertyType.SingleVectorChannel: |
| |
| { |
| |
| Vector4 startVectorValue = this.m_startVectorValue; |
| |
| startVectorValue[(int)this.m_vectorChannel] = Mathf.Lerp(this.m_startFloatValue, this.m_finalFloatValue, num); |
| |
| propertyBlock.SetVector(this.m_shaderID, startVectorValue); |
| |
| break; |
| |
| } |
| |
| case MaterialFader.PropertyType.Vector3: |
| |
| propertyBlock.SetVector(this.m_shaderID, Vector3.Lerp(this.m_startVectorValue, this.m_finalVectorValue, num)); |
| |
| break; |
| |
| } |
| |
| if (num == 1f) |
| |
| { |
| |
| this.m_finished = true; |
| |
| } |
| |
| } |
| |
| |
| |
| [Header("Settings")] |
| |
| public string m_propertyName; |
| |
| |
| |
| public AnimationCurve m_animationCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f); |
| |
| |
| |
| [global::Tooltip("Single Vector Channel mode will work on Colors too, if you just want to affect alpha color for example.")] |
| |
| public MaterialFader.PropertyType m_propertyType; |
| |
| |
| |
| [Min(0.1f)] |
| |
| public float m_fadeTime = 5f; |
| |
| |
| |
| [Min(0f)] |
| |
| public float m_delay; |
| |
| |
| |
| [NonSerialized] |
| |
| public int m_shaderID; |
| |
| |
| |
| [Header("Values")] |
| |
| [global::Tooltip("Used for floats and single vector channels")] |
| |
| public float m_finalFloatValue; |
| |
| |
| |
| public Color m_finalColorValue; |
| |
| |
| |
| public Vector3 m_finalVectorValue; |
| |
| |
| |
| [global::Tooltip("Only used for single vector channel mode.")] |
| |
| public MaterialFader.VectorChannel m_vectorChannel; |
| |
| |
| |
| [NonSerialized] |
| |
| public float m_startFloatValue; |
| |
| |
| |
| [NonSerialized] |
| |
| public Color m_startColorValue; |
| |
| |
| |
| [NonSerialized] |
| |
| public Vector4 m_startVectorValue; |
| |
| |
| |
| private float m_fadeTimer; |
| |
| |
| |
| private bool m_startedFade; |
| |
| |
| |
| private bool m_finished; |
| |
| |
| |
| private Material m_originalMaterial; |
| |
| } |
| |
| |
| |
| [Serializable] |
| |
| public enum PropertyType |
| |
| { |
| |
| Float, |
| |
| Color, |
| |
| SingleVectorChannel, |
| |
| Vector3 |
| |
| } |
| |
| |
| |
| [Serializable] |
| |
| public enum VectorChannel |
| |
| { |
| |
| X, |
| |
| Y, |
| |
| Z, |
| |
| W |
| |
| } |
| |
| } |
| |
| |