From e08e51b9393592e45e5a51cb731b1bfaba241289 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 17 Sep 2021 11:23:32 -0400 Subject: [PATCH] Planet background system --- core/src/mindustry/core/Renderer.java | 44 ++++++++++++++++++- core/src/mindustry/game/Rules.java | 3 ++ .../mindustry/graphics/g3d/PlanetParams.java | 6 ++- .../graphics/g3d/PlanetRenderer.java | 23 ++++------ core/src/mindustry/io/JsonIO.java | 13 ++++++ .../maps/planet/AsteroidGenerator.java | 7 ++- gradle.properties | 2 +- 7 files changed, 79 insertions(+), 19 deletions(-) diff --git a/core/src/mindustry/core/Renderer.java b/core/src/mindustry/core/Renderer.java index 73fbfbba1a..ae8fa03536 100644 --- a/core/src/mindustry/core/Renderer.java +++ b/core/src/mindustry/core/Renderer.java @@ -43,6 +43,7 @@ public class Renderer implements ApplicationListener{ public PlanetRenderer planets; public @Nullable Bloom bloom; + public @Nullable FrameBuffer backgroundBuffer; public FrameBuffer effectBuffer = new FrameBuffer(); public boolean animateShields, drawWeather = true, drawStatus; public float weatherAlpha; @@ -109,6 +110,14 @@ public class Renderer implements ApplicationListener{ t.setWrap(TextureWrap.repeat); t.setFilter(TextureFilter.linear); }; + + Events.on(WorldLoadEvent.class, e -> { + //reset background buffer on every world load, so it can be re-cached first render + if(backgroundBuffer != null){ + backgroundBuffer.dispose(); + backgroundBuffer = null; + } + }); } @Override @@ -278,7 +287,7 @@ public class Renderer implements ApplicationListener{ } if(bloom != null){ - bloom.resize(graphics.getWidth() / 4, graphics.getHeight() / 4); + bloom.resize(graphics.getWidth(), graphics.getHeight()); Draw.draw(Layer.bullet - 0.02f, bloom::capture); Draw.draw(Layer.effect + 0.02f, bloom::render); } @@ -312,7 +321,8 @@ public class Renderer implements ApplicationListener{ } protected void drawBackground(){ - if(state.rules.backgroundTexture != null){ + //draw background only if there is no planet background with a skybox + if(state.rules.backgroundTexture != null && (state.rules.planetBackground == null || !state.rules.planetBackground.drawSkybox)){ if(!assets.isLoaded(state.rules.backgroundTexture, Texture.class)){ var file = assets.getFileHandleResolver().resolve(state.rules.backgroundTexture); @@ -346,6 +356,36 @@ public class Renderer implements ApplicationListener{ Draw.rect(Tmp.tr1, camera.position.x, camera.position.y, camera.width, camera.height); } + + if(state.rules.planetBackground != null){ + int size = Math.max(graphics.getWidth(), graphics.getHeight()); + + boolean resized = false; + if(backgroundBuffer == null){ + resized = true; + backgroundBuffer = new FrameBuffer(size, size); + } + + if(resized || backgroundBuffer.resize(size, size)){ + backgroundBuffer.begin(Color.clear); + + var params = state.rules.planetBackground; + + //override some values + params.viewW = size; + params.viewH = size; + params.alwaysDrawAtmosphere = true; + params.drawUi = false; + + planets.render(params); + + backgroundBuffer.end(); + } + + float drawSize = Math.max(camera.width, camera.height); + Draw.rect(Draw.wrap(backgroundBuffer.getTexture()), camera.position.x, camera.position.y, drawSize, -drawSize); + } + } void updateLandParticles(){ diff --git a/core/src/mindustry/game/Rules.java b/core/src/mindustry/game/Rules.java index ab67b3d0e3..60d3748ea2 100644 --- a/core/src/mindustry/game/Rules.java +++ b/core/src/mindustry/game/Rules.java @@ -6,6 +6,7 @@ import arc.util.*; import arc.util.serialization.*; import arc.util.serialization.Json.*; import mindustry.content.*; +import mindustry.graphics.g3d.*; import mindustry.io.*; import mindustry.type.*; import mindustry.type.Weather.*; @@ -129,6 +130,8 @@ public class Rules{ public float backgroundScl = 1f; /** background UV offsets */ public float backgroundOffsetX = 0.1f, backgroundOffsetY = 0.1f; + /** Parameters for planet rendered in the background. Cannot be changed once a map is loaded. */ + public @Nullable PlanetParams planetBackground; /** Copies this ruleset exactly. Not efficient at all, do not use often. */ public Rules copy(){ diff --git a/core/src/mindustry/graphics/g3d/PlanetParams.java b/core/src/mindustry/graphics/g3d/PlanetParams.java index 8c913af577..bab65f4eb9 100644 --- a/core/src/mindustry/graphics/g3d/PlanetParams.java +++ b/core/src/mindustry/graphics/g3d/PlanetParams.java @@ -26,10 +26,14 @@ public class PlanetParams{ public boolean drawUi = false; /** If true, a space skybox is drawn. */ public boolean drawSkybox = true; + /** Handles drawing details. */ public @Nullable transient PlanetInterfaceRenderer renderer; + /** Viewport size. <=0 to use screen size. Do not change in rules. */ + public transient int viewW = -1, viewH = -1; + /** If true, atmosphere will be drawn regardless of player options. */ + public transient boolean alwaysDrawAtmosphere = false; //TODO: //- blur - //- transparent background } diff --git a/core/src/mindustry/graphics/g3d/PlanetRenderer.java b/core/src/mindustry/graphics/g3d/PlanetRenderer.java index fcc184abbc..dbdac3100b 100644 --- a/core/src/mindustry/graphics/g3d/PlanetRenderer.java +++ b/core/src/mindustry/graphics/g3d/PlanetRenderer.java @@ -59,12 +59,15 @@ public class PlanetRenderer implements Disposable{ Gl.enable(Gl.cullFace); Gl.cullFace(Gl.back); + int w = params.viewW <= 0 ? Core.graphics.getWidth() : params.viewW; + int h = params.viewH <= 0 ? Core.graphics.getHeight() : params.viewH; + bloom.blending = !params.drawSkybox; //lock to up vector so it doesn't get confusing cam.up.set(Vec3.Y); - cam.resize(Core.graphics.getWidth(), Core.graphics.getHeight()); + cam.resize(w, h); params.camPos.setLength((params.planet.radius + params.planet.camRadius) * camLength + (params.zoom-1f) * (params.planet.radius + params.planet.camRadius) * 2); cam.position.set(params.planet.position).add(params.camPos); //cam.up.set(params.camUp); //TODO broken @@ -79,7 +82,9 @@ public class PlanetRenderer implements Disposable{ Events.fire(Trigger.universeDrawBegin); - beginBloom(); + //begin bloom + bloom.resize(w, h); + bloom.capture(); if(params.drawSkybox){ //render skybox at 0,0,0 @@ -102,7 +107,7 @@ public class PlanetRenderer implements Disposable{ renderPlanet(params.solarSystem, params); renderTransparent(params.solarSystem, params); - endBloom(); + bloom.render(); Events.fire(Trigger.universeDrawEnd); @@ -118,16 +123,6 @@ public class PlanetRenderer implements Disposable{ cam.update(); } - public void beginBloom(){ - bloom.resize(Core.graphics.getWidth() / 4, Core.graphics.getHeight() / 4); - bloom.capture(); - } - - public void endBloom(){ - bloom.render(); - } - - public void renderPlanet(Planet planet, PlanetParams params){ if(!planet.visible()) return; @@ -152,7 +147,7 @@ public class PlanetRenderer implements Disposable{ renderSectors(planet, params); } - if(cam.frustum.containsSphere(planet.position, planet.clipRadius) && planet.parent != null && planet.hasAtmosphere && Core.settings.getBool("atmosphere")){ + if(cam.frustum.containsSphere(planet.position, planet.clipRadius) && planet.parent != null && planet.hasAtmosphere && (params.alwaysDrawAtmosphere || Core.settings.getBool("atmosphere"))){ planet.drawAtmosphere(atmosphere, cam); } diff --git a/core/src/mindustry/io/JsonIO.java b/core/src/mindustry/io/JsonIO.java index 3e6beb26e8..e92f59bdb1 100644 --- a/core/src/mindustry/io/JsonIO.java +++ b/core/src/mindustry/io/JsonIO.java @@ -163,6 +163,19 @@ public class JsonIO{ } }); + json.setSerializer(Planet.class, new Serializer<>(){ + @Override + public void write(Json json, Planet object, Class knownType){ + json.writeValue(object.name); + } + + @Override + public Planet read(Json json, JsonValue jsonData, Class type){ + Planet block = Vars.content.getByName(ContentType.planet, jsonData.asString()); + return block == null ? Planets.serpulo : block; + } + }); + json.setSerializer(Weather.class, new Serializer<>(){ @Override public void write(Json json, Weather object, Class knownType){ diff --git a/core/src/mindustry/maps/planet/AsteroidGenerator.java b/core/src/mindustry/maps/planet/AsteroidGenerator.java index fb912ee368..c48933fa6c 100644 --- a/core/src/mindustry/maps/planet/AsteroidGenerator.java +++ b/core/src/mindustry/maps/planet/AsteroidGenerator.java @@ -4,6 +4,7 @@ import arc.math.*; import arc.util.noise.*; import mindustry.content.*; import mindustry.game.*; +import mindustry.graphics.g3d.*; import mindustry.maps.generators.*; import mindustry.type.*; import mindustry.world.blocks.environment.*; @@ -132,7 +133,11 @@ public class AsteroidGenerator extends BlankPlanetGenerator{ Schematics.placeLaunchLoadout(sx, sy); - state.rules.backgroundTexture = "sprites/space.png"; + state.rules.planetBackground = new PlanetParams(){{ + planet = sector.planet; + zoom = 1f; + }}; + //state.rules.backgroundTexture = "sprites/space.png"; state.rules.borderDarkness = false; state.rules.environment = Env.space; } diff --git a/gradle.properties b/gradle.properties index 2c3b46df95..f3fc4af013 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,4 @@ android.useAndroidX=true #used for slow jitpack builds; TODO see if this actually works http.socketTimeout=80000 http.connectionTimeout=80000 -archash=e2eead82168c1a143e345bc5f78e2d72078f4657 +archash=e982971b17f48c13f3a18c344b8080b7498cccf6