如何使用qml qt3d(qt)将对象旋转一个角度?
How to rotate the object about an angle using qml qt3d (qt)?
我想旋转 window 屏幕中的对象。我正在使用 Qt/QML/Qt3D.
我写了一些代码 here 来在对象 window 显示屏幕中添加一个按钮。借助此按钮,我可以将显示屏中的对象旋转大约(90 和 180)度。
QML 源代码:
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Scene3D 2.0
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0
ApplicationWindow
{
visible: true
width: 640
height: 480
title: qsTr("3D Viewer")
header: ToolBar
{
ToolButton
{
text: "Open 3D Model"
onPressed:
{
fileDialog.open()
}
}
}
FileDialog
{
id: fileDialog
onAccepted:
{
sceneLoader.source = fileDialog.fileUrl
}
}
Scene3D
{
anchors.fill: parent
aspects: ["input", "logic"]
cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
Entity
{
id: sceneRoot
Camera
{
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 30
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 10.0, 0.0, 0.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
OrbitCameraController
{
camera: camera
}
components: [
RenderSettings
{
activeFrameGraph: ForwardRenderer
{
clearColor: Qt.rgba(0, 0.5, 1, 1)
camera: camera
}
},
InputSettings
{
}
]
Entity
{
id: monkeyEntity
components: [
SceneLoader
{
id: sceneLoader
}
]
}
}
}
}
因此,主要问题是:什么(假设 Transform
组件?)以及应该在何处向该源文件添加代码以更改加载模型的角度?
这是一个如何在 Qt(不是 Qt3D)中执行此操作的示例:
https://doc.qt.io/qt-5/qml-qtquick-item.html#rotation-prop
Rectangle {
color: "red"
x: 25; y: 25; width: 50; height: 50
rotation: 30 // !
}
你也可以绕其他轴旋转,这里是关于它的信息:
https://doc.qt.io/qt-5/qml-qtquick-rotation.html
示例:
Rectangle {
width: 100; height: 100
color: "blue"
transform: Rotation {
origin.x: 25;
origin.y: 25;
axis { x: 0; y: 1; z: 0 };
angle: 45
}
}
更新。对于 Qt3D,至少使用 2.15 版 Qt3D.Core
,并使用 Transform
。这里是编辑你的代码:
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.2
import QtQuick.Scene3D 2.15
import Qt3D.Core 2.15
import Qt3D.Render 2.15
import Qt3D.Input 2.15
import Qt3D.Extras 2.15
ApplicationWindow
{
visible: true
width: 640
height: 480
title: qsTr("3D Viewer")
header: ToolBar
{
RowLayout {
ToolButton
{
text: "Open 3D Model"
onPressed:
{
fileDialog.open()
}
}
ToolButton
{
text: "Rotate"
onPressed:
{
transform.rotation = Qt.quaternion(1, 0.1, 0, 0);
}
}
}
}
FileDialog
{
id: fileDialog
onAccepted:
{
sceneLoader.source = fileDialog.fileUrl
}
}
Scene3D
{
anchors.fill: parent
aspects: ["input", "logic"]
cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
Entity
{
id: sceneRoot
Camera
{
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 30
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 10.0, 0.0, 0.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
OrbitCameraController
{
camera: camera
}
components: [
RenderSettings
{
activeFrameGraph: ForwardRenderer
{
clearColor: Qt.rgba(0, 0.5, 1, 1)
camera: camera
}
},
InputSettings
{
}
]
Entity
{
id: monkeyEntity
components: [
SceneLoader
{
id: sceneLoader
},
Transform {
id: transform
}
]
}
}
}
}
我想旋转 window 屏幕中的对象。我正在使用 Qt/QML/Qt3D.
我写了一些代码 here 来在对象 window 显示屏幕中添加一个按钮。借助此按钮,我可以将显示屏中的对象旋转大约(90 和 180)度。
QML 源代码:
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Scene3D 2.0
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0
ApplicationWindow
{
visible: true
width: 640
height: 480
title: qsTr("3D Viewer")
header: ToolBar
{
ToolButton
{
text: "Open 3D Model"
onPressed:
{
fileDialog.open()
}
}
}
FileDialog
{
id: fileDialog
onAccepted:
{
sceneLoader.source = fileDialog.fileUrl
}
}
Scene3D
{
anchors.fill: parent
aspects: ["input", "logic"]
cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
Entity
{
id: sceneRoot
Camera
{
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 30
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 10.0, 0.0, 0.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
OrbitCameraController
{
camera: camera
}
components: [
RenderSettings
{
activeFrameGraph: ForwardRenderer
{
clearColor: Qt.rgba(0, 0.5, 1, 1)
camera: camera
}
},
InputSettings
{
}
]
Entity
{
id: monkeyEntity
components: [
SceneLoader
{
id: sceneLoader
}
]
}
}
}
}
因此,主要问题是:什么(假设 Transform
组件?)以及应该在何处向该源文件添加代码以更改加载模型的角度?
这是一个如何在 Qt(不是 Qt3D)中执行此操作的示例: https://doc.qt.io/qt-5/qml-qtquick-item.html#rotation-prop
Rectangle {
color: "red"
x: 25; y: 25; width: 50; height: 50
rotation: 30 // !
}
你也可以绕其他轴旋转,这里是关于它的信息: https://doc.qt.io/qt-5/qml-qtquick-rotation.html
示例:
Rectangle {
width: 100; height: 100
color: "blue"
transform: Rotation {
origin.x: 25;
origin.y: 25;
axis { x: 0; y: 1; z: 0 };
angle: 45
}
}
更新。对于 Qt3D,至少使用 2.15 版 Qt3D.Core
,并使用 Transform
。这里是编辑你的代码:
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.2
import QtQuick.Scene3D 2.15
import Qt3D.Core 2.15
import Qt3D.Render 2.15
import Qt3D.Input 2.15
import Qt3D.Extras 2.15
ApplicationWindow
{
visible: true
width: 640
height: 480
title: qsTr("3D Viewer")
header: ToolBar
{
RowLayout {
ToolButton
{
text: "Open 3D Model"
onPressed:
{
fileDialog.open()
}
}
ToolButton
{
text: "Rotate"
onPressed:
{
transform.rotation = Qt.quaternion(1, 0.1, 0, 0);
}
}
}
}
FileDialog
{
id: fileDialog
onAccepted:
{
sceneLoader.source = fileDialog.fileUrl
}
}
Scene3D
{
anchors.fill: parent
aspects: ["input", "logic"]
cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
Entity
{
id: sceneRoot
Camera
{
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 30
aspectRatio: 16/9
nearPlane : 0.1
farPlane : 1000.0
position: Qt.vector3d( 10.0, 0.0, 0.0 )
upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
}
OrbitCameraController
{
camera: camera
}
components: [
RenderSettings
{
activeFrameGraph: ForwardRenderer
{
clearColor: Qt.rgba(0, 0.5, 1, 1)
camera: camera
}
},
InputSettings
{
}
]
Entity
{
id: monkeyEntity
components: [
SceneLoader
{
id: sceneLoader
},
Transform {
id: transform
}
]
}
}
}
}