axmol/core/2d/ClippingNode.cpp

388 lines
11 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/*
* Copyright (c) 2012 Pierre-David Bélanger
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2013-2016 Chukong Technologies Inc.
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
*
* https://axmol.dev/
2019-11-23 20:27:39 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "2d/ClippingNode.h"
#include "renderer/Renderer.h"
#include "renderer/Shaders.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/ProgramState.h"
#include "base/Director.h"
#include "base/StencilStateManager.h"
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
ClippingNode::ClippingNode() : _stencilStateManager(new StencilStateManager()) {}
2019-11-23 20:27:39 +08:00
ClippingNode::~ClippingNode()
{
if (_stencil)
{
_stencil->stopAllActions();
_stencil->release();
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_stencilStateManager);
for (auto&& stencilProgramState : _originalStencilProgramState)
{
AX_SAFE_RELEASE(stencilProgramState.second);
}
2019-11-23 20:27:39 +08:00
}
ClippingNode* ClippingNode::create()
{
2021-12-25 10:04:45 +08:00
ClippingNode* ret = new ClippingNode();
2021-12-08 00:11:53 +08:00
if (ret->init())
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
ClippingNode* ClippingNode::create(Node* pStencil)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ClippingNode* ret = new ClippingNode();
2021-12-08 00:11:53 +08:00
if (ret->init(pStencil))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
bool ClippingNode::init()
{
return init(nullptr);
}
2021-12-25 10:04:45 +08:00
bool ClippingNode::init(Node* stencil)
2019-11-23 20:27:39 +08:00
{
setStencil(stencil);
return true;
}
void ClippingNode::onEnter()
{
Node::onEnter();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_stencil != nullptr)
{
_stencil->onEnter();
}
}
void ClippingNode::onEnterTransitionDidFinish()
{
Node::onEnterTransitionDidFinish();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_stencil != nullptr)
{
_stencil->onEnterTransitionDidFinish();
}
}
void ClippingNode::onExitTransitionDidStart()
{
if (_stencil != nullptr)
{
_stencil->onExitTransitionDidStart();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Node::onExitTransitionDidStart();
}
void ClippingNode::onExit()
{
if (_stencil != nullptr)
{
_stencil->onExit();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Node::onExit();
}
2021-12-25 10:04:45 +08:00
void ClippingNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
2019-11-23 20:27:39 +08:00
{
if (!_visible || !hasContent())
return;
2021-12-25 10:04:45 +08:00
2024-08-15 12:13:42 +08:00
AXASSERT(_stencil, "No stencil set");
2019-11-23 20:27:39 +08:00
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
2022-07-16 10:43:05 +08:00
AXASSERT(nullptr != _director, "Director is null when setting matrix stack");
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// Add group command
auto* groupCommandStencil = renderer->getNextGroupCommand();
groupCommandStencil->init(_globalZOrder);
renderer->addCommand(groupCommandStencil);
2019-11-23 20:27:39 +08:00
renderer->pushGroup(groupCommandStencil->getRenderQueueID());
2019-11-23 20:27:39 +08:00
// _beforeVisitCmd.init(_globalZOrder);
2022-07-16 10:43:05 +08:00
// _beforeVisitCmd.func = AX_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
2019-11-23 20:27:39 +08:00
// renderer->addCommand(&_beforeVisitCmd);
_stencilStateManager->onBeforeVisit(_globalZOrder);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto alphaThreshold = this->getAlphaThreshold();
if (alphaThreshold < 1)
{
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
2021-12-25 10:04:45 +08:00
auto programState = new backend::ProgramState(program);
2019-11-23 20:27:39 +08:00
auto alphaLocation = programState->getUniformLocation("u_alpha_value");
programState->setUniform(alphaLocation, &alphaThreshold, sizeof(alphaThreshold));
setProgramStateRecursively(_stencil, programState);
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(programState);
2019-11-23 20:27:39 +08:00
}
_stencil->visit(renderer, _modelViewTransform, flags);
2022-06-24 14:18:48 +08:00
auto afterDrawStencilCmd = renderer->nextCallbackCommand();
afterDrawStencilCmd->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
afterDrawStencilCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterDrawStencilCmd);
2019-11-23 20:27:39 +08:00
bool visibleByCamera = isVisitableByVisitingCamera();
// `_groupCommandChildren` is used as a barrier
// to ensure commands above be executed before children nodes
auto* groupCommandChildren = renderer->getNextGroupCommand();
2019-11-23 20:27:39 +08:00
groupCommandChildren->init(_globalZOrder);
renderer->addCommand(groupCommandChildren);
renderer->pushGroup(groupCommandChildren->getRenderQueueID());
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (!_children.empty())
2019-11-23 20:27:39 +08:00
{
sortAllChildren();
// draw children zOrder < 0
2021-10-11 12:15:41 +08:00
int i = 0;
2021-12-25 10:04:45 +08:00
for (int size = static_cast<int>(_children.size()); i < size; ++i)
2019-11-23 20:27:39 +08:00
{
auto node = _children.at(i);
2021-12-25 10:04:45 +08:00
if (node && node->getLocalZOrder() < 0)
2019-11-23 20:27:39 +08:00
node->visit(renderer, _modelViewTransform, flags);
else
break;
}
// self draw
if (visibleByCamera)
this->draw(renderer, _modelViewTransform, flags);
2021-12-25 10:04:45 +08:00
for (auto it = _children.cbegin() + i, itCend = _children.cend(); it != itCend; ++it)
2019-11-23 20:27:39 +08:00
(*it)->visit(renderer, _modelViewTransform, flags);
}
else if (visibleByCamera)
{
this->draw(renderer, _modelViewTransform, flags);
}
renderer->popGroup();
2022-06-24 14:18:48 +08:00
auto _afterVisitCmd = renderer->nextCallbackCommand();
_afterVisitCmd->init(_globalZOrder);
2022-07-16 10:43:05 +08:00
_afterVisitCmd->func = AX_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
2022-06-24 14:18:48 +08:00
renderer->addCommand(_afterVisitCmd);
2019-11-23 20:27:39 +08:00
renderer->popGroup();
2021-12-25 10:04:45 +08:00
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2019-11-23 20:27:39 +08:00
}
void ClippingNode::setGlobalZOrder(float globalZOrder)
{
Node::setGlobalZOrder(globalZOrder);
if (_stencil) {
// Make sure our stencil stays on the same globalZOrder:
_stencil->setGlobalZOrder(globalZOrder);
}
}
2019-11-23 20:27:39 +08:00
void ClippingNode::setCameraMask(unsigned short mask, bool applyChildren)
{
Node::setCameraMask(mask, applyChildren);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_stencil)
_stencil->setCameraMask(mask, applyChildren);
}
Node* ClippingNode::getStencil() const
{
return _stencil;
}
void ClippingNode::setStencil(Node* stencil, bool uniqueChildStencils)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// early out if the stencil is already set
2019-11-23 20:27:39 +08:00
if (_stencil == stencil)
return;
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
if (_stencil)
sEngine->releaseScriptObject(this, _stencil);
if (stencil)
sEngine->retainScriptObject(this, stencil);
}
2022-07-16 10:43:05 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2021-12-25 10:04:45 +08:00
// cleanup current stencil
if (_stencil != nullptr && _stencil->isRunning())
2019-11-23 20:27:39 +08:00
{
_stencil->onExitTransitionDidStart();
_stencil->onExit();
}
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_stencil);
2021-12-25 10:04:45 +08:00
// initialise new stencil
_uniqueChildStencils = uniqueChildStencils;
2019-11-23 20:27:39 +08:00
_stencil = stencil;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_stencil);
2021-12-25 10:04:45 +08:00
if (_stencil != nullptr && this->isRunning())
2019-11-23 20:27:39 +08:00
{
_stencil->onEnter();
2021-12-25 10:04:45 +08:00
if (this->_isTransitionFinished)
2019-11-23 20:27:39 +08:00
{
_stencil->onEnterTransitionDidFinish();
}
}
// Clear all existing entries in _originalStencilProgramState since they belong to a different stencil
for (auto&& stencilProgramState : _originalStencilProgramState)
{
AX_SAFE_RELEASE(stencilProgramState.second);
}
_originalStencilProgramState.clear();
2019-11-23 20:27:39 +08:00
if (_stencil != nullptr)
{
// Make sure our stencil stays on the same globalZOrder:
_stencil->setGlobalZOrder(getGlobalZOrder());
auto* stencilProgramState = _stencil->getProgramState();
AX_SAFE_RETAIN(stencilProgramState);
_originalStencilProgramState[_stencil] = stencilProgramState;
auto& children = _stencil->getChildren();
2021-12-25 10:04:45 +08:00
for (const auto& child : children)
{
auto* existingProgramState = child->getProgramState();
AX_SAFE_RETAIN(existingProgramState);
_originalStencilProgramState[child] = existingProgramState;
2019-11-23 20:27:39 +08:00
}
}
}
bool ClippingNode::hasContent() const
{
2020-08-18 14:29:09 +08:00
return !_children.empty();
2019-11-23 20:27:39 +08:00
}
float ClippingNode::getAlphaThreshold() const
{
return _stencilStateManager->getAlphaThreshold();
}
void ClippingNode::setAlphaThreshold(float alphaThreshold)
{
if (alphaThreshold == 1 && alphaThreshold != _stencilStateManager->getAlphaThreshold())
{
// should reset program used by _stencil
if (_stencil)
restoreAllProgramStates();
}
_stencilStateManager->setAlphaThreshold(alphaThreshold);
}
bool ClippingNode::isInverted() const
{
return _stencilStateManager->isInverted();
}
void ClippingNode::setInverted(bool inverted)
{
_stencilStateManager->setInverted(inverted);
}
void ClippingNode::setProgramStateRecursively(Node* node, backend::ProgramState* programState)
{
if (_originalStencilProgramState.find(node) == _originalStencilProgramState.end())
{
auto* existingProgramState = node->getProgramState();
AX_SAFE_RETAIN(existingProgramState);
_originalStencilProgramState[node] = existingProgramState;
}
if (_uniqueChildStencils)
{
node->setProgramState(programState->clone(), true);
}
else
{
node->setProgramState(programState);
}
2019-11-23 20:27:39 +08:00
auto& children = node->getChildren();
2021-12-25 10:04:45 +08:00
for (const auto& child : children)
{
2019-11-23 20:27:39 +08:00
setProgramStateRecursively(child, programState);
}
}
void ClippingNode::restoreAllProgramStates()
{
for (auto&& item : _originalStencilProgramState)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto node = item.first;
2019-11-23 20:27:39 +08:00
auto programState = item.second;
node->setProgramState(programState);
}
}
}