M4RKYU.SYSEdition 2027
Skip to content
LOCZH/安大略 · 加拿大/▸logs · implementing 3d graphics in react 9mg待机OK/--:--:--EST
M4M4RK_YUportfolio
  • 创作创作
    创作Overview
    • 作品精选案例与项目记录
    • 游戏可玩原型与游戏开发日志
  • 影像影像
    影像Overview
    • 照片影像合集与视觉实验
    • 商店印刷品、海报和限量物件
  • 写作写作
    写作Overview
    • 博客长篇开发日志与现场笔记
    • 笔记短观察、链接与代码片段
  • 资源资源
    资源Overview
    • 工具38 款浏览器内开发工具
    • 链接每日使用的开发与设计书签
  • 关于关于
  • 联系联系
EN

同步 · dev.to / @markyu

React Three Fiber: Build a 3D Scene Without Fighting React

A code-first React Three Fiber setup with lighting, controls, materials, and the mistakes that make your first scene render black.

发布日期
May 4 '24
·
阅读时长
3 min read
·
点赞
51
·
评论数
1
reactthreejswebdevgraphics
在 dev.to 查看评论

本页目录

  • The Pieces That Matter
  • Do Not Start With a Model Loader
  • Add Motion Carefully
  • Add a Floor
  • My Defaults
  • What I Would Avoid
  • Debug Checklist

If your first React Three Fiber scene renders as a flat black circle, nothing is wrong with you. You probably created geometry without a useful camera, material, light, or controls.

Here is the smallest setup I would start with today.

npm create vite@latest r3f-scene -- --template react
cd r3f-scene
npm install three @react-three/fiber @react-three/drei
npm run dev

Replace src/App.jsx with this:

import { Canvas } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";
import "./App.css";

function Sphere() {
  return (
    <mesh>
      <sphereGeometry args={[1.5, 48, 48]} />
      <meshStandardMaterial color="#e11d48" roughness={0.45} metalness={0.1} />
    </mesh>
  );
}

export default function App() {
  return (
    <main className="stage">
      <Canvas camera={{ position: [0, 1.5, 5], fov: 45 }}>
        <color attach="background" args={["#101114"]} />
        <ambientLight intensity={0.45} />
        <directionalLight position={[4, 6, 3]} intensity={1.8} />
        <Sphere />
        <OrbitControls enableDamping />
      </Canvas>
    </main>
  );
}

And give the canvas real space:

html,
body,
#root {
  width: 100%;
  height: 100%;
  margin: 0;
}

.stage {
  width: 100vw;
  height: 100vh;
  background: #101114;
}

That is already a complete 3D scene: canvas, camera, mesh, geometry, material, light, and controls.

The Pieces That Matter

React Three Fiber is not "Three.js but React-looking." It is a React renderer for Three.js. The JSX tags are still Three concepts.

<mesh>
  <sphereGeometry args={[1.5, 48, 48]} />
  <meshStandardMaterial color="#e11d48" />
</mesh>

The mesh is the object. The geometry is the shape. The material is how the surface responds to light.

If you skip material, you do not have a visible surface worth looking at. If you skip light while using meshStandardMaterial, the object can look black. If you skip camera positioning, your object may be technically there but framed badly.

Do Not Start With a Model Loader

A common beginner mistake is loading a GLB model before you understand the scene. That makes debugging miserable because every issue looks like an asset problem.

Start with a primitive:

function Box() {
  return (
    <mesh rotation={[0.4, 0.4, 0]}>
      <boxGeometry args={[2, 2, 2]} />
      <meshStandardMaterial color="#22c55e" />
    </mesh>
  );
}

If the box renders, your React, canvas, camera, lighting, and CSS are fine. Then add models.

Add Motion Carefully

Animation in React Three Fiber usually belongs in useFrame. Do not animate by putting React state updates in a render loop unless you want unnecessary rerenders.

import { useFrame } from "@react-three/fiber";
import { useRef } from "react";

function SpinningSphere() {
  const ref = useRef();

  useFrame((_, delta) => {
    ref.current.rotation.y += delta * 0.6;
  });

  return (
    <mesh ref={ref}>
      <sphereGeometry args={[1.5, 48, 48]} />
      <meshStandardMaterial color="#38bdf8" roughness={0.35} />
    </mesh>
  );
}

This changes the Three object directly. React does not need to rerender 60 times per second just because a sphere rotates.

Add a Floor

A floating object can look fake even when the code is correct. Add a simple floor so light and position become easier to read.

function Floor() {
  return (
    <mesh rotation={[-Math.PI / 2, 0, 0]} position={[0, -1.6, 0]}>
      <planeGeometry args={[20, 20]} />
      <meshStandardMaterial color="#27272a" />
    </mesh>
  );
}

Then use it:

<Canvas camera={{ position: [0, 1.5, 5], fov: 45 }}>
  <color attach="background" args={["#101114"]} />
  <ambientLight intensity={0.45} />
  <directionalLight position={[4, 6, 3]} intensity={1.8} />
  <SpinningSphere />
  <Floor />
  <OrbitControls enableDamping />
</Canvas>

My Defaults

For a first production-ish scene, I would use:

  • Vite instead of Create React App.
  • sphereGeometry, not old sphereBufferGeometry examples.
  • meshStandardMaterial for realistic light response.
  • OrbitControls while developing, even if you remove it later.
  • One ambient light and one directional light before getting fancy.
  • Fixed canvas height. Most "my scene is blank" bugs are CSS bugs.

What I Would Avoid

I would not start with physics, post-processing, GLTF models, shadows, or scroll-driven camera animation.

Those are fun, but they multiply the number of things that can be wrong. Get one primitive on screen first. Make it lit. Make it move. Then build.

Debug Checklist

  • Canvas has width and height.
  • Canvas is imported from @react-three/fiber.
  • Camera is far enough back to see the object.
  • Material matches your lighting setup.
  • Object is near [0, 0, 0].
  • DevTools console has no asset import errors.
  • Controls are enabled while debugging.

That is the base. Once this renders, everything else is iteration.

相关阅读

Next.js Images Without CLS: My LQIP Blur-Up Setup

A practical Next.js image optimization guide for zero CLS layouts, blur placeholders, dimensions, remote images, and production image hygiene.

nextjs

React 19 Micro-Interactions Without Layout Jank

A practical React 19 micro-interactions guide focused on motion boundaries, CSS transitions, optimistic UI, reduced motion, and performance.

react

Free 3D Asset Sites I’d Actually Use in a WebGL Project

A practical 2026 guide to free 3D asset sources for games, Blender, and WebGL, with license checks, optimization tips, and production workflow advice.

gamedev

原文发布

本文首发于 dev.to,评论与点赞保留在原站。

在 dev.to 继续阅读
上一篇VS Code Themes I Can Actually Code In for 8 HoursA practical 2026 refresh of VS Code themes, focused on readability, contrast, fatigue, and real daily developer use.
返回全部文章
下一篇Cloud Architecture Choices I Would Not OvercomplicateA practical 2026 cloud architecture guide for developers choosing between client-server, distributed systems, microservices, serverless, and cloud-native platforms.
返回档案
M4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYUM4RKYU
始于 2024
ZhenXiao Mark YuZhenXiao Mark Yu
联系

看到什么有意思的?和我聊聊。

这是一个作品集,不是服务 · 但每一条留言我都会看 — 如果哪里让你有所触动,或者只想打个招呼,欢迎写信过来。

开启对话
频道开放

随时打个招呼 · 2026

--:--:--EST加拿大 安大略
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat

订阅

偶尔收到一封简讯

来自 m4rkyu.com 的笔记与日志——简短、标注日期、没有杂音。随时可退订。

作品

线上发布、游戏作品与视觉档案。

  • 项目
  • 游戏
  • 档案
  • 日志

资源

每日好用的工具与个人收藏的链接库。

  • 搜索
  • 最新
  • 工具
  • 链接
  • 笔记
  • 主题
  • 商店
RSSJSON Feed

工作室

背景、联系方式以及合作渠道。

  • 关于
  • 联系
  • 更新日志
  • 技术说明
  • 简历筹备中

社交

在常去的平台上找到我。

  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
  • 邮件
© 2026 ZhenXiao Mark Yumarkyu0615@gmail.com
  • 邮件
  • GitHub
  • dev.to
  • 领英
  • 推特 / X
  • Instagram
  • Facebook
  • YouTube
  • CodePen
  • Spotify
  • Snapchat
隐私条款由 Next.js 16 · React 19 · Tailwind 4 构建