Qt6 QML Book/Qt Quick 3D/使用资产

本文介绍了如何使用Blender创建3D场景,然后利用Balsam工具将资源转换为QtQuick3D支持的格式。Balsam支持COLLADA、FBX、GLTF2、STL和Wavefront等格式,使得从建模工具到QML的集成变得更加顺畅。在Blender中创建猴头场景并导出为COLLADA文件后,使用Balsam将其转换为QtQuick3D的QML和mesh文件,这些文件可以直接在QtQuick3D项目中使用。文章还展示了如何在QML中直接加载和控制转换后的3D模型。

Working with Assets

使用资产

When working with 3D scenes, the built in meshes quickly grow old. Instead, a good flow from a modelling tool into QML is needed. Qt Quick 3D comes with the Balsam asset import tool, which is used to convert common asset formats into a Qt Quick 3D friendly format.

使用3D场景时,内置网格会很快变老旧。相反,需要从建模工具到导入QML。Qt Quick 3D附带Balsam资产导入工具,用于将常见资产格式转换为Qt Quick 3D友好格式。

The purpose of Balsam is to make it easy to take assets created in common tools such as Blender, Maya or 3ds Max and use them from Qt Quick 3D. Balsam supports the following asset types:

​Balsam的目的是便于获取在常用工具(如Blender、Maya或3ds Max)中创建的资产,并从Qt Quick 3D中使用它们。Balsam支持以下资产类型:

  • COLLADA (*.dae)
  • FBX (*.fbx)
  • GLTF2 (*.gltf*.glb)
  • STL (*.stl)
  • Wavefront (*.obj)

For some format, texture assets can also be exported into a Qt Quick 3D-friendly format, as long as Qt Quick 3D supports the given asset.

对于某些格式,只要Qt Quick 3D支持给定的资源,纹理资源也可以导出为Qt Quick 3D友好格式。

Blender

To generate an asset that we can import, we will use Blender to create a scene with a monkey head in it. We will then export this as a COLLADA file to be able to convert it to a Qt Quick 3D friendly file format using Balsam.

​为了生成可以导入的资源,我们将使用Blender创建一个包含猴子头的场景。然后,我们将其导出为COLLADA文件,以便能够使用Balsam将其转换为Qt Quick 3D友好文件格式。

Blender is available from https://www.blender.org/, and mastering Blender is a topic for another book, so we will do the most basic thing possible. Remove the original cube (select the cube with the left mouse button, pressshift + x, select Delete), add a mesh (from the keyboard shift + a, select Mesh), and select to add a monkey (select Monkey from the list of available meshes). There are a number of video tutorials demonstrating how to do this. The resulting Blender user interface with the monkey head scene can be seen below.

​Blender可从https://www.blender.org/下载,而掌握Blender是另一本书的主题,所以我们将尽可能做最基本的事情。移除原始立方体(用鼠标左键选择立方体,按shift+x,选择Delete),添加网格(从键盘shift+a,选择mesh),然后选择添加猴子(从可用网格列表中选择猴子)。有许多视频教程演示了如何做到这一点。Blender生成的用户界面与猴头场景如下所示。

Once the head is in place, go to File -> Export -> COLLADA.

猴头就位后,进入文件->导出->COLLADA。

This takes you to the Export COLLADA dialog where you can export the resulting scene.

这将带您进入“导出COLLADA”对话框,您可以在其中导出生成的场景。

Tip

Both the blender scene and the exported COLLADA file (*.dae) can be found among the example files for the chapter.

blender场景和导出的COLLADA文件(*.dae)都可以在本章的示例文件中找到。

Balsam

Once the COLLADA file has been saved to disk, we can prepare it for use in Qt Quick 3D using Balsam. Balsam is available as a command line tool, or through a graphical user interface, using the balsamui tool. The graphical tool is just a thin layer on top of the command line tool, so there is no difference in what you can do with either tool.

COLLADA文件保存到磁盘后,我们可以使用Balsam将其准备好用于Qt Quick 3D。Balsam可用作命令行工具,或通过图形用户界面使用balsamui工具。图形工具只是命令行工具顶部的一个薄层,因此使用这两种工具可以做什么没有区别。

We start by adding the monkey.dae file to the input files section, and set the output folder to a reasonable path.Your paths will most likely be different from the ones shown in the screenshot.

我们从添加monkey.dae文件开始,并将输出文件夹设置为合理的路径。您的路径很可能与屏幕截图中显示的路径不同。

The Settings tab in the balsamui includes all the options. These all correspond to a command line option of the balsam tool. For now, we will leave all of them with their default values.

balsamui中的设置选项卡包括所有选项。这些都对应于balsam工具的命令行选项。现在,我们将保留它们的默认值。

Now, go back to the Input tab and click Convert. This will result in the following output in the status section of the user interface:

现在,返回到“输入”选项卡并单击“转换”。这将在用户界面的状态部分产生以下输出:

Converting 1 files...
[1/1] Successfully converted '/home/.../src/basicasset/monkey.dae'

Successfully converted all files!

If you started balsamui from the command line, you will also see the following output there:

如果从命令行启动balsamui,您还将在那里看到以下输出:

generated file:  "/home/.../src/basicasset/Monkey.qml"
generated file:  "/home/.../src/basicasset/meshes/suzanne.mesh"

This means that Balsam generated a *.qml file and a *.mesh file.

这意味着生成了一个*.qml文件和一个*.mesh文件。

The Qt Quick 3D Assets

Qt Quick 3D资源

To use the files from a Qt Quick project we need to add them to the project. This is done in the CMakeLists.txt file, in the qt_add_qml_module macro. Add the files to the QML_FILES and RESOURCES sections as shown below.

要使用Qt Quick项目中的文件,我们需要将它们添加到项目中。这是在CMakeLists.txt文件中完成的。在qt_add_qml_module宏中。将文件添加到QML_FILESRESOURCES部分,如下所示。

qt_add_qml_module(appbasicasset
    URI basicasset
    VERSION 1.0
    QML_FILES main.qml Monkey.qml 
    RESOURCES meshes/suzanne.mesh
)

Having done this, we can populate a View3D with the Monkey.qml as shown below.

完成后,我们可以用Monkey.qml填充View3D,如下所示。

View3D {
    anchors.fill: parent

    environment: SceneEnvironment {
        clearColor: "#222222"
        backgroundMode: SceneEnvironment.Color
    }

    Monkey {}
}

The Monkey.qml contains the entire Blender scene, including the camera and a light, so the result is a complete scene as shown below.

Monkey.qml包含整个Blender场景,包括相机和灯光,因此结果是一个完整的场景,如下所示。

The interested reader is encouraged to explore the Monkey.qml file. As you will see, it contains a completely normal Qt Quick 3D scene built from the elements we already have used in this chapter.

鼓励感兴趣的读者探索Monkey.qml文件。如您所见,它包含一个完全正常的Qt Quick 3D场景,该场景是根据我们在本章中已经使用的元素构建的。

Tip

As Monkey.qml is generated by a tool, do not modify the file manually. If you do, your changes will be overwritten if you ever have to regenerate the file using Balsam.

Monkey.qml由工具生成,请勿手动修改文件。如果这样做,如果您必须使用Balsam重新生成文件,您的更改将被覆盖。

An alternative to using the entire scene from Blender is to use the *.mesh file in a Qt Quick 3D scene. This is demonstrated in the code below.

使用Blender中的整个场景的替代方法是在Qt Quick 3D场景中,使用*.mesh文件。下面的代码对此进行了演示。

Here, we put a DirectionalLight and PerspectiveCamera into a View3D, and combine it with the mesh using a Model element. This way, we can control the positioning, scale, and lighting from QML. We also specify a different, yellow, material for the monkey head.

在这里,我们将DirectionalLight和PerspectiveCamera放入View3D,并使用模型元素将其与网格相结合。这样,我们可以从QML控制位置、绽放和照明。我们还为猴头指定了不同的黄色材质。

View3D {
    anchors.fill: parent

    environment: SceneEnvironment {
        clearColor: "#222222"
        backgroundMode: SceneEnvironment.Color
    }

    Model {
        source: "meshes/suzanne.mesh"

        scale: Qt.vector3d(2, 2, 2)

        eulerRotation.y: 30
        eulerRotation.x: -80

        materials: [ DefaultMaterial { diffuseColor: "yellow"; } ]
    }

    PerspectiveCamera {
        position: Qt.vector3d(0, 0, 15)
        Component.onCompleted: lookAt(Qt.vector3d(0, 0, 0))
    }

    DirectionalLight {
        eulerRotation.x: -20
        eulerRotation.y: 110

        castsShadow: true
    }
}

The resulting view is shown below.

