#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实现球体和光源转动效果。