版本:Unity 6 (6000.0)
语言英语
  • C#

Mesh.boneWeights

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户建议的每一项更改,并在合适的情况下进行更新。

关闭

提交失败

由于某种原因,无法提交您建议的更改。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换至手册
public BoneWeight[] boneWeights;

说明

Mesh 中每个顶点的 BoneWeight,它表示每个顶点的 4 块骨头。

此数组的大小要么是 Mesh.vertexCount,要么是零。此数组按顶点索引排序。

请注意,此属性使用 BoneWeight 结构,它表示每个顶点的 4 个具体骨骼权重。新 BoneWeight1 结构描述一个单独的骨骼权重,它可以与相关的 Mesh.GetAllBoneWeightsMesh.SetBoneWeightsMesh.GetBonesPerVertex API 一起使用来描述每个顶点多达 255 个骨骼权重。最好使用 BoneWeight1 及其相关的 API;它们提供更多灵活性和 Unity 无需执行多余的转换操作,从而带来一些小的性能优势。

其他资源:Mesh.GetAllBoneWeightsMesh.SetBoneWeightsMesh.GetBonesPerVertexMesh.GetBoneWeightsModelImporter.maxBonesPerVertexQualitySettings.skinWeightsSkinnedMeshRenderer.quality

using UnityEngine;

public class SkinnedMeshExample : 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[] { 0, 3, 1, 0, 2, 3 }; 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. 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;

// A BoneWeights array (weights) was just created and the boneIndex and weight assigned. // The weights array will now be assigned to the boneWeights array in the Mesh. 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;

// assign the bindPoses array to the bindposes array which is part of 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.SetCurve("Lower", typeof(Transform), "m_LocalPosition.z", curve); clip.legacy = true; clip.wrapMode = WrapMode.Loop;

// Add and play the clip anim.AddClip(clip, "test"); anim.Play("test"); } }