From ca044bf81992043b9674815cd52d27851ad63cab Mon Sep 17 00:00:00 2001 From: fayer3 Date: Tue, 3 Oct 2023 02:29:40 +0200 Subject: [PATCH] fix mixed reality mirror --- .../gui/settings/GuiRenderOpticsSettings.java | 6 + .../org/vivecraft/client/render/HMDLayer.java | 1 - .../client/render/VRPlayerModel.java | 1 - .../client/render/VRPlayerModel_WithArms.java | 1 - .../org/vivecraft/client/utils/Utils.java | 1 - .../gameplay/trackers/ClimbTracker.java | 1 - .../vivecraft/client_vr/provider/MCVR.java | 1 - .../client_vr/provider/VRRenderer.java | 3 +- .../vivecraft/client_vr/render/VRShaders.java | 10 +- .../render/helpers/VREffectsHelper.java | 3 +- .../render/helpers/VRPassHelper.java | 27 +++- .../render/helpers/VRWidgetHelper.java | 1 - .../client_vr/settings/VRSettings.java | 24 +--- .../client_vr/utils/LoaderUtils.java | 1 - .../mixin/client_vr/MinecraftVRMixin.java | 118 ++++++++---------- .../renderer/ItemInHandRendererVRMixin.java | 1 - .../optifine/OptifineHelper.java | 23 ++++ .../minecraft/shaders/core/mixedreality.fsh | 89 +++++++++---- .../minecraft/shaders/core/mixedreality.json | 21 +--- .../minecraft/shaders/core/passthrough.vsh | 2 +- 20 files changed, 184 insertions(+), 151 deletions(-) diff --git a/common/src/main/java/org/vivecraft/client/gui/settings/GuiRenderOpticsSettings.java b/common/src/main/java/org/vivecraft/client/gui/settings/GuiRenderOpticsSettings.java index 901314c6a..d1eeea8d7 100644 --- a/common/src/main/java/org/vivecraft/client/gui/settings/GuiRenderOpticsSettings.java +++ b/common/src/main/java/org/vivecraft/client/gui/settings/GuiRenderOpticsSettings.java @@ -136,10 +136,16 @@ protected void actionPerformed(AbstractWidget widget) { } else { this.dataholder.vrRenderer.reinitFrameBuffers("Render Setting Changed"); } + this.reinit = true; } if (guivroption.getId() == VRSettings.VrOptions.RELOAD_EXTERNAL_CAMERA.ordinal()) { VRHotkeys.loadExternalCameraConfig(); } + if (guivroption.getId() == VRSettings.VrOptions.MIXED_REALITY_ALPHA_MASK.ordinal() + || guivroption.getId() == VRSettings.VrOptions.MIXED_REALITY_UNITY_LIKE.ordinal() + || guivroption.getId() == VRSettings.VrOptions.MIXED_REALITY_UNDISTORTED.ordinal()) { + this.reinit = true; + } } } diff --git a/common/src/main/java/org/vivecraft/client/render/HMDLayer.java b/common/src/main/java/org/vivecraft/client/render/HMDLayer.java index 74365011e..a6561b759 100644 --- a/common/src/main/java/org/vivecraft/client/render/HMDLayer.java +++ b/common/src/main/java/org/vivecraft/client/render/HMDLayer.java @@ -10,7 +10,6 @@ import net.minecraft.client.renderer.entity.layers.RenderLayer; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.entity.player.Player; import org.vivecraft.client.VRPlayersClient; public class HMDLayer extends RenderLayer> { diff --git a/common/src/main/java/org/vivecraft/client/render/VRPlayerModel.java b/common/src/main/java/org/vivecraft/client/render/VRPlayerModel.java index 92bac0994..931136b16 100644 --- a/common/src/main/java/org/vivecraft/client/render/VRPlayerModel.java +++ b/common/src/main/java/org/vivecraft/client/render/VRPlayerModel.java @@ -10,7 +10,6 @@ import net.minecraft.client.model.geom.builders.MeshDefinition; import net.minecraft.client.model.geom.builders.PartDefinition; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.player.Player; import org.vivecraft.client.VRPlayersClient; public class VRPlayerModel extends PlayerModel { diff --git a/common/src/main/java/org/vivecraft/client/render/VRPlayerModel_WithArms.java b/common/src/main/java/org/vivecraft/client/render/VRPlayerModel_WithArms.java index 3d3645529..0e917b993 100644 --- a/common/src/main/java/org/vivecraft/client/render/VRPlayerModel_WithArms.java +++ b/common/src/main/java/org/vivecraft/client/render/VRPlayerModel_WithArms.java @@ -15,7 +15,6 @@ import net.minecraft.util.Mth; import net.minecraft.world.entity.HumanoidArm; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.phys.Vec3; import org.vivecraft.client.VRPlayersClient; import org.vivecraft.client.Xplat; diff --git a/common/src/main/java/org/vivecraft/client/utils/Utils.java b/common/src/main/java/org/vivecraft/client/utils/Utils.java index 78977313c..d8baba3f4 100644 --- a/common/src/main/java/org/vivecraft/client/utils/Utils.java +++ b/common/src/main/java/org/vivecraft/client/utils/Utils.java @@ -32,7 +32,6 @@ import javax.annotation.Nullable; import java.io.*; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Path; diff --git a/common/src/main/java/org/vivecraft/client_vr/gameplay/trackers/ClimbTracker.java b/common/src/main/java/org/vivecraft/client_vr/gameplay/trackers/ClimbTracker.java index 658d58de3..3926e0fde 100644 --- a/common/src/main/java/org/vivecraft/client_vr/gameplay/trackers/ClimbTracker.java +++ b/common/src/main/java/org/vivecraft/client_vr/gameplay/trackers/ClimbTracker.java @@ -3,7 +3,6 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket; import net.minecraft.server.level.ServerPlayer; diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/MCVR.java b/common/src/main/java/org/vivecraft/client_vr/provider/MCVR.java index ae986b0e0..5a6de9e07 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/MCVR.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/MCVR.java @@ -3,7 +3,6 @@ import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.ChatScreen; -import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.WinScreen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.network.chat.Component; diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java b/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java index 322eab015..cccda865f 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java @@ -3,6 +3,7 @@ import com.mojang.blaze3d.pipeline.RenderTarget; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlUtil; +import com.mojang.blaze3d.shaders.ProgramManager; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.*; import net.minecraft.client.GraphicsStatus; @@ -177,7 +178,7 @@ public void doStencil(boolean inverse) { RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); RenderSystem.enableCull(); - GlStateManager._glUseProgram(s); + ProgramManager.glUseProgram(s); RenderSystem.stencilFunc(GL11.GL_NOTEQUAL, 255, 1); RenderSystem.stencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP); RenderSystem.stencilMask(0); // Dont Write to stencil buffer diff --git a/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java b/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java index 53a65fa28..fe2e7b043 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java @@ -14,14 +14,11 @@ public class VRShaders { public static AbstractUniform _Lanczos_projectionUniform; public static AbstractUniform _Lanczos_modelViewUniform; public static ShaderInstance depthMaskShader; - public static AbstractUniform _DepthMask_resolutionUniform; - public static AbstractUniform _DepthMask_positionUniform; - public static AbstractUniform _DepthMask_scaleUniform; public static AbstractUniform _DepthMask_hmdViewPosition; public static AbstractUniform _DepthMask_hmdPlaneNormal; public static AbstractUniform _DepthMask_projectionMatrix; public static AbstractUniform _DepthMask_viewMatrix; - public static AbstractUniform _DepthMask_passUniform; + public static AbstractUniform _DepthMask_firstPersonPassUniform; public static AbstractUniform _DepthMask_keyColorUniform; public static AbstractUniform _DepthMask_alphaModeUniform; public static int _FOVReduction_Enabled; @@ -55,14 +52,11 @@ private VRShaders() { public static void setupDepthMask() throws Exception { depthMaskShader = new ShaderInstance(Minecraft.getInstance().getResourceManager(), "mixedreality", DefaultVertexFormat.POSITION_TEX); - - _DepthMask_resolutionUniform = depthMaskShader.safeGetUniform("resolution"); - _DepthMask_positionUniform = depthMaskShader.safeGetUniform("position"); _DepthMask_hmdViewPosition = depthMaskShader.safeGetUniform("hmdViewPosition"); _DepthMask_hmdPlaneNormal = depthMaskShader.safeGetUniform("hmdPlaneNormal"); _DepthMask_projectionMatrix = depthMaskShader.safeGetUniform("projectionMatrix"); _DepthMask_viewMatrix = depthMaskShader.safeGetUniform("viewMatrix"); - _DepthMask_passUniform = depthMaskShader.safeGetUniform("pass"); + _DepthMask_firstPersonPassUniform = depthMaskShader.safeGetUniform("firstPersonPass"); _DepthMask_keyColorUniform = depthMaskShader.safeGetUniform("keyColor"); _DepthMask_alphaModeUniform = depthMaskShader.safeGetUniform("alphaMode"); } diff --git a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VREffectsHelper.java b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VREffectsHelper.java index 6facd1fc4..68aeb31d1 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VREffectsHelper.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VREffectsHelper.java @@ -820,8 +820,9 @@ private static boolean shouldRenderCrosshair() { return false; } else if (dataHolder.climbTracker.isGrabbingLadder(0)) { return false; - } else + } else { return !(dataHolder.vrPlayer.worldScale > 15.0F); + } } public static void renderCrosshairAtDepth(boolean depthAlways, PoseStack poseStack) { diff --git a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java index 4130df2e0..b8adb4bc2 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java @@ -2,19 +2,23 @@ import com.mojang.blaze3d.pipeline.RenderTarget; import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.shaders.ProgramManager; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.Util; import net.minecraft.client.Minecraft; import net.minecraft.util.Mth; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.Blocks; +import org.lwjgl.opengl.GL13C; import org.vivecraft.client.Xplat; import org.vivecraft.client.extensions.RenderTargetExtension; import org.vivecraft.client_vr.ClientDataHolderVR; import org.vivecraft.client_vr.extensions.GameRendererExtension; import org.vivecraft.client_vr.render.RenderPass; import org.vivecraft.client_vr.render.VRShaders; +import org.vivecraft.client_vr.settings.VRSettings; import org.vivecraft.mod_compat_vr.iris.IrisHelper; +import org.vivecraft.mod_compat_vr.optifine.OptifineHelper; public class VRPassHelper { @@ -182,7 +186,7 @@ public static void renderSingleView(RenderPass eye, float partialTicks, long nan VRShaders._Overlay_eye.set(dataHolder.currentPass == RenderPass.LEFT ? 1 : -1); ((RenderTargetExtension) rendertarget).vivecraft$blitFovReduction(VRShaders.fovReductionShader, dataHolder.vrRenderer.framebufferEye0.viewWidth, dataHolder.vrRenderer.framebufferEye0.viewHeight); - GlStateManager._glUseProgram(0); + ProgramManager.glUseProgram(0); checkGLError("post overlay" + eye); mc.getProfiler().pop(); } @@ -197,11 +201,28 @@ public static void renderSingleView(RenderPass eye, float partialTicks, long nan dataHolder.vrRenderer.cameraFramebuffer.viewHeight, 0, true, 0.0F, 0.0F, false); mc.getProfiler().pop(); } + + if (dataHolder.currentPass == RenderPass.THIRD + && dataHolder.vrSettings.displayMirrorMode == VRSettings.MirrorMode.MIXED_REALITY + && OptifineHelper.isOptifineLoaded() + && renderWorld && mc.level != null + && OptifineHelper.isShaderActive() + && OptifineHelper.bindShaderFramebuffer()) { + // copy optifine depth buffer, since we need it for the mixed reality split + RenderSystem.activeTexture(GL13C.GL_TEXTURE0); + RenderSystem.bindTexture(dataHolder.vrRenderer.framebufferMR.getDepthTextureId()); + checkGLError("pre copy depth"); + GlStateManager._glCopyTexSubImage2D(GL13C.GL_TEXTURE_2D, 0, 0, 0, 0, 0, dataHolder.vrRenderer.framebufferMR.width, dataHolder.vrRenderer.framebufferMR.height); + checkGLError("post copy depth"); + // rebind the original buffer + dataHolder.vrRenderer.framebufferMR.bindWrite(false); + } } private static void checkGLError(String string) { - if (GlStateManager._getError() != 0) { - System.err.println(string); + int i = GlStateManager._getError(); + if (i != 0) { + System.err.println(string + ": " + i); } } } diff --git a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRWidgetHelper.java b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRWidgetHelper.java index 449ba9d84..9320ea405 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRWidgetHelper.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRWidgetHelper.java @@ -17,7 +17,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.util.RandomSource; import net.minecraft.world.inventory.InventoryMenu; -import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.Vec3; import org.vivecraft.client.utils.Utils; import org.vivecraft.client_vr.ClientDataHolderVR; diff --git a/common/src/main/java/org/vivecraft/client_vr/settings/VRSettings.java b/common/src/main/java/org/vivecraft/client_vr/settings/VRSettings.java index a9fc527e2..5c7b69ec9 100644 --- a/common/src/main/java/org/vivecraft/client_vr/settings/VRSettings.java +++ b/common/src/main/java/org/vivecraft/client_vr/settings/VRSettings.java @@ -1167,16 +1167,6 @@ void onOptionChange() { ClientDataHolderVR.getInstance().vrRenderer.reinitFrameBuffers("Mirror Setting Changed"); } } - - @Override - Object setOptionValue(Object value) { - // TODO: remove this method after fixing mixed reality... again - MirrorMode mode = ((MirrorMode) value).getNext(); - if (mode == MirrorMode.MIXED_REALITY) { - mode = mode.getNext(); - } - return mode; - } }, MIRROR_EYE(false, true, "vivecraft.options.left", "vivecraft.options.right"), // Mirror Eye MIXED_REALITY_KEY_COLOR(false, false) { // Key Color @@ -1222,23 +1212,19 @@ Object setOptionValue(Object value) { @Override void onOptionChange() { - ClientDataHolderVR.getInstance().vrRenderer.resizeFrameBuffers("MR Setting Changed"); + // reinit, because of maybe new first person pass + ClientDataHolderVR.getInstance().vrRenderer.reinitFrameBuffers("MR Setting Changed"); } }, MIXED_REALITY_UNDISTORTED(false, true) { // Undistorted Pass @Override void onOptionChange() { - ClientDataHolderVR.getInstance().vrRenderer.resizeFrameBuffers("MR Setting Changed"); - } - }, - MIXED_REALITY_ALPHA_MASK(false, true) { // Alpha Mask - - @Override - void onOptionChange() { - ClientDataHolderVR.getInstance().vrRenderer.resizeFrameBuffers("MR Setting Changed"); + // reinit, because of maybe new first person pass + ClientDataHolderVR.getInstance().vrRenderer.reinitFrameBuffers("MR Setting Changed"); } }, + MIXED_REALITY_ALPHA_MASK(false, true), // Alpha Mask, MIXED_REALITY_FOV(true, false, 0, 179, 1, 0) { // Third Person FOV @Override diff --git a/common/src/main/java/org/vivecraft/client_vr/utils/LoaderUtils.java b/common/src/main/java/org/vivecraft/client_vr/utils/LoaderUtils.java index b94702c28..62d996189 100644 --- a/common/src/main/java/org/vivecraft/client_vr/utils/LoaderUtils.java +++ b/common/src/main/java/org/vivecraft/client_vr/utils/LoaderUtils.java @@ -8,7 +8,6 @@ import java.nio.file.FileSystems; import java.util.HashMap; import java.util.Map; -import java.util.zip.ZipException; public class LoaderUtils { public static URL ZipFileUrl; diff --git a/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java b/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java index c0ef5dd56..b96e8e459 100644 --- a/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java +++ b/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java @@ -5,9 +5,9 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.InputConstants; import com.mojang.blaze3d.platform.Window; +import com.mojang.blaze3d.shaders.ProgramManager; import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexSorting; +import com.mojang.blaze3d.vertex.*; import net.minecraft.ChatFormatting; import net.minecraft.client.*; import net.minecraft.client.gui.Font; @@ -983,88 +983,68 @@ public abstract class MinecraftVRMixin implements MinecraftExtension { @Unique private void vivecraft$doMixedRealityMirror() { -// boolean flag = Config.isShaders(); - boolean flag = false; - boolean flag1 = ClientDataHolderVR.getInstance().vrSettings.mixedRealityUnityLike - && ClientDataHolderVR.getInstance().vrSettings.mixedRealityAlphaMask; + // set viewport to fullscreen, since it would be still on the one from the last pass + RenderSystem.viewport(0, 0, window.getScreenWidth(), window.getScreenHeight()); - if (!flag1) { - RenderSystem.clearColor( - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getRed() / 255.0F, - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getGreen() / 255.0F, - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getBlue() / 255.0F, 1.0F); - } else { - RenderSystem.clearColor(0.0F, 0.0F, 0.0F, 1.0F); - } - - RenderSystem.clear(16640, ON_OSX); - Vec3 vec3 = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getHeadPivot() + Vec3 camPlayer = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getHeadPivot() .subtract(ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD).getPosition()); - Matrix4f matrix4f = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD) + Matrix4f viewMatrix = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD) .getMatrix().transposed().toMCMatrix(); - Vector3 vector3 = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD).getMatrix() + Vector3 cameraLook = ClientDataHolderVR.getInstance().vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD).getMatrix() .transform(Vector3.forward()); - VRShaders._DepthMask_projectionMatrix.set(((GameRendererExtension) this.gameRenderer).vivecraft$getThirdPassProjectionMatrix()); - VRShaders._DepthMask_viewMatrix.set(matrix4f); - VRShaders._DepthMask_hmdViewPosition.set((float) vec3.x, (float) vec3.y, - (float) vec3.z); - VRShaders._DepthMask_hmdPlaneNormal.set(-vector3.getX(), 0.0F, -vector3.getZ()); - VRShaders._DepthMask_keyColorUniform.set( - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getRed() / 255.0F, - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getGreen() / 255.0F, - (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getBlue() / 255.0F); - VRShaders._DepthMask_alphaModeUniform.set(flag1 ? 1 : 0); - RenderSystem.activeTexture(33985); - RenderSystem.setShaderTexture(0, ClientDataHolderVR.getInstance().vrRenderer.framebufferMR.getColorTextureId()); - RenderSystem.activeTexture(33986); - -// if (flag && Shaders.dfb != null) { TODO -// GlStateManager._bindTexture(Shaders.dfb.depthTextures.get(0)); -// } else { - RenderSystem.setShaderTexture(1, ClientDataHolderVR.getInstance().vrRenderer.framebufferMR.getDepthTextureId()); -// } - - RenderSystem.activeTexture(33984); + // set uniforms + VRShaders._DepthMask_projectionMatrix.set(((GameRendererExtension) this.gameRenderer).vivecraft$getThirdPassProjectionMatrix()); + VRShaders._DepthMask_viewMatrix.set(viewMatrix); - for (int i = 0; i < (flag1 ? 3 : 2); ++i) { - int j = this.window.getScreenWidth() / 2; - int k = this.window.getScreenHeight(); - int l = this.window.getScreenWidth() / 2 * i; - int i1 = 0; + VRShaders._DepthMask_hmdViewPosition.set((float) camPlayer.x, (float) camPlayer.y, (float) camPlayer.z); + VRShaders._DepthMask_hmdPlaneNormal.set(-cameraLook.getX(), 0.0F, -cameraLook.getZ()); - if (ClientDataHolderVR.getInstance().vrSettings.mixedRealityUnityLike) { - j = this.window.getScreenWidth() / 2; - k = this.window.getScreenHeight() / 2; + boolean alphaMask = ClientDataHolderVR.getInstance().vrSettings.mixedRealityUnityLike + && ClientDataHolderVR.getInstance().vrSettings.mixedRealityAlphaMask; + if (!alphaMask) { + VRShaders._DepthMask_keyColorUniform.set( + (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getRed() / 255.0F, + (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getGreen() / 255.0F, + (float) ClientDataHolderVR.getInstance().vrSettings.mixedRealityKeyColor.getBlue() / 255.0F); + } else { + VRShaders._DepthMask_keyColorUniform.set(0F, 0F, 0F); + } + VRShaders._DepthMask_alphaModeUniform.set(alphaMask ? 1 : 0); - if (ClientDataHolderVR.getInstance().vrSettings.mixedRealityAlphaMask && i == 2) { - l = this.window.getScreenWidth() / 2; - i1 = this.window.getScreenHeight() / 2; - } else { - l = 0; - i1 = this.window.getScreenHeight() / 2 * (1 - i); - } - } + VRShaders._DepthMask_firstPersonPassUniform.set( + ClientDataHolderVR.getInstance().vrSettings.mixedRealityUnityLike ? 1 : 0); - VRShaders._DepthMask_resolutionUniform.set((float) j, (float) k); - VRShaders._DepthMask_positionUniform.set((float) l, (float) i1); - VRShaders._DepthMask_passUniform.set(i); - ((RenderTargetExtension) ClientDataHolderVR.getInstance().vrRenderer.framebufferMR).vivecraft$blitToScreen(VRShaders.depthMaskShader, l, j, k, i1, true, - 0.0F, 0.0F, false); - } + // bind textures + RenderSystem.setShaderTexture(0, ClientDataHolderVR.getInstance().vrRenderer.framebufferMR.getColorTextureId()); + RenderSystem.setShaderTexture(1, ClientDataHolderVR.getInstance().vrRenderer.framebufferMR.getDepthTextureId()); - GlStateManager._glUseProgram(0); + VRShaders.depthMaskShader.setSampler("thirdPersonColor", RenderSystem.getShaderTexture(0)); + VRShaders.depthMaskShader.setSampler("thirdPersonDepth", RenderSystem.getShaderTexture(1)); if (ClientDataHolderVR.getInstance().vrSettings.mixedRealityUnityLike) { if (ClientDataHolderVR.getInstance().vrSettings.mixedRealityUndistorted) { - ((RenderTargetExtension) ClientDataHolderVR.getInstance().vrRenderer.framebufferUndistorted).vivecraft$blitToScreen( - this.window.getScreenWidth() / 2, this.window.getScreenWidth() / 2, - this.window.getScreenHeight() / 2, 0, true, 0.0F, 0.0F, false); + RenderSystem.setShaderTexture(2, + ClientDataHolderVR.getInstance().vrRenderer.framebufferUndistorted.getColorTextureId()); } else { - ((RenderTargetExtension) ClientDataHolderVR.getInstance().vrRenderer.framebufferEye0).vivecraft$blitToScreen( - this.window.getScreenWidth() / 2, this.window.getScreenWidth() / 2, - this.window.getScreenHeight() / 2, 0, true, 0.0F, 0.0F, false); + RenderSystem.setShaderTexture(2, + ClientDataHolderVR.getInstance().vrRenderer.framebufferEye0.getColorTextureId()); } + VRShaders.depthMaskShader.setSampler("firstPersonColor", RenderSystem.getShaderTexture(2)); } + + VRShaders.depthMaskShader.apply(); + + Tesselator tesselator = RenderSystem.renderThreadTesselator(); + BufferBuilder bufferbuilder = tesselator.getBuilder(); + bufferbuilder.begin(VertexFormat.Mode.QUADS, VRShaders.depthMaskShader.getVertexFormat()); + bufferbuilder.vertex(-1, -1, 0.0).uv(0, 0).endVertex(); + bufferbuilder.vertex(1, -1, 0.0).uv(2, 0).endVertex(); + bufferbuilder.vertex(1, 1, 0.0).uv(2, 2).endVertex(); + bufferbuilder.vertex(-1, 1, 0.0).uv(0, 2).endVertex(); + BufferUploader.draw(bufferbuilder.end()); + VRShaders.depthMaskShader.clear(); + + ProgramManager.glUseProgram(0); } } diff --git a/common/src/main/java/org/vivecraft/mixin/client_vr/renderer/ItemInHandRendererVRMixin.java b/common/src/main/java/org/vivecraft/mixin/client_vr/renderer/ItemInHandRendererVRMixin.java index eff1a2cef..9e2dc032f 100644 --- a/common/src/main/java/org/vivecraft/mixin/client_vr/renderer/ItemInHandRendererVRMixin.java +++ b/common/src/main/java/org/vivecraft/mixin/client_vr/renderer/ItemInHandRendererVRMixin.java @@ -18,7 +18,6 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.state.BlockState; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; diff --git a/common/src/main/java/org/vivecraft/mod_compat_vr/optifine/OptifineHelper.java b/common/src/main/java/org/vivecraft/mod_compat_vr/optifine/OptifineHelper.java index d9f7827d7..614c1858d 100644 --- a/common/src/main/java/org/vivecraft/mod_compat_vr/optifine/OptifineHelper.java +++ b/common/src/main/java/org/vivecraft/mod_compat_vr/optifine/OptifineHelper.java @@ -46,6 +46,9 @@ public class OptifineHelper { private static Class shaders; private static Method shadersBeginEntitiesMethod; private static Method shadersEndEntitiesMethod; + private static Field shadersDFB; + + private static Method shadersFramebufferBindFramebuffer; private static Field optionsOfRenderRegions; private static Field optionsOfCloudHeight; @@ -79,6 +82,19 @@ public static boolean isShaderActive() { } } + public static boolean bindShaderFramebuffer() { + try { + Object dfb = shadersDFB.get(shaders); + if (dfb != null) { + shadersFramebufferBindFramebuffer.invoke(dfb); + return true; + } + } catch (InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } + return false; + } + public static void beginOutlineShader() { try { shadersRenderBeginOutlineMethod.invoke(shadersRender); @@ -297,6 +313,9 @@ private static void init() { shadersBeginEntitiesMethod = shaders.getMethod("beginEntities"); shadersEndEntitiesMethod = shaders.getMethod("endEntities"); + Class shadersFramebuffer = Class.forName("net.optifine.shaders.ShadersFramebuffer"); + shadersFramebufferBindFramebuffer = shadersFramebuffer.getMethod("bindFramebuffer"); + // private methods customColorsGetSkyColoEndMethod = customColors.getDeclaredMethod("getSkyColorEnd", Vec3.class); customColorsGetSkyColoEndMethod.setAccessible(true); @@ -307,6 +326,10 @@ private static void init() { customColorsGetFogColorNetherMethod = customColors.getDeclaredMethod("getFogColorNether", Vec3.class); customColorsGetFogColorNetherMethod.setAccessible(true); + // private Fields + shadersDFB = shaders.getDeclaredField("dfb"); + shadersDFB.setAccessible(true); + try { vertexRenderPositions = ModelPart.Vertex.class.getField("renderPositions"); } catch (NoSuchFieldException e) { diff --git a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.fsh b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.fsh index d7eaa9526..1e7975cc2 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.fsh +++ b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.fsh @@ -1,43 +1,82 @@ #version 330 core -uniform vec2 resolution; -uniform vec2 position; -uniform sampler2D Sampler0; -uniform sampler2D Sampler1; +uniform sampler2D thirdPersonColor; +uniform sampler2D thirdPersonDepth; +uniform sampler2D firstPersonColor; + uniform vec3 hmdViewPosition; uniform vec3 hmdPlaneNormal; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; -uniform int pass; +uniform int firstPersonPass; uniform vec3 keyColor; uniform int alphaMode; + +in vec2 texCoordinates; + out vec4 out_Color; -vec3 getFragmentPosition(vec2 coord) { - vec4 posScreen = vec4(coord * 2.0 - 1.0, texture(Sampler1, coord).x * 2.0 - 1.0, 1); + +vec3 getFragmentPosition(in vec2 coord) { + vec4 posScreen = vec4(coord * 2.0 - 1.0, texture(thirdPersonDepth, coord).x * 2.0 - 1.0, 1); vec4 posView = inverse(projectionMatrix * viewMatrix) * posScreen; return posView.xyz / posView.w; } + +vec3 avoidKeyColor(in vec3 color) { + // make sure colors don't match keyColor + if (all(lessThan(color - keyColor, vec3(0.004)))) { + if (all(lessThan(keyColor, vec3(0.004)))) { + // if key is black add + return color + 0.004; + } else { + return color - 0.004; + } + } else { + return color; + } +} + void main(void) { - vec2 pos = (gl_FragCoord.xy - position) / resolution; - vec3 fragPos = getFragmentPosition(pos); - float fragHmdDot = dot(fragPos - hmdViewPosition, hmdPlaneNormal); - if (((pass == 0 || pass == 2) && fragHmdDot >= 0) || pass == 1) { - vec4 color = texture(Sampler0, pos); - if (pass == 2) { - color = vec4(1, 1, 1, 1); - } else if (alphaMode == 0) { - vec3 diff = color.rgb - keyColor;// The following code prevents actual colors from matching the key color and looking weird - if (keyColor.r < 0.004 && keyColor.g < 0.004 && keyColor.b < 0.004 && color.r < 0.004 && color.g < 0.004 && color.b < 0.004) { - color = vec4(0.004, 0.004, 0.004, 1); - } else if (diff.r < 0.004 && diff.g < 0.004 && diff.b < 0.004) { - color = vec4(color.r - 0.004, color.g - 0.004, color.b - 0.004, color.a); + + out_Color = vec4(keyColor, 1.0); + if (firstPersonPass == 1) { + // unity like + vec2 sampleTexcCoord = fract(texCoordinates); + if (texCoordinates.x < 1.0 && texCoordinates.y < 1.0) { + // third person all + out_Color.rgb = texture(thirdPersonColor, sampleTexcCoord).rgb; + } else if (texCoordinates.y >= 1.0){ + // third person front + vec3 fragPos = getFragmentPosition(sampleTexcCoord); + if (dot(fragPos - hmdViewPosition, hmdPlaneNormal) >= 0.0) { + if (texCoordinates.x < 1.0) { + // color + out_Color.rgb = texture(thirdPersonColor, sampleTexcCoord).rgb; + if (alphaMode == 0) { + out_Color.rgb = avoidKeyColor(out_Color.rgb); + } + } else if (alphaMode == 1){ + // white mask + out_Color.rgb = vec3(1.0); + } } + } else if (texCoordinates.x >= 1.0 && texCoordinates.y < 1.0){ + // first person + out_Color.rgb = texture(firstPersonColor, sampleTexcCoord).rgb; } - out_Color = color; - // out_Color = vec4(vec3((distance(fragPos.xz,hmdViewPosition.xz)) / 3), 1); // Draw distance to hmd - // out_Color = texture(depthTex, pos); // Draw depth buffer } else { - discard;// Throw out the fragment to save some GPU processing - //out_Color = vec4(1, 0, 1, 1); + // side by side + vec2 sampleTexcCoord = fract(texCoordinates * vec2(1.0, 0.5)); + if (texCoordinates.x >= 1.0) { + // third person all + out_Color.rgb = texture(thirdPersonColor, sampleTexcCoord).rgb; + } else { + // third person front + vec3 fragPos = getFragmentPosition(sampleTexcCoord); + if (dot(fragPos - hmdViewPosition, hmdPlaneNormal) >= 0.0) { + // color + out_Color.rgb = avoidKeyColor(texture(thirdPersonColor, sampleTexcCoord).rgb); + } + } } } diff --git a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.json b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.json index a4ad6369e..b167a1dba 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.json +++ b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality.json @@ -9,10 +9,13 @@ "attributes": ["Position", "UV0"], "samplers": [ { - "name": "Sampler0" + "name": "thirdPersonColor" }, { - "name": "Sampler1" + "name": "thirdPersonDepth" + }, + { + "name": "firstPersonColor" } ], "uniforms": [ @@ -28,18 +31,6 @@ "count": 16, "values": [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] }, - { - "name": "resolution", - "type": "float", - "count": 2, - "values": [1.0, 1.0] - }, - { - "name": "position", - "type": "float", - "count": 2, - "values": [1.0, 1.0] - }, { "name": "hmdViewPosition", "type": "float", @@ -59,7 +50,7 @@ "values": [1.0, 1.0, 1.0] }, { - "name": "pass", + "name": "firstPersonPass", "type": "int", "count": 1, "values": [1] diff --git a/common/src/main/resources/assets/minecraft/shaders/core/passthrough.vsh b/common/src/main/resources/assets/minecraft/shaders/core/passthrough.vsh index 6fcd47fa7..aa5d758b8 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/passthrough.vsh +++ b/common/src/main/resources/assets/minecraft/shaders/core/passthrough.vsh @@ -7,6 +7,6 @@ in vec2 UV0; out vec2 texCoordinates; void main() { - gl_Position = projection * modelView * vec4(Position, 1.0); + gl_Position = vec4(Position, 1.0); texCoordinates = UV0; }