固定地板立方体的动态高度

时间:2015-04-24 09:50:03

标签: javascript three.js

我正在创建一个动态多维数据集,可以通过缩放其网格来动态更改。问题是,我想在修改它的高度时将它固定在地板上。这是我的代码片段:

function init() {

// Floor position
floor = new THREE.Mesh( shadowGeo, shadowMaterial );
floor.position.y = 0;
floor.rotation.x = - Math.PI / 2;
scene.add( floor );

// Defines the cube and its original position
var BoxGeometry = new THREE.BoxGeometry(50, 50, 50);
var boxMaterial = new THREE.MeshLambertMaterial({color: 0x000088});
cube = new THREE.Mesh(BoxGeometry, boxMaterial);
cube.position.set(0,30,0);
scene.add(cube);

// GUI PANEL INTERACTION
// Now the GUI panel for the interaction is defined
gui = new dat.GUI();

parameters = {
        height: 1,
        reset: function() {resetCube()}     
    }

// Define the second folder which takes care of the scaling of the cube
var folder1 = gui.addFolder("Dimensions");
var cubeHeight = folder2.add(parameters, "height").min(0).max(200).step(1);
folder1.open();

// Function taking care of the cube changes
cubeHeight.onChange(function(value){cube.scale.y = value;});

gui.open();
}

// Update cube characteristics
function updateCube() {
    cube.scale.y = parameters.y;
}

// Reset cube settings
function resetCube() {
    parameters.height = 1;
    updateCube();
}

// Rest of the code

我已经四处寻找并看到了this similar topic,但它还没有正确解释当具有参考楼层的对象时如何修改尺寸。你知道我怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

更改了.onChange()功能,让立方体保持在地面上:

    // Function taking care of the cube changes
    cubeHeightScale.onChange(
        function(value)
        {
            cube.scale.y = value;
            cube.position.y = (cubeHeight * value) / 2;
        } );

以下是检查实时更改的小提琴:http://jsfiddle.net/Lsjh965o/

three.js r71