qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记

qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记

文章目录

  • qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记
    • 1.例程运行效果
    • 2.例程缩略图
    • 3.项目文件列表
    • 4.main.qml
    • 5.main.cpp
    • 6.CMakeLists.txt

1.例程运行效果

在这里插入图片描述

运行该项目需要自己准备一个模型文件

2.例程缩略图

在这里插入图片描述

3.项目文件列表

runtimeloader/
├── CMakeLists.txt
├── main.cpp
├── main.qml
├── qml.qrc
└── runtimeloader.pro1 directory, 5 files

4.main.qml

// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clauseimport QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Layoutsimport Qt.labs.platform
import QtCoreimport QtQuick3D
import QtQuick3D.Helpers
import QtQuick3D.AssetUtils// 创建一个窗口根节点,设置窗口大小和显示状态
Window {id: windowRootvisible: truewidth: 1280height: 720property url importUrl; // 用于导入模型的URL//! [base scene] 基础场景View3D {id: view3Danchors.fill: parent // 填充父窗口environment: SceneEnvironment {id: envbackgroundMode: SceneEnvironment.SkyBox // 背景模式为天空盒lightProbe: Texture {textureData: ProceduralSkyTextureData{} // 使用程序化天空纹理}InfiniteGrid {visible: helper.gridEnabled // 是否显示网格gridInterval: helper.gridInterval // 网格间隔}}camera: helper.orbitControllerEnabled ? orbitCamera : wasdCamera // 根据控制器选择相机// 设置方向光源DirectionalLight {eulerRotation.x: -35eulerRotation.y: -90castsShadow: true // 启用阴影}Node {id: orbitCameraNodePerspectiveCamera {id: orbitCamera // 轨道相机}}// 第一人称相机(WASD控制)PerspectiveCamera {id: wasdCameraonPositionChanged: {// 更新相机的近远裁剪面let distance = position.length()if (distance < 1) {clipNear = 0.01clipFar = 100} else if (distance < 100) {clipNear = 0.1clipFar = 1000} else {clipNear = 1clipFar = 10000}}}//! [base scene]// 重置视图的函数function resetView() {if (importNode.status === RuntimeLoader.Success) {helper.resetController()}}//! [instancing] 实例化RandomInstancing {id: instancinginstanceCount: 30 // 设置实例数量position: InstanceRange {property alias boundsDiameter: helper.boundsDiameterfrom: Qt.vector3d(-3*boundsDiameter, -3*boundsDiameter, -3*boundsDiameter); // 位置范围to: Qt.vector3d(3*boundsDiameter, 3*boundsDiameter, 3*boundsDiameter)}color: InstanceRange { from: "black"; to: "white" } // 颜色范围}//! [instancing]QtObject {id: helperproperty real boundsDiameter: 0 // 场景边界的直径property vector3d boundsCenter // 场景中心property vector3d boundsSize // 场景大小property bool orbitControllerEnabled: true // 是否启用轨道控制器property bool gridEnabled: gridButton.checked // 是否启用网格property real cameraDistance: orbitControllerEnabled ? orbitCamera.z : wasdCamera.position.length() // 相机与中心的距离property real gridInterval: Math.pow(10, Math.round(Math.log10(cameraDistance)) - 1) // 网格间隔计算// 更新场景边界信息function updateBounds(bounds) {boundsSize = Qt.vector3d(bounds.maximum.x - bounds.minimum.x,bounds.maximum.y - bounds.minimum.y,bounds.maximum.z - bounds.minimum.z)boundsDiameter = Math.max(boundsSize.x, boundsSize.y, boundsSize.z)boundsCenter = Qt.vector3d((bounds.maximum.x + bounds.minimum.x) / 2,(bounds.maximum.y + bounds.minimum.y) / 2,(bounds.maximum.z + bounds.minimum.z) / 2 )wasdController.speed = boundsDiameter / 1000.0 // 更新控制器速度wasdController.shiftSpeed = 3 * wasdController.speedwasdCamera.clipNear = boundsDiameter / 100wasdCamera.clipFar = boundsDiameter * 10view3D.resetView() // 重置视图}// 重置控制器function resetController() {orbitCameraNode.eulerRotation = Qt.vector3d(0, 0, 0)orbitCameraNode.position = boundsCenterorbitCamera.position = Qt.vector3d(0, 0, 2 * helper.boundsDiameter)orbitCamera.eulerRotation = Qt.vector3d(0, 0, 0)orbitControllerEnabled = true}// 切换控制器function switchController(useOrbitController) {if (useOrbitController) {let wasdOffset = wasdCamera.position.minus(boundsCenter)let wasdDistance = wasdOffset.length()let wasdDistanceInPlane = Qt.vector3d(wasdOffset.x, 0, wasdOffset.z).length()let yAngle = Math.atan2(wasdOffset.x, wasdOffset.z) * 180 / Math.PIlet xAngle = -Math.atan2(wasdOffset.y, wasdDistanceInPlane) * 180 / Math.PIorbitCameraNode.position = boundsCenterorbitCameraNode.eulerRotation = Qt.vector3d(xAngle, yAngle, 0)orbitCamera.position = Qt.vector3d(0, 0, wasdDistance)orbitCamera.eulerRotation = Qt.vector3d(0, 0, 0)} else {wasdCamera.position = orbitCamera.scenePositionwasdCamera.rotation = orbitCamera.sceneRotationwasdController.focus = true}orbitControllerEnabled = useOrbitController}}//! [runtimeloader] 运行时加载器RuntimeLoader {id: importNodesource: windowRoot.importUrl // 导入模型的URLinstancing: instancingButton.checked ? instancing : null // 实例化开关onBoundsChanged: helper.updateBounds(bounds) // 更新场景边界}//! [runtimeloader]//! [bounds] 场景边界Model {parent: importNodesource: "#Cube" // 默认使用立方体模型materials: PrincipledMaterial {baseColor: "red" // 设置基础颜色为红色}opacity: 0.2 // 设置模型透明度visible: visualizeButton.checked && importNode.status === RuntimeLoader.Success // 根据条件显示模型position: helper.boundsCenterscale: Qt.vector3d(helper.boundsSize.x / 100,helper.boundsSize.y / 100,helper.boundsSize.z / 100)}//! [bounds]//! [status report] 状态报告Rectangle {id: messageBoxvisible: importNode.status !== RuntimeLoader.Success // 如果导入失败,显示错误消息color: "red"width: parent.width * 0.8height: parent.height * 0.8anchors.centerIn: parentradius: Math.min(width, height) / 10opacity: 0.6Text {anchors.fill: parentfont.pixelSize: 36text: "Status: " + importNode.errorString + "\nPress \"Import...\" to import a model" // 显示错误信息color: "white"wrapMode: Text.WraphorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}//! [status report]}//! [camera control] 相机控制OrbitCameraController {id: orbitControllerorigin: orbitCameraNodecamera: orbitCameraenabled: helper.orbitControllerEnabled // 根据状态启用或禁用轨道控制器}WasdController {id: wasdControllercontrolledObject: wasdCameraenabled: !helper.orbitControllerEnabled // 根据状态启用或禁用WASD控制器}//! [camera control]// 界面控制面板Pane {width: parent.widthcontentHeight: controlsLayout.implicitHeightRowLayout {id: controlsLayoutButton {id: importButtontext: "Import..."onClicked: fileDialog.open() // 打开文件对话框focusPolicy: Qt.NoFocus}Button {id: resetButtontext: "Reset view"onClicked: view3D.resetView() // 重置视图focusPolicy: Qt.NoFocus}Button {id: visualizeButtoncheckable: truetext: "Visualize bounds" // 显示场景边界focusPolicy: Qt.NoFocus}Button {id: instancingButtoncheckable: truetext: "Instancing" // 开启实例化focusPolicy: Qt.NoFocus}Button {id: gridButtontext: "Show grid" // 显示网格focusPolicy: Qt.NoFocuscheckable: truechecked: false}Button {id: controllerButtontext: helper.orbitControllerEnabled ? "Orbit" : "WASD" // 切换控制器onClicked: helper.switchController(!helper.orbitControllerEnabled)focusPolicy: Qt.NoFocus}RowLayout {Label {text: "Material Override"}ComboBox {id: materialOverrideComboBoxtextRole: "text"valueRole: "value"implicitContentWidthPolicy: ComboBox.WidestTextonActivated: env.debugSettings.materialOverride = currentValue // 选择材质覆盖model: [{ value: DebugSettings.None, text: "None"},{ value: DebugSettings.BaseColor, text: "Base Color"},{ value: DebugSettings.Roughness, text: "Roughness"},{ value: DebugSettings.Metalness, text: "Metalness"},{ value: DebugSettings.Diffuse, text: "Diffuse"},{ value: DebugSettings.Specular, text: "Specular"},{ value: DebugSettings.ShadowOcclusion, text: "Shadow Occlusion"},{ value: DebugSettings.Emission, text: "Emission"},{ value: DebugSettings.AmbientOcclusion, text: "Ambient Occlusion"},{ value: DebugSettings.Normals, text: "Normals"},{ value: DebugSettings.Tangents, text: "Tangents"},{ value: DebugSettings.Binormals, text: "Binormals"},{ value: DebugSettings.F0, text: "F0"}]}}CheckBox {text: "Wireframe" // 启用或禁用线框模式checked: env.debugSettings.wireframeEnabledonCheckedChanged: {env.debugSettings.wireframeEnabled = checked}}}}// 文件对话框,允许用户选择glTF模型文件FileDialog {id: fileDialognameFilters: ["glTF files (*.gltf *.glb)", "All files (*)"]onAccepted: importUrl = file // 选择文件后导入Settings {id: fileDialogSettingscategory: "QtQuick3D.Examples.RuntimeLoader"property alias folder: fileDialog.folder}}// 调试视图切换按钮Item {width: debugViewToggleText.implicitWidthheight: debugViewToggleText.implicitHeightanchors.right: parent.rightLabel {id: debugViewToggleTexttext: "Click here " + (dbg.visible ? "to hide DebugView" : "for DebugView")anchors.right: parent.rightanchors.top: parent.top}MouseArea {anchors.fill: parentonClicked: dbg.visible = !dbg.visible // 切换调试视图可见性DebugView {y: debugViewToggleText.height * 2anchors.right: parent.rightsource: view3Did: dbgvisible: false}}}
}

