three.js + react 搭建万圣节孤岛🎃

搭建项目环境

bash 复制代码
npm create vite@latest my-vite-app
bash 复制代码
cd my-vite-app
bash 复制代码
npm install
bash 复制代码
npm run dev

安装 three

参考版本:

"@react-three/drei": "^9.88.0"

"@react-three/fiber": "^8.14.5"

"gltfjsx": "^6.2.13",

"react": "^18.2.0"

"three": "^0.157.0"


这个模型嘛,当然是用免费滴: sketchfab.com/3d-models

我们该如何使用这个模型呢

举例:sketchfab.com/3d-models/h...

解压之后,移动到 src/assets 目录下,终端目录定位到该目录下,执行

bash 复制代码
npx gltfjsx scene.gltf --transform

顺利的话,就会得到以下的目录

打开 Scene.jsx 文件,对 scene-transformed.glb 路径进行替换

js 复制代码
//....
import scenePath from "./scene-transformed.glb";

export function Lantern(props) {
  const { nodes, materials } = useGLTF(scenePath);

  return (
    // ....
  );
}

useGLTF.preload(scenePath);

Home.jsx

js 复制代码
import { Canvas } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
import { Lantern } from "../../assets/halloween_lantern/Scene";
import "./Halloween.css";

const Halloween = () => {
  return (
    <div className="halloween-container">
      <Canvas
        camera={{
          fov: 90,
        }}
      >
        <ambientLight intensity={1} />
        <pointLight color={"orange"} intensity={1} position={[2, 3, 4]} />
        <OrbitControls />
        <Lantern position={[0, 0.3, 0]} scale={[0.8, 0.8, 0.8]} />
      </Canvas>
    </div>
  );
};

export default Halloween;

加载第二个模型,按照上面的步骤

来点动画效果

在 useEffect hook 中执行动画

js 复制代码
export function Fire(props) {
  const group = useRef();
  const { nodes, materials, animations } = useGLTF(scenePath);
  const { actions } = useAnimations(animations, group);

  useEffect(() => {
    actions.Animation.play()
  }, [actions]);

  return (
    //...
  );
}

完整代码:gitee.com/suiboyu/thr...

路由切换到 http://localhost:5173/halloween

相关推荐
IT_陈寒1 小时前
Python异步编程的7个致命误区:90%开发者踩过的坑及高效解决方案
前端·人工智能·后端
猫猫村晨总1 小时前
整理了几道前端面试题
前端·vue.js·面试
江拥羡橙1 小时前
【目录-多选】鸿蒙HarmonyOS开发者基础
前端·ui·华为·typescript·harmonyos
你的电影很有趣1 小时前
lesson55:CSS导航组件全攻略:从基础导航条到动态三级菜单与伸缩菜单实现
前端·css
蔗理苦1 小时前
2025-09-05 CSS4——浮动与定位
开发语言·前端·css·html·css3
浊浪载清辉2 小时前
《Html泛型魔法学院:用霍格沃茨风格网页教授集合框架》
前端·javascript·学习·html
Want5952 小时前
HTML元素周期表
前端·html
一只一只妖5 小时前
突发奇想,还未实践,在Vben5的Antd模式下,将表单从「JS 配置化」改写成「模板可视化」形式(豆包版)
前端·javascript·vue.js
悟能不能悟8 小时前
js闭包问题
开发语言·前端·javascript
秋秋_瑶瑶8 小时前
vue-amap组件呈现的效果图如何截图
前端·javascript·vue-amap