Planet background system

This commit is contained in:
Anuken
2021-09-17 11:23:32 -04:00
parent 76b0ca53c5
commit e08e51b939
7 changed files with 79 additions and 19 deletions

View File

@@ -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(){

View File

@@ -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(){

View File

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

View File

@@ -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);
}

View File

@@ -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){

View File

@@ -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;
}