1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qquick3ddirectionallight_p.h" |
5 | #include "qquick3dobject_p.h" |
6 | |
7 | #include <QtQuick3DRuntimeRender/private/qssgrenderlight_p.h> |
8 | |
9 | #include "qquick3dnode_p_p.h" |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | /*! |
14 | \qmltype DirectionalLight |
15 | \inherits Light |
16 | \inqmlmodule QtQuick3D |
17 | \brief Defines a directional light in the scene. |
18 | |
19 | The directional light emits light in one direction from an unidentifiable source located |
20 | infinitely far away. This is similar to the way sunlight works in real life. A directional |
21 | light has infinite range and does not diminish. |
22 | |
23 | If \l {Light::castsShadow}{castsShadow} is enabled, shadows will be parallel to the light |
24 | direction. |
25 | |
26 | A directional light effectively have no position, so moving it does not |
27 | have any effect. The light will always be emitted in the direction of the |
28 | light's Z axis. |
29 | |
30 | Rotating the light along its X or Y axis will change the direction of the light emission. |
31 | |
32 | Scaling a directional light will only have an effect in the following cases: |
33 | \list |
34 | \li If Z scale is set to a negative number, the light will be emitted in the opposite direction. |
35 | \li If the scale of any axis is set to 0, the light will be emitted along the world's Z axis. |
36 | \note Rotating the light will then have no effect. |
37 | \endlist |
38 | |
39 | Let's look at a simple example: |
40 | |
41 | \qml |
42 | import QtQuick |
43 | import QtQuick3D |
44 | View3D { |
45 | anchors.fill: parent |
46 | |
47 | PerspectiveCamera { z: 600 } |
48 | |
49 | DirectionalLight { |
50 | } |
51 | |
52 | Model { |
53 | source: "#Sphere" |
54 | scale: Qt.vector3d(4, 4, 4) |
55 | materials: PrincipledMaterial { |
56 | baseColor: "#40c060" |
57 | roughness: 0.1 // make specular highlight visible |
58 | } |
59 | } |
60 | } |
61 | \endqml |
62 | |
63 | Here the DirectionalLight uses the default \c white color, emitting in the |
64 | direction of the DirectionalLight node's Z axis. |
65 | |
66 | \image directionallight-1.png |
67 | |
68 | Rotating 60 degrees around the X axis would lead to the following. Instead |
69 | of emitting straight in the direction of the Z axis, the light is now |
70 | pointing 60 degrees "down": |
71 | |
72 | \qml |
73 | DirectionalLight { |
74 | eulerRotation.x: 30 |
75 | } |
76 | \endqml |
77 | |
78 | \image directionallight-2.png |
79 | |
80 | For further usage examples, see \l{Qt Quick 3D - Lights Example}. |
81 | |
82 | \sa PointLight, SpotLight |
83 | */ |
84 | |
85 | QQuick3DDirectionalLight::QQuick3DDirectionalLight(QQuick3DNode *parent) |
86 | : QQuick3DAbstractLight(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::DirectionalLight)), parent) {} |
87 | |
88 | QSSGRenderGraphObject *QQuick3DDirectionalLight::updateSpatialNode(QSSGRenderGraphObject *node) |
89 | { |
90 | if (!node) { |
91 | markAllDirty(); |
92 | node = new QSSGRenderLight(/* defaults to directional */); |
93 | } |
94 | |
95 | QQuick3DAbstractLight::updateSpatialNode(node); // Marks the light node dirty if m_dirtyFlags != 0 |
96 | |
97 | return node; |
98 | } |
99 | |
100 | QT_END_NAMESPACE |
101 | |