QML中的3D功能--入门开发

Qt Quick 提供了强大的 3D 功能支持,主要通过 Qt 3D 模块实现。以下是 QML 中开发 3D 应用的全面指南。

1. 基本配置

环境要求

  • Qt 5.10 或更高版本(推荐 Qt 6.x)

  • 启用 Qt 3D 模块

  • 支持 OpenGL 的硬件

项目配置

在 .pro 文件中添加:

QT += 3dcore 3drender 3dinput 3dextras 3dquick

2. 核心组件

基本结构

qml

import Qt3D.Core 2.15
import Qt3D.Render 2.15
import Qt3D.Extras 2.15

Entity {
    id: sceneRoot
    
    components: [
        RenderSettings {
            activeFrameGraph: ForwardRenderer {
                clearColor: "black"
                camera: camera
            }
        }
    ]
    
    // 相机配置
    Camera {
        id: camera
        position: Qt.vector3d(0, 0, 10)
        viewCenter: Qt.vector3d(0, 0, 0)
    }
    
    // 3D内容放在这里
    FirstPersonCameraController { camera: camera }
}

常用属性

核心组件属性

Entity (Qt3D.Core)
属性/方法 类型 说明
components list<Component3D> 实体包含的组件列表
enabled bool 是否启用实体(默认true)
parent Entity 父实体(用于构建场景层次)
Transform (Qt3D.Core)
属性/方法 类型 说明
translation vector3d 位置坐标(x,y,z)
rotation quaternion 四元数旋转
rotationX real X轴旋转角度(度)
rotationY real Y轴旋转角度(度)
rotationZ real Z轴旋转角度(度)
scale real 统一缩放因子
scale3D vector3d 各轴独立缩放(x,y,z)

渲染相关

Camera (Qt3D.Render)
属性/方法 类型 说明
position vector3d 相机位置
viewCenter vector3d 观察中心点
upVector vector3d 相机的上向量(默认0,1,0)
fieldOfView real 视野角度(度)
nearPlane real 近裁剪面距离
farPlane real 远裁剪面距离
projectionType enum 投影类型(Orthographic/Perspective)
aspectRatio real 宽高比(自动计算)
Material (Qt3D.Extras)
属性/方法 类型 说明
ambient color 环境光颜色
diffuse color 漫反射颜色
specular color 镜面反射颜色
shininess real 高光强度(0-100)
alpha real 透明度(0-1)

几何体属性

通用Mesh属性 (Qt3D.Extras)
属性/方法 类型 说明
radius real 球体/环体的半径
length real 立方体长度
width real 立方体宽度
height real 立方体高度
rings int 球体经线分段数
slices int 球体纬线分段数

光源属性

通用Light属性 (Qt3D.Render)
属性/方法 类型 说明
color color 光源颜色
intensity real 光照强度
enabled bool 是否启用光源
PointLight特有
属性/方法 类型 说明
constantAttenuation real 恒定衰减
linearAttenuation real 线性衰减
quadraticAttenuation real 二次衰减

常用方法

SceneLoader (Qt3D.Core)
方法 参数 说明
setSource url 加载3D模型文件
This directory contains the Qt3D project for Qt5: * Qt3D QML bindings and * Qt3D C++ APIs Building Qt3D ================== Qt5 is a rapidly changing bleeding edge environment. This branch is our initial support for it and thus is also rapidly changing and bleeding edge. This branch is experimental, and unsupported. This information is provided for advanced use only. No guarantees about API stability or even if this works at all are supplied, use at your own risk. First fetch the Qt5 source tree and Qt3D master branch: cd ~/depot git clone ssh://codereview.qt-project.org:29418/qt/qt5.git cd qt5 ./init-repository --codereview-username \ --module-subset=qtbase,qtsvg,qtdeclarative,qttools,qtxmlpatterns,qtdoc,qlalr,qtrepotools,qtqa,qtlocation,qt3d git submodule foreach "git fetch gerrit && git reset --hard gerrit/master" cd qt3d scp -p -P 29418 codereview.qt-project.org:hooks/commit-msg .git/hooks/ git fetch gerrit git checkout --track -b master gerrit/master If you are reading this file then somehow you probably already got this far anyway. Now build Qt5, which will also build Qt3D as a module: cd ~/build mkdir qt5 cd qt5 ~/depot/qt5/configure -developer-build -opensource -confirm-license -no-webkit -no-phonon -nomake tests \ -nomake examples -declarative -opengl -svg && make -j 4 What's in Qt3D ================== Directory structure: src/threed/ This is the main library of the Qt3D project, containing abstractions for cross-platform GL, shaders, lighting models, and so on. src/plugins/ Scene format loading plugins. src/imports/ QML import plugins. util/ Various utilities that are useful when working with Qt3D. examples/ Some examples of using Qt3D QML bindings and Qt3D C++ API. demos/ Some more complex demos of using Qt3D QML bindings and Qt3D C++ API. tests/auto/qml3d/ Unit tests for the QML bindings. tests/auto/threed/ Unit tests for the C++ API doc/ Documentation. devices/symbian/ Symbian deployment file Documentation ============= The documentation can be generated with "make docs". It will be placed into "doc/html" in the build directory. Packages ======== This section is only for those developing Qt3D. Read on to discover how the building of packages works. This section is also important if you want to change how the structure of the Qt3D pro files work. To build Qt3D, run: qmake && make The .pro files will cause the toolchain to place the libraries, QML files and meshes of Qt3D directly into place, as part of the compile process. The files go into the bin/ directory, and the executables can be run directly from there. If you are doing a developer build, plugins will be installed in such a way that Qt will find them. After building the tree the install step is invoked using the INSTALL_ROOT environment export to cause the installation rules to place all the files into a sandboxed install tree, ready for packaging: INSTALL_ROOT=tmp make install Examples ======== Some examples require assimp library to parse the content. Go to http://assimp.sourceforge.net/ and build and install the assimp library. Then configure Qt3D to include assimp and run qmake && make.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byxdaz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值