Three.js r187
Three.js r187 已于 2026 年 10 月底发布,核心新增 WebGPU 光照烘焙工具 LightMapper、unwrapUVs 展开工具、WebGPURenderer 并行管线编译与 BatchedMesh 合并绘制优化,并移除了 r177 中标记弃用的旧接口。 安装方式与 r186 基本一致,下面按“生产项目 → 快速验证 → 升级注意”的顺序整理。
1. 生产项目推荐:npm + Vite
这是官方长期推荐的方式,适合正式项目、团队协作和后续接入加载器、控制器、后期处理等扩展。
# 1. 准备 Node.js 环境
node -v
npm -v
# 2. 创建 Vite 项目
npm create vite@latest my-three-demo -- --template vanilla
# 3. 进入项目
cd my-three-demo
npm install
# 4. 安装 Three.js r187
npm install three@0.187.0
# 5. 启动开发服务器
npm run devmain.js 示例:
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 1, 3);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({ color: 0x44aaff });
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
const light = new THREE.DirectionalLight(0xffffff, 2);
light.position.set(2, 5, 3);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.6));
function animate() {
requestAnimationFrame(animate);
mesh.rotation.y += 0.005;
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});CSS 建议:
body {
margin: 0;
overflow: hidden;
}2. 快速验证:CDN + importmap
如果只是跑一个单页 Demo,可以用 CDN。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>Three.js r187 Demo</title>
<style>body { margin: 0; overflow: hidden; }</style>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.187.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.187.0/examples/jsm/"
}
}
</script>
</head>
<body>
<script type="module" src="./main.js"></script>
</body>
</html>注意:CDN 方式不能直接双击打开 HTML 文件,需要用本地服务器。例如:
npx serve .
# 或
python -m http.server3. 使用 WebGPU 渲染器
r187 的 WebGPU 渲染器继续优化,新增了 LightMapper 和并行管线编译能力。 如果浏览器支持 WebGPU,可以这样切换:
import * as THREE from 'three/webgpu';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const renderer = new THREE.WebGPURenderer({ antialias: true });
await renderer.init();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);如果仍需兼容旧浏览器,继续使用 THREE.WebGLRenderer 即可。
4. r187 升级与排错要点
- 版本锁定:生产环境建议写死
three@0.187.0,避免自动升级导致 API 变化。 - addon 路径:r187 推荐使用
three/addons/,例如three/addons/controls/OrbitControls.js。 - 旧 API 移除:r187 移除了 r177 中已标记弃用的代码,升级旧项目时要重点检查控制台报错。
- 不要混版本:核心库、addon、CDN 路径必须使用同一版本,否则容易出现模块解析失败或运行时异常。
- 常见报错:
Failed to resolve module specifier "three":未使用构建工具,也未配置 importmap。THREE is not defined:仍在使用旧版全局引入方式,r187 默认走 ES Module。- 模型/贴图加载 404:资源必须放在 Vite 项目的
public/目录,或通过服务器提供。 - WebGPU 不可用:当前浏览器或显卡不支持时,回退到
WebGLRenderer。
5. 验证是否安装成功
运行项目后,如果能看到一个可旋转的立方体,并且控制台没有报错,说明 r187 已正常安装。也可以用:
console.log('Three.js version:', THREE.REVISION);确认输出为 187。