QtQuick3D实现相机的鼠标控制

方法一:将相机用Node封装起来,对Node进行旋转操作

在Qt的View3D中,Node是一个基本的3D对象,用于在场景中创建和组织其他3D对象。Node本身不可见,但可以用来组合其他3D对象,形成复杂的3D场景。

以下是Node的主要功能:

  1. 组合其他3D对象:Node可以包含其他3D对象,包括其他Node、Geometry、Material、Light等。可以通过Node的children属性来添加、删除、查询和遍历子对象。通过组合不同的子对象,可以创建复杂的3D场景。

  1. 变换Node和其子对象:Node具有位移、旋转和缩放等变换属性,可以通过设置position、rotation和scale属性来控制Node的变换。变换属性也可以应用于Node的所有子对象,从而实现整体变换。

  1. 定义Node的边界和形状:Node具有一个边界(Bounding Volume),用于确定Node的大小和形状。可以根据Node包含的子对象自动计算边界,也可以手动设置边界,例如使用BoundingSphere或BoundingBox等类。

  1. 定义Node的可见性和透明度:Node具有visible和opacity属性,用于控制Node及其子对象的可见性和透明度。可以通过设置这些属性来实现场景的隐藏、显示和淡入淡出等效果。

  1. 定义Node的选中状态和事件处理:Node具有enabled、selected和hovered等状态属性,可以用于实现Node的交互和事件处理。例如,可以在鼠标悬停在Node上时高亮显示Node,或者在鼠标单击Node时执行特定的操作。

总之,Node是Qt View3D中的基本3D对象,用于组合、变换和控制其他3D对象。通过使用Node及其子对象,可以创建复杂的3D场景,并实现交互和事件处理等功能。

依此方法的代码如下

import QtQuick
import QtQuick3D

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("My Monkey")

    View3D {
        id: view3D
        anchors.fill: parent

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

        MouseArea{
            id: mouse
            anchors.fill: parent
            property int cx: 0
            property int cy: 0
            onWheel:{//鼠标滚轮滚动
                if(wheel.angleDelta.y > 0){
                    camera.z -= 5//相机靠近,放大
                    console.log("wheel up")
                }
                else{
                    camera.z += 5//相机远离,缩小
                    console.log("wheel down")
                }
            }
            onPressed: {
                cx = mouse.x
                cy = mouse.y
                console.log("pressed at",cx,cy)
            }
            //相机的eulerRotation属性用于设置相机的旋转角度。
            //这个属性是一个包含三个值的向量,分别表示绕x轴、y轴和z轴的旋转角度(单位为度)。
            onPositionChanged: {//在鼠标或触摸指针位置发生变化时发射信号
                var intervalX = mouse.x-cx
                var intervalY = mouse.y-cy
                console.log("different",intervalX,intervalY)
                cameraNode.eulerRotation.y -= intervalX//绕y轴旋转
                cameraNode.eulerRotation.x -= intervalY//绕x轴旋转
                cx = mouse.x
                cy = mouse.y
            }
        }

        Model {
            id: suzanne
            source: "meshes/suzanne.mesh"

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

            eulerRotation.y: 30
            eulerRotation.x: -80

            materials: [ PrincipledMaterial {
                    baseColor: "yellow";
                    metalness: 0.8
                    roughness: 0.3
                } ]
        }

        Node {
            id:cameraNode
            PerspectiveCamera{
                id: camera
                z: 50
                //lookAtNode: suzanne
            }
        }
        DirectionalLight {
            eulerRotation.x: -20
            eulerRotation.y: 110
            castsShadow: true
        }
    }
}

方法二:利用Qt自带的相机控制器

OrbitCameraController --QML Type

Import Statement:

import QtQuick3D.Helpers

Inherits:

Item

Property Documentation

  • camera

camera : QtQuick3D::Camera

Specifies the camera node to control. This is a required property and should be a child of the origin node.

@@@camera

  • mouseEnabled

mouseEnabled : bool

Enables mouse controls. The default value is true.

@@@mouseEnabled

  • origin

origin : QtQuick3D::Node

Specifies the origin node to control. This is a required property and should be the parent of the camera

@@@origin

  • panEnabled

panEnabled : bool

Enables panning controls. The default value is true.

@@@panEnabled

  • xInvert

xInvert : bool

Inverts the x-axis controls. The default value is false.

@@@xInvert

  • xSpeed

xSpeed : QtQuick3D::Node

Specifies the speed of navigation when the mouse is moved along the X axis.

@@@xSpeed

  • yInvert

yInvert : bool

Inverts the y-axis controls. The default value is true.

@@@yInvert

  • ySpeed

ySpeed : QtQuick3D::Node

Specifies the speed of navigation when the mouse is moved along the Y axis.

部分代码如下

import QtQuick3D.Helpers

#...

View3D {
        id: view3D
        anchors.fill: parent

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

        Model {
            id: monkey
            source: "meshes/suzanne.mesh"

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

            eulerRotation.y: 30
            eulerRotation.x: -80

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

        PerspectiveCamera {
            id: camera
            z: 15
        }

        OrbitCameraController {
            camera: camera
            origin: monkey
            xInvert: true
            yInvert: false
        }

        DirectionalLight {//默认是从z轴正向投向负向
            eulerRotation.x: -20
            eulerRotation.y: 80

            castsShadow: true
        }

    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值