mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
@@ -44,6 +44,8 @@ THE SOFTWARE.
|
||||
#include "renderer/CCMaterial.h"
|
||||
#include "math/TransformUtils.h"
|
||||
|
||||
#include "editor-support/creator/CCCameraNode.h"
|
||||
|
||||
|
||||
#if CC_NODE_RENDER_SUBPIXEL
|
||||
#define RENDER_IN_SUBPIXEL
|
||||
@@ -1238,6 +1240,19 @@ 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 = creator::CameraNode::getInstance();
|
||||
if (camera) {
|
||||
if (camera->visitingIndex <= 0) {
|
||||
if (camera->containsNode(this)) {
|
||||
camera->visitingIndex ++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
camera->visitingIndex ++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!_children.empty())
|
||||
{
|
||||
sortAllChildren();
|
||||
@@ -1263,6 +1278,10 @@ void Node::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t paren
|
||||
{
|
||||
this->draw(renderer, _modelViewTransform, flags);
|
||||
}
|
||||
|
||||
if (camera && camera->visitingIndex > 0) {
|
||||
camera->visitingIndex --;
|
||||
}
|
||||
|
||||
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
||||
|
||||
|
||||
@@ -13,15 +13,23 @@
|
||||
using namespace cocos2d;
|
||||
|
||||
namespace creator {
|
||||
cocos2d::Mat4 CameraNode::_tempMat;
|
||||
static CameraNode* _cameraInstance;
|
||||
|
||||
|
||||
CameraNode::CameraNode()
|
||||
{
|
||||
_mat.setIdentity();
|
||||
_cameraInstance = this;
|
||||
visitingIndex = 0;
|
||||
}
|
||||
|
||||
CameraNode::~CameraNode()
|
||||
{
|
||||
_cameraInstance = nullptr;
|
||||
}
|
||||
|
||||
CameraNode* CameraNode::getInstance() {
|
||||
return _cameraInstance;
|
||||
}
|
||||
|
||||
void CameraNode::setTransform(float a, float b, float c, float d, float tx, float ty)
|
||||
@@ -33,18 +41,41 @@ namespace creator {
|
||||
m[5] = d;
|
||||
m[12] = tx;
|
||||
m[13] = ty;
|
||||
|
||||
_inverseMat = _mat;
|
||||
_inverseMat.inverse();
|
||||
|
||||
_visibleRect.origin = Director::getInstance()->getVisibleOrigin();
|
||||
_visibleRect.size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
_visibleRect = RectApplyTransform(_visibleRect, _inverseMat);
|
||||
}
|
||||
|
||||
const cocos2d::Rect& CameraNode::getVisibleRect()
|
||||
{
|
||||
return _visibleRect;
|
||||
}
|
||||
|
||||
bool CameraNode::containsNode(cocos2d::Node* node)
|
||||
{
|
||||
while (node) {
|
||||
if (std::find(_nodes.begin(), _nodes.end(), node) !=_nodes.end()) {
|
||||
return true;
|
||||
}
|
||||
node = node->getParent();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CameraNode::addTarget(Node* target)
|
||||
{
|
||||
for (auto i = _commands.begin(); i != _commands.end(); i++)
|
||||
{
|
||||
if (i->target == target)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (std::find(_nodes.begin(), _nodes.end(), target) !=_nodes.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
_nodes.push_back(target);
|
||||
|
||||
CustomCommand* beforeVisitCommand = new CustomCommand();
|
||||
CustomCommand* afterVisitCommand = new CustomCommand();
|
||||
|
||||
@@ -79,6 +110,13 @@ namespace creator {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto i = _nodes.begin(); i != _nodes.end(); i++) {
|
||||
if (*i == target) {
|
||||
_nodes.erase(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CameraNode::beforeVisit()
|
||||
|
||||
@@ -45,19 +45,29 @@ namespace creator {
|
||||
CameraNode();
|
||||
~CameraNode();
|
||||
|
||||
static CameraNode* getInstance();
|
||||
|
||||
void setTransform(float a, float b, float c, float d, float tx, float ty);
|
||||
|
||||
bool containsNode(cocos2d::Node* node);
|
||||
|
||||
void addTarget(cocos2d::Node* target);
|
||||
void removeTarget(cocos2d::Node* target);
|
||||
|
||||
const cocos2d::Rect& getVisibleRect();
|
||||
|
||||
int visitingIndex;
|
||||
public:
|
||||
void beforeVisit();
|
||||
void afterVisit();
|
||||
|
||||
protected:
|
||||
cocos2d::Mat4 _mat;
|
||||
static cocos2d::Mat4 _tempMat;
|
||||
cocos2d::Mat4 _inverseMat;
|
||||
cocos2d::Rect _visibleRect;
|
||||
|
||||
std::vector<CameraCommand> _commands;
|
||||
std::vector<cocos2d::Node*> _nodes;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@
|
||||
#include "base/CCEventType.h"
|
||||
#include "2d/CCScene.h"
|
||||
|
||||
#include "editor-support/creator/CCCameraNode.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
// helper
|
||||
@@ -878,37 +880,24 @@ void Renderer::flushTriangles()
|
||||
}
|
||||
|
||||
// helpers
|
||||
bool Renderer::checkVisibility(const Mat4 &transform, const Size &size)
|
||||
bool Renderer::checkVisibility(const Mat4& transform, const Size& size)
|
||||
{
|
||||
// half size of the screen
|
||||
Size screen_half = Director::getInstance()->getWinSize();
|
||||
screen_half.width /= 2;
|
||||
screen_half.height /= 2;
|
||||
|
||||
float hSizeX = size.width / 2;
|
||||
float hSizeY = size.height / 2;
|
||||
|
||||
Vec4 v4world, v4local;
|
||||
v4local.set(hSizeX, hSizeY, 0, 1);
|
||||
transform.transformVector(v4local, &v4world);
|
||||
|
||||
// center of screen is (0,0)
|
||||
v4world.x -= screen_half.width;
|
||||
v4world.y -= screen_half.height;
|
||||
|
||||
// convert content size to world coordinates
|
||||
float wshw = std::max(fabsf(hSizeX * transform.m[0] + hSizeY * transform.m[4]), fabsf(hSizeX * transform.m[0] - hSizeY * transform.m[4]));
|
||||
float wshh = std::max(fabsf(hSizeX * transform.m[1] + hSizeY * transform.m[5]), fabsf(hSizeX * transform.m[1] - hSizeY * transform.m[5]));
|
||||
|
||||
// compare if it in the positive quadrant of the screen
|
||||
float tmpx = (fabsf(v4world.x) - wshw);
|
||||
float tmpy = (fabsf(v4world.y) - wshh);
|
||||
bool ret = (tmpx < screen_half.width && tmpy < screen_half.height);
|
||||
|
||||
return ret;
|
||||
creator::CameraNode* camera = creator::CameraNode::getInstance();
|
||||
|
||||
Rect visibleRect;
|
||||
if (!camera || camera->visitingIndex <= 0) {
|
||||
visibleRect.origin = Director::getInstance()->getVisibleOrigin();
|
||||
visibleRect.size = Director::getInstance()->getVisibleSize();
|
||||
}
|
||||
else {
|
||||
visibleRect = camera->getVisibleRect();
|
||||
}
|
||||
|
||||
Rect rect(0,0, size.width, size.height);
|
||||
rect = RectApplyTransform(rect, transform);
|
||||
return rect.intersectsRect(visibleRect);
|
||||
}
|
||||
|
||||
|
||||
void Renderer::setClearColor(const Color4F &clearColor)
|
||||
{
|
||||
_clearColor = clearColor;
|
||||
|
||||
Reference in New Issue
Block a user