three.js 许多岩石掉落
时间: 2023-07-21 13:28:24 浏览: 187
three.js-r125.zip
要在three.js中制作多个岩石掉落的效果,你需要使用THREE.Geometry和THREE.Mesh结合使用,以及THREE.Vector3来控制物体的位置和运动。以下是一个简单的例子:
```
var rockCount = 10;
var rocks = [];
for (var i = 0; i < rockCount; i++) {
var size = Math.random() * 2 + 1;
var rock = new THREE.BoxGeometry(size, size, size);
var rockMaterial = new THREE.MeshPhongMaterial({ color: 0x808080 });
var rockMesh = new THREE.Mesh(rock, rockMaterial);
rockMesh.position.set(Math.random() * 100 - 50, Math.random() * 50 + 50, Math.random() * 100 - 50);
rockMesh.velocity = new THREE.Vector3(0, -Math.random() * 2 - 1, 0);
rocks.push(rockMesh);
scene.add(rockMesh);
}
function animate() {
requestAnimationFrame(animate);
for (var i = 0; i < rockCount; i++) {
var rock = rocks[i];
rock.position.add(rock.velocity);
if (rock.position.y < -50) {
rock.position.set(Math.random() * 100 - 50, Math.random() * 50 + 50, Math.random() * 100 - 50);
rock.velocity = new THREE.Vector3(0, -Math.random() * 2 - 1, 0);
}
}
renderer.render(scene, camera);
}
animate();
```
这将创建10个大小不同的岩石,并将它们随机放置在空间中。然后,它们将向下移动,直到它们到达底部,然后将它们随机放置在空间中,并再次向下移动。你可以通过调整岩石的数量、大小、颜色、速度和运动方向来自定义它。
阅读全文