如何用Three.js改变CubeGeometry的宽度?

时间:2011-12-05 22:25:10

标签: animation three.js mesh

我有一个立方体几何和一个网格,我不知道如何改变宽度(或高度......我可以改变x,y和z)。 这是我现在所拥有的片段:

geometry = new THREE.CubeGeometry( 200, 200, 200 );
material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
mesh = new THREE.Mesh( geometry, material );
// WebGL renderer here

function render(){
    mesh.rotation.x += 0.01;
    mesh.rotation.y += 0.02;
    renderer.render( scene, camera );
}

function changeStuff(){
    mesh.geometry.width = 500; //Doesn't work.
    mesh.width = 500; // Doesn't work.
    geometry.width = 500; //Doesn't work.
    mesh.position.x = 500// Works!!

    render();
}

谢谢!

修改

找到解决方案:

mesh.scale.x = 500;

3 个答案:

答案 0 :(得分:8)

只需完成问题的评论和解决方案(并在示例代码中提供答案):

// create a cube, 1 unit for width, height, depth
var geometry = new THREE.CubeGeometry(1,1,1);

// each cube side gets another color
var cubeMaterials = [ 
    new THREE.MeshBasicMaterial({color:0x33AA55, transparent:true, opacity:0.8}),
    new THREE.MeshBasicMaterial({color:0x55CC00, transparent:true, opacity:0.8}), 
    new THREE.MeshBasicMaterial({color:0x000000, transparent:true, opacity:0.8}),
    new THREE.MeshBasicMaterial({color:0x000000, transparent:true, opacity:0.8}), 
    new THREE.MeshBasicMaterial({color:0x0000FF, transparent:true, opacity:0.8}), 
    new THREE.MeshBasicMaterial({color:0x5555AA, transparent:true, opacity:0.8}), 
]; 
// create a MeshFaceMaterial, allows cube to have different materials on each face 
var cubeMaterial = new THREE.MeshFaceMaterial(cubeMaterials); 
var cube = new THREE.Mesh(geometry, cubeMaterial);

cube.position.set(0,0,0);
scene.add( cube );
cube.scale.x = 2.5; // SCALE
cube.scale.y = 2.5; // SCALE
cube.scale.z = 2.5; // SCALE

这里实现了一个稍微高级的动态示例:https://www.matheretter.de/formeln/geometrie/quader/(仍然是相同的缩放)

答案 1 :(得分:0)

缩放属性可用于更改立方体的宽度,高度和深度。

    //creating a cube 
    var geometry = new THREE.BoxGeometry(1,1,1);
    var material = new THREE.MeshBasicMaterial({color:"white"});
    var cube = new THREE.Mesh(geometry, material);


    //changing size of cube which is created.
    cube.scale.x = 30;
    cube.scale.y = 30;
    cube.scale.z = 30;

答案 2 :(得分:0)

您可以设置立方体的几何形状并像这样影响新的立方体:

let new_geometry = new THREE.CubeGeometry(500,200,200);
geometry.dispose();
cube.geometry = new_geometry;