three.js 开门
时间: 2023-09-10 08:05:40 浏览: 84
three.js是一个用于创建3D图形的JavaScript库。它提供了各种功能,如3D模型加载、相机控制、光源控制、材质和纹理等。下面是一个简单的three.js入门示例:
1. 创建场景(scene):
```
const scene = new THREE.Scene();
```
2. 创建相机(camera):
```
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
```
3. 创建渲染器(renderer):
```
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
```
4. 创建立方体(cube):
```
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
```
5. 渲染场景:
```
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
```
这个示例创建了一个绿色的立方体,并且让它在屏幕上旋转。你可以在网站上查看完整示例:https://threejs.org/docs/#manual/en/introduction/Creating-a-scene。
阅读全文