Version: 2020.1

Mesh.GetAllBoneWeights

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

返回

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

描述

获取网格的骨骼权重。

在数组的每个 BoneWeight1 结构中,权重采用降序,加起来等于 1。

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

另请参阅:Mesh.SetBoneWeightsMesh.GetBonesPerVertexMesh.boneWeightsMesh.GetBoneWeights ModelImporter.maxBonesPerVertexQualitySettings.skinWeightsSkinnedMeshRenderer.quality

using UnityEngine;
using Unity.Collections;

public class SkinnedMeshExample : 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();

// Get all the bone weights, in vertex index order var boneWeights = mesh.GetAllBoneWeights();

// Keep track of where we are in the array of bones, as we iterate over the vertices var bonesIndex = 0;

// Iterate over the vertices for (var vertIndex = 0; vertIndex < mesh.vertexCount; vertIndex++) { var numberOfBonesForThisVertex = bonesPerVertex[vertIndex]; Debug.Log("This vertex has " + numberOfBonesForThisVertex + " bones");

// For each vertex, iterate over the array of bones the correct number of times for (var i = 0; i < numberOfBonesForThisVertex; i++) { var currentBoneWeight = boneWeights[bonesIndex]; Debug.Log("Bone weight " + currentBoneWeight.boneIndex + " == " + currentBoneWeight.weight); bonesIndex++; } } } }