Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lerping Between An Old and New Blendshape Value

Discussion in 'Editor & General Support' started by Phenomonaut, Oct 21, 2016.

  1. Phenomonaut

    Phenomonaut

    Joined:
    Sep 30, 2012
    Posts:
    61
    I'm trying to Lerp between two values of a single blendshape over time after a button press creates a new random value, What I think I have going on here is:
    1. when the button is pressed, the current blendshape value is stored as headsizeOld,
    2. then headsize is given a new random value
    3. in the update function, the value of the blendshape is being lerped between the old and new values (headsizeOld and headsize).
    But no lerping is going on. It just jumps to the new value (regardless of what blendSpeed is set to). Spot anything wrong? Particularly in the way I'm trying to Lerp?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class BlendShapes : MonoBehaviour {
    6.  
    7.      public float headsize = 0.0F;
    8.      public float headsizeOld = 0.0F;
    9.      public Button nextButton;
    10.      float blendSpeed = 2f;
    11.      private SkinnedMeshRenderer skinMeshRenderer;
    12.  
    13.      void Start() {
    14.          skinMeshRenderer = GetComponent<SkinnedMeshRenderer>();
    15.          Button btn = nextButton.GetComponent<Button>();
    16.          btn.onClick.AddListener(HeadSizeRandomize);
    17.      }
    18.  
    19.      void HeadSizeRandomize (){
    20.          headsizeOld = headsize;
    21.          headsize = Random.Range(0f, 100f);
    22.      }
    23.    
    24.      void Update () {
    25.          skinMeshRenderer.SetBlendShapeWeight(0, Mathf.Lerp(headsizeOld, headsize, blendSpeed * Time.deltaTime));
    26.      }
    27. }
     
  2. Banksy

    Banksy

    Joined:
    Mar 31, 2013
    Posts:
    376
    I'm also interested in blendshapes... eg. blinking & mouth shapes.
    I'm tryingto use a coroutine to makes sure the blendshapes finishes but all I get is setWeight to one position in the blend.

    eg.

    void OnCollisionEnter(Collision collision)
    {

    StartCoroutine("CharBlinks",collision.gameObject.name);
    Debug.Log ("BLINK TEST ON OFF ");
    }


    IEnumerator CharBlinks(string name)
    {
    switch (name) {
    case "blinkCube":
    dudeMeshRenderer.SetBlendShapeWeight((int)FaceGestures.BLINK, blendvalue);
    Debug.Log ("Collided with BlinkCube Trigger");
    break;

    case "mySphere":
    dudeMeshRenderer.SetBlendShapeWeight((int)FaceGestures.EXCITED, blendvalue);
    Debug.Log ("Collided with mySphere Trigger");
    break;
    }


    // Debug.Log ("yielded");
    yield return new WaitForSeconds(wait_time);
    }
     
  3. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    I could be wrong, but I think it is because even though in Blender, the values for blendshapes are between 0 and 100, while in Unity, it is between 0 and 1. Then anything above 1 is the same as 1. You are lerping between 0 and 100, getting past 1 really quickly.
     
  4. Banksy

    Banksy

    Joined:
    Mar 31, 2013
    Posts:
    376
    I'm pretty sure Unity Blendshapes are 0-100
     
  5. Banksy

    Banksy

    Joined:
    Mar 31, 2013
    Posts:
    376
    at least that how it is in the inspector when i scrub the values of each blendshape.
     
  6. Banksy

    Banksy

    Joined:
    Mar 31, 2013
    Posts:
    376
    I can actually scrub in the inspector and the blend shapes operate fine... my question is, how can i increase and decrease the value so it goes through each number from 0-100 slowly or quickly ? so quickly for an eye blink or slowly for a smile ?

    i'd love to incoporate random blinks as well.... any thoughts on this ?
     
  7. kburkhart84

    kburkhart84

    Joined:
    Apr 28, 2012
    Posts:
    910
    That's my bad. I have it backwards. In Blender it is 0 to 1, and in Unity it is 0 to 100. Knowing this, I guess I know your problem. You need to learn lerping or tweening properly. I see you have a CoRoutine function there. Assuming that this function is working right in general, the next thing I see is that you are never changing the value. You need to have a variable start at 0 and go to 100, and then back to zero, and then each frame(or whatever small interval you want), you send that value to the blendweight setting function. I see you have a variable "blendvalue" but I don't see anywhere that you change that value, and so if you never change that value, then the value you send to the weight setting function never changes.