Skip to content

Commit

Permalink
The Trimming 2
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Aug 4, 2023
1 parent 1557f95 commit d124e09
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/net/coderbot/iris/vertices/NormalHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d124e09

Please sign in to comment.