版本: Unity 6 (6000.0)
语言英语
  • C#

Mesh.GetAllBoneWeights

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交的内容,但我们会阅读用户提出的每个建议并根据情况进行更新。

关闭

提交失败

由于某种原因,您的建议修改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册

声明

public NativeArray<BoneWeight1> GetAllBoneWeights();

返回值

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)); } } }