mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-04-20 04:20:51 -07:00
Refactored planet rendering to different classes
This commit is contained in:
parent
9af93dc44d
commit
7f53328f52
11 changed files with 189 additions and 195 deletions
|
|
@ -198,18 +198,7 @@ public class World{
|
|||
public void loadSector(Sector sector){
|
||||
state.rules.sector = sector;
|
||||
int size = (int)(sector.rect.radius * 3200);
|
||||
|
||||
loadGenerator(size, size, tiles -> {
|
||||
TileGen gen = new TileGen();
|
||||
tiles.each((x, y) -> {
|
||||
gen.reset();
|
||||
Vec3 position = sector.rect.project(x / (float)size, y / (float)size);
|
||||
|
||||
sector.planet.generator.generate(position, gen);
|
||||
tiles.set(x, y, new Tile(x, y, gen.floor, gen.overlay, gen.block));
|
||||
});
|
||||
sector.planet.generator.decorate(tiles, sector);
|
||||
});
|
||||
loadGenerator(size, size, tiles -> sector.planet.generator.generate(tiles, sector));
|
||||
}
|
||||
|
||||
public void loadMap(Map map){
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import arc.graphics.*;
|
|||
import arc.graphics.VertexAttributes.*;
|
||||
import arc.graphics.gl.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.math3d.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import arc.util.*;
|
||||
import mindustry.graphics.PlanetGrid.*;
|
||||
import mindustry.maps.planet.*;
|
||||
|
||||
public class PlanetMesh{
|
||||
private float[] floats = new float[3 + 3 + 1];
|
||||
|
|
@ -19,13 +19,13 @@ public class PlanetMesh{
|
|||
private boolean lines;
|
||||
private float radius, intensity = 0.2f;
|
||||
|
||||
private final PlanetGenerator gen;
|
||||
private final PlanetMesher gen;
|
||||
|
||||
public PlanetMesh(int divisions, PlanetGenerator gen){
|
||||
public PlanetMesh(int divisions, PlanetMesher gen){
|
||||
this(divisions, gen, 1f, false);
|
||||
}
|
||||
|
||||
public PlanetMesh(int divisions, PlanetGenerator gen, float radius, boolean lines){
|
||||
public PlanetMesh(int divisions, PlanetMesher gen, float radius, boolean lines){
|
||||
this.gen = gen;
|
||||
this.radius = radius;
|
||||
this.grid = PlanetGrid.newGrid(divisions);
|
||||
|
|
|
|||
10
core/src/mindustry/graphics/PlanetMesher.java
Normal file
10
core/src/mindustry/graphics/PlanetMesher.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package mindustry.graphics;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.math.geom.*;
|
||||
|
||||
/** Defines color and height for a planet mesh. */
|
||||
public interface PlanetMesher{
|
||||
float getHeight(Vec3 position);
|
||||
Color getColor(Vec3 position);
|
||||
}
|
||||
|
|
@ -1,149 +0,0 @@
|
|||
package mindustry.graphics;
|
||||
|
||||
import arc.*;
|
||||
import arc.graphics.*;
|
||||
import arc.graphics.g2d.*;
|
||||
import arc.graphics.g3d.*;
|
||||
import arc.input.*;
|
||||
import arc.math.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.util.*;
|
||||
import mindustry.graphics.PlanetGrid.*;
|
||||
import mindustry.maps.planet.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.type.Sector.*;
|
||||
import mindustry.world.*;
|
||||
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
public class PlanetRenderer implements PlanetGenerator{
|
||||
private static final Color outlineColor = Pal.accent.cpy().a(0.6f);
|
||||
private static final float camLength = 4f, outlineRad = 1.15f;
|
||||
private static final boolean drawRect = false;
|
||||
|
||||
private final PlanetMesh[] outlines = new PlanetMesh[10];
|
||||
private final Camera3D cam = new Camera3D();
|
||||
private final VertexBatch3D batch = new VertexBatch3D(false, true, 0);
|
||||
|
||||
private float lastX, lastY;
|
||||
|
||||
public PlanetRenderer(){
|
||||
Tmp.v1.trns(0, camLength);
|
||||
cam.position.set(Tmp.v1.x, 0f, Tmp.v1.y);
|
||||
}
|
||||
|
||||
public void render(Planet planet){
|
||||
Draw.flush();
|
||||
Gl.clear(Gl.depthBufferBit);
|
||||
Gl.enable(Gl.depthTest);
|
||||
|
||||
input();
|
||||
|
||||
cam.resize(Core.graphics.getWidth(), Core.graphics.getHeight());
|
||||
cam.update();
|
||||
cam.lookAt(0, 0, 0);
|
||||
cam.update();
|
||||
|
||||
PlanetMesh outline = outline(planet.size);
|
||||
|
||||
planet.mesh.render(cam.combined());
|
||||
outline.render(cam.combined());
|
||||
|
||||
Ptile tile = outline.getTile(cam.getPickRay(Core.input.mouseX(), Core.input.mouseY()));
|
||||
if(tile != null){
|
||||
|
||||
Sector sector = planet.getSector(tile);
|
||||
for(int i = 0; i < sector.tile.corners.length; i++){
|
||||
batch.color(outlineColor);
|
||||
batch.vertex(sector.tile.corners[i].v);
|
||||
}
|
||||
batch.flush(cam.combined(), Gl.triangleFan);
|
||||
|
||||
if(drawRect){
|
||||
SectorRect rect = sector.rect;
|
||||
rect.center.scl(outlineRad);
|
||||
rect.right.scl(outlineRad);
|
||||
rect.top.scl(outlineRad);
|
||||
|
||||
batch.color(Color.red);
|
||||
batch.vertex(rect.center);
|
||||
batch.color(Color.red);
|
||||
batch.vertex(sector.tile.corners[0].v);
|
||||
|
||||
batch.color(Color.green);
|
||||
batch.vertex(rect.center);
|
||||
batch.color(Color.green);
|
||||
batch.vertex(rect.top.cpy().add(rect.center));
|
||||
batch.flush(cam.combined(), Gl.lines);
|
||||
|
||||
//Log.info((int)(sector.tile.corners[0].v.cpy().sub(rect.center).angle(rect.top)));
|
||||
|
||||
rect.center.scl(1f / outlineRad);
|
||||
rect.right.scl(1f / outlineRad);
|
||||
rect.top.scl(1f / outlineRad);
|
||||
}
|
||||
|
||||
if(Core.input.keyTap(KeyCode.SPACE)){
|
||||
control.playSector(sector);
|
||||
ui.planet.hide();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Gl.disable(Gl.depthTest);
|
||||
}
|
||||
|
||||
private PlanetMesh outline(int size){
|
||||
if(outlines[size] == null){
|
||||
outlines[size] = new PlanetMesh(size, this, outlineRad, true);
|
||||
}
|
||||
return outlines[size];
|
||||
}
|
||||
|
||||
private void input(){
|
||||
Vec3 v = Tmp.v33.set(Core.input.mouseX(), Core.input.mouseY(), 0);
|
||||
|
||||
if(Core.input.keyDown(KeyCode.MOUSE_LEFT)){
|
||||
float upV = cam.position.angle(Vec3.Y);
|
||||
float xscale = 9f, yscale = 10f;
|
||||
float margin = 1;
|
||||
|
||||
//scale X speed depending on polar coordinate
|
||||
float speed = 1f - Math.abs(upV - 90) / 90f;
|
||||
|
||||
cam.position.rotate(cam.up, (v.x - lastX) / xscale * speed);
|
||||
|
||||
//prevent user from scrolling all the way up and glitching it out
|
||||
float amount = (v.y - lastY) / yscale;
|
||||
amount = Mathf.clamp(upV + amount, margin, 180f - margin) - upV;
|
||||
|
||||
cam.position.rotate(Tmp.v31.set(cam.up).rotate(cam.direction, 90), amount);
|
||||
}
|
||||
|
||||
//lock to Y coordinate so planet rotation doesn't get confusing
|
||||
cam.up.set(Vec3.Y);
|
||||
|
||||
lastX = v.x;
|
||||
lastY = v.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHeight(Vec3 position){
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor(Vec3 position){
|
||||
return outlineColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate(Vec3 position, TileGen tile){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decorate(Tiles tiles, Sector sec){
|
||||
|
||||
}
|
||||
}
|
||||
31
core/src/mindustry/maps/generators/PlanetGenerator.java
Normal file
31
core/src/mindustry/maps/generators/PlanetGenerator.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package mindustry.maps.generators;
|
||||
|
||||
import arc.math.geom.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.world.*;
|
||||
|
||||
public abstract class PlanetGenerator extends BasicGenerator implements PlanetMesher{
|
||||
protected Sector sector;
|
||||
|
||||
protected void genTile(Vec3 position, TileGen tile){
|
||||
|
||||
}
|
||||
|
||||
public void generate(Tiles tiles, Sector sec){
|
||||
this.tiles = tiles;
|
||||
this.sector = sec;
|
||||
this.rand.setSeed(sec.id);
|
||||
|
||||
TileGen gen = new TileGen();
|
||||
tiles.each((x, y) -> {
|
||||
gen.reset();
|
||||
Vec3 position = sector.rect.project(x / (float)tiles.width, y / (float)tiles.height);
|
||||
|
||||
genTile(position, gen);
|
||||
tiles.set(x, y, new Tile(x, y, gen.floor, gen.overlay, gen.block));
|
||||
});
|
||||
|
||||
generate(tiles);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
package mindustry.maps.planet;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.math.geom.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.world.*;
|
||||
|
||||
public interface PlanetGenerator{
|
||||
float getHeight(Vec3 position);
|
||||
Color getColor(Vec3 position);
|
||||
void generate(Vec3 position, TileGen tile);
|
||||
void decorate(Tiles tiles, Sector sec);
|
||||
}
|
||||
|
|
@ -13,8 +13,7 @@ import mindustry.world.*;
|
|||
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
//TODO refactor into generic planet class
|
||||
public class TestPlanetGenerator extends BasicGenerator implements PlanetGenerator{
|
||||
public class TestPlanetGenerator extends PlanetGenerator{
|
||||
Simplex noise = new Simplex();
|
||||
RidgedPerlin rid = new RidgedPerlin(1, 2);
|
||||
float scl = 5f;
|
||||
|
|
@ -68,7 +67,7 @@ public class TestPlanetGenerator extends BasicGenerator implements PlanetGenerat
|
|||
}
|
||||
|
||||
@Override
|
||||
public void generate(Vec3 position, TileGen tile){
|
||||
public void genTile(Vec3 position, TileGen tile){
|
||||
tile.floor = getBlock(position);
|
||||
tile.block = tile.floor.asFloor().wall;
|
||||
|
||||
|
|
@ -81,15 +80,6 @@ public class TestPlanetGenerator extends BasicGenerator implements PlanetGenerat
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decorate(Tiles tiles, Sector sec){
|
||||
this.tiles = tiles;
|
||||
this.sector = sec;
|
||||
rand.setSeed(sec.id);
|
||||
|
||||
generate(tiles);
|
||||
}
|
||||
|
||||
Block getBlock(Vec3 position){
|
||||
float height = rawHeight(position);
|
||||
position = Tmp.v33.set(position).scl(scl);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package mindustry.type;
|
|||
|
||||
import arc.files.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.math3d.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.struct.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
|
|
@ -11,7 +12,7 @@ import mindustry.*;
|
|||
import mindustry.ctype.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.graphics.PlanetGrid.*;
|
||||
import mindustry.maps.planet.*;
|
||||
import mindustry.maps.generators.*;
|
||||
import mindustry.type.Sector.*;
|
||||
|
||||
public class Planet extends UnlockableContent{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package mindustry.type;
|
||||
|
||||
import arc.math.geom.*;
|
||||
import arc.math3d.*;
|
||||
import arc.util.*;
|
||||
import arc.util.io.*;
|
||||
import mindustry.*;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,33 @@
|
|||
package mindustry.ui.dialogs;
|
||||
|
||||
import arc.*;
|
||||
import arc.graphics.*;
|
||||
import arc.graphics.g2d.*;
|
||||
import arc.graphics.g3d.*;
|
||||
import arc.input.*;
|
||||
import arc.math.*;
|
||||
import arc.math.geom.*;
|
||||
import arc.util.*;
|
||||
import mindustry.content.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.graphics.PlanetGrid.*;
|
||||
import mindustry.type.*;
|
||||
import mindustry.type.Sector.*;
|
||||
import mindustry.ui.*;
|
||||
|
||||
import static mindustry.Vars.ui;
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
public class PlanetDialog extends FloatingDialog{
|
||||
private PlanetRenderer renderer = new PlanetRenderer();
|
||||
private static final Color outlineColor = Pal.accent.cpy().a(0.6f);
|
||||
private static final float camLength = 4f, outlineRad = 1.15f;
|
||||
private static final boolean drawRect = false;
|
||||
|
||||
private final PlanetMesh[] outlines = new PlanetMesh[10];
|
||||
private final Camera3D cam = new Camera3D();
|
||||
private final VertexBatch3D batch = new VertexBatch3D(false, true, 0);
|
||||
|
||||
private float lastX, lastY;
|
||||
|
||||
public PlanetDialog(){
|
||||
super("", Styles.fullDialog);
|
||||
|
|
@ -16,6 +35,34 @@ public class PlanetDialog extends FloatingDialog{
|
|||
addCloseButton();
|
||||
buttons.addImageTextButton("$techtree", Icon.tree, () -> ui.tech.show()).size(230f, 64f);
|
||||
|
||||
Tmp.v1.trns(0, camLength);
|
||||
cam.position.set(Tmp.v1.x, 0f, Tmp.v1.y);
|
||||
|
||||
update(() -> {
|
||||
|
||||
Vec3 v = Tmp.v33.set(Core.input.mouseX(), Core.input.mouseY(), 0);
|
||||
|
||||
if(Core.input.keyDown(KeyCode.MOUSE_LEFT)){
|
||||
float upV = cam.position.angle(Vec3.Y);
|
||||
float xscale = 9f, yscale = 10f;
|
||||
float margin = 1;
|
||||
|
||||
//scale X speed depending on polar coordinate
|
||||
float speed = 1f - Math.abs(upV - 90) / 90f;
|
||||
|
||||
cam.position.rotate(cam.up, (v.x - lastX) / xscale * speed);
|
||||
|
||||
//prevent user from scrolling all the way up and glitching it out
|
||||
float amount = (v.y - lastY) / yscale;
|
||||
amount = Mathf.clamp(upV + amount, margin, 180f - margin) - upV;
|
||||
|
||||
cam.position.rotate(Tmp.v31.set(cam.up).rotate(cam.direction, 90), amount);
|
||||
}
|
||||
|
||||
lastX = v.x;
|
||||
lastY = v.y;
|
||||
});
|
||||
|
||||
shown(this::setup);
|
||||
}
|
||||
|
||||
|
|
@ -24,7 +71,94 @@ public class PlanetDialog extends FloatingDialog{
|
|||
titleTable.remove();
|
||||
|
||||
cont.addRect((x, y, w, h) -> {
|
||||
renderer.render(Planets.starter);
|
||||
render(Planets.starter);
|
||||
}).grow();
|
||||
}
|
||||
|
||||
private void render(Planet planet){
|
||||
Draw.flush();
|
||||
Gl.clear(Gl.depthBufferBit);
|
||||
Gl.enable(Gl.depthTest);
|
||||
|
||||
//lock to up vector so it doesn't get confusing
|
||||
cam.up.set(Vec3.Y);
|
||||
|
||||
cam.resize(Core.graphics.getWidth(), Core.graphics.getHeight());
|
||||
cam.update();
|
||||
cam.lookAt(0, 0, 0);
|
||||
cam.update();
|
||||
|
||||
PlanetMesh outline = outline(planet.size);
|
||||
|
||||
planet.mesh.render(cam.combined());
|
||||
outline.render(cam.combined());
|
||||
|
||||
Ptile tile = outline.getTile(cam.getPickRay(Core.input.mouseX(), Core.input.mouseY()));
|
||||
if(tile != null){
|
||||
float length = 0.1f;
|
||||
|
||||
Sector sector = planet.getSector(tile);
|
||||
for(int i = 0; i < sector.tile.corners.length; i++){
|
||||
Corner next = sector.tile.corners[(i + 1) % sector.tile.corners.length];
|
||||
Corner curr = sector.tile.corners[i];
|
||||
sector.tile.v.scl(outlineRad);
|
||||
Tmp.v31.set(curr.v).sub(sector.tile.v).setLength(length).add(sector.tile.v);
|
||||
Tmp.v32.set(next.v).sub(sector.tile.v).setLength(length).add(sector.tile.v);
|
||||
sector.tile.v.scl(1f / outlineRad);
|
||||
|
||||
batch.tri(curr.v, next.v, Tmp.v31, Pal.accent);
|
||||
batch.tri(Tmp.v31, Tmp.v32, next.v, Pal.accent);
|
||||
}
|
||||
batch.flush(cam.combined(), Gl.triangles);
|
||||
|
||||
if(drawRect){
|
||||
SectorRect rect = sector.rect;
|
||||
rect.center.scl(outlineRad);
|
||||
rect.right.scl(outlineRad);
|
||||
rect.top.scl(outlineRad);
|
||||
|
||||
batch.color(Color.red);
|
||||
batch.vertex(rect.center);
|
||||
batch.color(Color.red);
|
||||
batch.vertex(sector.tile.corners[0].v);
|
||||
|
||||
batch.color(Color.green);
|
||||
batch.vertex(rect.center);
|
||||
batch.color(Color.green);
|
||||
batch.vertex(rect.top.cpy().add(rect.center));
|
||||
batch.flush(cam.combined(), Gl.lines);
|
||||
|
||||
//Log.info((int)(sector.tile.corners[0].v.cpy().sub(rect.center).angle(rect.top)));
|
||||
|
||||
rect.center.scl(1f / outlineRad);
|
||||
rect.right.scl(1f / outlineRad);
|
||||
rect.top.scl(1f / outlineRad);
|
||||
}
|
||||
|
||||
if(Core.input.keyTap(KeyCode.SPACE)){
|
||||
control.playSector(sector);
|
||||
ui.planet.hide();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Gl.disable(Gl.depthTest);
|
||||
}
|
||||
|
||||
private PlanetMesh outline(int size){
|
||||
if(outlines[size] == null){
|
||||
outlines[size] = new PlanetMesh(size, new PlanetMesher(){
|
||||
@Override
|
||||
public float getHeight(Vec3 position){
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor(Vec3 position){
|
||||
return outlineColor;
|
||||
}
|
||||
}, outlineRad, true);
|
||||
}
|
||||
return outlines[size];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
org.gradle.daemon=true
|
||||
org.gradle.jvmargs=-Xms256m -Xmx1024m
|
||||
archash=d8ea4539e93211d2134b806e7f4997ce09a491e2
|
||||
archash=93908dfe0ce9665ebfecee3e5d7bc5717b970f1d
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue