简体   繁体   English

Three.js 对象居中

[英]Three.js Object Centering

I just started working with three.js recently and I finally have my models loading in properly.我最近才开始使用three.js,我终于可以正确加载我的模型了。 However, looking at the examples I noticed that the centering in most of the examples is done with coding like this但是,查看示例时,我注意到大多数示例中的居中都是通过这样的编码完成的

object.position.y = - 95;

That works fine for the example's purpose but I'm loading multiple objects of different sizes and I don't want to hard code a value for every single model.这对于示例的目的来说很好用,但我正在加载多个不同大小的对象,我不想为每个模型硬编码一个值。 So my question is, how do you center an object dynamically?所以我的问题是,如何动态居中对象?

I saw geometry.center was a way to achieve this but I couldn't find any examples and I don't understand how to add that into this example我看到 geometry.center 是实现这一目标的一种方法,但我找不到任何示例,我不明白如何将其添加到此示例中

http://threejs.org/examples/#webgl_loader_obj_mtl http://threejs.org/examples/#webgl_loader_obj_mtl

Any suggestions, tutorials, tips, or examples would be great.任何建议、教程、技巧或示例都会很棒。 Thanks!谢谢!

If the pivot point of your models is at the vertical center of your model, you can use this general approach:如果模型的枢轴点位于模型的垂直中心,则可以使用以下通用方法:

//object.geometry.center();
var box = new THREE.Box3().setFromObject( object )
var boundingBoxSize = box.max.sub( box.min );
var height = boundingBoxSize.y;
object.position.y = - height / 2;

And if the geometry is already shifted, you can re-center it using geometry.center() .如果几何图形已经移动,您可以使用geometry.center()其重新居中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM