Skip to content

Commit

Permalink
Fixed mirrored cubes and children
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocelot5836 committed Jan 20, 2025
1 parent 7e75473 commit efe5137
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package foundry.veil.api.client.necromancer;

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import org.joml.Matrix3f;
import org.joml.Matrix4x3f;
import org.joml.Quaternionf;
import org.joml.Vector4f;
import org.joml.*;

import java.lang.Math;
import java.nio.ByteBuffer;
import java.util.*;

Expand Down Expand Up @@ -38,6 +36,7 @@ public void addBone(Bone part) {
}

public void buildRoots() {
this.maxDepth = 0;
for (Bone part : this.bones.values()) {
if (part.parent == null) {
this.roots.add(part);
Expand All @@ -54,8 +53,9 @@ public void buildRoots() {
parentBone = parentBone.parent;
}
Collections.reverse(part.parentChain);
this.maxDepth = Math.max(part.parentChain.size() + 1, this.maxDepth);
this.maxDepth = Math.max(part.parentChain.size(), this.maxDepth);
}
this.maxDepth++;
}

/**
Expand All @@ -75,12 +75,12 @@ public int getMaxDepth() {
* @param matrixStack
* @param orientationStack
*/
public void storeInstancedData(ByteBuffer buffer, Collection<Bone> bones, Object2IntMap<String> boneIds, int depth, Vector4f color, Matrix3f normalMatrix, Matrix4x3f[] matrixStack, Quaternionf[] orientationStack, float partialTicks) {
public void storeInstancedData(ByteBuffer buffer, Collection<Bone> bones, Object2IntMap<String> boneIds, int depth, Vector4f color, Matrix3f normalMatrix, Matrix4x3f baseTransform, Matrix4x3f[] matrixStack, Quaternionf[] orientationStack, float partialTicks) {
for (Bone bone : bones) {
int id = boneIds.getInt(bone.identifier);
boolean hasChildren = !bone.children.isEmpty();

Matrix4x3f matrix = matrixStack[depth];
Matrix4x3f matrix = matrixStack[depth].set(baseTransform);
Quaternionf orientation = orientationStack[depth];
if (id != -1 || hasChildren) {
bone.getLocalTransform(matrix, orientation, partialTicks);
Expand All @@ -89,16 +89,12 @@ public void storeInstancedData(ByteBuffer buffer, Collection<Bone> bones, Object
matrix.getTransposed(buffer.position() + id * UNIFORM_STRIDE, buffer);
bone.getColor(color, partialTicks);
color.get(buffer.position() + id * UNIFORM_STRIDE + 12 * Float.BYTES, buffer);
// Workaround for a JOML bug with get3x4
matrix.normal(normalMatrix).get3x4(buffer.position() + id * UNIFORM_STRIDE + 16 * Float.BYTES, buffer);
}
}

if (hasChildren) {
// Copy from current depth to the next
matrixStack[depth + 1].set(matrix);
orientationStack[depth + 1].set(orientation);
this.storeInstancedData(buffer, bones, boneIds, depth + 1, color, normalMatrix, matrixStack, orientationStack, partialTicks);
this.storeInstancedData(buffer, bone.children, boneIds, depth + 1, color, normalMatrix, matrix, matrixStack, orientationStack, partialTicks);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Skin(VertexArray vertexArray, Object2IntMap<String> boneIds) {
}

@ApiStatus.Internal
public void render(RenderType renderType, List<Matrix4f> transforms, List<Skeleton> skeletons, int instancedBuffer, ByteBufferBuilder boneBuilder, int boneBuffer, DynamicShaderBlock<?> boneBlock, FloatList partialTicks) {
public void render(RenderType renderType, List<Matrix4x3f> transforms, List<Skeleton> skeletons, int instancedBuffer, ByteBufferBuilder boneBuilder, int boneBuffer, DynamicShaderBlock<?> boneBlock, FloatList partialTicks) {
if (skeletons.isEmpty()) {
return;
}
Expand Down Expand Up @@ -97,11 +97,9 @@ public void render(RenderType renderType, List<Matrix4f> transforms, List<Skelet
Skeleton skeleton = skeletons.get(i);
for (int j = 0; j < maxDepth; j++) {
this.matrixStack[j].identity();
this.orientationStack[j].identity();
}
this.matrixStack[0].set(transforms.get(i));
buffer.position(i * skeletonDataSize);
skeleton.storeInstancedData(buffer, skeleton.roots, this.boneIds, 0, this.color, this.normalMatrix, this.matrixStack, this.orientationStack, partialTicks.getFloat(i));
skeleton.storeInstancedData(buffer, skeleton.roots, this.boneIds, 0, this.color, this.normalMatrix, transforms.get(i), this.matrixStack, this.orientationStack, partialTicks.getFloat(i));
}

buffer.rewind();
Expand Down Expand Up @@ -324,42 +322,44 @@ public Builder addCube(float xSize, float ySize, float zSize, float xOffset, flo
this.addVertex(minX, maxY, maxZ, northUStart / this.textureWidth, topVStart / this.textureHeight, 0.0F, 1.0F, 0.0F);
this.addVertex(maxX, maxY, maxZ, westUStart / this.textureWidth, topVStart / this.textureHeight, 0.0F, 1.0F, 0.0F);
this.addVertex(maxX, maxY, minZ, westUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, 1.0F, 0.0F);
this.addQuadIndices(this.nextIndex);

// Down
this.addVertex(maxX, minY, maxZ, westUStart / this.textureWidth, topVStart / this.textureHeight, 0.0F, -1.0F, 0.0F);
this.addVertex(minX, minY, maxZ, southUStart / this.textureWidth, topVStart / this.textureHeight, 0.0F, -1.0F, 0.0F);
this.addVertex(minX, minY, minZ, southUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, -1.0F, 0.0F);
this.addVertex(maxX, minY, minZ, westUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, -1.0F, 0.0F);
this.addQuadIndices(this.nextIndex);

// East
this.addVertex(maxX, minY, maxZ, eastUStart / this.textureWidth, sideVEnd / this.textureHeight, 1.0F, 0.0F, 0.0F);
this.addVertex(maxX, minY, minZ, northUStart / this.textureWidth, sideVEnd / this.textureHeight, 1.0F, 0.0F, 0.0F);
this.addVertex(maxX, maxY, minZ, northUStart / this.textureWidth, sideVStart / this.textureHeight, 1.0F, 0.0F, 0.0F);
this.addVertex(maxX, maxY, maxZ, eastUStart / this.textureWidth, sideVStart / this.textureHeight, 1.0F, 0.0F, 0.0F);
this.addQuadIndices(this.nextIndex);

// West
this.addVertex(minX, minY, minZ, westUStart / this.textureWidth, sideVEnd / this.textureHeight, -1.0F, 0.0F, 0.0F);
this.addVertex(minX, minY, maxZ, southUStart / this.textureWidth, sideVEnd / this.textureHeight, -1.0F, 0.0F, 0.0F);
this.addVertex(minX, maxY, maxZ, southUStart / this.textureWidth, sideVStart / this.textureHeight, -1.0F, 0.0F, 0.0F);
this.addVertex(minX, maxY, minZ, westUStart / this.textureWidth, sideVStart / this.textureHeight, -1.0F, 0.0F, 0.0F);
this.addQuadIndices(this.nextIndex);

// North
this.addVertex(maxX, minY, minZ, northUStart / this.textureWidth, sideVEnd / this.textureHeight, 0.0F, 0.0F, -1.0F);
this.addVertex(minX, minY, minZ, westUStart / this.textureWidth, sideVEnd / this.textureHeight, 0.0F, 0.0F, -1.0F);
this.addVertex(minX, maxY, minZ, westUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, 0.0F, -1.0F);
this.addVertex(maxX, maxY, minZ, northUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, 0.0F, -1.0F);
this.addQuadIndices(this.nextIndex);

// South
this.addVertex(minX, minY, maxZ, southUStart / this.textureWidth, sideVEnd / this.textureHeight, 0.0F, 0.0F, 1.0F);
this.addVertex(maxX, minY, maxZ, southUEnd / this.textureWidth, sideVEnd / this.textureHeight, 0.0F, 0.0F, 1.0F);
this.addVertex(maxX, maxY, maxZ, southUEnd / this.textureWidth, sideVStart / this.textureHeight, 0.0F, 0.0F, 1.0F);
this.addVertex(minX, maxY, maxZ, southUStart / this.textureWidth, sideVStart / this.textureHeight, 0.0F, 0.0F, 1.0F);
this.addQuadIndices(this.nextIndex);

for (int i = 0; i < 6; i++) {
if (mirrored) {
this.addMirroredQuadIndices(this.nextIndex);
} else {
this.addQuadIndices(this.nextIndex);
}
}

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.joml.Matrix4x3f;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

Expand Down Expand Up @@ -234,7 +235,7 @@ private static class SkeletonBatch {

private final RenderType renderType;
private final Skin skin;
private final List<Matrix4f> transforms;
private final List<Matrix4x3f> transforms;
private final List<Skeleton> skeletons;
private final FloatList partialTicks;

Expand All @@ -260,7 +261,7 @@ public void add(Matrix4fc transform, Skeleton skeleton, int overlay, int light,
this.instancedData.put((byte) g);
this.instancedData.put((byte) b);
this.instancedData.put((byte) a);
this.transforms.add(new Matrix4f(transform));
this.transforms.add(new Matrix4x3f().set(transform));
this.skeletons.add(skeleton);
this.partialTicks.add(partialTicks);
}
Expand Down

0 comments on commit efe5137

Please sign in to comment.