结果视图如下所示。

This demonstrates how a simple mesh can be exported from a 3D design tool such as blender, converted to a Qt Quick 3D format and then used from QML. One thing to think about is that we can import the entire scene as is, i.e. using Monkey.qml, or use only the assets, e.g. suzanne.mesh. This puts you in control of the trade-off between simple importing of a scene, and added complexity while gaining flexibility by setting up the scene in QML.

这演示了如何从blender等3D设计工具导出简单网格,将其转换为Qt Quick 3D格式,然后从QML中使用。需要考虑的一件事是,我们可以按原样导入整个场景,即使用Monkey.qml,或仅使用资产,例如suzanne.mesh。这使您能够控制场景的简单导入和增加复杂性之间的权衡,同时通过在QML中设置场景获得灵活性。

        你是否想用 Python 打造专业桌面客户端?你是否在寻找一门系统全面、案例实战驱动的 QtQuick 视频教程?这门课程就是为你量身定制的!  本课程通过 10大模块、多个实战项目,手把手带你掌握 PySide6 + QtQuick 的核心开发技能,最终实现一个高度还原的「剪映」客户端。无论你是 Python 爱好者,还是 Qt 桌面开发者,这门课都能帮你快速提升!课程亮点一览全平台开发环境搭建(Win11 & macOS)掌握 QML 常用控件与布局深入理解 Python 与 QML 的双向交互掌握 QML 动画与 Canvas 绘图模型-视图-代理 MVD 编程模式玩转 QtQuick3D,轻松构建3D场景完整项目实战:高仿剪映 PC 客户端课程总结+进阶学习路线指导课程章节介绍第1章:开发环境全攻略  从零配置开发环境:Miniconda 安装、PySide6 配置、PyCharm & VS Code 开发调试、macOS 与 Win11 跨平台部署,让你轻松开始 QtQuick 编程之旅!第2章:QML 核心控件全面掌握  掌握 Item、Window、ApplicationWindow 等窗口根对象,文本/图片/按钮、CheckBox、RadioButton、ComboBox、SpinBox等控件的使用,还将实现一个视频进度调节的 Slider与视频播放控件。第3章:布局系统与组件机制  详细讲解行列布局、栅格布局、流式布局、Component、Loader、Repeater 等关键概念,最后实现一个综合案例:自定义电池控件。第4章:Python × JS × QML 高效交互  系统讲解 Python 注册 QML 的四种方式、QML 调用 Python 方法、信号槽机制、值绑定、鼠标/键盘事件处理,让你实现Python 与前端 QML 的无缝通信!第5章:QML 动画系统实战  掌握缩放、旋转、State 状态切换、NumberAnimation 数值动画、Behavior 行为动画、动画控制启动与停止 —— 为你的应用增添高级动态体验!第6章:模型-视图-代理 MVD 精讲  详细解析 ListView、GridView、TableView 等复杂控件使用,配合 DelegateChooser 实现高灵活的代理渲染效果,助力你构建数据驱动的 UI 界面。第7章:Canvas 绘图:打造炫酷 UI  深入 Canvas 编程,绘制路径、圆弧、文本,并结合项目案例,开发一个动态汽车仪表盘,提升界面表现力!第8章:QtQuick3D:迈入三维开发  从 View3D 的基本使用到复杂属性控制,掌握材质、纹理、光照、天空盒、实例化渲染等核心内容,最终完成两个3D项目:3D 茶壶 与 3D 多形态机器人!第9章:项目实战:高仿剪映 PC 客户端  将前面所学内容应用于真实项目,完整实现一个桌面级视频编辑客户端,真正做到学以致用。项目截图如下:  首页  剪辑主窗口  导出窗口第10章:课程总结与进阶建议  回顾全程知识点,规划你的 QtQuick 技术进阶路线,让你少走弯路,技能持续成长!适合人群想快速入门 QtQuick 桌面开发的 Python 开发者有 PyQt / C++ Qt 基础,想转向更现代的 PySide6/QML 开发者UI 工程师、工具开发人员、开源爱好者对剪映客户端等专业项目实现感兴趣的进阶程序员你将获得230页精美课件完整的源代码 & 项目素材持续更新答疑 & 技术支持结课后具备独立构建 QtQuick 应用的能力不止是教程,更是一次实战成长之旅!  立即加入课程,掌握 PySide6 QtQuick 开发的核心技能,开启你的桌面应用进阶之路!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值