water optimizations

This commit is contained in:
Issac332 2026-06-22 16:58:57 +03:00
parent 1df8c9e1d6
commit 4f913f36b5
12 changed files with 81 additions and 117 deletions

View file

@ -144,6 +144,7 @@ set(MAIN_SOURCE
src/i18n.cpp
src/meow.cpp
src/modloader.cpp
src/sunshine.cpp
)
if(WIN32)

View file

@ -1,6 +1,5 @@
uniform sampler2D texture;
uniform sampler2D noiseTexture;
uniform float uTime;
uniform float aniIndex;
@ -18,62 +17,11 @@ const float SpriteAreaH = 128.0;
const float atAniOffset = 32.0*3.0;
const vec2 AtlasSize = vec2(96.0, 128.0);
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
float pnoise(vec2 P)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
float pnoise(vec2 P){
vec2 uv = P;
uv *= 0.025;
uv -= floor(uv);
return texture2D(noiseTexture, uv);
}
void main(){
@ -88,16 +36,16 @@ void main(){
vec2 uv = worldCoord / 16.0 * vec2(1.0, 4.0);
uv = floor(uv * vec2(8.0, 2.0)) / vec2(8.0, 2.0);
float noise = (pnoise(uv) + 1.0) / 2.0;
float noise = pnoise(uv);
float offsetTime = uTime + 100.0;
vec2 uv1 = uv - vec2(offsetTime, offsetTime * 0.5 + 0.5) * 0.56;
float noise1 = (pnoise(uv1 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise) + 1.0) / 2.0;
float noise1 = pnoise(uv1 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise);
float p = pow(noise1 + 0.2, 10.0);
vec2 uv2 = uv - vec2(offsetTime - 1.5, -offsetTime * 0.5) * 0.24;
float noise2 = (pnoise(uv2 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise) + 1.0) / 2.0;
float noise2 = pnoise(uv2 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise);
float p2 = pow(noise2 + 0.2, 10.0);
vec4 frag = mix(vec4(waterColor1.rgb, 1.0), vec4(waterColor2.rgb, 1.0), noise1) + vec4(vec3(mix(p, 1.0, textureColor.r)) * waterColor3.rgb, 1.0);

View file

@ -1,4 +1,5 @@
uniform sampler2D texture;
uniform sampler2D noiseTexture;
uniform lowp vec4 tone;
@ -18,71 +19,24 @@ uniform vec2 texSizeInv;
const vec3 lumaF = vec3(.299, .587, .114);
const float WATER_DISTORTION = 0.75;
vec4 mod289(vec4 x){
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x){
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r){
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
float pnoise(vec2 P){
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
vec2 uv = P;
uv *= 0.025;
uv -= floor(uv);
return texture2D(noiseTexture, uv);
}
void main(){
vec2 uv = v_texCoord * texSizeInv * 8192.0;
uv = floor(uv * 16.0) / 16.0;
float noise = (pnoise(uv) + 1.0) / 2.0;
float noise = pnoise(uv);
vec2 uv1 = uv - vec2(uTime, uTime * 0.5 + 0.5) * 0.56;
float noise1 = (pnoise(uv1 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise) + 1.0) / 2.0;
float noise1 = pnoise(uv1 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise);
float p = pow(noise1 + 0.02, 20.0);
vec2 uv2 = uv - vec2(uTime - 1.5, -uTime * 0.5) * 0.24;
float noise2 = (pnoise(uv2 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise) + 1.0) / 2.0;
float noise2 = pnoise(uv2 + vec2(WATER_DISTORTION, -WATER_DISTORTION) * noise);
float p2 = pow(noise2 + 0.02, 20.0);
vec4 frag = mix(vec4(color.r, color.g, color.b, 1.0) * color.a, vec4(color.r, color.g, color.b, 1.0) * tone.a, noise1) + vec4(p, p, p, 1.0) * tone;

View file

@ -308,6 +308,9 @@ void Plane::draw(){
WaterShader &shader = shState->shaders().water;
defaultSpriteShaderInit(shader);
if (shState->sunshine().noiseBitmap)
shader.setNoiseTexture(shState->sunshine().noiseBitmap->getGLTypes().tex);
base = &shader;

View file

@ -58,6 +58,7 @@
#include "obscured.frag.xxd"
#include "meow.h"
#include "sunshine.h"
#define INIT_SHADER(vert, frag, name){ \
Shader::init(shader_##vert##_vert, shader_##vert##_vert_len, shader_##frag##_frag, shader_##frag##_frag_len, \
@ -428,6 +429,12 @@ WMShader::WMShader(){
WaterShader::WaterShader(){
INIT_SHADER(simple, water, WaterShader);
SpriteShaderBase::SpriteShaderInit();
GET_U(noiseTexture);
}
void WaterShader::setNoiseTexture(TEX::ID texture){
setTexUniform(u_noiseTexture, 1, texture);
}
CRTShader::CRTShader(){
@ -493,6 +500,7 @@ TilemapWaterShader::TilemapWaterShader(){
GET_U(aniIndex);
GET_U(offset);
GET_U(noiseTexture);
}
void TilemapWaterShader::setAniIndex(int value){
@ -503,6 +511,10 @@ void TilemapWaterShader::setOffset(const Vec2i &value){
gl.Uniform2f(u_offset, value.x, value.y);
}
void TilemapWaterShader::setNoiseTexture(TEX::ID texture){
setTexUniform(u_noiseTexture, 1, texture);
}
FlashMapShader::FlashMapShader(){
INIT_SHADER(simpleColor, flashMap, FlashMapShader);

View file

@ -189,6 +189,11 @@ public:
class WaterShader : public SpriteShaderBase{
public:
WaterShader();
void setNoiseTexture(TEX::ID texture);
private:
GLint u_noiseTexture;
};
class CRTShader : public SpriteShaderBase{
@ -202,9 +207,10 @@ public:
void setAniIndex(int value);
void setOffset(const Vec2i &value);
void setNoiseTexture(TEX::ID texture);
private:
GLint u_aniIndex, u_offset;
GLint u_aniIndex, u_offset, u_noiseTexture;
};
class PlaneShader : public ShaderBase{

View file

@ -27,6 +27,7 @@
#include "input.h"
#include "audio.h"
#include "oneshot.h"
#include "sunshine.h"
#ifdef STEAM
#include "steam.h"
#endif
@ -66,6 +67,7 @@ struct SharedStatePrivate{
Audio audio;
Oneshot oneshot;
Sunshine sunshine;
#ifdef STEAM
Steam steam;
#endif
@ -102,6 +104,7 @@ struct SharedStatePrivate{
input(*threadData),
audio(*threadData),
oneshot(*threadData),
sunshine(),
_glState(threadData->config),
fontState(threadData->config),
stampCounter(0)
@ -164,6 +167,8 @@ void SharedState::initInstance(RGSSThreadData *threadData){
}
SharedState::instance->p->defaultFont = defaultFont;
SharedState::instance->p->sunshine.loadNoise();
}
void SharedState::finiInstance(){
@ -195,6 +200,7 @@ GSATT(Graphics&, graphics)
GSATT(Input&, input)
GSATT(Audio&, audio)
GSATT(Oneshot&, oneshot)
GSATT(Sunshine&, sunshine)
#ifdef STEAM
GSATT(Steam&, steam)
#endif

View file

@ -42,6 +42,7 @@ class Graphics;
class Input;
class Audio;
class Oneshot;
class Sunshine;
#ifdef STEAM
class Steam;
#endif
@ -74,6 +75,7 @@ struct SharedState{
Audio &audio() const;
Oneshot &oneshot() const;
Sunshine &sunshine() const;
#ifdef STEAM
Steam &steam() const;
#endif

View file

@ -529,7 +529,10 @@ void Sprite::draw(){
defaultSpriteShaderInit(shader);
base = &shader;
if (shState->sunshine().noiseBitmap)
shader.setNoiseTexture(shState->sunshine().noiseBitmap->getGLTypes().tex);
break;
}
case ShaderType::SHADER_crt:

11
src/sunshine.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "sunshine.h"
Sunshine::Sunshine() {}
void Sunshine::loadNoise()
{
if(!noiseLoaded){
Sunshine::noiseBitmap = new Bitmap(Sunshine::noisePath);
noiseLoaded = true;
}
}

View file

@ -1,4 +1,19 @@
#include <boost/chrono.hpp>
#include "bitmap.h"
//помойка ебаная
inline boost::chrono::high_resolution_clock::time_point startTime;
class Sunshine{
public:
Sunshine();
const char* noisePath = "Graphics/Misc/noise";
Bitmap* noiseBitmap;
void loadNoise();
private:
bool noiseLoaded = false;
};

View file

@ -732,6 +732,9 @@ struct TilemapPrivate {
boost::chrono::duration<float> elapsed = currentTime - startTime;
tilemapShader.setTime(elapsed.count());
if (shState->sunshine().noiseBitmap)
tilemapShader.setNoiseTexture(shState->sunshine().noiseBitmap->getGLTypes().tex);
shaderVar = &tilemapShader;
}/*
else if(tiles.animated){