Skip to content

Commit

Permalink
improved locator support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalbrc committed Apr 9, 2024
1 parent 8ade777 commit 15494ca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/main/java/de/tomalbrc/bil/file/bbmodel/BbElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BbElement {
public int color;
public float inflate;
public Vector3f origin;
public Vector3f position; // used by locators
public Object2ObjectOpenHashMap<String, BbFace> faces;
public ElementType type;
public UUID uuid;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/tomalbrc/bil/file/extra/BbModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public static BbOutliner findParent(BbModel model, BbOutliner x, BbElement eleme
return null;
}

public static List<BbElement> elementsForOutliner(BbModel model, BbOutliner outliner) {
public static List<BbElement> elementsForOutliner(BbModel model, BbOutliner outliner, BbElement.ElementType elementType) {
List<BbElement> elements = new ObjectArrayList<>();
for (BbElement element: model.elements) {
if (outliner.hasUuidChild(element.uuid) && element.type == BbElement.ElementType.CUBE) {
if (outliner.hasUuidChild(element.uuid) && element.type == elementType) {
elements.add(element);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected Reference2ObjectOpenHashMap<UUID, Variant> variants() {
}

if (!outliner.isHitbox() && affected) {
List<BbElement> elements = BbModelUtils.elementsForOutliner(model, outliner);
List<BbElement> elements = BbModelUtils.elementsForOutliner(model, outliner, BbElement.ElementType.CUBE);

Int2ObjectOpenHashMap<BbTexture> textureMap = new Int2ObjectOpenHashMap<>();
if (variant.textureMap() == null || variant.textureMap().isEmpty()) {
Expand Down
52 changes: 31 additions & 21 deletions src/main/java/de/tomalbrc/bil/file/importer/BbModelImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected Object2ObjectOpenHashMap<UUID, Node> makeNodeMap() {
}

protected PolymerModelData generateModel(BbOutliner outliner) {
List<BbElement> elements = BbModelUtils.elementsForOutliner(model, outliner);
List<BbElement> elements = BbModelUtils.elementsForOutliner(model, outliner, BbElement.ElementType.CUBE);

ResourcePackItemModel.Builder builder = new ResourcePackItemModel.Builder(model.modelIdentifier)
.withTextures(this.makeDefaultTextureMap())
Expand Down Expand Up @@ -89,6 +89,18 @@ protected void createBones(Node parent, BbOutliner parentOutliner, Collection<Bb
Node node = new Node(Node.NodeType.BONE, parent, tr, outliner.name, outliner.uuid, modelData);
nodeMap.put(outliner.uuid, node);

List<BbElement> locatorElements = BbModelUtils.elementsForOutliner(model, outliner, BbElement.ElementType.LOCATOR);
for (BbElement locator : locatorElements) {
Vector3f localPos2 = locator.position.sub(outliner.origin, new Vector3f());

var locatorTransform = new Node.Transform(localPos2.div(16), createQuaternion(locator.rotation), 1);
locatorTransform.mul(node.transform());

Node locatorNode = new Node(Node.NodeType.LOCATOR, node, locatorTransform, locator.name, locator.uuid, null);
nodeMap.put(locator.uuid, locatorNode);
}


// children
createBones(node, outliner, outliner.children, nodeMap);
}
Expand Down Expand Up @@ -133,32 +145,30 @@ protected Reference2ObjectOpenHashMap<UUID, Pose> poses(BbAnimation animation, O
Reference2ObjectOpenHashMap<UUID, Pose> poses = new Reference2ObjectOpenHashMap<>();

for (var entry: nodeMap.entrySet()) {
if (entry.getValue().modelData() != null) {
Matrix4f matrix4f = new Matrix4f().rotateY(Mth.PI);
boolean requiresFrame = time == 0;
List<Node> nodePath = nodePath(entry.getValue());

for (var node : nodePath) {
BbAnimator animator = animation.animators.get(node.uuid());
requiresFrame |= animator != null;
Matrix4f matrix4f = new Matrix4f().rotateY(Mth.PI);
boolean requiresFrame = time == 0;
List<Node> nodePath = nodePath(entry.getValue());

Vector3fc origin = node.transform().origin();
for (var node : nodePath) {
BbAnimator animator = animation.animators.get(node.uuid());
requiresFrame |= animator != null;

var triple = animator == null ?
Triple.of(new Vector3f(), new Vector3f(), new Vector3f(1.f)) :
Sampler.sample(animator.keyframes, model.animationVariablePlaceholders, environment, time);
Vector3fc origin = node.transform().origin();

Quaternionf localRot = node.transform().rotation().mul(createQuaternion(triple.getMiddle().mul(-1, -1, 1)), new Quaternionf());
Vector3f localPos = triple.getLeft().mul(-1,1,1).div(16).add(origin);
var triple = animator == null ?
Triple.of(new Vector3f(), new Vector3f(), new Vector3f(1.f)) :
Sampler.sample(animator.keyframes, model.animationVariablePlaceholders, environment, time);

matrix4f.translate(localPos);
matrix4f.rotate(localRot);
matrix4f.scale(triple.getRight());
}
Quaternionf localRot = node.transform().rotation().mul(createQuaternion(triple.getMiddle().mul(-1, -1, 1)), new Quaternionf());
Vector3f localPos = triple.getLeft().mul(-1,1,1).div(16).add(origin);

if (requiresFrame)
poses.put(entry.getKey(), Pose.of(matrix4f.scale(entry.getValue().transform().scale())));
matrix4f.translate(localPos);
matrix4f.rotate(localRot);
matrix4f.scale(triple.getRight());
}

if (requiresFrame)
poses.put(entry.getKey(), Pose.of(matrix4f.scale(entry.getValue().transform().scale())));
}
return poses;
}
Expand Down

0 comments on commit 15494ca

Please sign in to comment.