编程语言
首页 > 编程语言> > javascript-在运行时更新使用Three.js创建的3D圆柱体的高度/半径

javascript-在运行时更新使用Three.js创建的3D圆柱体的高度/半径

作者:互联网

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

我们如何在使用Three.js创建的Cylinder运行时更改半径和长度

这是我的代码:

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/

让我知道您是否需要其他信息.

请提出建议.

解决方法:

将对象几何体添加到网格后,它将转换为面/顶点/ UV /法线并存储为网格的一部分.例如,您指定的圆柱形状被Three.js细分(划分)为顶点数大于10,000的三角形.

因此,虽然可以更新诸如变换之类的全局网格属性,但更新单个几何图形与在每个动画帧中创建一个新几何图形一样好.如果您恰好确切地知道需要修改的顶点,则可以使用geometry.vertices属性直接更新它.但是,如果没有,我认为没有办法.

标签:three-js,user-input,javascript
来源: https://codeday.me/bug/20191029/1963427.html