Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Like I got a Fiddle in SO which changes Color of Cube at run time : http://jsfiddle.net/mpXrv/1/

Similarly can we change the Dimension??

Here is my code:

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

Here is the Fiddle for the same! http://jsfiddle.net/EtSf3/1/

Let me know if you need any other information.

Please suggest.

like image 865
UID Avatar asked Mar 20 '23 14:03

UID


1 Answers

Solution 1 (better)

You must use the scale mesh property to increase (or decrease) the dimensions of your object.

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

First, You need to set your cube variable to the global scope.

I've replaced the dimensions of your cube to 1, 1, 1.

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

Attach an even on the 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

An other solution will be to delete your object and create a new mesh with the given values.

like image 192
Paul Rad Avatar answered Mar 23 '23 02:03

Paul Rad