From 138a9ffae7c793ab3f738fb16278f1ec38412b48 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 27 Sep 2019 22:59:02 -0400 Subject: [PATCH] Experimenting with light --- core/assets/shaders/light.fragment.glsl | 22 +++++++++ .../src/io/anuke/mindustry/core/Renderer.java | 3 ++ .../anuke/mindustry/entities/type/Bullet.java | 2 + .../anuke/mindustry/entities/type/Player.java | 2 + .../mindustry/graphics/LightRenderer.java | 47 +++++++++++++++++++ .../io/anuke/mindustry/graphics/Shaders.java | 16 +++++++ .../blocks/power/ItemLiquidGenerator.java | 33 ++++++------- .../blocks/production/GenericSmelter.java | 4 ++ 8 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 core/assets/shaders/light.fragment.glsl create mode 100644 core/src/io/anuke/mindustry/graphics/LightRenderer.java diff --git a/core/assets/shaders/light.fragment.glsl b/core/assets/shaders/light.fragment.glsl new file mode 100644 index 0000000000..a4dcd4e710 --- /dev/null +++ b/core/assets/shaders/light.fragment.glsl @@ -0,0 +1,22 @@ +#ifdef GL_ES +precision mediump float; +precision mediump int; +#endif + +#define steprad 0.13 + +uniform sampler2D u_texture; +uniform vec4 u_ambient; + +varying vec4 v_color; +varying vec2 v_texCoord; + +float stepped(float inp){ + return inp; +} + +void main(){ + vec4 color = texture2D(u_texture, v_texCoord.xy); + float rounded = stepped(color.a); + gl_FragColor = clamp(vec4(mix(u_ambient.rgb, color.rgb, rounded), u_ambient.a - rounded), 0.0, 1.0); +} diff --git a/core/src/io/anuke/mindustry/core/Renderer.java b/core/src/io/anuke/mindustry/core/Renderer.java index c6652e3327..2d8ec566a2 100644 --- a/core/src/io/anuke/mindustry/core/Renderer.java +++ b/core/src/io/anuke/mindustry/core/Renderer.java @@ -33,6 +33,7 @@ public class Renderer implements ApplicationListener{ public final BlockRenderer blocks = new BlockRenderer(); public final MinimapRenderer minimap = new MinimapRenderer(); public final OverlayRenderer overlays = new OverlayRenderer(); + public final LightRenderer lights = new LightRenderer(); public final Pixelator pixelator = new Pixelator(); public FrameBuffer shieldBuffer = new FrameBuffer(2, 2); @@ -297,6 +298,8 @@ public class Renderer implements ApplicationListener{ playerGroup.draw(p -> !p.isDead(), Player::drawName); + lights.draw(); + drawLanding(); Draw.color(); diff --git a/core/src/io/anuke/mindustry/entities/type/Bullet.java b/core/src/io/anuke/mindustry/entities/type/Bullet.java index a1f4831f66..a5c3d21697 100644 --- a/core/src/io/anuke/mindustry/entities/type/Bullet.java +++ b/core/src/io/anuke/mindustry/entities/type/Bullet.java @@ -11,6 +11,7 @@ import io.anuke.mindustry.entities.bullet.*; import io.anuke.mindustry.entities.effect.*; import io.anuke.mindustry.entities.traits.*; import io.anuke.mindustry.game.*; +import io.anuke.mindustry.graphics.*; import io.anuke.mindustry.world.*; import static io.anuke.mindustry.Vars.*; @@ -294,6 +295,7 @@ public class Bullet extends SolidEntity implements DamageTrait, ScaleTrait, Pool @Override public void draw(){ type.draw(this); + renderer.lights.add(x, y, 16f, Pal.powerLight, 0.3f); } @Override diff --git a/core/src/io/anuke/mindustry/entities/type/Player.java b/core/src/io/anuke/mindustry/entities/type/Player.java index 9f35158043..7f51aed5d1 100644 --- a/core/src/io/anuke/mindustry/entities/type/Player.java +++ b/core/src/io/anuke/mindustry/entities/type/Player.java @@ -341,6 +341,8 @@ public class Player extends Unit implements BuilderMinerTrait, ShooterTrait{ } Draw.reset(); + + renderer.lights.add(x, y, 100f, Pal.powerLight, 0.6f); } @Override diff --git a/core/src/io/anuke/mindustry/graphics/LightRenderer.java b/core/src/io/anuke/mindustry/graphics/LightRenderer.java new file mode 100644 index 0000000000..bfd5a3c7c0 --- /dev/null +++ b/core/src/io/anuke/mindustry/graphics/LightRenderer.java @@ -0,0 +1,47 @@ +package io.anuke.mindustry.graphics; + +import io.anuke.arc.*; +import io.anuke.arc.collection.*; +import io.anuke.arc.graphics.*; +import io.anuke.arc.graphics.g2d.*; +import io.anuke.arc.graphics.glutils.*; + +public class LightRenderer{ + private static final int scaling = 4; + private FrameBuffer buffer = new FrameBuffer(2, 2); + private Array lights = new Array<>(); + + public void add(Runnable run){ + lights.add(run); + } + + public void add(float x, float y, float radius, Color color, float opacity){ + add(() -> { + Draw.color(color, opacity); + Draw.rect("circle-shadow", x, y, radius * 2, radius * 2); + }); + } + + public void draw(){ + if(buffer.getWidth() != Core.graphics.getWidth()/scaling || buffer.getHeight() != Core.graphics.getHeight()/scaling){ + buffer.resize(Core.graphics.getWidth()/scaling, Core.graphics.getHeight()/scaling); + } + + Draw.color(); + buffer.beginDraw(Color.clear); + Draw.blend(Blending.additive); + for(Runnable run : lights){ + run.run(); + } + Draw.reset(); + Draw.blend(); + buffer.endDraw(); + + Draw.color(); + Draw.shader(Shaders.light); + Draw.rect(Draw.wrap(buffer.getTexture()), Core.camera.position.x, Core.camera.position.y, Core.camera.width, -Core.camera.height); + Draw.shader(); + + lights.clear(); + } +} diff --git a/core/src/io/anuke/mindustry/graphics/Shaders.java b/core/src/io/anuke/mindustry/graphics/Shaders.java index 2c2316c07d..83c879aa90 100644 --- a/core/src/io/anuke/mindustry/graphics/Shaders.java +++ b/core/src/io/anuke/mindustry/graphics/Shaders.java @@ -15,6 +15,7 @@ public class Shaders{ public static UnitBuild build; public static FogShader fog; public static MenuShader menu; + public static LightShader light; public static SurfaceShader water, tar; public static void init(){ @@ -30,10 +31,25 @@ public class Shaders{ build = new UnitBuild(); fog = new FogShader(); menu = new MenuShader(); + light = new LightShader(); water = new SurfaceShader("water"); tar = new SurfaceShader("tar"); } + public static class LightShader extends LoadShader{ + public Color ambient = new Color(0.01f, 0.01f, 0.04f, 0.99f); + + public LightShader(){ + super("light", "default"); + } + + @Override + public void apply(){ + setUniformf("u_ambient", ambient); + } + + } + public static class MenuShader extends LoadShader{ float time = 0f; diff --git a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java index d32813791d..01f42a6805 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java @@ -1,24 +1,19 @@ package io.anuke.mindustry.world.blocks.power; -import io.anuke.arc.Core; -import io.anuke.arc.graphics.Color; -import io.anuke.arc.graphics.g2d.Draw; -import io.anuke.arc.graphics.g2d.TextureRegion; -import io.anuke.arc.math.Mathf; -import io.anuke.arc.util.Time; -import io.anuke.mindustry.content.Fx; -import io.anuke.mindustry.entities.Effects; -import io.anuke.mindustry.entities.type.TileEntity; -import io.anuke.mindustry.type.Item; -import io.anuke.mindustry.type.Liquid; -import io.anuke.mindustry.world.Tile; -import io.anuke.mindustry.world.consumers.ConsumeItemFilter; -import io.anuke.mindustry.world.consumers.ConsumeLiquidFilter; -import io.anuke.mindustry.world.meta.BlockStat; -import io.anuke.mindustry.world.meta.StatUnit; +import io.anuke.arc.*; +import io.anuke.arc.graphics.*; +import io.anuke.arc.graphics.g2d.*; +import io.anuke.arc.math.*; +import io.anuke.arc.util.*; +import io.anuke.mindustry.content.*; +import io.anuke.mindustry.entities.*; +import io.anuke.mindustry.entities.type.*; +import io.anuke.mindustry.type.*; +import io.anuke.mindustry.world.*; +import io.anuke.mindustry.world.consumers.*; +import io.anuke.mindustry.world.meta.*; -import static io.anuke.mindustry.Vars.content; -import static io.anuke.mindustry.Vars.tilesize; +import static io.anuke.mindustry.Vars.*; /** * Power generation block which can use items, liquids or both as input sources for power production. @@ -157,6 +152,8 @@ public class ItemLiquidGenerator extends PowerGenerator{ Draw.rect(liquidRegion, tile.drawx(), tile.drawy()); Draw.color(); } + + renderer.lights.add(tile.drawx(), tile.drawy(), (60f + Mathf.absin(10f, 5f)) * entity.productionEfficiency * size, Color.orange, 0.5f); } protected float getItemEfficiency(Item item){ diff --git a/core/src/io/anuke/mindustry/world/blocks/production/GenericSmelter.java b/core/src/io/anuke/mindustry/world/blocks/production/GenericSmelter.java index 576236cd53..99eb3628bf 100644 --- a/core/src/io/anuke/mindustry/world/blocks/production/GenericSmelter.java +++ b/core/src/io/anuke/mindustry/world/blocks/production/GenericSmelter.java @@ -7,6 +7,8 @@ import io.anuke.arc.math.Mathf; import io.anuke.arc.util.Time; import io.anuke.mindustry.world.Tile; +import static io.anuke.mindustry.Vars.renderer; + /** A GenericCrafter with a new glowing region drawn on top. */ public class GenericSmelter extends GenericCrafter{ protected Color flameColor = Color.valueOf("ffc999"); @@ -43,6 +45,8 @@ public class GenericSmelter extends GenericCrafter{ Fill.circle(tile.drawx(), tile.drawy(), 1.9f + Mathf.absin(Time.time(), 5f, 1f) + cr); Draw.color(); + + renderer.lights.add(tile.drawx(), tile.drawy(), (60f + Mathf.absin(10f, 5f)) * entity.warmup * size, flameColor, 0.65f); } } }