Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improve 3D Map View performance #60672

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/3d/qgs3dmapscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,7 @@ void Qgs3DMapScene::onCameraChanged()
}

updateScene( true );
bool changedCameraPlanes = updateCameraNearFarPlanes();

if ( changedCameraPlanes )
{
// repeat update of entities - because we have updated camera's near/far planes,
// the active nodes may have changed as well
updateScene( true );
updateCameraNearFarPlanes();
}
updateCameraNearFarPlanes();

onShadowSettingsChanged();

Expand All @@ -342,7 +334,22 @@ void Qgs3DMapScene::updateScene( bool forceUpdate )
sceneContext.cameraPos = camera->position();
const QSize size = mEngine->size();
sceneContext.screenSizePx = std::max( size.width(), size.height() ); // TODO: is this correct?
sceneContext.viewProjectionMatrix = camera->projectionMatrix() * camera->viewMatrix();

// Make our own projection matrix so that frustum culling done by the
// entities isn't dependent on the current near/far planes, which would then
// require multiple steps to stabilize.
// The matrix is constructed just like in QMatrix4x4::perspective(), but for
// all elements involving the near and far plane, the limit of the expression
// with the far plane going to infinity is taken.
float fovRadians = ( camera->fieldOfView() / 2.0f ) * static_cast<float>( M_PI ) / 180.0f;
float fovCotan = std::cos( fovRadians ) / std::sin( fovRadians );
QMatrix4x4 projMatrix(
fovCotan / camera->aspectRatio(), 0, 0, 0,
0, fovCotan, 0, 0,
0, 0, -1, -2,
0, 0, -1, 0
);
sceneContext.viewProjectionMatrix = projMatrix * camera->viewMatrix();


for ( Qgs3DMapSceneEntity *entity : std::as_const( mSceneEntities ) )
Expand Down
13 changes: 11 additions & 2 deletions src/3d/qgsvirtualpointcloudentity_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,18 @@ void QgsVirtualPointCloudEntity::handleSceneUpdate( const SceneContext &sceneCon
const QVector<QgsPointCloudSubIndex> subIndexes = provider()->subIndexes();
for ( int i = 0; i < subIndexes.size(); ++i )
{
// If the chunked entity needs an update, do it even if it's occluded,
// since otherwise we'd return needsUpdate() == true until it comes into
// view again.
bool needsUpdate = mChunkedEntitiesMap.contains( i ) && mChunkedEntitiesMap[i]->needsUpdate();

const QgsBox3D &box3D = mBboxes.at( i );

if ( box3D.isEmpty() )
if ( !needsUpdate && box3D.isEmpty() )
continue;

QgsAABB aabb = QgsAABB::fromBox3D( box3D, mMapSettings->origin() );
if ( !needsUpdate && Qgs3DUtils::isCullable( aabb, sceneContext.viewProjectionMatrix ) )
continue;

// magic number 256 is the common span value for a COPC root node
Expand Down Expand Up @@ -181,7 +190,7 @@ QgsRange<float> QgsVirtualPointCloudEntity::getNearFarPlaneRange( const QMatrix4
{
for ( const QgsBox3D &box : mBboxes )
{
QgsAABB aabb = QgsAABB::fromBox3D( box, mBboxesEntity->vertexDataOrigin() );
QgsAABB aabb = QgsAABB::fromBox3D( box, mMapSettings->origin() );
float bboxfnear;
float bboxffar;
Qgs3DUtils::computeBoundingBoxNearFarPlanes( aabb, viewMatrix, bboxfnear, bboxffar );
Expand Down