我想在窗口屏幕上旋转这个物体。我正在使用Qt/QML/Qt3D。
我编写了一些代码这里来在对象窗口显示屏幕中添加一个按钮。在这个按钮的帮助下,我可以将显示屏幕中的物体旋转(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
组件?)在这个源文件中应该添加代码以改变加载模型的角度?
发布于 2020-07-28 08:55:42
下面是如何在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
}
]
}
}
}
}
https://stackoverflow.com/questions/63130177
复制相似问题