shader using

This commit is contained in:
Issac332 2026-05-27 19:38:45 +03:00
parent 5376a7e098
commit 50edfe0e12
10 changed files with 160 additions and 121 deletions

View File

@ -303,6 +303,7 @@ set(BINDING_SOURCE
binding-mri/chroma-binding.cpp binding-mri/chroma-binding.cpp
binding-mri/niko-binding.cpp binding-mri/niko-binding.cpp
binding-mri/time-binding.cpp binding-mri/time-binding.cpp
binding-mri/shader-binging.cpp
) )
source_group("Binding Source" FILES ${BINDING_SOURCE} ${BINDING_HEADERS}) source_group("Binding Source" FILES ${BINDING_SOURCE} ${BINDING_HEADERS})

View File

@ -84,6 +84,7 @@ void oneshotBindingInit();
void SunshineBindingInit(); void SunshineBindingInit();
void steamBindingInit(); void steamBindingInit();
void chromaBindingInit(); void chromaBindingInit();
void shaderBindingInit();
RB_METHOD(mriPrint); RB_METHOD(mriPrint);
RB_METHOD(mriP); RB_METHOD(mriP);
@ -119,6 +120,7 @@ static void mriBindingInit(){
SunshineBindingInit(); SunshineBindingInit();
steamBindingInit(); steamBindingInit();
chromaBindingInit(); chromaBindingInit();
shaderBindingInit();
_rb_define_module_function(rb_mKernel, "rgss_main", mriRgssMain); _rb_define_module_function(rb_mKernel, "rgss_main", mriRgssMain);
_rb_define_module_function(rb_mKernel, "rgss_stop", mriRgssStop); _rb_define_module_function(rb_mKernel, "rgss_stop", mriRgssStop);

View File

@ -0,0 +1,19 @@
#include <ruby.h>
#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
}

View File

