就像我们在这个小提琴中在运行时改变3D立方体的高度/宽度/深度一样: 的 http://jsfiddle.net/EtSf3/4/
如何在使用Three.js
创建的圆柱体的运行时更改半径和长度这是我的代码:
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();
以下是相同的小提琴:http://jsfiddle.net/dpPjD/
如果您需要任何其他信息,请与我们联系。
请建议。
答案 0 :(得分:4)
将对象几何体添加到网格后,它将转换为面/顶点/ UV /法线并存储为网格的一部分。例如,您指定的圆柱形状将由Three.js细分(划分)为顶点数超过10,000的三角形。
因此,虽然可以更新像变换这样的全局网格属性,但更新单个几何图形与在每个动画帧中创建新几何图形一样好。如果您恰好知道需要修改的顶点,可以使用geometry.vertices属性直接更新它。但如果没有,我认为没有办法。