Skip to content

Commit

Permalink
Vivecraft 1.2.0 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed Jan 6, 2025
1 parent db7ce7f commit e59e97c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ subprojects {
mappings loom.officialMojangMappings()
// The following line declares the yarn mappings you may select this one as well.
// mappings "net.fabricmc:yarn:1.19.2+build.3:v2"
implementation("org.joml:joml:1.10.5")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ReflectionConstants {
public static Object[] ControllerType_ENUMS = null;
// Whether Vivecraft is successfully loaded by the library.
private static boolean hasVivecraft = false;
// Whether we're using Vivecraft 1.2.0 or later which uses JOML matrices
private static boolean useJOMLClasses = false;

public static void init() {
try {
Expand All @@ -45,8 +47,12 @@ public static void init() {
MCVR = Class.forName(VIVECRAFT_CLIENT_VR_PACKAGE + ".provider.MCVR");
ControllerType = Class.forName(VIVECRAFT_CLIENT_VR_PACKAGE + ".provider.ControllerType");
ControllerType_ENUMS = ControllerType.getEnumConstants();
Matrix4f = Class.forName(VIVECRAFT_PACKAGE + ".common.utils.math.Matrix4f");
VRSettings = Class.forName(VIVECRAFT_CLIENT_VR_PACKAGE + ".settings.VRSettings");
try {
Matrix4f = Class.forName(VIVECRAFT_PACKAGE + ".common.utils.math.Matrix4f");
} catch (ClassNotFoundException ignored2) {
useJOMLClasses = true;
}
hasVivecraft = true;
} catch (ClassNotFoundException ignored2) {}
}
Expand All @@ -55,4 +61,8 @@ public static void init() {
public static boolean clientHasVivecraft() {
return hasVivecraft;
}

public static boolean useJOMLClasses() {
return useJOMLClasses;
}
}
37 changes: 30 additions & 7 deletions common/src/main/java/net/blf02/vrapi/client/VRDataGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import net.blf02.vrapi.data.VRData;
import net.blf02.vrapi.data.VRPlayer;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;
import org.lwjgl.BufferUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -78,7 +81,10 @@ public static void init() {
VRDevicePose_getRoll = getMethod(ReflectionConstants.VRDevicePoseRaw, "getRoll");
VRDevicePose_getMatrix = getMethod(ReflectionConstants.VRDevicePoseRaw, "getMatrix");

Matrix4f_toFloatBuffer = getMethod(ReflectionConstants.Matrix4f, "toFloatBuffer");
if (!ReflectionConstants.useJOMLClasses()) {
Matrix4f_toFloatBuffer = getMethod(ReflectionConstants.Matrix4f, "toFloatBuffer");

}

try {
Minecraft_vr = getField(Minecraft.class, "vr");
Expand Down Expand Up @@ -151,27 +157,27 @@ public static VRPlayer getVRPlayer(PlayerType type) {
Object eye1DevicePoseRaw = VRData_eye1.get(vrDataRaw);

Vec3 hmdPosition = (Vec3) VRDevicePose_getPosition.invoke(hmdDevicePoseRaw); // Gets the position for the HMD in the world.
Vec3 hmdLookVec = (Vec3) VRDevicePose_getDirection.invoke(hmdDevicePoseRaw);
Vec3 hmdLookVec = fromVivecraftVec3(VRDevicePose_getDirection.invoke(hmdDevicePoseRaw));
float hmdRoll = (float) VRDevicePose_getRoll.invoke(hmdDevicePoseRaw);
Matrix4f hmdRotMatr = fromVivecraftMatrix4f(VRDevicePose_getMatrix.invoke(hmdDevicePoseRaw));

Vec3 c0Position = (Vec3) VRDevicePose_getPosition.invoke(c0DevicePoseRaw);
Vec3 c0LookVec = (Vec3) VRDevicePose_getDirection.invoke(c0DevicePoseRaw);
Vec3 c0LookVec = fromVivecraftVec3(VRDevicePose_getDirection.invoke(c0DevicePoseRaw));
float c0roll = (float) VRDevicePose_getRoll.invoke(c0DevicePoseRaw);
Matrix4f c0RotMatr = fromVivecraftMatrix4f(VRDevicePose_getMatrix.invoke(c0DevicePoseRaw));

Vec3 c1Position = (Vec3) VRDevicePose_getPosition.invoke(c1DevicePoseRaw);
Vec3 c1LookVec = (Vec3) VRDevicePose_getDirection.invoke(c1DevicePoseRaw);
Vec3 c1LookVec = fromVivecraftVec3(VRDevicePose_getDirection.invoke(c1DevicePoseRaw));
float c1roll = (float) VRDevicePose_getRoll.invoke(c1DevicePoseRaw);
Matrix4f c1RotMatr = fromVivecraftMatrix4f(VRDevicePose_getMatrix.invoke(c1DevicePoseRaw));

Vec3 eye0Position = (Vec3) VRDevicePose_getPosition.invoke(eye0DevicePoseRaw);
Vec3 eye0LookVec = (Vec3) VRDevicePose_getDirection.invoke(eye0DevicePoseRaw);
Vec3 eye0LookVec = fromVivecraftVec3(VRDevicePose_getDirection.invoke(eye0DevicePoseRaw));
float eye0roll = (float) VRDevicePose_getRoll.invoke(eye0DevicePoseRaw);
Matrix4f eye0RotMatr = fromVivecraftMatrix4f(VRDevicePose_getMatrix.invoke(eye0DevicePoseRaw));

Vec3 eye1Position = (Vec3) VRDevicePose_getPosition.invoke(eye1DevicePoseRaw);
Vec3 eye1LookVec = (Vec3) VRDevicePose_getDirection.invoke(eye1DevicePoseRaw);
Vec3 eye1LookVec = fromVivecraftVec3(VRDevicePose_getDirection.invoke(eye1DevicePoseRaw));
float eye1roll = (float) VRDevicePose_getRoll.invoke(eye1DevicePoseRaw);
Matrix4f eye1RotMatr = fromVivecraftMatrix4f(VRDevicePose_getMatrix.invoke(eye1DevicePoseRaw));

Expand All @@ -188,8 +194,25 @@ public static VRPlayer getVRPlayer(PlayerType type) {
}
}

private static Vec3 fromVivecraftVec3(Object vecIn) {
if (!ReflectionConstants.useJOMLClasses()) {
return (Vec3) vecIn;
}
Vector3f vec = (Vector3f) vecIn;
return new Vec3(vec.x, vec.y, vec.z);
}

public static Matrix4f fromVivecraftMatrix4f(Object matrixIn) throws InvocationTargetException, IllegalAccessException {
FloatBuffer buffer = (FloatBuffer) Matrix4f_toFloatBuffer.invoke(matrixIn);
FloatBuffer buffer;
if (ReflectionConstants.useJOMLClasses()) {
org.joml.Matrix4f jomlMat = (org.joml.Matrix4f) matrixIn;
buffer = BufferUtils.createFloatBuffer(16);
buffer.position(0);
buffer = jomlMat.get(buffer);
buffer.position(0);
} else {
buffer = (FloatBuffer) Matrix4f_toFloatBuffer.invoke(matrixIn);
}
Matrix4f matr = new Matrix4f();
matr.load(buffer);
return matr;
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/net/blf02/vrapi/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {

// Version {major, minor, patch}
public static final int[] version = new int[]{3, 0, 11};
public static final int[] version = new int[]{3, 0, 12};

// Debugging
public static final boolean doDebugging = false;
Expand Down
2 changes: 2 additions & 0 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencies {

common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionFabric")) { transitive false }

include(implementation("org.joml:joml:1.10.5"))
}

processResources {
Expand Down
2 changes: 2 additions & 0 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {

common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }

include(implementation("org.joml:joml:1.10.5"))
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.19.2
enabled_platforms=fabric,forge

archives_base_name=vrapi
mod_version=3.0.11
mod_version=3.0.12
maven_group=net.blf02.vrapi

architectury_version=6.2.43
Expand Down

0 comments on commit e59e97c

Please sign in to comment.