Skip to content

Commit 20a4002

Browse files
committed
Slash shader
Felt cute, *cuts reality*
1 parent aff43ba commit 20a4002

File tree

7 files changed

+173
-7
lines changed

7 files changed

+173
-7
lines changed

assets/shaders/slash.frag

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#define HIGHP
2+
3+
#define PI 3.1415926535897932384626433832795
4+
#define PI2 PI * 2.0
5+
#define HALFPI PI / 2.0
6+
7+
uniform sampler2D u_texture;
8+
9+
uniform vec2 u_campos;
10+
uniform vec2 u_resolution;
11+
12+
uniform int u_slashescount;
13+
uniform vec4 u_slashes[MAX_COUNT]; //x, y, angle, offset
14+
15+
varying vec2 v_texCoords;
16+
17+
bool invert(float a1, float a2){
18+
float fd = abs(a1 - a2);
19+
float bd = PI2 - fd;
20+
return a1 > a2 == bd > fd;
21+
}
22+
23+
void main() {
24+
vec2 c = v_texCoords.xy;
25+
vec2 coords = (c * u_resolution) + u_campos;
26+
27+
vec2 offset = vec2(0.0);
28+
for(int i = 0; i < u_slashescount; ++i){
29+
vec4 slash = u_slashes[i];
30+
float sX = slash.r;
31+
float sY = slash.g;
32+
float sA = slash.b; // [0, 2pi]
33+
float sO = slash.a;
34+
35+
float angleTo = atan(coords.x - sX, coords.y - sY); // [-pi, pi]
36+
if(angleTo < 0.0) angleTo += PI2; // Convert to [0, 2pi]
37+
38+
vec2 off = vec2(cos(HALFPI - sA) * sO, sin(HALFPI - sA) * sO);
39+
if(invert(angleTo, sA)) off = -off; //Below the slash, invert
40+
41+
offset += off;
42+
}
43+
44+
coords += offset;
45+
gl_FragColor = texture2D(u_texture, (coords - u_campos) / u_resolution);
46+
}

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mindustryBEVersion =
2626
# Arc version, should either follow `mindustryVersion` for release or whatever hash bleeding-edge Mindustry uses.
2727
arcVersion = v146
2828
# My black hole renderer. Just use the latest release.
29-
blackHoleRendererVersion = v11
29+
blackHoleRendererVersion = v12
3030

3131
##### Android SDK configuration for building Android artifacts.
3232
# Android platform SDK version.

src/progressed/content/bullets/PayloadBullets.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,16 @@ public static void load(){
404404
gravity = 0.02f;
405405
fragBullets = 1;
406406
fragBullet = new BlackHoleBulletType(0f, 4400f / 30f){{
407-
lifetime = 10f * 60f;
408-
growTime = lifetime - MissileFx.bigBlackHoleSwirl.lifetime;
407+
lifetime = 4f * 60f;
408+
growTime = lifetime;
409+
shrinkTime = 0f;
409410
swirlEffect = MissileFx.bigBlackHoleSwirl;
410411
loopSoundVolume = 6f;
411412

412413
damageRadius = 18f * tilesize;
413414
suctionRadius = 64f * tilesize;
415+
416+
despawnEffect = MissileFx.slashTest;
414417
}};
415418
}};
416419

src/progressed/content/effects/MissileFx.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import arc.math.*;
77
import arc.util.*;
88
import blackhole.entities.effect.*;
9+
import blackhole.graphics.*;
910
import mindustry.entities.*;
1011
import mindustry.gen.*;
1112
import mindustry.graphics.*;
13+
import progressed.graphics.renders.*;
1214

1315
import static arc.graphics.g2d.Draw.*;
1416
import static arc.graphics.g2d.Lines.*;
1517
import static arc.math.Angles.*;
1618
import static arc.util.Tmp.*;
19+
import static mindustry.Vars.*;
1720
import static mindustry.graphics.Drawf.*;
1821
import static progressed.util.PMUtls.*;
1922

@@ -185,5 +188,23 @@ public class MissileFx{
185188
});
186189
}),
187190

188-
bigBlackHoleSwirl = new SwirlEffect(90f, 16, 8f, 120f, 480f, true).layer(Layer.effect + 0.005f);
191+
bigBlackHoleSwirl = new SwirlEffect(90f, 16, 8f, 120f, 480f, true).layer(Layer.effect + 0.005f),
192+
193+
slashTest = new Effect(90, 128 * tilesize * 8, e -> { //b i g c l i p
194+
float ang = Mathf.randomSeed(e.id, Mathf.PI2);
195+
float fout = e.fout(Interp.circleIn);
196+
SlashRenderer.addSlash(e.x, e.y, Mathf.halfPi - ang, 64 * fout);
197+
198+
ang *= Mathf.radDeg;
199+
Tmp.v1.trns(ang, 128 * tilesize * fout);
200+
Tmp.v2.trns(ang + 90f, tilesize * fout);
201+
202+
Draw.color(Color.white);
203+
Fill.quad(
204+
e.x + Tmp.v1.x, e.y + Tmp.v1.y,
205+
e.x + Tmp.v2.x, e.y + Tmp.v2.y,
206+
e.x - Tmp.v1.x, e.y - Tmp.v1.y,
207+
e.x - Tmp.v2.x, e.y - Tmp.v2.y
208+
);
209+
}).layer(BHLayer.end + 2f);
189210
}

