18#include <Qt3DRender/QParameter>
19#include <Qt3DRender/QRenderPass>
20#include <Qt3DRender/QTechnique>
21#include <Qt3DRender/QTexture>
22#include <Qt3DRender/QAbstractTexture>
23#include <Qt3DRender/QEffect>
24#include <Qt3DRender/QShaderProgramBuilder>
25#include <Qt3DRender/QGraphicsApiFilter>
28QgsMetalRoughMaterial::QgsMetalRoughMaterial( QNode *parent )
30 , mBaseColorParameter( new
Qt3DRender::QParameter( QStringLiteral(
"baseColor" ), QColor(
"grey" ), this ) )
31 , mMetalnessParameter( new
Qt3DRender::QParameter( QStringLiteral(
"metalness" ), 0.0f, this ) )
32 , mRoughnessParameter( new
Qt3DRender::QParameter( QStringLiteral(
"roughness" ), 0.0f, this ) )
33 , mBaseColorMapParameter( new
Qt3DRender::QParameter( QStringLiteral(
"baseColorMap" ), QVariant(), this ) )
34 , mMetalnessMapParameter( new
Qt3DRender::QParameter( QStringLiteral(
"metalnessMap" ), QVariant(), this ) )
35 , mRoughnessMapParameter( new
Qt3DRender::QParameter( QStringLiteral(
"roughnessMap" ), QVariant(), this ) )
36 , mAmbientOcclusionMapParameter( new
Qt3DRender::QParameter( QStringLiteral(
"ambientOcclusionMap" ), QVariant(), this ) )
37 , mNormalMapParameter( new
Qt3DRender::QParameter( QStringLiteral(
"normalMap" ), QVariant(), this ) )
38 , mTextureScaleParameter( new
Qt3DRender::QParameter( QStringLiteral(
"texCoordScale" ), 1.0f, this ) )
39 , mMetalRoughEffect( new
Qt3DRender::QEffect( this ) )
40 , mMetalRoughGL3Technique( new
Qt3DRender::QTechnique( this ) )
41 , mMetalRoughGL3RenderPass( new
Qt3DRender::QRenderPass( this ) )
42 , mMetalRoughGL3Shader( new
Qt3DRender::QShaderProgram( this ) )
43 , mFilterKey( new
Qt3DRender::QFilterKey( this ) )
48QgsMetalRoughMaterial::~QgsMetalRoughMaterial() =
default;
50QVariant QgsMetalRoughMaterial::baseColor()
const
52 return mBaseColorParameter->value();
55QVariant QgsMetalRoughMaterial::metalness()
const
57 return mMetalnessParameter->value();
60QVariant QgsMetalRoughMaterial::roughness()
const
62 return mRoughnessParameter->value();
65QVariant QgsMetalRoughMaterial::ambientOcclusion()
const
67 return mAmbientOcclusionMapParameter->value();
70QVariant QgsMetalRoughMaterial::normal()
const
72 return mNormalMapParameter->value();
75float QgsMetalRoughMaterial::textureScale()
const
77 return mTextureScaleParameter->value().toFloat();
80void QgsMetalRoughMaterial::setBaseColor(
const QVariant &baseColor )
82 mBaseColorParameter->setValue( baseColor );
83 mBaseColorMapParameter->setValue( baseColor );
84 bool oldUsingBaseColorMap = mUsingBaseColorMap;
86 if ( baseColor.value<Qt3DRender::QAbstractTexture *>() )
88 mUsingBaseColorMap =
true;
89 mMetalRoughEffect->addParameter( mBaseColorMapParameter );
90 if ( mMetalRoughEffect->parameters().contains( mBaseColorParameter ) )
91 mMetalRoughEffect->removeParameter( mBaseColorParameter );
95 mUsingBaseColorMap =
false;
96 if ( mMetalRoughEffect->parameters().contains( mBaseColorMapParameter ) )
97 mMetalRoughEffect->removeParameter( mBaseColorMapParameter );
98 mMetalRoughEffect->addParameter( mBaseColorParameter );
101 if ( oldUsingBaseColorMap != mUsingBaseColorMap )
102 updateFragmentShader();
105void QgsMetalRoughMaterial::setMetalness(
const QVariant &metalness )
107 mMetalnessParameter->setValue( metalness );
108 mMetalnessMapParameter->setValue( metalness );
109 bool oldUsingMetalnessMap = mUsingMetalnessMap;
111 if ( metalness.value<Qt3DRender::QAbstractTexture *>() )
113 mUsingMetalnessMap =
true;
114 mMetalRoughEffect->addParameter( mMetalnessMapParameter );
115 if ( mMetalRoughEffect->parameters().contains( mMetalnessParameter ) )
116 mMetalRoughEffect->removeParameter( mMetalnessParameter );
120 mUsingMetalnessMap =
false;
121 if ( mMetalRoughEffect->parameters().contains( mMetalnessMapParameter ) )
122 mMetalRoughEffect->removeParameter( mMetalnessMapParameter );
123 mMetalRoughEffect->addParameter( mMetalnessParameter );
126 if ( oldUsingMetalnessMap != mUsingMetalnessMap )
127 updateFragmentShader();
130void QgsMetalRoughMaterial::setRoughness(
const QVariant &roughness )
132 mRoughnessParameter->setValue( roughness );
133 mRoughnessMapParameter->setValue( roughness );
134 bool oldUsingRoughnessMap = mUsingRoughnessMap;
136 if ( roughness.value<Qt3DRender::QAbstractTexture *>() )
138 mUsingRoughnessMap =
true;
139 mMetalRoughEffect->addParameter( mRoughnessMapParameter );
140 if ( mMetalRoughEffect->parameters().contains( mRoughnessParameter ) )
141 mMetalRoughEffect->removeParameter( mRoughnessParameter );
145 mUsingRoughnessMap =
false;
146 if ( mMetalRoughEffect->parameters().contains( mRoughnessMapParameter ) )
147 mMetalRoughEffect->removeParameter( mRoughnessMapParameter );
148 mMetalRoughEffect->addParameter( mRoughnessParameter );
151 if ( oldUsingRoughnessMap != mUsingRoughnessMap )
152 updateFragmentShader();
155void QgsMetalRoughMaterial::setAmbientOcclusion(
const QVariant &ambientOcclusion )
157 mAmbientOcclusionMapParameter->setValue( ambientOcclusion );
158 bool oldUsingAmbientOcclusionMap = mUsingAmbientOcclusionMap;
160 if ( ambientOcclusion.value<Qt3DRender::QAbstractTexture *>() )
162 mUsingAmbientOcclusionMap =
true;
163 mMetalRoughEffect->addParameter( mAmbientOcclusionMapParameter );
167 mUsingAmbientOcclusionMap =
false;
168 if ( mMetalRoughEffect->parameters().contains( mAmbientOcclusionMapParameter ) )
169 mMetalRoughEffect->removeParameter( mAmbientOcclusionMapParameter );
172 if ( oldUsingAmbientOcclusionMap != mUsingAmbientOcclusionMap )
173 updateFragmentShader();
176void QgsMetalRoughMaterial::setNormal(
const QVariant &normal )
178 mNormalMapParameter->setValue( normal );
179 bool oldUsingNormalMap = mUsingNormalMap;
181 if ( normal.value<Qt3DRender::QAbstractTexture *>() )
183 mUsingNormalMap =
true;
184 mMetalRoughEffect->addParameter( mNormalMapParameter );
188 mUsingNormalMap =
false;
189 if ( mMetalRoughEffect->parameters().contains( mNormalMapParameter ) )
190 mMetalRoughEffect->removeParameter( mNormalMapParameter );
193 if ( oldUsingNormalMap != mUsingNormalMap )
194 updateFragmentShader();
197void QgsMetalRoughMaterial::setTextureScale(
float textureScale )
199 mTextureScaleParameter->setValue( textureScale );
202void QgsMetalRoughMaterial::init()
204 QObject::connect( mBaseColorParameter, &Qt3DRender::QParameter::valueChanged,
205 this, &QgsMetalRoughMaterial::baseColorChanged );
206 QObject::connect( mMetalnessParameter, &Qt3DRender::QParameter::valueChanged,
207 this, &QgsMetalRoughMaterial::metalnessChanged );
208 QObject::connect( mRoughnessParameter, &Qt3DRender::QParameter::valueChanged,
209 this, &QgsMetalRoughMaterial::roughnessChanged );
210 QObject::connect( mAmbientOcclusionMapParameter, &Qt3DRender::QParameter::valueChanged,
211 this, &QgsMetalRoughMaterial::ambientOcclusionChanged );
212 QObject::connect( mNormalMapParameter, &Qt3DRender::QParameter::valueChanged,
213 this, &QgsMetalRoughMaterial::normalChanged );
214 connect( mTextureScaleParameter, &Qt3DRender::QParameter::valueChanged,
215 this, &QgsMetalRoughMaterial::handleTextureScaleChanged );
217 mMetalRoughGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/default.vert" ) ) ) );
219 updateFragmentShader();
221 mMetalRoughGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
222 mMetalRoughGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
223 mMetalRoughGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
224 mMetalRoughGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
226 mFilterKey->setParent(
this );
227 mFilterKey->setName( QStringLiteral(
"renderingStyle" ) );
228 mFilterKey->setValue( QStringLiteral(
"forward" ) );
230 mMetalRoughGL3Technique->addFilterKey( mFilterKey );
231 mMetalRoughGL3RenderPass->setShaderProgram( mMetalRoughGL3Shader );
232 mMetalRoughGL3Technique->addRenderPass( mMetalRoughGL3RenderPass );
233 mMetalRoughEffect->addTechnique( mMetalRoughGL3Technique );
236 mBaseColorMapParameter->setParent( mMetalRoughEffect );
237 mMetalnessMapParameter->setParent( mMetalRoughEffect );
238 mRoughnessMapParameter->setParent( mMetalRoughEffect );
240 mMetalRoughEffect->addParameter( mBaseColorParameter );
241 mMetalRoughEffect->addParameter( mMetalnessParameter );
242 mMetalRoughEffect->addParameter( mRoughnessParameter );
243 mMetalRoughEffect->addParameter( mTextureScaleParameter );
245 setEffect( mMetalRoughEffect );
248void QgsMetalRoughMaterial::updateFragmentShader()
251 QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/metalrough.frag" ) ) );
253 if ( mUsingBaseColorMap )
254 defines +=
"BASE_COLOR_MAP";
255 if ( mUsingMetalnessMap )
256 defines +=
"METALNESS_MAP";
257 if ( mUsingRoughnessMap )
258 defines +=
"ROUGHNESS_MAP";
259 if ( mUsingAmbientOcclusionMap )
260 defines +=
"AMBIENT_OCCLUSION_MAP";
261 if ( mUsingNormalMap )
262 defines +=
"NORMAL_MAP";
265 defines +=
"FLAT_SHADING";
268 mMetalRoughGL3Shader->setFragmentShaderCode( finalShaderCode );
271void QgsMetalRoughMaterial::handleTextureScaleChanged(
const QVariant &var )
273 emit textureScaleChanged( var.toFloat() );
276bool QgsMetalRoughMaterial::flatShadingEnabled()
const
281void QgsMetalRoughMaterial::setFlatShadingEnabled(
bool enabled )
283 if ( enabled != mFlatShading )
285 mFlatShading = enabled;
286 updateFragmentShader();
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.