如何在Three.js中获得立方体的高度

时间:2021-11-16 05:05:12

I have the following code;

我有以下代码;

// World
var geometry = new THREE.BoxGeometry( 100, 200, 100 );
var material = new THREE.MeshLambertMaterial( { color: 'green' } );
var cube = new THREE.Mesh( geometry, material );
cube.position.y = (cube.height / 2); // this doesn't work
scene.add( cube );

What is the correct method to get the height of the cube? I know the height = 100, but I want to understand how I get the height using program code.

获得立方体高度的正确方法是什么?我知道身高= 100,但我想知道如何使用程序代码获得高度。

4 个解决方案

#1


If you create a cube mesh using THREE.BoxGeometry, you can get the height of the cube by accessing the parameters property of the geometry:

如果使用THREE.BoxGeometry创建立方体网格,则可以通过访问几何的parameters属性来获取立方体的高度:

height = mesh.geometry.parameters.height;

If you have changed the mesh property scale.y from its default value of 1, then you will have to multiply the above quantity by scale.y.

如果已将网格属性scale.y从其默认值1更改,则必须将上述数量乘以scale.y。

three.js r.71

#2


The bounding box of an object will give its accurate dimensions:

对象的边界框将给出其准确的尺寸:

var cube_bbox = new THREE.Box3();
cube_bbox.setFromObject( cube );

Now you have the vectors cube_bbox.max and cube_bbox.min so:

现在你有了矢量cube_bbox.max和cube_bbox.min所以:

cube_height = cube_bbox.max.y - cube_bbox.min.y;

#3


The box Geometry object that you created has 3 properties (Height, Width and Depth). These 3 properties are read as x,y,z. As a result, if you want to change the height, you will have to change the Y axis.

您创建的框Geometry对象具有3个属性(高度,宽度和深度)。这3个属性读作x,y,z。因此,如果要更改高度,则必须更改Y轴。

geometry.scale.y = 200;

#4


You have to calculate it from geometry.

你必须从几何计算它。

//you need to get correct verts 0 and 4 are for example
var height=cube.geometry.vertices[4].y-cube.geometry.vertices[0].y;

#1


If you create a cube mesh using THREE.BoxGeometry, you can get the height of the cube by accessing the parameters property of the geometry:

如果使用THREE.BoxGeometry创建立方体网格,则可以通过访问几何的parameters属性来获取立方体的高度:

height = mesh.geometry.parameters.height;

If you have changed the mesh property scale.y from its default value of 1, then you will have to multiply the above quantity by scale.y.

如果已将网格属性scale.y从其默认值1更改,则必须将上述数量乘以scale.y。

three.js r.71

#2


The bounding box of an object will give its accurate dimensions:

对象的边界框将给出其准确的尺寸:

var cube_bbox = new THREE.Box3();
cube_bbox.setFromObject( cube );

Now you have the vectors cube_bbox.max and cube_bbox.min so:

现在你有了矢量cube_bbox.max和cube_bbox.min所以:

cube_height = cube_bbox.max.y - cube_bbox.min.y;

#3


The box Geometry object that you created has 3 properties (Height, Width and Depth). These 3 properties are read as x,y,z. As a result, if you want to change the height, you will have to change the Y axis.

您创建的框Geometry对象具有3个属性(高度,宽度和深度)。这3个属性读作x,y,z。因此,如果要更改高度,则必须更改Y轴。

geometry.scale.y = 200;

#4


You have to calculate it from geometry.

你必须从几何计算它。

//you need to get correct verts 0 and 4 are for example
var height=cube.geometry.vertices[4].y-cube.geometry.vertices[0].y;