NativeArray<byte> 返回每个顶点的不为零的骨骼权重的数量。
每个顶点的不为零的骨骼权重的数量。
返回的数组大小为 网格.vertexCount 或 0。该数组按顶点索引顺序排序。
使用 网格.GetAllBoneWeights 配合使用以获取网格中的顶点的骨骼权重。
您无需处理返回的原生数组。但是,原生数组指向可能被取消配置或重新配置的内存,因此您应该在每一帧中调用 GetBonesPerVertex
以获取正确的数据,或每帧检查原生数组是否仍然有效。
其他资源:网格.GetAllBoneWeights,网格.SetBoneWeights,模型导入器.maxBonesPerVertex,质量设置.skinWeights,蒙皮网格渲染器.quality。
using UnityEngine;
public class TestSkinnedMesh : MonoBehaviour { void Start() { // Get a reference to the mesh var skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>(); var mesh = skinnedMeshRenderer.sharedMesh;
// Get the number of bone weights per vertex var bonesPerVertex = mesh.GetBonesPerVertex(); if (bonesPerVertex.Length == 0) { return; }
// Get all the bone weights, in vertex index order var boneWeights = mesh.GetAllBoneWeights();
// Keep track of where we are in the array of BoneWeights, as we iterate over the vertices var boneWeightIndex = 0;
// Iterate over the vertices for (var vertIndex = 0; vertIndex < mesh.vertexCount; vertIndex++) { var totalWeight = 0f; var numberOfBonesForThisVertex = bonesPerVertex[vertIndex]; Debug.Log("This vertex has " + numberOfBonesForThisVertex + " bone influences");
// For each vertex, iterate over its BoneWeights for (var i = 0; i < numberOfBonesForThisVertex; i++) { var currentBoneWeight = boneWeights[boneWeightIndex]; totalWeight += currentBoneWeight.weight; if (i > 0) { Debug.Assert(boneWeights[boneWeightIndex - 1].weight >= currentBoneWeight.weight); } boneWeightIndex++; } Debug.Assert(Mathf.Approximately(1f, totalWeight)); } } }