React 使用 three.js 加载 gltf 3D模型 | three.js 入门

系列文章

示例项目(gitcode):https://gitcode.com/qq_41456316/simple-react-three-demo


文章目录

  • 系列文章
  • 前言
  • 一、three.js是什么?
  • [二、使用 React 和 three.js 加载 glTF 3D 模型的步骤](#二、使用 React 和 three.js 加载 glTF 3D 模型的步骤)
    • [步骤 1:创建 React 应用](#步骤 1:创建 React 应用)
    • [步骤 2:安装 three.js](#步骤 2:安装 three.js)
    • [步骤 3:准备 glTF 3D 模型文件](#步骤 3:准备 glTF 3D 模型文件)
    • [步骤 4:创建组件加载 3D 模型](#步骤 4:创建组件加载 3D 模型)
    • [步骤 5:在应用中使用组件](#步骤 5:在应用中使用组件)
    • [步骤 6:运行效果](#步骤 6:运行效果)
    • 总结

前言

在当今的软件开发中,3D 技术已经成为了一个备受关注的领域。无论是游戏、虚拟现实还是增强现实,3D 技术都扮演着至关重要的角色。而在 Web 开发领域,随着 WebGL 和 WebGPU 技术的发展,通过浏览器来展示和交互 3D 模型也变得越来越普遍。在这篇博文中,我们将探讨如何在 React 项目中使用 three.js 加载 glTF 格式的 3D 模型,并为您提供 three.js 的入门指南。


一、three.js是什么?

React :React 是一个由 Facebook 推出的用于构建用户界面的 JavaScript 库。它以其简单性、灵活性和高效性而闻名,被广泛应用于 Web 开发中。
three.js :three.js 是一个基于 WebGL 的 JavaScript 3D 库,提供了在浏览器中创建和展示 3D 场景的丰富功能和 API。
glTF:glTF (GL Transmission Format) 是一种用于将 3D 模型和场景进行传输的开放标准格式,旨在提供高效的文件大小和快速加载速度。

二、使用 React 和 three.js 加载 glTF 3D 模型的步骤

步骤 1:创建 React 应用

首先,确保您已经安装了 Node.js 和 npm。然后,使用 Create React App 来创建一个新的 React 应用:

bash 复制代码
npx create-react-app simple-react-three-demo
cd simple-react-three-demo

步骤 2:安装 three.js

在 React 应用的根目录下,通过 npm 安装 three.js:

bash 复制代码
npm install three

步骤 3:准备 glTF 3D 模型文件

您可以在网上找到许多免费的 glTF 3D 模型资源。确保您已经下载了您想要使用的 glTF 模型,并将其放置在您的 React 项目的 public 文件夹中。示例中使用模型文件的下载地址如下

https://sketchfab.com/3d-models/just-a-girl-b2359160a4f54e76b5ae427a55d9594d

步骤 4:创建组件加载 3D 模型

在src文件夹创建components文件夹,创建文件ThreeContainer.js,在文件中创建一个ThreeContainer组件来加载和展示 glTF 3D 模型:

js 复制代码
import React, { useEffect, useRef } from "react";
import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
// import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

function ThreeContainer({ style }) {
  const defaultStyle = {
    height: "100vh",
    width: "100vw",
    backgroundColor: "transparent",
  };
  const isContainerRunning = useRef(false);
  const containerRef = useRef(null);
  useEffect(() => {
    if (!isContainerRunning.current && containerRef.current) {
      isContainerRunning.current = true;
      const containerWidth = containerRef.current.offsetWidth;
      const containerHeight = containerRef.current.offsetHeight;
      const loader = new GLTFLoader();
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(
        75, // 视野角度
        containerWidth / containerHeight, //宽高比
        0.1, // 近裁剪面
        1000 // 远裁剪面
      );
      // 生成渲染器
      const renderer = new THREE.WebGLRenderer({ alpha: true });
      renderer.setSize(containerWidth * 0.9995, containerHeight * 0.9995);
      containerRef.current.appendChild(renderer.domElement);
      // const controls = new OrbitControls(camera, renderer.domElement);
      loadModel(loader, scene);
      animate(isContainerRunning, camera, renderer, scene);

      function animate(runningFlag, camera, renderer, scene) {
        if (runningFlag.current) {
          requestAnimationFrame(() =>
            animate(runningFlag, camera, renderer, scene)
          );

          const radius = 140;
          const angle = Date.now() * 0.0005;
          const x = Math.cos(angle) * radius;
          const z = Math.sin(angle) * radius;
          camera.position.set(x, 70, z);
          camera.lookAt(0, 50, 0);

          renderer.render(scene, camera);
        }
      }

      function loadModel(loader, scene) {
        loader.load(
          "just_a_girl/scene.gltf",
          function (gltf) {
            scene.add(gltf.scene);
          },
          // called while loading is progressing
          function (xhr) {
            console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
          },
          // called when loading has errors
          function (error) {
            console.log("An error happened");
          }
        );
      }
    }
  }, []);

  return (
    <div
      ref={containerRef}
      id="container"
      style={{ ...defaultStyle, ...style }}
    />
  );
}

export default ThreeContainer;

步骤 5:在应用中使用组件

在您的应用中使用这个组件来展示 3D 模型,修改src/App.js内容如下:

js 复制代码
import "./App.css";
import ThreeContainer from "./components/ThreeContainer";

function App() {
  return (
    <div>
      <ThreeContainer />
    </div>
  );
}

export default App;

步骤 6:运行效果

npm start运行项目,效果如下

总结

通过本文,我们学习了如何在 React 应用中使用 three.js 加载 glTF 3D 模型。这是一个简单而强大的方法,使您能够将引人入胜的 3D 内容整合到您的 Web 应用中,为用户提供更丰富、更交互的体验。希望这篇指南能够帮助您开始使用 three.js 和 React 来创建令人惊叹的 3D Web 应用程序!

以上就是本次博文的全部内容,希望能对您有所帮助。如果您对任何内容有疑问或需要进一步了解,请随时在评论区留言,我会尽力为您解答。感谢您的阅读!

相关推荐
爱学习的程序媛15 分钟前
【Web前端】JavaScript设计模式全解析
前端·javascript·设计模式·web
老黑33 分钟前
开源工具 AIDA:给 AI 辅助开发加一个数据采集层,让 AI 从错误中自动学习(Glama 3A 认证)
前端·react.js·ai·nodejs·cursor·vibe coding·claude code
薛先生_09939 分钟前
js学习语法第一天
开发语言·javascript·学习
苦瓜小生1 小时前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript
和沐阳学逆向1 小时前
我现在怎么用 CC Switch 管中转站,顺手拿 Codex 举个例子
开发语言·javascript·ecmascript
阿酷tony2 小时前
Nano Banna 提示词:创意超逼真的3D商业风格产品图
人工智能·3d·gemini·图片生成
智算菩萨3 小时前
【OpenGL】10 完整游戏开发实战:基于OpenGL的2D/3D游戏框架、物理引擎集成与AI辅助编程指南
人工智能·python·游戏·3d·矩阵·pygame·opengl
kgduu4 小时前
js之客户端存储
javascript·数据库·oracle
四千岁4 小时前
2026 最新版:WSL + Ubuntu 全栈开发环境,一篇搞定!
javascript·node.js
竹林8184 小时前
从“连接失败”到丝滑登录:我用 ethers.js 连接 MetaMask 的完整踩坑实录
前端·javascript