4

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You Rock Man!!! I think Solution 1 will work for me!! looks simple, easy and Quick... Thank you so much!!!
In the fiddle its working fine!!!... but when I incorporated the changes in my code, its throwing error "Cannot call method 'change' of null"... any idea why??
Hi, I have added my solution in your answer... please review and let me know if this is a correct approach.. if yes, why its not working in Fiddle?
Can you also look into this question? stackoverflow.com/questions/21788385/…
1

What you want is Mesh.scale (cube.scale in you example).

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

3 Comments

When I change the dimensions and click the button, the Fiddle stop working... Am I doing somthing wrong??
I got this error in Console: Failed to load resource: the server responded with a status of 500 (Internal Server Error) jobs.jsfiddle.net/… THREE.WebGLRenderer THREE.WebGLRenderer 57 three.min.js:386
Thank you for looking!!! Appreciate It! Though not sure why its throwing the error... but "Paul Rad-Dupuy's" Solution worked for me... Thanks once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.