Qt6.9 Qt Quick 3D实现球体示例

#Qt6.9

import QtQuick
import QtQuick3D
import QtQuick3D.Helpers

Window {
    width: 800
    height: 600
    visible: true
    title: qsTr("旋转光源示例")

    View3D {
        anchors.fill: parent

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

        PerspectiveCamera {
            position: Qt.vector3d(0, 200, 600)
        }

        // 球体模型
        Model {
            source: "#Sphere"
            scale: Qt.vector3d(2, 2, 2)
            position: Qt.vector3d(0, 200, 0)
            materials: PrincipledMaterial {
                baseColor: "blue"
                roughness: 0.1
                metalness: 0.5
                specularAmount: 1.0
            }
        }

        // 方向光源 - 绕Y轴旋转
        DirectionalLight {
            id: dirLight
            color: "white"
            brightness: 1
            eulerRotation.y: 0

            // Y轴旋转动画
            NumberAnimation on eulerRotation.y {
                from: 0
                to: 360
                duration: 10000
                loops: Animation.Infinite
                running: true
            }
        }

        // 辅助显示光源方向
        Model {
            source: "#Sphere"

            scale: Qt.vector3d(0.1, 0.1, 0.1)
            materials: DefaultMaterial {
                diffuseColor: "red"
                //emissiveColor: "red"
                emissiveFactor: 1.0
            }
            // 跟随光源方向
            position: Qt.vector3d(
                Math.sin(dirLight.eulerRotation.y * Math.PI / 180) * 300,
                200,
                Math.cos(dirLight.eulerRotation.y * Math.PI / 180) * 300
            )
        }

        // 坐标轴辅助
        DebugView {
            source: parent
        }
    }
}

使用Qt Quick 3D model实现球体和光源转动效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值