From ff8a16350f057cedeb92c37a06fd193364948ff1 Mon Sep 17 00:00:00 2001 From: Turky Mohammed <45469625+DelinWorks@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:33:39 -0700 Subject: [PATCH] [IMPROVEMENT] Fix tilemap camera move and zoom culling (#1134) * Fix tilemap culling * Update CCFastTMXLayer.cpp * Update CCFastTMXLayer.h --- core/2d/CCFastTMXLayer.cpp | 21 +++++++++++++++------ core/2d/CCFastTMXLayer.h | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/core/2d/CCFastTMXLayer.cpp b/core/2d/CCFastTMXLayer.cpp index 652ba7e974..dbf8636195 100644 --- a/core/2d/CCFastTMXLayer.cpp +++ b/core/2d/CCFastTMXLayer.cpp @@ -132,13 +132,22 @@ void FastTMXLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flag { updateTotalQuads(); - if (flags != 0 || _dirty || _quadsDirty) + auto cam = Camera::getVisitingCamera(); + if (flags != 0 || _dirty || _quadsDirty || !_cameraPositionDirty.fuzzyEquals(cam->getPosition(), _tileSet->_tileSize.x) || + _cameraZoomDirty != cam->getZoom()) { - Vec2 s = _director->getVisibleSize(); - const Vec2& anchor = getAnchorPoint(); - auto rect = Rect(Camera::getVisitingCamera()->getPositionX() - s.width * (anchor.x == 0.0f ? 0.5f : anchor.x), - Camera::getVisitingCamera()->getPositionY() - s.height * (anchor.y == 0.0f ? 0.5f : anchor.y), - s.width, s.height); + _cameraPositionDirty = cam->getPosition(); + auto zoom = _cameraZoomDirty = cam->getZoom(); + Vec2 s = _director->getVisibleSize(); + const Vec2& anchor = getAnchorPoint(); + auto rect = Rect(cam->getPositionX() - s.width * zoom * (anchor.x == 0.0f ? 0.5f : anchor.x), + cam->getPositionY() - s.height * zoom * (anchor.y == 0.0f ? 0.5f : anchor.y), + s.width * zoom, s.height * zoom); + + rect.origin.x -= _tileSet->_tileSize.x; + rect.origin.y -= _tileSet->_tileSize.y; + rect.size.x += s.x * (zoom / 2) / 2 + _tileSet->_tileSize.x * zoom; + rect.size.y += s.y * (zoom / 2) / 2 + _tileSet->_tileSize.y * zoom; Mat4 inv = transform; inv.inverse(); diff --git a/core/2d/CCFastTMXLayer.h b/core/2d/CCFastTMXLayer.h index a9c5c77475..f1390143d8 100644 --- a/core/2d/CCFastTMXLayer.h +++ b/core/2d/CCFastTMXLayer.h @@ -361,6 +361,9 @@ protected: Mat4 _tileToNodeTransform; /** data for rendering */ bool _quadsDirty = true; + Vec2 _cameraPositionDirty = {INFINITY, INFINITY}; + float _cameraZoomDirty; + std::vector _tileToQuadIndex; std::vector _totalQuads; #ifdef AX_FAST_TILEMAP_32_BIT_INDICES