SpotLight QML Type
在场景中定义一个聚光灯。更多...
| Import Statement: | import QtQuick3D |
| Inherits: |
属性
- coneAngle : real
- constantFade : real
- innerConeAngle : real
- linearFade : real
- quadraticFade : real
详细说明
聚光灯以锥形方式向一个方向发射光线,其锥形范围由coneAngle 属性定义。当光线接近coneAngle 时,光强会逐渐减弱。光强开始减弱的角度由innerConeAngle 定义。这两个角度均以度为单位。
在innerConeAngle 范围内,聚光灯的行为与点光源类似。 在此范围内,光强遵循反平方定律衰减。不过,可以通过constantFade 、linearFade 和quadraticFade 属性来控制光强的衰减(及范围)。光强衰减的计算公式如下:constantFade +distance * (linearFade * 0.01) +distance * (quadraticFade * 0.0001)^2
让我们看一个简单的例子。这里将一盏聚光灯(SpotLight)放置在Z轴的300处,即摄像机与场景中心的中点。默认情况下,光线沿Z轴方向发射。将brightness 值增加到10,使其看起来更像典型的聚光灯。
import QtQuick
import QtQuick3D
View3D {
anchors.fill: parent
PerspectiveCamera { z: 600 }
SpotLight {
z: 300
brightness: 10
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
}
Model {
source: "#Rectangle"
scale: Qt.vector3d(10, 10, 10)
z: -100
materials: PrincipledMaterial { }
}
Model {
source: "#Sphere"
scale: Qt.vector3d(2, 2, 2)
materials: PrincipledMaterial {
baseColor: "#40c060"
roughness: 0.1
}
}
}
旋转操作与DirectionalLight 类似。这里我们希望光线更多地向右侧照射,因此沿Y轴旋转-20度。通过将coneAngle 设置为30(而非默认的40),缩小了光锥范围。此外,我们将innerConeAngle 改为10,使光强更早开始衰减。
SpotLight {
z: 300
brightness: 10
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
eulerRotation.y: -20
coneAngle: 30
innerConeAngle: 10
}
更多使用示例,请参见Qt Quick 3D - Lights Example。
另请参阅 DirectionalLight 、PointLight 以及“阴影映射”。
属性文档
coneAngle : real
该属性定义了截止角(从边缘到边缘),超过该角度后,光线将不再影响场景。单位为度,取值范围在0 到180 之间。默认值为40 。
注意:当 锥角接近180 度时,阴影质量将开始下降。因此建议将该值设置为低于170 。
constantFade : real
该属性是光衰减项的一个常数因子。默认值为 1.0。
innerConeAngle : real
该属性定义了光线在接近coneAngle 时,光强开始逐渐减弱的角度(从边缘到边缘)。以0到180度之间的角度值表示。如果该值设置为大于coneAngle ,其行为将等同于coneAngle 。默认值为30。
linearFade : real
此属性会根据光源的距离,成比例地增加灯光效果的变暗速率。默认值为0.0 ,这意味着灯光不会呈现线性淡出效果。此处的数值在用于计算光线衰减之前,会先乘以0.01 。
quadraticFade : real
此属性会根据反平方定律,增加灯光效果调暗光线的速度。 默认值为 1.0,这意味着聚光灯的淡出效果完全遵循反平方定律,即当到物体的距离加倍时,光强会减弱至原来的 1/4。此处的数值在用于计算光衰减之前,会先乘以0.0001 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.