User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Decal Transform Animation

In some cases it's inefficient to create a new material animation for very basic transformations like rotation and scale. 

The Easy Decal framework allows to hook custom components to prefabs and process them after the first projection cycle took place.

It's important to always start any costum logic in the Start() method and not in the Awake(). When the decal prefab gets instantiated using the API, it's also necessary to release the geometry before any processing takes place. Since version 1.6.1 this can easily be achieved using the LateUnbake() method. Please keep in mind that every decal instance needs to be baked again after processing.

private void Start ()      
{         
    decal = GetComponent<EasyDecal>();         

    //Unbake decal in next frame         
    decal.LateUnbake();
}

The example component below demonstrates how a curve-controlled scale effect can be applied to decal.

using UnityEngine;
using System.Collections;
using ch.sycoforge.Decal;

/// <summary>
/// Simple component showing how to implement a geometry post effect with Easy Decal.
/// </summary>
[RequireComponent(typeof(EasyDecal))]
public class DecalScaleAnimation : MonoBehaviour 
{
    //-----------------------
    // Exposed Fields
    //-----------------------

    /// <summary>
    /// Final value of the animation.
    /// </summary>
    [Range(0.0f, 3.0f)]
    public float FinalScale = 2;

    /// <summary>
    /// Overall animation time.
    /// </summary>
    [Range(0.1f, 10.0f)]
    public float ScaleTime = 2;

    /// <summary>
    /// Animation tween curve. Should be clamped to [0, 0] -> [1, 1].
    /// </summary>
    public AnimationCurve Curve = new AnimationCurve(new Keyframe(0,0), new Keyframe(1,1));

    //-----------------------
    // Fields
    //-----------------------
    private float currentScaleFactor = 1;
    private Vector3 startLocalScale;
    private EasyDecal decal;

    //-----------------------
    // Methods
    //-----------------------
    private void Start () 
    {
        decal = GetComponent<EasyDecal>();

        //Unbake decal in next frame
        decal.LateUnbake();

        // Start scale routine
        StartAnimation();
    }

    /// <summary>
    /// Starts the animation.
    /// </summary>
    public void StartAnimation()
    {
        StartCoroutine(ScaleRoutine());
    }

    private IEnumerator ScaleRoutine()
    {
        // Initialize 
        float normalizedProgress = 0.0f;
        float elapsedTime = 0.0f;
        float sampledValue = 0.0f;

        // Reset scale factor
        currentScaleFactor = 1.0f;

        // Reset current local scale vector
        startLocalScale = this.transform.localScale;

        while (elapsedTime <= ScaleTime)
        {
            normalizedProgress = elapsedTime / ScaleTime;

            sampledValue = Curve.Evaluate(normalizedProgress);

            // Inegrate over time
            currentScaleFactor = sampledValue * FinalScale;

            // Sum overall animation time
            elapsedTime += Time.deltaTime;

            this.transform.localScale = startLocalScale * currentScaleFactor;

            yield return null;
        }

        // Bake after scale animation has been finished
        decal.LateBake();
    }
}

The component can simply be plugged to the component stack of the decal prefab.