QQuick3DGeometry Class
用于定义自定义几何体的基类。更多...
| Header: | #include <QQuick3DGeometry> |
| In QML: | Geometry |
| Inherits: | QQuick3DObject |
公共函数
| void | addAttribute(QQuick3DGeometry::Attribute::Semantic semantic, int offset, QQuick3DGeometry::Attribute::ComponentType componentType) |
| void | addAttribute(const QQuick3DGeometry::Attribute &attribute) |
| void | addSubset(int offset, int count, const QVector3D &boundsMin, const QVector3D &boundsMax, const QString &name = {}) |
(since 6.6) void | addTargetAttribute(quint32 targetId, QQuick3DGeometry::Attribute::Semantic semantic, int offset, int stride = 0) |
(since 6.6) void | addTargetAttribute(const QQuick3DGeometry::TargetAttribute &attribute) |
| QQuick3DGeometry::Attribute | attribute(int index) const |
| int | attributeCount() const |
| QVector3D | boundsMax() const |
| QVector3D | boundsMin() const |
| void | clear() |
| QByteArray | indexData() const |
| QQuick3DGeometry::PrimitiveType | primitiveType() const |
| void | setBounds(const QVector3D &min, const QVector3D &max) |
| void | setIndexData(const QByteArray &data) |
| void | setIndexData(int offset, const QByteArray &data) |
| void | setPrimitiveType(QQuick3DGeometry::PrimitiveType type) |
| void | setStride(int stride) |
(since 6.6) void | setTargetData(const QByteArray &data) |
(since 6.6) void | setTargetData(int offset, const QByteArray &data) |
| void | setVertexData(const QByteArray &data) |
| void | setVertexData(int offset, const QByteArray &data) |
| int | stride() const |
| QVector3D | subsetBoundsMax(int subset) const |
| QVector3D | subsetBoundsMin(int subset) const |
| int | subsetCount() const |
| int | subsetCount(int subset) const |
| QString | subsetName(int subset) const |
| int | subsetOffset(int subset) const |
(since 6.6) QQuick3DGeometry::TargetAttribute | targetAttribute(int index) const |
(since 6.6) int | targetAttributeCount() const |
(since 6.6) QByteArray | targetData() const |
| QByteArray | vertexData() const |
详细说明
QQuick3DGeometry 可用于在Qt Quick 3D 场景中为模型指定自定义几何体。
虽然并非严格必要,但通常的做法是继承自该类。随后,通过将其注册到类型系统中,将子类暴露给 QML。此时,Model 的geometry 属性即可设置为引用已注册类型的实例。
此类的高级结构通常类似于以下示例:
class CustomGeometry : public QQuick3DGeometry
{
public:
CustomGeometry() { rebuildGeometry(); }
void setSomething() {
// Change relevant internal data.
// ...
// Then rebuild the vertex and index data and pass it to QQuick3DGeometry.
rebuildGeometry();
// Finally, trigger an update. This is relevant in case nothing else
// is changing in the scene; this way we make sure a new frame will
// be rendered.
update();
}
private:
void rebuildGeometry()
{
QByteArray vertices;
QByteArray indices;
...
setPrimitiveType(Lines);
setVertexBuffer(vertices);
setIndexBuffer(indices);
setStride(3 * sizeof(float)); // e.g. when having 3 components per vertex
setBounds(...); // minimum and maximum extents, for picking
addAttribute(PositionSemantic, 0, F32Type);
...
}
};随后,该类可作为 QML 类型进行注册,并配合 `Model` 使用。
在 Qt 5 中,类型注册是通过 `qmlRegisterType` 实现的:
qmlRegisterType<CustomGeometry>("Example", 1, 0, "CustomGeometry");在 Qt 6 中,默认做法是借助构建系统进行自动注册。无需调用 `qmlRegisterType`,.pro 文件现在可以包含:
CONFIG += qmltypes
QML_IMPORT_NAME = Example
QML_IMPORT_MAJOR_VERSION = 1使用 CMake 时,自动注册是默认行为,因此除基本的 QML 模块配置外无需进行特殊设置:
qt_add_qml_module(application
URI Example
VERSION 1.0
)类实现中应添加QML_NAMED_ELEMENT :
class CustomGeometry : public QQuick3DGeometry
{
Q_OBJECT
QML_NAMED_ELEMENT(CustomGeometry)
...
};随后,QML 代码即可使用该自定义类型:
import Example 1.0
Model {
id: customModel
geometry: CustomGeometry {
}
}自定义几何体至少应指定以下内容:
- 顶点数据、
- 顶点步长,
- 基元类型,
- 一个具有 PositionSemantic 的属性。
这些信息足以渲染网格。对于索引绘制,还需指定索引缓冲区数据以及一个具有 IndexSemantic 属性的属性。为了支持拾取(输入)功能,该类必须通过setBounds() 指定边界体积。 为了实现正确的照明效果,需要一个具有 NormalSemantic 的属性。当材质使用纹理时,必须提供至少一组 UV 坐标,并在 TexCoord0Semantic 或 TexCoord1Semantic 属性中进行描述。某些材质可能还需提供切线和副法线。
作为一个具体的、最简示例,以下类将提供单个三角形的几何体:
class ExampleGeometry : public QQuick3DGeometry
{
Q_OBJECT
QML_NAMED_ELEMENT(ExampleGeometry)
public:
ExampleGeometry();
private:
void updateData();
};
ExampleGeometry::ExampleGeometry()
{
updateData();
}
void ExampleGeometry::updateData()
{
QByteArray v;
v.resize(3 * 3 * sizeof(float));
float *p = reinterpret_cast<float *>(v.data());
// a triangle, front face = counter-clockwise
*p++ = -1.0f; *p++ = -1.0f; *p++ = 0.0f;
*p++ = 1.0f; *p++ = -1.0f; *p++ = 0.0f;
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f;
setVertexData(v);
setStride(3 * sizeof(float));
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
0,
QQuick3DGeometry::Attribute::F32Type);
}根据场景中的光照情况,从 Model 引用此几何体后的结果如下:

