Qt6 QML Book/Qt Quick 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中设置场景获得灵活性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值