23 #include <Qt3DCore/QTransform> 24 #include <Qt3DExtras/QPhongMaterial> 25 #include <Qt3DRender/QEffect> 26 #include <Qt3DRender/QTechnique> 27 #include <Qt3DRender/QCullFace> 28 #include <Qt3DRender/QGeometryRenderer> 37 class QgsPolygon3DSymbolHandler :
public QgsFeature3DHandler
41 : mSymbol( symbol ), mSelectedIds( selectedIds ) {}
43 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
44 void processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
45 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
52 QList<QgsPolygon *> polygons;
53 QList<QgsFeatureId> fids;
54 QList<float> extrusionHeightPerPolygon;
57 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &out,
bool selected );
66 PolygonData outNormal;
67 PolygonData outSelected;
71 bool QgsPolygon3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
74 attributeNames.unite( attrs );
78 void QgsPolygon3DSymbolHandler::processFeature(
QgsFeature &f,
const Qgs3DRenderContext &context )
83 PolygonData &out = mSelectedIds.contains( f.
id() ) ? outSelected : outNormal;
97 float height = mSymbol.height();
98 float extrusionHeight = mSymbol.extrusionHeight();
101 if ( hasDDExtrusion )
104 if (
const QgsPolygon *poly = qgsgeometry_cast< const QgsPolygon *>( g ) )
108 out.polygons.append( polyClone );
109 out.fids.append( f.
id() );
110 if ( hasDDExtrusion )
111 out.extrusionHeightPerPolygon.append( extrusionHeight );
113 else if (
const QgsMultiPolygon *mpoly = qgsgeometry_cast< const QgsMultiPolygon *>( g ) )
115 for (
int i = 0; i < mpoly->numGeometries(); ++i )
121 out.polygons.append( polyClone );
122 out.fids.append( f.
id() );
123 if ( hasDDExtrusion )
124 out.extrusionHeightPerPolygon.append( extrusionHeight );
128 qDebug() <<
"not a polygon";
132 void QgsPolygon3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
135 makeEntity( parent, context, outNormal,
false );
136 makeEntity( parent, context, outSelected,
true );
140 void QgsPolygon3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &out,
bool selected )
142 if ( out.polygons.isEmpty() )
145 Qt3DExtras::QPhongMaterial *mat = material( mSymbol );
149 mat->setDiffuse( context.map().selectionColor() );
150 mat->setAmbient( context.map().selectionColor().darker() );
153 QgsPointXY origin( context.map().origin().x(), context.map().origin().y() );
156 geometry->setAddBackFaces( mSymbol.addBackFaces() );
157 geometry->setPolygons( out.polygons, out.fids, origin, mSymbol.extrusionHeight(), out.extrusionHeightPerPolygon );
159 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
160 renderer->setGeometry( geometry );
163 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
164 entity->addComponent( renderer );
165 entity->addComponent( mat );
166 entity->setParent( parent );
169 entity->findChild<Qt3DRender::QGeometryRenderer *>()->setObjectName( QStringLiteral(
"main" ) );
182 return Qt3DRender::QCullFace::NoCulling;
185 Qt3DExtras::QPhongMaterial *QgsPolygon3DSymbolHandler::material(
const QgsPolygon3DSymbol &symbol )
const 187 Qt3DExtras::QPhongMaterial *material =
new Qt3DExtras::QPhongMaterial;
190 auto techniques = material->effect()->techniques();
191 for (
auto tit = techniques.constBegin(); tit != techniques.constEnd(); ++tit )
193 auto renderPasses = ( *tit )->renderPasses();
194 for (
auto rpit = renderPasses.begin(); rpit != renderPasses.end(); ++rpit )
196 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
197 cullFace->setMode( _qt3DcullingMode( symbol.
cullingMode() ) );
198 ( *rpit )->addRenderState( cullFace );
213 namespace Qgs3DSymbolImpl
224 QgsFeature3DHandler *handler = handlerForPolygon3DSymbol( layer, symbol );
225 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );
float shininess() const
Returns shininess of the surface.
QSet< QgsFeatureId > QgsFeatureIds
void setInvertNormals(bool invert)
Sets whether the normals of triangles will be inverted (useful for fixing clockwise / counter-clockwi...
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...
QColor specular() const
Returns specular color component.
A class to represent a 2D point.
3 3D symbol that draws polygon geometries as planar polygons, optionally extruded (with added walls)...
CullingMode
Triangle culling mode.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer's property collection, used for data defined overrides...
Extrusion height (zero means no extrusion)
A geometry is the spatial representation of a feature.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Will not render anything.
QgsPhongMaterialSettings material() const
Returns material used for shading of the symbol.
3 Definition of the world
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Will render only front faces of triangles (recommended when input data are consistent) ...
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Will render only back faces of triangles.
Will render both front and back faces of triangles.
3 Class derived from Qt3DRender::QGeometry that represents polygons tessellated into 3D geometry...
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Qgs3DTypes::CullingMode cullingMode() const
Returns front/back culling mode.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext()) const override
Returns the set of any fields referenced by the active properties from the collection.
Multi polygon geometry collection.
static bool isCurvedType(Type type)
Returns true if the WKB type is a curved type or can contain curved geometries.
QgsPolygon * clone() const override
Clones the geometry by performing a deep copy.
QColor ambient() const
Returns ambient color component.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
static void clampAltitudes(QgsLineString *lineString, Qgs3DTypes::AltitudeClamping altClamp, Qgs3DTypes::AltitudeBinding altBind, const QgsPoint ¢roid, float height, const Qgs3DMapSettings &map)
Clamps altitude of vertices of a linestring according to the settings.
QColor diffuse() const
Returns diffuse color component.
Represents a vector layer which manages a vector based data sets.
static Type flatType(Type type)
Returns the flat type for a WKB type.
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.