注意:顶点 数据应遵循 OpenGL 规范。这意味着提供数据时,必须假设在归一化设备坐标系中 Y 轴向上,且前表面采用逆时针绕组。
成员函数文档
void QQuick3DGeometry::addAttribute(QQuick3DGeometry::Attribute::Semantic semantic, int offset, QQuick3DGeometry::Attribute::ComponentType componentType)
添加顶点属性描述。每个属性都有一个semantic ,用于指定该属性的用途及其组件数量;一个offset ,用于描述从起点经顶点到顶点内部属性位置的路径;以及一个componentType ,用于指定属性的数据类型和大小。
语义可以是以下之一:
| 常量 | 描述 |
|---|---|
PositionSemantic | 该属性为位置。包含 3 个分量:x、y 和z |
NormalSemantic | 该属性为法线向量。包含 3 个分量:x、y 和z |
TexCoord0Semantic | 该属性是纹理坐标。包含 2 个分量:u和v |
TexCoord1Semantic | 该属性是纹理坐标。包含 2 个分量:u和v |
TangentSemantic | 该属性是一个切向量。包含 3 个分量:x、y 和z |
BinormalSemantic | 该属性是一个副法线向量。包含 3 个分量:x、y 和z |
JointSemantic | 该属性是用于皮肤绑定的关节索引向量。包含 4 个分量:关节索引 1-4 |
WeightSemantic | 该属性是皮肤映射的权重向量。包含 4 个分量:关节权重 1-4 |
ColorSemantic | 该属性是一个顶点颜色向量。包含 4 个分量:r、g、b 和a |
TargetPositionSemantic | 该属性是第一个变形目标的位置。包含 3 个分量:x、y 和z |
TargetNormalSemantic | 该属性是第一个变形目标的法线向量。包含 3 个分量:x、y 和z |
TargetTangentSemantic | 该属性是第一个形态目标的切向量。3 个分量:x、y 和z |
TargetBinormalSemantic | 该属性是第一个形态目标的法线矢量。3个分量:x、y 和z |
此外,semantic 可以是IndexSemantic 。在这种情况下,该属性并不代表顶点缓冲区中的条目,而是描述索引缓冲区中的索引数据。由于每个顶点始终只有一个索引,因此offset 对于索引缓冲区而言没有意义,应保留为零。
分量类型可以是以下之一:
| 常量 | 描述 |
|---|---|
U16Type | 索引分量类型为无符号 16 位整数。仅支持IndexSemantic 。 |
U32Type | 该属性(或索引分量)为无符号 32 位整数。 |
I32Type | 该属性是一个带符号的32位整数。请注意,旧版OpenGL(例如2.1或OpenGL ES 2.0)可能不支持此数据类型。 |
F32Type | 该属性是一个单精度浮点数。 注意: 联合索引数据 通常为 注意:对于 索引数据( 注意:TargetXXXSemantics 将被废弃。addTargetAttribute 可用于形态目标。目前仅出于向后兼容性考虑才支持这些语义。若将其与addTargetAttribute 和setTargetData 混合使用,则无法保证结果。 |
void QQuick3DGeometry::addAttribute(const QQuick3DGeometry::Attribute &attribute)
添加顶点属性描述。每个属性都有一个语义,用于指定该属性的用途及其包含的组件数量;一个从顶点起始位置到该属性在顶点内位置的偏移量;以及一个 componentType,用于指定该属性的数据类型和大小。
这是一个重载函数。
void QQuick3DGeometry::addSubset(int offset, int count, const QVector3D &boundsMin, const QVector3D &boundsMax, const QString &name = {})
向几何体添加新的子集。子集允许使用不同的材质渲染几何体的部分区域。材质在model 中指定。
如果几何体具有索引缓冲区,则 `offset ` 和 `count ` 分别表示子集中的原始偏移量和索引数量。如果几何体仅具有顶点缓冲区,则偏移量即为顶点偏移量,计数即为子集中的顶点数量。
边界boundsMin 和boundsMax 应像几何体边界一样包围该子集。此外,子集还可以具有name 。
[since 6.6] void QQuick3DGeometry::addTargetAttribute(quint32 targetId, QQuick3DGeometry::Attribute::Semantic semantic, int offset, int stride = 0)
添加了变形目标属性描述。每个属性都有一个所属的targetId (目标ID),一个semantic (属性ID),用于指定该属性的用途及其包含的组件数量;一个offset (从起始点到顶点,再到顶点内属性位置的路径),以及一个stride (元素之间的字节间距)。
注意:targetId 应从 0 开始递增且不得跳号,所有目标应具有相同的属性。
注意:其 语义与顶点属性相同,但不允许在目标属性中使用 IndexSemantic、JointSemantic 和 WeightSemantic。
注意: 所有目标属性的componentTypes 必须为 F32Type。
注意:如果 未指定步长,或者步长小于或等于零,则该属性被视为紧密排列。
该函数在 Qt 6.6 中引入。
另请参阅 addAttribute 。
[since 6.6] void QQuick3DGeometry::addTargetAttribute(const QQuick3DGeometry::TargetAttribute &attribute)
添加了变形目标属性的描述。每个属性都有一个 targetId(该属性所属的目标标识符)、一个语义(指定属性的用途及其包含的组件数量)、一个偏移量(从顶点起始位置到该属性在顶点内位置的距离),以及一个步长(元素之间的字节间隔)。
这是一个重载函数。
该函数在 Qt 6.6 中引入。
QQuick3DGeometry::Attribute QQuick3DGeometry::attribute(int index) const
返回属性定义编号index
属性定义的编号范围为 0 到attributeCount() - 1
int QQuick3DGeometry::attributeCount() const
返回为此几何体定义的属性数量。
另请参阅 attribute 。
QVector3D QQuick3DGeometry::boundsMax() const
返回边界体积的最大坐标。
另请参阅 setBounds 。
QVector3D QQuick3DGeometry::boundsMin() const
返回包围体的最小坐标。
另请参阅 setBounds 。
void QQuick3DGeometry::clear()
将几何体重置为初始状态,清除先前设置的顶点和索引数据以及属性。
QByteArray QQuick3DGeometry::indexData() const
返回索引缓冲区数据。
另请参阅 setIndexData()。
QQuick3DGeometry::PrimitiveType QQuick3DGeometry::primitiveType() const
返回渲染时使用的基本类型。默认值为Triangles 。
另请参阅 setPrimitiveType 。
void QQuick3DGeometry::setBounds(const QVector3D &min, const QVector3D &max)
将几何体的包围体积设置为由点min 和max 定义的立方体。此设置用于picking 。
void QQuick3DGeometry::setIndexData(const QByteArray &data)
将索引缓冲区设置为data 。要使用索引绘制,请添加一个属性,其中包含IndexSemantic
另请参阅 indexData() 和addAttribute 。
void QQuick3DGeometry::setIndexData(int offset, const QByteArray &data)
更新索引缓冲区的子集。offset 指定以字节为单位的偏移量,data 指定大小和数据。
此函数不会调整缓冲区的大小。如果offset + data.size() 大于缓冲区的当前大小,超出部分的数据将被忽略。
注意: 针对顶点、索引和变形目标数据的局部更新函数, 不保证此类更改在内部的实现方式。根据底层实现的不同,即使是局部更改也可能导致整个图形资源被更新。
这是一个重载函数。
void QQuick3DGeometry::setPrimitiveType(QQuick3DGeometry::PrimitiveType type)
将渲染所用的基本类型设置为type 。
| 常量 | 描述 |
|---|---|
Points | 这些基元是点。 |
LineStrip | 基元是条带中的线。 |
Lines | 基元是列表中的线。 |
TriangleStrip | 基元是条带中的三角形。 |
TriangleFan | 这些基元是扇形排列中的三角形。请注意,根据底层图形 API 的不同,运行时可能不支持三角形扇形。 |
Triangles | 这些基元是列表中的三角形。 |
初始值为Triangles 。
注意:请 注意,根据底层图形 API 的不同,运行时可能不支持三角形扇形(TriangleFan)。例如,在 Direct 3D 中,这种拓扑结构将完全无法运行。
注意: “Points”的点大小以及“Lines”和“LineStrip”的线宽由material 控制。但请注意,根据底层图形API的不同,运行时可能不支持除1以外的大小。
另请参阅 primitiveType()。
void QQuick3DGeometry::setStride(int stride)
将顶点缓冲区的步长设置为stride (单位为字节)。这是缓冲区中两个相邻顶点之间的距离。
例如,对于使用PositionSemantic 、IndexSemantic 和ColorSemantic 的几何体,其紧凑交错的顶点缓冲区的步长为28 (总共七个浮点数:三个用于位置,四个用于颜色,没有用于索引,因为索引不包含在顶点缓冲区中。)
注意: QQuick3DGeometry 仅支持且仅能处理采用交错属性布局的顶点数据。
另请参阅 stride() 和addAttribute 。
[since 6.6] void QQuick3DGeometry::setTargetData(const QByteArray &data)
设置变形目标缓冲区data 。该缓冲区应包含所有变形目标数据。
该函数于 Qt 6.6 中引入。
另请参阅 targetData() 和addTargetAttribute 。
[since 6.6] void QQuick3DGeometry::setTargetData(int offset, const QByteArray &data)
更新形态目标缓冲区的一部分。offset 指定以字节为单位的偏移量,data 指定数据的大小和数据内容。
此函数不会调整缓冲区的大小。如果offset + data.size() 大于缓冲区的当前大小,则超出部分的数据将被忽略。
注意: 针对顶点、索引和变形目标数据的局部更新函数, 不保证此类更改在内部的实现方式。根据底层实现的不同,即使是局部更改也可能导致整个图形资源被更新。
这是一个重载函数。
该函数在 Qt 6.6 中引入。
void QQuick3DGeometry::setVertexData(const QByteArray &data)
设置顶点缓冲区data 。该缓冲区应包含数组中打包的所有顶点数据,具体格式由属性定义规定。请注意,这不包括IndexSemantic 属性的数据,这些数据应存放在索引缓冲区中。
另请参阅 vertexData()、addAttribute 、setStride 以及setIndexData 。
void QQuick3DGeometry::setVertexData(int offset, const QByteArray &data)
更新顶点缓冲区的子集。offset 指定以字节为单位的偏移量,data 指定大小和数据。
此函数不会调整缓冲区的大小。如果 `offset + data.size() ` 大于缓冲区的当前大小,超出部分的数据将被忽略。
注意: 针对顶点、索引和变形目标数据的局部更新函数, 并不保证此类更改在内部如何实现。根据底层实现的不同,即使是局部更改也可能导致整个图形资源被更新。
这是一个重载函数。
int QQuick3DGeometry::stride() const
返回顶点缓冲区的字节步长。
另请参阅 setStride 。
QVector3D QQuick3DGeometry::subsetBoundsMax(int subset) const
返回subset 的最大边界数。
另请参阅 subsetBoundsMin 。
QVector3D QQuick3DGeometry::subsetBoundsMin(int subset) const
返回subset 的最小边界个数。
另请参阅 subsetBoundsMax 。
int QQuick3DGeometry::subsetCount() const
返回子集的个数。
int QQuick3DGeometry::subsetCount(int subset) const
返回子集的基元个数。
另请参阅 subsetOffset 。
QString QQuick3DGeometry::subsetName(int subset) const
返回subset 的名称。
int QQuick3DGeometry::subsetOffset(int subset) const
返回顶点或索引缓冲区中subset 的偏移量。
另请参阅 subsetCount 。
[since 6.6] QQuick3DGeometry::TargetAttribute QQuick3DGeometry::targetAttribute(int index) const
返回形态目标属性定义编号index
属性定义的编号范围为 0 到attributeCount() - 1
该函数于 Qt 6.6 中引入。
[since 6.6] int QQuick3DGeometry::targetAttributeCount() const
返回为此几何体定义的变形目标属性的数量。
该函数于 Qt 6.6 中引入。
另请参阅 targetAttribute 。
[since 6.6] QByteArray QQuick3DGeometry::targetData() const
返回由setTargetData 设置的目标缓冲区数据。
该函数自 Qt 6.6 起引入。
另请参阅 setTargetData()。
QByteArray QQuick3DGeometry::vertexData() const
返回由setVertexData 设置的顶点缓冲区数据。
另请参阅 setVertexData()。
© 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.