Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dmaterial.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
9
10#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterial_p.h>
11#include <QtQuick3DRuntimeRender/private/qssgrendercustommaterial_p.h>
12#include <QtQuick3DUtils/private/qssgutils_p.h>
13
15
16/*!
17 \qmltype Material
18 \inherits Object3D
19 \inqmlmodule QtQuick3D
20 \brief Abstract base type providing functionality common to materials.
21*/
22
23/*!
24 \qmlproperty Texture Material::lightProbe
25
26 This property defines a Texture for overriding or setting an image based
27 lighting Texture for use with this material only.
28
29 \note Setting a light probe on the material will override the
30 \l {SceneEnvironment::lightProbe} {scene's light probe} for models using this material.
31
32 \note This property is ignored when Reflection Probe is used to show
33 reflections on the Model using this material because Reflection Probe uses
34 the \l {SceneEnvironment::lightProbe} {scene's light probe}.
35
36 \sa SceneEnvironment::lightProbe
37*/
38
39/*!
40 \qmlproperty enumeration Material::cullMode
41
42 This property defines whether primitive culling is enabled, and, when
43 enabled, which primitives are discarded.
44
45 The default value is Material.BackFaceCulling.
46
47 A triangle is considered front-facing if it has a counter-clockwise
48 winding, meaning its vertices in framebuffer coordinates are in
49 counter-clockwise order.
50
51 \value Material.BackFaceCulling Back-facing triangles are discarded.
52 \value Material.FrontFaceCulling Front-facing triangles are discarded.
53 \value Material.NoCulling No triangles are discarded.
54*/
55
56/*!
57 \qmlproperty enumeration Material::depthDrawMode
58
59 This property determines if and when depth rendering takes place for this
60 material. The default behavior when \l {SceneEnvironment::depthTestEnabled}
61 is set to \c true is that during the main render pass only opaque materials
62 will write to the depth buffer. This property makes it possible to change
63 this behavior to fine tune the rendering of a material.
64
65 The default value is Material.OqaqueOnlyDepthDraw
66
67 \value Material.OpaqueOnlyDepthDraw Depth rendering is only performed if
68 the material is opaque.
69 \value Material.AlwaysDepthDraw Depth rendering is always performed
70 regardless of the material type.
71 \value Material.NeverDepthDraw Depth rendering is never performed.
72 \value Material.OpaquePrePassDepthDraw Depth rendering is performed in a
73 separate depth pass, but only opaque values are written. This mode also
74 enables transparent materials to be used in combination with shadows.
75
76 \note If \l {SceneEnvironment::depthPrePassEnabled} is set to \c true then all
77 depth writes will take place as a result of the depth prepass, but it is
78 still necessary to explicitly set \c Material.OpaquePrePassDepthDraw to only
79 write the opaque fragments in the depth and shadow passes.
80
81*/
82
83
84QQuick3DMaterial::QQuick3DMaterial(QQuick3DObjectPrivate &dd, QQuick3DObject *parent)
85 : QQuick3DObject(dd, parent) {}
86
87QQuick3DMaterial::~QQuick3DMaterial()
88{
89}
90
91QQuick3DTexture *QQuick3DMaterial::lightProbe() const
92{
93 return m_iblProbe;
94}
95
96QQuick3DMaterial::CullMode QQuick3DMaterial::cullMode() const
97{
98 return m_cullMode;
99}
100
101QQuick3DMaterial::DepthDrawMode QQuick3DMaterial::depthDrawMode() const
102{
103 return m_depthDrawMode;
104}
105
106void QQuick3DMaterial::setLightProbe(QQuick3DTexture *iblProbe)
107{
108 if (m_iblProbe == iblProbe)
109 return;
110
111 QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DMaterial::setLightProbe, iblProbe, m_iblProbe);
112
113 m_iblProbe = iblProbe;
114 emit lightProbeChanged(m_iblProbe);
115 update();
116}
117
118void QQuick3DMaterial::setCullMode(QQuick3DMaterial::CullMode cullMode)
119{
120 if (m_cullMode == cullMode)
121 return;
122
123 m_cullMode = cullMode;
124 emit cullModeChanged(m_cullMode);
125 update();
126}
127
128void QQuick3DMaterial::setDepthDrawMode(QQuick3DMaterial::DepthDrawMode depthDrawMode)
129{
130 if (m_depthDrawMode == depthDrawMode)
131 return;
132
133 m_depthDrawMode = depthDrawMode;
134 emit depthDrawModeChanged(m_depthDrawMode);
135 update();
136}
137
138QSSGRenderGraphObject *QQuick3DMaterial::updateSpatialNode(QSSGRenderGraphObject *node)
139{
140 if (!node)
141 return nullptr;
142
143 QQuick3DObject::updateSpatialNode(node);
144
145 // Set the common properties
146 if (node->type == QSSGRenderGraphObject::Type::DefaultMaterial ||
147 node->type == QSSGRenderGraphObject::Type::PrincipledMaterial ||
148 node->type == QSSGRenderGraphObject::Type::SpecularGlossyMaterial) {
149 auto defaultMaterial = static_cast<QSSGRenderDefaultMaterial *>(node);
150
151 if (!m_iblProbe)
152 defaultMaterial->iblProbe = nullptr;
153 else
154 defaultMaterial->iblProbe = m_iblProbe->getRenderImage();
155
156 defaultMaterial->cullMode = QSSGCullFaceMode(m_cullMode);
157 defaultMaterial->depthDrawMode = QSSGDepthDrawMode(m_depthDrawMode);
158
159 DebugViewHelpers::ensureDebugObjectName(defaultMaterial, this);
160
161 node = defaultMaterial;
162 } else if (node->type == QSSGRenderGraphObject::Type::CustomMaterial) {
163 auto customMaterial = static_cast<QSSGRenderCustomMaterial *>(node);
164
165 if (!m_iblProbe)
166 customMaterial->m_iblProbe = nullptr;
167 else
168 customMaterial->m_iblProbe = m_iblProbe->getRenderImage();
169
170 customMaterial->m_cullMode = QSSGCullFaceMode(m_cullMode);
171 customMaterial->m_depthDrawMode = QSSGDepthDrawMode(m_depthDrawMode);
172
173 DebugViewHelpers::ensureDebugObjectName(customMaterial, this);
174
175 node = customMaterial;
176 }
177
178 return node;
179}
180
181void QQuick3DMaterial::itemChange(QQuick3DObject::ItemChange change, const QQuick3DObject::ItemChangeData &value)
182{
183 if (change == QQuick3DObject::ItemSceneChange)
184 updateSceneManager(value.sceneManager);
185}
186
187void QQuick3DMaterial::updateSceneManager(QQuick3DSceneManager *sceneManager)
188{
189 if (sceneManager) {
190 QQuick3DObjectPrivate::refSceneManager(m_iblProbe, *sceneManager);
191 } else {
192 QQuick3DObjectPrivate::derefSceneManager(m_iblProbe);
193 }
194}
195
196QT_END_NAMESPACE
Combined button and popup list for selecting options.