From 50edfe0e12dd92545a81017c325de5f81d250748 Mon Sep 17 00:00:00 2001 From: Issac332 <119879937+Issac8658@users.noreply.github.com> Date: Wed, 27 May 2026 19:38:45 +0300 Subject: [PATCH] shader using --- CMakeLists.txt | 1 + binding-mri/binding-mri.cpp | 2 + binding-mri/shader-binging.cpp | 19 ++++ binding-mri/sprite-binding.cpp | 4 +- scripts/FastTravel.rb | 8 +- scripts/Sprite_Character.rb | 4 +- src/shader.cpp | 16 +--- src/shader.h | 60 +++++++----- src/sprite.cpp | 162 +++++++++++++++++---------------- src/sprite.h | 5 +- 10 files changed, 160 insertions(+), 121 deletions(-) create mode 100644 binding-mri/shader-binging.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a01397f..45dbe43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -303,6 +303,7 @@ set(BINDING_SOURCE binding-mri/chroma-binding.cpp binding-mri/niko-binding.cpp binding-mri/time-binding.cpp + binding-mri/shader-binging.cpp ) source_group("Binding Source" FILES ${BINDING_SOURCE} ${BINDING_HEADERS}) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index b105482..f2c935b 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -84,6 +84,7 @@ void oneshotBindingInit(); void SunshineBindingInit(); void steamBindingInit(); void chromaBindingInit(); +void shaderBindingInit(); RB_METHOD(mriPrint); RB_METHOD(mriP); @@ -119,6 +120,7 @@ static void mriBindingInit(){ SunshineBindingInit(); steamBindingInit(); chromaBindingInit(); + shaderBindingInit(); _rb_define_module_function(rb_mKernel, "rgss_main", mriRgssMain); _rb_define_module_function(rb_mKernel, "rgss_stop", mriRgssStop); diff --git a/binding-mri/shader-binging.cpp b/binding-mri/shader-binging.cpp new file mode 100644 index 0000000..0504bb2 --- /dev/null +++ b/binding-mri/shader-binging.cpp @@ -0,0 +1,19 @@ +#include + +#include "shader.h" + +#include "disposable-binding.h" +#include "binding-util.h" +#include "binding-types.h" + +VALUE rb_mShader; + +void shaderBindingInit(){ + printf("[shaderBindingInit] Initializing Shaders binding\n"); + rb_mShader = rb_define_module("Shader"); + + #define DEFINE_RUBY_CONST(name, type, rb) \ + rb_define_const(rb_mShader, #rb, INT2NUM(ShaderType::SHADER_##name)); + SHADER_LIST(DEFINE_RUBY_CONST) + #undef DEFINE_RUBY_CONST +} \ No newline at end of file diff --git a/binding-mri/sprite-binding.cpp b/binding-mri/sprite-binding.cpp index 58170c7..9e2ee59 100644 --- a/binding-mri/sprite-binding.cpp +++ b/binding-mri/sprite-binding.cpp @@ -63,6 +63,7 @@ DEF_PROP_I(Sprite, BlendType) DEF_PROP_I(Sprite, WaveAmp) DEF_PROP_I(Sprite, WaveLength) DEF_PROP_I(Sprite, WaveSpeed) +DEF_PROP_I(Sprite, Shader) DEF_PROP_F(Sprite, ZoomX) DEF_PROP_F(Sprite, ZoomY) @@ -71,7 +72,6 @@ DEF_PROP_F(Sprite, WavePhase) DEF_PROP_B(Sprite, Mirror) DEF_PROP_B(Sprite, Obscured) -DEF_PROP_B(Sprite, WorldMachine) RB_METHOD(spriteWidth){ RB_UNUSED_PARAM; @@ -123,7 +123,7 @@ void spriteBindingInit(){ INIT_PROP_BIND( Sprite, Tone, "tone" ); INIT_PROP_BIND( Sprite, Modulate, "modulate" ); INIT_PROP_BIND( Sprite, Obscured, "obscured" ); - INIT_PROP_BIND( Sprite, WorldMachine, "world_machine" ); + INIT_PROP_BIND( Sprite, Shader, "shader" ); _rb_define_method(klass, "width", spriteWidth); _rb_define_method(klass, "height", spriteHeight); diff --git a/scripts/FastTravel.rb b/scripts/FastTravel.rb index b6584d5..3f6f315 100644 --- a/scripts/FastTravel.rb +++ b/scripts/FastTravel.rb @@ -37,7 +37,7 @@ class FastTravel @niko_icon.zoom_x = @niko_icon.zoom_y = @scale_multiplier @niko_icon.z = 10 @niko_icon.opacity = 0 - @niko_icon.world_machine = true + @niko_icon.shader = Shader::WorldMachine @niko_pos_x = 0.0 @niko_pos_y = 0.0 @@ -70,8 +70,8 @@ class FastTravel @arrow_top.opacity = @arrow_bottom.opacity = @arrow_left.opacity = @arrow_right.opacity = 0 - @arrow_top.world_machine = @arrow_bottom.world_machine = - @arrow_left.world_machine = @arrow_right.world_machine = true + @arrow_top.shader = @arrow_bottom.shader = + @arrow_left.shader = @arrow_right.shader = Shader::WorldMachine @arrow_top.z = @arrow_bottom.z = @arrow_left.z = @arrow_right.z = 11 @@ -195,7 +195,7 @@ class FastTravel spr.x = Graphics.width / 2 + zone.locations[item].x * @scale_multiplier spr.y = Graphics.height / 2 + zone.locations[item].y * @scale_multiplier spr.opacity = 0 - spr.world_machine = true + spr.shader = Shader::WorldMachine @data_locations[item] = spr set_color(item) else diff --git a/scripts/Sprite_Character.rb b/scripts/Sprite_Character.rb index b9258d9..4bac6cf 100644 --- a/scripts/Sprite_Character.rb +++ b/scripts/Sprite_Character.rb @@ -27,8 +27,6 @@ class Sprite_Character @text_sprite.bitmap.font.size = 12 @character = character - #for debug - @last_char_name update end #-------------------------------------------------------------------------- @@ -37,7 +35,7 @@ class Sprite_Character def update # Choose the appropriate light sprite @light_sprite.viewport = ($game_screen.tone.blank?) ? @viewport : @light_viewport - @light_sprite.world_machine = @sprite.world_machine = @character.character_name.start_with?("en") || (@character.character_name.start_with?("niko") && $game_switches[160]) + @light_sprite.shader = @sprite.shader = @character.character_name.start_with?("en") || (@character.character_name.start_with?("niko") && $game_switches[160]) ? Shader::WorldMachine : Shader::Sprite if @last_char_name != character.character_name && Settings[:debug] @text_sprite.bitmap.clear @text_sprite.bitmap.draw_text(0, 0, 256, 12, "#{@character.character_name}") diff --git a/src/shader.cpp b/src/shader.cpp index 03afdf7..951d295 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -231,6 +231,7 @@ void ShaderBase::GLProjMat::apply(const Vec2i &value) { void ShaderBase::init(){ GET_U(texSizeInv); GET_U(translation); + GET_U(uTime); projMat.u_mat = gl.GetUniformLocation(program, "projMat"); } @@ -248,6 +249,10 @@ void ShaderBase::setTranslation(const Vec2i &value){ gl.Uniform2f(u_translation, value.x, value.y); } +void ShaderBase::setTime(float value){ + gl.Uniform1f(u_uTime, value); +} + FlatColorShader::FlatColorShader(){ INIT_SHADER(minimal, flatColor, FlatColorShader); @@ -422,19 +427,8 @@ void SpriteShader::setBushOpacity(float value){ WMShader::WMShader(){ INIT_SHADER(worldMachine, worldMachine, WMShader); SpriteShader::init(); - - GET_U(uTime); - //GET_U(resolution); } -void WMShader::setTime(float value){ - gl.Uniform1f(u_uTime, value); -} -/* -void WMShader::setResolution(float x, float y){ - gl.Uniform2f(u_resolution, x, y); -}*/ - PlaneShader::PlaneShader(){ INIT_SHADER(simple, plane, PlaneShader); diff --git a/src/shader.h b/src/shader.h index 7cd1c4d..5e50885 100644 --- a/src/shader.h +++ b/src/shader.h @@ -76,11 +76,15 @@ public: void setTexSize(const Vec2i &value); void setTranslation(const Vec2i &value); + void setTime(float value); protected: void init(); GLint u_texSizeInv, u_translation; + +private: + GLint u_uTime; }; class FlatColorShader : public ShaderBase{ @@ -179,11 +183,6 @@ private: class WMShader : public SpriteShader{ public: WMShader(); - - void setTime(float value); - //void setResolution(float x, float y); -private: - GLint u_uTime/*, u_resolution*/; }; class PlaneShader : public ShaderBase{ @@ -302,26 +301,39 @@ private: }; /* Global object containing all available shaders */ + +// Name C++ Class In ruby const name +#define SHADER_LIST(X) \ + X(flatColor, FlatColorShader, Flat) \ + X(simple, SimpleShader, Simple) \ + X(simpleColor, SimpleColorShader, SimpleColor) \ + X(simpleAlpha, SimpleAlphaShader, SimpleAlpha) \ + X(simpleSprite, SimpleSpriteShader, SimpleSprite) \ + X(alphaSprite, AlphaSpriteShader, AlphaSprite) \ + X(sprite, SpriteShader, Sprite) \ + X(worldMachine, WMShader, WorldMachine) \ + X(plane, PlaneShader, Plane) \ + X(gray, GrayShader, Gray) \ + X(tilemap, TilemapShader, TileMap) \ + X(flashMap, FlashMapShader, Flash) \ + X(trans, TransShader, Trans) \ + X(simpleTrans, SimpleTransShader, SimpleTrans) \ + X(hue, HueShader, Hue) \ + X(blt, BltShader, Blt) \ + X(simpleMatrix, SimpleMatrixShader, SimpleMatrix) \ + X(blur, BlurShader, Blur) \ + X(obscured, ObscuredShader, Obscured) + struct ShaderSet{ - FlatColorShader flatColor; - SimpleShader simple; - SimpleColorShader simpleColor; - SimpleAlphaShader simpleAlpha; - SimpleSpriteShader simpleSprite; - AlphaSpriteShader alphaSprite; - SpriteShader sprite; - WMShader worldMachine; - PlaneShader plane; - GrayShader gray; - TilemapShader tilemap; - FlashMapShader flashMap; - TransShader trans; - SimpleTransShader simpleTrans; - HueShader hue; - BltShader blt; - SimpleMatrixShader simpleMatrix; - BlurShader blur; - ObscuredShader obscured; + #define DECLARE_SHADER(name, type, rb) type name; + SHADER_LIST(DECLARE_SHADER) + #undef DECLARE_SHADER +}; +enum ShaderType{ +#define DECLARE_ENUM(name, type, rb) SHADER_##name, + SHADER_LIST(DECLARE_ENUM) +#undef DECLARE_ENUM + SHADER_COUNT }; #endif // SHADER_H diff --git a/src/sprite.cpp b/src/sprite.cpp index 9935941..03e8dd3 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -44,7 +44,6 @@ #include struct SpritePrivate{ - boost::chrono::high_resolution_clock::time_point startTime = boost::chrono::high_resolution_clock::now(); Bitmap *bitmap; @@ -69,11 +68,12 @@ struct SpritePrivate{ bool isVisible; bool obscured; - bool worldMachine; Color *color; Tone *tone; Color *modulate; + + int shader; struct{ int amp; @@ -103,10 +103,10 @@ struct SpritePrivate{ blendType(BlendNormal), isVisible(false), obscured(false), - worldMachine(false), color(&tmp.color), tone(&tmp.tone), - modulate(&tmp.modulate) + modulate(&tmp.modulate), + shader(ShaderType::SHADER_sprite) { sceneRect.x = sceneRect.y = 0; @@ -315,14 +315,14 @@ DEF_ATTR_RD_SIMPLE(Sprite, WaveLength, int, p->wave.length) DEF_ATTR_RD_SIMPLE(Sprite, WaveSpeed, int, p->wave.speed) DEF_ATTR_RD_SIMPLE(Sprite, WavePhase, float, p->wave.phase) -DEF_ATTR_SIMPLE(Sprite, BushOpacity, int, p->bushOpacity) -DEF_ATTR_SIMPLE(Sprite, Opacity, int, p->opacity) -DEF_ATTR_SIMPLE(Sprite, SrcRect, Rect&, *p->srcRect) -DEF_ATTR_SIMPLE(Sprite, Color, Color&, *p->color) -DEF_ATTR_SIMPLE(Sprite, Tone, Tone&, *p->tone) -DEF_ATTR_SIMPLE(Sprite, Modulate, Color&, *p->modulate) -DEF_ATTR_SIMPLE(Sprite, Obscured, bool, p->obscured) -DEF_ATTR_SIMPLE(Sprite, WorldMachine, bool, p->worldMachine) +DEF_ATTR_SIMPLE(Sprite, BushOpacity, int, p->bushOpacity) +DEF_ATTR_SIMPLE(Sprite, Opacity, int, p->opacity) +DEF_ATTR_SIMPLE(Sprite, SrcRect, Rect&, *p->srcRect) +DEF_ATTR_SIMPLE(Sprite, Color, Color&, *p->color) +DEF_ATTR_SIMPLE(Sprite, Tone, Tone&, *p->tone) +DEF_ATTR_SIMPLE(Sprite, Modulate, Color&, *p->modulate) +DEF_ATTR_SIMPLE(Sprite, Obscured, bool, p->obscured) +DEF_ATTR_SIMPLE(Sprite, Shader, int, p->shader) void Sprite::setBitmap(Bitmap *bitmap){ guardDisposed(); @@ -512,73 +512,83 @@ void Sprite::draw(){ shader.setObscured(shState->graphics().obscuredTex()); base = &shader; } - else if (p->worldMachine){ - WMShader &shader = shState->shaders().worldMachine; - - shader.bind(); - shader.applyViewportProj(); - shader.setSpriteMat(p->trans.getMatrix()); - - shader.setTone(p->tone->norm); - shader.setOpacity(p->opacity.norm); - shader.setBushDepth(p->efBushDepth); - shader.setBushOpacity(p->bushOpacity.norm); - - /* When both flashing and effective color are set, - * the one with higher alpha will be blended */ - const Vec4 *blend = (flashing && flashColor.w > p->color->norm.w) ? - &flashColor : &p->color->norm; - - shader.setColor(*blend); - shader.setModulate(p->modulate->norm); - //shader.setResolution(p->bitmap->width(), p->bitmap->height()); - - boost::chrono::high_resolution_clock::time_point currentTime = boost::chrono::high_resolution_clock::now(); - - boost::chrono::duration elapsed = currentTime - p->startTime; - shader.setTime(elapsed.count()); - - base = &shader; - } - else if (renderEffect){ - SpriteShader &shader = shState->shaders().sprite; - - shader.bind(); - shader.applyViewportProj(); - shader.setSpriteMat(p->trans.getMatrix()); - - shader.setTone(p->tone->norm); - shader.setOpacity(p->opacity.norm); - shader.setBushDepth(p->efBushDepth); - shader.setBushOpacity(p->bushOpacity.norm); - - /* When both flashing and effective color are set, - * the one with higher alpha will be blended */ - const Vec4 *blend = (flashing && flashColor.w > p->color->norm.w) ? - &flashColor : &p->color->norm; - - shader.setColor(*blend); - shader.setModulate(p->modulate->norm); - - base = &shader; - } - else if (p->opacity != 255){ - AlphaSpriteShader &shader = shState->shaders().alphaSprite; - shader.bind(); - - shader.setSpriteMat(p->trans.getMatrix()); - shader.setAlpha(p->opacity.norm); - shader.applyViewportProj(); - base = &shader; - } else{ - SimpleSpriteShader &shader = shState->shaders().simpleSprite; - shader.bind(); + switch (p->shader) + { + case ShaderType::SHADER_worldMachine: + { + WMShader &shader = shState->shaders().worldMachine; - shader.setSpriteMat(p->trans.getMatrix()); - shader.applyViewportProj(); - base = &shader; + shader.bind(); + shader.applyViewportProj(); + shader.setSpriteMat(p->trans.getMatrix()); + + shader.setTone(p->tone->norm); + shader.setOpacity(p->opacity.norm); + shader.setBushDepth(p->efBushDepth); + shader.setBushOpacity(p->bushOpacity.norm); + + /* When both flashing and effective color are set, + * the one with higher alpha will be blended */ + const Vec4 *blend = (flashing && flashColor.w > p->color->norm.w) ? + &flashColor : &p->color->norm; + + shader.setColor(*blend); + shader.setModulate(p->modulate->norm); + + base = &shader; + + break; + } + default: + { + if (renderEffect){ + SpriteShader &shader = shState->shaders().sprite; + + shader.bind(); + shader.applyViewportProj(); + shader.setSpriteMat(p->trans.getMatrix()); + + shader.setTone(p->tone->norm); + shader.setOpacity(p->opacity.norm); + shader.setBushDepth(p->efBushDepth); + shader.setBushOpacity(p->bushOpacity.norm); + + /* When both flashing and effective color are set, + * the one with higher alpha will be blended */ + const Vec4 *blend = (flashing && flashColor.w > p->color->norm.w) ? + &flashColor : &p->color->norm; + + shader.setColor(*blend); + shader.setModulate(p->modulate->norm); + + base = &shader; + } + else if (p->opacity != 255){ + AlphaSpriteShader &shader = shState->shaders().alphaSprite; + shader.bind(); + + shader.setSpriteMat(p->trans.getMatrix()); + shader.setAlpha(p->opacity.norm); + shader.applyViewportProj(); + base = &shader; + } + else{ + SimpleSpriteShader &shader = shState->shaders().simpleSprite; + shader.bind(); + + shader.setSpriteMat(p->trans.getMatrix()); + shader.applyViewportProj(); + base = &shader; + } + + break; + } + } } + boost::chrono::high_resolution_clock::time_point currentTime = boost::chrono::high_resolution_clock::now(); + boost::chrono::duration elapsed = currentTime - startTime; + base->setTime(elapsed.count()); glState.blendMode.pushSet(p->blendType); diff --git a/src/sprite.h b/src/sprite.h index b9db4a5..78b4c8d 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -27,6 +27,7 @@ #include "disposable.h" #include "viewport.h" #include "util.h" +#include "shader.h" #include @@ -39,6 +40,8 @@ struct SpritePrivate; class Sprite : public ViewportElement, public Flashable, public Disposable{ public: + boost::chrono::high_resolution_clock::time_point startTime = boost::chrono::high_resolution_clock::now(); + Sprite(Viewport *viewport = 0); ~Sprite(); @@ -69,7 +72,7 @@ public: DECL_ATTR( WaveSpeed, int ) DECL_ATTR( WavePhase, float ) DECL_ATTR( Obscured, bool ) - DECL_ATTR( WorldMachine, bool ) + DECL_ATTR( Shader, int ) DECL_ATTR( startTime, boost::chrono::high_resolution_clock::time_point) void initDynAttribs();