Skip to content

Commit

Permalink
LOD kinda works + f3 text
Browse files Browse the repository at this point in the history
f3 text only shows when debug boxes are enabled and will show how many of the wakes are renderer
  • Loading branch information
Goby56 committed Apr 6, 2024
1 parent 8ddc38a commit a9ce2ee
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/goby56/wakes/mixin/DebugHudMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.goby56.wakes.mixin;

import com.goby56.wakes.WakesClient;
import com.goby56.wakes.render.WakeTextureRenderer;
import com.goby56.wakes.simulation.WakeHandler;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.minecraft.client.gui.hud.DebugHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin(DebugHud.class)
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/%d wake nodes", WakeTextureRenderer.nodesRendered, WakeHandler.getInstance().getTotal()));
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/goby56/wakes/render/DynamicWakeTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ private static int distToLOD(float distance) {
}

public void populatePixels(WakeNode node, float distance, int waterColor, float opacity) {
// TODO SAVE EACH NODE'S TEXTURE EACH TICK (HAVE THE TEXTURE READY FOR EACH FRAME)
// May decrease rendering time but will use up more memory
int lod = distToLOD(distance);
Texture texture = lods.get(lod);
int samples = Math.max(1, node.res / texture.res);
for (int i = 0; i < texture.res; i += samples) {
for (int j = 0; j < texture.res; j += samples) {
int samples = Math.max(1, WakeNode.res / texture.res);
for (int i = 0; i < texture.res; i++) {
for (int j = 0; j < texture.res; j++) {
float avg = 0;
for (int dy = 0; dy < samples; dy++) {
for (int dx = 0; dx < samples; dx++) {
avg = (node.u[0][i + dy + 1][j + dx + 1] + node.u[1][i + dy + 1][j + dx + 1] + node.u[2][i + dy + 1][j + dx + 1]) / 3;
}
}
int color = WakeColor.getColor(avg / (samples * samples), waterColor, opacity);
MemoryUtil.memPutInt(texture.imgPtr + (((i*(long)node.res)+j)*4), color);
MemoryUtil.memPutInt(texture.imgPtr + (((i*(long) texture.res)+j)*4), color);
}
}
this.currentTexture = texture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;

public class WakeTextureRenderer implements WorldRenderEvents.AfterTranslucent {
public static int nodesRendered = 0;

@Override
public void afterTranslucent(WorldRenderContext context) {
Expand Down Expand Up @@ -40,6 +41,7 @@ public void afterTranslucent(WorldRenderContext context) {

t1 = System.nanoTime();

int n = 0;
for (WakeNode node : nodes) {
if (node.isDead()) continue;
if (WakesClient.CONFIG_INSTANCE.wakeResolution.res != WakeNode.res) continue;
Expand All @@ -64,11 +66,15 @@ public void afterTranslucent(WorldRenderContext context) {
// TODO IMPLEMENT NODE TEXTURE RENDER CLUMPING (RENDER MULTIPLE NODES IN ONE QUAD/PASS)
wakeTexture.render(matrix, x, y, z, light);

n++;

renderingTime += System.nanoTime() - t3;
}

RenderSystem.enableCull();

WakeTextureRenderer.nodesRendered = n;

fullTime = System.nanoTime() - t1;
// if (!nodes.isEmpty() && !MinecraftClient.getInstance().isPaused()) {
// System.out.printf("Full time: %d, Memory time: %.2f, Rendering time: %.2f\n", fullTime, memoryTime / (float) fullTime, renderingTime / (float) fullTime);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/goby56/wakes/simulation/QuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ private void subdivide() {
this.SE = new QuadTree<>(x + w, z + w, w, depth + 1, this.ROOT);
}

public int count() {
int n = this.nodes.size();
if (this.NE == null) {
return n;
}
return n + this.NE.count() + this.NW.count() + this.SW.count() + this.SE.count();
}

public void prune() {
this.nodes.forEach(T::markDead);
this.nodes.clear();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/goby56/wakes/simulation/WakeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public ArrayList<WakeNode> getVisible(Frustum frustum) {
return foundNodes;
}

public int getTotal() {
// TODO SEEMS LIKE THERE ARE DUPLICATE NODES
int n = 0;
for (int y = 0; y < this.maxY - this.minY; y++) {
if (this.trees.get(y) != null) {
n += this.trees.get(y).count();
}
}
return n;
}

public ArrayList<WakeNode> getNearby(int x, int y, int z) {
ArrayList<WakeNode> foundNodes = new ArrayList<>();
int i = this.getArrayIndex(y);
Expand Down
17 changes: 9 additions & 8 deletions src/main/resources/wakes.mixins.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"required": true,
"package": "com.goby56.wakes.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"WakeSpawnerMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"package": "com.goby56.wakes.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"DebugHudMixin",
"WakeSpawnerMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit a9ce2ee

Please sign in to comment.