Skip to content

Commit

Permalink
split wind and wind disturbance renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Aug 29, 2024
1 parent c610c23 commit 734d5dc
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.frozenblock.lib.debug.client.renderer.ImprovedGameEventListenerRenderer;
import net.frozenblock.lib.debug.client.renderer.ImprovedGoalSelectorDebugRenderer;
import net.frozenblock.lib.debug.client.renderer.WindDebugRenderer;
import net.frozenblock.lib.debug.client.renderer.WindDisturbanceDebugRenderer;
import net.frozenblock.lib.debug.networking.GoalDebugRemovePayload;
import net.frozenblock.lib.debug.networking.ImprovedGameEventDebugPayload;
import net.frozenblock.lib.debug.networking.ImprovedGameEventListenerDebugPayload;
Expand Down Expand Up @@ -127,6 +128,20 @@ public static void init() {

registerRenderer(FrozenSharedConstants.id("wind"), windDebugRenderer::render);
});

DebugRendererEvents.DEBUG_RENDERERS_CREATED.register(client -> {
WindDisturbanceDebugRenderer windDisturbanceDebugRenderer = new WindDisturbanceDebugRenderer(client);

ClientTickEvents.START_WORLD_TICK.register(clientLevel -> {
if (FrozenLibConfig.IS_DEBUG) {
windDisturbanceDebugRenderer.tick();
}
});

addClearRunnable(windDisturbanceDebugRenderer::clear);

registerRenderer(FrozenSharedConstants.id("wind_disturbance"), windDisturbanceDebugRenderer::render);
});
}

public static void clearAdditionalRenderers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@
import net.fabricmc.api.Environment;
import net.frozenblock.lib.core.client.api.FrustumUtil;
import net.frozenblock.lib.wind.api.ClientWindManager;
import net.frozenblock.lib.wind.api.WindDisturbance;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.debug.DebugRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.util.FastColor;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.Shapes;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class WindDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
private final Minecraft minecraft;
private List<WindDisturbance<?>> windDisturbances = Collections.emptyList();
private List<Vec3> accessedWindPositions = Collections.emptyList();
private List<List<Pair<Vec3, Integer>>> windNodes = Collections.emptyList();

Expand All @@ -54,9 +49,6 @@ public WindDebugRenderer(Minecraft client) {
}