5.main.cpp

// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifdef HAS_MODULE_QT_WIDGETS
# include <QApplication>
#endif
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QtQuick3D/qquick3d.h>int main(int argc, char *argv[])
{
#ifdef HAS_MODULE_QT_WIDGETSQApplication app(argc, argv);
#elseQGuiApplication app(argc, argv);
#endifapp.setOrganizationName("The Qt Company");app.setOrganizationDomain("qt.io");app.setApplicationName("Runtime Asset Loading Example");const auto importUrl = argc > 1 ? QUrl::fromLocalFile(argv[1]) : QUrl{};if (importUrl.isValid())qDebug() << "Importing" << importUrl;QSurfaceFormat::setDefaultFormat(QQuick3D::idealSurfaceFormat(4));QQmlApplicationEngine engine;const QUrl url(QStringLiteral("qrc:/main.qml"));engine.load(url);if (engine.rootObjects().isEmpty()) {qWarning() << "Could not find root object in" << url;return -1;}QObject *topLevel = engine.rootObjects().value(0);QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);if (window)window->setProperty("importUrl", importUrl);return app.exec();
}

6.CMakeLists.txt

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clausecmake_minimum_required(VERSION 3.16)
project(runtimeloader LANGUAGES CXX)set(CMAKE_AUTOMOC ON)find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick Quick3D Widgets)qt_add_executable(runtimeloadermain.cpp
)set_target_properties(runtimeloader PROPERTIESWIN32_EXECUTABLE TRUEMACOSX_BUNDLE TRUE
)target_link_libraries(runtimeloader PUBLICQt::CoreQt::GuiQt::QuickQt::Quick3D
)if(TARGET Qt::Widgets)target_compile_definitions(runtimeloader PUBLICHAS_MODULE_QT_WIDGETS)target_link_libraries(runtimeloader PUBLICQt::Widgets)
endif()qt_add_qml_module(runtimeloaderURI ExampleVERSION 1.0QML_FILES main.qmlNO_RESOURCE_TARGET_PATH
)install(TARGETS runtimeloaderBUNDLE  DESTINATION .RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)qt_generate_deploy_qml_app_script(TARGET runtimeloaderOUTPUT_SCRIPT deploy_scriptMACOS_BUNDLE_POST_BUILDNO_UNSUPPORTED_PLATFORM_ERRORDEPLOY_USER_QML_MODULES_ON_UNSUPPORTED_PLATFORM
)
install(SCRIPT ${deploy_script})

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/68074.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

以太坊入门【详解】

以太坊的组成部分 P2P网络&#xff1a;以太坊在以太坊网络上运行&#xff0c;该网络可在TCP端口30303上寻址&#xff0c;并运行一个协议。交易&#xff1a;以太坊交易时网络消息&#xff0c;其中包括发送者&#xff0c;接受者&#xff0c;值和数据的有效载荷以太坊虚拟机&…

实验十四 EL和JSTL

实验十四 EL和JSTL 一、实验目的 1、掌握EL表达式的使用 2、掌握JSTL的使用 二、实验过程 1、在数据库Book中建立表Tbook&#xff0c;包含图书ID&#xff0c;图书名称&#xff0c;图书价格。实现在bookQuery.jsp页面中模糊查询图书&#xff0c;如果图书的价格在50元以上&#…

安装和卸载RabbitMQ

我的飞书:https://rvg7rs2jk1g.feishu.cn/docx/SUWXdDb0UoCV86xP6b3c7qtMn6b 使用Ubuntu环境进行安装 一、安装Erlang 在安装RabbitMQ之前,我们需要先安装Erlang,RabbitMQ需要Erlang的语言支持 #安装Erlang sudo apt-get install erlang 在安装的过程中,会弹出一段信息,此…

音视频多媒体编解码器基础-codec

如果要从事编解码多媒体的工作&#xff0c;需要准备哪些更为基础的内容&#xff0c;这里帮你总结完。 因为数据类型不同所以编解码算法不同&#xff0c;分为图像、视频和音频三大类&#xff1b;因为流程不同&#xff0c;可以分为编码和解码两部分&#xff1b;因为编码器实现不…

ML基础-Jupyter notebook中的魔法命令

在 Jupyter Notebook 或 IPython 环境中&#xff0c;“魔法命令”&#xff08;Magic Commands&#xff09;是一些以百分号&#xff08;%&#xff09;或惊叹号&#xff08;!)开头的特殊命令&#xff0c;用于执行一些与代码运行环境相关的操作&#xff0c;而不仅仅是执行普通的 P…

【Unity2D 2022:UI】创建滚动视图

一、创建Scroll View游戏对象 在Canvas画布下新建Scroll View游戏对象 二、为Content游戏对象添加Grid Layout Group&#xff08;网格布局组&#xff09;组件 选中Content游戏物体&#xff0c;点击Add Competent添加组件&#xff0c;搜索Grid Layout Group组件 三、调整Grid La…

9-收纳的知识

[ComponentOf(typeof(xxx))]组件描述&#xff0c;表示是哪个实体的组件 [EntitySystemOf(typeof(xxx))] 系统描述 [Event(SceneType.Demo)] 定义事件&#xff0c;在指定场景的指定事件发生后触发 [ChildOf(typeof(ComputersComponent))] 标明是谁的子实体 [ResponseType(na…

数据库系统概念第六版记录 一

1.关系型数据库 关系型数据库&#xff08;Relational Database&#xff0c;简称 RDB&#xff09;是基于关系模型的一种数据库&#xff0c;它通过表格的形式来组织和存储数据。每个表由若干行&#xff08;记录&#xff09;和列&#xff08;字段&#xff09;组成&#xff0c;数据…

Vue前端开发-pinia之Actions插件

Store中的Actions部分&#xff0c;用于定义操作属性的方法&#xff0c;类似于组件中的methods部分&#xff0c;它与Getters都可以操作State属性&#xff0c;但在定义方法时&#xff0c;Getters是对State属性进行加工处理&#xff0c;再返回使用&#xff0c;属于内部计算;Action…

生成式AI安全最佳实践 - 抵御OWASP Top 10攻击 (下)

今天小李哥将开启全新的技术分享系列&#xff0c;为大家介绍生成式AI的安全解决方案设计方法和最佳实践。近年来生成式 AI 安全市场正迅速发展。据IDC预测&#xff0c;到2025年全球 AI 安全解决方案市场规模将突破200亿美元&#xff0c;年复合增长率超过30%&#xff0c;而Gartn…

