Reimplement LayerColor and LayerGradient for auto batch draw

This commit is contained in:
halx99 2021-08-29 18:59:09 +08:00
parent 89f0e21508
commit 8e1d7bd85d
2 changed files with 163 additions and 303 deletions

View File

@ -4,8 +4,9 @@ Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) Bytedance Inc.
http://www.cocos2d-x.org
https://adxe.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -66,10 +67,7 @@ Layer::Layer()
setAnchorPoint(Vec2(0.5f, 0.5f));
}
Layer::~Layer()
{
}
Layer::~Layer() {}
bool Layer::init()
{
@ -106,7 +104,9 @@ int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* tou
#endif
}
int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType,
const std::vector<Touch*>& touches,
Event* event)
{
#if CC_ENABLE_SCRIPT_BINDING
TouchesScriptData data(eventType, this, touches, event);
@ -120,7 +120,6 @@ int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const st
#endif
}
void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
{
#if CC_ENABLE_SCRIPT_BINDING
@ -132,9 +131,7 @@ void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
#endif
}
void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
{
}
void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/) {}
void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
{
@ -237,59 +234,7 @@ std::string Layer::getDescription() const
}
/// LayerColor
LayerColor::LayerColor()
{
// default blend function
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
auto& pipelinePS = _customCommand.getPipelineDescriptor().programState;
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
//!!! LayerColor private programState don't want affect by Node::_programState, so store at _customCommand
pipelinePS = new backend::ProgramState(program);
auto vertexLayout = pipelinePS->getVertexLayout();
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
}
iter = attributeInfo.find("a_color");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::FLOAT4, sizeof(_vertexData[0].vertices), false);
}
vertexLayout->setLayout(sizeof(_vertexData[0]));
_mvpMatrixLocation = pipelinePS->getUniformLocation("u_MVPMatrix");
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, 6, CustomCommand::BufferUsage::STATIC);
unsigned short indices[] = {0, 1, 2, 2, 1, 3};
_customCommand.updateIndexBuffer(indices, sizeof(indices));
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), 4, CustomCommand::BufferUsage::DYNAMIC);
_customCommand.setDrawType(CustomCommand::DrawType::ELEMENT);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
}
LayerColor::~LayerColor()
{
CC_SAFE_RELEASE_NULL(_customCommand.getPipelineDescriptor().programState);
}
/// blendFunc getter
const BlendFunc &LayerColor::getBlendFunc() const
{
return _blendFunc;
}
/// blendFunc setter
void LayerColor::setBlendFunc(const BlendFunc &var)
{
_blendFunc = var;
}
LayerColor::LayerColor() {}
LayerColor* LayerColor::create()
{
@ -337,25 +282,17 @@ bool LayerColor::init()
bool LayerColor::initWithColor(const Color4B& color, float w, float h)
{
if (Layer::init())
if (Sprite::init())
{
// Anchor behavior same with Layer
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Vec2(0.5f, 0.5f));
// default blend function
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
_displayedColor.r = _realColor.r = color.r;
_displayedColor.g = _realColor.g = color.g;
_displayedColor.b = _realColor.b = color.b;
_displayedOpacity = _realOpacity = color.a;
for (size_t i = 0; i<sizeof(_squareVertices) / sizeof( _squareVertices[0]); i++ )
{
_squareVertices[i].x = 0.0f;
_squareVertices[i].y = 0.0f;
}
updateColor();
const Rect defaultRect{0.f, 0.f, 2.f, 2.f};
setTextureRect(defaultRect, false, defaultRect.size);
setContentSize(Size(w, h));
setColor(Color3B{color});
setOpacity(color.a);
return true;
}
@ -368,17 +305,6 @@ bool LayerColor::initWithColor(const Color4B& color)
return initWithColor(color, s.width, s.height);
}
/// override contentSize
void LayerColor::setContentSize(const Size & size)
{
_squareVertices[1].x = size.width;
_squareVertices[2].y = size.height;
_squareVertices[3].x = size.width;
_squareVertices[3].y = size.height;
Layer::setContentSize(size);
}
void LayerColor::changeWidthAndHeight(float w, float h)
{
this->setContentSize(Size(w, h));
@ -394,52 +320,12 @@ void LayerColor::changeHeight(float h)
this->setContentSize(Size(_contentSize.width, h));
}
void LayerColor::updateColor()
{
for (int i = 0; i < 4; i++ )
{
_vertexData[i].colors.r = _displayedColor.r / 255.0f;
_vertexData[i].colors.g = _displayedColor.g / 255.0f;
_vertexData[i].colors.b = _displayedColor.b / 255.0f;
_vertexData[i].colors.a = _displayedOpacity / 255.0f;
}
}
void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
_customCommand.init(_globalZOrder, _blendFunc);
renderer->addCommand(&_customCommand);
cocos2d::Mat4 projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
for(int i = 0; i < 4; ++i)
{
Vec4 pos;
pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
pos.w = 1;
_modelViewTransform.transformVector(&pos);
_vertexData[i].vertices = Vec3(pos.x,pos.y,pos.z)/pos.w;
}
updateVertexBuffer();
}
void LayerColor::updateVertexBuffer()
{
_customCommand.updateVertexBuffer(_vertexData, sizeof(_vertexData));
}
//
// LayerGradient
//
LayerGradient::LayerGradient()
{
}
LayerGradient::LayerGradient() {}
LayerGradient::~LayerGradient()
{
}
LayerGradient::~LayerGradient() {}
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
{
@ -506,8 +392,6 @@ bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, cons
void LayerGradient::updateColor()
{
LayerColor::updateColor();
float h = _alongVector.getLength();
if (h == 0)
return;
@ -524,40 +408,42 @@ void LayerGradient::updateColor()
float opacityf = (float)_displayedOpacity / 255.0f;
Color4F S(
_displayedColor.r / 255.0f,
_displayedColor.g / 255.0f,
_displayedColor.b / 255.0f,
_startOpacity * opacityf / 255.0f
);
Color4F S(_displayedColor.r / 255.0f, _displayedColor.g / 255.0f, _displayedColor.b / 255.0f,
_startOpacity * opacityf / 255.0f);
Color4F E(
_endColor.r / 255.0f,
_endColor.g / 255.0f,
_endColor.b / 255.0f,
_endOpacity * opacityf / 255.0f
);
Color4F E(_endColor.r / 255.0f, _endColor.g / 255.0f, _endColor.b / 255.0f, _endOpacity * opacityf / 255.0f);
// (-1, -1)
_vertexData[0].colors.r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.a = E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c));
_quad.bl.colors.r = (E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c))) * 255;
_quad.bl.colors.g = (E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c))) * 255;
_quad.bl.colors.b = (E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c))) * 255;
_quad.bl.colors.a = (E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c))) * 255;
// (1, -1)
_vertexData[1].colors.r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.a = E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c));
_quad.br.colors.r = (E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c))) * 255;
_quad.br.colors.g = (E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c))) * 255;
_quad.br.colors.b = (E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c))) * 255;
_quad.br.colors.a = (E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c))) * 255;
// (-1, 1)
_vertexData[2].colors.r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.a = E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c));
_quad.tl.colors.r = (E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c))) * 255;
_quad.tl.colors.g = (E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c))) * 255;
_quad.tl.colors.b = (E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c))) * 255;
_quad.tl.colors.a = (E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c))) * 255;
// (1, 1)
_vertexData[3].colors.r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
_quad.tr.colors.r = (E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c))) * 255;
_quad.tr.colors.g = (E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c))) * 255;
_quad.tr.colors.b = (E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c))) * 255;
_quad.tr.colors.a = (E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c))) * 255;
// renders using batch node
if (_renderMode == RenderMode::QUAD_BATCHNODE)
{
if (_atlasIndex != INDEX_NOT_INITIALIZED)
_textureAtlas->updateQuad(&_quad, _atlasIndex);
else
// no need to set it recursively
// update dirty_, don't update recursiveDirty_
setDirty(true);
}
}
const Color3B& LayerGradient::getStartColor() const
@ -633,7 +519,11 @@ std::string LayerGradient::getDescription() const
/**
* LayerRadialGradient
*/
LayerRadialGradient* LayerRadialGradient::create(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand)
LayerRadialGradient* LayerRadialGradient::create(const Color4B& startColor,
const Color4B& endColor,
float radius,
const Vec2& center,
float expand)
{
auto layerGradient = new LayerRadialGradient();
if (layerGradient && layerGradient->initWithColor(startColor, endColor, radius, center, expand))
@ -681,7 +571,8 @@ LayerRadialGradient::LayerRadialGradient()
}
vertexLayout->setLayout(sizeof(_vertices[0]));
_customCommand.createVertexBuffer(sizeof(_vertices[0]), sizeof(_vertices) / sizeof(_vertices[0]), CustomCommand::BufferUsage::STATIC);
_customCommand.createVertexBuffer(sizeof(_vertices[0]), sizeof(_vertices) / sizeof(_vertices[0]),
CustomCommand::BufferUsage::STATIC);
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}
@ -691,7 +582,11 @@ LayerRadialGradient::~LayerRadialGradient()
CC_SAFE_RELEASE_NULL(_customCommand.getPipelineDescriptor().programState);
}
bool LayerRadialGradient::initWithColor(const cocos2d::Color4B &startColor, const cocos2d::Color4B &endColor, float radius, const Vec2& center, float expand)
bool LayerRadialGradient::initWithColor(const cocos2d::Color4B& startColor,
const cocos2d::Color4B& endColor,
float radius,
const Vec2& center,
float expand)
{
// should do it before Layer::init()
for (int i = 0; i < 4; ++i)
@ -856,17 +751,14 @@ void LayerRadialGradient::convertColor4B24F(Color4F& outColor, const Color4B& in
outColor.a = inColor.a / 255.0f;
}
/// MultiplexLayer
LayerMultiplex::LayerMultiplex()
: _enabledLayer(0)
{
}
LayerMultiplex::LayerMultiplex() : _enabledLayer(0) {}
LayerMultiplex::~LayerMultiplex()
{
for(const auto &layer : _layers) {
for (const auto& layer : _layers)
{
layer->cleanup();
}
}
@ -958,7 +850,8 @@ bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
_layers.pushBack(layer);
Layer* l = va_arg(params, Layer*);
while( l ) {
while (l)
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
if (sEngine)
{

View File

@ -3,9 +3,9 @@ Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) Bytedance Inc.
http://www.cocos2d-x.org
https://adxe.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -28,6 +28,7 @@ THE SOFTWARE.
#pragma once
#include "2d/CCNode.h"
#include "2d/CCSprite.h"
#include "base/CCProtocols.h"
#include "renderer/CCCustomCommand.h"
@ -196,7 +197,7 @@ All features from Layer are valid, plus the following new features:
- opacity
- RGB colors
*/
class CC_DLL LayerColor : public Layer, public BlendProtocol
class CC_DLL LayerColor : public Sprite
{
public:
@ -238,46 +239,12 @@ public:
*/
void changeWidthAndHeight(float w, float h);
//
// Overrides
//
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
virtual void setContentSize(const Size & var) override;
/** BlendFunction. Conforms to BlendProtocol protocol */
/**
* @lua NA
*/
virtual const BlendFunc& getBlendFunc() const override;
/**
*@code
*When this function bound into js or lua,the parameter will be changed
*In js: var setBlendFunc(var src, var dst)
*In lua: local setBlendFunc(local src, local dst)
*@endcode
*/
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
CC_CONSTRUCTOR_ACCESS:
LayerColor();
virtual ~LayerColor();
bool init() override;
bool initWithColor(const Color4B& color, float width, float height);
bool initWithColor(const Color4B& color);
protected:
virtual void updateColor() override;
void updateVertexBuffer();
BlendFunc _blendFunc;
Vec2 _squareVertices[4];
CustomCommand _customCommand;
V3F_C4F _vertexData[4];
backend::UniformLocation _mvpMatrixLocation;
private:
CC_DISALLOW_COPY_AND_ASSIGN(LayerColor);