Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public abstract class AnimationController implements IAnimation {
public static KeyframeLocation EMPTY_SCALE_KEYFRAME_LOCATION = new KeyframeLocation(new Keyframe(0, Collections.singletonList(FloatExpression.ONE), Collections.singletonList(FloatExpression.ONE)), 0);

protected final AnimationStateHandler stateHandler;
protected final Map<String, Vec3f> bonePositions;
protected final Map<String, AdvancedPlayerAnimBone> bones = new Object2ObjectOpenHashMap<>();
protected final Map<String, PlayerAnimBone> activeBones = new Object2ObjectOpenHashMap<>();
protected final Map<String, CustomBone> pivotBones = new Object2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -111,12 +110,10 @@ public abstract class AnimationController implements IAnimation {
* Instantiates a new {@code AnimationController}
*
* @param animationHandler The {@link AnimationStateHandler} animation state handler responsible for deciding which animations to play
* @param bonePositions Map of bones and their pivots
* @param molangRuntime A function that provides the MoLang runtime engine for this animation controller when applied
*/
public AnimationController(AnimationStateHandler animationHandler, Map<String, Vec3f> bonePositions, Function<AnimationController, MochaEngine<AnimationController>> molangRuntime) {
public AnimationController(AnimationStateHandler animationHandler, Function<AnimationController, MochaEngine<AnimationController>> molangRuntime) {
this.stateHandler = animationHandler;
this.bonePositions = bonePositions;
this.molangRuntime = molangRuntime.apply(this);

registerBones();
Expand Down Expand Up @@ -540,9 +537,6 @@ private void processCurrentAnimation(float adjustedTick, AnimationData animation
for (AdvancedPlayerAnimBone bone : this.bones.values()) {
bone.setToInitialPose();
}
for (PlayerAnimBone bone : this.pivotBones.values()) {
bone.setToInitialPose();
}

return;
} else {
Expand All @@ -560,9 +554,6 @@ private void processCurrentAnimation(float adjustedTick, AnimationData animation
for (PlayerAnimBone bone : this.bones.values()) {
bone.setToInitialPose();
}
for (PlayerAnimBone bone : this.pivotBones.values()) {
bone.setToInitialPose();
}

for (Map.Entry<String, BoneAnimation> entry : animation.boneAnimations().entrySet()) {
PlayerAnimBone bone = this.bones.getOrDefault(entry.getKey(), null);
Expand Down Expand Up @@ -649,7 +640,7 @@ private void processBoneHierarchy(PlayerAnimBone bone, Map<String, String> paren
processBoneHierarchy(parent, parentsMap, processedBones);

this.activeBones.put(boneName, bone);
MatrixUtil.applyParentsToChild(bone, Collections.singletonList(parent), this::getBonePosition);
MatrixUtil.applyParentsToChild(bone, Collections.singletonList(parent), _ -> null);

processedBones.add(boneName);
}
Expand Down Expand Up @@ -927,12 +918,6 @@ public void setupAnim(AnimationData state) {
else process(state);
}

public Vec3f getBonePosition(String name) {
if (bonePositions.containsKey(name)) return bonePositions.get(name);
if (pivotBones.containsKey(name)) return pivotBones.get(name).getPivot();
return Vec3f.ZERO;
}

/**
* PLEASE DON'T USE THIS UNLESS YOU KNOW WHAT YOU'RE DOING.
* THE {@link AnimationController#linkModifiers()} METHOD MUST BE CALLED EVERYTIME ANYTHING IN THE MODIFIER LIST IS CHANGED.
Expand Down Expand Up @@ -1001,8 +986,8 @@ protected void linkModifiers() {
}
}

public AdvancedPlayerAnimBone registerPlayerAnimBone(String name) {
return registerPlayerAnimBone(new AdvancedPlayerAnimBone(name));
public AdvancedPlayerAnimBone registerPlayerAnimBone(String name, Vec3f pivot) {
return registerPlayerAnimBone(new AdvancedPlayerAnimBone(name, pivot));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,7 @@ public abstract class HumanoidAnimationController extends AnimationController {
* @param molangRuntime A function that provides the MoLang runtime engine for this animation controller when applied
*/
public HumanoidAnimationController(AnimationStateHandler animationHandler, Function<AnimationController, MochaEngine<AnimationController>> molangRuntime) {
this(animationHandler, BONE_POSITIONS, molangRuntime);
}

/**
* Instantiates a new {@code AnimationController}
*
* @param animationHandler The {@link AnimationStateHandler} animation state handler responsible for deciding which animations to play
* @param bonePositions Map of bones and their pivots
* @param molangRuntime A function that provides the MoLang runtime engine for this animation controller when applied
*/
public HumanoidAnimationController(AnimationStateHandler animationHandler, Map<String, Vec3f> bonePositions, Function<AnimationController, MochaEngine<AnimationController>> molangRuntime) {
super(animationHandler, bonePositions, molangRuntime);
super(animationHandler, molangRuntime);
}

@Override
Expand All @@ -103,6 +92,10 @@ public void registerTopPlayerAnimBone(String name) {
this.registerPlayerAnimBone(name);
}

public void registerPlayerAnimBone(String name) {
this.registerPlayerAnimBone(name, BONE_POSITIONS.getOrDefault(name, Vec3f.ZERO));
}

@Override
public void process(AnimationData state) {
super.process(state);
Expand All @@ -122,7 +115,7 @@ public PlayerAnimBone get3DTransformRaw(@NotNull PlayerAnimBone bone) {
bone = super.get3DTransformRaw(bone);
String name = bone.getName();
if (this.torsoBendSign != 0 && this.top_bones.contains(name)) {
float offset = getBonePosition(name).y() - 18;
float offset = BONE_POSITIONS.get(name).y() - 18;
bone.rotation.x += this.torsoBend;
bone.position.x += (offset * this.torsoBendZPosMultiplier - offset) * this.torsoBendSign;
bone.position.y += offset * this.torsoBendYPosMultiplier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.zigythebird.playeranimcore.bones;

public class AdvancedPlayerAnimBone extends ToggleablePlayerAnimBone {
import com.zigythebird.playeranimcore.math.Vec3f;

public class AdvancedPlayerAnimBone extends ToggleablePlayerAnimBone implements IPivotBone {
public Vec3f pivot = null;

public Float scaleXTransitionLength = null;
public Float scaleYTransitionLength = null;
public Float scaleZTransitionLength = null;
Expand All @@ -15,12 +19,15 @@ public class AdvancedPlayerAnimBone extends ToggleablePlayerAnimBone {

public Float bendTransitionLength = null;

public AdvancedPlayerAnimBone(String name) {
public AdvancedPlayerAnimBone(String name, Vec3f pivot) {
super(name);
this.pivot = pivot;
}

public AdvancedPlayerAnimBone(PlayerAnimBone bone) {
super(bone);
if (bone instanceof IPivotBone pivotBone)
this.pivot = pivotBone.getPivot();
}

public void setEnabled(boolean enabled) {
Expand Down Expand Up @@ -78,4 +85,9 @@ public void setScaleYTransitionLength(Float scaleYTransitionLength) {
public void setScaleXTransitionLength(Float scaleXTransitionLength) {
this.scaleXTransitionLength = scaleXTransitionLength;
}

@Override
public Vec3f getPivot() {
return this.pivot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zigythebird.playeranimcore.bones;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid one more abstraction

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will look ugly
I will have to do two instance of checks
I don't see the harm in abstraction, but if you still insist I will avoid it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clearly no worse than now


import com.zigythebird.playeranimcore.math.Vec3f;

public interface IPivotBone {
Vec3f getPivot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static EasingType easingTypeFromString(String string) {
return easingType;
}

private static final Map<String, Vec3f> DEFAULT_VALUES = Map.of(
public static final Map<String, Vec3f> DEFAULT_VALUES = Map.of(
"right_arm", new Vec3f(-5, 2, 0),
"left_arm", new Vec3f(5, 2, 0),
"left_leg", new Vec3f(1.9f, 12, 0.1f),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zigythebird.playeranimcore.util;

import com.zigythebird.playeranimcore.bones.CustomBone;
import com.zigythebird.playeranimcore.bones.IPivotBone;
import com.zigythebird.playeranimcore.bones.PlayerAnimBone;
import com.zigythebird.playeranimcore.math.Vec3f;
import org.joml.Matrix4f;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static void applyParentsToChild(PlayerAnimBone child, Iterable<? extends
Matrix4f matrix = new Matrix4f();

for (PlayerAnimBone parent : parents) {
Vec3f pivot = parent instanceof CustomBone pivotBone ? pivotBone.getPivot() : positions.apply(parent.getName());
Vec3f pivot = parent instanceof IPivotBone pivotBone ? pivotBone.getPivot() : positions.apply(parent.getName());
MatrixUtil.prepMatrixForBone(matrix, parent, pivot);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package com.zigythebird.playeranim.animation;

import com.google.gson.JsonArray;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import com.zigythebird.playeranim.PlayerAnimLibMod;
import com.zigythebird.playeranim.util.RenderUtil;
import com.zigythebird.playeranimcore.PlayerAnimLib;
import com.zigythebird.playeranimcore.animation.AnimationController;
import com.zigythebird.playeranimcore.animation.HumanoidAnimationController;
import com.zigythebird.playeranimcore.animation.layered.modifier.AbstractFadeModifier;
import com.zigythebird.playeranimcore.bindings.PlatformModel;
import com.zigythebird.playeranimcore.bones.PlayerAnimBone;
import com.zigythebird.playeranimcore.math.Vec3f;
import com.zigythebird.playeranimcore.molang.MolangLoader;
import net.minecraft.resources.Identifier;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Avatar;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import team.unnamed.mocha.MochaEngine;
Expand Down Expand Up @@ -77,24 +70,6 @@ public boolean replaceAnimationWithFade(@NotNull AbstractFadeModifier fadeModifi
return replaceAnimationWithFade(fadeModifier, newAnimation, true);
}

/**
* Get the position of a bone in the world in the form of a PoseStack.
*/
public @Nullable PoseStack getBoneWorldPositionPoseStack(String name, float tickDelta, Vec3 cameraPos) {
if (!this.activeBones.containsKey(name)) return null;
return getBoneWorldPositionPoseStack(this.activeBones.get(name), tickDelta, cameraPos);
}

public @NotNull PoseStack getBoneWorldPositionPoseStack(PlayerAnimBone bone, float tickDelta, Vec3 cameraPos) {
PoseStack poseStack = new PoseStack();
Vec3f pivot = getBonePosition(bone.getName());
Vec3 position = avatar.getPosition(tickDelta).subtract(cameraPos).add(pivot.x(), pivot.y(), pivot.z());
poseStack.translate(position.x(), position.y(), position.z());
poseStack.mulPose(Axis.YP.rotationDegrees(180 - Mth.lerp(tickDelta, avatar.yBodyRotO, avatar.yBodyRot)));
RenderUtil.translateMatrixToBone(poseStack, bone);
return poseStack;
}

@Override
protected @Nullable PlatformModel loadCustomModel(@Nullable String texture, @Nullable JsonArray elements) {
if (elements == null || elements.isEmpty()) return null;
Expand Down