[IMPROVEMENT] Fix tilemap camera move and zoom culling (#1134)

* Fix tilemap culling

* Update CCFastTMXLayer.cpp

* Update CCFastTMXLayer.h
This commit is contained in:
Turky Mohammed 2023-03-24 17:33:39 -07:00 committed by GitHub
parent eae2ffabcb
commit fc4766076c
2 changed files with 18 additions and 6 deletions

View File

@ -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();

View File

@ -361,6 +361,9 @@ protected:
Mat4 _tileToNodeTransform;
/** data for rendering */
bool _quadsDirty = true;
Vec2 _cameraPositionDirty = {INFINITY, INFINITY};
float _cameraZoomDirty;
std::vector<int> _tileToQuadIndex;
std::vector<V3F_C4B_T2F_Quad> _totalQuads;
#ifdef AX_FAST_TILEMAP_32_BIT_INDICES