简体   繁体   中英

three.js Cube Geometry - how to update parameters?

Possibly dumb question but here goes. Three.js geometries have 'parameter' feilds associated with them, see the box geometry here...

box Geometry parameters

I am trying to update these parameters like this...

var nodeSize = 10;
var geometry = new THREE.CubeGeometry(nodeSize, nodeSize, nodeSize);
mesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial({side:THREE.DoubleSide}));

scene.add(mesh);
mesh.geometry.parameters.depth=20;

But of course, the geometry remains unchanged. Is there a way of updating the geometry by editing these parameters?

fiddle here https://jsfiddle.net/kn3owveg/2/

Any help appreciated!

parameters.depth is only used at geometry construction time. it has no effect when modifying it. you can think of it as read only .

Use the example at BoxGeometry and the gui on the right to see how to achieve what you want.

Gaitat is totally right, you can't change geometry with changing of parameters .

And there can be another solution. With scaling of your cube.

function setSize( myMesh, xSize, ySize, zSize){
  scaleFactorX = xSize / myMesh.geometry.parameters.width;
  scaleFactorY = ySize / myMesh.geometry.parameters.height;
  scaleFactorZ = zSize / myMesh.geometry.parameters.depth;
  myMesh.scale.set( scaleFactorX, scaleFactorY, scaleFactorZ );
}
...
setSize(mesh, 10, 10, 20);

jsfiddle example

更改参数后,需要再次render

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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