threejs一个功能强大的JavaScript 3D库

three.js

JavaScript 3D library

该项目的目标是创建一个易于使用、轻量级、跨浏览器的通用 3D 库。当前版本仅包含 WebGL 和 WebGPU 渲染器,但 SVG 和 CSS3D 渲染器也可作为插件使用。

用法

此代码创建了一个场景、一个相机和一个几何立方体,并将立方体添加到场景中。然后,它为场景和相机创建一个 WebGL 渲染器,并将该视口添加到 document.body 元素中。最后,它在场景中为相机设置立方体的动画。

javascript 复制代码
import * as THREE from 'three';

const width = window.innerWidth, height = window.innerHeight;
// init
const camera = new THREE.PerspectiveCamera( 70, width / height, 0.01, 10 );
camera.position.z = 1;

const scene = new THREE.Scene();

const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshNormalMaterial();

const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );

const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
// animation
function animate( time ) {

	mesh.rotation.x = time / 2000;
	mesh.rotation.y = time / 1000;

	renderer.render( scene, camera );
}

如果一切顺利,你应该会看到这个

克隆此代码库

克隆此代码库及其所有历史记录大约需要下载 2 GB 的文件。如果您不需要完整历史记录,可以使用“depth”参数来显著减少下载大小。

sh 复制代码
git clone --depth=1 https://github.com/mrdoob/three.js.git

关于项目

该项目的目标是创建一个易于使用、轻量级、跨浏览器的通用 3D 库。当前版本仅包含 WebGL 和 WebGPU 渲染器,但 SVG 和 CSS3D 渲染器也可作为插件使用。
MIT
Javascript
108,905
36025
2531
2010-03-24
2025-10-10

增长趋势 - stars