Skip to content

Commit

Permalink
Extract 3D camera and world darkness to a common module
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Mar 30, 2024
1 parent 12447f1 commit 916e85e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 81 deletions.
2 changes: 1 addition & 1 deletion assets/shaders/confictura/emissive-batch.vert
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main(){
vec3 normal = normalize(u_normal * a_normal);
vec3 lightDir = normalize(u_light - pos.xyz);

vec3 diffuse = u_ambientColor * vec3(0.01 + clamp((dot(normal, lightDir) + 1.0) / 2.0, 0.0, 1.0));
vec3 diffuse = u_ambientColor * ((dot(normal, lightDir) + 1.0) / 2.0);
v_color = a_color * vec4(diffuse, 1.0);
v_texCoord = a_texCoord0;

Expand Down
3 changes: 3 additions & 0 deletions src/confictura/ConficturaMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ConficturaMod extends Mod{

public static Cinematic cinematic;

public static RenderContext renderContext;
public static ModelPropDrawer modelPropDrawer;

public static CinematicEditor cinematicEditor;
Expand Down Expand Up @@ -170,6 +171,8 @@ public String getName(){
CModels.load();
app.post(() -> {
CShaders.load();

renderContext = new RenderContext();
modelPropDrawer = new ModelPropDrawer(CShaders.modelProp, 8192, 16384);
});
}
Expand Down
65 changes: 65 additions & 0 deletions src/confictura/graphics/RenderContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package confictura.graphics;

import arc.*;
import arc.graphics.g3d.*;
import arc.math.*;
import mindustry.game.EventType.*;

import static arc.Core.*;
import static mindustry.Vars.*;

public class RenderContext{
public Camera3D camera = new Camera3D(){{
fov = 60f;
near = 1f;

up.set(0f, 0f, -1f);
direction.set(0f, -1f, 0f);
}};

protected float[] darkness;

public RenderContext(){
Events.run(Trigger.draw, () -> {
int sw = graphics.getWidth(), sh = graphics.getHeight();

camera.resize(sw, sh);
camera.position.set(Core.camera.position.x, Core.camera.height / 2f / Mathf.tan(camera.fov / 2f * Mathf.degRad, 1f, 1f), -Core.camera.position.y);
camera.far = Math.max(150f, camera.position.y * 1.5f);
camera.update();
});

Events.on(WorldLoadEvent.class, e -> {
darkness = new float[world.width() * world.height()];
world.tiles.each(this::updateDarkness);
});

Events.on(TileChangeEvent.class, e -> updateDarkness(e.tile.x, e.tile.y));
}

protected void updateDarkness(int x, int y){
float dark = world.getDarkness(x, y);
if(dark > 0f){
darkness[world.packArray(x, y)] = 1f - Math.min((dark + 0.5f) / 4f, 1f);
}else{
darkness[world.packArray(x, y)] = 1f;
}
}

public float darkness(float x, float y){
x /= tilesize;
y /= tilesize;

int x1 = (int)x, x2 = x1 + 1,
y1 = (int)y, y2 = y1 + 1;

float out = state.rules.borderDarkness ? 0f : 1f;
var t = world.tiles;

return Mathf.lerp(
Mathf.lerp(t.in(x1, y1) ? darkness[world.packArray(x1, y1)] : out, t.in(x2, y1) ? darkness[world.packArray(x2, y1)] : out, x % 1f),
Mathf.lerp(t.in(x1, y2) ? darkness[world.packArray(x1, y2)] : out, t.in(x2, y2) ? darkness[world.packArray(x2, y2)] : out, x % 1f),
y % 1f
);
}
}
10 changes: 6 additions & 4 deletions src/confictura/graphics/g3d/Draw3DUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @author GlennFolker
*/
public final class Draw3DUtils{
private static final Vec3 a = new Vec3(), b = new Vec3(), c = new Vec3(), d = new Vec3();

private Draw3DUtils(){
throw new AssertionError();
}
Expand Down Expand Up @@ -68,10 +70,10 @@ public static void quad2(

quad(
batch,
p4, n4, c4, u4, v4,
p3, n3, c3, u3, v3,
p2, n2, c2, u2, v2,
p1, n1, c1, u1, v1
p4, a.set(n4).scl(-1f), c4, u4, v4,
p3, b.set(n3).scl(-1f), c3, u3, v3,
p2, c.set(n2).scl(-1f), c2, u2, v2,
p1, d.set(n1).scl(-1f), c1, u1, v1
);
}
}
62 changes: 5 additions & 57 deletions src/confictura/graphics/g3d/ModelPropDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
import arc.func.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.graphics.g3d.*;
import arc.graphics.gl.*;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import arc.util.pooling.*;
import confictura.graphics.shaders.*;
import mindustry.core.*;
import mindustry.game.EventType.*;
import mindustry.graphics.*;

import static arc.Core.*;
import static confictura.ConficturaMod.*;
import static confictura.util.StructUtils.*;
import static mindustry.Vars.*;

Expand All @@ -40,24 +38,15 @@ public class ModelPropDrawer implements Disposable{
private static final Color col = new Color();

protected ObjectMap<Mesh, PropData> data = new ObjectMap<>();
protected Camera3D cam = new Camera3D(){{
fov = 60f;
near = 1f;

up.set(0f, 0f, -1f);
direction.set(0f, -1f, 0f);
}};
protected FrameBuffer buffer = new FrameBuffer(2, 2, true);
protected Seq<Req> requests = new Seq<>();

protected Mesh batch;
protected ModelPropShader shader;
protected final Mesh batch;
protected final ModelPropShader shader;
protected float[] vertices;
protected short[] indices;
protected int vertexOffset, indexOffset;

protected float[] darkness;

public ModelPropDrawer(ModelPropShader shader, int maxVertices, int maxIndices){
batch = new Mesh(false, maxVertices, maxIndices,
VertexAttribute.position3, VertexAttribute.normal, VertexAttribute.color, new VertexAttribute(1, Gl.floatV, false, "a_darkness")
Expand All @@ -68,42 +57,6 @@ VertexAttribute.position3, VertexAttribute.normal, VertexAttribute.color, new Ve
indices = new short[maxIndices];

Events.run(Trigger.drawOver, () -> Draw.draw(flushLayer, this::render));

Events.on(WorldLoadEvent.class, e -> {
darkness = new float[world.width() * world.height()];
world.tiles.each(this::updateDarkness);
});

Events.on(TileChangeEvent.class, e -> updateDarkness(e.tile.x, e.tile.y));
}

protected void updateDarkness(int x, int y){
float dark = world.getDarkness(x, y);
if(dark > 0f){
darkness[world.packArray(x, y)] = 1f - Math.min((dark + 0.5f) / 4f, 1f);
}else{
darkness[world.packArray(x, y)] = 1f;
}
}

protected float darkness(float x, float y){
x /= tilesize;
y /= tilesize;

int x1 = (int)x, x2 = x1 + 1,
y1 = (int)y, y2 = y1 + 1;

float out = state.rules.borderDarkness ? 0f : 1f;
var t = world.tiles;

return Mathf.lerp(
Mathf.lerp(t.in(x1, y1) ? darkness[world.packArray(x1, y1)] : out, t.in(x2, y1) ? darkness[world.packArray(x2, y1)] : out, x % 1f),
Mathf.lerp(t.in(x1, y2) ? darkness[world.packArray(x1, y2)] : out, t.in(x2, y2) ? darkness[world.packArray(x2, y2)] : out, x % 1f),
y % 1f
);

//int tx = World.toTile(x), ty = World.toTile(y);
//return world.tiles.in(tx, ty) ? darkness[world.packArray(tx, ty)] : state.rules.borderDarkness ? 0f : 1f;
}

public PropData getData(Mesh mesh){
Expand Down Expand Up @@ -132,11 +85,6 @@ public void render(){
if(requests.isEmpty()) return;
int sw = graphics.getWidth(), sh = graphics.getHeight();

cam.resize(sw, sh);
cam.position.set(camera.position.x, camera.height / 2f / Mathf.tan(cam.fov / 2f * Mathf.degRad, 1f, 1f), -camera.position.y);
cam.far = Math.max(150f, cam.position.y * 1.5f);
cam.update();

buffer.resize(sw, sh);
buffer.begin();

Expand Down Expand Up @@ -173,7 +121,7 @@ public void render(){
vertices[dst + 5] = n.z;

vertices[dst + 6] = color.toFloatBits();
vertices[dst + 7] = darkness(vertices[dst], -vertices[dst + 2]);
vertices[dst + 7] = renderContext.darkness(vertices[dst], -vertices[dst + 2]);
}

for(short index : data.indices) indices[indexOffset++] = (short)(vertexOffset + index);
Expand All @@ -197,7 +145,7 @@ protected void flush(){
if(indexOffset == 0) return;

shader.bind();
shader.camera = cam;
shader.camera = renderContext.camera;
shader.lightDir.set(-1.2f, -0.8f, 0.9f).nor();
shader.reflectColor.set(!state.rules.lighting ? Color.white : state.rules.ambientLight);
shader.apply();
Expand Down
2 changes: 1 addition & 1 deletion src/confictura/world/celestial/EmissiveObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void draw(PlanetParams params, Mat3D projection, Mat3D transform){
}

@Override
public void drawAtmosphere(Mesh atmosphere, Camera3D cam){
public final void drawAtmosphere(Mesh atmosphere, Camera3D cam){
Gl.depthMask(false);
Blending.additive.apply();

Expand Down
21 changes: 4 additions & 17 deletions src/confictura/world/celestial/Portal.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Portal(String name, Planet parent, float radius){
forcefieldRadius = radius;
hasAtmosphere = true;

emissions = new Color[]{monolithDark, monolithDarker};
emissions = new Color[]{monolithMid, monolithDark};
atmosphereColor.set(0x3366e5ff);
atmosphereOutlineColor.set(0x1966ffff);

Expand Down Expand Up @@ -107,20 +107,7 @@ protected SectorRect makeRect(){
sectorApproxRadius = sectors.first().tile.v.dst(sectors.first().tile.corners[0].v);
gridMeshLoader = () -> CMeshBuilder.gridLines(grid, sectorColor);

generator = new PlanetGenerator(){
@Override
public void generateSector(Sector sector){}

@Override
public float getHeight(Vec3 position){
return 0f;
}

@Override
public Color getColor(Vec3 position){
return null;
}
};
generator = new BlankPlanetGenerator();
}

@Override
Expand Down Expand Up @@ -365,13 +352,13 @@ public void drawSelection(VertexBatch3D batch, Sector sector, Color color, float
v2.set(next.v);
v3.set(curr.v).sub(tile.v);
v3.setLength(v3.len() - stroke).add(tile.v);
batch.tri(v1, v2, v3, c1);
batch.tri2(v1, v2, v3, c1);

v1.set(v3);
v2.set(next.v);
v3.set(next.v).sub(tile.v);
v3.setLength(v3.len() - stroke).add(tile.v);
batch.tri(v1, v2, v3, c1);
batch.tri2(v1, v2, v3, c1);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/confictura/world/celestial/Satellite.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import gltfrenzy.model.*;
import mindustry.game.EventType.*;
import mindustry.graphics.g3d.*;
import mindustry.graphics.g3d.PlanetGrid.*;
import mindustry.maps.generators.*;
import mindustry.type.*;
import mindustry.world.meta.*;

Expand All @@ -37,8 +39,11 @@ public Satellite(String name, Planet observed, float distance){
defaultEnv = Env.space;
meshLoader = SatelliteMesh::new;
tidalLock = true;
camRadius = 1.3f;

emissions = new Color[]{monolithDark, monolithDarker};
sectors.add(new Sector(this, Ptile.empty));
generator = new BlankPlanetGenerator();

// Deliberately make it as the first child to get as close as possible.
// This *might* be invasive for mods that are sensitive towards their planet children indices (which I can
Expand Down Expand Up @@ -116,7 +121,7 @@ public void drawEmissive(){
Tmp.v33.set(Tmp.v32).y -= 1f;
Tmp.v34.set(Tmp.v31).y -= 1f;

normal(nor, Tmp.v31, Tmp.v32, Tmp.v33);
normal(nor, Tmp.v33, Tmp.v32, Tmp.v31);
Tmp.c1.a /= 2f;
Tmp.c2.set(Tmp.c1).a(0f);

Expand Down

0 comments on commit 916e85e

Please sign in to comment.