一个开源 GenBI AI 本地代理(确保本地数据安全),使数据驱动型团队能够与其数据进行互动,生成文本到 SQL、图表、电子表格、报告和 BI

一、GenBI AI 代理介绍&#xff08;文末提供下载&#xff09; github地址&#xff1a;https://github.com/Canner/WrenAI 本文信息图片均来源于github作者主页 在 Wren AI&#xff0c;我们的使命是通过生成式商业智能 &#xff08;GenBI&#xff09; 使组织能够无缝访问数据&…

JAVA架构师进阶之路

JAVA架构师进阶之路 前言 苦于网络上充斥的各种java知识&#xff0c;多半是互相抄袭&#xff0c;导致很多后来者在学习java知识中味同嚼蜡&#xff0c;本人闲暇之余整理了进阶成为java架构师所必须掌握的核心知识点&#xff0c;后续会不断扩充。 废话少说&#xff0c;直接上正…

java程序员面试自身优缺点,详细说明

程序员面试大厂经常被问到的Java异常机制问题,你搞懂了吗运行时异常:运行时异常是可能被程序员避免的异常。与检查性相反,运行时异常可以在编译时被忽略。错误(ERROR):错误不是异常,而是脱离程序员控制的问题。错误通常在代码中容易被忽略。例如:当栈溢出时,一个错误就发生了,它…

C++六大默认成员函数

C六大默认成员函数 默认构造函数默认析构函数RAII技术RAII的核心思想优点示例应用场景 默认拷贝构造深拷贝和浅拷贝 默认拷贝赋值运算符移动构造函数&#xff08;C11起&#xff09;默认移动赋值运算符&#xff08;C11起&#xff09;取地址及const取地址操作符重载取地址操作符重…

防火墙的安全策略

1.VLAN 2属于办公区;VLAN 3属于生产区&#xff0c;创建时间段 [FW]ip address-set BG type object [FW-object-address-set-BG]address 192.168.1.0 mask 25 [FW]ip address-set SC type object [FW-object-address-set-SC]address 192.168.1.129 mask 25 [FW]ip address-se…

windows下搭建鸿蒙OS应用开发环境

一、前言 HUAWEI DevEco Studio 是华为推出的一款集成开发环境&#xff08;IDE&#xff09;&#xff0c;主要用于开发基于华为鸿蒙操作系统&#xff08;HarmonyOS&#xff09;的应用。作为华为开发者工具的核心之一&#xff0c;DevEco Studio 提供了一个多功能的开发平台&…

MacBook Pro(M1芯片)Qt环境配置

MacBook Pro&#xff08;M1芯片&#xff09;Qt环境配置 1、准备 试图写一个跨平台的桌面应用&#xff0c;此时想到了使用Qt&#xff0c;于是开始了搭建开发环境&#xff5e; 在M1芯片的电脑上安装&#xff0c;使用brew工具比较方便 Apple Silicon&#xff08;ARM/M1&#xf…

Sqlserver DBCC Check 遇到Msg 3853报错涉及sys.columns和sys.objects信息不匹配的解决方法

对数据库CacheDBMSIntl执行DBCC checkcatalog(‘CacheDBMSIntl’)时遇到报错如下 Msg 3853, Level 16, State 1, Line 7 Attribute (object_id1071830442) of row (object_id1071830442,column_id1) in sys.columns does not have a matching row (object_id1071830442) in sy…

VUE之组件通信(二)

1、v-model v-model的底层原理&#xff1a;是:value值和input事件的结合 $event到底是啥&#xff1f;啥时候能.target 对于原生事件&#xff0c;$event就是事件对象 &#xff0c;能.target对应自定义事件&#xff0c;$event就是触发事件时&#xff0c;所传递的数据&#xff…

P2036 [COCI 2008/2009 #2] PERKET(dfs)

#include<bits/stdc.h> using namespace std;int n; int a[15],b[15]; int ansINT_MAX; // 初始化最小差值为一个很大的数&#xff0c;保证能找到最小值void dfs(int i,int s,int k){if(in){ // 当遍历完所有元素时if(s1&&k0) return;int difabs(s-k);ans mi…