Skip to content

Commit

Permalink
1.16.2 Forge + frustum calculations
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Aug 14, 2020
1 parent 9d8fdf1 commit ac20605
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 75 deletions.
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ idea.project.settings {
project = rootProject.project(":fabric")
taskNames = Collections.singletonList("runServer")
}
// "Forge: Minecraft Client"(org.jetbrains.gradle.ext.Gradle) {
// project = rootProject.project(":forge")
// taskNames = Collections.singletonList("runClient")
// }
// "Forge: Minecraft Server"(org.jetbrains.gradle.ext.Gradle) {
// project = rootProject.project(":forge")
// taskNames = Collections.singletonList("runServer")
// }
"Forge: Minecraft Client"(org.jetbrains.gradle.ext.Gradle) {
project = rootProject.project(":forge")
taskNames = Collections.singletonList("runClient")
}
"Forge: Minecraft Server"(org.jetbrains.gradle.ext.Gradle) {
project = rootProject.project(":forge")
taskNames = Collections.singletonList("runServer")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("fabric-loom") version "0.5-SNAPSHOT"
id("fabric-loom") version "0.4-SNAPSHOT"
}

minecraft {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.shedaniel.lightoverlay.fabric;

import net.minecraft.client.render.Frustum;
import net.minecraft.client.util.math.Vector4f;

public class FrustumHelper {
public static boolean isVisible(Frustum frustum, double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
float x1 = (float) (minX - frustum.x);
float y1 = (float) (minY - frustum.y);
float z1 = (float) (minZ - frustum.z);
float x2 = (float) (maxX - frustum.x);
float y2 = (float) (maxY - frustum.y);
float z2 = (float) (maxZ - frustum.z);
return isAnyCornerVisible(frustum, x1, y1, z1, x2, y2, z2);
}

private static boolean isAnyCornerVisible(Frustum frustum, float x1, float y1, float z1, float x2, float y2, float z2) {
Vector4f[] homogeneousCoordinates = frustum.homogeneousCoordinates;
for (Vector4f vector4f : homogeneousCoordinates) {
if (dotProduct(vector4f, x1, y1, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y1, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y2, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y2, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y1, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y1, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y2, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y2, z2, 1.0F) <= 0.0F) {
return false;
}
}

return true;
}

private static float dotProduct(Vector4f self, float x, float y, float z, float w) {
return self.getX() * x + self.getY() * y + self.getZ() * z + self.getW() * w;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.Frustum;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.util.InputUtil;
Expand Down Expand Up @@ -47,7 +48,10 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

Expand All @@ -72,6 +76,7 @@ public class LightOverlay implements ClientModInitializer {
private static boolean enabled = false;
private static EntityType<Entity> testingEntityType;
private static int threadNumber = 0;
public static Frustum frustum;
private static final ThreadPoolExecutor EXECUTOR = (ThreadPoolExecutor) Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), r -> {
Thread thread = new Thread(r, "light-overlay-" + threadNumber++);
thread.setDaemon(true);
Expand Down Expand Up @@ -437,6 +442,7 @@ public void onInitializeClient() {
RenderSystem.enableTexture();
RenderSystem.depthMask(true);
BlockPos.Mutable mutable = new BlockPos.Mutable();
BlockPos.Mutable downMutable = new BlockPos.Mutable();
for (Map.Entry<ChunkPos, Long2ReferenceMap<Object>> entry : CHUNK_MAP.entrySet()) {
if (caching && (MathHelper.abs(entry.getKey().x - playerPosX) > getChunkRange() || MathHelper.abs(entry.getKey().z - playerPosZ) > getChunkRange())) {
continue;
Expand All @@ -445,8 +451,10 @@ public void onInitializeClient() {
if (objectEntry.getValue() instanceof Integer) {
mutable.set(BlockPos.unpackLongX(objectEntry.getLongKey()), BlockPos.unpackLongY(objectEntry.getLongKey()), BlockPos.unpackLongZ(objectEntry.getLongKey()));
if (mutable.isWithinDistance(playerPos, reach)) {
BlockPos down = mutable.down();
LightOverlay.renderLevel(CLIENT, camera, world, mutable, down, (Integer) objectEntry.getValue(), entityContext);
if (frustum == null || FrustumHelper.isVisible(frustum, mutable.getX(), mutable.getY(), mutable.getZ(), mutable.getX() + 1, mutable.getX() + 1, mutable.getX() + 1)) {
downMutable.set(mutable.getX(), mutable.getY() - 1, mutable.getZ());
LightOverlay.renderLevel(CLIENT, camera, world, mutable, downMutable, (Integer) objectEntry.getValue(), entityContext);
}
}
}
}
Expand All @@ -469,8 +477,10 @@ public void onInitializeClient() {
if (objectEntry.getValue() instanceof CrossType) {
mutable.set(BlockPos.unpackLongX(objectEntry.getLongKey()), BlockPos.unpackLongY(objectEntry.getLongKey()), BlockPos.unpackLongZ(objectEntry.getLongKey()));
if (mutable.isWithinDistance(playerPos, reach)) {
int color = objectEntry.getValue() == CrossType.RED ? redColor : objectEntry.getValue() == CrossType.YELLOW ? yellowColor : secondaryColor;
LightOverlay.renderCross(camera, world, mutable, color, entityContext);
if (frustum == null || FrustumHelper.isVisible(frustum, mutable.getX(), mutable.getY(), mutable.getZ(), mutable.getX() + 1, mutable.getX() + 1, mutable.getX() + 1)) {
int color = objectEntry.getValue() == CrossType.RED ? redColor : objectEntry.getValue() == CrossType.YELLOW ? yellowColor : secondaryColor;
LightOverlay.renderCross(camera, world, mutable, color, entityContext);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.shedaniel.lightoverlay.fabric.mixin;

import me.shedaniel.lightoverlay.fabric.LightOverlay;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.Frustum;
import net.minecraft.client.render.WorldRenderer;
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.CallbackInfo;

@Mixin(WorldRenderer.class)
public class MixinWorldRenderer {
@Inject(method = "setupTerrain", at = @At("HEAD"))
private void setupTerrain(Camera camera, Frustum frustum, boolean hasForcedFrustum, int frame, boolean spectator, CallbackInfo ci) {
LightOverlay.frustum = frustum;
}
}
6 changes: 5 additions & 1 deletion fabric/src/main/resources/lightoverlay.accesswidener
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
accessWidener v1 named
accessible field net/minecraft/network/packet/s2c/play/ChunkDeltaUpdateS2CPacket sectionPos Lnet/minecraft/util/math/ChunkSectionPos;
accessible field net/minecraft/network/packet/s2c/play/ChunkDeltaUpdateS2CPacket sectionPos Lnet/minecraft/util/math/ChunkSectionPos;
accessible field net/minecraft/client/render/Frustum x D
accessible field net/minecraft/client/render/Frustum y D
accessible field net/minecraft/client/render/Frustum z D
accessible field net/minecraft/client/render/Frustum homogeneousCoordinates [Lnet/minecraft/client/util/math/Vector4f;
3 changes: 2 additions & 1 deletion fabric/src/main/resources/lightoverlay.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"compatibilityLevel": "JAVA_8",
"mixins": [],
"client": [
"MixinClientConnection"
"MixinClientConnection",
"MixinWorldRenderer"
],
"injectors": {
"defaultRequire": 1
Expand Down
1 change: 1 addition & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apply plugin: "eclipse"

minecraft {
mappings(channel: "snapshot", version: rootProject.mcp_snapshot)
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
runs {
client {
workingDirectory project.file("run")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.shedaniel.lightoverlay.forge;

import net.minecraft.client.renderer.culling.ClippingHelper;
import net.minecraft.util.math.vector.Vector4f;

public class FrustumHelper {
public static boolean isVisible(ClippingHelper frustum, double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
float x1 = (float) (minX - frustum.cameraX);
float y1 = (float) (minY - frustum.cameraY);
float z1 = (float) (minZ - frustum.cameraZ);
float x2 = (float) (maxX - frustum.cameraX);
float y2 = (float) (maxY - frustum.cameraY);
float z2 = (float) (maxZ - frustum.cameraZ);
return isAnyCornerVisible(frustum, x1, y1, z1, x2, y2, z2);
}

private static boolean isAnyCornerVisible(ClippingHelper frustum, float x1, float y1, float z1, float x2, float y2, float z2) {
Vector4f[] homogeneousCoordinates = frustum.frustum;
for (Vector4f vector4f : homogeneousCoordinates) {
if (dotProduct(vector4f, x1, y1, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y1, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y2, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y2, z1, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y1, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y1, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x1, y2, z2, 1.0F) <= 0.0F && dotProduct(vector4f, x2, y2, z2, 1.0F) <= 0.0F) {
return false;
}
}

return true;
}

private static float dotProduct(Vector4f self, float x, float y, float z, float w) {
return self.getX() * x + self.getY() * y + self.getZ() * z + self.getW() * w;
}
}
Loading

0 comments on commit ac20605

Please sign in to comment.