Threejs 3d web

From findscripter/everything-skills · View interactive version

Threejs 3d web is an AI agent skill from the GitHub repository findscripter/everything-skills. 当为网页构建交互式 3D 场景、产品配置器、沉浸式作品集或滚动驱动 3D 时使用;做技术选型(Spline/R3F/Three.js)、模型优化、性能调优并产出可运行的 3D 网页代码与降级方案;不适用于纯 3D 游戏引擎(Babylon/Unity)或后端/CAD 建模。触发词:Three.js、React Three Fiber、WebGL、Spline、3D 网站、产品配置器

How to use this skill

Skill file

何时使用

为网页带来「第三维度」:交互式 3D 场景、产品配置器、3D 作品集、沉浸式落地页、滚动驱动的 3D 动画。核心心法是「在 3D 真能增强体验时才用,而非炫技」,并始终在视觉冲击与性能、可用性之间取得平衡。

适用:网页内嵌 3D、Three.js / React Three Fiber (R3F) / Spline / WebGL / GLSL、3D 模型(GLB)集成与优化、滚动联动相机/模型。

不该用(负边界):

步骤

  1. 技术选型:按下表与决策树选栈。

| 工具 | 适合 | 学习曲线 | 控制力 |

|------|------|---------|--------|

| Spline | 快速原型、设计师 | 低 | 中 |

| React Three Fiber | React、复杂场景 | 中 | 高 |

| Three.js 原生 | 极致控制、非 React | 高 | 最高 |

| Babylon.js | 游戏、重度 3D | 高 | 最高 |

  1. 准备模型管线:Blender 建模 → 降面(网页 < 100K 面) → 烘焙贴图合并材质 → 导出 GLB → Draco + WebP 压缩 → 校验体积(理想 < 5MB)。格式优先 GLB/GLTF(体积最小),其次 OBJ;USDZ 用于 Apple AR。
  1. 搭建场景:R3F 用 <Canvas> + <Suspense> + useGLTF 加载,加上灯光与 OrbitControls;Spline 直接嵌 <Spline scene=.../>。
  1. 加交互/滚动:用 ScrollControls + useScroll + useFrame 联动,或 Three.js + GSAP ScrollTrigger 驱动相机。
  1. 性能与降级:见「注意事项」,必须配 loading UI 与 WebGL 不支持时的静态图兜底。

指令

# 安装 gltf-transform 并压缩模型(Draco 几何压缩 + WebP 贴图压缩)
npm install -g @gltf-transform/cli
gltf-transform optimize input.glb output.glb \
  --compress draco \
  --texture-compress webp

示例

Spline(最快上手):

import Spline from '@splinetool/react-spline';
export default function Scene() {
  return <Spline scene="https://prod.spline.design/xxx/scene.splinecode" />;
}

R3F 加载模型 + loading 指示器(务必用 Suspense):

import { Canvas } from '@react-three/fiber';
import { OrbitControls, useGLTF, useProgress, Html } from '@react-three/drei';
import { Suspense } from 'react';

function Model() {
  const { scene } = useGLTF('/model.glb');
  return <primitive object={scene} />;
}
function Loader() {
  const { progress } = useProgress();
  return <Html center>{progress.toFixed(0)}%</Html>;
}
export default function Scene() {
  return (
    <Canvas>
      <ambientLight />
      <Suspense fallback={<Loader />}>
        <Model />
      </Suspense>
      <OrbitControls />
    </Canvas>
  );
}

滚动驱动(R3F ScrollControls):

import { ScrollControls, useScroll } from '@react-three/drei';
import { useFrame } from '@react-three/fiber';
import { useRef } from 'react';

function RotatingModel() {
  const scroll = useScroll();
  const ref = useRef();
  useFrame(() => { ref.current.rotation.y = scroll.offset * Math.PI * 2; });
  return <mesh ref={ref}>...</mesh>;
}
// <Canvas><ScrollControls pages={3}><RotatingModel /></ScrollControls></Canvas>

GSAP + Three.js 相机随滚动移动:

import gsap from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';
gsap.to(camera.position, {
  scrollTrigger: { trigger: '.section', scrub: true },
  z: 5, y: 2,
});

移动端降配 + WebGL 兜底:

const isMobile = /iPhone|iPad|Android/i.test(navigator.userAgent);
// <Canvas dpr={isMobile ? 1 : 2} performance={{ min: 0.5 }} />
// 无 WebGL 时返回 <img src="/fallback.png" alt="3D preview" />

注意事项

性能目标(始终优化,3D 很贵):

| 设备 | 目标帧率 | 最大三角面 |

|------|---------|-----------|

| 桌面 | 60fps | 500K |

| 移动 | 30-60fps | 100K |

| 低端 | 30fps | 50K |

提速要点:重复物体用 Instances;灯光尽量少(一个 ambientLight + 一个 directionalLight);用 LOD 分级;模型懒加载。

常见校验项(落地前自查):

通用约束:本技能产出不替代环境内的真实测试与专家评审;输入、权限、安全边界或成功标准缺失时,先停下询问。

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.