Each vertex can be affected by up to 4 different bones.
All 4 bone weights should sum up to 1.
function Start () {
gameObject.AddComponent(
Animation);
gameObject.AddComponent(
SkinnedMeshRenderer);
var renderer :
SkinnedMeshRenderer = GetComponent(
SkinnedMeshRenderer);
var mesh : Mesh =
new Mesh ();
mesh.vertices = [
Vector3(-1, 0, 0),
Vector3(1, 0, 0),
Vector3(-1, 5, 0),
Vector3(1, 5, 0)];
mesh.uv = [
Vector2(0, 0),
Vector2(1, 0),
Vector2(0, 1),
Vector2(1, 1)];
mesh.triangles = [0, 1, 2, 1, 3, 2];
mesh.RecalculateNormals();
renderer.material =
new Material (
Shader.Find(
" Diffuse"));
var weights :
BoneWeight[] =
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;
var bones :
Transform[] =
new Transform[2];
var bindPoses :
Matrix4x4[] =
new Matrix4x4[2];
bones[0] =
new GameObject (
"Lower").transform;
bones[0].parent = transform;
bones[0].localRotation =
Quaternion.identity;
bones[0].localPosition =
Vector3.zero;
bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
bones[1] =
new GameObject (
"Upper").transform;
bones[1].parent = transform;
bones[1].localRotation =
Quaternion.identity;
bones[1].localPosition =
Vector3 (0, 5, 0);
bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
mesh.bindposes = bindPoses;
renderer.bones = bones;
renderer.sharedMesh = mesh;
var curve :
AnimationCurve =
new AnimationCurve();
curve.keys = [
new Keyframe (0, 0, 0, 0),
new Keyframe (1, 3, 0, 0),
new Keyframe (2, 0.0, 0, 0) ];
var clip :
AnimationClip =
new AnimationClip();
clip.SetCurve(
"Lower",
Transform,
"m_LocalPosition.z", curve);
animation.AddClip(clip,
"test");
animation.Play(
"test");
}
using UnityEngine;
using System.Collections;
public class example :
MonoBehaviour {
void Start() {
gameObject.AddComponent<
Animation>();
gameObject.AddComponent<
SkinnedMeshRenderer>();
SkinnedMeshRenderer renderer = GetComponent<
SkinnedMeshRenderer>();
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[] {0, 1, 2, 1, 3, 2};
mesh.RecalculateNormals();
renderer.material =
new Material(
Shader.Find(
" Diffuse"));
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;
Transform[] bones =
new Transform[2];
Matrix4x4[] bindPoses =
new Matrix4x4[2];
bones[0] =
new GameObject(
"Lower").transform;
bones[0].parent = transform;
bones[0].localRotation =
Quaternion.identity;
bones[0].localPosition =
Vector3.zero;
bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
bones[1] =
new GameObject(
"Upper").transform;
bones[1].parent = transform;
bones[1].localRotation =
Quaternion.identity;
bones[1].localPosition =
new Vector3(0, 5, 0);
bindPoses[1] = bones[1].worldToLocalMatrix * transform.localToWorldMatrix;
mesh.bindposes = bindPoses;
renderer.bones = bones;
renderer.sharedMesh = mesh;
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)};
AnimationClip clip =
new AnimationClip();
clip.SetCurve(
"Lower", typeof(
Transform),
"m_LocalPosition.z", curve);
animation.AddClip(clip,
"test");
animation.Play(
"test");
}
}
import UnityEngine
import System.Collections
class example(
MonoBehaviour):
def
Start():
gameObject.AddComponent[of
Animation]()
gameObject.AddComponent[of
SkinnedMeshRenderer]()
renderer as
SkinnedMeshRenderer = GetComponent[of
SkinnedMeshRenderer]()
mesh as Mesh = Mesh()
mesh.vertices = (
Vector3(-1, 0, 0),
Vector3(1, 0, 0),
Vector3(-1, 5, 0),
Vector3(1, 5, 0))
mesh.uv = (
Vector2(0, 0),
Vector2(1, 0),
Vector2(0, 1),
Vector2(1, 1))
mesh.triangles = (0, 1, 2, 1, 3, 2)
mesh.RecalculateNormals()
renderer.material =
Material(
Shader.Find(' Diffuse'))
weights as (
BoneWeight) = array[of
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
bones as (
Transform) = array[of
Transform](2)
bindPoses as (
Matrix4x4) = array[of
Matrix4x4](2)
bones[0] =
GameObject('Lower').transform
bones[0].parent = transform
bones[0].localRotation =
Quaternion.identity bones[0].localPosition =
Vector3.zero bindPoses[0] = (bones[0].worldToLocalMatrix * transform.localToWorldMatrix)
bones[1] =
GameObject('Upper').transform
bones[1].parent = transform
bones[1].localRotation =
Quaternion.identity bones[1].localPosition =
Vector3(0, 5, 0)
bindPoses[1] = (bones[1].worldToLocalMatrix * transform.localToWorldMatrix)
mesh.bindposes = bindPoses
renderer.bones = bones
renderer.sharedMesh = mesh
curve as
AnimationCurve =
AnimationCurve()
curve.keys = (
Keyframe(0, 0, 0, 0),
Keyframe(1, 3, 0, 0),
Keyframe(2, 0.0F, 0, 0))
clip as
AnimationClip =
AnimationClip()
clip.SetCurve('Lower',
Transform, 'm_LocalPosition.z', curve)
animation.AddClip(clip, 'test')
animation.Play('test')