最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update heightradius of 3D Cylinder created with Three.js at runtime - Stack Overflow

programmeradmin0浏览0评论

Like we are changing height/width/depth of 3D cube at run time in this Fiddle: /

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

Here is my code:

HTML:

<script src=".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: /

Let me know if you need any other information.

Please suggest.

Like we are changing height/width/depth of 3D cube at run time in this Fiddle: http://jsfiddle/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./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/dpPjD/

Let me know if you need any other information.

Please suggest.

Share Improve this question edited Feb 15, 2014 at 21:39 user128511 asked Feb 14, 2014 at 21:50 UIDUID 4,49411 gold badges47 silver badges76 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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.

发布评论

评论列表(0)

  1. 暂无评论