Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

529
Visualizações
Three.js - Rotate around Axis at an **absoulte** angle

I am using this function (from Three JS Pivot point) to rotate an Object around an axis in three.js:

// obj - your object (THREE.Object3D or derived)
// point - the point of rotation (THREE.Vector3)
// axis - the axis of rotation (normalized THREE.Vector3)
// theta - radian value of rotation
// pointIsWorld - boolean indicating the point is in world coordinates (default = false)
function rotateAboutPoint(obj, point, axis, theta, pointIsWorld){
    pointIsWorld = (pointIsWorld === undefined)? false : pointIsWorld;

    if(pointIsWorld){
        obj.parent.localToWorld(obj.position); // compensate for world coordinate
    }

    obj.position.sub(point); // remove the offset
    obj.position.applyAxisAngle(axis, theta); // rotate the POSITION
    obj.position.add(point); // re-add the offset

    if(pointIsWorld){
        obj.parent.worldToLocal(obj.position); // undo world coordinates compensation
    }

    obj.rotateOnAxis(axis, theta); // rotate the OBJECT
}

The issue I am having, is that the transformation is persistent, as pointed out in the answer. So when i call the function again the transformation is applied from where it left off. How can I "reset" the transformation so that the provided angle is an absolute value?

The following does not work:

 obj.rotation.y = 0;

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The object's transformation is stored under obj.matrix

If you want to reset the object to have the default transformation (i.e. no scaling, rotation or translation), you can do this:

  obj.matrix.identity();
  obj.matrix.decompose(obj.position, obj.quaternion, obj.scale);

or if you just want to reset the rotation to the default, you can do this

  obj.quaternion.identity();

https://threejs.org/docs/index.html?q=matrix4#api/en/math/Matrix4.identity

If you want to return the object to some specific transformation that it had prior to the rotation, you could either.

  1. reverse previous the rotation before applying the new one:
  obj.rotateOnAxis(oldAxis, -oldTheta);

or...

  1. store off the old transform matrix and restore it before applying the new transformation. Needs to be stored somewhere persistent (so not e.g. a local variable). Various places you could potentially store it, but one that would definitely be available to you would be the object3D User Data.
  obj.userData.oldMatrix = obj.matrix.clone();

and then later re-instate it like this:

  obj.matrix.copy(obj.userData.oldMatrix);
  obj.matrix.decompose(obj.position, obj.quaternion, obj.scale);

https://threejs.org/docs/index.html?q=object3D#api/en/core/Object3D.userData

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda