2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ShaderTest2.h"
|
|
|
|
#include "ShaderTest.h"
|
|
|
|
#include "../testResource.h"
|
2022-10-12 00:15:09 +08:00
|
|
|
#include "axmol.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
#include <tuple>
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#define SET_UNIFORM(ps, name, value) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
decltype(value) __v = value; \
|
|
|
|
auto __loc = (ps)->getUniformLocation(name); \
|
|
|
|
(ps)->setUniform(__loc, &__v, sizeof(__v)); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define SET_TEXTURE(ps, name, idx, value) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto* __v = value; \
|
|
|
|
auto __loc = (ps)->getUniformLocation(name); \
|
|
|
|
(ps)->setTexture(__loc, idx, __v); \
|
|
|
|
} while (false)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Shader2Tests::Shader2Tests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(EffectSpriteTest);
|
|
|
|
ADD_TEST_CASE(EffectSpriteLamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// EffectSprite
|
|
|
|
//
|
2021-12-31 12:12:40 +08:00
|
|
|
static int tuple_sort(const std::tuple<ssize_t, Effect*, QuadCommand>& tuple1,
|
|
|
|
const std::tuple<ssize_t, Effect*, QuadCommand>& tuple2)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return std::get<0>(tuple1) < std::get<0>(tuple2);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static void updateUniforms(backend::ProgramState* programState)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
float time = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();
|
|
|
|
Vec4 uTime(time / 10.0f, time, time * 2.0f, time * 4.0f);
|
|
|
|
Vec4 sinTime(time / 8.0f, time / 4.0f, time / 2.0f, sinf(time));
|
|
|
|
Vec4 cosTime(time / 8.0f, time / 4.0f, time / 2.0f, cosf(time));
|
|
|
|
|
|
|
|
SET_UNIFORM(programState, "u_Time", uTime);
|
|
|
|
SET_UNIFORM(programState, "u_SinTime", sinTime);
|
|
|
|
SET_UNIFORM(programState, "u_CosTime", cosTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Effect;
|
|
|
|
class EffectSprite : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
2021-12-31 12:12:40 +08:00
|
|
|
static EffectSprite* create(std::string_view filename)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new EffectSprite;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (ret->initWithFile(filename))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void setEffect(Effect* effect)
|
|
|
|
{
|
|
|
|
if (_defaultEffect != effect)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
effect->setTarget(this);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_defaultEffect);
|
2019-11-23 20:27:39 +08:00
|
|
|
_defaultEffect = effect;
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(_defaultEffect);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
setProgramState(_defaultEffect->getProgramState());
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
void addEffect(Effect* effect, ssize_t order)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
effect->retain();
|
|
|
|
effect->setTarget(this);
|
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
_effects.emplace_back(std::make_tuple(order, effect, QuadCommand()));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::sort(std::begin(_effects), std::end(_effects), tuple_sort);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_USE_CULLING
|
2019-11-23 20:27:39 +08:00
|
|
|
// Don't do calculate the culling if the transform was not updated
|
2021-12-31 12:12:40 +08:00
|
|
|
_insideBounds =
|
|
|
|
(flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_insideBounds)
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// negative effects: order < 0
|
2021-12-31 12:12:40 +08:00
|
|
|
int idx = 0;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&effect : _effects)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (std::get<0>(effect) >= 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
auto* programState = std::get<1>(effect)->getProgramState();
|
|
|
|
if (programState)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
QuadCommand& q = std::get<2>(effect);
|
2019-11-23 20:27:39 +08:00
|
|
|
q.init(_globalZOrder, _texture, _blendFunc, &_quad, 1, transform, flags);
|
|
|
|
updateUniforms(programState);
|
|
|
|
renderer->addCommand(&q);
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal effect: order == 0
|
|
|
|
_trianglesCommand.init(_globalZOrder, _texture, _blendFunc, _polyInfo.triangles, transform, flags);
|
|
|
|
|
|
|
|
updateUniforms(_trianglesCommand.getPipelineDescriptor().programState);
|
|
|
|
renderer->addCommand(&_trianglesCommand);
|
|
|
|
|
|
|
|
// positive effects: order >= 0
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it = std::begin(_effects) + idx; it != std::end(_effects); ++it)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
|
|
|
QuadCommand& q = std::get<2>(*it);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto* programState = std::get<1>(*it)->getProgramState();
|
|
|
|
updateUniforms(programState);
|
|
|
|
q.init(_globalZOrder, _texture, _blendFunc, &_quad, 1, transform, flags);
|
|
|
|
q.getPipelineDescriptor().programState = programState;
|
|
|
|
renderer->addCommand(&q);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
EffectSprite() : _defaultEffect(nullptr) { _effects.reserve(2); }
|
|
|
|
~EffectSprite()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&tuple : _effects)
|
2021-12-31 12:12:40 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::get<1>(tuple)->release();
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_defaultEffect);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::vector<std::tuple<ssize_t, Effect*, QuadCommand>> _effects;
|
2019-11-23 20:27:39 +08:00
|
|
|
Effect* _defaultEffect;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Effect
|
|
|
|
//
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool Effect::initProgramState(std::string_view fragmentFilename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
auto fileUtiles = FileUtils::getInstance();
|
|
|
|
auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename);
|
|
|
|
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
|
|
|
|
|
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
|
|
|
_fragSource = fragSource;
|
|
|
|
#endif
|
|
|
|
auto program = ProgramManager::newProgram(positionTextureColor_vert, fragSource, VertexLayoutHelper::setupSprite);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto programState = new backend::ProgramState(program);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_programState);
|
2023-09-01 16:28:34 +08:00
|
|
|
AX_SAFE_RELEASE(program);
|
2019-11-23 20:27:39 +08:00
|
|
|
_programState = programState;
|
|
|
|
|
|
|
|
return _programState != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Effect::Effect() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Effect::~Effect()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Blur
|
|
|
|
class EffectBlur : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectBlur);
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void setTarget(EffectSprite* sprite) override;
|
2019-11-23 20:27:39 +08:00
|
|
|
void setBlurRadius(float radius);
|
|
|
|
void setBlurSampleNum(float num);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(float blurRadius = 10.0f, float sampleNum = 5.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float _blurRadius;
|
|
|
|
float _blurSampleNum;
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EffectBlur::setTarget(EffectSprite* sprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_programState == nullptr)
|
|
|
|
return;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Size size = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "resolution", size);
|
|
|
|
SET_UNIFORM(_programState, "blurRadius", _blurRadius);
|
|
|
|
SET_UNIFORM(_programState, "sampleNum", _blurSampleNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EffectBlur::init(float blurRadius, float sampleNum)
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_Blur.fsh");
|
2021-12-31 12:12:40 +08:00
|
|
|
_blurRadius = blurRadius;
|
2019-11-23 20:27:39 +08:00
|
|
|
_blurSampleNum = sampleNum;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectBlur::setBlurRadius(float radius)
|
|
|
|
{
|
|
|
|
_blurRadius = radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectBlur::setBlurSampleNum(float num)
|
|
|
|
{
|
|
|
|
_blurSampleNum = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outline
|
|
|
|
class EffectOutline : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectOutline);
|
|
|
|
|
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_Outline.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Vec3 color(1.0f, 0.2f, 0.3f);
|
2021-12-31 12:12:40 +08:00
|
|
|
float radius = 0.01f;
|
2019-11-23 20:27:39 +08:00
|
|
|
float threshold = 1.75;
|
|
|
|
|
|
|
|
SET_UNIFORM(_programState, "u_outlineColor", color);
|
|
|
|
SET_UNIFORM(_programState, "u_radius", radius);
|
|
|
|
SET_UNIFORM(_programState, "u_threshold", threshold);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Noise
|
|
|
|
class EffectNoise : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectNoise);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_Noisy.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setTarget(EffectSprite* sprite) override
|
|
|
|
{
|
|
|
|
auto s = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "resolution", Vec2(s.width, s.height));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Edge Detect
|
|
|
|
class EffectEdgeDetect : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectEdgeDetect);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_EdgeDetection.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setTarget(EffectSprite* sprite) override
|
|
|
|
{
|
|
|
|
auto s = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "resolution", Vec2(s.width, s.height));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Grey
|
|
|
|
class EffectGreyScale : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectGreyScale);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_GreyScale.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sepia
|
|
|
|
class EffectSepia : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectSepia);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_Sepia.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// bloom
|
|
|
|
class EffectBloom : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectBloom);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_Bloom.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setTarget(EffectSprite* sprite) override
|
|
|
|
{
|
|
|
|
auto s = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "resolution", Vec2(s.width, s.height));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// cel shading
|
|
|
|
class EffectCelShading : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectCelShading);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_CelShading.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setTarget(EffectSprite* sprite) override
|
|
|
|
{
|
|
|
|
auto s = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "resolution", Vec2(s.width, s.height));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lens Flare
|
|
|
|
class EffectLensFlare : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectLensFlare);
|
|
|
|
|
|
|
|
protected:
|
2021-12-31 12:12:40 +08:00
|
|
|
bool init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders/example_LensFlare.fsh");
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setTarget(EffectSprite* sprite) override
|
|
|
|
{
|
|
|
|
auto s = sprite->getTexture()->getContentSizeInPixels();
|
|
|
|
SET_UNIFORM(_programState, "textureResolution", Vec2(s.width, s.height));
|
|
|
|
s = Director::getInstance()->getWinSize();
|
|
|
|
SET_UNIFORM(_programState, "resolution", Vec2(s.width, s.height));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EffectNormalMapped : public Effect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(EffectNormalMapped);
|
2021-12-31 12:12:40 +08:00
|
|
|
static EffectNormalMapped* create(std::string_view normalMapFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
EffectNormalMapped* normalMappedSprite = new EffectNormalMapped();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (normalMappedSprite->init() && normalMappedSprite->initNormalMap(normalMapFileName))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
normalMappedSprite->autorelease();
|
|
|
|
return normalMappedSprite;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(normalMappedSprite);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
void setKBump(float value);
|
|
|
|
void setLightPos(const Vec3& pos);
|
|
|
|
void setLightColor(const Color4F& color);
|
2021-12-31 12:12:40 +08:00
|
|
|
float getKBump() const { return _kBump; }
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
bool init();
|
2021-12-31 12:12:40 +08:00
|
|
|
bool initNormalMap(std::string_view normalMapFileName);
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void setTarget(EffectSprite* sprite) override;
|
|
|
|
EffectSprite* _sprite;
|
|
|
|
Vec3 _lightPos;
|
|
|
|
Color4F _lightColor;
|
2021-12-31 12:12:40 +08:00
|
|
|
float _kBump;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool EffectNormalMapped::init()
|
|
|
|
{
|
2023-09-01 16:28:34 +08:00
|
|
|
initProgramState("Shaders3D/Normal.frag");
|
2019-11-23 20:27:39 +08:00
|
|
|
_kBump = 2;
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
bool EffectNormalMapped::initNormalMap(std::string_view normalMapFileName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto normalMapTexture = Director::getInstance()->getTextureCache()->addImage(normalMapFileName);
|
|
|
|
SET_TEXTURE(_programState, "u_normalMap", 1, normalMapTexture->getBackendTexture());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void EffectNormalMapped::setTarget(EffectSprite* sprite)
|
|
|
|
{
|
|
|
|
_sprite = sprite;
|
|
|
|
SET_UNIFORM(_programState, "u_kBump", _kBump);
|
|
|
|
SET_UNIFORM(_programState, "u_contentSize", Vec2(sprite->getContentSize().width, sprite->getContentSize().height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectNormalMapped::setKBump(float value)
|
|
|
|
{
|
|
|
|
_kBump = value;
|
|
|
|
SET_UNIFORM(_programState, "u_kBump", _kBump);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectNormalMapped::setLightPos(const Vec3& pos)
|
|
|
|
{
|
|
|
|
_lightPos = pos;
|
|
|
|
SET_UNIFORM(_programState, "u_lightPosInLocalSpace", Vec4(_lightPos.x, _lightPos.y, _lightPos.z, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EffectNormalMapped::setLightColor(const Color4F& color)
|
|
|
|
{
|
|
|
|
_lightColor = color;
|
|
|
|
SET_UNIFORM(_programState, "u_diffuseL", Vec3(_lightColor.r, _lightColor.g, _lightColor.b));
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
EffectSpriteTest::EffectSpriteTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool EffectSpriteTest::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto layer = LayerColor::create(Color4B::BLUE);
|
|
|
|
this->addChild(layer);
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto itemPrev = MenuItemImage::create("Images/b1.png", "Images/b2.png", [&](Ref* sender) {
|
|
|
|
_vectorIndex--;
|
|
|
|
if (_vectorIndex < 0)
|
|
|
|
_vectorIndex = _effects.size() - 1;
|
|
|
|
_sprite->setEffect(_effects.at(_vectorIndex));
|
|
|
|
});
|
|
|
|
|
|
|
|
auto itemNext = MenuItemImage::create("Images/f1.png", "Images/f2.png", [&](Ref* sender) {
|
|
|
|
_vectorIndex++;
|
|
|
|
if (_vectorIndex >= _effects.size())
|
|
|
|
_vectorIndex = 0;
|
|
|
|
_sprite->setEffect(_effects.at(_vectorIndex));
|
|
|
|
});
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(itemPrev, itemNext, nullptr);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setScale(0.5);
|
2021-12-31 12:12:40 +08:00
|
|
|
menu->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
menu->setPosition(Vec2(s.width / 2, 70.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(menu);
|
|
|
|
|
|
|
|
_sprite = EffectSprite::create("Images/grossini.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
_sprite->setPosition(Vec2(0.0f, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(_sprite);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto jump = JumpBy::create(4, Vec2(s.width, 0.0f), 100, 4);
|
|
|
|
auto rot = RotateBy::create(4, 720);
|
|
|
|
auto spawn = Spawn::create(jump, rot, nullptr);
|
|
|
|
auto rev = spawn->reverse();
|
|
|
|
auto seq = Sequence::create(spawn, rev, nullptr);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto repeat = RepeatForever::create(seq);
|
|
|
|
_sprite->runAction(repeat);
|
|
|
|
|
|
|
|
// set the Effects
|
|
|
|
_effects.pushBack(EffectBlur::create());
|
|
|
|
_effects.pushBack(EffectOutline::create());
|
|
|
|
_effects.pushBack(EffectNoise::create());
|
|
|
|
_effects.pushBack(EffectEdgeDetect::create());
|
|
|
|
_effects.pushBack(EffectGreyScale::create());
|
|
|
|
_effects.pushBack(EffectSepia::create());
|
|
|
|
_effects.pushBack(EffectBloom::create());
|
|
|
|
_effects.pushBack(EffectCelShading::create());
|
|
|
|
_effects.pushBack(EffectLensFlare::create());
|
|
|
|
|
|
|
|
_vectorIndex = 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
_sprite->setEffect(_effects.at(_vectorIndex));
|
|
|
|
|
|
|
|
// _sprite->addEffect( _effects.at(8), -10 );
|
|
|
|
// _sprite->addEffect( _effects.at(1), 1 );
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
EffectSpriteLamp::EffectSpriteLamp() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool EffectSpriteLamp::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
_sprite = EffectSprite::create("Images/elephant1_Diffuse.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
// auto contentSize = _sprite->getContentSize();
|
|
|
|
_sprite->setPosition(Vec2(s.width / 2, s.height / 2));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(_sprite);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto lampEffect = EffectNormalMapped::create("Images/elephant1_Normal.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
Vec3 pos(150, 150, 50);
|
2019-11-23 20:27:39 +08:00
|
|
|
_lightSprite = Sprite::create("Images/ball.png");
|
|
|
|
this->addChild(_lightSprite);
|
2021-12-31 12:12:40 +08:00
|
|
|
_lightSprite->setPosition(Vec2(pos.x, s.height - pos.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 mat = _sprite->getNodeToWorldTransform();
|
2021-12-31 12:12:40 +08:00
|
|
|
Point lightPosInLocalSpace =
|
|
|
|
PointApplyAffineTransform(Vec2(pos.x, pos.y), _sprite->getWorldToNodeAffineTransform());
|
|
|
|
lampEffect->setLightColor(Color4F(1, 1, 1, 1));
|
2019-11-23 20:27:39 +08:00
|
|
|
lampEffect->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50.0f));
|
|
|
|
lampEffect->setKBump(2);
|
|
|
|
_sprite->setEffect(lampEffect);
|
2021-12-31 12:12:40 +08:00
|
|
|
_effect = lampEffect;
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
listener->onTouchesBegan = AX_CALLBACK_2(EffectSpriteLamp::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = AX_CALLBACK_2(EffectSpriteLamp::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = AX_CALLBACK_2(EffectSpriteLamp::onTouchesEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EffectSpriteLamp::onTouchesBegan(const std::vector<Touch*>& touches, Event* unused_event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&item : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto touch = item;
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Point loc_winSpace = touch->getLocationInView();
|
2021-12-31 12:12:40 +08:00
|
|
|
_lightSprite->setPosition(Vec2(loc_winSpace.x, s.height - loc_winSpace.y));
|
|
|
|
Vec3 pos(loc_winSpace.x, loc_winSpace.y, 50);
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 mat = _sprite->getNodeToWorldTransform();
|
2021-12-31 12:12:40 +08:00
|
|
|
Point lightPosInLocalSpace =
|
|
|
|
PointApplyAffineTransform(Vec2(pos.x, pos.y), _sprite->getWorldToNodeAffineTransform());
|
2019-11-23 20:27:39 +08:00
|
|
|
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EffectSpriteLamp::onTouchesMoved(const std::vector<Touch*>& touches, Event* unused_event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&item : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto touch = item;
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Point loc_winSpace = touch->getLocationInView();
|
2021-12-31 12:12:40 +08:00
|
|
|
_lightSprite->setPosition(Vec2(loc_winSpace.x, s.height - loc_winSpace.y));
|
|
|
|
Vec3 pos(loc_winSpace.x, loc_winSpace.y, 50);
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 mat = _sprite->getNodeToWorldTransform();
|
2021-12-31 12:12:40 +08:00
|
|
|
Point lightPosInLocalSpace =
|
|
|
|
PointApplyAffineTransform(Vec2(pos.x, pos.y), _sprite->getWorldToNodeAffineTransform());
|
2019-11-23 20:27:39 +08:00
|
|
|
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void EffectSpriteLamp::onTouchesEnded(const std::vector<Touch*>& touches, Event* unused_event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&&item : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto touch = item;
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
Point loc_winSpace = touch->getLocationInView();
|
2021-12-31 12:12:40 +08:00
|
|
|
_lightSprite->setPosition(Vec2(loc_winSpace.x, s.height - loc_winSpace.y));
|
|
|
|
Vec3 pos(loc_winSpace.x, loc_winSpace.y, 50);
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 mat = _sprite->getNodeToWorldTransform();
|
2021-12-31 12:12:40 +08:00
|
|
|
Point lightPosInLocalSpace =
|
|
|
|
PointApplyAffineTransform(Vec2(pos.x, pos.y), _sprite->getWorldToNodeAffineTransform());
|
2019-11-23 20:27:39 +08:00
|
|
|
((EffectNormalMapped*)_effect)->setLightPos(Vec3(lightPosInLocalSpace.x, lightPosInLocalSpace.y, 50.0f));
|
|
|
|
}
|
|
|
|
}
|