axmol/core/2d/MotionStreak.cpp

441 lines
13 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2011 ForzeField Studios S.L.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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 false 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/MotionStreak.h"
#include "math/Vertex.h"
#include "base/Director.h"
#include "base/Utils.h"
#include "renderer/TextureCache.h"
#include "renderer/Texture2D.h"
#include "renderer/Renderer.h"
#include "renderer/Shaders.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/ProgramState.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
MotionStreak::MotionStreak()
{
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}
MotionStreak::~MotionStreak()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_texture);
AX_SAFE_FREE(_pointState);
AX_SAFE_FREE(_pointVertexes);
AX_SAFE_FREE(_vertices);
AX_SAFE_FREE(_colorPointer);
AX_SAFE_FREE(_texCoords);
2019-11-23 20:27:39 +08:00
}
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, std::string_view path)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
MotionStreak* ret = new MotionStreak();
2021-12-08 00:11:53 +08:00
if (ret->initWithFade(fade, minSeg, stroke, color, path))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
2021-12-25 10:04:45 +08:00
MotionStreak* ret = new MotionStreak();
2021-12-08 00:11:53 +08:00
if (ret->initWithFade(fade, minSeg, stroke, color, texture))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, std::string_view path)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!path.empty(), "Invalid filename");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Texture2D* texture = _director->getTextureCache()->addImage(path);
2019-11-23 20:27:39 +08:00
return initWithFade(fade, minSeg, stroke, color, texture);
}
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
Node::setPosition(Vec2::ZERO);
setAnchorPoint(Vec2::ZERO);
setIgnoreAnchorPointForPosition(true);
_startingPositionInitialized = false;
_positionR.setZero();
_fastMode = true;
2021-12-25 10:04:45 +08:00
_minSeg = (minSeg == -1.0f) ? stroke / 5.0f : minSeg;
2019-11-23 20:27:39 +08:00
_minSeg *= _minSeg;
2021-12-25 10:04:45 +08:00
_stroke = stroke;
2019-11-23 20:27:39 +08:00
_fadeDelta = 1.0f / fade;
2021-12-25 10:04:45 +08:00
2022-05-04 01:54:44 +08:00
// Fix #629 Motion streak vsync off crash
2022-05-04 01:01:12 +08:00
double interval = _director->getAnimationInterval();
double fps = 1 / interval;
if (roundf(fps) > 240.0)
2022-05-04 14:26:54 +08:00
fps = 240.0;
2022-05-04 01:01:12 +08:00
2019-11-23 20:27:39 +08:00
_maxPoints = (int)(fade * fps) + 2;
2021-12-25 10:04:45 +08:00
_pointState = (float*)malloc(sizeof(float) * _maxPoints);
2019-11-23 20:27:39 +08:00
_pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
const size_t VERTEX_SIZE = sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4;
2021-12-25 10:04:45 +08:00
_vertexCount = _maxPoints * 2;
_vertices = (Vec2*)malloc(sizeof(Vec2) * _vertexCount);
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _vertexCount);
_colorPointer = (uint8_t*)malloc(sizeof(uint8_t) * 4 * _vertexCount);
2019-11-23 20:27:39 +08:00
_customCommand.createVertexBuffer(VERTEX_SIZE, _vertexCount, CustomCommand::BufferUsage::DYNAMIC);
auto zeros = std::make_unique<uint8_t[]>(VERTEX_SIZE * _vertexCount);
_customCommand.updateVertexBuffer(zeros.get(), VERTEX_SIZE * _vertexCount);
2019-11-23 20:27:39 +08:00
setTexture(texture);
setColor(color);
scheduleUpdate();
return true;
}
void MotionStreak::setPosition(const Vec2& position)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2019-11-23 20:27:39 +08:00
_startingPositionInitialized = true;
}
_positionR = position;
}
void MotionStreak::setPosition(float x, float y)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2019-11-23 20:27:39 +08:00
_startingPositionInitialized = true;
}
_positionR.x = x;
_positionR.y = y;
}
const Vec2& MotionStreak::getPosition() const
{
return _positionR;
}
void MotionStreak::getPosition(float* x, float* y) const
{
*x = _positionR.x;
*y = _positionR.y;
}
float MotionStreak::getPositionX() const
{
return _positionR.x;
}
Vec3 MotionStreak::getPosition3D() const
{
return Vec3(_positionR.x, _positionR.y, getPositionZ());
}
void MotionStreak::setPositionX(float x)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2019-11-23 20:27:39 +08:00
_startingPositionInitialized = true;
}
_positionR.x = x;
}
float MotionStreak::getPositionY() const
{
2021-12-25 10:04:45 +08:00
return _positionR.y;
2019-11-23 20:27:39 +08:00
}
void MotionStreak::setPositionY(float y)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2019-11-23 20:27:39 +08:00
_startingPositionInitialized = true;
}
_positionR.y = y;
}
void MotionStreak::tintWithColor(const Color3B& colors)
{
setColor(colors);
// Fast assignation
2021-12-25 10:04:45 +08:00
for (unsigned int i = 0; i < _nuPoints * 2; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
*((Color3B*)(_colorPointer + i * 4)) = colors;
2019-11-23 20:27:39 +08:00
}
}
Texture2D* MotionStreak::getTexture() const
{
return _texture;
}
2021-12-25 10:04:45 +08:00
void MotionStreak::setTexture(Texture2D* texture)
2019-11-23 20:27:39 +08:00
{
if (_texture != texture)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(texture);
AX_SAFE_RELEASE(_texture);
2019-11-23 20:27:39 +08:00
_texture = texture;
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, _texture);
}
}
bool MotionStreak::setProgramState(backend::ProgramState* programState, bool ownPS /*= false*/)
2019-11-23 20:27:39 +08:00
{
if (Node::setProgramState(programState, ownPS))
2021-12-25 10:04:45 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(programState, "argument should not be nullptr");
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
2020-09-09 13:03:31 +08:00
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_tex0");
2020-09-09 13:03:31 +08:00
2022-09-23 22:41:30 +08:00
// setup custom vertex layout for V2F_T2F_C4B
2020-09-09 13:03:31 +08:00
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
2021-12-25 10:04:45 +08:00
auto iter = attributeInfo.find("a_position");
auto layout = _programState->getMutableVertexLayout();
2020-09-09 13:03:31 +08:00
if (iter != attributeInfo.end())
{
layout->setAttrib("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
2020-09-09 13:03:31 +08:00
}
iter = attributeInfo.find("a_texCoord");
if (iter != attributeInfo.end())
{
layout->setAttrib("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
2022-09-23 22:41:30 +08:00
2 * sizeof(float), false);
2020-09-09 13:03:31 +08:00
}
iter = attributeInfo.find("a_color");
if (iter != attributeInfo.end())
{
layout->setAttrib("a_color", iter->second.location, backend::VertexFormat::UBYTE4,
2022-09-23 22:41:30 +08:00
4 * sizeof(float), true);
2020-09-09 13:03:31 +08:00
}
layout->setStride(4 * sizeof(float) + 4 * sizeof(uint8_t));
updateProgramStateTexture(_texture);
2020-09-09 13:03:31 +08:00
return true;
2019-11-23 20:27:39 +08:00
}
2020-09-09 13:03:31 +08:00
return false;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void MotionStreak::setBlendFunc(const BlendFunc& blendFunc)
2019-11-23 20:27:39 +08:00
{
_blendFunc = blendFunc;
}
const BlendFunc& MotionStreak::getBlendFunc() const
{
return _blendFunc;
}
void MotionStreak::setOpacity(uint8_t /*opacity*/)
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Set opacity no supported");
2019-11-23 20:27:39 +08:00
}
uint8_t MotionStreak::getOpacity() const
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Opacity no supported");
2019-11-23 20:27:39 +08:00
return 0;
}
2021-12-25 10:04:45 +08:00
void MotionStreak::setOpacityModifyRGB(bool /*bValue*/) {}
2019-11-23 20:27:39 +08:00
bool MotionStreak::isOpacityModifyRGB() const
{
return false;
}
void MotionStreak::update(float delta)
{
if (!_startingPositionInitialized)
return;
delta *= _fadeDelta;
unsigned int newIdx, newIdx2, i, i2;
unsigned int mov = 0;
// Update current points
for (i = 0; i < _nuPoints; i++)
{
_pointState[i] -= delta;
if (_pointState[i] <= 0)
mov++;
else
{
newIdx = i - mov;
if (mov > 0)
{
// Move data
_pointState[newIdx] = _pointState[i];
// Move point
_pointVertexes[newIdx] = _pointVertexes[i];
// Move vertices
2021-12-25 10:04:45 +08:00
i2 = i * 2;
newIdx2 = newIdx * 2;
_vertices[newIdx2] = _vertices[i2];
_vertices[newIdx2 + 1] = _vertices[i2 + 1];
2019-11-23 20:27:39 +08:00
// Move color
i2 *= 4;
newIdx2 *= 4;
2021-12-25 10:04:45 +08:00
_colorPointer[newIdx2 + 0] = _colorPointer[i2 + 0];
_colorPointer[newIdx2 + 1] = _colorPointer[i2 + 1];
_colorPointer[newIdx2 + 2] = _colorPointer[i2 + 2];
_colorPointer[newIdx2 + 4] = _colorPointer[i2 + 4];
_colorPointer[newIdx2 + 5] = _colorPointer[i2 + 5];
_colorPointer[newIdx2 + 6] = _colorPointer[i2 + 6];
}
else
2019-11-23 20:27:39 +08:00
newIdx2 = newIdx * 8;
2021-12-25 10:04:45 +08:00
const uint8_t op = (uint8_t)(_pointState[newIdx] * 255.0f);
_colorPointer[newIdx2 + 3] = op;
_colorPointer[newIdx2 + 7] = op;
2019-11-23 20:27:39 +08:00
}
}
_nuPoints -= mov;
// Append new point
bool appendNewPoint = true;
if (_nuPoints >= _maxPoints)
appendNewPoint = false;
else if (_nuPoints > 0)
{
2021-12-25 10:04:45 +08:00
bool a1 = _pointVertexes[_nuPoints - 1].getDistanceSq(_positionR) < _minSeg;
bool a2 =
(_nuPoints == 1) ? false : (_pointVertexes[_nuPoints - 2].getDistanceSq(_positionR) < (_minSeg * 2.0f));
2019-11-23 20:27:39 +08:00
if (a1 || a2)
appendNewPoint = false;
}
if (appendNewPoint)
{
_pointVertexes[_nuPoints] = _positionR;
2021-12-25 10:04:45 +08:00
_pointState[_nuPoints] = 1.0f;
2019-11-23 20:27:39 +08:00
// Color assignment
2021-12-25 10:04:45 +08:00
const unsigned int offset = _nuPoints * 8;
*((Color3B*)(_colorPointer + offset)) = _displayedColor;
*((Color3B*)(_colorPointer + offset + 4)) = _displayedColor;
2019-11-23 20:27:39 +08:00
// Opacity
2021-12-25 10:04:45 +08:00
_colorPointer[offset + 3] = 255;
_colorPointer[offset + 7] = 255;
2019-11-23 20:27:39 +08:00
// Generate polygon
2021-12-25 10:04:45 +08:00
if (_nuPoints > 0 && _fastMode)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_nuPoints > 1)
2019-11-23 20:27:39 +08:00
{
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
}
else
{
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
}
}
_nuPoints++;
}
2021-12-25 10:04:45 +08:00
if (!_fastMode)
2019-11-23 20:27:39 +08:00
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
// Updated Tex Coords only if they are different than previous step
2021-12-25 10:04:45 +08:00
if (_nuPoints && _previousNuPoints != _nuPoints)
{
2019-11-23 20:27:39 +08:00
float texDelta = 1.0f / _nuPoints;
2021-12-25 10:04:45 +08:00
for (i = 0; i < _nuPoints; i++)
{
_texCoords[i * 2] = Tex2F(0, texDelta * i);
_texCoords[i * 2 + 1] = Tex2F(1, texDelta * i);
2019-11-23 20:27:39 +08:00
}
_previousNuPoints = _nuPoints;
}
}
void MotionStreak::reset()
{
_nuPoints = 0;
}
2021-12-25 10:04:45 +08:00
void MotionStreak::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_nuPoints <= 1)
2019-11-23 20:27:39 +08:00
return;
auto drawCount = _nuPoints * 2;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_customCommand.init(_globalZOrder, _blendFunc);
_customCommand.setVertexDrawInfo(0, drawCount);
renderer->addCommand(&_customCommand);
auto programState = _customCommand.getPipelineDescriptor().programState;
const auto& projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2021-12-25 10:04:45 +08:00
Mat4 finalMat = projectionMat * transform;
2019-11-23 20:27:39 +08:00
programState->setUniform(_mvpMatrixLocaiton, finalMat.m, sizeof(Mat4));
2021-12-25 10:04:45 +08:00
unsigned int offset = 0;
2019-11-23 20:27:39 +08:00
unsigned int vertexSize = sizeof(Vec2) + sizeof(Vec2) + sizeof(uint8_t) * 4;
for (unsigned int i = 0; i < drawCount; ++i)
{
offset = i * vertexSize;
_customCommand.updateVertexBuffer(&_vertices[i], offset, sizeof(_vertices[0]));
2021-12-25 10:04:45 +08:00
_customCommand.updateVertexBuffer(&_texCoords[i], offset + sizeof(_vertices[0]), sizeof(_texCoords[0]));
_customCommand.updateVertexBuffer(&_colorPointer[i * 4], offset + sizeof(_vertices[0]) + sizeof(_texCoords[0]),
4 * sizeof(uint8_t));
2019-11-23 20:27:39 +08:00
}
}
NS_AX_END