Improved planet generation

This commit is contained in:
Anuken 2020-01-12 14:11:47 -05:00
parent 18ce4b6bde
commit e6f1d194ae
3 changed files with 51 additions and 28 deletions

View File

@ -3,9 +3,11 @@ package mindustry.graphics;
import arc.graphics.*;
import arc.graphics.VertexAttributes.*;
import arc.graphics.gl.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.ArcAnnotate.*;
import arc.util.*;
import arc.util.noise.*;
import mindustry.graphics.Pgrid.*;
public class PlanetMesh{
@ -16,10 +18,13 @@ public class PlanetMesh{
private float color;
private boolean lines;
private float length;
private float radius;
public PlanetMesh(int divisions, float length, boolean lines, Color color){
this.length = length;
private Simplex sim = new Simplex();
private Color[] colors = {Color.royal, Color.royal, Color.royal, Color.tan, Color.valueOf("3f9a50"), Color.valueOf("3f9a50"), Color.gray, Color.white, Color.white};
public PlanetMesh(int divisions, float radius, boolean lines, Color color){
this.radius = radius;
this.lines = lines;
this.color = color.toFloatBits();
this.grid = Pgrid.newGrid(divisions);
@ -38,24 +43,21 @@ public class PlanetMesh{
}
public void render(Mat3D mat){
Gl.enable(Gl.depthTest);
Shaders.planet.begin();
Shaders.planet.setUniformMatrix4("u_projModelView", mat.val);
mesh.render(Shaders.planet, lines ? Gl.lines : Gl.triangles);
Shaders.planet.end();
Gl.disable(Gl.depthTest);
}
public @Nullable Ptile getTile(Ray ray){
Vec3 vec = intersect(ray);
if(vec == null) return null;
//TODO fix O(N) search
return Structs.findMin(grid.tiles, t -> t.v.dst(vec));
}
public @Nullable Vec3 intersect(Ray ray){
if(Intersector3D.intersectRaySphere(ray, center, length, Tmp.v33)){
if(Intersector3D.intersectRaySphere(ray, center, radius, Tmp.v33)){
return Tmp.v33;
}
return null;
@ -69,11 +71,13 @@ public class PlanetMesh{
Corner[] c = tile.corners;
for(Corner corner : c){
corner.v.setLength(length);
corner.v.setLength(radius);
nor.add(corner.v);
}
nor.nor();
Color color = color(nor);
if(lines){
nor.set(1f, 1f, 1f);
@ -81,30 +85,35 @@ public class PlanetMesh{
Vec3 v1 = c[i].v;
Vec3 v2 = c[(i + 1) % c.length].v;
vert(v1, nor);
vert(v2, nor);
vert(v1, nor, color);
vert(v2, nor, color);
}
}else{
verts(c[0].v, c[1].v, c[2].v, nor);
verts(c[0].v, c[2].v, c[3].v, nor);
verts(c[0].v, c[3].v, c[4].v, nor);
verts(c[0].v, c[1].v, c[2].v, nor, color);
verts(c[0].v, c[2].v, c[3].v, nor, color);
verts(c[0].v, c[3].v, c[4].v, nor, color);
if(c.length > 5){
verts(c[0].v, c[4].v, c[5].v, nor);
verts(c[0].v, c[4].v, c[5].v, nor, color);
}else{
verts(c[0].v, c[3].v, c[4].v, nor);
verts(c[0].v, c[3].v, c[4].v, nor, color);
}
}
}
}
private void verts(Vec3 a, Vec3 b, Vec3 c, Vec3 normal){
vert(a, normal);
vert(b, normal);
vert(c, normal);
private Color color(Vec3 v){
float f = ((float)sim.octaveNoise3D(6, 0.6, 1 / 2.0, v.x, v.y, v.z));
return colors[Mathf.clamp((int)(f * colors.length), 0, colors.length - 1)].cpy().mul(Mathf.round(Mathf.lerp(f*2f, 2f, 0.2f), 0.2f)).a(1f);
}
private void vert(Vec3 a, Vec3 normal){
private void verts(Vec3 a, Vec3 b, Vec3 c, Vec3 normal, Color color){
vert(a, normal, color);
vert(b, normal, color);
vert(c, normal, color);
}
private void vert(Vec3 a, Vec3 normal, Color color){
floats[0] = a.x;
floats[1] = a.y;
floats[2] = a.z;
@ -113,7 +122,7 @@ public class PlanetMesh{
floats[4] = normal.y;
floats[5] = normal.z;
floats[6] = color;
floats[6] = color.toFloatBits();
mesh.getVerticesBuffer().put(floats);
}
}

View File

@ -7,6 +7,7 @@ import arc.graphics.g3d.*;
import arc.input.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.graphics.Pgrid.*;
public class PlanetRenderer{
private Camera3D cam = new Camera3D();
@ -14,6 +15,7 @@ public class PlanetRenderer{
private PlanetMesh planet = new PlanetMesh(3, 1f, false, Color.royal);
private PlanetMesh outline = new PlanetMesh(3, 1.01f, true, Pal.accent);
private VertexBatch3D batch = new VertexBatch3D(false, true, 0);
public PlanetRenderer(){
Tmp.v1.trns(0, 2.5f);
@ -24,8 +26,7 @@ public class PlanetRenderer{
Draw.flush();
Gl.clearColor(0, 0, 0, 1);
Gl.clear(Gl.depthBufferBit | Gl.colorBufferBit);
Ray ray = cam.getPickRay(Core.input.mouseX(), Core.input.mouseY());
Gl.enable(Gl.depthTest);
input();
@ -35,14 +36,27 @@ public class PlanetRenderer{
cam.update();
planet.render(cam.combined());
outline.render(cam.combined());
//outline.render(cam.combined());
//Log.info(cam.position + " " + cam.getPickRay(Core.input.mouseX(), Core.input.mouseY()));
Ptile tile = outline.getTile(cam.getPickRay(Core.input.mouseX(), Core.input.mouseY()));
if(tile != null){
for(int i = 0; i < tile.corners.length + 1; i++){
batch.color(Pal.accent);
batch.vertex(tile.corners[i % tile.corners.length].v);
}
batch.flush(cam.combined(), Gl.lineStrip);
}
Gl.disable(Gl.depthTest);
}
void input(){
Vec3 v = cam.unproject(Tmp.v33.set(Core.input.mouseX(), Core.input.mouseY(), 0f));
Vec3 v = Tmp.v33.set(Core.input.mouseX(), Core.input.mouseY(), 0);
if(Core.input.keyDown(KeyCode.MOUSE_LEFT)){
cam.position.rotate(Vec3.Y, (v.x - lastX) * 100);
cam.position.rotate(Vec3.Y, (v.x - lastX) / 10);
}
lastX = v.x;
lastY = v.y;

View File

@ -1,3 +1,3 @@
org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx1024m
archash=145648d83c4659f727ea8b6676006747b87e7716
archash=097f07c939d99020b24a9b33fc3e24bf800e210d