25#include <Qt3DCore/QAttribute>
26#include <Qt3DCore/QBuffer>
27#include <Qt3DCore/QGeometry>
28#include <Qt3DRender/QEffect>
29#include <Qt3DRender/QGraphicsApiFilter>
30#include <Qt3DRender/QParameter>
31#include <Qt3DRender/QTechnique>
33using namespace Qt::StringLiterals;
53 return new QgsHighlightMaterial();
57 Q_ASSERT( phongSettings );
59 QgsPhongMaterial *material =
new QgsPhongMaterial();
60 material->setObjectName( u
"phongMaterial"_s );
70 material->setAmbient( ambient,
static_cast<float>( phongSettings->
ambientCoefficient() ) );
71 material->setDiffuse( diffuse,
static_cast<float>( phongSettings->
diffuseCoefficient() ) );
74 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
75 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
76 material->setDataDefinedEnabled( dataDefined );
91 Q_ASSERT( phongSettings );
93 QMap<QString, QString> parameters;
94 parameters[u
"Kd"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
diffuse().redF() ).arg( phongSettings->
diffuse().greenF() ).arg( phongSettings->
diffuse().blueF() );
95 parameters[u
"Ka"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
ambient().redF() ).arg( phongSettings->
ambient().greenF() ).arg( phongSettings->
ambient().blueF() );
96 parameters[u
"Ks"_s] = u
"%1 %2 %3"_s.arg( phongSettings->
specular().redF() ).arg( phongSettings->
specular().greenF() ).arg( phongSettings->
specular().blueF() );
97 parameters[u
"Ns"_s] = QString::number( phongSettings->
shininess() );
104 Q_ASSERT( phongSettings );
115 if ( diffuseCoefficient < 1 || ambientCoefficient < 1 || specularCoefficient < 1 )
119 array.resize(
sizeof(
float ) * 9 );
120 float *fptr =
reinterpret_cast<float *
>( array.data() );
122 *fptr++ =
static_cast<float>( diffuse.redF() * diffuseCoefficient );
123 *fptr++ =
static_cast<float>( diffuse.greenF() * diffuseCoefficient );
124 *fptr++ =
static_cast<float>( diffuse.blueF() * diffuseCoefficient );
126 *fptr++ =
static_cast<float>( ambient.redF() * ambientCoefficient );
127 *fptr++ =
static_cast<float>( ambient.greenF() * ambientCoefficient );
128 *fptr++ =
static_cast<float>( ambient.blueF() * ambientCoefficient );
130 *fptr++ =
static_cast<float>( specular.redF() * specularCoefficient );
131 *fptr++ =
static_cast<float>( specular.greenF() * specularCoefficient );
132 *fptr++ =
static_cast<float>( specular.blueF() * specularCoefficient );
136 array.resize(
sizeof(
unsigned char ) * 9 );
137 unsigned char *ptr =
reinterpret_cast<unsigned char *
>( array.data() );
139 *ptr++ =
static_cast<unsigned char>( diffuse.red() );
140 *ptr++ =
static_cast<unsigned char>( diffuse.green() );
141 *ptr++ =
static_cast<unsigned char>( diffuse.blue() );
143 *ptr++ =
static_cast<unsigned char>( ambient.red() );
144 *ptr++ =
static_cast<unsigned char>( ambient.green() );
145 *ptr++ =
static_cast<unsigned char>( ambient.blue() );
147 *ptr++ =
static_cast<unsigned char>( specular.red() );
148 *ptr++ =
static_cast<unsigned char>( specular.green() );
149 *ptr++ =
static_cast<unsigned char>( specular.blue() );
158 Q_ASSERT( phongSettings );
160 Qt3DCore::QBuffer *dataBuffer =
new Qt3DCore::QBuffer( geometry );
166 Qt3DCore::QAttribute *diffuseAttribute =
new Qt3DCore::QAttribute( geometry );
167 diffuseAttribute->setName( u
"dataDefinedDiffuseColor"_s );
168 diffuseAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
169 diffuseAttribute->setVertexSize( 3 );
170 diffuseAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
171 diffuseAttribute->setBuffer( dataBuffer );
172 diffuseAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
173 diffuseAttribute->setByteOffset( 0 );
174 diffuseAttribute->setCount( vertexCount );
175 geometry->addAttribute( diffuseAttribute );
177 Qt3DCore::QAttribute *ambientAttribute =
new Qt3DCore::QAttribute( geometry );
178 ambientAttribute->setName( u
"dataDefinedAmbiantColor"_s );
179 ambientAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
180 ambientAttribute->setVertexSize( 3 );
181 ambientAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
182 ambientAttribute->setBuffer( dataBuffer );
183 ambientAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
184 ambientAttribute->setByteOffset( 3 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
185 ambientAttribute->setCount( vertexCount );
186 geometry->addAttribute( ambientAttribute );
188 Qt3DCore::QAttribute *specularAttribute =
new Qt3DCore::QAttribute( geometry );
189 specularAttribute->setName( u
"dataDefinedSpecularColor"_s );
190 specularAttribute->setVertexBaseType( useFloats ? Qt3DCore::QAttribute::Float : Qt3DCore::QAttribute::UnsignedByte );
191 specularAttribute->setVertexSize( 3 );
192 specularAttribute->setAttributeType( Qt3DCore::QAttribute::VertexAttribute );
193 specularAttribute->setBuffer( dataBuffer );
194 specularAttribute->setByteStride( 9 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
195 specularAttribute->setByteOffset( 6 * ( useFloats ?
sizeof(
float ) :
sizeof(
unsigned char ) ) );
196 specularAttribute->setCount( vertexCount );
197 geometry->addAttribute( specularAttribute );
199 dataBuffer->setData( data );
206 QgsPhongMaterial *material = sceneRoot->findChild<QgsPhongMaterial *>();
207 if ( !material || material->objectName() !=
"phongMaterial"_L1 )
213 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
214 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
225 QgsPhongMaterial *material =
new QgsPhongMaterial();
226 material->setInstancingEnabled(
true, flags );
227 material->setInstancingMeshTransform( transform );
228 material->setObjectName( u
"phongMaterial"_s );
232 material->setAmbient( ambient,
static_cast<float>( phongSettings->
ambientCoefficient() ) );
233 material->setDiffuse( diffuse,
static_cast<float>( phongSettings->
diffuseCoefficient() ) );
235 material->setShininess(
static_cast<float>( phongSettings->
shininess() ) );
236 material->setOpacity(
static_cast<float>( phongSettings->
opacity() ) );
MaterialRenderingTechnique
Material rendering techniques.
@ Points
Point based rendering, requires point data.
@ Triangles
Triangle based rendering (default).
@ TrianglesFromModel
Triangle based rendering, using a model object source.
@ Lines
Line based rendering, requires line data.
@ Billboards
Flat billboard rendering.
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
@ InstancedPoints
Instanced based rendering, requiring triangles and point data.
@ TrianglesWithFixedTexture
Triangle based rendering, using a fixed, non-user-configurable texture (e.g. for terrain rendering).
QFlags< InstancedMaterialFlag > InstancedMaterialFlags
static QColor srgbToLinear(const QColor &color)
Converts a SRGB color to a linear color.
Abstract base class for material settings.
@ Specular
Specular color.
@ Diffuse
Diffuse color (phong material).
@ Ambient
Ambient color (phong material).
QgsPropertyCollection dataDefinedProperties() const
Returns the symbol material property collection, used for data defined overrides.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Context settings for a material.
QColor selectionColor() const
Returns the color for representing materials in a selected state.
bool isSelected() const
Returns true if the material should represent a selected state.
bool isHighlighted() const
Returns true if the material should represent a highlighted state.
Base class for all materials used within QGIS 3D views.
QByteArray dataDefinedVertexColorsAsByte(const QgsAbstractMaterialSettings *settings, const QgsExpressionContext &expressionContext) const override
Returns byte array corresponding to the data defined colors depending of the expressionContext,...
QgsMaterial * toInstancedMaterial(const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context, Qgis::InstancedMaterialFlags flags, const QMatrix4x4 &transform=QMatrix4x4()) const override
Creates a QgsMaterial for instanced point rendering.
void applyDataDefinedToGeometry(const QgsAbstractMaterialSettings *settings, Qt3DCore::QGeometry *geometry, int vertexCount, const QByteArray &data) const override
Applies the data defined bytes, dataDefinedBytes, on the geometry by filling a specific vertex buffer...
bool updatePreviewScene(Qt3DCore::QEntity *sceneRoot, const QgsAbstractMaterialSettings *settings, const QgsMaterialContext &context) const override
Updates an existing material preview scene with new material settings.
QMap< QString, QString > toExportParameters(const QgsAbstractMaterialSettings *settings) const override
Returns the parameters to be exported to .mtl file.
QgsMaterial * toMaterial(const QgsAbstractMaterialSettings *settings, Qgis::MaterialRenderingTechnique technique, const QgsMaterialContext &context) const override
Creates a new QgsMaterial object representing the material settings.
Basic shading material used for rendering based on the Phong shading model with three color component...
double ambientCoefficient() const
Returns the coefficient for the ambient color contribution (ie strength factor of the ambient color).
QColor diffuse() const
Returns diffuse color component.
double opacity() const
Returns the opacity of the surface.
QColor specular() const
Returns specular color component.
QColor ambient() const
Returns ambient color component.
double specularCoefficient() const
Returns the coefficient for the specular color contribution (ie strength factor of the specular color...
double shininess() const
Returns shininess of the surface.
double diffuseCoefficient() const
Returns the coefficient for the diffuse color contribution (ie strength factor of the diffuse color).
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.