NativeArray<byte> 返回每个顶点的非零骨骼权重数量。
每个顶点的非零骨骼权重数量。
返回数组的大小为 Mesh.vertexCount 或 0。此数组按顶点索引顺序进行排序。
与 Mesh.GetAllBoneWeights 结合使用来获取网格中顶点的骨骼权重。
You don't need to dispose the returned NativeArray.
另请参阅:Mesh.GetAllBoneWeights、Mesh.SetBoneWeights、ModelImporter.maxBonesPerVertex、QualitySettings.skinWeights、SkinnedMeshRenderer.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)); } } }