17#include "moc_qgsmetalroughmaterial.cpp"
19#include <Qt3DRender/QParameter>
20#include <Qt3DRender/QRenderPass>
21#include <Qt3DRender/QTechnique>
22#include <Qt3DRender/QTexture>
23#include <Qt3DRender/QAbstractTexture>
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,
this, &QgsMetalRoughMaterial::baseColorChanged );
205 QObject::connect( mMetalnessParameter, &Qt3DRender::QParameter::valueChanged,
this, &QgsMetalRoughMaterial::metalnessChanged );
206 QObject::connect( mRoughnessParameter, &Qt3DRender::QParameter::valueChanged,
this, &QgsMetalRoughMaterial::roughnessChanged );
207 QObject::connect( mAmbientOcclusionMapParameter, &Qt3DRender::QParameter::valueChanged,
this, &QgsMetalRoughMaterial::ambientOcclusionChanged );
208 QObject::connect( mNormalMapParameter, &Qt3DRender::QParameter::valueChanged,
this, &QgsMetalRoughMaterial::normalChanged );
209 connect( mTextureScaleParameter, &Qt3DRender::QParameter::valueChanged,
this, &QgsMetalRoughMaterial::handleTextureScaleChanged );
211 mMetalRoughGL3Shader->setVertexShaderCode( Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/default.vert" ) ) ) );
213 updateFragmentShader();
215 mMetalRoughGL3Technique->graphicsApiFilter()->setApi( Qt3DRender::QGraphicsApiFilter::OpenGL );
216 mMetalRoughGL3Technique->graphicsApiFilter()->setMajorVersion( 3 );
217 mMetalRoughGL3Technique->graphicsApiFilter()->setMinorVersion( 1 );
218 mMetalRoughGL3Technique->graphicsApiFilter()->setProfile( Qt3DRender::QGraphicsApiFilter::CoreProfile );
220 mFilterKey->setParent(
this );
221 mFilterKey->setName( QStringLiteral(
"renderingStyle" ) );
222 mFilterKey->setValue( QStringLiteral(
"forward" ) );
224 mMetalRoughGL3Technique->addFilterKey( mFilterKey );
225 mMetalRoughGL3RenderPass->setShaderProgram( mMetalRoughGL3Shader );
226 mMetalRoughGL3Technique->addRenderPass( mMetalRoughGL3RenderPass );
227 mMetalRoughEffect->addTechnique( mMetalRoughGL3Technique );
230 mBaseColorMapParameter->setParent( mMetalRoughEffect );
231 mMetalnessMapParameter->setParent( mMetalRoughEffect );
232 mRoughnessMapParameter->setParent( mMetalRoughEffect );
234 mMetalRoughEffect->addParameter( mBaseColorParameter );
235 mMetalRoughEffect->addParameter( mMetalnessParameter );
236 mMetalRoughEffect->addParameter( mRoughnessParameter );
237 mMetalRoughEffect->addParameter( mTextureScaleParameter );
239 setEffect( mMetalRoughEffect );
242void QgsMetalRoughMaterial::updateFragmentShader()
245 QByteArray fragmentShaderCode = Qt3DRender::QShaderProgram::loadSource( QUrl( QStringLiteral(
"qrc:/shaders/metalrough.frag" ) ) );
247 if ( mUsingBaseColorMap )
248 defines +=
"BASE_COLOR_MAP";
249 if ( mUsingMetalnessMap )
250 defines +=
"METALNESS_MAP";
251 if ( mUsingRoughnessMap )
252 defines +=
"ROUGHNESS_MAP";
253 if ( mUsingAmbientOcclusionMap )
254 defines +=
"AMBIENT_OCCLUSION_MAP";
255 if ( mUsingNormalMap )
256 defines +=
"NORMAL_MAP";
259 defines +=
"FLAT_SHADING";
262 mMetalRoughGL3Shader->setFragmentShaderCode( finalShaderCode );
265void QgsMetalRoughMaterial::handleTextureScaleChanged(
const QVariant &var )
267 emit textureScaleChanged( var.toFloat() );
270bool QgsMetalRoughMaterial::flatShadingEnabled()
const
275void QgsMetalRoughMaterial::setFlatShadingEnabled(
bool enabled )
277 if ( enabled != mFlatShading )
279 mFlatShading = enabled;
280 updateFragmentShader();
static QByteArray addDefinesToShaderCode(const QByteArray &shaderCode, const QStringList &defines)
Inserts some define macros into a shader source code.