|
| 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