src/progressed/graphics/PMShaders.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ public PassThroughShader(){
152152
}
153153
}
154154

155-
static class PMLoadShader extends Shader{
156-
PMLoadShader(String vert, String frag){
155+
public static class PMLoadShader extends Shader{
156+
public PMLoadShader(String vert, String frag){
157157
super(
158158
files.internal("shaders/" + vert + ".vert"),
159159
tree.get("shaders/" + frag + ".frag")
160160
);
161161
}
162162

163-
PMLoadShader(String frag){
163+
public PMLoadShader(String frag){
164164
this("default", frag);
165165
}
166166
}

src/progressed/graphics/renders/PMRenders.java

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class PMRenders{
2121

2222
public static void init(){
2323
dimRenderer = new DimRenderer();
24+
SlashRenderer.init();
2425

2526
Events.on(ResetEvent.class, e -> {
2627
flashReduction = flashTime = 0;
@@ -40,6 +41,8 @@ public static void init(){
4041
Draw.color();
4142
}
4243
});
44+
45+
SlashRenderer.draw();
4346
});
4447
}
4548

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package progressed.graphics.renders;
2+
3+
import arc.*;
4+
import arc.graphics.g2d.*;
5+
import arc.graphics.gl.*;
6+
import arc.math.*;
7+
import arc.struct.*;
8+
import blackhole.graphics.*;
9+
import progressed.graphics.PMShaders.*;
10+
11+
import static arc.Core.*;
12+
13+
public class SlashRenderer{
14+
private static int maxCount = 4;
15+
private static SlashShader slashShader;
16+
private static final Seq<SlashData> slashes = new Seq<>();
17+
private static FrameBuffer buffer;
18+
19+
private static void createShader(){
20+
if(maxCount >= 1021) return; //Exceeds maximum number of registers for a single shader
21+
22+
if(slashShader != null){
23+
maxCount = Math.min(1021, maxCount * 2);
24+
slashShader.dispose();
25+
}
26+
27+
Shader.prependFragmentCode = "#define MAX_COUNT " + maxCount + "\n";
28+
slashShader = new SlashShader();
29+
Shader.prependFragmentCode = "";
30+
}
31+
32+
public static void init(){
33+
createShader();
34+
buffer = new FrameBuffer();
35+
}
36+
37+
public static void addSlash(float x, float y, float a, float off){
38+
slashes.add(new SlashData(x, y, a, off));
39+
}
40+
41+
public static void draw(){
42+
Draw.draw(BHLayer.begin - 0.1f, () -> {
43+
buffer.resize(graphics.getWidth(), graphics.getHeight());
44+
buffer.begin();
45+
});
46+
47+
Draw.draw(BHLayer.end + 1f, () -> {
48+
buffer.end();
49+
50+
if(slashes.size > maxCount) createShader();
51+
52+
float[] slashArray = new float[slashes.size * 4];
53+
for(int i = 0; i < slashes.size; i++){
54+
SlashData slash = slashes.get(i);
55+
slashArray[i * 4] = slash.x;
56+
slashArray[i * 4 + 1] = slash.y;
57+
slashArray[i * 4 + 2] = Mathf.mod(slash.angle, Mathf.PI2) ;
58+
slashArray[i * 4 + 3] = slash.offset;
59+
}
60+
slashShader.slashes = slashArray;
61+
buffer.blit(slashShader);
62+
slashes.clear();
63+
});
64+
}
65+
66+
private static class SlashShader extends PMLoadShader{
67+
public float[] slashes;
68+
69+
private SlashShader(){
70+
super("screenspace", "slash");
71+
}
72+
73+
@Override
74+
public void apply(){
75+
setUniformf("u_campos", Core.camera.position.x - Core.camera.width / 2, Core.camera.position.y - Core.camera.height / 2);
76+
setUniformf("u_resolution", Core.camera.width, Core.camera.height);
77+
78+
setUniformi("u_slashescount", slashes.length / 4);
79+
setUniform4fv("u_slashes", slashes, 0, slashes.length);
80+
}
81+
}
82+
83+
private static class SlashData{
84+
public float x, y, angle, offset;
85+
86+
public SlashData(float x, float y, float angle, float offset){
87+
this.x = x;
88+
this.y = y;
89+
this.angle = angle;
90+
this.offset = offset;
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)