Version: 2023.1

Mesh.GetAllBoneWeights

切换到手册
public NativeArray<BoneWeight1> GetAllBoneWeights ();

返回

NativeArray<BoneWeight1> 返回网格的所有非零骨骼权重(按顶点索引顺序)。

描述

获取网格的骨骼权重。

使用 Mesh.GetBonesPerVertex 可获取每个顶点的非零权重数量,然后使用该数量可遍历此数据。

For each vertex, the BoneWeight1 structs are sorted by BoneWeight1.weight, in descending order.

另请参阅:Mesh.SetBoneWeightsMesh.GetBonesPerVertexMesh.boneWeightsMesh.GetBoneWeights ModelImporter.maxBonesPerVertexQualitySettings.skinWeightsSkinnedMeshRenderer.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)); } } }