2015-08-20 16:19:57 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
****************************************************************************/
|
2015-08-21 10:27:19 +08:00
|
|
|
#include "2d/CCCameraBackgroundBrush.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-06 14:24:06 +08:00
|
|
|
#include "base/ccTypes.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "base/ccMacros.h"
|
2018-09-11 14:39:30 +08:00
|
|
|
#include "base/ccUtils.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCRenderState.h"
|
2015-08-24 10:49:10 +08:00
|
|
|
#include "renderer/CCTextureCube.h"
|
2019-03-04 09:14:45 +08:00
|
|
|
#include "renderer/ccShaders.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "base/CCEventCustom.h"
|
|
|
|
# include "base/CCEventListenerCustom.h"
|
|
|
|
# include "base/CCEventType.h"
|
|
|
|
# include "base/CCEventDispatcher.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CameraBackgroundBrush::CameraBackgroundBrush() {}
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundBrush::~CameraBackgroundBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundBrush* CameraBackgroundBrush::createNoneBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new CameraBackgroundBrush();
|
2015-08-20 16:19:57 +08:00
|
|
|
ret->init();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush* CameraBackgroundBrush::createColorBrush(const Color4F& color, float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
return CameraBackgroundColorBrush::create(color, depth);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush* CameraBackgroundBrush::createDepthBrush(float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
return CameraBackgroundDepthBrush::create(depth);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* CameraBackgroundBrush::createSkyboxBrush(std::string_view positive_x,
|
|
|
|
std::string_view negative_x,
|
|
|
|
std::string_view positive_y,
|
|
|
|
std::string_view negative_y,
|
|
|
|
std::string_view positive_z,
|
|
|
|
std::string_view negative_z)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
return CameraBackgroundSkyBoxBrush::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _depth(0.f)
|
|
|
|
, _clearColor(false)
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2018-09-18 09:14:27 +08:00
|
|
|
, _backToForegroundListener(nullptr)
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
_backToForegroundListener =
|
|
|
|
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
|
2018-09-18 09:14:27 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2018-09-18 09:14:27 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new CameraBackgroundDepthBrush();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2016-11-14 10:05:26 +08:00
|
|
|
{
|
|
|
|
ret->_depth = depth;
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-14 10:05:26 +08:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
bool CameraBackgroundDepthBrush::init()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2019-10-15 09:40:59 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::CAMERA_CLEAR);
|
|
|
|
_programState = new backend::ProgramState(program);
|
2019-03-04 09:14:45 +08:00
|
|
|
|
|
|
|
_locDepth = _programState->getUniformLocation("dpeth");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
|
2019-03-04 09:14:45 +08:00
|
|
|
pipelineDescriptor.programState = _programState;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto layout = _programState->getVertexLayout();
|
2019-03-12 19:40:13 +08:00
|
|
|
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto iter = attributeInfo.find("a_position");
|
|
|
|
if (iter != attributeInfo.end())
|
2019-03-12 19:40:13 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3,
|
|
|
|
offsetof(V3F_C4B_T2F, vertices), false);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
|
|
|
iter = attributeInfo.find("a_color");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != attributeInfo.end())
|
2019-03-12 19:40:13 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4,
|
|
|
|
offsetof(V3F_C4B_T2F, colors), true);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
|
|
|
iter = attributeInfo.find("a_texCoord");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != attributeInfo.end())
|
2019-03-12 19:40:13 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
|
|
|
|
offsetof(V3F_C4B_T2F, texCoords), true);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setLayout(sizeof(_vertices[0]));
|
2019-03-04 09:14:45 +08:00
|
|
|
|
|
|
|
_vertices.resize(4);
|
|
|
|
_vertices[0].vertices = Vec3(-1, -1, 0);
|
|
|
|
_vertices[1].vertices = Vec3(1, -1, 0);
|
|
|
|
_vertices[2].vertices = Vec3(1, 1, 0);
|
|
|
|
_vertices[3].vertices = Vec3(-1, 1, 0);
|
|
|
|
|
|
|
|
_vertices[0].colors = _vertices[1].colors = _vertices[2].colors = _vertices[3].colors = Color4B(0, 0, 0, 1);
|
|
|
|
|
|
|
|
_vertices[0].texCoords = Tex2F(0, 0);
|
|
|
|
_vertices[1].texCoords = Tex2F(1, 0);
|
|
|
|
_vertices[2].texCoords = Tex2F(1, 1);
|
|
|
|
_vertices[3].texCoords = Tex2F(0, 1);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_customCommand.setBeforeCallback(AX_CALLBACK_0(CameraBackgroundDepthBrush::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(AX_CALLBACK_0(CameraBackgroundDepthBrush::onAfterDraw, this));
|
2019-02-19 16:06:17 +08:00
|
|
|
|
2018-09-18 09:14:27 +08:00
|
|
|
initBuffer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundDepthBrush::initBuffer()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
uint16_t indices[6] = {0, 1, 2, 2, 3, 0};
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.createVertexBuffer(sizeof(_vertices[0]), _vertices.size(), CustomCommand::BufferUsage::STATIC);
|
2021-12-25 10:04:45 +08:00
|
|
|
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, sizeof(indices) / sizeof(indices[0]),
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2017-07-17 15:16:42 +08:00
|
|
|
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.updateVertexBuffer(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
|
|
|
|
_customCommand.updateIndexBuffer(indices, sizeof(indices));
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2019-03-12 15:41:32 +08:00
|
|
|
// `clear screen` should be executed before other commands
|
|
|
|
_groupCommand.init(-1.0f);
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.init(0.0f);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2017-07-17 15:16:42 +08:00
|
|
|
|
2019-03-12 15:41:32 +08:00
|
|
|
renderer->addCommand(&_groupCommand);
|
|
|
|
renderer->pushGroup(_groupCommand.getRenderQueueID());
|
2019-02-19 16:06:17 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
|
|
|
|
auto& blend = pipelineDescriptor.blendDescriptor;
|
|
|
|
blend.writeMask = _clearColor ? backend::ColorWriteMask::ALL : backend::ColorWriteMask::NONE;
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// draw
|
2019-03-04 09:14:45 +08:00
|
|
|
_programState->setUniform(_locDepth, &_depth, sizeof(_depth));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-03-04 09:14:45 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2019-03-12 15:41:32 +08:00
|
|
|
|
|
|
|
renderer->popGroup();
|
2019-03-04 09:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundDepthBrush::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
|
|
|
_stateBlock.stencilWriteMask = renderer->getStencilWriteMask();
|
|
|
|
_stateBlock.depthTest = renderer->getDepthTest();
|
|
|
|
_stateBlock.compareFunc = renderer->getDepthCompareFunction();
|
2019-03-12 15:41:32 +08:00
|
|
|
|
|
|
|
renderer->setStencilWriteMask(0);
|
|
|
|
renderer->setDepthTest(true);
|
|
|
|
renderer->setDepthCompareFunction(backend::CompareFunction::ALWAYS);
|
2019-03-04 09:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundDepthBrush::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-04 09:14:45 +08:00
|
|
|
renderer->setStencilWriteMask(_stateBlock.stencilWriteMask);
|
|
|
|
renderer->setDepthTest(_stateBlock.depthTest);
|
|
|
|
renderer->setDepthCompareFunction(_stateBlock.compareFunc);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CameraBackgroundColorBrush::CameraBackgroundColorBrush() : _color(0.f, 0.f, 0.f, 0.f) {}
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CameraBackgroundColorBrush::~CameraBackgroundColorBrush() {}
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2015-08-21 12:18:36 +08:00
|
|
|
bool CameraBackgroundColorBrush::init()
|
|
|
|
{
|
2015-08-24 10:23:59 +08:00
|
|
|
CameraBackgroundDepthBrush::init();
|
2019-06-05 17:58:33 +08:00
|
|
|
this->_clearColor = true;
|
2015-08-21 12:18:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:43:45 +08:00
|
|
|
void CameraBackgroundColorBrush::drawBackground(Camera* camera)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
BlendFunc op = {BlendFunc::ALPHA_NON_PREMULTIPLIED.src, BlendFunc::ALPHA_NON_PREMULTIPLIED.dst};
|
2019-03-12 15:41:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
|
|
|
|
blend.sourceRGBBlendFactor = blend.sourceAlphaBlendFactor = op.src;
|
2019-03-12 15:41:32 +08:00
|
|
|
blend.destinationRGBBlendFactor = blend.destinationAlphaBlendFactor = op.dst;
|
2021-12-25 10:04:45 +08:00
|
|
|
blend.blendEnabled = true;
|
2017-10-20 09:43:45 +08:00
|
|
|
|
|
|
|
CameraBackgroundDepthBrush::drawBackground(camera);
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundColorBrush::setColor(const Color4F& color)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& vert : _vertices)
|
2017-09-19 09:41:10 +08:00
|
|
|
{
|
2019-03-04 09:14:45 +08:00
|
|
|
vert.colors = Color4B(color);
|
2017-09-19 09:41:10 +08:00
|
|
|
}
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.updateVertexBuffer(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush* CameraBackgroundColorBrush::create(const Color4F& color, float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new CameraBackgroundColorBrush();
|
2016-11-14 10:05:26 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2016-11-14 10:05:26 +08:00
|
|
|
{
|
|
|
|
ret->setColor(color);
|
|
|
|
ret->setDepth(depth);
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-14 10:05:26 +08:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush::CameraBackgroundSkyBoxBrush()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _texture(nullptr)
|
|
|
|
, _actived(true)
|
|
|
|
, _textureValid(true)
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
, _backToForegroundListener(nullptr)
|
2018-09-10 15:04:35 +08:00
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
_backToForegroundListener =
|
|
|
|
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
|
2015-08-20 16:19:57 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush::~CameraBackgroundSkyBoxBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2018-09-10 15:04:35 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create(std::string_view positive_x,
|
|
|
|
std::string_view negative_x,
|
|
|
|
std::string_view positive_y,
|
|
|
|
std::string_view negative_y,
|
|
|
|
std::string_view positive_z,
|
|
|
|
std::string_view negative_z)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2016-11-14 10:05:26 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* ret = nullptr;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto texture = TextureCube::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
|
2016-11-14 10:05:26 +08:00
|
|
|
|
|
|
|
if (texture != nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
Texture2D::TexParams tRepeatParams;
|
2021-12-25 10:04:45 +08:00
|
|
|
tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
|
2019-03-13 10:54:26 +08:00
|
|
|
tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
2016-11-14 10:05:26 +08:00
|
|
|
texture->setTexParameters(tRepeatParams);
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
ret = new CameraBackgroundSkyBoxBrush;
|
2016-11-14 10:05:26 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2016-11-14 10:05:26 +08:00
|
|
|
{
|
|
|
|
ret->setTexture(texture);
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(texture);
|
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-14 10:05:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new CameraBackgroundSkyBoxBrush();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->init())
|
2016-11-14 10:05:26 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2016-11-14 10:05:26 +08:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::drawBackground(Camera* camera)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2019-03-12 15:41:32 +08:00
|
|
|
// `clear screen` should be executed before other commands
|
|
|
|
_groupCommand.init(-1.0f);
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.init(0.0f);
|
|
|
|
|
2015-11-12 09:49:49 +08:00
|
|
|
if (!_actived)
|
|
|
|
return;
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
Mat4 cameraModelMat = camera->getNodeToWorldTransform();
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
|
2019-03-04 09:14:45 +08:00
|
|
|
pipelineDescriptor.blendDescriptor.blendEnabled = false;
|
|
|
|
|
|
|
|
Vec4 color(1.f, 1.f, 1.f, 1.f);
|
|
|
|
cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
|
|
|
|
|
|
|
|
_programState->setUniform(_uniformColorLoc, &color, sizeof(color));
|
|
|
|
_programState->setUniform(_uniformCameraRotLoc, cameraModelMat.m, sizeof(cameraModelMat.m));
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2019-03-12 15:41:32 +08:00
|
|
|
renderer->addCommand(&_groupCommand);
|
|
|
|
renderer->pushGroup(_groupCommand.getRenderQueueID());
|
|
|
|
|
2019-03-04 09:14:45 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2019-03-12 15:41:32 +08:00
|
|
|
|
|
|
|
renderer->popGroup();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
bool CameraBackgroundSkyBoxBrush::init()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_customCommand.setBeforeCallback(AX_CALLBACK_0(CameraBackgroundSkyBoxBrush::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(AX_CALLBACK_0(CameraBackgroundSkyBoxBrush::onAfterDraw, this));
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D);
|
|
|
|
_programState = new backend::ProgramState(program);
|
|
|
|
_uniformColorLoc = _programState->getUniformLocation("u_color");
|
|
|
|
_uniformCameraRotLoc = _programState->getUniformLocation("u_cameraRot");
|
|
|
|
_uniformEnvLoc = _programState->getUniformLocation("u_Env");
|
|
|
|
|
|
|
|
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
|
|
|
|
auto layout = _programState->getVertexLayout();
|
|
|
|
pipelineDescriptor.programState = _programState;
|
2019-03-04 09:14:45 +08:00
|
|
|
// disable blend
|
2021-12-25 10:04:45 +08:00
|
|
|
pipelineDescriptor.blendDescriptor.blendEnabled = false;
|
2019-03-12 15:41:32 +08:00
|
|
|
|
2020-01-06 09:35:12 +08:00
|
|
|
auto attrNameLoc = _programState->getAttributeLocation(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION);
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION, attrNameLoc, backend::VertexFormat::FLOAT3, 0,
|
|
|
|
false);
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setLayout(sizeof(Vec3));
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
initBuffer();
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::initBuffer()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
// init vertex buffer object
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 vexBuf[] = {Vec3(1, -1, 1), Vec3(1, 1, 1), Vec3(-1, 1, 1), Vec3(-1, -1, 1),
|
|
|
|
Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)};
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_customCommand.createVertexBuffer(sizeof(vexBuf[0]), sizeof(vexBuf) / sizeof(vexBuf[0]),
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.updateVertexBuffer(vexBuf, sizeof(vexBuf));
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
// init index buffer object
|
2021-12-25 10:04:45 +08:00
|
|
|
uint16_t idxBuf[] = {
|
|
|
|
2, 1, 0, 3, 2, 0, // font
|
|
|
|
1, 5, 4, 1, 4, 0, // right
|
|
|
|
4, 5, 6, 4, 6, 7, // back
|
|
|
|
7, 6, 2, 7, 2, 3, // left
|
|
|
|
2, 6, 5, 2, 5, 1, // up
|
|
|
|
3, 0, 4, 3, 4, 7 // down
|
2015-08-20 16:19:57 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, sizeof(idxBuf) / sizeof(idxBuf[0]),
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2019-03-04 09:14:45 +08:00
|
|
|
_customCommand.updateIndexBuffer(idxBuf, sizeof(idxBuf));
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::setTexture(TextureCube* texture)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(texture);
|
|
|
|
AX_SAFE_RELEASE(_texture);
|
2015-08-20 16:19:57 +08:00
|
|
|
_texture = texture;
|
2019-03-04 09:14:45 +08:00
|
|
|
_programState->setTexture(_uniformEnvLoc, 0, _texture->getBackendTexture());
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-11-12 10:57:22 +08:00
|
|
|
bool CameraBackgroundSkyBoxBrush::isActived() const
|
2015-11-12 09:49:49 +08:00
|
|
|
{
|
|
|
|
return _actived;
|
|
|
|
}
|
|
|
|
void CameraBackgroundSkyBoxBrush::setActived(bool actived)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_actived = actived;
|
2015-11-12 09:49:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundSkyBoxBrush::setTextureValid(bool valid)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_textureValid = valid;
|
2015-11-12 09:49:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraBackgroundSkyBoxBrush::isValid()
|
|
|
|
{
|
|
|
|
return _actived;
|
|
|
|
}
|
|
|
|
|
2019-03-04 09:14:45 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
|
|
|
_stateBlock.depthTest = renderer->getDepthTest();
|
|
|
|
_stateBlock.depthWrite = renderer->getDepthWrite();
|
|
|
|
_stateBlock.depthFunc = renderer->getDepthCompareFunction();
|
|
|
|
_stateBlock.cullMode = renderer->getCullMode();
|
2019-03-04 09:14:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundSkyBoxBrush::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-04 09:14:45 +08:00
|
|
|
renderer->setDepthTest(_stateBlock.depthTest);
|
|
|
|
renderer->setDepthWrite(_stateBlock.depthWrite);
|
|
|
|
renderer->setDepthCompareFunction(_stateBlock.depthFunc);
|
|
|
|
renderer->setCullMode(_stateBlock.cullMode);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|