How to do it...

To use Blend Shapes to morph objects, follow these steps:

  1. Import the model with at least one Blend Shape to Unity. You may need to go to the Import SettingsModel tab, and choose Import BlendShapes.
  2. Place the model in the Scene.
  3. Create a new script and call it ChangeShape.cs. This script is similar to the one from the previous recipe. In the Update() function, we change the weight of the first Blend Shape when player presses the space bar. Again, we use a helper variable weightTarget to set the new weight to 0 or 100, depending on its current value. Blend Shapes have weights from 0 to 100 instead of 1. Finally, we interpolate the weight value in time to make the transition smoother. We use the SetBlendShapeWeight() function on the skinnedRenderer object. This variable is set in the Start() function with the GetComponent<SkinnedMeshRenderer>() function:
        if (Input.GetKeyDown(KeyCode.Space)) 
        { 
            if (weightTarget < 50f) 
            { 
                weightTarget = 100f; 
            } 
            else if (weightTarget > 50f) 
            { 
                weightTarget = 0f; 
            } 
        } 
        weight = Mathf.Lerp(weight, weightTarget, Time.deltaTime * 
        blendShapeLerpSpeed); 
        skinnedRenderer.SetBlendShapeWeight(0, weight); 
  1. Attach the script to the model on the scene.
  2. Play the game and press the space bar to see the model morph.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.27.131