@ -63,6 +63,7 @@ DEF_PROP_I(Sprite, BlendType)
DEF_PROP_I(Sprite, WaveAmp) DEF_PROP_I(Sprite, WaveAmp)
DEF_PROP_I(Sprite, WaveLength) DEF_PROP_I(Sprite, WaveLength)
DEF_PROP_I(Sprite, WaveSpeed) DEF_PROP_I(Sprite, WaveSpeed)
DEF_PROP_I(Sprite, Shader)
DEF_PROP_F(Sprite, ZoomX) DEF_PROP_F(Sprite, ZoomX)
DEF_PROP_F(Sprite, ZoomY) DEF_PROP_F(Sprite, ZoomY)
@ -71,7 +72,6 @@ DEF_PROP_F(Sprite, WavePhase)
DEF_PROP_B(Sprite, Mirror) DEF_PROP_B(Sprite, Mirror)
DEF_PROP_B(Sprite, Obscured) DEF_PROP_B(Sprite, Obscured)
DEF_PROP_B(Sprite, WorldMachine)
RB_METHOD(spriteWidth){ RB_METHOD(spriteWidth){
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
@ -123,7 +123,7 @@ void spriteBindingInit(){
INIT_PROP_BIND( Sprite, Tone, "tone" ); INIT_PROP_BIND( Sprite, Tone, "tone" );
INIT_PROP_BIND( Sprite, Modulate, "modulate" ); INIT_PROP_BIND( Sprite, Modulate, "modulate" );
INIT_PROP_BIND( Sprite, Obscured, "obscured" ); 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, "width", spriteWidth);
_rb_define_method(klass, "height", spriteHeight); _rb_define_method(klass, "height", spriteHeight);

View File

@ -37,7 +37,7 @@ class FastTravel
@niko_icon.zoom_x = @niko_icon.zoom_y = @scale_multiplier @niko_icon.zoom_x = @niko_icon.zoom_y = @scale_multiplier
@niko_icon.z = 10 @niko_icon.z = 10
@niko_icon.opacity = 0 @niko_icon.opacity = 0
@niko_icon.world_machine = true @niko_icon.shader = Shader::WorldMachine
@niko_pos_x = 0.0 @niko_pos_x = 0.0
@niko_pos_y = 0.0 @niko_pos_y = 0.0
@ -70,8 +70,8 @@ class FastTravel
@arrow_top.opacity = @arrow_bottom.opacity = @arrow_top.opacity = @arrow_bottom.opacity =
@arrow_left.opacity = @arrow_right.opacity = 0 @arrow_left.opacity = @arrow_right.opacity = 0
@arrow_top.world_machine = @arrow_bottom.world_machine = @arrow_top.shader = @arrow_bottom.shader =
@arrow_left.world_machine = @arrow_right.world_machine = true @arrow_left.shader = @arrow_right.shader = Shader::WorldMachine
@arrow_top.z = @arrow_bottom.z = @arrow_top.z = @arrow_bottom.z =
@arrow_left.z = @arrow_right.z = 11 @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.x = Graphics.width / 2 + zone.locations[item].x * @scale_multiplier
spr.y = Graphics.height / 2 + zone.locations[item].y * @scale_multiplier spr.y = Graphics.height / 2 + zone.locations[item].y * @scale_multiplier
spr.opacity = 0 spr.opacity = 0
spr.world_machine = true spr.shader = Shader::WorldMachine
@data_locations[item] = spr @data_locations[item] = spr
set_color(item) set_color(item)
else else

View File

@ -27,8 +27,6 @@ class Sprite_Character
@text_sprite.bitmap.font.size = 12 @text_sprite.bitmap.font.size = 12
@character = character @character = character
#for debug
@last_char_name
update update
end end
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
@ -37,7 +35,7 @@ class Sprite_Character
def update def update
# Choose the appropriate light sprite # Choose the appropriate light sprite
@light_sprite.viewport = ($game_screen.tone.blank?) ? @viewport : @light_viewport @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] if @last_char_name != character.character_name && Settings[:debug]
@text_sprite.bitmap.clear @text_sprite.bitmap.clear
@text_sprite.bitmap.draw_text(0, 0, 256, 12, "#{@character.character_name}") @text_sprite.bitmap.draw_text(0, 0, 256, 12, "#{@character.character_name}")

View File

@ -231,6 +231,7 @@ void ShaderBase::GLProjMat::apply(const Vec2i &value) {
void ShaderBase::init(){ void ShaderBase::init(){
GET_U(texSizeInv); GET_U(texSizeInv);
GET_U(translation); GET_U(translation);
GET_U(uTime);
projMat.u_mat = gl.GetUniformLocation(program, "projMat"); 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); gl.Uniform2f(u_translation, value.x, value.y);
} }
void ShaderBase::setTime(float value){
gl.Uniform1f(u_uTime, value);
}
FlatColorShader::FlatColorShader(){ FlatColorShader::FlatColorShader(){
INIT_SHADER(minimal, flatColor, FlatColorShader); INIT_SHADER(minimal, flatColor, FlatColorShader);
@ -422,19 +427,8 @@ void SpriteShader::setBushOpacity(float value){
WMShader::WMShader(){ WMShader::WMShader(){
INIT_SHADER(worldMachine, worldMachine, WMShader); INIT_SHADER(worldMachine, worldMachine, WMShader);
SpriteShader::init(); 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(){ PlaneShader::PlaneShader(){
INIT_SHADER(simple, plane, PlaneShader); INIT_SHADER(simple, plane, PlaneShader);

View File

@ -76,11 +76,15 @@ public:
void setTexSize(const Vec2i &value); void setTexSize(const Vec2i &value);
void setTranslation(const Vec2i &value); void setTranslation(const Vec2i &value);
void setTime(float value);
protected: protected:
void init(); void init();
GLint u_texSizeInv, u_translation; GLint u_texSizeInv, u_translation;
private:
GLint u_uTime;
}; };
class FlatColorShader : public ShaderBase{ class FlatColorShader : public ShaderBase{
@ -179,11 +183,6 @@ private:
class WMShader : public SpriteShader{ class WMShader : public SpriteShader{
public: public:
WMShader(); WMShader();
void setTime(float value);
//void setResolution(float x, float y);
private:
GLint u_uTime/*, u_resolution*/;
}; };
class PlaneShader : public ShaderBase{ class PlaneShader : public ShaderBase{
@ -302,26 +301,39 @@ private:
}; };
/* Global object containing all available shaders */ /* 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{ struct ShaderSet{
FlatColorShader flatColor; #define DECLARE_SHADER(name, type, rb) type name;
SimpleShader simple; SHADER_LIST(DECLARE_SHADER)
SimpleColorShader simpleColor; #undef DECLARE_SHADER
SimpleAlphaShader simpleAlpha; };
SimpleSpriteShader simpleSprite; enum ShaderType{
AlphaSpriteShader alphaSprite; #define DECLARE_ENUM(name, type, rb) SHADER_##name,
SpriteShader sprite; SHADER_LIST(DECLARE_ENUM)
WMShader worldMachine; #undef DECLARE_ENUM
PlaneShader plane; SHADER_COUNT
GrayShader gray;
TilemapShader tilemap;
FlashMapShader flashMap;
TransShader trans;
SimpleTransShader simpleTrans;
HueShader hue;
BltShader blt;
SimpleMatrixShader simpleMatrix;
BlurShader blur;
ObscuredShader obscured;
}; };
#endif // SHADER_H #endif // SHADER_H

View File

@ -44,7 +44,6 @@
#include <boost/chrono.hpp> #include <boost/chrono.hpp>
struct SpritePrivate{ struct SpritePrivate{
boost::chrono::high_resolution_clock::time_point startTime = boost::chrono::high_resolution_clock::now();
Bitmap *bitmap; Bitmap *bitmap;
@ -69,12 +68,13 @@ struct SpritePrivate{
bool isVisible; bool isVisible;
bool obscured; bool obscured;
bool worldMachine;
Color *color; Color *color;
Tone *tone; Tone *tone;
Color *modulate; Color *modulate;
int shader;
struct{ struct{
int amp; int amp;
int length; int length;
@ -103,10 +103,10 @@ struct SpritePrivate{
blendType(BlendNormal), blendType(BlendNormal),
isVisible(false), isVisible(false),
obscured(false), obscured(false),
worldMachine(false),
color(&tmp.color), color(&tmp.color),
tone(&tmp.tone), tone(&tmp.tone),
modulate(&tmp.modulate) modulate(&tmp.modulate),
shader(ShaderType::SHADER_sprite)
{ {
sceneRect.x = sceneRect.y = 0; sceneRect.x = sceneRect.y = 0;
@ -322,7 +322,7 @@ DEF_ATTR_SIMPLE(Sprite, Color, Color&, *p->color)
DEF_ATTR_SIMPLE(Sprite, Tone, Tone&, *p->tone) DEF_ATTR_SIMPLE(Sprite, Tone, Tone&, *p->tone)
DEF_ATTR_SIMPLE(Sprite, Modulate, Color&, *p->modulate) DEF_ATTR_SIMPLE(Sprite, Modulate, Color&, *p->modulate)
DEF_ATTR_SIMPLE(Sprite, Obscured, bool, p->obscured) DEF_ATTR_SIMPLE(Sprite, Obscured, bool, p->obscured)
DEF_ATTR_SIMPLE(Sprite, WorldMachine, bool, p->worldMachine) DEF_ATTR_SIMPLE(Sprite, Shader, int, p->shader)
void Sprite::setBitmap(Bitmap *bitmap){ void Sprite::setBitmap(Bitmap *bitmap){
guardDisposed(); guardDisposed();
@ -512,7 +512,11 @@ void Sprite::draw(){
shader.setObscured(shState->graphics().obscuredTex()); shader.setObscured(shState->graphics().obscuredTex());
base = &shader; base = &shader;
} }
else if (p->worldMachine){ else{
switch (p->shader)
{
case ShaderType::SHADER_worldMachine:
{
WMShader &shader = shState->shaders().worldMachine; WMShader &shader = shState->shaders().worldMachine;
shader.bind(); shader.bind();
@ -531,16 +535,14 @@ void Sprite::draw(){
shader.setColor(*blend); shader.setColor(*blend);
shader.setModulate(p->modulate->norm); 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<float> elapsed = currentTime - p->startTime;
shader.setTime(elapsed.count());
base = &shader; base = &shader;
break;
} }
else if (renderEffect){ default:
{
if (renderEffect){
SpriteShader &shader = shState->shaders().sprite; SpriteShader &shader = shState->shaders().sprite;
shader.bind(); shader.bind();
@ -580,6 +582,14 @@ void Sprite::draw(){
base = &shader; base = &shader;
} }
break;
}
}
}
boost::chrono::high_resolution_clock::time_point currentTime = boost::chrono::high_resolution_clock::now();
boost::chrono::duration<float> elapsed = currentTime - startTime;
base->setTime(elapsed.count());
glState.blendMode.pushSet(p->blendType); glState.blendMode.pushSet(p->blendType);
p->bitmap->bindTex(*base); p->bitmap->bindTex(*base);

View File

@ -27,6 +27,7 @@
#include "disposable.h" #include "disposable.h"
#include "viewport.h" #include "viewport.h"
#include "util.h" #include "util.h"
#include "shader.h"
#include <boost/chrono.hpp> #include <boost/chrono.hpp>
@ -39,6 +40,8 @@ struct SpritePrivate;
class Sprite : public ViewportElement, public Flashable, public Disposable{ class Sprite : public ViewportElement, public Flashable, public Disposable{
public: public:
boost::chrono::high_resolution_clock::time_point startTime = boost::chrono::high_resolution_clock::now();
Sprite(Viewport *viewport = 0); Sprite(Viewport *viewport = 0);
~Sprite(); ~Sprite();
@ -69,7 +72,7 @@ public:
DECL_ATTR( WaveSpeed, int ) DECL_ATTR( WaveSpeed, int )
DECL_ATTR( WavePhase, float ) DECL_ATTR( WavePhase, float )
DECL_ATTR( Obscured, bool ) DECL_ATTR( Obscured, bool )
DECL_ATTR( WorldMachine, bool ) DECL_ATTR( Shader, int )
DECL_ATTR( startTime, boost::chrono::high_resolution_clock::time_point) DECL_ATTR( startTime, boost::chrono::high_resolution_clock::time_point)
void initDynAttribs(); void initDynAttribs();