From a5d41aac0d4d950a76ef6b827077d1c2b5bdb82e Mon Sep 17 00:00:00 2001 From: geron-cn Date: Fri, 24 Jul 2015 19:48:05 +0800 Subject: [PATCH] fix bug:use wrong _modelviewTransform in director to draw bone --- .../ActionTimeline/CCSkeletonNode.cpp | 76 +++++++++++++++---- .../ActionTimeline/CCSkeletonNode.h | 1 + 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp index f8864039c5..a3881ffc82 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.cpp @@ -145,29 +145,75 @@ void SkeletonNode::updateColor() _transformUpdated = _transformDirty = _inverseDirty = _contentSizeDirty = true; } - void SkeletonNode::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) { - BoneNode::visit(renderer, parentTransform, parentFlags); - if (_visible && _isRackShow) + // quick return if not visible. children won't be drawn. + if (!_visible) { - _customCommand.init(_globalZOrder, parentTransform, parentFlags); - _customCommand.func = CC_CALLBACK_0(SkeletonNode::onDraw, this, parentTransform, parentFlags); - renderer->addCommand(&_customCommand); - for (int i = 0; i < 8; ++i) + return; + } + + uint32_t flags = processParentFlags(parentTransform, parentFlags); + + // IMPORTANT: + // To ease the migration to v3.0, we still support the Mat4 stack, + // but it is deprecated and your code should not rely on it + _director->pushMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + _director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform); + + bool visibleByCamera = isVisitableByVisitingCamera(); + + int i = 0; + + if (!_children.empty()) + { + sortAllChildren(); + // draw children zOrder < 0 + for (; i < _children.size(); i++) { - cocos2d::Vec4 pos; - pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ; - pos.w = 1; - _modelViewTransform.transformVector(&pos); - _noMVPVertices[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w; + auto node = _children.at(i); + + if (node && node->getLocalZOrder() < 0) + node->visit(renderer, _modelViewTransform, flags); + else + break; } - this->draw(renderer, parentTransform, parentFlags); - _batchBoneCommand.init(_globalZOrder, parentTransform, parentFlags); - _batchBoneCommand.func = CC_CALLBACK_0(SkeletonNode::batchDrawAllSubBones, this, parentTransform); + for (auto it = _children.cbegin() + i; it != _children.cend(); ++it) + (*it)->visit(renderer, _modelViewTransform, flags); + } + + if (visibleByCamera) + { + this->draw(renderer, _modelViewTransform, flags); + // batch draw all sub bones + _batchBoneCommand.init(_globalZOrder, _modelViewTransform, parentFlags); + _batchBoneCommand.func = CC_CALLBACK_0(SkeletonNode::batchDrawAllSubBones, this, _modelViewTransform); renderer->addCommand(&_batchBoneCommand); } + _director->popMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + // FIX ME: Why need to set _orderOfArrival to 0?? + // Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920 + // reset for next frame + // _orderOfArrival = 0; +} + +void SkeletonNode::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) +{ + if (!_isRackShow) + return; + + _customCommand.init(_globalZOrder, transform, flags); + _customCommand.func = CC_CALLBACK_0(SkeletonNode::onDraw, this, transform, flags); + renderer->addCommand(&_customCommand); + for (int i = 0; i < 8; ++i) + { + cocos2d::Vec4 pos; + pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ; + pos.w = 1; + _modelViewTransform.transformVector(&pos); + _noMVPVertices[i] = cocos2d::Vec3(pos.x, pos.y, pos.z) / pos.w; + } } void SkeletonNode::batchDrawAllSubBones(const cocos2d::Mat4 &transform) diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h index 94fc1f4ea5..8fc4d99fa9 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCSkeletonNode.h @@ -82,6 +82,7 @@ protected: virtual void updateColor() override; virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) override; + virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; virtual void onDraw(const cocos2d::Mat4 &transform, uint32_t flags) override; protected: