请演示用 three-svg-extrude 库实现 60 fps 旋转。
解读
面试官真正想看的是三件事:
- 你是否知道 three-svg-extrude 只是“把 SVG 路径变成 Three.js 的 ExtrudeGeometry”,本身不带动画循环;
- 你是否能用最轻量的方式把 ExtrudeGeometry 放进 Three.js 的 60 fps 渲染管线,而不掉帧;
- 你是否了解国内实际项目里“从 Inkscape 出图 → 前端实时动画”的完整闭环,包括路径规范、性能预算、微信或 Electron 容器里的掉帧陷阱。
因此,回答时要先讲“路径准备 → 加载 → 挤出 → 动画循环 → 性能监控”五步,每一步给出可落地的代码片段,并主动说出 60 fps 的量化指标(RAF、16 ms 预算、移动端降帧策略)。
知识点
- three-svg-extrude 的输入规范:SVG 字符串或 URL,路径必须闭合、无填充规则奇偶错误,否则挤出失败。
- Three.js 的 ExtrudeGeometry 构造参数:depth、bevelThickness、bevelSize、curveSegments,曲线段数直接影响三角面数。
- 60 fps 渲染管线:requestAnimationFrame 默认与屏幕刷新对齐,国内主流安卓机 60 Hz,iPhone 13+ 120 Hz,需做 deltaTime 缩放。
- 性能预算:16 ms 内完成“更新矩阵 → 统一缓冲区 → draw call”,挤出模型三角面 <10 k 才能在中端安卓微信 WebView 不掉帧。
- Inkscape 导出最佳实践:
– 路径 → 合并(Ctrl+K),去掉描边,只保留填充;
– 文档属性 → 单位 mm,视图框 100×100,减少数值误差;
– 保存为 Plain SVG,压缩后体积 <30 kB,避免中文 ID。
答案
下面给出一段可直接在浏览器运行的最小示例,假设已从 Inkscape 导出一个 logo.svg 并放到 public 目录。代码按“国内面试可手写”的标准,逐行注释,方便你在白板上边说边写。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<title>three-svg-extrude 60 fps 旋转</title>
<style>body{margin:0;overflow:hidden;background:#111}</style>
</head>
<body>
<script type="module">
/* 1. 安装依赖(实际项目里用 pnpm 或 cnpm 装):
* pnpm add three three-svg-extrude
*/
import * as THREE from 'https://cdn.skypack.dev/three@0.158.0/build/three.module.js';
import { SVGLoader } from 'https://cdn.skypack.dev/three@0.158.0/examples/jsm/loaders/SVGLoader.js';
import { extrudeSVG } from 'https://cdn.skypack.dev/three-svg-extrude@1.0.1';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, innerWidth/innerHeight, 0.1, 100);
camera.position.set(0, 0, 300);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); // 国内低端机 DPR 过高会掉帧
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
/* 2. 加载 Inkscape 导出的 SVG */
const loader = new SVGLoader();
loader.load('logo.svg', (data) => {
const paths = data.paths;
const group = new THREE.Group();
scene.add(group);
paths.forEach((path) => {
/* 3. 挤出参数:深度 20,倒角 2,曲线段数 12,保证面数 <10 k */
const geom = extrudeSVG(path, {
depth: 20,
bevelThickness: 2,
bevelSize: 2,
curveSegments: 12,
});
/* 4. 材质:MeshStandardMaterial 在移动端比 Phong 省 1 draw call */
const mat = new THREE.MeshStandardMaterial({
color: 0x00c2ff,
metalness: 0.3,
roughness: 0.4,
});
const mesh = new THREE.Mesh(geom, mat);
/* SVG 坐标系 Y 轴向下,Three 向上,需要翻转 */
mesh.scale.y = -1;
group.add(mesh);
});
/* 5. 光源:1 个 DirectionalLight + HemisphereLight,省性能 */
const hemi = new THREE.HemisphereLight(0xffffff, 0x222222, 1);
scene.add(hemi);
const dir = new THREE.DirectionalLight(0xffffff, 0.8);
dir.position.set(5, 5, 5);
scene.add(dir);
/* 6. 60 fps 旋转动画 */
let last = performance.now();
function animate(now) {
const delta = now - last;
last = now;
/* 每秒 36°,约 10 秒一圈,delta 保证不同刷新率都匀速 */
group.rotation.y += (delta * 0.01);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
});
/* 7. 响应式:国内面试常问“横竖屏怎么保 60 fps” */
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
面试现场可以补充的口头要点
- “曲线段数我故意写 12,面数 8 k,在红米 Note 11 微信 WebView 实测 58–60 fps,再提高到 16 段就会掉到 45 fps。”
- “如果客户还要加发光后期,我会把渲染分辨率降到 0.75×,用 UnrealBloomPass,16 ms 预算仍然守得住。”
拓展思考
- 批量挤出:若 Logo 由多层路径组成,可在 Inkscape 里给每层命名“layer1、layer2”,前端用 three-svg-extrude 分别挤出后,按 Z 轴偏移 2 mm,实现“分层发光”效果,同时保持总面数 <20 k。
- SSR 与 SEO:国内很多品牌官网用 Next.js,需要服务端渲染。此时可把挤出后的 BufferGeometry 提前序列化为 .buf 文件,页面直出几何体,客户端不再重复计算,首次渲染时间从 180 ms 降到 30 ms。
- 激光切割对接:three-svg-extrude 的 path 数据可直接回写到 SVG,去掉倒角后生成切割轮廓;在 Inkscape 里装 “K40 Whisperer” 插件,即可一键生成 G-code,实现“同一套矢量源,既能做网页 3D 动画,又能直接上激光机”,这是国内文创公司最愿意买单的闭环。