Material Colour Motion

Everyone wants to create flashy animations now and then and the best way to do that is by tweening the colour. FlowEnt has multiple ways how to do that, either by using a gradient or by using a colour. They can be applied on several components but today we're gonna talk about the Renderer's colour motions. There are 2 options for anyone who wants to do this. The Renderer's colour or a colour that is passed to the Material through the shader

Here are some examples:

new Tween(1f)
    .For(transform.GetComponent<MeshRenderer>().material)
    .ColorTo(Color.red, Color.green)
    .Start();
new Tween(1f)
    .For(transform.GetComponent<MeshRenderer>().material)
    .ColorTo("AlbedoColour", Color.red, Color.green)
    .Start();

And as always, here are the motions...

public class ColorMotion : AbstractColorMotion<Material>
{
    [Serializable]
    public class ValueBuilder : AbstractValueBuilder
    {
        public override ITweenMotion Build()
            => new ColorMotion(item, value);
    }

    [Serializable]
    public class FromToBuilder : AbstractFromToBuilder
    {
        public override ITweenMotion Build()
            => new ColorMotion(item, from, to);
    }

    public ColorMotion(Material item, Color value) : base(item, value)
    {
    }

    public ColorMotion(Material item, Color? from, Color to) : base(item, from, to)
    {
    }

    protected override Color GetFrom() => item.color;
    protected override void SetValue(Color value) => item.color = value;
}
}
public class ColorPropertyIdMotion<TMaterial> : AbstractColorMotion<TMaterial>
    where TMaterial : Material
{
    public ColorPropertyIdMotion(TMaterial item, int propertyId, Color value) : base(item, value)
    {
        this.propertyId = propertyId;
    }

    public ColorPropertyIdMotion(TMaterial item, int propertyId, Color? from, Color to) : base(item, from, to)
    {
        this.propertyId = propertyId;
    }

    private readonly int propertyId;
    protected override Color GetFrom() => item.GetColor(propertyId);
    protected override void SetValue(Color value) => item.SetColor(propertyId, value);
}

Comments

Popular Posts