UID
UID

Reputation: 4514

Update height/radius of 3D Cylinder created with Three.js at runtime

Like we are changing height/width/depth of 3D cube at run time in this Fiddle: http://jsfiddle.net/EtSf3/4/

How can we change the Radius and Length at runtime of Cylinder created using Three.js

Here is my code:

HTML:

<script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
<div id="container"></div>

JS

//Script for 3D Cylinder 

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

var cylinder = null;
// 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;
    cylinder.rotation.x += angleChange;
    cylinder.rotation.z += 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(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;

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

// cylinder
// API: THREE.CylinderGeometry(bottomRadius, topRadius, height, segmentsRadius, segmentsHeight)
cylinder = new THREE.Mesh(new THREE.CylinderGeometry(150, 150, 500, 100, 100, false), new THREE.MeshPhongMaterial({
    // light
    specular: '#cccccc',
    // intermediate
    color: '#666666',
    // dark
    emissive: '#444444',
    shininess: 100
}));
cylinder.overdraw = true;
cylinder.rotation.x = Math.PI * 0.2;
//cylinder.rotation.y = Math.PI * 0.5;
scene.add(cylinder);

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

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

// start animation
animate();

Here is the Fiddle for the same: http://jsfiddle.net/dpPjD/

Let me know if you need any other information.

Please suggest.

Upvotes: 4

Views: 3355

Answers (2)

Wesley Melencion
Wesley Melencion

Reputation: 21

You can try overwriting the geometry of an object by doing something like this:

cylinder.geometry.dispose();
cylinder.geometry = new THREE.CylinderGeometry(botRad, topRad, height, 32);

where: botRad, topRad and height are new values from initial or a variable that is constantly changing if you are planning on oscillating cylinder height.

The dispose makes sure that it deletes the previous geometry of the object before overwriting a new geometry to it.

Upvotes: 2

Prabindh
Prabindh

Reputation: 3504

Once the object geometry is added to the mesh, it is converted to face/vertex/UV/normals and stored as part of the mesh. For example, the cylinder shape you have specified is tessellated (divided) by Three.js into triangles with a vertex count of more than 10,000.

Hence while the global mesh properties like transforms can be updated, updating the individual geometries is as good as creating a new geometry every animation-frame. If you happen to know precisely the vertices you need to modify, you can update it directly using the geometry.vertices property. But if not, I do not think there is a way.

Upvotes: 4

Related Questions