Skip to content

Commit

Permalink
Transform Parenting
Browse files Browse the repository at this point in the history
- Improved local rotations and translations done in MutablePartPose
- Changed how the first person item in hand is parented to display the potential of this new feature (think abstract bones for extra parent transforms with fewer rotation order problems)
  • Loading branch information
Trainguy9512 committed Oct 11, 2023
1 parent b0a72fa commit 32d978d
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 36 deletions.
@@ -1,19 +1,23 @@
package com.trainguy9512.animationoverhaul.animation.entity;

import com.trainguy9512.animationoverhaul.AnimationOverhaulMain;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.BakedAnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.MutablePartPose;
import com.trainguy9512.animationoverhaul.animation.pose.sample.*;
import com.trainguy9512.animationoverhaul.util.animation.LocatorSkeleton;
import com.trainguy9512.animationoverhaul.util.data.AnimationDataContainer;
import com.trainguy9512.animationoverhaul.util.data.TimelineGroupData;
import com.trainguy9512.animationoverhaul.util.time.TickTimeUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import org.joml.Vector3f;

import java.util.List;

Expand Down Expand Up @@ -166,11 +170,16 @@ public FirstPersonPlayerAnimator(){

@Override
protected LocatorSkeleton<FPPlayerLocators> buildRig() {
return LocatorSkeleton.of(FPPlayerLocators.values())
return LocatorSkeleton.of(FPPlayerLocators.root)
.addChildLocator(FPPlayerLocators.camera)
.addChildLocator(FPPlayerLocators.leftArm)
.addChildLocator(FPPlayerLocators.rightArm)
.addChildLocator(FPPlayerLocators.leftArm, FPPlayerLocators.leftHand)
.addChildLocator(FPPlayerLocators.rightArm, FPPlayerLocators.rightHand)
.setLocatorDefaultPose(FPPlayerLocators.leftHand, PartPose.offsetAndRotation(-1, -2, -8, 0, 0, 0))
.setLocatorDefaultPose(FPPlayerLocators.rightHand, PartPose.offsetAndRotation(1, 2, -8, Mth.HALF_PI, 0, Mth.PI))
.setLocatorMirror(FPPlayerLocators.rightArm, FPPlayerLocators.leftArm)
.setLocatorMirror(FPPlayerLocators.leftArm, FPPlayerLocators.rightArm)
.setLocatorMirror(FPPlayerLocators.rightHand, FPPlayerLocators.leftHand)
.setLocatorMirror(FPPlayerLocators.leftHand, FPPlayerLocators.rightHand);
.setLocatorMirror(FPPlayerLocators.rightHand, FPPlayerLocators.leftHand);
}

@Override
Expand All @@ -191,6 +200,9 @@ protected AnimationPose<FPPlayerLocators> calculatePose() {
AnimationPose<FPPlayerLocators> pose = sampleAnimationState(MAINHAND_ITEMSWITCH_STATE_MACHINE);
pose = pose.getSelectedByLocators(getCameraPose(), List.of(FPPlayerLocators.camera));




return pose;
}

Expand Down Expand Up @@ -271,6 +283,7 @@ private AnimationPose<FPPlayerLocators> getStaticMainBasicItemPose(){

public void tick(LivingEntity livingEntity, AnimationDataContainer entityAnimationData){


// Tick the main hand lower/empty sequence players based on active states
getAnimationState(MAIN_EMPTY_LOWER_SEQUENCE_PLAYER).playFromStartOnStateActive(getAnimationState(MAINHAND_ITEMSWITCH_STATE_MACHINE),
ItemSwitchStates.LOWERING);
Expand Down Expand Up @@ -381,6 +394,10 @@ public void tickExternal(){
}
animationPose.applyDefaultPoseOffset();

AnimationOverhaulMain.LOGGER.info(animationPose.getLocatorPose(FPPlayerLocators.rightHand).getEulerRotation());



this.localBakedPose.setPose(animationPose.getCopy());
}

Expand Down
Expand Up @@ -68,7 +68,14 @@ public PlayerPartAnimator(){
// Building the locator rig
@Override
protected LocatorSkeleton<ModelPartLocators> buildRig() {
return LocatorSkeleton.of(ModelPartLocators.values())
return LocatorSkeleton.of(ModelPartLocators.root)
.addChildLocator(ModelPartLocators.body)
.addChildLocator(ModelPartLocators.cape)
.addChildLocator(ModelPartLocators.leftArm)
.addChildLocator(ModelPartLocators.rightArm)
.addChildLocator(ModelPartLocators.leftLeg)
.addChildLocator(ModelPartLocators.rightLeg)
.addChildLocator(ModelPartLocators.head)
.setLocatorModelPart(ModelPartLocators.head, MODEL_PART_HEAD)
.setLocatorModelPart(ModelPartLocators.leftArm, MODEL_PART_LEFT_ARM)
.setLocatorModelPart(ModelPartLocators.rightArm, MODEL_PART_RIGHT_ARM)
Expand Down Expand Up @@ -108,7 +115,7 @@ public void tick(LivingEntity livingEntity, AnimationDataContainer entityAnimati
getAnimationState(TEST_STATE_MACHINE)
.setTransitionCondition(TestStates.IDLE, TestStates.WALKING, isWalking)
.setTransitionCondition(TestStates.WALKING, TestStates.IDLE, !isWalking);
AnimationOverhaulMain.LOGGER.info(this.getAnimationState(ANIM_TEST_IDLE_SEQUENCE_PLAYER).getPlayRate());
//AnimationOverhaulMain.LOGGER.info(this.getAnimationState(ANIM_TEST_IDLE_SEQUENCE_PLAYER).getPlayRate());

}

Expand Down
@@ -1,10 +1,16 @@
package com.trainguy9512.animationoverhaul.animation.pose;

import com.google.common.collect.Maps;
import com.mojang.blaze3d.vertex.PoseStack;
import com.trainguy9512.animationoverhaul.util.animation.LocatorSkeleton;
import com.trainguy9512.animationoverhaul.util.time.Easing;
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -61,6 +67,48 @@ public void subtractPose(AnimationPose animationPose){
*/

public AnimationPose<L> getHierarchyBakedPose(){
AnimationPose<L> animationPose = this.getCopy();
animationPose.transformChildren(this.getSkeleton().getRootLocator());
return animationPose;
}

private void transformChildren(Enum<L> parent){
MutablePartPose parentPose = this.getLocatorPose(parent);

for (Enum<L> child : this.getSkeleton().getLocatorChildren(parent)){
MutablePartPose childPose = this.getLocatorPose(child).getCopy();

/*
PoseStack poseStack = new PoseStack();
parentPose.transformPoseStack(poseStack, 1);
poseStack.pushPose();
childPose.transformPoseStack(poseStack, 1);
Matrix4f pose = poseStack.last().pose();
Quaternionf rotation = pose.getNormalizedRotation(new Quaternionf());
this.setLocatorPose(child, MutablePartPose.fromTranslationAndRotation(
pose.m30(),
pose.m31(),
pose.m32(),
rotation
));
poseStack.popPose();
*/


this.setLocatorPose(child, parentPose
.getCopy()
.translate(childPose.getTranslation(), true)
.rotate(childPose.getRotation(), true)
);

transformChildren(child);
}
}

public void blend(AnimationPose<L> animationPose, float alpha, Easing easing){
for(Enum<L> locator : this.getSkeleton().getLocators()){
MutablePartPose mutablePartPoseA = this.getLocatorPose(locator);
Expand Down
Expand Up @@ -25,7 +25,7 @@ public void setPose(AnimationPose<L> animationPose){
public AnimationPose<L> getBlendedPose(float partialTicks){
// uncomment this for debugging
//partialTicks = 0;
return this.poseOld.getBlendedLinear(this.pose, partialTicks);
return this.poseOld.getBlendedLinear(this.pose, partialTicks).getHierarchyBakedPose();
}

public void bakeToModelParts(ModelPart rootModelPart, float partialTicks){
Expand Down
Expand Up @@ -17,8 +17,7 @@
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Rotation;
import org.joml.*;

import java.lang.Math;
import org.joml.Math;

public class MutablePartPose {
public float x = 0;
Expand Down Expand Up @@ -62,6 +61,10 @@ public static MutablePartPose fromTranslationAndRotation(float x, float y, float
return new MutablePartPose(x, y, z, xRot, yRot, zRot);
}

public static MutablePartPose fromTranslationAndRotation(float x, float y, float z, Quaternionf rotation){
return new MutablePartPose(x, y, z, rotation);
}

/*
public static Quaternionf getQuaternionFromEuler(Vector3f eulerRotation){
return new Quaternionf().rotationXYZ(eulerRotation.x(), eulerRotation.y(), eulerRotation.z());
Expand Down Expand Up @@ -105,7 +108,12 @@ public PartPose asPartPose(){
public MutablePartPose translate(Vector3f translation, boolean localSpace){
if(translation.x() != 0 || translation.y() != 0 || translation.z() != 0){
if(localSpace){
translation.rotate(this.rotation);
translation.rotateX(this.getEulerRotation().x());
translation.rotateY(this.getEulerRotation().y());
translation.rotateZ(this.getEulerRotation().z());


//translation.rotate(this.rotation);
}
this.x += translation.x();
this.y += translation.y();
Expand All @@ -116,10 +124,55 @@ public MutablePartPose translate(Vector3f translation, boolean localSpace){

public MutablePartPose rotate(Quaternionf rotation, boolean localSpace){
if(localSpace){

/*
Vector3f eulerRotation = this.getEulerRotation();
rotation.rotateX(eulerRotation.x());
rotation.rotateY(eulerRotation.y());
rotation.rotateZ(eulerRotation.z());
*/




/*
Vector3f eulerRotation = rotation.getEulerAnglesXYZ(new Vector3f());
PoseStack poseStack = new PoseStack();
poseStack.pushPose();
poseStack.setIdentity();
poseStack.mulPose(this.rotation);
poseStack.pushPose();
poseStack.mulPose(rotation);
this.rotation = poseStack.last().pose().getNormalizedRotation(new Quaternionf());
poseStack.popPose();
poseStack.popPose();
*/




Quaternionf newRotation = new Quaternionf(rotation.x(), rotation.y(), rotation.z(), rotation.w());
Quaternionf oldRotation = new Quaternionf(this.rotation.x(), this.rotation.y(), this.rotation.z(), this.rotation.w());
this.rotation = newRotation.mul(oldRotation);




/*
Vector3f rotationOriginal = this.getEulerRotation();
Vector3f rotationAdded = rotation.getEulerAnglesXYZ(new Vector3f());
rotationOriginal.add(rotationAdded);
this.setEulerRotation(rotationOriginal);
*/
} else {
this.rotation.mul(rotation);

Expand Down
Expand Up @@ -68,7 +68,7 @@ private void overwriteItemInHandRendering(float f, PoseStack poseStack, MultiBuf

playerModel.rightArm.render(poseStack, bufferSource.getBuffer(RenderType.entitySolid(abstractClientPlayer.getSkin().texture())), i, OverlayTexture.NO_OVERLAY);
playerModel.rightSleeve.render(poseStack, bufferSource.getBuffer(RenderType.entityTranslucent(abstractClientPlayer.getSkin().texture())), i, OverlayTexture.NO_OVERLAY);
//playerModel.leftArm.render(poseStack, bufferSource.getBuffer(RenderType.entitySolid(abstractClientPlayer.getSkinTextureLocation())), i, OverlayTexture.NO_OVERLAY);
//playerModel.leftArm.render(poseStack, bufferSource.getBuffer(RenderType.entitySolid(abstractClientPlayer.getSkin().texture())), i, OverlayTexture.NO_OVERLAY);
//playerModel.leftSleeve.render(poseStack, bufferSource.getBuffer(RenderType.entityTranslucent(abstractClientPlayer.getSkinTextureLocation())), i, OverlayTexture.NO_OVERLAY);

/*
Expand Down Expand Up @@ -106,13 +106,13 @@ private void renderItemInHand(AbstractClientPlayer abstractClientPlayer, ItemSta


poseStack.pushPose();
armPose.transformPoseStack(poseStack);
poseStack.translate((humanoidArm == HumanoidArm.LEFT ? 1 : -1) /16F, 9/16F, 0);
//armPose.transformPoseStack(poseStack);
//poseStack.translate((humanoidArm == HumanoidArm.LEFT ? 1 : -1) /16F, 9/16F, 0);
handPose.transformPoseStack(poseStack);

poseStack.mulPose(Axis.XP.rotationDegrees(-90.0f));
poseStack.mulPose(Axis.YP.rotationDegrees(180.0f));
poseStack.translate(0F, 2F/16F, -1F/16F);
//poseStack.mulPose(Axis.XP.rotationDegrees(-90.0f));
//poseStack.mulPose(Axis.YP.rotationDegrees(180.0f));
//poseStack.translate(0F, 2F/16F, -1F/16F);
this.renderItem(abstractClientPlayer, itemStack, humanoidArm == HumanoidArm.LEFT ? ItemDisplayContext.THIRD_PERSON_LEFT_HAND : ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, humanoidArm == HumanoidArm.LEFT, poseStack, multiBufferSource, i);
//this.renderItem(abstractClientPlayer, itemStack, humanoidArm == HumanoidArm.LEFT ? ItemDisplayContext.THIRD_PERSON_LEFT_HAND : ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, humanoidArm == HumanoidArm.LEFT, poseStack, multiBufferSource, i);
poseStack.popPose();
Expand Down

0 comments on commit 32d978d

Please sign in to comment.