36#include <Qt3DCore/QTransform>
37#include <Qt3DRender/QGeometryRenderer>
39#include "moc_qgsterrainentity.cpp"
41using namespace Qt::StringLiterals;
46class TerrainMapUpdateJobFactory :
public QgsChunkQueueJobFactory
49 TerrainMapUpdateJobFactory( QgsTerrainTextureGenerator *textureGenerator )
50 : mTextureGenerator( textureGenerator )
53 QgsChunkQueueJob *createJob( QgsChunkNode *chunk )
override {
return new TerrainMapUpdateJob( mTextureGenerator, chunk ); }
56 QgsTerrainTextureGenerator *mTextureGenerator =
nullptr;
63QgsTerrainEntity::QgsTerrainEntity(
Qgs3DMapSettings *map, Qt3DCore::QNode *parent )
64 : QgsChunkedEntity( map, map->terrainSettings()->maximumScreenError(), map->terrainGenerator(), false, std::numeric_limits<int>::max(), parent )
70 connect( mLayerWatcher.get(), &QgsLayerStyleWatcher::styleChanged,
this, &QgsTerrainEntity::invalidateMapImages );
79 mTextureGenerator = std::make_unique<QgsTerrainTextureGenerator>( *map );
81 mUpdateJobFactory = std::make_unique<TerrainMapUpdateJobFactory>( mTextureGenerator.get() );
83 mTerrainTransform =
new Qt3DCore::QTransform;
84 mTerrainTransform->setScale( 1.0f );
86 addComponent( mTerrainTransform );
89QgsTerrainEntity::~QgsTerrainEntity()
98 QList<QgsRayCastHit> result;
102 switch ( mMapSettings->terrainGenerator()->type() )
109 const float dist =
static_cast<float>( mMapSettings->terrainSettings()->elevationOffset() - ray.
origin().z() - mMapSettings->origin().z() ) / ray.
direction().z();
110 const QVector3D terrainPlanePoint = ray.
point( dist );
112 if ( mMapSettings->extent().contains( mapCoords.
x(), mapCoords.
y() ) )
115 intersectionPointMapCoords = mapCoords;
121 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
122 QVector3D nearestIntersectionPoint;
123 for ( QgsChunkNode *node : activeNodes )
129 Qt3DRender::QGeometryRenderer *rend = node->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
130 auto *geom = rend->geometry();
131 Qt3DCore::QTransform *tr = node->entity()->findChild<Qt3DCore::QTransform *>();
132 QVector3D nodeIntPoint;
133 DemTerrainTileGeometry *demGeom =
static_cast<DemTerrainTileGeometry *
>( geom );
134 if ( demGeom->rayIntersection( ray, context, tr->matrix(), nodeIntPoint ) )
136 const float dist = ( ray.
origin() - nodeIntPoint ).length();
137 if ( minDist < 0 || dist < minDist )
140 nearestIntersectionPoint = nodeIntPoint;
160 result.append( hit );
165void QgsTerrainEntity::onShowBoundingBoxesChanged()
170void QgsTerrainEntity::invalidateMapImages()
172 QgsEventTracing::addEvent( QgsEventTracing::Instant, u
"3D"_s, u
"Invalidate textures"_s );
176 updateNodes( mActiveNodes, mUpdateJobFactory.get() );
180 QList<QgsChunkNode *> inactiveNodes;
181 const QList<QgsChunkNode *> descendants = mRootNode->descendants();
182 for ( QgsChunkNode *node : descendants )
184 if ( !node->entity() )
186 if ( mActiveNodes.contains( node ) )
188 inactiveNodes << node;
191 updateNodes( inactiveNodes, mUpdateJobFactory.get() );
193 setNeedsUpdate(
true );
196void QgsTerrainEntity::onTerrainElevationOffsetChanged()
198 float newOffset = qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset();
199 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
202float QgsTerrainEntity::terrainElevationOffset()
const
204 return mMapSettings->terrainSettings()->elevationOffset();
211TerrainMapUpdateJob::TerrainMapUpdateJob( QgsTerrainTextureGenerator *textureGenerator, QgsChunkNode *node )
212 : QgsChunkQueueJob( node )
213 , mTextureGenerator( textureGenerator )
216void TerrainMapUpdateJob::start()
218 QgsChunkNode *node = chunk();
220 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( node->entity() );
221 connect( mTextureGenerator, &QgsTerrainTextureGenerator::tileReady,
this, &TerrainMapUpdateJob::onTileReady );
222 mJobId = mTextureGenerator->render( entity->textureImage()->imageExtent(), node->tileId(), entity->textureImage()->imageDebugText() );
225void TerrainMapUpdateJob::cancel()
228 mTextureGenerator->cancelJob( mJobId );
232void TerrainMapUpdateJob::onTileReady(
int jobId,
const QImage &image )
234 if ( mJobId == jobId )
236 QgsTerrainTileEntity *entity = qobject_cast<QgsTerrainTileEntity *>( mNode->entity() );
237 entity->textureImage()->setImage( image );
@ ShowTerrainBoundingBoxes
Displays bounding boxes of terrain tiles.
void backgroundColorChanged()
Emitted when the background color has changed.
void showTerrainBoundingBoxesChanged()
Emitted when the flag whether terrain's bounding boxes are shown has changed.
const QgsAbstractTerrainSettings * terrainSettings() const
Returns the terrain settings.
void terrainMapThemeChanged()
Emitted when terrain's map theme has changed.
QgsTerrainGenerator * terrainGenerator() const
Returns the terrain generator.
void terrainSettingsChanged()
Emitted when the terrain settings are changed.
void showLabelsChanged()
Emitted when the flag whether labels are displayed on terrain tiles 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).
Axis-aligned bounding box - in world coords.
float distanceFromPoint(float x, float y, float z) const
Returns shortest distance from the box to a point.
double elevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down).
A representation of a ray in 3D.
QVector3D origin() const
Returns the origin of the ray.
QVector3D direction() const
Returns the direction of the ray see setDirection().
QVector3D point(float distance) const
Returns the point along the ray with the specified distance from the ray's origin.
Responsible for defining parameters of the ray casting operations in 3D map canvases.
Contains details about the ray intersecting entities when ray casting in a 3D map canvas.
void setMapCoordinates(const QgsVector3D &point)
Sets the hit point position in 3d map coordinates.
void setDistance(double distance)
Sets the hit's distance from the ray's origin.
@ 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.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
double y() const
Returns Y coordinate.
double x() const
Returns X coordinate.
bool rayBoxIntersection(const QgsRay3D &ray, const QgsAABB &nodeBbox)
Tests whether an axis aligned box is intersected by a ray.
constexpr QObjectUniquePtr< Tp > make_qobject_unique(Args &&...args)
Create an object owned by a QObjectUniquePtr.