axmol/core/3d/CCMotionStreak3D.cpp

466 lines
13 KiB
C++
Raw Normal View History

2015-08-28 11:40:12 +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.
2015-08-28 11:40:12 +08:00
https://axis-project.github.io/
2015-08-28 11:40:12 +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 "3d/CCMotionStreak3D.h"
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
2015-08-28 11:40:12 +08:00
#include "math/CCVertex.h"
#include "base/CCDirector.h"
2018-09-11 14:39:30 +08:00
#include "base/ccUtils.h"
2015-08-28 11:40:12 +08:00
#include "renderer/CCTextureCache.h"
#include "renderer/CCTexture2D.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCRenderState.h"
2019-02-20 16:56:35 +08:00
#include "renderer/ccShaders.h"
2015-08-28 11:40:12 +08:00
NS_AX_BEGIN
2015-08-28 11:40:12 +08:00
MotionStreak3D::MotionStreak3D()
2021-12-25 10:04:45 +08:00
: _startingPositionInitialized(false)
, _texture(nullptr)
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _positionR2D(0.f, 0.f)
, _sweepAxis(0.f, 1.f, 0.f)
, _stroke(0.0f)
, _fadeDelta(0.0f)
, _minSeg(0.0f)
, _maxPoints(0)
, _nuPoints(0)
, _previousNuPoints(0)
{}
2015-08-28 11:40:12 +08:00
MotionStreak3D::~MotionStreak3D()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_texture);
2015-08-28 11:40:12 +08:00
}
2021-12-25 10:04:45 +08:00
MotionStreak3D* MotionStreak3D::create(float fade,
float minSeg,
float stroke,
const Color3B& color,
2021-12-26 23:26:34 +08:00
std::string_view path)
2015-08-28 11:40:12 +08:00
{
2021-12-25 10:04:45 +08:00
MotionStreak3D* ret = new MotionStreak3D();
2015-08-28 11:40:12 +08:00
if (ret && ret->initWithFade(fade, minSeg, stroke, color, path))
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-08-28 11:40:12 +08:00
return nullptr;
}
MotionStreak3D* MotionStreak3D::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
2021-12-25 10:04:45 +08:00
MotionStreak3D* ret = new MotionStreak3D();
2015-08-28 11:40:12 +08:00
if (ret && ret->initWithFade(fade, minSeg, stroke, color, texture))
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
2015-08-28 11:40:12 +08:00
return nullptr;
}
2021-12-26 23:26:34 +08:00
bool MotionStreak3D::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, std::string_view path)
2015-08-28 11:40:12 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(!path.empty(), "Invalid filename");
2015-08-28 11:40:12 +08:00
2021-12-25 10:04:45 +08:00
Texture2D* texture = _director->getTextureCache()->addImage(path);
2015-08-28 11:40:12 +08:00
return initWithFade(fade, minSeg, stroke, color, texture);
}
bool MotionStreak3D::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
Node::setPosition(Vec2::ZERO);
setAnchorPoint(Vec2::ZERO);
2016-04-22 20:36:02 +08:00
setIgnoreAnchorPointForPosition(true);
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = false;
_positionR.setZero();
2021-12-25 10:04:45 +08:00
_minSeg = (minSeg == -1.0f) ? stroke / 5.0f : minSeg;
2015-08-28 11:40:12 +08:00
_minSeg *= _minSeg;
2021-12-25 10:04:45 +08:00
_stroke = stroke;
_fadeDelta = 1.0f / fade;
_maxPoints = (int)(fade * 60.0f) + 2;
_nuPoints = 0;
2015-08-28 11:40:12 +08:00
2019-02-20 16:56:35 +08:00
_pointState.resize(_maxPoints);
_pointVertexes.resize(_maxPoints);
2015-08-28 11:40:12 +08:00
2019-02-20 16:56:35 +08:00
_vertexData.resize(_maxPoints * 2);
2015-08-28 11:40:12 +08:00
// Set blend mode
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
// shader state
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
setProgramState(new backend::ProgramState(program), false);
2019-02-20 16:56:35 +08:00
_customCommand.getPipelineDescriptor().programState = _programState;
initCustomCommand();
2015-08-28 11:40:12 +08:00
setTexture(texture);
setColor(color);
scheduleUpdate();
return true;
}
2019-02-20 16:56:35 +08:00
void MotionStreak3D::initCustomCommand()
{
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
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
{
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
2019-03-12 19:40:13 +08:00
}
2021-12-25 10:04:45 +08:00
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(VertexData, color), true);
2019-03-12 19:40:13 +08:00
}
2021-12-25 10:04:45 +08:00
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(VertexData, texPos), false);
2019-03-12 19:40:13 +08:00
}
layout->setLayout(sizeof(VertexData));
2019-02-20 16:56:35 +08:00
2021-12-25 10:04:45 +08:00
auto& blend = pipelineDescriptor.blendDescriptor;
blend.blendEnabled = true;
2019-02-20 16:56:35 +08:00
blend.sourceAlphaBlendFactor = blend.sourceRGBBlendFactor = _blendFunc.src;
blend.destinationAlphaBlendFactor = blend.destinationRGBBlendFactor = _blendFunc.dst;
2021-12-25 10:04:45 +08:00
_locMVP = _programState->getUniformLocation("u_MVPMatrix");
2022-07-04 21:26:48 +08:00
_locTexture = _programState->getUniformLocation("u_tex0");
2019-02-20 16:56:35 +08:00
_customCommand.createVertexBuffer(sizeof(VertexData), _vertexData.size(), CustomCommand::BufferUsage::DYNAMIC);
}
2015-08-28 11:40:12 +08:00
void MotionStreak3D::setPosition(const Vec2& position)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = true;
}
_positionR = Vec3(position.x, position.y, 0);
}
void MotionStreak3D::setPosition(float x, float y)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = true;
}
_positionR.x = x;
_positionR.y = y;
}
void MotionStreak3D::setPosition3D(const Vec3& position)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = true;
}
_positionR = position;
}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::setRotation3D(const Vec3& /*rotation*/) {}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::setRotationQuat(const Quaternion& /*quat*/) {}
2015-08-28 11:40:12 +08:00
const Vec2& MotionStreak3D::getPosition() const
{
_positionR2D.x = _positionR.x;
_positionR2D.y = _positionR.y;
return _positionR2D;
}
void MotionStreak3D::getPosition(float* x, float* y) const
{
*x = _positionR.x;
*y = _positionR.y;
}
float MotionStreak3D::getPositionX() const
{
return _positionR.x;
}
Vec3 MotionStreak3D::getPosition3D() const
{
return Vec3(_positionR.x, _positionR.y, getPositionZ());
}
void MotionStreak3D::setPositionX(float x)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = true;
}
_positionR.x = x;
}
float MotionStreak3D::getPositionY() const
{
2021-12-25 10:04:45 +08:00
return _positionR.y;
2015-08-28 11:40:12 +08:00
}
void MotionStreak3D::setPositionY(float y)
{
2021-12-25 10:04:45 +08:00
if (!_startingPositionInitialized)
{
2015-08-28 11:40:12 +08:00
_startingPositionInitialized = true;
}
_positionR.y = y;
}
void MotionStreak3D::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++)
2015-08-28 11:40:12 +08:00
{
2021-12-25 10:04:45 +08:00
auto& color = _vertexData[i].color;
2019-02-20 16:56:35 +08:00
color.set(colors.r, colors.g, colors.b, color.a);
2015-08-28 11:40:12 +08:00
}
}
Texture2D* MotionStreak3D::getTexture() const
2015-08-28 11:40:12 +08:00
{
return _texture;
}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::setTexture(Texture2D* texture)
2015-08-28 11:40:12 +08:00
{
if (_texture != texture)
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(texture);
AX_SAFE_RELEASE(_texture);
2015-08-28 11:40:12 +08:00
_texture = texture;
2019-02-20 16:56:35 +08:00
_programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
2015-08-28 11:40:12 +08:00
}
}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::setBlendFunc(const BlendFunc& blendFunc)
2015-08-28 11:40:12 +08:00
{
_blendFunc = blendFunc;
}
const BlendFunc& MotionStreak3D::getBlendFunc() const
2015-08-28 11:40:12 +08:00
{
return _blendFunc;
}
2019-06-05 17:58:33 +08:00
void MotionStreak3D::setOpacity(uint8_t /*opacity*/)
2015-08-28 11:40:12 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Set opacity no supported");
2015-08-28 11:40:12 +08:00
}
uint8_t MotionStreak3D::getOpacity() const
2015-08-28 11:40:12 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Opacity no supported");
2015-08-28 11:40:12 +08:00
return 0;
}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::setOpacityModifyRGB(bool /*bValue*/) {}
2015-08-28 11:40:12 +08:00
bool MotionStreak3D::isOpacityModifyRGB() const
2015-08-28 11:40:12 +08:00
{
return false;
}
void MotionStreak3D::update(float delta)
{
if (!_startingPositionInitialized)
{
return;
}
2021-12-25 10:04:45 +08:00
2015-08-28 11:40:12 +08:00
delta *= _fadeDelta;
unsigned int newIdx, newIdx2, i, i2;
unsigned int mov = 0;
// Update current points
2021-12-25 10:04:45 +08:00
for (i = 0; i < _nuPoints; i++)
2015-08-28 11:40:12 +08:00
{
2021-12-25 10:04:45 +08:00
_pointState[i] -= delta;
2015-08-28 11:40:12 +08:00
2021-12-25 10:04:45 +08:00
if (_pointState[i] <= 0)
2015-08-28 11:40:12 +08:00
mov++;
else
{
2021-12-25 10:04:45 +08:00
newIdx = i - mov;
2015-08-28 11:40:12 +08:00
2021-12-25 10:04:45 +08:00
if (mov > 0)
2015-08-28 11:40:12 +08:00
{
// 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;
_vertexData[newIdx2].pos = _vertexData[i2].pos;
_vertexData[newIdx2 + 1].pos = _vertexData[i2 + 1].pos;
2015-08-28 11:40:12 +08:00
// Move color
2021-12-25 10:04:45 +08:00
_vertexData[newIdx2].color = _vertexData[i2].color;
2019-02-20 16:56:35 +08:00
_vertexData[newIdx2 + 1].color = _vertexData[i2 + 1].color;
2021-12-25 10:04:45 +08:00
}
else
newIdx2 = newIdx * 2;
2015-08-28 11:40:12 +08:00
2021-12-25 10:04:45 +08:00
const uint8_t op = (uint8_t)(_pointState[newIdx] * 255.0f);
_vertexData[newIdx2].color.a = op;
_vertexData[newIdx2 + 1].color.a = op;
2015-08-28 11:40:12 +08:00
}
}
2021-12-25 10:04:45 +08:00
_nuPoints -= mov;
2015-08-28 11:40:12 +08:00
// Append new point
bool appendNewPoint = true;
2021-12-25 10:04:45 +08:00
if (_nuPoints >= _maxPoints)
2015-08-28 11:40:12 +08:00
{
appendNewPoint = false;
}
2021-12-25 10:04:45 +08:00
else if (_nuPoints > 0)
2015-08-28 11:40:12 +08:00
{
2021-12-25 10:04:45 +08:00
bool a1 = (_pointVertexes[_nuPoints - 1] - _positionR).lengthSquared() < _minSeg;
bool a2 = (_nuPoints == 1) ? false
: ((_pointVertexes[_nuPoints - 2] - _positionR).lengthSquared() < (_minSeg * 2.0f));
if (a1 || a2)
2015-08-28 11:40:12 +08:00
{
appendNewPoint = false;
}
}
2021-12-25 10:04:45 +08:00
if (appendNewPoint)
2015-08-28 11:40:12 +08:00
{
_pointVertexes[_nuPoints] = _positionR;
2021-12-25 10:04:45 +08:00
_pointState[_nuPoints] = 1.0f;
2015-08-28 11:40:12 +08:00
// Color assignment
2021-12-25 10:04:45 +08:00
_vertexData[_nuPoints * 2].color = Color4B(_displayedColor, 255);
2019-02-20 16:56:35 +08:00
_vertexData[_nuPoints * 2 + 1].color = Color4B(_displayedColor, 255);
2015-08-28 11:40:12 +08:00
// Generate polygon
{
2021-12-25 10:04:45 +08:00
float stroke = _stroke * 0.5f;
_vertexData[_nuPoints * 2].pos = _pointVertexes[_nuPoints] + (_sweepAxis * stroke);
2019-02-20 16:56:35 +08:00
_vertexData[_nuPoints * 2 + 1].pos = _pointVertexes[_nuPoints] - (_sweepAxis * stroke);
2015-08-28 11:40:12 +08:00
}
2021-12-25 10:04:45 +08:00
_nuPoints++;
2015-08-28 11:40:12 +08:00
}
// Updated Tex Coords only if they are different than previous step
2021-12-25 10:04:45 +08:00
if (_nuPoints && _previousNuPoints != _nuPoints)
{
2015-08-28 11:40:12 +08:00
float texDelta = 1.0f / _nuPoints;
2021-12-25 10:04:45 +08:00
for (i = 0; i < _nuPoints; i++)
{
_vertexData[i * 2].texPos = Tex2F(0, texDelta * i);
_vertexData[i * 2 + 1].texPos = Tex2F(1, texDelta * i);
2015-08-28 11:40:12 +08:00
}
_previousNuPoints = _nuPoints;
}
}
void MotionStreak3D::reset()
{
_nuPoints = 0;
}
2021-12-25 10:04:45 +08:00
void MotionStreak3D::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2015-08-28 11:40:12 +08:00
{
2021-12-25 10:04:45 +08:00
if (_nuPoints <= 1)
2015-08-28 11:40:12 +08:00
return;
2022-06-24 14:18:48 +08:00
auto beforeCommand = renderer->nextCallbackCommand();
auto afterCommand = renderer->nextCallbackCommand();
beforeCommand->init(_globalZOrder);
afterCommand->init(_globalZOrder);
2015-08-28 11:40:12 +08:00
_customCommand.init(_globalZOrder, transform, flags);
2021-12-25 10:04:45 +08:00
auto pmatrix = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2019-02-20 16:56:35 +08:00
auto mvpMatrix = pmatrix * transform;
2021-12-25 10:04:45 +08:00
2019-02-20 16:56:35 +08:00
_programState->setUniform(_locMVP, mvpMatrix.m, sizeof(mvpMatrix.m));
2022-07-15 19:17:01 +08:00
beforeCommand->func = AX_CALLBACK_0(MotionStreak3D::onBeforeDraw, this);
afterCommand->func = AX_CALLBACK_0(MotionStreak3D::onAfterDraw, this);
2021-12-25 10:04:45 +08:00
2019-02-20 16:56:35 +08:00
_customCommand.updateVertexBuffer(_vertexData.data(), sizeof(_vertexData[0]) * _nuPoints * 2);
_customCommand.setVertexDrawInfo(0, _nuPoints * 2);
2022-06-24 14:18:48 +08:00
renderer->addCommand(beforeCommand);
2015-08-28 11:40:12 +08:00
renderer->addCommand(&_customCommand);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterCommand);
2022-07-15 19:17:01 +08:00
AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints * 2);
2019-02-20 16:56:35 +08:00
}
void MotionStreak3D::onBeforeDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = _director->getRenderer();
2019-02-20 16:56:35 +08:00
_rendererDepthTest = renderer->getDepthTest();
2021-12-25 10:04:45 +08:00
_rendererCullface = renderer->getCullMode();
2019-02-20 16:56:35 +08:00
renderer->setCullMode(CullMode::NONE);
renderer->setDepthTest(true);
}
void MotionStreak3D::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = _director->getRenderer();
2019-02-20 16:56:35 +08:00
renderer->setDepthTest(_rendererDepthTest);
renderer->setCullMode(_rendererCullface);
2015-08-28 11:40:12 +08:00
}
NS_AX_END