DEV Community

orca forge
orca forge

Posted on Originally published at forge.workstyle.tech

How to Fix VRM Bounds and Arm Rotation in three.js

📝 Originally published (in Japanese) at forge.workstyle.tech.

TL;DR In three.js, all three VRM characters appeared small and near the edge of the screen. The skinned mesh bounds were measured in the T-pose before the animation was applied. Three days after fixing the order, I also found that the arms remained hidden when I tried to lower them.

TL;DR

The VRM appeared small and pushed to the edge of the screen because the skinned mesh’s (a mesh moved by bones) bounding box was being measured while it was still in its default T-pose. Apply the VRMA animation first, then measure the bounding box and fit the camera. To lower the arms, I tested rotation directions on the normalized rig and found that rotating the left arm around +z and the right arm around -z works.

Why the VRM Appeared Smaller and Shifted to the Edge of the Screen

On October 10, before adjusting the camera, I checked how the bounding box was calculated. Here, the bounding box defines the character’s dimensions and is used to frame the camera. The cause was the skinned mesh: its bounds were still being measured in the T-pose, before the character was posed.

That meant the camera was framed to fit the character’s outstretched arms. Even though I intended to frame the character at its posed size, the calculation still used the T-pose bounds. That’s why all three characters appeared smaller and shifted toward the edge.

In What Order Should You Update the Pose and Bounding Box?

I changed the order in which the bounding box is measured. First, I apply the VRMA animation with update(0). Then I update the VRM and the rig’s (bone structure’s) world matrices, and recalculate the skinned mesh bounding boxes. Finally, I position the camera based on those bounds.

// Measure the posed figure, not the T-pose the skinned meshes were first bounded in.
currentModels.forEach(vrm => { vrm.poseMixer?.update(0); vrm.update(0); });
rig.updateMatrixWorld(true);
rig.traverse(object => { if (object.isSkinnedMesh) object.computeBoundingBox(); });
const box = new THREE.Box3().setFromObject(rig);
Enter fullscreen mode Exit fullscreen mode

This way, the camera is no longer framed based on the width of the T-pose. The pose is now set before measuring the bounding box.

Why the Arms Still Weren’t Visible After I Lowered Them

On October 13, someone reviewing the result said, “Please use a natural pose instead of a T-pose.” This time, the task was to lower the arms of a single character.

In the first version, I changed rotation.z on the normalized bones. I set leftUpperArm to -1.2 and rightUpperArm to +1.2, and set the forearms to ∓0.28 as well. I expected the arms to lower, but the feedback was: “Even after changing the arm angles, the arms aren’t visible.”

Tweaking the values a little wouldn’t explain the cause. I added some temporary code to try different axes and angles, lined up values such as 0, 0.3, and -0.6 on the z-axis, along with values on the x-axis, then took screenshots to compare the results. I also tried 0.6 on the upper arms along the way.

The comparison showed that I had rotated the arms in the wrong direction. Instead of lowering, they moved up and were hidden behind the head. On the normalized rig, the direction to lower the left arm was +z, and for the right arm it was -z. That’s what I observed from testing. I didn’t investigate why the signs were reversed.

Check Arm Pose Before the Bounding Box in the Final Version

In the final version at 09:48, I separated the upper arms, forearms, and idle sway.

// In this model's normalized rig a positive z lowers the left arm and a negative z the right one.
const ARMS = { upper: .75, lower: .15, swing: .05 };
set('leftUpperArm', ARMS.upper + swing);
set('rightUpperArm', -(ARMS.upper - swing));
set('leftLowerArm', ARMS.lower);
set('rightLowerArm', -ARMS.lower);
Enter fullscreen mode Exit fullscreen mode

For swing, I added an oscillation based on elapsed seconds: sin(elapsed seconds × 1.1) × 0.05. On load, I call VRMUtils.rotateVRM0(vrm).

The two issues were connected in practice. I also called the arm-lowering routine before measuring the bounding box. The camera is fitted to the lowered-arm pose. I recreated the static image for devices that cannot render 3D using the new pose as well.

In the first issue, the model was still in a T-pose when the bounding box was measured. In the second, the arms were meant to be lowered, but the rotation was in the wrong direction. In both cases, before fitting the camera, I needed to check the pose of the subject being calculated or displayed.

Key takeaways

  • Check the character's pose when measuring its bounds.
  • Apply the VRMA animation with update(0), then update the VRM and rig.
  • Recompute skinned mesh bounds before fitting the camera.
  • Try different axes and angles when lowering the arms; on this rig, +z lowered the left arm and -z lowered the right.

Top comments (0)