返回介绍

Mesh.boneWeights 骨骼权重

发布于 2019-12-18 15:38:01 字数 9011 浏览 1295 评论 0 收藏 0

JavaScript => public var boneWeights: BoneWeight[];
C# => public BoneWeight[] boneWeights;

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.

JavaScript:

function Start () {
	gameObject.AddComponent.<Animation>();
	gameObject.AddComponent.<SkinnedMeshRenderer>();
	var rend: SkinnedMeshRenderer = GetComponent.<SkinnedMeshRenderer>();
	var anim: Animation = GetComponent.<Animation>();
 
	// 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
	rend.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
	rend.bones = bones;
	rend.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
	anim.AddClip(clip, "test");
	anim.Play("test");
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Start() {
        gameObject.AddComponent<Animation>();
        gameObject.AddComponent<SkinnedMeshRenderer>();
        SkinnedMeshRenderer rend = GetComponent<SkinnedMeshRenderer>();
        Animation anim = GetComponent<Animation>();
        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();
        rend.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;
        rend.bones = bones;
        rend.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);
        anim.AddClip(clip, "test");
        anim.Play("test");
    }
}

Mesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文