2019-11-23 20:27:39 +08:00
|
|
|
/******************************************************************************
|
2019-12-12 23:26:12 +08:00
|
|
|
* Spine Runtimes License Agreement
|
2022-11-07 15:48:19 +08:00
|
|
|
* Last updated September 24, 2021. Replaces all prior versions.
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
2022-11-07 15:48:19 +08:00
|
|
|
* Copyright (c) 2013-2021, Esoteric Software LLC
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
2019-12-12 23:26:12 +08:00
|
|
|
* Integration of the Spine Runtimes into software or otherwise creating
|
|
|
|
* derivative works of the Spine Runtimes is permitted under the terms and
|
|
|
|
* conditions of Section 2 of the Spine Editor License Agreement:
|
|
|
|
* http://esotericsoftware.com/spine-editor-license
|
2019-11-23 20:27:39 +08:00
|
|
|
*
|
2019-12-12 23:26:12 +08:00
|
|
|
* Otherwise, it is permitted to integrate the Spine Runtimes into software
|
|
|
|
* or otherwise create derivative works of the Spine Runtimes (collectively,
|
|
|
|
* "Products"), provided that each user of the Products must obtain their own
|
|
|
|
* Spine Editor license and redistribution of the Products in any form must
|
|
|
|
* include this license and copyright notice.
|
|
|
|
*
|
2020-01-05 03:09:32 +08:00
|
|
|
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
|
|
|
|
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-11-23 20:27:39 +08:00
|
|
|
*****************************************************************************/
|
2020-01-05 03:09:32 +08:00
|
|
|
|
|
|
|
#include <spine/spine-cocos2dx.h>
|
|
|
|
#if COCOS2D_VERSION >= 0x00040000
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Types.h"
|
|
|
|
#include "base/Utils.h"
|
2022-11-07 15:48:19 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <spine/Extension.h>
|
|
|
|
#include <stddef.h>// offsetof
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2019-12-12 23:26:12 +08:00
|
|
|
#include "renderer/backend/Device.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "renderer/Shaders.h"
|
2022-11-07 15:48:19 +08:00
|
|
|
#include "xxhash.h"
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
USING_NS_CC;
|
2019-11-23 20:27:39 +08:00
|
|
|
#define EVENT_AFTER_DRAW_RESET_POSITION "director_after_draw"
|
|
|
|
using std::max;
|
|
|
|
#define INITIAL_SIZE (10000)
|
|
|
|
#define MAX_VERTICES 64000
|
|
|
|
#define MAX_INDICES 64000
|
|
|
|
|
2023-08-25 16:46:32 +08:00
|
|
|
#define STRINGIFY(A) #A
|
|
|
|
|
2019-12-12 23:26:12 +08:00
|
|
|
namespace {
|
|
|
|
|
2023-08-25 16:46:32 +08:00
|
|
|
const char *TWO_COLOR_TINT_VERTEX_SHADER = STRINGIFY(
|
|
|
|
uniform mat4 u_PMatrix;
|
|
|
|
attribute vec4 a_position;
|
|
|
|
attribute vec4 a_color;
|
|
|
|
attribute vec4 a_color2;
|
|
|
|
attribute vec2 a_texCoords;
|
|
|
|
|
|
|
|
\n #ifdef GL_ES\n
|
|
|
|
varying lowp vec4 v_light;
|
|
|
|
varying lowp vec4 v_dark;
|
|
|
|
varying mediump vec2 v_texCoord;
|
|
|
|
\n #else \n
|
|
|
|
varying vec4 v_light;
|
|
|
|
varying vec4 v_dark;
|
|
|
|
varying vec2 v_texCoord;
|
|
|
|
|
|
|
|
\n #endif \n
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
v_light = a_color;
|
|
|
|
v_dark = a_color2;
|
|
|
|
v_texCoord = a_texCoords;
|
|
|
|
gl_Position = u_PMatrix * a_position;
|
|
|
|
});
|
|
|
|
|
|
|
|
const char *TWO_COLOR_TINT_FRAGMENT_SHADER = STRINGIFY(
|
|
|
|
\n #ifdef GL_ES\n
|
|
|
|
precision lowp float;
|
|
|
|
\n #endif \n
|
|
|
|
uniform sampler2D u_texture;
|
|
|
|
varying vec4 v_light;
|
|
|
|
varying vec4 v_dark;
|
|
|
|
varying vec2 v_texCoord;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
vec4 texColor = texture2D(u_texture, v_texCoord);
|
|
|
|
float alpha = texColor.a * v_light.a;
|
|
|
|
gl_FragColor.a = alpha;
|
|
|
|
gl_FragColor.rgb = ((texColor.a - 1.0) * v_dark.a + 1.0 - texColor.rgb) * v_dark.rgb + texColor.rgb * v_light.rgb;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
std::shared_ptr<backend::ProgramState> __twoColorProgramState = nullptr;
|
|
|
|
backend::UniformLocation __locPMatrix;
|
|
|
|
backend::UniformLocation __locTexture;
|
|
|
|
|
|
|
|
static void updateProgramStateLayout(backend::ProgramState *programState) {
|
|
|
|
__locPMatrix = programState->getUniformLocation("u_PMatrix");
|
2023-08-25 16:46:32 +08:00
|
|
|
__locTexture = programState->getUniformLocation("u_texture");
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2020-01-05 03:09:32 +08:00
|
|
|
auto locPosition = programState->getAttributeLocation("a_position");
|
2023-08-25 16:46:32 +08:00
|
|
|
auto locTexcoord = programState->getAttributeLocation("a_texCoords");
|
2020-01-05 03:09:32 +08:00
|
|
|
auto locColor = programState->getAttributeLocation("a_color");
|
|
|
|
auto locColor2 = programState->getAttributeLocation("a_color2");
|
|
|
|
|
2023-08-25 16:46:32 +08:00
|
|
|
programState->setVertexAttrib("a_position", locPosition, backend::VertexFormat::FLOAT3, offsetof(spine::V3F_C4B_C4B_T2F, position), false);
|
|
|
|
programState->setVertexAttrib("a_color", locColor, backend::VertexFormat::UBYTE4, offsetof(spine::V3F_C4B_C4B_T2F, color), true);
|
|
|
|
programState->setVertexAttrib("a_color2", locColor2, backend::VertexFormat::UBYTE4, offsetof(spine::V3F_C4B_C4B_T2F, color2), true);
|
|
|
|
programState->setVertexAttrib("a_texCoords", locTexcoord, backend::VertexFormat::FLOAT2, offsetof(spine::V3F_C4B_C4B_T2F, texCoords), false);
|
|
|
|
programState->setVertexStride(sizeof(spine::V3F_C4B_C4B_T2F));
|
2020-10-17 22:00:53 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
static void initTwoColorProgramState() {
|
|
|
|
if (__twoColorProgramState) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-25 16:46:32 +08:00
|
|
|
auto program = backend::Device::getInstance()->newProgram(TWO_COLOR_TINT_VERTEX_SHADER, TWO_COLOR_TINT_FRAGMENT_SHADER);
|
2022-11-07 15:48:19 +08:00
|
|
|
auto *programState = new backend::ProgramState(program);
|
2023-08-25 16:46:32 +08:00
|
|
|
program->release();
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
updateProgramStateLayout(programState);
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
__twoColorProgramState = std::shared_ptr<backend::ProgramState>(programState);
|
|
|
|
}
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
}// namespace
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
namespace spine {
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
TwoColorTrianglesCommand::TwoColorTrianglesCommand() : _materialID(0), _texture(nullptr), _blendType(BlendFunc::DISABLE) {
|
|
|
|
_type = RenderCommand::Type::CUSTOM_COMMAND;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void TwoColorTrianglesCommand::init(float globalOrder, cocos2d::Texture2D *texture, cocos2d::backend::ProgramState *programState, BlendFunc blendType, const TwoColorTriangles &triangles, const Mat4 &mv, uint32_t flags) {
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
updateCommandPipelineDescriptor(programState);
|
|
|
|
const cocos2d::Mat4 &projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
auto finalMatrix = projectionMat * mv;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
_programState->setUniform(_locPMatrix, finalMatrix.m, sizeof(finalMatrix.m));
|
|
|
|
_programState->setTexture(_locTexture, 0, texture->getBackendTexture());
|
2019-12-12 23:26:12 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
RenderCommand::init(globalOrder, mv, flags);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
_triangles = triangles;
|
|
|
|
if (_triangles.indexCount % 3 != 0) {
|
|
|
|
int count = _triangles.indexCount;
|
|
|
|
_triangles.indexCount = count / 3 * 3;
|
|
|
|
CCLOGERROR("Resize indexCount from %d to %d, size must be multiple times of 3", count, _triangles.indexCount);
|
|
|
|
}
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
_mv = mv;
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
if (_blendType.src != blendType.src || _blendType.dst != blendType.dst ||
|
|
|
|
_texture != texture->getBackendTexture() || _pipelineDescriptor.programState != _programState) {
|
|
|
|
_texture = texture->getBackendTexture();
|
|
|
|
_blendType = blendType;
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
_prog = _programState->getProgram();
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
auto &blendDescriptor = _pipelineDescriptor.blendDescriptor;
|
|
|
|
blendDescriptor.blendEnabled = true;
|
|
|
|
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendType.src;
|
|
|
|
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendType.dst;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
generateMaterialID();
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void TwoColorTrianglesCommand::updateCommandPipelineDescriptor(cocos2d::backend::ProgramState *programState) {
|
|
|
|
// OPTIMIZE ME: all commands belong a same Node should share a same programState like SkeletonBatch
|
|
|
|
if (!__twoColorProgramState) {
|
|
|
|
initTwoColorProgramState();
|
|
|
|
}
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
bool needsUpdateStateLayout = false;
|
|
|
|
auto &pipelinePS = _pipelineDescriptor.programState;
|
|
|
|
if (programState != nullptr) {
|
|
|
|
if (_programState != programState) {
|
|
|
|
CC_SAFE_RELEASE(_programState);
|
|
|
|
_programState = programState;// Because the programState belong to Node, so no need to clone
|
|
|
|
CC_SAFE_RETAIN(_programState);
|
|
|
|
needsUpdateStateLayout = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
needsUpdateStateLayout = _programState != nullptr && _programState->getProgram() != __twoColorProgramState->getProgram();
|
|
|
|
CC_SAFE_RELEASE(_programState);
|
|
|
|
_programState = __twoColorProgramState->clone();
|
|
|
|
}
|
2020-10-17 22:00:53 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
CCASSERT(_programState, "programState should not be null");
|
|
|
|
pipelinePS = _programState;
|
2020-10-17 22:00:53 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
if (needsUpdateStateLayout)
|
|
|
|
updateProgramStateLayout(pipelinePS);
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
_locPMatrix = __locPMatrix;
|
|
|
|
_locTexture = __locTexture;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
TwoColorTrianglesCommand::~TwoColorTrianglesCommand() {
|
|
|
|
CC_SAFE_RELEASE_NULL(_programState);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void TwoColorTrianglesCommand::generateMaterialID() {
|
|
|
|
// do not batch if using custom uniforms (since we cannot batch) it
|
2019-12-12 23:26:12 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
void *texture;
|
|
|
|
void *prog;
|
|
|
|
backend::BlendFactor src;
|
|
|
|
backend::BlendFactor dst;
|
|
|
|
} hashMe;
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
// NOTE: Initialize hashMe struct to make the value of padding bytes be filled with zero.
|
|
|
|
// It's important since XXH32 below will also consider the padding bytes which probably
|
|
|
|
// are set to random values by different compilers.
|
|
|
|
memset(&hashMe, 0, sizeof(hashMe));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
hashMe.texture = _texture;
|
|
|
|
hashMe.src = _blendType.src;
|
|
|
|
hashMe.dst = _blendType.dst;
|
|
|
|
hashMe.prog = _prog;
|
|
|
|
_materialID = XXH32((const void *) &hashMe, sizeof(hashMe), 0);
|
|
|
|
}
|
2019-12-12 23:26:12 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void TwoColorTrianglesCommand::draw(Renderer *r) {
|
|
|
|
SkeletonTwoColorBatch::getInstance()->batch(r, this);
|
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void TwoColorTrianglesCommand::updateVertexAndIndexBuffer(Renderer *r, V3F_C4B_C4B_T2F *vertices, int verticesSize, uint16_t *indices, int indicesSize) {
|
|
|
|
if (verticesSize != _vertexCapacity)
|
|
|
|
createVertexBuffer(sizeof(V3F_C4B_C4B_T2F), verticesSize, CustomCommand::BufferUsage::DYNAMIC);
|
|
|
|
if (indicesSize != _indexCapacity)
|
|
|
|
createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, indicesSize, CustomCommand::BufferUsage::DYNAMIC);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
updateVertexBuffer(vertices, sizeof(V3F_C4B_C4B_T2F) * verticesSize);
|
|
|
|
updateIndexBuffer(indices, sizeof(uint16_t) * indicesSize);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
static SkeletonTwoColorBatch *instance = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
SkeletonTwoColorBatch *SkeletonTwoColorBatch::getInstance() {
|
|
|
|
if (!instance) instance = new SkeletonTwoColorBatch();
|
|
|
|
return instance;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::destroyInstance() {
|
|
|
|
if (instance) {
|
|
|
|
delete instance;
|
|
|
|
instance = nullptr;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
SkeletonTwoColorBatch::SkeletonTwoColorBatch() : _vertexBuffer(0), _indexBuffer(0) {
|
|
|
|
_commandsPool.reserve(INITIAL_SIZE);
|
|
|
|
for (unsigned int i = 0; i < INITIAL_SIZE; i++) {
|
|
|
|
_commandsPool.push_back(new TwoColorTrianglesCommand());
|
|
|
|
}
|
|
|
|
|
|
|
|
reset();
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
// callback after drawing is finished so we can clear out the batch state
|
|
|
|
// for the next frame
|
|
|
|
Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom *eventCustom) {
|
|
|
|
this->update(0);
|
|
|
|
});
|
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
SkeletonTwoColorBatch::~SkeletonTwoColorBatch() {
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
for (unsigned int i = 0; i < _commandsPool.size(); i++) {
|
|
|
|
delete _commandsPool[i];
|
|
|
|
_commandsPool[i] = nullptr;
|
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
delete[] _vertexBuffer;
|
|
|
|
delete[] _indexBuffer;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2019-12-12 23:26:12 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::update(float delta) {
|
|
|
|
reset();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
V3F_C4B_C4B_T2F *SkeletonTwoColorBatch::allocateVertices(uint32_t numVertices) {
|
|
|
|
if (_vertices.size() - _numVertices < numVertices) {
|
|
|
|
V3F_C4B_C4B_T2F *oldData = _vertices.data();
|
|
|
|
_vertices.resize((_vertices.size() + numVertices) * 2 + 1);
|
|
|
|
V3F_C4B_C4B_T2F *newData = _vertices.data();
|
|
|
|
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
|
|
|
|
TwoColorTrianglesCommand *command = _commandsPool[i];
|
|
|
|
TwoColorTriangles &triangles = (TwoColorTriangles &) command->getTriangles();
|
|
|
|
triangles.verts = newData + (triangles.verts - oldData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
V3F_C4B_C4B_T2F *vertices = _vertices.data() + _numVertices;
|
|
|
|
_numVertices += numVertices;
|
|
|
|
return vertices;
|
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::deallocateVertices(uint32_t numVertices) {
|
|
|
|
_numVertices -= numVertices;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
unsigned short *SkeletonTwoColorBatch::allocateIndices(uint32_t numIndices) {
|
|
|
|
if (_indices.getCapacity() - _indices.size() < numIndices) {
|
|
|
|
unsigned short *oldData = _indices.buffer();
|
2023-05-07 21:15:56 +08:00
|
|
|
int oldSize = (int)_indices.size();
|
2022-11-07 15:48:19 +08:00
|
|
|
_indices.ensureCapacity(_indices.size() + numIndices);
|
|
|
|
unsigned short *newData = _indices.buffer();
|
|
|
|
for (uint32_t i = 0; i < this->_nextFreeCommand; i++) {
|
|
|
|
TwoColorTrianglesCommand *command = _commandsPool[i];
|
|
|
|
TwoColorTriangles &triangles = (TwoColorTriangles &) command->getTriangles();
|
|
|
|
if (triangles.indices >= oldData && triangles.indices < oldData + oldSize) {
|
|
|
|
triangles.indices = newData + (triangles.indices - oldData);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
unsigned short *indices = _indices.buffer() + _indices.size();
|
|
|
|
_indices.setSize(_indices.size() + numIndices, 0);
|
|
|
|
return indices;
|
2019-12-12 23:26:12 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::deallocateIndices(uint32_t numIndices) {
|
|
|
|
_indices.setSize(_indices.size() - numIndices, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
TwoColorTrianglesCommand *SkeletonTwoColorBatch::addCommand(cocos2d::Renderer *renderer, float globalOrder, cocos2d::Texture2D *texture, backend::ProgramState *programState, cocos2d::BlendFunc blendType, const TwoColorTriangles &triangles, const cocos2d::Mat4 &mv, uint32_t flags) {
|
|
|
|
TwoColorTrianglesCommand *command = nextFreeCommand();
|
|
|
|
command->init(globalOrder, texture, programState, blendType, triangles, mv, flags);
|
|
|
|
command->updateVertexAndIndexBuffer(renderer, triangles.verts, triangles.vertCount, triangles.indices, triangles.indexCount);
|
|
|
|
renderer->addCommand(command);
|
|
|
|
return command;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::batch(cocos2d::Renderer *renderer, TwoColorTrianglesCommand *command) {
|
|
|
|
if (_numVerticesBuffer + command->getTriangles().vertCount >= MAX_VERTICES || _numIndicesBuffer + command->getTriangles().indexCount >= MAX_INDICES) {
|
|
|
|
flush(renderer, _lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t materialID = command->getMaterialID();
|
|
|
|
if (_lastCommand && _lastCommand->getMaterialID() != materialID) {
|
|
|
|
flush(renderer, _lastCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(_vertexBuffer + _numVerticesBuffer, command->getTriangles().verts, sizeof(V3F_C4B_C4B_T2F) * command->getTriangles().vertCount);
|
|
|
|
const Mat4 &modelView = command->getModelView();
|
|
|
|
for (int i = _numVerticesBuffer; i < _numVerticesBuffer + command->getTriangles().vertCount; i++) {
|
|
|
|
modelView.transformPoint(&_vertexBuffer[i].position);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short vertexOffset = (unsigned short) _numVerticesBuffer;
|
|
|
|
unsigned short *indices = command->getTriangles().indices;
|
|
|
|
for (int i = 0, j = _numIndicesBuffer; i < command->getTriangles().indexCount; i++, j++) {
|
|
|
|
_indexBuffer[j] = indices[i] + vertexOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
_numVerticesBuffer += command->getTriangles().vertCount;
|
|
|
|
_numIndicesBuffer += command->getTriangles().indexCount;
|
|
|
|
|
|
|
|
if (command->isForceFlush()) {
|
|
|
|
flush(renderer, command);
|
|
|
|
}
|
|
|
|
_lastCommand = command;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
void SkeletonTwoColorBatch::flush(cocos2d::Renderer *renderer, TwoColorTrianglesCommand *materialCommand) {
|
|
|
|
if (!materialCommand)
|
|
|
|
return;
|
2020-01-05 03:09:32 +08:00
|
|
|
|
2022-11-07 15:48:19 +08:00
|
|
|
materialCommand->updateVertexAndIndexBuffer(renderer, _vertexBuffer, _numVerticesBuffer, _indexBuffer, _numIndicesBuffer);
|
|
|
|
|
|
|
|
renderer->addCommand(materialCommand);
|
|
|
|
|
|
|
|
_numVerticesBuffer = 0;
|
|
|
|
_numIndicesBuffer = 0;
|
|
|
|
_numBatches++;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-11-07 15:48:19 +08:00
|
|
|
|
|
|
|
void SkeletonTwoColorBatch::reset() {
|
|
|
|
_nextFreeCommand = 0;
|
|
|
|
_numVertices = 0;
|
|
|
|
_indices.setSize(0, 0);
|
|
|
|
_numVerticesBuffer = 0;
|
|
|
|
_numIndicesBuffer = 0;
|
|
|
|
_lastCommand = nullptr;
|
|
|
|
_numBatches = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwoColorTrianglesCommand *SkeletonTwoColorBatch::nextFreeCommand() {
|
|
|
|
if (_commandsPool.size() <= _nextFreeCommand) {
|
2023-05-07 21:15:56 +08:00
|
|
|
unsigned int newSize = (int)_commandsPool.size() * 2 + 1;
|
|
|
|
for (int i = (int)_commandsPool.size(); i < newSize; i++) {
|
2022-11-07 15:48:19 +08:00
|
|
|
_commandsPool.push_back(new TwoColorTrianglesCommand());
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-11-07 15:48:19 +08:00
|
|
|
TwoColorTrianglesCommand *command = _commandsPool[_nextFreeCommand++];
|
|
|
|
command->setForceFlush(false);
|
|
|
|
return command;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-11-07 15:48:19 +08:00
|
|
|
}// namespace spine
|
2020-01-05 03:09:32 +08:00
|
|
|
|
|
|
|
#endif
|