diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c56475c..dce4ba40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,6 +190,8 @@ set(SOURCES sources/Component/Color/ColorComponent.cpp sources/Component/Stat/StatComponent.cpp sources/Component/Stat/StatComponent.hpp + sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp + sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp sources/Component/Speed/SpeedComponent.cpp sources/Component/Speed/SpeedComponent.hpp ) diff --git a/assets/shaders/explosion.vs b/assets/shaders/explosion.vs index 26dd04b1..9e1e1fde 100644 --- a/assets/shaders/explosion.vs +++ b/assets/shaders/explosion.vs @@ -104,7 +104,7 @@ varying vec3 fragNormal; void main() { // Send vertex attributes to fragment shader - fragPosition = vertexPosition + vertexPosition * vec3(cnoise(vec3(vertexNormal + vec3(frame))) * 0.5); + fragPosition = vertexPosition + vertexPosition * vec3(cnoise(vec3(vertexNormal + center + vec3(frame))) * 0.5); fragColor = vertexColor; fragNormal = vertexNormal; fragTexCoord = vertexTexCoord; diff --git a/lib/Ray/sources/Shaders/Shaders.cpp b/lib/Ray/sources/Shaders/Shaders.cpp index 0def3c96..811fe34a 100644 --- a/lib/Ray/sources/Shaders/Shaders.cpp +++ b/lib/Ray/sources/Shaders/Shaders.cpp @@ -6,6 +6,7 @@ #include #include "Exceptions/RayError.hpp" +#include "Vector/Vector3.hpp" namespace RAY { @@ -38,6 +39,7 @@ namespace RAY SetShaderValue(*this->_rayLibShader, this->_shaderIndexVars[varName], &value, SHADER_UNIFORM_FLOAT); } + void Shader::setShaderUniformVar(const std::string &varName, int value) { if (this->_shaderIndexVars.find(varName) == this->_shaderIndexVars.end()) { @@ -60,4 +62,18 @@ namespace RAY { EndShaderMode(); } + + void Shader::setShaderUniformVar(const std::string &varName, const RAY::Vector3 &vector) + { + if (this->_shaderIndexVars.find(varName) == this->_shaderIndexVars.end()) { + int varShaderIndex = GetShaderLocation(*this->_rayLibShader, varName.c_str()); + + if (varShaderIndex < 0) { + throw Exception::WrongInputError("The loaded shader doesn't have a variable called: " + varName); + } + this->_shaderIndexVars[varName] = varShaderIndex; + } + SetShaderValue(*this->_rayLibShader, this->_shaderIndexVars[varName], &vector, SHADER_UNIFORM_VEC3); + } + } \ No newline at end of file diff --git a/lib/Ray/sources/Shaders/Shaders.hpp b/lib/Ray/sources/Shaders/Shaders.hpp index 20231ea7..a4616670 100644 --- a/lib/Ray/sources/Shaders/Shaders.hpp +++ b/lib/Ray/sources/Shaders/Shaders.hpp @@ -9,6 +9,7 @@ #include #include #include "Utils/Cache.hpp" +#include "Vector/Vector3.hpp" namespace RAY { @@ -46,6 +47,8 @@ namespace RAY //! @note Throw if the var is not found void setShaderUniformVar(const std::string &varName, int value); + void setShaderUniformVar(const std::string &varName, const RAY::Vector3 &vector); + void setLocation(::ShaderLocationIndex, const std::string &name); //! @brief ctor if no vertexfile in needed set it to nullptr diff --git a/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp new file mode 100644 index 00000000..49634922 --- /dev/null +++ b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp @@ -0,0 +1,20 @@ +// +// Created by cbihan on 18/06/2021. +// + +#include "AlphaCtxShaderComponent.hpp" + + +namespace BBM +{ + + AlphaVarShaderComponent::AlphaVarShaderComponent(WAL::Entity &entity) : + WAL::Component(entity) + { + } + + WAL::Component *AlphaVarShaderComponent::clone(WAL::Entity &entity) const + { + return new AlphaVarShaderComponent(this->_entity); + } +} \ No newline at end of file diff --git a/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp new file mode 100644 index 00000000..5408d091 --- /dev/null +++ b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp @@ -0,0 +1,51 @@ +// +// Created by cbihan on 18/06/2021. +// + +#pragma once + +#include +#include + +using namespace std::chrono_literals; + +namespace BBM +{ + class AlphaVarShaderComponent : public WAL::Component + { + public: + //! @brief Transparency + float alpha = 1; + + //! @brief minimum transparency + float minAlpha = 0.2; + //! @brief maximum transparency + float maxAlpha = 1; + //! @brief inital step value + float initalStepValue = 0.04; + //! @brief how fast the alpha will vary + float step = 0.04; + //! @brief if the alpha should increase or decrease + float balance = -1; + + //! @brief The clock to use + std::chrono::nanoseconds clock = 0ns; + + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief ctor + explicit AlphaVarShaderComponent(WAL::Entity &entity); + + //! @brief Default copy ctor + AlphaVarShaderComponent(const AlphaVarShaderComponent &) = default; + + //! @brief Default dtor + ~AlphaVarShaderComponent() override = default; + + //! @brief Default assignment operator + AlphaVarShaderComponent &operator=(const AlphaVarShaderComponent &) = delete; + }; + +} \ No newline at end of file diff --git a/sources/Items/Bonus.cpp b/sources/Items/Bonus.cpp index ff7828be..c2926ab6 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -59,6 +59,7 @@ namespace BBM { if (!playerBonus) return; playerBonus->nextNoClipRate = playerBonus->noClipBonusRate; + playerBonus->isNoClipOn = true; const_cast(bonus).scheduleDeletion(); } diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index 9bc3c632..489be7ee 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -19,9 +19,11 @@ #include "Component/BombHolder/BombHolderComponent.hpp" #include "Component/Tag/TagComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" +#include "Component/Shaders/Items/AlphaCtxShaderComponent.hpp" #include "Component/Speed/SpeedComponent.hpp" #include "Component/Renderer/Drawable2DComponent.hpp" #include +#include "Component/Shaders/ShaderComponent.hpp" #include "Drawables/Texture.hpp" #include "Component/Gravity/GravityComponent.hpp" #include "Component/BumperTimer/BumperTimerComponent.hpp" @@ -70,6 +72,44 @@ namespace BBM .addComponent("assets/player/player.iqm", 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() + .addComponent() + .addComponent("assets/shaders/alpha.fs", "", [](WAL::Entity &myEntity, WAL::Wal &wal, std::chrono::nanoseconds dtime) { + auto &ctx = myEntity.getComponent(); + + ctx.clock += dtime; + if (duration_cast(ctx.clock).count() <= 10) + return; + ctx.clock = 0ns; + auto &bonus = myEntity.getComponent(); + auto &shader = myEntity.getComponent(); + + if (!bonus.isNoClipOn) { + ctx.alpha = ctx.maxAlpha; + shader.shader.setShaderUniformVar("alpha", ctx.alpha); + return; + } + + auto nbMilliSec = duration_cast(bonus.nextNoClipRate).count(); + + if (nbMilliSec > 1500) { + ctx.step = ctx.initalStepValue; + } else if (nbMilliSec > 1000) { + ctx.step = 0.15; + } else if (nbMilliSec > 200) { + ctx.step = 0.30; + } else { + ctx.step = 0.5; + } + ctx.alpha += static_cast(ctx.step * ctx.balance); + + if (ctx.alpha <= ctx.minAlpha) { + ctx.balance = 1; + } + if (ctx.alpha >= ctx.maxAlpha) { + ctx.balance = -1; + } + shader.shader.setShaderUniformVar("alpha", ctx.alpha); + }, true) .addComponent(soundPath) .addComponent("assets/musics/music_battle.ogg") .addComponent() diff --git a/sources/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index f2a2b398..9474e2a5 100644 --- a/sources/System/BombHolder/BombHolderSystem.cpp +++ b/sources/System/BombHolder/BombHolderSystem.cpp @@ -53,6 +53,7 @@ namespace BBM .addComponent("assets/shaders/explosion.fs", "assets/shaders/explosion.vs", [](WAL::Entity &entity, WAL::Wal &, std::chrono::nanoseconds dtime) { auto &ctx = entity.getComponent(); auto &shader = entity.getComponent(); + auto &pos = entity.getComponent(); ctx.clock += dtime; if (duration_cast(ctx.clock).count() <= 10) @@ -69,7 +70,8 @@ namespace BBM shader.shader.setShaderUniformVar("frame", ctx.frameCounter); shader.shader.setShaderUniformVar("alpha", ctx.alpha); shader.shader.setShaderUniformVar("radius", ctx.explosionRadius); - }) + shader.shader.setShaderUniformVar("center", pos.position); + }, true) .addComponent(500ms, [](WAL::Entity &explosion, WAL::Wal &) { explosion.scheduleDeletion(); })