Skip to content

Commit

Permalink
brick indexing fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Jul 27, 2024
1 parent 66c7ead commit ec54e9b
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/goby56/wakes/event/WakeTicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.goby56.wakes.render.SplashPlaneRenderer;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.utils.WakesTimers;
import com.goby56.wakes.utils.WakesDebugInfo;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.world.ClientWorld;

public class WakeTicker implements ClientTickEvents.EndWorldTick {
@Override
public void onEndTick(ClientWorld world) {
WakesTimers.reset();
WakesDebugInfo.reset();
WakeHandler.getInstance().tick();
SplashPlaneRenderer.tick();
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/goby56/wakes/mixin/DebugHudMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.goby56.wakes.WakesClient;
import com.goby56.wakes.render.WakeRenderer;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.utils.WakesTimers;
import com.goby56.wakes.utils.WakesDebugInfo;
import net.minecraft.client.gui.hud.DebugHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -18,11 +18,11 @@ public class DebugHudMixin {
@Inject(at = @At("RETURN"), method = "getLeftText")
protected void getLeftText(CallbackInfoReturnable<List<String>> info) {
if (WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
info.getReturnValue().add(String.format("[Wakes] Rendering %d quads for %d wake nodes", WakeRenderer.quadsRendered, WakeHandler.getInstance().getTotal()));
info.getReturnValue().add(String.format("[Wakes] Node logic: %.2fms/t", 10e-6 * WakesTimers.nodeLogicTime));
info.getReturnValue().add(String.format("[Wakes] Mesh gen: %.2fms/t", 10e-6 * WakesTimers.meshGenerationTime));
info.getReturnValue().add(String.format("[Wakes] Texturing: %.2fms/t", 10e-6 * WakesTimers.texturingTime));
info.getReturnValue().add(String.format("[Wakes] Rendering: %.3fms/f", 10e-6 * WakesTimers.wakeRenderingTime.stream().reduce(0.0, Double::sum) / WakesTimers.wakeRenderingTime.size()));
info.getReturnValue().add(String.format("[Wakes] Rendering %d quads for %d wake nodes", WakesDebugInfo.quadsRendered, WakeHandler.getInstance().getTotal()));
info.getReturnValue().add(String.format("[Wakes] Node logic: %.2fms/t", 10e-6 * WakesDebugInfo.nodeLogicTime));
info.getReturnValue().add(String.format("[Wakes] Mesh gen: %.2fms/t", 10e-6 * WakesDebugInfo.meshGenerationTime));
info.getReturnValue().add(String.format("[Wakes] Texturing: %.2fms/t", 10e-6 * WakesDebugInfo.texturingTime));
info.getReturnValue().add(String.format("[Wakes] Rendering: %.3fms/f", 10e-6 * WakesDebugInfo.wakeRenderingTime.stream().reduce(0.0, Double::sum) / WakesDebugInfo.wakeRenderingTime.size()));
}
}
}
10 changes: 0 additions & 10 deletions src/main/java/com/goby56/wakes/render/BrickMesher.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/goby56/wakes/render/WakeQuad.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public WakeQuad(int x, int z, int w, int h, WakeNode[][] affectedNodes) {
}

public void populatePixels(WakeTexture texture, World world) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
WakeNode node = nodes[i][j];
long nodeOffset = (((i*(long) w)+j)*h);
long nodeOffset = (((i*(long) h)+j)*w);
int waterCol = BiomeColors.getWaterColor(world, node.blockPos());
float opacity = (float) ((-Math.pow(node.t, 2) + 1) * WakesClient.CONFIG_INSTANCE.wakeOpacity);
for (int r = 0; r < texture.res; r++) {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/goby56/wakes/render/WakeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import com.goby56.wakes.config.enums.Resolution;
import com.goby56.wakes.simulation.Brick;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.simulation.WakeNode;
import com.goby56.wakes.utils.WakesTimers;
import com.goby56.wakes.utils.WakesDebugInfo;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
Expand Down Expand Up @@ -44,20 +43,21 @@ public void afterTranslucent(WorldRenderContext context) {
RenderSystem.disableCull();
context.lightmapTextureManager().enable();

int n = 0;
for (var brick : bricks) {
Resolution resolution = WakesClient.CONFIG_INSTANCE.wakeResolution;
if (resolution.res != WakeNode.res) continue;
// int n = 0;
// for (var brick : bricks) {
// Resolution resolution = WakesClient.CONFIG_INSTANCE.wakeResolution;
// if (resolution.res != WakeNode.res) continue;

for (var quad : brick.quads) {
wakeTextures.get(resolution).render(matrix, context.camera(), brick, quad, wakeHandler.world);
n++;
}
}
RenderSystem.enableCull();
// for (var quad : brick.quads) {
// wakeTextures.get(resolution).render(matrix, context.camera(), brick, quad, wakeHandler.world);
// n++;
// }

// }
// RenderSystem.enableCull();

quadsRendered = n;
// quadsRendered = n;

WakesTimers.wakeRenderingTime.add(System.nanoTime() - tRendering);
WakesDebugInfo.wakeRenderingTime.add(System.nanoTime() - tRendering);
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/goby56/wakes/render/WakeTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ public void render(Matrix4f matrix, Camera camera, Brick brick, WakeQuad quad, W
int x = quad.x, z = quad.z, w = quad.w, h = quad.h;
WakeNode[][] nodes = quad.nodes;


Vec3d screenSpace = brick.getPos().add(camera.getPos().negate());
float xPos = (float) screenSpace.x + x;
float zPos = (float) screenSpace.z + z;

GlStateManager._bindTexture(glTexId);
GlStateManager._pixelStore(GlConst.GL_UNPACK_ROW_LENGTH, 0);
GlStateManager._pixelStore(GlConst.GL_UNPACK_SKIP_PIXELS, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.goby56.wakes.render.debug;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.utils.WakesTimers;
import com.goby56.wakes.simulation.WakeHandler;
import com.goby56.wakes.simulation.WakeNode;
import com.goby56.wakes.utils.WakesDebugInfo;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.render.debug.DebugRenderer;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;

import java.awt.*;
import java.util.Random;
Expand All @@ -17,18 +17,22 @@ public class WakeDebugRenderer implements WorldRenderEvents.DebugRender {
@Override
public void beforeDebugRender(WorldRenderContext context) {
WakeHandler wakeHandler = WakeHandler.getInstance();
int n = 0;
if (WakesClient.CONFIG_INSTANCE.drawDebugBoxes) {
for (var brick : wakeHandler.getVisible(context.frustum())) {
if (WakesClient.CONFIG_INSTANCE.wakeResolution.res != WakeNode.res) continue;
for (var quad : brick.quads) {
Vec3d brickPos = brick.getPos();
float y = quad.nodes[0][0].height;
Box box = new Box(quad.x, y, quad.z, quad.x + quad.w, y + 0.1f, quad.z + quad.h);
System.out.printf();
Box box = new Box(brickPos.x + quad.x, y, brickPos.z + quad.z, brickPos.x + quad.x + quad.w, y + 0.1f, brickPos.z + quad.z + quad.h);
var col = Color.getHSBColor(new Random(quad.hashCode()).nextFloat(), 1f, 1f).getRGBColorComponents(null);
DebugRenderer.drawBox(context.matrixStack(), context.consumers(),
box.offset(context.camera().getPos().negate()),
col[0], col[1], col[2], 0.5f);
n++;
}
}
}
WakesDebugInfo.quadsRendered = n;
}
}
45 changes: 23 additions & 22 deletions src/main/java/com/goby56/wakes/simulation/Brick.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import com.goby56.wakes.render.WakeQuad;
import com.goby56.wakes.render.WakeRenderer;
import com.goby56.wakes.render.WakeTexture;
import com.goby56.wakes.utils.WakesTimers;
import com.goby56.wakes.utils.WakesDebugInfo;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.Objects;

public class Brick {
public int[] bitMask = new int[32];
Expand All @@ -34,8 +33,7 @@ public Brick(int x, int z) {
this.quads = new ArrayList<>();
}

private void generateMesh() {
quads.clear();
public void generateMesh() {
var ints = bitMask;
for (int i = 0; i < dim; i++) {
int j = 0;
Expand All @@ -57,45 +55,48 @@ private void generateMesh() {
ints[i + w] &= ~mask;
w++;
}
quads.add(new WakeQuad(i, j, w, h, getFromArea(i, j, w, h)));
quads.add(new WakeQuad(i, dim - j - h, w, h, getFromArea(i, dim - j - h, w, h)));
j += h;
}
}
}

private WakeNode[][] getFromArea(int x, int z, int w, int h) {
WakeNode[][] nodes = new WakeNode[w][h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
nodes[i][j] = this.get(x + i, z + j);
WakeNode[][] nodes = new WakeNode[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
WakeNode node = this.get(x + j, z + i);
assert node != null;
nodes[i][j] = node;
}
}
return nodes;
}

public boolean tick(World world) {
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (this.get(i, j) == null) continue;
quads.clear();
for (int z = 0; z < dim; z++) {
for (int x = 0; x < dim; x++) {
if (this.get(x, z) == null) continue;

long tNode = System.nanoTime();
if (!this.get(i, j).tick()) {
this.clear(i, j);
if (!this.get(x, z).tick()) {
this.clear(x, z);
}
WakesTimers.nodeLogicTime += (System.nanoTime() - tNode);
WakesDebugInfo.nodeLogicTime += (System.nanoTime() - tNode);
}
}
if (occupied != 0) {
long tMesh = System.nanoTime();
generateMesh();
WakesTimers.meshGenerationTime += (System.nanoTime() - tMesh);
WakesDebugInfo.meshGenerationTime += (System.nanoTime() - tMesh);

long tTexture = System.nanoTime();
for (var quad : quads) {
WakeTexture tex = WakeRenderer.wakeTextures.get(WakesClient.CONFIG_INSTANCE.wakeResolution);
quad.populatePixels(tex, world);
}
WakesTimers.texturingTime += (System.nanoTime() - tTexture);
WakesDebugInfo.texturingTime += (System.nanoTime() - tTexture);


}
Expand All @@ -107,21 +108,21 @@ public Vec3d getPos() {
}

public WakeNode get(int x, int z) {
return nodes[x][z];
return nodes[z][x];
}

public void insert(WakeNode node) {
this.set(Math.floorMod(node.x(), dim), Math.floorMod(node.z(), dim), node);
}

protected void set(int x, int z, WakeNode node) {
boolean wasNull = nodes[x][z] == null;
nodes[x][z] = node;
boolean wasNull = nodes[z][x] == null;
nodes[z][x] = node;
if (node == null) {
bitMask[x] &= ~(1 << z);
bitMask[x] &= ~(1 << (dim - z - 1));
if (!wasNull) this.occupied--;
} else {
bitMask[x] |= (1 << z);
bitMask[x] |= (1 << (dim - z - 1));
if (wasNull) this.occupied++;
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/com/goby56/wakes/simulation/WakeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.*;

public class WakeNode implements Position<WakeNode>, Age<WakeNode> {
private final WakeHandler wakeHandler = WakeHandler.getInstance();
// TODO MAKE SURE THIS WONT EVER BE NULL

public static int res = WakesClient.CONFIG_INSTANCE.wakeResolution.res;
Expand All @@ -41,6 +40,11 @@ public class WakeNode implements Position<WakeNode>, Age<WakeNode> {
public float t = 0;
public int floodLevel;

// public WakeNode(int x, int z) {
// this.x = x;
// this.z = z;
// }

//TODO MORE GENERALIZED CONSTRUCTOR
public WakeNode(Vec3d position, int initialStrength) {
this.initValues();
Expand Down Expand Up @@ -142,28 +146,29 @@ public boolean tick() {
}

public void floodFill() {
if (this.floodLevel > 0 && this.age > WakesClient.CONFIG_INSTANCE.ticksBeforeFill) {
WakeHandler wh = WakeHandler.getInstance();
if (floodLevel > 0 && this.age > WakesClient.CONFIG_INSTANCE.ticksBeforeFill) {
if (this.NORTH == null) {
wakeHandler.insert(new WakeNode(this.x, this.z - 1, this.height, this.floodLevel - 1));
wh.insert(new WakeNode(this.x, this.z - 1, this.height, floodLevel - 1));
} else {
this.NORTH.updateFloodLevel(this.floodLevel - 1);
this.NORTH.updateFloodLevel(floodLevel - 1);
}
if (this.EAST == null) {
wakeHandler.insert(new WakeNode(this.x + 1, this.z, this.height, this.floodLevel - 1));
wh.insert(new WakeNode(this.x + 1, this.z, this.height, floodLevel - 1));
} else {
this.EAST.updateFloodLevel(this.floodLevel - 1);
this.EAST.updateFloodLevel(floodLevel - 1);
}
if (this.SOUTH == null) {
wakeHandler.insert(new WakeNode(this.x, this.z + 1, this.height, this.floodLevel - 1));
wh.insert(new WakeNode(this.x, this.z + 1, this.height, floodLevel - 1));
} else {
this.SOUTH.updateFloodLevel(this.floodLevel - 1);
this.SOUTH.updateFloodLevel(floodLevel - 1);
}
if (this.WEST == null) {
wakeHandler.insert(new WakeNode(this.x - 1, this.z, this.height, this.floodLevel - 1));
wh.insert(new WakeNode(this.x - 1, this.z, this.height, floodLevel - 1));
} else {
this.WEST.updateFloodLevel(this.floodLevel - 1);
this.WEST.updateFloodLevel(floodLevel - 1);
}
this.floodLevel = 0;
floodLevel = 0;
// TODO IF BLOCK IS BROKEN (AND WATER APPEARS IN ITS STEAD) RETRY FLOOD FILL
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/goby56/wakes/tests/BrickIndexingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.goby56.wakes.tests;

import com.goby56.wakes.render.WakeQuad;
import com.goby56.wakes.simulation.Brick;
import com.goby56.wakes.simulation.WakeNode;
import net.minecraft.util.math.Vec3d;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class BrickIndexingTest {
// @Test
// void brickBitMaskCreation() {
// Brick brick = new Brick(0, 0);
// brick.insert(new WakeNode(0, 2));
// brick.insert(new WakeNode(0, 3));
// brick.insert(new WakeNode(1, 2));
// brick.insert(new WakeNode(1, 3));

// assertEquals(805306368, brick.bitMask[0]);
// assertEquals(805306368, brick.bitMask[1]);
// }

// @Test
// void nodeFromQuadRetrieval() {
// Brick brick = new Brick(0, 0);
// brick.insert(new WakeNode(0, 2));
// brick.insert(new WakeNode(0, 3));
// brick.insert(new WakeNode(1, 2));
// brick.insert(new WakeNode(1, 3));

// brick.generateMesh();
// assertEquals(1, brick.quads.size());
// WakeQuad quad = brick.quads.get(0);
// assertEquals(0, quad.x);
// assertEquals(2, quad.z);
// assertEquals(2, quad.w);
// assertEquals(2, quad.h);

// for (int i = 0; i < quad.h; i++) {
// for (int j = 0; j < quad.w; j++) {
// WakeNode node = quad.nodes[i][j];
// assertEquals(quad.x + j, node.x);
// assertEquals(quad.z + i, node.z);
// }
// }
// }
}
Loading

0 comments on commit ec54e9b

Please sign in to comment.