写一个桥梁模型的three.js
时间: 2023-12-26 08:53:16 浏览: 97
使用Three.js 绘制3D模型
以下是一个简单的桥梁模型的 Three.js 代码:
```javascript
// 创建场景
var scene = new THREE.Scene();
// 创建相机
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 10, 30);
// 创建渲染器
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建桥梁
var bridgeGeometry = new THREE.BoxGeometry(20, 1, 2);
var bridgeMaterial = new THREE.MeshBasicMaterial({ color: 0x808080 });
var bridge = new THREE.Mesh(bridgeGeometry, bridgeMaterial);
bridge.position.set(0, 0, 0);
scene.add(bridge);
// 创建桥墩
var pillarGeometry = new THREE.CylinderGeometry(0.5, 0.5, 10, 32);
var pillarMaterial = new THREE.MeshBasicMaterial({ color: 0x808080 });
var pillar1 = new THREE.Mesh(pillarGeometry, pillarMaterial);
pillar1.position.set(-8, -5, 0);
scene.add(pillar1);
var pillar2 = new THREE.Mesh(pillarGeometry, pillarMaterial);
pillar2.position.set(8, -5, 0);
scene.add(pillar2);
// 创建光源
var light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(0, 10, 0);
scene.add(light);
// 渲染场景
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
```
这个代码创建了一个简单的桥梁和两个桥墩,并在场景中添加了一个点光源。你可以调整相机的位置和角度,以查看不同的视角。
阅读全文