25 #include <Qt3DCore/QTransform>
26 #include <Qt3DRender/QMaterial>
27 #include <Qt3DExtras/QPhongMaterial>
29 #include <Qt3DExtras/QDiffuseMapMaterial>
30 #include <Qt3DRender/QAbstractTextureImage>
31 #include <Qt3DRender/QTexture>
33 #include <Qt3DRender/QEffect>
34 #include <Qt3DRender/QTechnique>
35 #include <Qt3DRender/QCullFace>
36 #include <Qt3DRender/QGeometryRenderer>
50 class QgsPolygon3DSymbolHandler :
public QgsFeature3DHandler
55 , mSelectedIds( selectedIds ) {}
57 bool prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
override;
58 void processFeature(
QgsFeature &feature,
const Qgs3DRenderContext &context )
override;
59 void finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
override;
66 std::unique_ptr<QgsTessellator> tessellator;
67 QVector<QgsFeatureId> triangleIndexFids;
68 QVector<uint> triangleIndexStartingIndices;
71 void processPolygon(
QgsPolygon *polyClone,
QgsFeatureId fid,
float height,
float extrusionHeight,
const Qgs3DRenderContext &context, PolygonData &out );
72 void makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &out,
bool selected );
73 Qt3DRender::QMaterial *material(
const QgsPolygon3DSymbol *symbol,
bool isSelected,
const Qgs3DRenderContext &context )
const;
76 std::unique_ptr< QgsPolygon3DSymbol > mSymbol;
81 PolygonData outNormal;
82 PolygonData outSelected;
84 QgsLineVertexData outEdges;
88 bool QgsPolygon3DSymbolHandler::prepare(
const Qgs3DRenderContext &context, QSet<QString> &attributeNames )
90 outEdges.withAdjacency =
true;
91 outEdges.init( mSymbol->altitudeClamping(), mSymbol->altitudeBinding(), 0, &context.map() );
95 outNormal.tessellator.reset(
new QgsTessellator( context.map().origin().x(), context.map().origin().y(),
true, mSymbol->invertNormals(), mSymbol->addBackFaces(),
false,
97 mSymbol->renderedFacade(),
98 texturedMaterialSettings ? texturedMaterialSettings->
textureRotation() : 0 ) );
99 outSelected.tessellator.reset(
new QgsTessellator( context.map().origin().x(), context.map().origin().y(),
true, mSymbol->invertNormals(),
100 mSymbol->addBackFaces(),
false,
102 mSymbol->renderedFacade(),
103 texturedMaterialSettings ? texturedMaterialSettings->
textureRotation() : 0 ) );
105 QSet<QString> attrs = mSymbol->dataDefinedProperties().referencedFields( context.expressionContext() );
106 attributeNames.unite( attrs );
110 void QgsPolygon3DSymbolHandler::processPolygon(
QgsPolygon *polyClone,
QgsFeatureId fid,
float height,
float extrusionHeight,
const Qgs3DRenderContext &context, PolygonData &out )
112 if ( mSymbol->edgesEnabled() )
119 if ( extrusionHeight )
123 outEdges.addLineString( *exterior, extrusionHeight + height );
124 outEdges.addVerticalLines( *exterior, extrusionHeight, height );
128 outEdges.addLineString( *interior, extrusionHeight + height );
129 outEdges.addVerticalLines( *interior, extrusionHeight, height );
136 Q_ASSERT( out.tessellator->dataVerticesCount() % 3 == 0 );
137 uint startingTriangleIndex =
static_cast<uint
>( out.tessellator->dataVerticesCount() / 3 );
138 out.triangleIndexStartingIndices.append( startingTriangleIndex );
139 out.triangleIndexFids.append( fid );
140 out.tessellator->addPolygon( *polyClone, extrusionHeight );
144 void QgsPolygon3DSymbolHandler::processFeature(
QgsFeature &f,
const Qgs3DRenderContext &context )
149 PolygonData &out = mSelectedIds.contains( f.
id() ) ? outSelected : outNormal;
163 float height = mSymbol->height();
164 float extrusionHeight = mSymbol->extrusionHeight();
167 if ( hasDDExtrusion )
170 if (
const QgsPolygon *poly = qgsgeometry_cast< const QgsPolygon *>( g ) )
173 processPolygon( polyClone, f.
id(), height, extrusionHeight, context, out );
175 else if (
const QgsMultiPolygon *mpoly = qgsgeometry_cast< const QgsMultiPolygon *>( g ) )
177 for (
int i = 0; i < mpoly->numGeometries(); ++i )
180 processPolygon( polyClone, f.
id(), height, extrusionHeight, context, out );
185 for (
int i = 0; i < gc->numGeometries(); ++i )
191 processPolygon( polyClone, f.
id(), height, extrusionHeight, context, out );
196 qDebug() <<
"not a polygon";
200 void QgsPolygon3DSymbolHandler::finalize( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context )
203 makeEntity( parent, context, outNormal,
false );
204 makeEntity( parent, context, outSelected,
true );
206 mZMin = std::min( outNormal.tessellator->zMinimum(), outSelected.tessellator->zMinimum() );
207 mZMax = std::max( outNormal.tessellator->zMaximum(), outSelected.tessellator->zMaximum() );
210 if ( mSymbol->edgesEnabled() && !outEdges.indexes.isEmpty() )
212 QgsLineMaterial *mat =
new QgsLineMaterial;
213 mat->setLineColor( mSymbol->edgeColor() );
214 mat->setLineWidth( mSymbol->edgeWidth() );
216 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
219 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
220 renderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
221 renderer->setGeometry( outEdges.createGeometry( entity ) );
222 renderer->setVertexCount( outEdges.indexes.count() );
223 renderer->setPrimitiveRestartEnabled(
true );
224 renderer->setRestartIndexValue( 0 );
227 entity->addComponent( renderer );
228 entity->addComponent( mat );
229 entity->setParent( parent );
234 void QgsPolygon3DSymbolHandler::makeEntity( Qt3DCore::QEntity *parent,
const Qgs3DRenderContext &context, PolygonData &out,
bool selected )
236 if ( out.tessellator->dataVerticesCount() == 0 )
239 Qt3DRender::QMaterial *mat = material( mSymbol.get(), selected, context );
242 QByteArray data( (
const char * )out.tessellator->data().constData(), out.tessellator->data().count() *
sizeof(
float ) );
243 int nVerts = data.count() / out.tessellator->stride();
249 geometry->
setData( data, nVerts, out.triangleIndexFids, out.triangleIndexStartingIndices );
251 Qt3DRender::QGeometryRenderer *renderer =
new Qt3DRender::QGeometryRenderer;
252 renderer->setGeometry( geometry );
255 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity;
256 entity->addComponent( renderer );
257 entity->addComponent( mat );
258 entity->setParent( parent );
261 entity->findChild<Qt3DRender::QGeometryRenderer *>()->setObjectName( QStringLiteral(
"main" ) );
276 return Qt3DRender::QCullFace::NoCulling;
282 auto techniques = material->effect()->techniques();
283 for (
auto tit = techniques.constBegin(); tit != techniques.constEnd(); ++tit )
285 auto renderPasses = ( *tit )->renderPasses();
286 for (
auto rpit = renderPasses.begin(); rpit != renderPasses.end(); ++rpit )
288 Qt3DRender::QCullFace *cullFace =
new Qt3DRender::QCullFace;
289 cullFace->setMode( _qt3DcullingMode( cullingMode ) );
290 ( *rpit )->addRenderState( cullFace );
295 Qt3DRender::QMaterial *QgsPolygon3DSymbolHandler::material(
const QgsPolygon3DSymbol *symbol,
bool isSelected,
const Qgs3DRenderContext &context )
const
301 Qt3DRender::QMaterial *material = symbol->
material()->
toMaterial( QgsMaterialSettingsRenderingTechnique::Triangles, materialContext );
302 applyCullingMode( symbol->
cullingMode(), material );
310 namespace Qgs3DSymbolImpl
317 if ( !polygonSymbol )
325 QgsFeature3DHandler *handler = handlerForPolygon3DSymbol( layer, &symbol );
326 Qt3DCore::QEntity *e = entityFromHandler( handler, map, layer );