Three.js - move custom geometry to origin

ε祈祈猫儿з 提交于 2019-12-17 19:42:14

问题


I have some custom geometries obtained from a STEP file conversion and I use the mouse to rotate them. They rotate around the origin of the scene, but since they are far from it, they seem rotating on a virtual sphere. How can I move them to the origin so that they don't seem "floating" around (I mean that I'd like to reduce to zero the radius of the virtual sphere). This is the example I'd like to move. I've tried setting their position to (0, 0, 0) doing:

object.position.x = 0;
object.position.y = 0;
object.position.z = 0;

but it didin't work.


回答1:


The typical solution to this problem is to translate the geometry right after it is created. You do that by applying a translation matrix to the geometry like so:

geometry.applyMatrix( new THREE.Matrix4().makeTranslation( distX, distY, distZ ) );

EDIT: You can simply do this, instead:

geometry.translate( distX, distY, distZ ); // three.js r.72

The function geometry.computeBoundingBox() may be of help to you in determining an amount to translate.

However, I see in your case, you have multiple geometries, so it it a bit more complicated, but doable. You will need to translate each geometry by the same amount.

EDIT

Tip: Instead of adding each object to the scene, create a parent object, add it to the scene, and then add the objects to the parent.

var parent;

parent = new THREE.Object3D();
scene.add( parent );

parent.add( object1 );
parent.add( object2 );
// and so on...

Then in your render function, just rotate the parent, not the individual objects.



来源:https://stackoverflow.com/questions/12835361/three-js-move-custom-geometry-to-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!