简体   繁体   English

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

[英]Change width/height/length of 3D Cube created with Three.js at runtime

Can we change the dimension "width/height/length" of 3D Cube created with Three.js at runtime? 我们可以在运行时更改使用Three.js创建的3D立方体的尺寸“宽度/高度/长度”吗?

Like I got a Fiddle in SO which changes Color of Cube at run time : http://jsfiddle.net/mpXrv/1/ 就像我在SO中有一个小提琴,它在运行时改变了立方体的颜色: http//jsfiddle.net/mpXrv/1/

Similarly can we change the Dimension?? 同样可以改变维度吗?

Here is my code: 这是我的代码:

HTML 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 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();

Here is the Fiddle for the same! 这是同样的小提琴! http://jsfiddle.net/EtSf3/1/ http://jsfiddle.net/EtSf3/1/

Let me know if you need any other information. 如果您需要任何其他信息,请与我们联系。

Please suggest. 请建议。

Solution 1 (better) 解决方案1(更好)

You must use the scale mesh property to increase (or decrease) the dimensions of your object. 必须使用缩放网格属性来增加(或减少)对象的尺寸。

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

First, You need to set your cube variable to the global scope. 首先,您需要将cube变量设置为全局范围。

I've replaced the dimensions of your cube to 1, 1, 1. 我已经将立方体的尺寸替换为1,1,1。

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

Attach an even on the btn 在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;
};

Solution 2 解决方案2

An other solution will be to delete your object and create a new mesh with the given values. 另一种解决方案是删除对象并使用给定值创建新网格。

What you want is Mesh.scale (cube.scale in you example). 你想要的是Mesh.scale(在你的例子中是cube.scale)。

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

This is the code that I added 这是我添加的代码

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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