1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "sceneeffects_p.h"
5
6QT_BEGIN_NAMESPACE
7
8SceneEffectBase::SceneEffectBase(QQuick3DObject *parent)
9 : QQuick3DEffect(parent)
10{
11
12}
13
14QQuick3DSceneEnvironment *SceneEffectBase::environment() const
15{
16 return m_environment;
17}
18
19void SceneEffectBase::setEnvironment(QQuick3DSceneEnvironment *newEnvironment)
20{
21 if (m_environment == newEnvironment)
22 return;
23
24 if (auto env = qobject_cast<SceneEffectEnvironment *>(object: m_environment))
25 unregisterWithEnv(oldEnvironment: env);
26
27 m_environment = newEnvironment;
28
29 QQuick3DObjectPrivate::attachWatcher(context: this, setter: &SceneEffectBase::setEnvironment, newO: newEnvironment, oldO: m_environment);
30
31 if (auto env = qobject_cast<SceneEffectEnvironment *>(object: m_environment))
32 registerWithEnv(newEnvironment: env);
33
34 emit environmentChanged();
35}
36
37void SceneEffectBase::scheduleEnvUpdate()
38{
39 if (m_environment)
40 m_environment->update();
41}
42
43void SceneEffectBase::registerWithEnv(SceneEffectEnvironment *env)
44{
45 Q_UNUSED(env);
46 qWarning() << "We shouldn't be here!!!";
47}
48void SceneEffectBase::unregisterWithEnv(SceneEffectEnvironment *oldEnvironment)
49{
50 Q_UNUSED(oldEnvironment);
51 qWarning() << "We shouldn't be here!!!";
52}
53
54MainSceneEffect::MainSceneEffect(QQuick3DObject *p)
55 : SceneEffectBase(p)
56{
57
58}
59
60void MainSceneEffect::registerWithEnv(SceneEffectEnvironment *newEnvironment)
61{
62 if (newEnvironment)
63 newEnvironment->setMainSceneEffect(this);
64}
65
66void MainSceneEffect::unregisterWithEnv(SceneEffectEnvironment *oldEnvironment)
67{
68 if (oldEnvironment)
69 oldEnvironment->setMainSceneEffect(nullptr);
70}
71
72DepthOfFieldEffect::DepthOfFieldEffect(QQuick3DObject *p)
73 : SceneEffectBase(p)
74{
75
76}
77
78void DepthOfFieldEffect::registerWithEnv(SceneEffectEnvironment *newEnvironment)
79{
80 if (newEnvironment)
81 newEnvironment->setDeptOfFieldEffect(this);
82}
83
84void DepthOfFieldEffect::unregisterWithEnv(SceneEffectEnvironment *oldEnvironment)
85{
86 if (oldEnvironment)
87 oldEnvironment->setDeptOfFieldEffect(nullptr);
88}
89
90bool DepthOfFieldEffect::enabled() const
91{
92 return m_enabled;
93}
94
95void DepthOfFieldEffect::setEnabled(bool newEnabled)
96{
97 if (m_enabled == newEnabled)
98 return;
99 m_enabled = newEnabled;
100 scheduleEnvUpdate();
101 emit enabledChanged();
102}
103
104SceneEffectEnvironment::SceneEffectEnvironment(QQuick3DObject *p)
105 : QQuick3DSceneEnvironment(p)
106{
107}
108
109void SceneEffectEnvironment::setMainSceneEffect(MainSceneEffect *tonemapper)
110{
111 if (m_tonemapper == tonemapper)
112 return;
113
114 QQuick3DObjectPrivate::attachWatcher(context: this, setter: &SceneEffectEnvironment::setMainSceneEffect, newO: tonemapper, oldO: m_tonemapper);
115
116 m_tonemapper = tonemapper;
117}
118
119void SceneEffectEnvironment::setDeptOfFieldEffect(DepthOfFieldEffect *dof)
120{
121 if (m_dof == dof)
122 return;
123
124 QQuick3DObjectPrivate::attachWatcher(context: this, setter: &SceneEffectEnvironment::setDeptOfFieldEffect, newO: dof, oldO: m_dof);
125
126 m_dof = dof;
127}
128
129QSSGRenderGraphObject *SceneEffectEnvironment::updateSpatialNode(QSSGRenderGraphObject *node)
130{
131 m_effects = QQuick3DSceneEnvironment::effectList();
132 if (m_dof && m_dof->enabled())
133 m_effects.push_back(t: m_dof);
134 if (m_tonemapper)
135 m_effects.push_back(t: m_tonemapper);
136 node = QQuick3DSceneEnvironment::updateSpatialNode(node);
137
138 return node;
139}
140
141const QVector<QQuick3DEffect *> &SceneEffectEnvironment::effectList() const
142{
143 return m_effects;
144}
145
146bool SceneEffectEnvironment::useBuiltinTonemapper() const
147{
148 return false;
149}
150
151QT_END_NAMESPACE
152

source code of qtquick3d/src/helpers/impl/sceneeffects.cpp