Skip to content

Commit

Permalink
Extra stage background prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Akaricchi committed Mar 4, 2020
1 parent e3d5518 commit 4aa6ba5
Show file tree
Hide file tree
Showing 22 changed files with 725 additions and 10 deletions.
Binary file added resources/00-taisei.pkgdir/gfx/stageex/bg.webp
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions resources/00-taisei.pkgdir/gfx/stageex/code.num_slices
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
10 changes: 10 additions & 0 deletions resources/00-taisei.pkgdir/gfx/stageex/code.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# Disable mipmaps to reduce artifacts at the seams.
# Note that some artifacts will still be present, because the linear filter is unaware of how the slices are supposed to wrap.
mipmaps = 1
filter_min = linear
filter_mag = linear

# Nearest-neighbour filter avoids artifacts entirely, but looks worse.
#filter_min = nearest
#filter_mag = nearest
Binary file added resources/00-taisei.pkgdir/gfx/stageex/code.webp
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_bg.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 330 core

#include "lib/render_context.glslh"
#include "interface/standard.glslh"
#include "extra_bg.glslh"

UNIFORM(1) sampler2D background_tex;
UNIFORM(2) sampler2D background_binary_tex;
UNIFORM(3) sampler2D code_tex;
UNIFORM(4) vec4 code_tex_params;
UNIFORM(5) float time;

void main(void) {
fragColor = sample_background(
background_tex,
background_binary_tex,
code_tex,
code_tex_params,
texCoord,
1,
time
);
}
63 changes: 63 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_bg.glslh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

vec2 code_transform_uv(float num_segs, float inv_num_segs, vec2 uv) {
uv *= inv_num_segs;
float cur_segment = floor(mod(uv.y, num_segs)) * inv_num_segs;
uv.x = mod(uv.x - cur_segment, 1.0);
uv.y = mod(uv.y, 1.0);
return uv;
}

vec4 sample_code(sampler2D sampler_code, vec4 code_tex_params, vec2 uv) {
vec2 code_aspect = code_tex_params.xy;
float num_segs = code_tex_params.z;
float inv_num_segs = code_tex_params.w;

uv *= code_aspect;
uv = code_transform_uv(num_segs, inv_num_segs, uv * 2);
return texture(sampler_code, uv);
}

vec2 estd(vec2 p, vec2 f) {
p = mod(p, 1.0);
p -= 0.5;
p *= f;
p = p * sqrt(1.0 - 0.5 * p.yx * p.yx);
p /= f;
p += 0.5;
return p;
}

vec4 sample_background(
sampler2D sampler_bg,
sampler2D sampler_bg_binary,
sampler2D sampler_code,
vec4 code_tex_params,
vec2 uv,
float r,
float t
) {
uv = estd(uv, vec2(1.5, 1.0) * r);

vec2 bg_uv = uv * vec2(0.5, 0.8) * 0.5;
vec2 bg_binary_uv = bg_uv;

t *= 0.5;

bg_uv.x += t * 0.02;
bg_uv.y -= t * 0.145;
bg_binary_uv.x += t * 0.013937;
bg_binary_uv.y += t * 0.05;

vec2 code_uv = uv;
code_uv.x += t * 0.3;
code_uv.y += t * 0.2;

vec4 background = texture(sampler_bg, bg_uv);
vec4 background_binary = texture(sampler_bg_binary, bg_binary_uv);
vec4 code = sample_code(sampler_code, code_tex_params, code_uv) * r;

background = background * background_binary + code;
background.rgb = pow(background.rgb, mix(1 - vec3(1.0, 0.5, 0.3), vec3(1), r));

return background;
}
2 changes: 2 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_bg.prog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

objects = standardnotex.vert extra_bg.frag
59 changes: 59 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_tower_apply_mask.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#version 330 core

#include "lib/render_context.glslh"
#include "lib/util.glslh"
#include "interface/standard.glslh"
#include "extra_bg.glslh"

