Skip to content

Commit

Permalink
Use the right camera transform
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed May 30, 2023
1 parent 97d6a04 commit 1b8498d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/components/ik-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ AFRAME.registerComponent("ik-controller", {
this.isInView = true;
this.hasConvergedHips = false;
this.lastCameraTransform = new THREE.Matrix4();
this.lastCameraWorldTransform = new THREE.Matrix4();
this.transformUpdated = false;
waitForDOMContentLoaded().then(() => {
this.playerCamera = document.getElementById("viewing-camera").getObject3D("camera");
});
Expand Down Expand Up @@ -180,7 +182,7 @@ AFRAME.registerComponent("ik-controller", {

camera.object3D.updateMatrix();

this.transformUpdated = !this.lastCameraTransform.equals(camera.object3D.matrix);
const hasNewCameraTransform = !this.lastCameraTransform.equals(camera.object3D.matrix);

// Optimization: if the camera hasn't moved and the hips converged to the target orientation on a previous frame,
// then the avatar does not need any IK this frame.
Expand All @@ -189,9 +191,9 @@ AFRAME.registerComponent("ik-controller", {
if (
this.data.alwaysUpdate ||
this.forceIkUpdate ||
(this.isInView && (this.transformUpdated || !this.hasConvergedHips))
(this.isInView && (hasNewCameraTransform || !this.hasConvergedHips))
) {
if (this.transformUpdated) {
if (hasNewCameraTransform) {
this.lastCameraTransform.copy(camera.object3D.matrix);
}

Expand Down Expand Up @@ -236,6 +238,11 @@ AFRAME.registerComponent("ik-controller", {

if (this._hadFirstTick) {
camera.object3D.updateMatrices();
this.transformUpdated = !this.lastCameraWorldTransform.equals(camera.object3D.matrixWorld);
if (this.transformUpdated) {
this.lastCameraWorldTransform.copy(camera.object3D.matrixWorld);
}

avatar.updateMatrices();
// Note: Camera faces down -Z, avatar faces down +Z
const yDelta = Math.PI - angleOnXZPlaneBetweenMatrixRotations(camera.object3D.matrixWorld, avatar.matrixWorld);
Expand Down

0 comments on commit 1b8498d

Please sign in to comment.