Skip to content

Commit 0c90969

Browse files
committed
Added ClientMatrices, some changes to WorldMouse
1 parent 90aede9 commit 0c90969

File tree

7 files changed

+75
-29
lines changed

7 files changed

+75
-29
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dev.latvian.mods.klib;
2+
3+
import dev.latvian.mods.klib.math.ClientMatrices;
4+
import net.neoforged.api.distmarker.Dist;
5+
import net.neoforged.bus.api.EventPriority;
6+
import net.neoforged.bus.api.SubscribeEvent;
7+
import net.neoforged.fml.common.EventBusSubscriber;
8+
import net.neoforged.neoforge.client.event.FrameGraphSetupEvent;
9+
10+
@EventBusSubscriber(modid = KLibMod.ID, value = Dist.CLIENT)
11+
public class KLibClientEventHandler {
12+
@SubscribeEvent(priority = EventPriority.HIGHEST)
13+
public static void setup(FrameGraphSetupEvent event) {
14+
ClientMatrices.MODEL_VIEW.set(event.getModelViewMatrix());
15+
ClientMatrices.PROJECTION.set(event.getProjectionMatrix());
16+
ClientMatrices.WORLD.set(event.getProjectionMatrix().mul(event.getModelViewMatrix()));
17+
ClientMatrices.INVERSE_WORLD.set(ClientMatrices.WORLD).invert();
18+
}
19+
}

src/main/java/dev/latvian/mods/klib/codec/MCCodecs.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package dev.latvian.mods.klib.codec;
22

