From bc8504a0262c96eba39b2a46d5acac11affb3db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 17 Jun 2021 18:22:59 +0200 Subject: [PATCH 1/4] adding position in shader --- assets/shaders/explosion.vs | 2 +- lib/Ray/sources/Shaders/Shaders.cpp | 16 ++++++++++++++++ lib/Ray/sources/Shaders/Shaders.hpp | 3 +++ sources/System/BombHolder/BombHolderSystem.cpp | 4 +++- 4 files changed, 23 insertions(+), 2 deletions(-) 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/System/BombHolder/BombHolderSystem.cpp b/sources/System/BombHolder/BombHolderSystem.cpp index 02ed40aa..d792bec8 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 &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 &wal) { explosion.scheduleDeletion(); }) From cdba078231f530b62dc01b5ad9fb86980972b9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Fri, 18 Jun 2021 16:25:20 +0200 Subject: [PATCH 2/4] first correct no clip version --- CMakeLists.txt | 2 +- .../Shaders/Items/AlphaCtxShaderComponent.cpp | 20 ++++++++ .../Shaders/Items/AlphaCtxShaderComponent.hpp | 51 +++++++++++++++++++ sources/Items/Bonus.cpp | 3 +- sources/Runner/GameScene.cpp | 40 +++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp create mode 100644 sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 38fa4806..ab2f7c6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -176,7 +176,7 @@ 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) add_executable(bomberman sources/main.cpp 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 6a579d43..0ebfcb25 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -58,6 +58,7 @@ namespace BBM { if (!playerBonus) return; playerBonus->nextNoClipRate = playerBonus->noClipBonusRate; + playerBonus->isNoClipOn = true; const_cast(bonus).scheduleDeletion(); } @@ -70,7 +71,7 @@ namespace BBM { {SPEEDUP, 45.0f}, {BOMBSTOCK, 30.0f}, {EXPLOSIONINC, 15.0f}, - {NOCLIP, 1.5f}, + {NOCLIP, 70.5f}, }; std::uniform_int_distribution distribution(1,1000); float value = (distribution(generator) / 10); diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index d4e5a382..6c7f8fd7 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -16,8 +16,10 @@ #include "Component/BombHolder/BombHolderComponent.hpp" #include "Component/Tag/TagComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" +#include "Component/Shaders/Items/AlphaCtxShaderComponent.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" @@ -66,6 +68,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 > 1000) { + ctx.step = ctx.initalStepValue; + } else if (nbMilliSec > 500) { + ctx.step = 0.15; + } else if (nbMilliSec > 100) { + 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() From afc1fda08e84ce5aa8b6e7912658e0c911cdd95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Fri, 18 Jun 2021 16:28:30 +0200 Subject: [PATCH 3/4] resetting correct noclip spawn rate --- sources/Items/Bonus.cpp | 2 +- sources/Runner/GameScene.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sources/Items/Bonus.cpp b/sources/Items/Bonus.cpp index 0ebfcb25..c34691bb 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -71,7 +71,7 @@ namespace BBM { {SPEEDUP, 45.0f}, {BOMBSTOCK, 30.0f}, {EXPLOSIONINC, 15.0f}, - {NOCLIP, 70.5f}, + {NOCLIP, 1.5f}, }; std::uniform_int_distribution distribution(1,1000); float value = (distribution(generator) / 10); diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index 6c7f8fd7..2327cc36 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -87,11 +87,11 @@ namespace BBM auto nbMilliSec = duration_cast(bonus.nextNoClipRate).count(); - if (nbMilliSec > 1000) { + if (nbMilliSec > 1500) { ctx.step = ctx.initalStepValue; - } else if (nbMilliSec > 500) { + } else if (nbMilliSec > 1000) { ctx.step = 0.15; - } else if (nbMilliSec > 100) { + } else if (nbMilliSec > 200) { ctx.step = 0.30; } else { ctx.step = 0.5; From cafe6bbd89889004a1cc0a368e703580f6f3dc8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Fri, 18 Jun 2021 18:25:00 +0200 Subject: [PATCH 4/4] fixing CMakeLists.txt --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab2f7c6f..951854b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -176,7 +176,9 @@ 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/Shaders/Items/AlphaCtxShaderComponent.cpp + sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp + ) add_executable(bomberman sources/main.cpp