基础场景
QtQuick3D场景由几个标准元素组成
View3D是表示整个3D场景的顶级QML元素
SceneEnvironment控制场景的渲染方式,包括背景或天空盒的渲染方式
PerspectiveCamera是场景中的摄影机,也可以是OrthographicCamera,甚至是自定义投影矩阵的自定义相机
场景中包含三维空间中对象的模型实例和灯光
import QtQuick
import QtQuick.Window
import QtQuick3D
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
View3D{
anchors.fill: parent
environment: SceneEnvironment{
clearColor: "#222222"
backgroundMode: SceneEnvironment.Color
}
Model{
position:Qt.vector3d(0,0,0)
scale:Qt.vector3d(1,1.5,1)
source:"#Cone"
materials:[
PrincipledMaterial{baseColor: "yellow"}
]
}
Model{
position: Qt.vector3d(80,0,50)
source:"#Sphere"
materials: [
PrincipledMaterial{baseColor: "green"}
]
}
Model{
position:Qt.vector3d(-80,0,50)
source:"#Cube"
materials: [PrincipledMaterial{baseColor: "white"}]
}
Model{
position:Qt.vector3d(0,-50,0)
eulerRotation.x:-90
scale:Qt.vector3d(4,4,4)
source:"#Rectangle"
materials: [PrincipledMaterial{baseColor: "red"}]
}
DirectionalLight{
eulerRotation.x:-20
eulerRotation.y:100
castsShadow: true
}
PointLight{
position:Qt.vector3d(100,100,100)
castsShadow: true
}
SpotLight{
position:Qt.vector3d(50,200,50)
eulerRotation.x:-90
brightness: 5
ambientColor: Qt.rgba(0.1,0.1,0.1,1)//环境光
castsShadow: true //显示阴影
}
PerspectiveCamera{
position: Qt.vector3d(0,200,300)
Component.onCompleted: lookAt(Qt.vector3d(0,0,0))
}
}
}
执行后的效果图