34#include <Qt3DCore/QTransform>
35#include <Qt3DRender/QGeometryRenderer>
37#include "moc_qgsterrainentity.cpp"
39using namespace Qt::StringLiterals;
43QgsTerrainEntity::QgsTerrainEntity(
Qgs3DMapSettings *map, Qt3DCore::QNode *parent )
44 : QgsChunkedEntity( map, map->terrainSettings()->maximumScreenError(), map->terrainGenerator(), false, std::numeric_limits<int>::max(), parent )
50 connect( mLayerWatcher.get(), &QgsLayerStyleWatcher::styleChanged,
this, &QgsTerrainEntity::invalidateMapImages );
59 mTextureGenerator = std::make_unique<QgsTerrainTextureGenerator>( *map );
61 mTerrainTransform =
new Qt3DCore::QTransform;
62 mTerrainTransform->setScale( 1.0f );
64 addComponent( mTerrainTransform );
67QgsTerrainEntity::~QgsTerrainEntity()
76 QList<QgsRayCastHit> result;
80 switch ( mMapSettings->terrainGenerator()->type() )
87 const float dist =
static_cast<float>( mMapSettings->terrainSettings()->elevationOffset() - ray.
origin().z() - mMapSettings->origin().z() ) / ray.
direction().z();
88 const QVector3D terrainPlanePoint = ray.
point( dist );
90 if ( mMapSettings->extent().contains( mapCoords.
x(), mapCoords.
y() ) )
93 intersectionPointMapCoords = mapCoords;
99 const QList<QgsChunkNode *> activeNodes = this->activeNodes();
100 QVector3D nearestIntersectionPoint;
101 for ( QgsChunkNode *node : activeNodes )
107 Qt3DRender::QGeometryRenderer *rend = node->entity()->findChild<Qt3DRender::QGeometryRenderer *>();
108 auto *geom = rend->geometry();
109 Qt3DCore::QTransform *tr = node->entity()->findChild<Qt3DCore::QTransform *>();
110 QVector3D nodeIntPoint;
111 DemTerrainTileGeometry *demGeom =
static_cast<DemTerrainTileGeometry *
>( geom );
112 if ( demGeom->rayIntersection( ray, context, tr->matrix(), nodeIntPoint ) )
114 const float dist = ( ray.
origin() - nodeIntPoint ).length();
115 if ( minDist < 0 || dist < minDist )
118 nearestIntersectionPoint = nodeIntPoint;
138 result.append( hit );
143void QgsTerrainEntity::onShowBoundingBoxesChanged()
148void QgsTerrainEntity::invalidateMapImages()
150 QgsEventTracing::addEvent( QgsEventTracing::Instant, u
"3D"_s, u
"Invalidate textures"_s );
154 updateNodes( mActiveNodes );
158 QList<QgsChunkNode *> inactiveNodes;
159 const QList<QgsChunkNode *> descendants = mRootNode->descendants();
160 for ( QgsChunkNode *node : descendants )
162 if ( !node->entity() )
164 if ( mActiveNodes.contains( node ) )
166 inactiveNodes << node;
169 updateNodes( inactiveNodes );
171 setNeedsUpdate(
true );
174void QgsTerrainEntity::onTerrainElevationOffsetChanged()
176 float newOffset = qobject_cast<Qgs3DMapSettings *>( sender() )->terrainSettings()->elevationOffset();
177 mTerrainTransform->setTranslation( QVector3D( 0.0f, 0.0f, newOffset ) );
180float QgsTerrainEntity::terrainElevationOffset()
const
182 return mMapSettings->terrainSettings()->elevationOffset();
@ 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.