javascript - set the model on the center of the scene in threejs -
am using following code visualize object on center of scene, working fine json loder, if use objloder
or objmtlloder
not working
function modelloadedcallback(geometry, materials) { /* create object geometry , materials loaded. there can multiple materials, can applied object using meshfacematerials. note tha material can include references texture images might finish loading later. */ var object = new three.mesh(geometry, new three.meshfacematerial(materials)); /* determine ranges of x, y, , z in vertices of geometry. */ var xmin = infinity; var xmax = -infinity; var ymin = infinity; var ymax = -infinity; var zmin = infinity; var zmax = -infinity; (var = 0; < geometry.vertices.length; i++) { var v = geometry.vertices[i]; if (v.x < xmin) xmin = v.x; else if (v.x > xmax) xmax = v.x; if (v.y < ymin) ymin = v.y; else if (v.y > ymax) ymax = v.y; if (v.z < zmin) zmin = v.z; else if (v.z > zmax) zmax = v.z; } /* translate center of object origin */ var centerx = (xmin+xmax)/2; var centery = (ymin+ymax)/2; var centerz = (zmin+zmax)/2; var max = math.max(centerx - xmin, xmax - centerx); max = math.max(max, math.max(centery - ymin, ymax - centery) ); max = math.max(max, math.max(centerz - zmin, zmax - centerz) ); var scale = 10/max; object.position.set( -centerx, -centery, -centerz ); console.log("loading finished, scaling object " + scale); console.log("center @ ( " + centerx + ", " + centery + ", " + centerz + " )"); /* create wrapper, model, scale , rotate object. */ model = new three.object3d(); model.add(object); model.scale.set(scale,scale,scale); rotatex = rotatey = 0; scene.add(model); render(); }
it causing typeerror: s undefined
, model not showing on scene, wrong here?? there other method avail center object objloder
or objmtlloder
you not have go through vertices of model figure out center. there geometry.computeboundingbox()
method can use compute bounding box of geometry. take @ http://threejs.org/docs/#reference/core/geometry.
for geometry node should able do:
geometry.computeboundingbox (); var bbox = geometry.boundingbox; alert('bounding box coordinates: ' + '(' + bbox.min.x + ', ' + bbox.min.y + ', ' + bbox.min.z + '), ' + '(' + bbox.max.x + ', ' + bbox.max.y + ', ' + bbox.max.z + ')' );
Comments
Post a Comment