バインドポーズ。各インデックスのバインドポーズは同じインデックスのボーンを参照します。
ボーンがバインドポーズのとき、バインドポーズはボーンの変換行列の逆行列です。
Note: See the Mesh page where example 2 shows vertices being copied, updated and and re-assigned to the Mesh. The example on this page also updates bindposes, SkinnedMeshRenderer.bones and SkinnedMeshRenderer.sharedMesh in the same way.
using UnityEngine; using System.Collections;
// this example creates a quad mesh from scratch, creates bones // and assigns them, and animates the bones motion to make the // quad animate based on a simple animation curve. public class BindPoseExample : MonoBehaviour { void Start() { gameObject.AddComponent<Animation>(); gameObject.AddComponent<SkinnedMeshRenderer>(); SkinnedMeshRenderer rend = GetComponent<SkinnedMeshRenderer>(); Animation anim = GetComponent<Animation>();
// Build basic mesh Mesh mesh = new Mesh(); mesh.vertices = new Vector3[] { new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(-1, 5, 0), new Vector3(1, 5, 0) }; mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) }; mesh.triangles = new int[] { 2, 3, 1, 2, 1, 0 }; mesh.RecalculateNormals(); rend.material = new Material(Shader.Find("Diffuse"));
// assign bone weights to mesh BoneWeight[] weights = new BoneWeight[4]; weights[0].boneIndex0 = 0; weights[0].weight0 = 1; weights[1].boneIndex0 = 0; weights[1].weight0 = 1; weights[2].boneIndex0 = 1; weights[2].weight0 = 1; weights[3].boneIndex0 = 1; weights[3].weight0 = 1; mesh.boneWeights = weights;
// Create Bone Transforms and Bind poses // One bone at the bottom and one at the top
Transform[] bones = new Transform[2]; Matrix4x4[] bindPoses = new Matrix4x4[2]; bones[0] = new GameObject("Lower").transform; bones[0].parent = transform; // Set the position relative to the parent bones[0].localRotation = Quaternion.identity; bones[0].localPosition = Vector3.zero; // The bind pose is bone's inverse transformation matrix // In this case the matrix we also make this matrix relative to the root // So that we can move the root game object around freely bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
bones[1] = new GameObject("Upper").transform; bones[1].parent = transform; // Set the position relative to the parent bones[1].localRotation = Quaternion.identity; bones[1].localPosition = new Vector3(0, 5, 0); // The bind pose is bone's inverse transformation matrix // In this case the matrix we also make this matrix relative to the root // So that we can move the root game object around freely bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
// bindPoses was created earlier and was updated with the required matrix. // The bindPoses array will now be assigned to the bindposes in the Mesh. mesh.bindposes = bindPoses;
// Assign bones and bind poses rend.bones = bones; rend.sharedMesh = mesh;
// Assign a simple waving animation to the bottom bone AnimationCurve curve = new AnimationCurve(); curve.keys = new Keyframe[] { new Keyframe(0, 0, 0, 0), new Keyframe(1, 3, 0, 0), new Keyframe(2, 0.0F, 0, 0) };
// Create the clip with the curve AnimationClip clip = new AnimationClip(); clip.legacy = true; clip.SetCurve("Lower", typeof(Transform), "m_LocalPosition.z", curve);
// Add and play the clip clip.wrapMode = WrapMode.Loop; anim.AddClip(clip, "test"); anim.Play("test"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.