使用Three.js在运行时更改创建的3D立方体的宽度/高度/长度

4
我们是否能够在运行时更改使用Three.js创建的3D立方体的“宽度/高度/长度”维度?类似于我在SO上得到的一个演示,它可以在运行时更改立方体的颜色:http://jsfiddle.net/mpXrv/1/。同样地,我们能否改变这个立方体的尺寸吗?以下是我的代码:HTML
<script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
<div id="container"></div>
<div class="inputRow clear" id="dimensionsNotRound" data-role="tooltip">
    <label class="grid-8">Dimensions (mm):</label>
    <br/>
    <br/>
    <div> <span>Length</span>

        <input class="numeric-textbox" type="text" value="">
        <br/>
        <br/>
    </div>
    <div> <span>Width</span>

        <input class="numeric-textbox" type="text" value="">
        <br/>
        <br/>
    </div>
    <div> <span>Height</span>

        <input class="numeric-textbox" type="text" value="">
        <br/>
        <br/>
    </div>
    <button id="btn">Click me to change the Dimensions</button>

JS

    //Script for 3D Box 


// revolutions per second
var angularSpeed = 0.2;
var lastTime = 0;

// this function is executed on each animation frame
function animate() {
    // update
    var time = (new Date()).getTime();
    var timeDiff = time - lastTime;
    var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
    cube.rotation.y += angleChange;
    lastTime = time;

    // render
    renderer.render(scene, camera);

    // request new frame
    requestAnimationFrame(function () {
        animate();
    });
}

// renderer
var container = document.getElementById("container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);


// camera
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;

// scene
var scene = new THREE.Scene();

// cube
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 100, 200), new THREE.MeshLambertMaterial({
    color: '#cccccc'
}));
cube.overdraw = true;
cube.rotation.x = Math.PI * 0.1;
cube.rotation.y = Math.PI * 0.3;
scene.add(cube);

// add subtle ambient lighting
var ambientLight = new THREE.AmbientLight(0x888888);
scene.add(ambientLight);

// directional lighting
var directionalLight = new THREE.DirectionalLight(0x666666);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);

// start animation
animate();

这里是相应的 Fiddle!http://jsfiddle.net/EtSf3/1/ 如果需要其他信息,请告知。请提出建议。
2个回答

6

解决方法1(更好)

你必须使用比例网格属性,增加(或减少)物体的尺寸。

http://jsfiddle.net/EtSf3/4/

首先,你需要将你的cube变量设置为全局作用域。

我已经替换了你的立方体的尺寸为1,1,1。

cube = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshLambertMaterial({
  color: '#cccccc'
}));

在btn上附加一个事件

var $ = function(id) { return document.getElementById(id); };

$('btn').onclick = function() {
    var width = parseInt($('inp-width').value),
        height = parseInt($('inp-height').value),
        length = parseInt($('inp-length').value);

    cube.scale.x = width;
    cube.scale.y = height;
    cube.scale.z = length;
};

解决方法 2

另一个解决方案是删除您的对象,并使用给定的值创建一个新网格。


你太棒了!我认为方案1适合我!看起来简单、易懂且快速...非常感谢你! - UID
在fiddle中它运行得很好!!!...但是当我将更改合并到我的代码中时,它会抛出错误"无法调用null的方法'change'"...有任何想法为什么?? - UID
嗨,我已经在你的答案中添加了我的解决方案...请查看并告诉我这是否是正确的方法..如果是,为什么在Fiddle中它不起作用? - UID
你能否也看一下这个问题?http://stackoverflow.com/questions/21788385/setting-new-dimension-using-jquery-or-javascript-for-cylindergeometry-from-three - UID

1
您想要的是 Mesh.scale(在您的示例中为 cube.scale)。

http://jsfiddle.net/EtSf3/3/

这是我添加的代码。
document.getElementById('btn').addEventListener('click', function(e) {
    var inputs = document.getElementsByClassName('numeric-textbox');
    cube.scale.x = parseFloat(inputs[0].value) || cube.scale.x;
    cube.scale.z = parseFloat(inputs[1].value) || cube.scale.z;
    cube.scale.y = parseFloat(inputs[2].value) || cube.scale.y;
});

当我改变尺寸并点击按钮时,这个Fiddle就停止工作了...我做错了什么吗? - UID
我在控制台中看到了这个错误:无法加载资源:服务器响应状态为500(内部服务器错误)http://jobs.jsfiddle.net/random.js?callback=Request.JSONP.request_map.request_0 THREE.WebGLRenderer THREE.WebGLRenderer 57 three.min.js:386 - UID
谢谢您的关注!非常感谢!虽然不确定为什么会出现错误,但“Paul Rad-Dupuy”的解决方案对我很有帮助。再次感谢。 - UID

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接