1
\$\begingroup\$

I am trying to learn how to use AnimationCurve to change a float value over time.

However i am not really sure how to do it

Say i have the following class:

    public class SpiritAway : MonoBehaviour
{
    public List<SkinnedMeshRenderer> MeshList;
    public List<Material> DissolveList;
    public float percentage = 0.0f;
    public bool IsDissolveing;
    public AnimationCurve Curve;

    private void Start()
    {
        percentage = 0.0f;
        for (int i = 0; i < MeshList.Count; i++)
        {
            for (int j = 0; j < MeshList[i].materials.Length; j++)
            {
                if (MeshList[i].materials[j].shader.name == "Dissolve")
                {
                    DissolveList.Add(MeshList[i].materials[j]);
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (IsDissolveing)
        {
            if (percentage < 1)
            {
                    percentage += 0.01f;
                    for (int i = 0; i < DissolveList.Count; i++)
                    {
                        DissolveList[i].SetFloat("_alphaClipDissolve", percentage);
                    }
            }
        }
    }

Now i wish to incrase the percentage value with the percentage of the animation curve to get a more smooth and controlled transition of the value.

Could anyone explain how this is possible?

\$\endgroup\$
  • \$\begingroup\$ I don't see in your code where you've tried to use your curve so far, eg. Curve.Evaluate(percentage) — am I missing part of this question? \$\endgroup\$ – DMGregory May 7 '19 at 11:56
0
\$\begingroup\$

It looks to me like you can replace this with:

public float speed = 1f/30f;
public Renderer[] renderers;
public AnimationCurve curve;

MaterialPropertyBlock _block;

struct MaterialSlot {
    public readonly Renderer renderer;
    public readonly int index;

    public MaterialSlot(Renderer renderer, int index) {
        this.renderer = renderer;
        this.index = index;
    }
}

private IEnumerator Start()
{
    // Define a material property block for modifying alpha
    // without copying materials (lighter on allocations/garbage).
    var properties = new MaterialPropertyBlock();

    // Cache an ID for our parameter, to avoid lots of string lookups.
    int id = Shader.PropertyToIndex("_alphaClipDissolve");

    float perecentage = 0f;

    // Read our alpha value from this position on the curve
    // and apply it to our property block.
    properties.SetFloat(id, curve.Evaluate(percentage));

    // Find all materials that need this alpha adjusted, and cache them.
    var slots = new List<MaterialSlot>();
    foreach(var renderer in renderers) {

        // Look at the renderer's materials without copying them.
        var materials = renderer.sharedMaterials;

        for(int i = 0; i < materials.Length; i++) {
            if(materials[i].shader.name == "Dissolve") {
                // Initialize the alpha value for this material.
                renderer.SetPropertyBlock(properties, i);

                // Cache a reference to its slot so we can iterate it later.
                var slot = new MaterialSlot(renderer, i);
                slots.Add(slot);
            }
        }
    }

    // Keep updating the alpha on subsequent frames until we're done.
    while(percentage < 1f) {
        // Wait a frame, then resume.
        yield return null;

        // Advance the animation at a fixed rate over time.
        percentage += Time.deltaTime * speed;

        // Read our alpha value from this position on the curve
        // and apply it to our property block.
        properties.SetFloat(id, curve.Evaluate(percentage));

        // Apply this modified property block to every "Dissolve" material slot.
        foreach(var slot in slots) {
            slot.renderer.SetPropertyBlock(properties, slot.index);
        }
    }

    // percent >= 1f, we're done! End this coroutine.
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.