简体   繁体   English

如何在 Three.js 和 Threebox 中找到 Box3 的对角线长度

[英]How to find the diagonal length of a Box3 in Three.js and Threebox

I'm using Threebox (a Three.js plugin for Mapbox) to load 3D models of buildings to a map.我正在使用 Threebox(Mapbox 的 Three.js 插件)将 3D 建筑物模型加载到 map。 There is support for a certain level of interactivity with the buildings (rotating and moving).支持与建筑物的一定程度的交互性(旋转和移动)。 The rotation functionality is accessed via a floating ring that when clicked, allows the model to be rotated.旋转功能通过一个浮动环访问,单击该浮动环可旋转 model。 Example:例子:

房子上的旋转环

Since my models are of different sizes, the ring needs to be sized accordingly, but I'm having some issues finding the correct measurements of the model to do this.由于我的模型尺寸不同,因此需要相应地调整戒指的尺寸,但我在找到 model 的正确测量值来执行此操作时遇到了一些问题。

Ideally, it would looked like the following:理想情况下,它应该如下所示:

在此处输入图像描述

I create the ring with a Three.js TorusGeometry like this:我使用 Three.js TorusGeometry 创建环,如下所示:

let geom = new THREE.TorusGeometry(ringSize, 0.01, 30, 25);
let material = new THREE.MeshStandardMaterial({ color: 0xffc000, side: THREE.DoubleSide });
let mesh = new THREE.Mesh(geom, material);

wherein I need to calculate the size the ring needs to be beforehand.其中我需要预先计算戒指需要的尺寸。

Here is the issue: the ring size (radius) for the house example above is about ~0.7, but I can't find a way to accurately derive that from anywhere in the models properties.这是问题所在:上述房屋示例的环尺寸(半径)约为 0.7,但我找不到从模型属性中的任何位置准确得出该值的方法。 I'm suspecting this has to do with Threebox having meters as units where Three has it's own map units.我怀疑这与 Threebox 以米为单位有关,其中 Three 拥有自己的 map 单位。

Here are some properties I'm able to access in the object:以下是我可以在 object 中访问的一些属性:

unitsPerMeter: 0.0600151
modelSize: { x: 13.9649, y: 11.4863, z: 9.1121 }
boundingBox.box: {
    "min": {
        "x": -6.982465131993985,
        "y": -5.743162421920143,
        "z": 0
    },
    "max": {
        "x": 6.982465131993986,
        "y": 5.743162421920143,
        "z": 9.112114852394226
    }
}
center: {
    "x": -1.4794275894722282,
    "y": 0.5752914186997362,
    "z": -0.0858976981176804
}

I have tried the following to get the size:我尝试了以下方法来获取大小:

let box = new THREE.Box3().setFromObject(object);
let size = new THREE.Vector3();
box.getSize(size);
console.log(size)

which results in:这导致:

{
    "x": 153.10896959049688,
    "y": 125.93398806704032,
    "z": 99.90357914196488
}

and that doesn't seem very helpful since the length is like 0.7这似乎不是很有帮助,因为长度就像 0.7

For square it's always a * sqrt(2) where a is side of rectangle对于正方形,它总是a * sqrt(2)其中a是矩形的边

Is this part of the documentation what you are looking for? 这部分文档是您要查找的吗?

According to it, you can do:根据它,你可以这样做:

tb.projectToWorld(lnglat)

to

calculate the corresponding THREE.Vector3 for a given lnglat计算给定 lnglat 对应的 THREE.Vector3

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

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