NativeArray<BoneWeight1> 返回网格的所有非零骨骼权重,按顶点索引顺序排列。
获取网格的骨骼权重。
使用 Mesh.GetBonesPerVertex 获取每个顶点的非零权重数量,然后使用它来迭代这些数据。
对于每个顶点,BoneWeight1 结构体按 BoneWeight1.weight 降序排列。
其他资源: Mesh.SetBoneWeights, Mesh.GetBonesPerVertex, Mesh.boneWeights, Mesh.GetBoneWeights 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)); } } }