![](/img/trans.png)
[英]Change width/height/length of 3D Cube created with Three.js at runtime
[英]Update height/radius of 3D Cylinder created with Three.js at runtime
就像我們在這個小提琴的運行時改變 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/
如果您需要任何其他信息,請告訴我。
請建議。
將對象幾何體添加到網格后,它將轉換為面/頂點/ UV /法線並存儲為網格的一部分。 例如,您指定的圓柱形狀將由Three.js細分(划分)為頂點數超過10,000的三角形。
因此,雖然可以更新像變換這樣的全局網格屬性,但更新單個幾何圖形與在每個動畫幀中創建新幾何圖形一樣好。 如果您恰好知道需要修改的頂點,可以使用geometry.vertices屬性直接更新它。 但如果沒有,我認為沒有辦法。
您可以嘗試通過執行以下操作來覆蓋 object 的幾何圖形:
cylinder.geometry.dispose();
cylinder.geometry = new THREE.CylinderGeometry(botRad, topRad, height, 32);
其中:botRad、topRad 和 height 是來自初始值的新值,或者如果您計划在振盪圓柱體高度上不斷變化的變量。
dispose 確保它在覆蓋新幾何之前刪除 object 的先前幾何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.