diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 9634ff38af..a83d9e0a3d 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1242,7 +1242,7 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform); - auto camera = director->getCurrentCamera(); + auto camera = Camera::getVisitingCamera(); bool visibleByCamera = camera ? (unsigned short)camera->getCameraFlag() & _cameraMask : true; int i = 0; diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 1763da5e8d..5fc4c3bd14 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -185,12 +185,6 @@ void Scene::addChildToPhysicsWorld(Node* child) } } -Camera* Scene::getCameraByIndex(int index) const -{ - CCASSERT(index < _cameras.size(), "invalid index"); - return _cameras[index]; -} - #endif NS_CC_END diff --git a/cocos/2d/CCScene.h b/cocos/2d/CCScene.h index aad487b0dd..41c53659a4 100644 --- a/cocos/2d/CCScene.h +++ b/cocos/2d/CCScene.h @@ -65,10 +65,8 @@ public: using Node::addChild; virtual std::string getDescription() const override; - /** get camera count */ - ssize_t getCameraCount() const { return _cameras.size(); } - /** get camera */ - Camera* getCameraByIndex(int index) const; + /** get all cameras */ + const std::vector& getCameras() const { return _cameras; } CC_CONSTRUCTOR_ACCESS: Scene(); @@ -82,6 +80,7 @@ protected: friend class ProtectedNode; friend class SpriteBatchNode; friend class Camera; + friend class Director; std::vector _cameras; //weak ref to Camera diff --git a/cocos/base/CCCamera.cpp b/cocos/base/CCCamera.cpp index f903249962..d4ee14911d 100644 --- a/cocos/base/CCCamera.cpp +++ b/cocos/base/CCCamera.cpp @@ -29,6 +29,8 @@ NS_CC_BEGIN +Camera* Camera::_visitingCamera = nullptr; + Camera* Camera::create() { Camera* camera = nullptr; @@ -224,17 +226,9 @@ void Camera::onEnter() { if (_scene == nullptr) { - auto parent = getParent(); - while (parent) - { - auto scene = dynamic_cast(parent); - if (scene) - { - setScene(scene); - break; - } - parent = parent->getParent(); - } + auto scene = getScene(); + if (scene) + setScene(scene); } Node::onEnter(); } diff --git a/cocos/base/CCCamera.h b/cocos/base/CCCamera.h index 5beeffa734..d65ba3798e 100644 --- a/cocos/base/CCCamera.h +++ b/cocos/base/CCCamera.h @@ -131,6 +131,8 @@ public: //override virtual void onEnter() override; virtual void onExit() override; + + static const Camera* getVisitingCamera() { return _visitingCamera; } CC_CONSTRUCTOR_ACCESS: Camera(); @@ -157,6 +159,10 @@ protected: float _farPlane; mutable bool _viewProjectionDirty; unsigned short _cameraFlag; // camera flag + + static Camera* _visitingCamera; + + friend class Director; }; NS_CC_END diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 2355897eb6..ac8fac6eb3 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -158,7 +158,6 @@ bool Director::init(void) initMatrixStack(); _renderer = new Renderer; - _currentCamera = nullptr; #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) _console = new Console; @@ -289,18 +288,19 @@ void Director::drawScene() if (_runningScene) { Camera* defaultCamera = nullptr; + const auto& cameras = _runningScene->_cameras; //draw with camera - for (ssize_t i = 0; i < _runningScene->getCameraCount(); i++) + for (size_t i = 0; i < cameras.size(); i++) { - _currentCamera = _runningScene->getCameraByIndex((int)i); - if (_currentCamera->getCameraFlag() == CameraFlag::DEFAULT) + Camera::_visitingCamera = cameras[i]; + if (Camera::_visitingCamera->getCameraFlag() == CameraFlag::DEFAULT) { - defaultCamera = _currentCamera; + defaultCamera = Camera::_visitingCamera; continue; } pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _currentCamera->getViewProjectionMatrix()); + loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix()); //visit the scene _runningScene->visit(_renderer, Mat4::IDENTITY, false); @@ -311,9 +311,9 @@ void Director::drawScene() //draw with default camera if (defaultCamera) { - _currentCamera = defaultCamera; + Camera::_visitingCamera = defaultCamera; pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); - loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _currentCamera->getViewProjectionMatrix()); + loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix()); //visit the scene _runningScene->visit(_renderer, Mat4::IDENTITY, false); @@ -321,7 +321,7 @@ void Director::drawScene() popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); } - _currentCamera = nullptr; + Camera::_visitingCamera = nullptr; _eventDispatcher->dispatchEvent(_eventAfterVisit); } diff --git a/cocos/base/CCDirector.h b/cocos/base/CCDirector.h index cf8ea8b0e0..c5ffd80e0a 100644 --- a/cocos/base/CCDirector.h +++ b/cocos/base/CCDirector.h @@ -406,8 +406,6 @@ public: * get Frame Rate */ float getFrameRate() const { return _frameRate; } - - Camera* getCurrentCamera() const { return _currentCamera; } protected: void purgeDirector(); @@ -508,9 +506,6 @@ protected: /* Renderer for the Director */ Renderer *_renderer; - - /* current camera, only vailid during drawScene */ - Camera* _currentCamera; #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) /* Console for the director */