public void tick() {
this.windDisturbances = ImmutableList.copyOf(
ClientWindManager.getWindDisturbances()
);
this.accessedWindPositions = ClientWindManager.getAccessedPositions();
this.windNodes = ImmutableList.copyOf(
this.createAllWindNodes()
Expand All @@ -65,74 +57,33 @@ public void tick() {

@Override
public void clear() {
this.windDisturbances = Collections.emptyList();
this.accessedWindPositions = Collections.emptyList();
this.windNodes = Collections.emptyList();
}

@Override
public void render(PoseStack matrices, MultiBufferSource vertexConsumers, double cameraX, double cameraY, double cameraZ) {
this.windDisturbances.forEach(
windDisturbance -> {
LevelRenderer.renderVoxelShape(
matrices,
vertexConsumers.getBuffer(RenderType.lines()),
Shapes.create(windDisturbance.affectedArea),
-cameraX,
-cameraY,
-cameraZ,
0.5F,
1F,
0.5F,
0.35F,
true
);
renderFilledBox(
matrices,
vertexConsumers,
AABB.ofSize(windDisturbance.origin, 0.2D, 0.2D, 0.2D),
cameraX, cameraY, cameraZ
);
}
);

renderWindNodesFromList(matrices, vertexConsumers, cameraX, cameraY, cameraZ, this.windNodes);
}

private @NotNull List<List<Pair<Vec3, Integer>>> createAllWindNodes() {
List<List<Pair<Vec3, Integer>>> windNodes = new ArrayList<>();
this.windDisturbances.forEach(
windDisturbance -> {
if (FrustumUtil.isVisible(windDisturbance.affectedArea)) {
BlockPos.betweenClosed(
BlockPos.containing(windDisturbance.affectedArea.getMinPosition()),
BlockPos.containing(windDisturbance.affectedArea.getMaxPosition())
).forEach(
blockPos -> {
Vec3 blockPosCenter = Vec3.atCenterOf(blockPos);
windNodes.add(createWindNodes(blockPosCenter, 1D, true));
}
);
}
}
);

this.accessedWindPositions.forEach(
vec3 -> {
if (FrustumUtil.isVisible(vec3, 0.5D)) {
windNodes.add(createWindNodes(vec3, 1.5D, false));
windNodes.add(createWindNodes(this.minecraft.level, vec3, 1.5D, false));
}
}
);

return windNodes;
}

private @NotNull List<Pair<Vec3, Integer>> createWindNodes(Vec3 origin, double stretch, boolean disturbanceOnly) {
protected static @NotNull List<Pair<Vec3, Integer>> createWindNodes(Level level, Vec3 origin, double stretch, boolean disturbanceOnly) {
List<Pair<Vec3, Integer>> windNodes = new ArrayList<>();
Vec3 wind = disturbanceOnly ?
ClientWindManager.getRawDisturbanceMovement(this.minecraft.level, origin)
: ClientWindManager.getWindMovement(this.minecraft.level, origin);
ClientWindManager.getRawDisturbanceMovement(level, origin)
: ClientWindManager.getWindMovement(level, origin);
double windlength = wind.length();
if (windlength != 0D) {
int increments = 3;
Expand All @@ -155,15 +106,15 @@ public void render(PoseStack matrices, MultiBufferSource vertexConsumers, double
);
lineStart = lineEnd;
wind = disturbanceOnly ?
ClientWindManager.getRawDisturbanceMovement(this.minecraft.level, lineStart)
: ClientWindManager.getWindMovement(this.minecraft.level, lineStart);
ClientWindManager.getRawDisturbanceMovement(level, lineStart)
: ClientWindManager.getWindMovement(level, lineStart);
}
}

return windNodes;
}

private int calculateNodeColor(double strength, boolean disturbanceOnly) {
protected static int calculateNodeColor(double strength, boolean disturbanceOnly) {
return FastColor.ARGB32.color(
255,
(int) Mth.lerp(strength, 255, 0),
Expand All @@ -172,7 +123,7 @@ private int calculateNodeColor(double strength, boolean disturbanceOnly) {
);
}

private static void renderWindNodesFromList(
protected static void renderWindNodesFromList(
PoseStack matrices,
MultiBufferSource vertexConsumers,
double cameraX,
Expand All @@ -183,7 +134,7 @@ private static void renderWindNodesFromList(
windNodes.forEach(nodes -> renderWindNodes(matrices, vertexConsumers, cameraX, cameraY, cameraZ, nodes));
}

private static void renderWindNodes(
protected static void renderWindNodes(
PoseStack matrices,
MultiBufferSource vertexConsumers,
double cameraX,
Expand All @@ -210,16 +161,6 @@ private static void renderWindNodes(
}
}

private static void renderFilledBox(
PoseStack matrices,
MultiBufferSource vertexConsumers,
@NotNull AABB box,
double cameraX, double cameraY, double cameraZ
) {
Vec3 vec3 = new Vec3(-cameraX, -cameraY, -cameraZ);
DebugRenderer.renderFilledBox(matrices, vertexConsumers, box.move(vec3), 1F, 1F, 1F, 1F);
}

private static void drawLine(
@NotNull PoseStack matrices,
@NotNull MultiBufferSource vertexConsumers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (C) 2024 FrozenBlock
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.frozenblock.lib.debug.client.renderer;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.datafixers.util.Pair;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.frozenblock.lib.core.client.api.FrustumUtil;
import net.frozenblock.lib.wind.api.ClientWindManager;
import net.frozenblock.lib.wind.api.WindDisturbance;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.debug.DebugRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.util.FastColor;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.Shapes;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class WindDisturbanceDebugRenderer implements DebugRenderer.SimpleDebugRenderer {
private final Minecraft minecraft;
private List<WindDisturbance<?>> windDisturbances = Collections.emptyList();
private List<List<Pair<Vec3, Integer>>> windNodes = Collections.emptyList();

public WindDisturbanceDebugRenderer(Minecraft client) {
this.minecraft = client;
}

public void tick() {
this.windDisturbances = ImmutableList.copyOf(
ClientWindManager.getWindDisturbances()
);
this.windNodes = ImmutableList.copyOf(
this.createAllWindNodes()
);
}

@Override
public void clear() {
this.windDisturbances = Collections.emptyList();
this.windNodes = Collections.emptyList();
}

@Override
public void render(PoseStack matrices, MultiBufferSource vertexConsumers, double cameraX, double cameraY, double cameraZ) {
this.windDisturbances.forEach(
windDisturbance -> {
LevelRenderer.renderVoxelShape(
matrices,
vertexConsumers.getBuffer(RenderType.lines()),
Shapes.create(windDisturbance.affectedArea),
-cameraX,
-cameraY,
-cameraZ,
0.5F,
1F,
0.5F,
0.35F,
true
);
renderFilledBox(
matrices,
vertexConsumers,
AABB.ofSize(windDisturbance.origin, 0.2D, 0.2D, 0.2D),
cameraX, cameraY, cameraZ
);
}
);

WindDebugRenderer.renderWindNodesFromList(matrices, vertexConsumers, cameraX, cameraY, cameraZ, this.windNodes);
}

private @NotNull List<List<Pair<Vec3, Integer>>> createAllWindNodes() {
List<List<Pair<Vec3, Integer>>> windNodes = new ArrayList<>();
this.windDisturbances.forEach(
windDisturbance -> {
if (FrustumUtil.isVisible(windDisturbance.affectedArea)) {
BlockPos.betweenClosed(
BlockPos.containing(windDisturbance.affectedArea.getMinPosition()),
BlockPos.containing(windDisturbance.affectedArea.getMaxPosition())
).forEach(
blockPos -> {
Vec3 blockPosCenter = Vec3.atCenterOf(blockPos);
windNodes.add(WindDebugRenderer.createWindNodes(this.minecraft.level, blockPosCenter, 1D, true));
}
);
}
}
);
return windNodes;
}

private static void renderFilledBox(
PoseStack matrices,
MultiBufferSource vertexConsumers,
@NotNull AABB box,
double cameraX, double cameraY, double cameraZ
) {
Vec3 vec3 = new Vec3(-cameraX, -cameraY, -cameraZ);
DebugRenderer.renderFilledBox(matrices, vertexConsumers, box.move(vec3), 1F, 1F, 1F, 1F);
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/frozenlib/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"debug.light": "Light",
"debug.breeze": "Breeze",
"debug.wind": "Wind",
"debug.wind_disturbance": "Wind Disturbance",
"debug.neighbor_update": "Neighbor Updates",
"debug.game_event": "Game Events",

Expand Down

0 comments on commit 734d5dc

Please sign in to comment.