1
\$\begingroup\$

I've loaded an object and set its position on vector(5,2,-5) by threejs. And I wanted it to rotate around the origin point in the world coodinate system. I found a method in class Object3D called 'rotateOnWorldAxis', so I had a try on it but it didn't work, it still rotate on it own space. I want to know why it didn't work and how to make this right.

The code is like this:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//resize event
window.addEventListener("resize",function(){
  var width = window.innerWidth;
  var height = window.innerHeight;
  renderer.setSize(width,height);
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
});

camera.position.set(0,0,15);

var ambientLight = new THREE.AmbientLight(0xffffff,0.8);
scene.add(ambientLight);

var mesh;
var fgxloader = new THREE.FBXLoader().
load("asset/source/1520514930250.FBX",function(object){
  object.position.set(5,2,-5);
  scene.add(object);
  mesh = object;
},function(xhr){
  console.log("object "+(xhr.loaded/xhr.total*100)+"% loaded");
},function(error){
  alert(error);
});

var rotateSpeed = 0.1;
var update = function(){
  if(mesh===undefined)
    return;
  mesh.rotateOnWorldAxis(new THREE.Vector3(0,0,0),rotateSpeed);
  //mesh.rotateOnAxis(new THREE.Vector3(0,1,0),rotateSpeed);
  //mesh.rotateY(rotateSpeed);
  //mesh.rotation.y += rotateSpeed
};

var render = function(){
  renderer.render(scene,camera);
};

var GameLoop = function(){
  requestAnimationFrame(GameLoop);
  update();
  render();
};

GameLoop();
\$\endgroup\$
  • \$\begingroup\$ i;ve never used threejs but doing a quick search i came across this one, might help u. stackoverflow.com/questions/15214582/… \$\endgroup\$ – Big T Larrity May 9 '18 at 9:19
  • \$\begingroup\$ Thanks a lot man! I worked.I followed this method and had a try in my code and it ran just right as I expected. \$\endgroup\$ – tedyage May 10 '18 at 9:24
  • \$\begingroup\$ nice work! glad it helped \$\endgroup\$ – Big T Larrity May 10 '18 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.