|
| 1 | +using System.Collections.Generic; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +namespace Zigurous.Graphics |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A shader property that can be animated. |
| 8 | + /// </summary> |
| 9 | + [System.Serializable] |
| 10 | + public abstract class AnimatedShaderProperty |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// The shader property to animate. |
| 14 | + /// </summary> |
| 15 | + [Tooltip("The shader property to animate.")] |
| 16 | + public ShaderProperty property; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Creates a new animated shader property. |
| 20 | + /// </summary> |
| 21 | + /// <param name="property">The shader property to animate.</param> |
| 22 | + public AnimatedShaderProperty(string property) |
| 23 | + { |
| 24 | + this.property = property; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Animates the shader property. |
| 29 | + /// </summary> |
| 30 | + /// <param name="material">The material to animate.</param> |
| 31 | + /// <param name="time">The time of the animation to evaluate.</param> |
| 32 | + public abstract void Animate(Material material, float time); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// A shader float property that can be animated. |
| 38 | + /// </summary> |
| 39 | + [System.Serializable] |
| 40 | + public class AnimatedShaderFloatProperty : AnimatedShaderProperty |
| 41 | + { |
| 42 | + /// <summary> |
| 43 | + /// The value over time of the shader property. |
| 44 | + /// </summary> |
| 45 | + [Tooltip("The value over time of the shader property.")] |
| 46 | + public AnimationCurve valueOverTime; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Creates a new animated shader float property. |
| 50 | + /// </summary> |
| 51 | + /// <param name="valueOverTime">The value over time of the shader property.</param> |
| 52 | + /// <param name="property">The shader property to animate.</param> |
| 53 | + public AnimatedShaderFloatProperty(AnimationCurve valueOverTime, string property) : base(property) |
| 54 | + { |
| 55 | + this.valueOverTime = valueOverTime; |
| 56 | + } |
| 57 | + |
| 58 | + /// <inheritdoc /> |
| 59 | + public override void Animate(Material material, float time) |
| 60 | + { |
| 61 | + material.SetFloat(this.property.id, this.valueOverTime.Evaluate(time)); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// A shader int property that can be animated. |
| 68 | + /// </summary> |
| 69 | + [System.Serializable] |
| 70 | + public class AnimatedShaderIntProperty : AnimatedShaderProperty |
| 71 | + { |
| 72 | + /// <summary> |
| 73 | + /// The value over time of the shader property. |
| 74 | + /// </summary> |
| 75 | + [Tooltip("The value over time of the shader property.")] |
| 76 | + public AnimationCurve valueOverTime; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Creates a new animated shader int property. |
| 80 | + /// </summary> |
| 81 | + /// <param name="valueOverTime">The value over time of the shader property.</param> |
| 82 | + /// <param name="property">The shader property to animate.</param> |
| 83 | + public AnimatedShaderIntProperty(AnimationCurve valueOverTime, string property) : base(property) |
| 84 | + { |
| 85 | + this.valueOverTime = valueOverTime; |
| 86 | + } |
| 87 | + |
| 88 | + /// <inheritdoc /> |
| 89 | + public override void Animate(Material material, float time) |
| 90 | + { |
| 91 | + material.SetInt(this.property.id, (int)this.valueOverTime.Evaluate(time)); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// A shader color property that can be animated. |
| 98 | + /// </summary> |
| 99 | + [System.Serializable] |
| 100 | + public class AnimatedShaderColorProperty : AnimatedShaderProperty |
| 101 | + { |
| 102 | + /// <summary> |
| 103 | + /// The color over time of the shader property. |
| 104 | + /// </summary> |
| 105 | + public Gradient colorOverTime; |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Creates a new animated shader color property. |
| 109 | + /// </summary> |
| 110 | + /// <param name="colorOverTime">The color over time of the shader property.</param> |
| 111 | + /// <param name="property">The shader property to animate.</param> |
| 112 | + public AnimatedShaderColorProperty(Gradient colorOverTime, string property) : base(property) |
| 113 | + { |
| 114 | + this.colorOverTime = colorOverTime; |
| 115 | + } |
| 116 | + |
| 117 | + /// <inheritdoc /> |
| 118 | + public override void Animate(Material material, float time) |
| 119 | + { |
| 120 | + material.SetColor(this.property.id, this.colorOverTime.Evaluate(time)); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Extension methods for <see cref="AnimatedShaderProperty"/>. |
| 127 | + /// </summary> |
| 128 | + public static class AnimatedShaderPropertyExtensions |
| 129 | + { |
| 130 | + /// <summary> |
| 131 | + /// Animates an array of shader properties. |
| 132 | + /// </summary> |
| 133 | + /// <param name="properties">The shader properties to animate.</param> |
| 134 | + /// <param name="material">The material to animate.</param> |
| 135 | + /// <param name="time">The time of the animation to evaluate.</param> |
| 136 | + public static void Animate(this AnimatedShaderProperty[] properties, Material material, float time) |
| 137 | + { |
| 138 | + for (int i = 0; i < properties.Length; i++) { |
| 139 | + properties[i].Animate(material, time); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Animates an array of shader properties. |
| 145 | + /// </summary> |
| 146 | + /// <param name="properties">The shader properties to animate.</param> |
| 147 | + /// <param name="material">The material to animate.</param> |
| 148 | + /// <param name="time">The time of the animation to evaluate.</param> |
| 149 | + public static void Animate(this List<AnimatedShaderProperty> properties, Material material, float time) |
| 150 | + { |
| 151 | + foreach (AnimatedShaderProperty property in properties) { |
| 152 | + property.Animate(material, time); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments