本页内容

顶点蒙皮

简介

Qt Quick 3D 支持对网格几何体的骨骼动画进行顶点蒙皮。

请参阅“简单蒙皮示例”以了解骨骼动画的实际演示。

在大多数情况下,应用程序开发人员不会手动使用皮肤绑定 API。通常的工作流程是使用外部内容创建工具来定义骨骼和皮肤(这有时也被称为“绑定”),然后使用Balsam 资源导入工具将资源转换为Qt Quick 3D 的原生格式。

定义骨骼

骨骼动画的基础是Skeleton 。这是对模型运动方式的一种抽象表示,其灵感来源于脊椎动物物理骨骼的工作原理。骨骼的“骨骼”由一组Joint 节点的层次结构表示。当然,这些并不一定代表实际的骨骼。

Skeleton {
    id: qmlskeleton
    Joint {
        id: joint0
        index: 0
        skeletonRoot: qmlskeleton
        Joint {
            id: joint1
            index: 1
            skeletonRoot: qmlskeleton
        }
        Joint {
            id: joint2
            index: 2
            skeletonRoot: qmlskeleton
        }

    }
}

将骨骼连接到模型

要将骨骼应用于模型,需设置模型的skeleton 属性:

Model {
    skeleton: qmlskeleton
    ...

为了使骨骼生效,模型的geometry 属性需要包含蒙皮信息。这可以通过在顶点缓冲区中包含具有JointSemantic 和WeightSemantic 属性的vertex attributes 节点来实现。

JointSemantic 属性决定了骨骼中的哪些关节可以影响给定的顶点。这使用Joint.index 中指定的索引值。由于该属性包含4个索引,因此最多有4个关节可以影响一个顶点。

WeightSemantic 属性描述了这些关节的影响强度。它包含四个浮点数值,每个数值决定赋予JointSemantic 属性中对应位置索引所对应关节的权重。

例如,针对上图所示的骨骼,如果一个顶点具有以下属性:

JointSemantic 属性WeightSemantic 属性
QVector4D(2, 0, 0, 0)QVector4D(1.0, 0.0, 0.0, 0.0)

该顶点将 100% 受关节 2 影响,其移动幅度将与该关节完全一致。由于对应的权重为0.0 ,因此“JointSemantic ”属性中的最后三个索引将被忽略。

再举一个例子,假设属性如下:

JointSemantic 属性WeightSemantic 属性
QVector4D(1, 2, 0, 0)QVector4D(0.5, 0.25, 0.0, 0.0)

该顶点将移动joint1位移的50%加上joint2位移的25%。

此外,由于骨骼是一种抽象表示,模型需要为各关节指定几何信息。出于性能考虑,这并非通过直接指定信息来实现。相反,Model.inverseBindPoses 包含将每个关节移动到其初始位置所需的变换矩阵的逆矩阵。

骨骼的动画制作

对骨骼中的某个关节进行变换将带动与该关节相连的所有顶点移动。由于 Joint 类继承自Node ,因此只需使用标准的QML animations 即可对骨骼进行动画处理。

NumberAnimation {
    target: joint1
    property: "eulerRotation.z"
    duration: 5000
    from: -90
    to: 90
    running: true
}

虽然可以通过嵌套使用SequentialAnimation 、ParallelAnimation 和NumberAnimation 来创建复杂的动画,但对于带皮肤绑定的模型,通常使用时间线动画更为便捷。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.