如何居中和缩放对象threejs

迪奥戈·贝尼尼·米拉格雷斯

我正在使用以下代码来缩放和居中使用ObjectLoader加载的msgpack压缩对象,并且它无法正常工作。我认为我的物体旋转了,因此导致了奇怪的行为。在某些对象上,它可以成功居中,但在其他对象上,居中是偏移的,缩放也不正确。

在此代码片段中,结果是来自ObjectLoader的场景。我以为该对象的形成不是很好,但是我不确定。我希望图像或任何其他用户输入的网格上的表格位于网格的顶部,居中并缩放,以使最大尺寸为1个单位。

每个正方形的尺寸为0.25,轴位于0,0,0 http://i.stack.imgur.com/fkKYC.png

    // result is a threejs scene        
    var geometry = result.children[0].geometry;
    var mesh = result.children[0];
    geometry.computeBoundingBox();
    var middle = new THREE.Vector3();
    middle.x        = ( geometry.boundingBox.max.x + geometry.boundingBox.min.x ) / 2;
    middle.y        = -geometry.boundingBox.min.y;
    middle.z        = ( geometry.boundingBox.max.z + geometry.boundingBox.min.z ) / 2;
    middle.negate();
    mesh.position.copy(middle);
    // scales the mesh up to maxsize
    var maxSize = 1;
    // gets the biggest axis
    var maxVal = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
    if (maxVal < geometry.boundingBox.max.y - geometry.boundingBox.min.y) {
        maxVal = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
    }
    if (maxVal < geometry.boundingBox.max.z - geometry.boundingBox.min.z) {
        maxVal = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
    // scales the current size proportional to the maxval, times maxsize
    mesh.scale.divideScalar(maxVal * maxSize);

    self.scene.add(result);
比约克

无需拨打geometry.computeBoundingBox();电话,geometry.center();那么您就不需要middle.xmiddle.z,也可以直接打电话mesh.translateY()而不是摆弄middle

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章