UNIFORM(1) sampler2D depth_tex;
UNIFORM(2) sampler2D mask_tex;
UNIFORM(3) sampler2D background_tex;
UNIFORM(4) sampler2D background_binary_tex;
UNIFORM(5) sampler2D code_tex;
UNIFORM(6) vec4 code_tex_params; // (aspect_x, aspect_y, num_segs, 1/num_segs)
UNIFORM(7) vec2 dissolve; // (dissolve, dissolve^2)
UNIFORM(8) float time;

vec4 sample_background(vec2 uv, float r, float t) {
return sample_background(
background_tex,
background_binary_tex,
code_tex,
code_tex_params,
uv,
r,
t
);
}

void main(void) {
vec4 tower = texture(tex, texCoord);
float depthval = texture(depth_tex, texCoord).r;
vec4 masks = texture(mask_tex, texCoord);

float global_dissolve_phase = smoothstep(dissolve.y, dissolve.x, masks.a);
masks.rg = mix(masks.rg, vec2(1, 0), global_dissolve_phase);

if(masks.g == 1) {
fragColor = tower;
gl_FragDepth = depthval;
return;
}

float maskval = masks.r;
vec4 background = sample_background(texCoord, maskval, time);

maskval = 0.5 + 0.5 * sin(pi * (maskval + 1.42 * smoothstep(0.5, 1.0, depthval) * masks.b));
maskval = smoothstep(0, 1, maskval + 0.5);
maskval = smoothstep(0.75, 1.0, maskval);
maskval = mix(maskval, 1, masks.g);
maskval = mix(maskval, 0, global_dissolve_phase);

vec4 result = background;
result = alphaCompose(result, tower * maskval);
depthval = mix(depthval, 0, global_dissolve_phase);
depthval = mix(depthval, depthval * depthval, (1 - maskval) * 0.5);

fragColor = result;
gl_FragDepth = depthval;
}
2 changes: 2 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_tower_apply_mask.prog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

objects = standard.vert extra_tower_apply_mask.frag
22 changes: 22 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_tower_mask.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#version 330 core

#include "interface/tower_light.glslh"
#include "lib/util.glslh"

UNIFORM(24) float time;
UNIFORM(25) sampler2D tex2;
UNIFORM(26) float dissolve;

void main(void) {
vec4 texel2 = texture(tex2, texCoord);
vec4 texel = texture(tex, texCoord);

float m = texel.r;
m = 0.5 + 0.5 * sin(pi*m + time);
m = 0.5 + 0.5 * sin(2*pi*m + time);

float dissolve_phase = smoothstep(dissolve * dissolve, dissolve, texel.r);
float d = mix(1.0 - texel2.r, 1.0, dissolve_phase);

fragColor = vec4(m, d, texel2.g, texel.r);
}
1 change: 1 addition & 0 deletions resources/00-taisei.pkgdir/shader/extra_tower_mask.prog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
objects = extra_tower_mask.vert extra_tower_mask.frag
11 changes: 11 additions & 0 deletions resources/00-taisei.pkgdir/shader/extra_tower_mask.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#version 330 core

#include "lib/render_context.glslh"
#include "interface/tower_light.glslh"

void main(void) {
gl_Position = r_projectionMatrix * r_modelViewMatrix * vec4(position, 1.0);
texCoord = (r_textureMatrix * vec4(texCoordRawIn, 0.0, 1.0)).xy;
normal = normalIn;
l = lightvec - (r_modelViewMatrix*vec4(position,1.0)).xyz;
}
4 changes: 4 additions & 0 deletions resources/00-taisei.pkgdir/shader/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ glsl_files = files(
'boss_zoom.frag.glsl',
'circle_distort.frag.glsl',
'copy_depth.frag.glsl',
'extra_bg.frag.glsl',
'extra_tower_apply_mask.frag.glsl',
'extra_tower_mask.frag.glsl',
'extra_tower_mask.vert.glsl',
'fxaa.frag.glsl',
'fxaa.vert.glsl',
'glitch.frag.glsl',
Expand Down
2 changes: 2 additions & 0 deletions resources/00-taisei.pkgdir/shader/zbuf_fog.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ void main(void) {
float f = clamp((end - z)/(end-start),0.0,1.0);

fragColor = f*texture(tex, texCoord) + (1.0-f)*fog_color;

// fragColor = texture(depth, texCoord);
}
2 changes: 1 addition & 1 deletion src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void stage_init_array(void) {
#endif

add_stage(0xC0, &corotest_procs, STAGE_SPECIAL, "Coroutines!", "wow such concurrency very async", NULL, D_Any);
add_stage(0xC1, &extra_procs, STAGE_SPECIAL, "Extra Stage", "totally a good idea to create already", NULL, D_Extra);
add_stage(0xC1, &extra_procs, STAGE_SPECIAL, "Extra Stage", "Descent into Madness", NULL, D_Extra);

end_stages();

Expand Down
16 changes: 13 additions & 3 deletions src/stagedraw.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static void stage_draw_setup_framebuffers(void) {
memcpy(&a_color->tex_params, &tex_common, sizeof(tex_common));
memcpy(&a_depth->tex_params, &tex_common, sizeof(tex_common));

a_depth->tex_params.type = TEX_TYPE_DEPTH;
a_depth->tex_params.type = TEX_TYPE_DEPTH_32;

// Foreground: 1 RGB texture per FB
a_color->tex_params.type = TEX_TYPE_RGB_16;
Expand Down Expand Up @@ -695,10 +695,13 @@ static void finish_3d_scene(FBPair *fbpair) {
// as far as I can tell.

apply_shader_rules((ShaderRule[]) {
/*
config_get_int(CONFIG_FXAA)
? fxaa_rule
: copydepth_rule,
NULL
*/
copydepth_rule, NULL
}, fbpair);
}

Expand All @@ -724,6 +727,11 @@ static void apply_bg_shaders(ShaderRule *shaderrules, FBPair *fbos) {
if(should_draw_stage_bg()) {
finish_3d_scene(fbos);
apply_shader_rules(shaderrules, fbos);

// anti-aliasing
if(config_get_int(CONFIG_FXAA)) {
apply_shader_rules((ShaderRule[]) { fxaa_rule, NULL } , fbos);
}
}

if(b && b->current && b->current->draw_rule) {
Expand Down Expand Up @@ -1426,11 +1434,13 @@ void stage_draw_bottom_text(void) {
Font *font;

#ifdef DEBUG
snprintf(buf, sizeof(buf), "%.2f lfps, %.2f rfps, timer: %d, frames: %d",
snprintf(buf, sizeof(buf), "%.2f lfps, %.2f rfps, timer: %d, frames: %d (%d:%02d) ",
global.fps.logic.fps,
global.fps.render.fps,
global.timer,
global.frames
global.frames,
global.frames / 3600,
(global.frames % 3600) / 60
);
#else
if(get_effective_frameskip() > 1) {
Expand Down
6 changes: 3 additions & 3 deletions src/stagedraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void stage_display_clear_screen(const StageClearBonus *bonus);
void stage_draw_begin_noshake(void);
void stage_draw_end_noshake(void);

FBPair* stage_get_fbpair(StageFBPair id) attr_returns_nonnull;
Framebuffer* stage_add_foreground_framebuffer(const char *label, float scale_worst, float scale_best, uint num_attachments, FBAttachmentConfig attachments[num_attachments]);
Framebuffer* stage_add_background_framebuffer(const char *label, float scale_worst, float scale_best, uint num_attachments, FBAttachmentConfig attachments[num_attachments]);
FBPair *stage_get_fbpair(StageFBPair id) attr_returns_nonnull;
Framebuffer *stage_add_foreground_framebuffer(const char *label, float scale_worst, float scale_best, uint num_attachments, FBAttachmentConfig attachments[num_attachments]);
Framebuffer *stage_add_background_framebuffer(const char *label, float scale_worst, float scale_best, uint num_attachments, FBAttachmentConfig attachments[num_attachments]);

#endif // IGUARD_stagedraw_h
Loading

0 comments on commit 4aa6ba5

Please sign in to comment.