34#include <Qt3DExtras/QDiffuseMapMaterial>
35#include <Qt3DExtras/QPhongMaterial>
36#include <Qt3DRender/QAbstractTextureImage>
37#include <Qt3DRender/QCullFace>
38#include <Qt3DRender/QEffect>
39#include <Qt3DRender/QGeometryRenderer>
40#include <Qt3DRender/QTechnique>
41#include <Qt3DRender/QTexture>
46class QgsPolygon3DSymbolHandler :
public QgsFeature3DHandler
49 QgsPolygon3DSymbolHandler(
const QgsPolygon3DSymbol *symbol,
const QgsFeatureIds &selectedIds )
50 : mSymbol( static_cast<QgsPolygon3DSymbol *>( symbol->clone() ) )
51 , mSelectedIds( selectedIds ) {}
53 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames,
const QgsVector3D &chunkOrigin )
override;
54 void processFeature(
const QgsFeature &f,
const Qgs3DRenderContext &context )
override;
55 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
61 std::unique_ptr<QgsTessellator> tessellator;
62 QVector<QgsFeatureId> triangleIndexFids;
63 QVector<uint> triangleIndexStartingIndices;
64 QByteArray materialDataDefined;
67 void processPolygon(
const QgsPolygon *poly,
QgsFeatureId fid,
float offset,
float extrusionHeight,
const Qgs3DRenderContext &context, PolygonData &out );
68 void processMaterialDatadefined( uint verticesCount,
const QgsExpressionContext &context, PolygonData &out );
69 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &out,
bool selected );
70 QgsMaterial *material(
const QgsPolygon3DSymbol *symbol,
bool isSelected,
const Qgs3DRenderContext &context )
const;
73 std::unique_ptr<QgsPolygon3DSymbol> mSymbol;
78 QgsVector3D mChunkOrigin;
81 PolygonData outNormal;
82 PolygonData outSelected;
84 QgsLineVertexData outEdges;
90 outEdges.withAdjacency =
true;
91 outEdges.init( mSymbol->altitudeClamping(), mSymbol->altitudeBinding(), 0, context, chunkOrigin );
93 mChunkOrigin = chunkOrigin;
97 auto tessellator = std::make_unique<QgsTessellator>();
98 tessellator->setOrigin( chunkOrigin );
99 tessellator->setAddNormals(
true );
100 tessellator->setInvertNormals( mSymbol->invertNormals() );
101 tessellator->setBackFacesEnabled( mSymbol->addBackFaces() );
102 tessellator->setOutputZUp(
true );
103 tessellator->setExtrusionFaces( mSymbol->extrusionFaces() );
104 tessellator->setTextureRotation( texturedMaterialSettings ?
static_cast<float>( texturedMaterialSettings->
textureRotation() ) : 0.f );
106 tessellator->setOutputZUp(
true );
108 outNormal.tessellator = std::move( tessellator );
110 tessellator = std::make_unique<QgsTessellator>();
111 tessellator->setOrigin( chunkOrigin );
112 tessellator->setAddNormals(
true );
113 tessellator->setInvertNormals( mSymbol->invertNormals() );
114 tessellator->setBackFacesEnabled( mSymbol->addBackFaces() );
115 tessellator->setOutputZUp(
true );
116 tessellator->setExtrusionFaces( mSymbol->extrusionFaces() );
117 tessellator->setTextureRotation( texturedMaterialSettings ?
static_cast<float>( texturedMaterialSettings->
textureRotation() ) : 0.f );
119 tessellator->setOutputZUp(
true );
121 outSelected.tessellator = std::move( tessellator );
123 QSet<QString> attrs = mSymbol->dataDefinedProperties().referencedFields( context.
expressionContext() );
124 attributeNames.unite( attrs );
125 attrs = mSymbol->materialSettings()->dataDefinedProperties().referencedFields( context.
expressionContext() );
126 attributeNames.unite( attrs );
132 std::unique_ptr<QgsPolygon> polyClone( poly->
clone() );
134 const uint oldVerticesCount = out.tessellator->dataVerticesCount();
135 if ( mSymbol->edgesEnabled() )
138 outEdges.addLineString( *
static_cast<const QgsLineString *
>( polyClone->exteriorRing() ), offset );
139 for (
int i = 0; i < polyClone->numInteriorRings(); ++i )
140 outEdges.addLineString( *
static_cast<const QgsLineString *
>( polyClone->interiorRing( i ) ), offset );
142 if ( extrusionHeight )
146 outEdges.addLineString( *exterior, extrusionHeight + offset );
147 outEdges.addVerticalLines( *exterior, extrusionHeight, offset );
148 for (
int i = 0; i < polyClone->numInteriorRings(); ++i )
151 outEdges.addLineString( *interior, extrusionHeight + offset );
152 outEdges.addVerticalLines( *interior, extrusionHeight, offset );
159 Q_ASSERT( out.tessellator->dataVerticesCount() % 3 == 0 );
160 const uint startingTriangleIndex =
static_cast<uint
>( out.tessellator->dataVerticesCount() / 3 );
161 out.triangleIndexStartingIndices.append( startingTriangleIndex );
162 out.triangleIndexFids.append( fid );
163 out.tessellator->addPolygon( *polyClone, extrusionHeight );
164 if ( !out.tessellator->error().isEmpty() )
169 if ( mSymbol->materialSettings()->dataDefinedProperties().hasActiveProperties() )
170 processMaterialDatadefined( out.tessellator->dataVerticesCount() - oldVerticesCount, context.
expressionContext(), out );
173void QgsPolygon3DSymbolHandler::processMaterialDatadefined( uint verticesCount,
const QgsExpressionContext &context, QgsPolygon3DSymbolHandler::PolygonData &out )
175 const QByteArray bytes = mSymbol->materialSettings()->dataDefinedVertexColorsAsByte( context );
176 out.materialDataDefined.append( bytes.repeated( verticesCount ) );
184 PolygonData &out = mSelectedIds.contains( f.
id() ) ? outSelected : outNormal;
200 float offset = mSymbol->offset();
201 float extrusionHeight = mSymbol->extrusionHeight();
204 if ( hasDDExtrusion )
209 processPolygon( poly, f.
id(), offset, extrusionHeight, context, out );
213 for (
int i = 0; i < mpoly->numGeometries(); ++i )
215 const QgsPolygon *poly = mpoly->polygonN( i );
216 processPolygon( poly, f.
id(), offset, extrusionHeight, context, out );
221 for (
int i = 0; i < gc->numGeometries(); ++i )
227 processPolygon( poly, f.
id(), offset, extrusionHeight, context, out );
233 for (
int i = 0; i < polySurface->numPatches(); ++i )
235 const QgsPolygon *poly = polySurface->patchN( i );
236 processPolygon( poly, f.
id(), offset, extrusionHeight, context, out );
240 qWarning() <<
"not a polygon";
245void QgsPolygon3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
248 makeEntity( parent, context, outNormal,
false );
249 makeEntity( parent, context, outSelected,
true );
251 mZMin = std::min( outNormal.tessellator->zMinimum(), outSelected.tessellator->zMinimum() );
252 mZMax = std::max( outNormal.tessellator->zMaximum(), outSelected.tessellator->zMaximum() );
255 if ( mSymbol->edgesEnabled() && !outEdges.indexes.isEmpty() )
257 QgsLineMaterial *mat =
new QgsLineMaterial;
258 mat->setLineColor( mSymbol->edgeColor() );
259 mat->setLineWidth( mSymbol->edgeWidth() );
261 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
262 entity->setObjectName( parent->objectName() +
"_EDGES" );
265 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
266 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
267 renderer->setGeometry( outEdges.createGeometry( entity ) );
268 renderer->setVertexCount( outEdges.indexes.count() );
269 renderer->setPrimitiveRestartEnabled(
true );
270 renderer->setRestartIndexValue( 0 );
273 QgsGeoTransform *tr =
new QgsGeoTransform;
274 tr->setGeoTranslation( mChunkOrigin );
277 entity->addComponent( renderer );
278 entity->addComponent( mat );
279 entity->addComponent( tr );
280 entity->setParent( parent );
285void QgsPolygon3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &polyData,
bool selected )
287 if ( polyData.tessellator->dataVerticesCount() == 0 )
290 QgsMaterial *mat = material( mSymbol.get(), selected, context );
293 const QByteArray data(
reinterpret_cast<const char *
>( polyData.tessellator->data().constData() ),
static_cast<int>( polyData.tessellator->data().count() *
sizeof(
float ) ) );
294 const int nVerts = data.count() / polyData.tessellator->stride();
300 geometry->
setData( data, nVerts, polyData.triangleIndexFids, polyData.triangleIndexStartingIndices );
302 if ( mSymbol->materialSettings()->dataDefinedProperties().hasActiveProperties() )
303 mSymbol->materialSettings()->applyDataDefinedToGeometry( geometry, nVerts, polyData.materialDataDefined );
305 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
306 renderer->setGeometry( geometry );
309 QgsGeoTransform *tr =
new QgsGeoTransform;
310 tr->setGeoTranslation( mChunkOrigin );
313 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
314 entity->setObjectName( parent->objectName() +
"_CHUNK_MESH" );
315 entity->addComponent( renderer );
316 entity->addComponent( mat );
317 entity->addComponent( tr );
318 entity->setParent( parent );
330 const auto techniques = material->effect()->techniques();
331 for (
auto tit = techniques.constBegin(); tit != techniques.constEnd(); ++tit )
333 auto renderPasses = ( *tit )->renderPasses();
334 for (
auto rpit = renderPasses.begin(); rpit != renderPasses.end(); ++rpit )
336 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
338 ( *rpit )->addRenderState( cullFace );
349 const bool dataDefined = mSymbol->materialSettings()->dataDefinedProperties().hasActiveProperties();
351 applyCullingMode( symbol->
cullingMode(), material );
359namespace Qgs3DSymbolImpl
363 QgsFeature3DHandler *handlerForPolygon3DSymbol( QgsVectorLayer *layer,
const QgsAbstract3DSymbol *symbol )
365 const QgsPolygon3DSymbol *polygonSymbol =
dynamic_cast<const QgsPolygon3DSymbol *
>( symbol );
366 if ( !polygonSymbol )
Rendering context for preparation of 3D entities.
QColor selectionColor() const
Returns color used for selected features.
QgsExpressionContext & expressionContext()
Gets the expression context.
@ Main3DRenderer
Renderer for normal entities.
static const char * PROP_NAME_3D_RENDERER_FLAG
Qt property name to hold the 3D geometry renderer flag.
CullingMode
Triangle culling mode.
static Qt3DRender::QCullFace::CullingMode qt3DcullingMode(Qgs3DTypes::CullingMode mode)
Converts Qgs3DTypes::CullingMode mode into its Qt3D equivalent.
static void clampAltitudes(QgsLineString *lineString, Qgis::AltitudeClamping altClamp, Qgis::AltitudeBinding altBind, const QgsPoint ¢roid, float offset, const Qgs3DRenderContext &context)
Clamps altitude of vertices of a linestring according to the settings.
@ ExtrusionHeight
Extrusion height (zero means no extrusion).
@ Height
Height (altitude).
Abstract base class for all geometries.
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
virtual QgsMaterial * toMaterial(QgsMaterialSettingsRenderingTechnique technique, const QgsMaterialContext &context) const =0
Creates a new QgsMaterial object representing the material settings.
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Line string geometry type, with support for z-dimension and m-values.
Context settings for a material.
void setIsSelected(bool isSelected)
Sets whether the material should represent a selected state.
void setSelectionColor(const QColor &color)
Sets the color for representing materials in a selected state.
Base class for all materials used within QGIS 3D views.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Multi polygon geometry collection.
A Phong shading model with diffuse texture map.
bool requiresTextureCoordinates() const
Returns true if the material requires texture coordinates to be generated during triangulation....
double textureRotation() const
Returns the texture rotation, in degrees.
3D symbol that draws polygon geometries as planar polygons, optionally extruded (with added walls).
QgsAbstractMaterialSettings * materialSettings() const
Returns material settings used for shading of the symbol.
Qgs3DTypes::CullingMode cullingMode() const
Returns front/back culling mode.
QgsPolygon * clone() const override
Clones the geometry by performing a deep copy.
Polyhedral surface geometry type.
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.
Qt3DRender::QGeometry subclass that represents polygons tessellated into 3D geometry.
void setData(const QByteArray &vertexBufferData, int vertexCount, const QVector< QgsFeatureId > &triangleIndexFids, const QVector< uint > &triangleIndexStartingIndices)
Initializes vertex buffer (and other members) from data that were already tessellated.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
static Q_INVOKABLE bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
@ Triangles
Triangle based rendering (default).
@ TrianglesDataDefined
Triangle based rendering with possibility of datadefined color.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features