3d - threejs get center of object -
i have trying retrieve , plot centerpoint of objects keep getting same value objects x=0, y=0 , z=0. centerpoint centerpoint of scene. i'm reading on 3d computer matrixes i'm bit novice in area. need update scene somehow or update matrix after every added object or something?
function initboxes(){     var box = new three.mesh(geometry, material);     box.position.set(0, 2, 2);     getcenterpoint(box);      var box2 = new three.mesh(geometry, material);     box2.position.set(0, 6, 6);     getcenterpoint(box2); }  function getcenterpoint(mesh) {     var middle = new three.vector3();     var geometry = mesh.geometry;      geometry.computeboundingbox();      middle.x = (geometry.boundingbox.max.x + geometry.boundingbox.min.x) / 2;     middle.y = (geometry.boundingbox.max.y + geometry.boundingbox.min.y) / 2;     middle.z = (geometry.boundingbox.max.z + geometry.boundingbox.min.z) / 2;      return middle; }      
the object boundingbox in local space. if want center in world space have translate middle vertex world space. can the three.object3d localtoworld method this:
function getcenterpoint(mesh) {     var middle = new three.vector3();     var geometry = mesh.geometry;      geometry.computeboundingbox();      middle.x = (geometry.boundingbox.max.x + geometry.boundingbox.min.x) / 2;     middle.y = (geometry.boundingbox.max.y + geometry.boundingbox.min.y) / 2;     middle.z = (geometry.boundingbox.max.z + geometry.boundingbox.min.z) / 2;      mesh.localtoworld( middle );     return middle; }   update:
in newer three.js versions there a convenience method getcenter inside three.box3 class.
function getcenterpoint(mesh) {     var geometry = mesh.geometry;     geometry.computeboundingbox();        center = geometry.boundingbox.getcenter();     mesh.localtoworld( center );     return center; }      
Comments
Post a Comment