srayner
srayner

Reputation: 1837

How do I get the height of a cube in Three.js

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.

Upvotes: 4

Views: 10575

Answers (4)

WestLangley
WestLangley

Reputation: 104783

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:

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.

three.js r.71

Upvotes: 10

gaitat
gaitat

Reputation: 12632

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_height = cube_bbox.max.y - cube_bbox.min.y;

Upvotes: 5

Rendolf
Rendolf

Reputation: 96

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;

Upvotes: 0

Llogari Casas
Llogari Casas

Reputation: 962

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.scale.y = 200;

Upvotes: 0

Related Questions