3D モデリングアプリケーションでブレンドシェイプを設定したら、以下を行います。
0 はブレンドシェイプが影響しないことを意味します。100 はブレンドシェイプが完全に影響することを意味します。ブレンドアニメーションを作成するには:
アニメーションをプレビューするには、エディターウィンドウまたは Animation ウィンドウで Play をクリックします。
GetBlendShapeWeight や SetBlendShapeWeight などの関数を使用して、スクリプトを通してブレンドウェイトを設定することも可能です。
メッシュが持つブレンドシェイプの数を確認するには、blendShapeCount 変数を使用します。
このコード例は、3 つ以上のブレンドシェイプを持つゲームオブジェクトにアタッチされたときに、デフォルトのシェイプを経時的に他の 2 つのブレンドシェイプにブレンドする方法を示しています。
using UnityEngine;
using System.Collections;
public class BlendShapeExample : MonoBehaviour
{
int blendShapeCount;
SkinnedMeshRenderer skinnedMeshRenderer;
Mesh skinnedMesh;
float blendOne = 0f;
float blendTwo = 0f;
float blendSpeed = 1f;
bool blendOneFinished = false;
void Awake ()
{
skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
skinnedMesh = GetComponent<SkinnedMeshRenderer> ().sharedMesh;
}
void Start ()
{
blendShapeCount = skinnedMesh.blendShapeCount;
}
void Update ()
{
if (blendShapeCount > 2) {
if (blendOne < 100f) {
skinnedMeshRenderer.SetBlendShapeWeight (0, blendOne);
blendOne += blendSpeed;
} else {
blendOneFinished = true;
}
if (blendOneFinished == true && blendTwo < 100f) {
skinnedMeshRenderer.SetBlendShapeWeight (1, blendTwo);
blendTwo += blendSpeed;
}
}
}
}