Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在运行时更新使用Three.js创建的三维圆柱体的高度/半径_Javascript_Three.js_User Input - Fatal编程技术网

Javascript 在运行时更新使用Three.js创建的三维圆柱体的高度/半径

Javascript 在运行时更新使用Three.js创建的三维圆柱体的高度/半径,javascript,three.js,user-input,Javascript,Three.js,User Input,就像我们在运行时在此小提琴中更改3D立方体的高度/宽度/深度一样: 如何在运行时更改使用Three.js创建的圆柱体的半径和长度 这是我的密码: HTML: <script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script> <div id="container"></div> 这是同样的小提琴: 如果您需要任何其他信息,请告诉我 请建议。将对象几

就像我们在运行时在此小提琴中更改3D立方体的高度/宽度/深度一样:

如何在运行时更改使用Three.js创建的圆柱体的半径和长度

这是我的密码:

HTML:

<script src="http://www.html5canvastutorials.com/libraries/three.min.js"></script>
<div id="container"></div>
这是同样的小提琴:

如果您需要任何其他信息,请告诉我


请建议。

将对象几何体添加到网格后,它将转换为面/顶点/UV/法线,并作为网格的一部分存储。例如,您指定的圆柱体形状被3.js细分(分割)为顶点数超过10000的三角形

因此,虽然可以更新全局网格特性(如变换),但更新单个几何体与在每个动画帧中创建新几何体一样好。如果恰好知道需要修改的顶点,可以直接使用geometry.vertices属性对其进行更新。但如果没有,我认为没有办法

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