Mesh.boneWeights 权重

var boneWeights : BoneWeight[]

Description描述

The bone weights of each vertex

每个顶点的骨骼权重。

The size of the array is either the same as vertexCount or empty.

这个数组的大小是等同于vertexCount或者为空。

Each vertex can be affected by up to 4 different bones. All 4 bone weights should sum up to 1

每一个顶点都可以最多收到4个不同骨骼的影响。所有这4个骨骼的权值应该的和为1.

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");
	}
}
function Start () {
	gameObject.AddComponent(Animation);
	gameObject.AddComponent(SkinnedMeshRenderer);
	var renderer : SkinnedMeshRenderer = GetComponent(SkinnedMeshRenderer);

	// Build basic mesh
	// 建立基本网格
	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();

	// Assign mesh to mesh filter & renderer
	// 赋予网格过滤器和渲染器。
	renderer.material = new Material (Shader.Find(" Diffuse"));

	// Assign bone weights to mesh
	// 赋予网格权值。
	// We use 2 bones. One for the lower vertices, one for the upper vertices.
	// 我们使用两个骨架。一个为低层顶点,一个为高层顶点。
	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;

	// Create Bone Transforms and Bind poses
	// 建立骨骼变换和绑定姿势
	// One bone at the bottom and one at the top
	// 一个骨骼在底部另一个在顶部
	var bones : Transform[] = new Transform[2];
	var bindPoses : Matrix4x4[] = 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 = 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;

	mesh.bindposes = bindPoses;

	// Assign bones and bind poses
	// 为骨骼赋值并绑定姿势
	renderer.bones = bones;
	renderer.sharedMesh = mesh;

	// Assign a simple waving animation to the bottom bone
	// 赋予底部骨骼一个简单的波动动画
	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) ];

	// Create the clip with the curve
	// 创建曲线的剪辑
	var clip : AnimationClip = new AnimationClip();
	clip.SetCurve("Lower", Transform, "m_LocalPosition.z", curve);

	// Add and play the clip
	// 添加并且播放剪辑
	animation.AddClip(clip, "test");
	animation.Play("test");
}
最后修改:2010年12月9日 Thursday 18:28

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。