3-
import com.mojang.authlib.GameProfile;
43
import com.mojang.datafixers.util.Either;
54
import com.mojang.serialization.Codec;
6-
import com.mojang.serialization.codecs.RecordCodecBuilder;
75
import dev.latvian.mods.klib.math.KMath;
86
import net.minecraft.Util;
97
import net.minecraft.core.SectionPos;
@@ -32,9 +30,4 @@ public interface MCCodecs {
3230
Codec<SoundSource> SOUND_SOURCE = KLibCodecs.anyEnumCodec(SoundSource.values(), SoundSource::getName);
3331
Codec<BlockState> BLOCK_STATE = Codec.either(BlockState.CODEC, BuiltInRegistries.BLOCK.byNameCodec()).xmap(either -> either.map(Function.identity(), Block::defaultBlockState), state -> state == state.getBlock().defaultBlockState() ? Either.right(state.getBlock()) : Either.left(state));
3432
Codec<FluidState> FLUID_STATE = Codec.either(FluidState.CODEC, BuiltInRegistries.FLUID.byNameCodec()).xmap(either -> either.map(Function.identity(), Fluid::defaultFluidState), state -> state == state.getType().defaultFluidState() ? Either.right(state.getType()) : Either.left(state));
35-
36-
Codec<GameProfile> GAME_PROFILE = RecordCodecBuilder.create(instance -> instance.group(
37-
KLibCodecs.UUID.fieldOf("id").forGetter(GameProfile::getId),
38-
Codec.STRING.fieldOf("name").forGetter(GameProfile::getName)
39-
).apply(instance, GameProfile::new));
4033
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.latvian.mods.klib.core.mixin;
2+
3+
import dev.latvian.mods.klib.math.ClientMatrices;
4+
import net.minecraft.client.renderer.GameRenderer;
5+
import net.minecraft.client.renderer.LevelRenderer;
6+
import net.minecraft.world.phys.Vec3;
7+
import org.joml.Matrix4f;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Redirect;
11+
12+
@Mixin(GameRenderer.class)
13+
public class GameRendererMixin {
14+
@Redirect(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;prepareCullFrustum(Lnet/minecraft/world/phys/Vec3;Lorg/joml/Matrix4f;Lorg/joml/Matrix4f;)V"))
15+
private void klib$getFrustumMatrix(LevelRenderer instance, Vec3 cameraPosition, Matrix4f fMatrix, Matrix4f projectionMatrix) {
16+
ClientMatrices.PERSPECTIVE.set(projectionMatrix);
17+
instance.prepareCullFrustum(cameraPosition, fMatrix, projectionMatrix);
18+
ClientMatrices.FRUSTUM.set(fMatrix);
19+
}
20+
}

src/main/java/dev/latvian/mods/klib/data/DataTypes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import net.minecraft.resources.ResourceLocation;
5151
import net.minecraft.sounds.SoundEvent;
5252
import net.minecraft.sounds.SoundSource;
53+
import net.minecraft.util.ExtraCodecs;
5354
import net.minecraft.world.InteractionHand;
5455
import net.minecraft.world.item.ItemStack;
5556
import net.minecraft.world.level.Level;
@@ -90,7 +91,7 @@ public interface DataTypes {
9091
DataType<Vec3> VEC3S = DataType.of(MCCodecs.VEC3S, MCStreamCodecs.VEC3S, Vec3.class);
9192
DataType<BlockPos> BLOCK_POS = DataType.of(BlockPos.CODEC, BlockPos.STREAM_CODEC, BlockPos.class);
9293
DataType<Integer> TICKS = DataType.of(KLibCodecs.TICKS, ByteBufCodecs.VAR_INT, Integer.class);
93-
DataType<GameProfile> GAME_PROFILE = DataType.of(MCCodecs.GAME_PROFILE, MCStreamCodecs.GAME_PROFILE, GameProfile.class);
94+
DataType<GameProfile> GAME_PROFILE = DataType.of(ExtraCodecs.GAME_PROFILE, ByteBufCodecs.GAME_PROFILE, GameProfile.class);
9495
DataType<ResourceKey<Level>> DIMENSION = DataType.of(MCCodecs.DIMENSION, MCStreamCodecs.DIMENSION, (Class) ResourceKey.class);
9596

9697
static void register() {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.latvian.mods.klib.math;
2+
3+
import org.joml.Matrix4f;
4+
5+
public interface ClientMatrices {
6+
Matrix4f MODEL_VIEW = new Matrix4f();
7+
Matrix4f PROJECTION = new Matrix4f();
8+
Matrix4f WORLD = new Matrix4f();
9+
Matrix4f INVERSE_WORLD = new Matrix4f();
10+
Matrix4f PERSPECTIVE = new Matrix4f();
11+
Matrix4f FRUSTUM = new Matrix4f();
12+
}

src/main/java/dev/latvian/mods/klib/math/WorldMouse.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,37 @@
77
import net.minecraft.world.phys.HitResult;
88
import net.minecraft.world.phys.Vec3;
99
import org.jetbrains.annotations.Nullable;
10-
import org.joml.Matrix4f;
11-
import org.joml.Matrix4fc;
10+
import org.joml.Vector4d;
1211
import org.joml.Vector4f;
1312

1413
public record WorldMouse(
1514
Minecraft mc,
16-
Matrix4fc worldMatrix,
17-
Matrix4fc invertedWorldMatrix,
1815
Vec3 cameraPos,
1916
float width,
2017
float height,
21-
Vec2f defaultScreenPos
18+
Vec2d defaultScreenPos
2219
) {
23-
public static WorldMouse of(Minecraft mc, Vec3 cameraPos, Matrix4fc worldMatrix) {
20+
public static WorldMouse of(Minecraft mc, Vec3 cameraPos) {
2421
var width = mc.getWindow().getGuiScaledWidth();
2522
var height = mc.getWindow().getGuiScaledHeight();
2623

2724
return new WorldMouse(
2825
mc,
29-
new Matrix4f(worldMatrix),
30-
new Matrix4f(worldMatrix).invert(),
3126
cameraPos,
3227
width,
3328
height,
34-
mc.screen == null ? new Vec2f(
35-
width * 0.5F,
36-
height * 0.5F
37-
) : new Vec2f(
38-
(float) (mc.mouseHandler.xpos() * width / (double) mc.getWindow().getWidth()),
39-
(float) (mc.mouseHandler.ypos() * height / (double) mc.getWindow().getHeight())
29+
mc.screen == null ? new Vec2d(
30+
width * 0.5D,
31+
height * 0.5D
32+
) : new Vec2d(
33+
mc.mouseHandler.xpos() * width / (double) mc.getWindow().getWidth(),
34+
mc.mouseHandler.ypos() * height / (double) mc.getWindow().getHeight()
4035
)
4136
);
4237
}
4338

4439
@Nullable
45-
public Cursor clip(double maxDistance, ClipContext.Block blockClipContext, ClipContext.Fluid fluidClipContext, @Nullable Vec2f screenPos, @Nullable Entity clipEntity) {
40+
public Cursor clip(double maxDistance, ClipContext.Block blockClipContext, ClipContext.Fluid fluidClipContext, @Nullable Vec2d screenPos, @Nullable Entity clipEntity) {
4641
if (screenPos == null) {
4742
screenPos = defaultScreenPos;
4843
}
@@ -92,8 +87,13 @@ public Cursor clipCollision() {
9287
*/
9388
@Nullable
9489
public Vec2f screen(double worldX, double worldY, double worldZ, boolean allowOutside) {
95-
var v = new Vector4f((float) (worldX - cameraPos.x), (float) (worldY - cameraPos.y), (float) (worldZ - cameraPos.z), 1F);
96-
v.mul(worldMatrix);
90+
double rx = worldX - cameraPos.x;
91+
double ry = worldY - cameraPos.y;
92+
double rz = worldZ - cameraPos.z;
93+
double len = Math.sqrt(rx * rx + ry * ry + rz * rz);
94+
95+
var v = new Vector4f((float) (rx / len), (float) (ry / len), (float) (rz / len), 1F);
96+
v.mul(ClientMatrices.WORLD);
9797
v.div(v.w);
9898

9999
if (allowOutside || v.z > 0F && v.z < 1F) {
@@ -137,15 +137,15 @@ public Vec2f screen(Position worldPos) {
137137
}
138138

139139
/**
140-
* Convert screen coordinates to world position. Use {@link WorldMouse#clip(double, ClipContext.Block, ClipContext.Fluid, Vec2f, Entity)} if you only care about current mouse position
140+
* Convert screen coordinates to world position. Use {@link WorldMouse#clip(double, ClipContext.Block, ClipContext.Fluid, Vec2d, Entity)} if you only care about current mouse position
141141
*
142142
* @param x screen coordinate x-position
143143
* @param y screen coordinate y-position
144144
* @return a {@link Vec3} containing the screen coordinates in world position
145145
*/
146-
public Vec3 world(float x, float y) {
147-
var v = new Vector4f(x * 2F / width - 1F, -(y * 2F / height - 1F), 1F, 1F);
148-
v.mul(invertedWorldMatrix);
146+
public Vec3 world(double x, double y) {
147+
var v = new Vector4d(x * 2D / width - 1D, -(y * 2D / height - 1F), 1D, 1D);
148+
v.mul(ClientMatrices.INVERSE_WORLD);
149149
v.div(v.w);
150150
return new Vec3(v.x + cameraPos.x, v.y + cameraPos.y, v.z + cameraPos.z);
151151
}

src/main/resources/klib.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"StreamCodecMixin"
99
],
1010
"client": [
11+
"GameRendererMixin",
1112
"PoseStackPoseMixin",
1213
"VertexConsumerMixin"
1314
],

0 commit comments

Comments
 (0)