diff --git a/src/main/java/net/coderbot/iris/mixin/entity_render_context/MixinHumanoidArmorLayer.java b/src/main/java/net/coderbot/iris/mixin/entity_render_context/MixinHumanoidArmorLayer.java index f92e9164fb..898a7f1b01 100644 --- a/src/main/java/net/coderbot/iris/mixin/entity_render_context/MixinHumanoidArmorLayer.java +++ b/src/main/java/net/coderbot/iris/mixin/entity_render_context/MixinHumanoidArmorLayer.java @@ -45,15 +45,15 @@ private void changeId(PoseStack pHumanoidArmorLayer0, MultiBufferSource pMultiBu private int backupValue = 0; @Inject(method = "renderTrim", at = @At(value = "HEAD"), locals = LocalCapture.CAPTURE_FAILHARD) - private void changeTrimTemp(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, boolean pBoolean5, A pHumanoidModel6, boolean pBoolean7, float pFloat8, float pFloat9, float pFloat10, CallbackInfo ci) { + private void changeTrimTemp(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, A pHumanoidModel5, boolean pBoolean6, CallbackInfo ci) { if (BlockRenderingSettings.INSTANCE.getItemIds() == null) return; backupValue = CapturedRenderingState.INSTANCE.getCurrentRenderedItem(); CapturedRenderingState.INSTANCE.setCurrentRenderedItem(BlockRenderingSettings.INSTANCE.getItemIds().applyAsInt(new NamespacedId("minecraft", "trim_" + pArmorTrim4.material().value().assetName()))); } - @Inject(method = "renderTrim", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE_FAILHARD) - private void changeTrimTemp2(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, boolean pBoolean5, A pHumanoidModel6, boolean pBoolean7, float pFloat8, float pFloat9, float pFloat10, CallbackInfo ci, TextureAtlasSprite lvTextureAtlasSprite12, VertexConsumer lvVertexConsumer13) { + @Inject(method = "renderTrim", at = @At(value = "TAIL")) + private void changeTrimTemp2(ArmorMaterial pHumanoidArmorLayer0, PoseStack pPoseStack1, MultiBufferSource pMultiBufferSource2, int pInt3, ArmorTrim pArmorTrim4, A pHumanoidModel5, boolean pBoolean6, CallbackInfo ci) { if (BlockRenderingSettings.INSTANCE.getItemIds() == null) return; CapturedRenderingState.INSTANCE.setCurrentRenderedItem(backupValue); backupValue = 0; diff --git a/src/main/java/net/coderbot/iris/vertices/NormalHelper.java b/src/main/java/net/coderbot/iris/vertices/NormalHelper.java index 5d16128984..44aca7fff9 100644 --- a/src/main/java/net/coderbot/iris/vertices/NormalHelper.java +++ b/src/main/java/net/coderbot/iris/vertices/NormalHelper.java @@ -381,6 +381,73 @@ public static int computeTangent(float normalX, float normalY, float normalZ, Tr return NormI8.pack(tangentx, tangenty, tangentz, tangentW); } + public static int computeTangent(float normalX, float normalY, float normalZ, float x0, float y0, float z0, float u0, float v0, + float x1, float y1, float z1, float u1, float v1, + float x2, float y2, float z2, float u2, float v2) { + float edge1x = x1 - x0; + float edge1y = y1 - y0; + float edge1z = z1 - z0; + + float edge2x = x2 - x0; + float edge2y = y2 - y0; + float edge2z = z2 - z0; + + float deltaU1 = u1 - u0; + float deltaV1 = v1 - v0; + float deltaU2 = u2 - u0; + float deltaV2 = v2 - v0; + + float fdenom = deltaU1 * deltaV2 - deltaU2 * deltaV1; + float f; + + if (fdenom == 0.0) { + f = 1.0f; + } else { + f = 1.0f / fdenom; + } + + float tangentx = f * (deltaV2 * edge1x - deltaV1 * edge2x); + float tangenty = f * (deltaV2 * edge1y - deltaV1 * edge2y); + float tangentz = f * (deltaV2 * edge1z - deltaV1 * edge2z); + float tcoeff = rsqrt(tangentx * tangentx + tangenty * tangenty + tangentz * tangentz); + tangentx *= tcoeff; + tangenty *= tcoeff; + tangentz *= tcoeff; + + float bitangentx = f * (-deltaU2 * edge1x + deltaU1 * edge2x); + float bitangenty = f * (-deltaU2 * edge1y + deltaU1 * edge2y); + float bitangentz = f * (-deltaU2 * edge1z + deltaU1 * edge2z); + float bitcoeff = rsqrt(bitangentx * bitangentx + bitangenty * bitangenty + bitangentz * bitangentz); + bitangentx *= bitcoeff; + bitangenty *= bitcoeff; + bitangentz *= bitcoeff; + + // predicted bitangent = tangent × normal + // Compute the determinant of the following matrix to get the cross product + // i j k + // tx ty tz + // nx ny nz + + // Be very careful when writing out complex multi-step calculations + // such as vector cross products! The calculation for pbitangentz + // used to be broken because it multiplied values in the wrong order. + + float pbitangentx = tangenty * normalZ - tangentz * normalY; + float pbitangenty = tangentz * normalX - tangentx * normalZ; + float pbitangentz = tangentx * normalY - tangenty * normalX; + + float dot = (bitangentx * pbitangentx) + (bitangenty * pbitangenty) + (bitangentz * pbitangentz); + float tangentW; + + if (dot < 0) { + tangentW = -1.0F; + } else { + tangentW = 1.0F; + } + + return NormI8.pack(tangentx, tangenty, tangentz, tangentW); + } + private static float rsqrt(float value) { if (value == 0.0f) { // You heard it here first, folks: 1 divided by 0 equals 1 diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/GlyphToTerrainVertexSerializer.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/GlyphToTerrainVertexSerializer.java index 44c4ab0530..59d3a414e8 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/GlyphToTerrainVertexSerializer.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/GlyphToTerrainVertexSerializer.java @@ -5,6 +5,7 @@ import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.QuadViewEntity; import net.coderbot.iris.uniforms.CapturedRenderingState; import net.coderbot.iris.vertices.IrisVertexFormats; +import net.coderbot.iris.vertices.NormI8; import net.coderbot.iris.vertices.NormalHelper; import net.minecraft.client.renderer.texture.OverlayTexture; import org.joml.Vector3f; @@ -71,7 +72,7 @@ private static void endQuad(float uSum, float vSum, long src, long dst) { normalX = saveNormal.x; normalY = saveNormal.y; normalZ = saveNormal.z; - int normal = NormalHelper.packNormal(saveNormal, 0.0F); + int normal = NormI8.pack(saveNormal); int tangent = NormalHelper.computeTangent(normalX, normalY, normalZ, quad); diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/entity_xhfp/GlyphVertexExt.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/entity_xhfp/GlyphVertexExt.java index 06045d7941..3a98da0dee 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/entity_xhfp/GlyphVertexExt.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/impl/vertex_format/entity_xhfp/GlyphVertexExt.java @@ -8,7 +8,6 @@ import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatRegistry; import net.coderbot.iris.uniforms.CapturedRenderingState; import net.coderbot.iris.vertices.IrisVertexFormats; -import net.coderbot.iris.vertices.NormI8; import net.coderbot.iris.vertices.NormalHelper; import net.minecraft.client.renderer.texture.OverlayTexture; import org.joml.Matrix3f; diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/copyEntity/shadows/EntityRenderDispatcherMixin.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/copyEntity/shadows/EntityRenderDispatcherMixin.java index 96cdedce93..b7add70524 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/copyEntity/shadows/EntityRenderDispatcherMixin.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/copyEntity/shadows/EntityRenderDispatcherMixin.java @@ -2,13 +2,13 @@ import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; -import net.caffeinemc.mods.sodium.api.util.NormI8; import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex; import net.caffeinemc.mods.sodium.api.util.ColorABGR; import net.caffeinemc.mods.sodium.api.math.MatrixHelper; import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.EntityVertex; import net.coderbot.iris.vertices.ImmediateState; +import net.coderbot.iris.vertices.NormI8; import net.coderbot.iris.vertices.NormalHelper; import net.irisshaders.iris.api.v0.IrisApi; import net.minecraft.client.renderer.LightTexture; @@ -238,7 +238,7 @@ private static int getTangent(int normal, float x0, float y0, float z0, float u0 tangentW = 1.0F; } - return NormalHelper.packNormal(tangentx, tangenty, tangentz, tangentW); + return NormI8.pack(tangentx, tangenty, tangentz, tangentW); } private static float rsqrt(float value) { diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/fast_render/MixinModelPart.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/fast_render/MixinModelPart.java index 65c3d4cc43..4d833e7d34 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/fast_render/MixinModelPart.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/fast_render/MixinModelPart.java @@ -5,13 +5,11 @@ import me.jellysquid.mods.sodium.client.model.ModelCuboidAccessor; import me.jellysquid.mods.sodium.client.render.immediate.model.ModelCuboid; import net.caffeinemc.mods.sodium.api.util.ColorABGR; -import net.caffeinemc.mods.sodium.api.util.NormI8; import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex; import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.EntityVertex; import net.coderbot.iris.vertices.ImmediateState; import net.coderbot.iris.vertices.NormI8; -import net.coderbot.iris.vertices.NormalHelper; import net.irisshaders.iris.api.v0.IrisApi; import net.minecraft.client.model.geom.ModelPart; import org.lwjgl.system.MemoryStack; diff --git a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/entity/MixinEntityRenderDispatcher.java b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/entity/MixinEntityRenderDispatcher.java index 0ec5ce5462..f100803fd9 100644 --- a/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/entity/MixinEntityRenderDispatcher.java +++ b/src/sodiumCompatibility/java/net/coderbot/iris/compat/sodium/mixin/vertex_format/entity/MixinEntityRenderDispatcher.java @@ -4,20 +4,16 @@ import com.mojang.blaze3d.vertex.VertexConsumer; import net.caffeinemc.mods.sodium.api.math.MatrixHelper; import net.caffeinemc.mods.sodium.api.util.ColorABGR; -import net.caffeinemc.mods.sodium.api.util.NormI8; import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter; import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex; import net.coderbot.iris.compat.sodium.impl.vertex_format.entity_xhfp.EntityVertex; import net.coderbot.iris.vertices.ImmediateState; import net.coderbot.iris.vertices.NormI8; -import net.coderbot.iris.vertices.NormalHelper; import net.irisshaders.iris.api.v0.IrisApi; import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.entity.EntityRenderDispatcher; import net.minecraft.client.renderer.texture.OverlayTexture; import org.joml.Matrix4f; -import org.joml.Vector3f; -import org.joml.Vector3fc; import org.lwjgl.system.MemoryStack; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite;