1

I have a complex model loaded with OBJLoader. I want to move and rotate its center to let the user ( the user of my application ) to place the center point where he/she wants. I'm aware of this: old question, but for me does not work. This is a test in fiddle : fiddle example

Pressing the button in the example I expect that the center is moved. The code in the button is :

objMesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(new THREE.Vector3(1, 0.1, 0.1)));
Community
  • 1
  • 1
user2194221
  • 111
  • 1
  • 1
  • 6

2 Answers2

3

I found the solution empirically, but I don't understand why it works. I post the code to help and maybe someone could explain me what I found.

The code below shift the center in x for 0.1 units.

var x = 0.1;
var y = 0;
var z = 0;

for (var i = 0; i < object.children.length; i++)
{
    var geometry = object.children[i].geometry;

    object.children[i].centroid.divideScalar(geometry.vertices.length); // Why this line ?

    var offset = object.children[i].centroid.clone(); 
    object.children[i].geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-offset.x+x, -offset.y+y, -offset.z+z));
    object.children[i].position.x -= x;
    object.children[i].position.y -= y;
    object.children[i].position.z -= z;
    object.children[i].geometry.verticesNeedUpdate = true;
}
amon
  • 57,091
  • 2
  • 89
  • 149
user2194221
  • 111
  • 1
  • 1
  • 6
2

Use

mesh.geometry.translate( x, y, z );

or

mesh.geometry.center();

Also, consider just calling mesh.position.set(), rather than moving each vertex of the geometry.

three.js r.95

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Thank you for you answer. I don't want to move or resize the shape, but only move or rotate the center. For example I would like to place the center in the one of the corner. Or for example rotate the center so it does not allign with the world coordinate system. – user2194221 May 31 '13 at 07:59
  • 1. what do you mean by "rotate the center"? 2. a geometry does not have a "center" property. – WestLangley May 31 '13 at 14:27
  • I mean that: if you try to rotate the model, it rotates around its center. I want to move this center. The problem is that I import models from a CAD system and the orientation of the axes do not match with the center calculated with a bounding box. So if I try to rotate or stretch ( change the scale) an imported shape, the result is not correct. For example if I import a cube that it is not aligned with the x,y,z axes, the result of rotare/resize is totally wrong. I hope I made my self clear – user2194221 May 31 '13 at 16:19
  • 1
    OK, try `THREE.GeometryUtils.center( geometry )`, which translates the geometry so that the center of the geometry's bounding box is at the origin. – WestLangley May 31 '13 at 18:59