17#include "moc_qgsterrainentity.cpp"
33#include <Qt3DCore/QTransform>
34#include <Qt3DRender/QGeometryRenderer>
40class TerrainMapUpdateJobFactory :
public QgsChunkQueueJobFactory
43 TerrainMapUpdateJobFactory( QgsTerrainTextureGenerator *textureGenerator )
44 : mTextureGenerator( textureGenerator )
48 QgsChunkQueueJob *createJob( QgsChunkNode *chunk )
override
50 return new TerrainMapUpdateJob( mTextureGenerator, chunk );
54 QgsTerrainTextureGenerator *mTextureGenerator =
nullptr;
61QgsTerrainEntity::QgsTerrainEntity(
Qgs3DMapSettings *map, Qt3DCore::QNode *parent )
62 : QgsChunkedEntity( map, map->maxTerrainScreenError(), map->terrainGenerator(), false, std::numeric_limits<int>::max(), parent )
75 connectToLayersRepaintRequest();
77 mTextureGenerator =
new QgsTerrainTextureGenerator( *map );
79 mUpdateJobFactory.reset(
new TerrainMapUpdateJobFactory( mTextureGenerator ) );
81 mTerrainTransform =
new Qt3DCore::QTransform;
82 mTerrainTransform->setScale( 1.0f );
84 addComponent( mTerrainTransform );
87QgsTerrainEntity::~QgsTerrainEntity()
92 delete mTextureGenerator;
98 QVector<QgsRayCastingUtils::RayHit> result;
101 QVector3D intersectionPoint;
102 switch ( mMapSettings->terrainGenerator()->type() )
106 if ( ray.direction().z() == 0 )
109 const float dist =
static_cast<float>( mMapSettings->terrainElevationOffset() - ray.origin().z() ) / ray.direction().z();
110 const QVector3D terrainPlanePoint = ray.origin() + ray.direction() * dist;
112 if ( mMapSettings->extent().contains( mapCoords.
x(), mapCoords.
y() ) )
115 intersectionPoint = terrainPlanePoint;
121 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
122 for ( QgsChunkNode *node : activeNodes )
127 if ( node->entity() &&
129 QgsRayCastingUtils::rayBoxIntersection( ray, nodeBbox ) )
131 Qt3DRender::QGeometryRenderer *rend = node->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
132 auto *geom = rend->geometry();
133 Qt3DCore::QTransform *tr = node->entity()->findChild<Qt3DCore::QTransform *>();
134 QVector3D nodeIntPoint;
135 DemTerrainTileGeometry *demGeom =
static_cast<DemTerrainTileGeometry *
>( geom );
136 if ( demGeom->rayIntersection( ray, tr->matrix(), nodeIntPoint ) )
138 const float dist = ( ray.origin() - intersectionPoint ).length();
139 if ( minDist < 0 || dist < minDist )
142 intersectionPoint = nodeIntPoint;
155 if ( !intersectionPoint.isNull() )
158 result.append( hit );
163void QgsTerrainEntity::onShowBoundingBoxesChanged()
165 setShowBoundingBoxes( mMapSettings->showTerrainBoundingBoxes() );
169void QgsTerrainEntity::invalidateMapImages()
171 QgsEventTracing::addEvent( QgsEventTracing::Instant, QStringLiteral(
"3D" ), QStringLiteral(
"Invalidate textures" ) );
175 updateNodes( mActiveNodes, mUpdateJobFactory.get() );
179 QList<QgsChunkNode *> inactiveNodes;
180 const QList<QgsChunkNode *> descendants = mRootNode->descendants();
181 for ( QgsChunkNode *node : descendants )
183 if ( !node->entity() )
185 if ( mActiveNodes.contains( node ) )
187 inactiveNodes << node;
190 updateNodes( inactiveNodes, mUpdateJobFactory.get() );
192 setNeedsUpdate(
true );
195void QgsTerrainEntity::onLayersChanged()
197 connectToLayersRepaintRequest();
198 invalidateMapImages();
201void QgsTerrainEntity::connectToLayersRepaintRequest()
203 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
208 mLayers = mMapSettings->layers();
210 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
216void QgsTerrainEntity::onTerrainElevationOffsetChanged(
float newOffset )
218 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
221float QgsTerrainEntity::terrainElevationOffset()
const
223 return mMapSettings->terrainElevationOffset();
230TerrainMapUpdateJob::TerrainMapUpdateJob( QgsTerrainTextureGenerator *textureGenerator, QgsChunkNode *node )
231 : QgsChunkQueueJob( node )
232 , mTextureGenerator( textureGenerator )
234 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
235 connect( textureGenerator, &QgsTerrainTextureGenerator::tileReady,
this, &TerrainMapUpdateJob::onTileReady );
236 mJobId = textureGenerator->render( entity->textureImage()->imageExtent(), node->tileId(), entity->textureImage()->imageDebugText() );
239void TerrainMapUpdateJob::cancel()
242 mTextureGenerator->cancelJob( mJobId );
246void TerrainMapUpdateJob::onTileReady(
int jobId,
const QImage &image )
248 if ( mJobId == jobId )
250 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( mNode->entity() );
251 entity->textureImage()->setImage( image );
void backgroundColorChanged()
Emitted when the background color has changed.
void showTerrainBoundingBoxesChanged()
Emitted when the flag whether terrain's bounding boxes are shown has changed.
void terrainMapThemeChanged()
Emitted when terrain's map theme has changed.
float terrainElevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down)
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
void showLabelsChanged()
Emitted when the flag whether labels are displayed on terrain tiles has changed.
void terrainElevationOffsetChanged(float newElevation)
Emitted when the terrain elevation offset is changed.
void layersChanged()
Emitted when the list of map layers for 3d rendering has changed.
void showTerrainTilesInfoChanged()
Emitted when the flag whether terrain's tile info is shown has changed.
static QgsAABB mapToWorldExtent(const QgsRectangle &extent, double zMin, double zMax, const QgsVector3D &mapOrigin)
Converts map extent to axis aligned bounding box in 3D world coordinates.
static QgsVector3D worldToMapCoordinates(const QgsVector3D &worldCoords, const QgsVector3D &origin)
Converts 3D world coordinates to map coordinates (applies offset)
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
Base class for all map layer types.
void repaintRequested(bool deferredUpdate=false)
By emitting this signal the layer tells that either appearance or content have been changed and any v...
@ QuantizedMesh
Terrain is built from quantized mesh tiles.
@ Dem
Terrain is built from raster layer with digital elevation model.
@ Online
Terrain is built from downloaded tiles with digital elevation model.
@ Mesh
Terrain is built from mesh layer with z value on vertices.
@ Flat
The whole terrain is flat area.
virtual void setTerrain(QgsTerrainEntity *t)
Sets terrain entity for the generator (does not transfer ownership)
bool isValid() const
Returns whether the terrain generator is valid.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
double y() const
Returns Y coordinate.
double x() const
Returns X coordinate.
Helper struct to store ray casting parameters.
Helper struct to store ray casting results.