npx create-react-app my-avatar-app --template typescript
コマンドでReactプロジェクトを作成cd my-avatar-app
でプロジェクトフォルダに移動npm install three react-three-fiber @react-three/drei three-stdlib
コマンドで必要なライブラリを一気にインストールする
プロジェクトの起動my-avatar-app
├─ public
│ ├─ assets
│ │ ├─ idle.fbx
│ │ └─ ...
│ └─ ...
└─ src
└─ ...
const fbx = useLoader(FBXLoader, process.env.PUBLIC_URL + "/assets/idle.fbx");
function Avatar() {
const fbx = useLoader(FBXLoader, process.env.PUBLIC_URL + "/assets/idle.fbx");
const mixer = useRef<AnimationMixer>();
// アニメーションを適用
if (fbx.animations.length > 0 && !mixer.current) {
mixer.current = new AnimationMixer(fbx);
const action = mixer.current.clipAction(fbx.animations[0]);
action.play();
}
// アニメーションの制御を行うupdate関数
const update = (delta: number) => {
if (mixer.current) {
mixer.current.update(delta);
}
};
// useFrameフックを使用して、update関数を毎フレーム実行
useFrame((_, delta) => {
update(delta);
});
return <primitive object={fbx} />;
}
<Canvas style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }}>
<Suspense fallback={null}>
<Avatar />
</Suspense>
</Canvas>
<Canvas style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }}>
<Suspense fallback={null}>
<Avatar />
</Suspense>
<PerspectiveCamera makeDefault position={[0, 150, 300]} />
<OrbitControls target={[0, 50, 0]} />
<ambientLight intensity={0.5} />
<directionalLight color="white" intensity={0.6} position={[0, 50, 50]} />
</Canvas>
import { useRef } from "react";
import { FBXLoader } from "three-stdlib";
import { useLoader, useFrame } from "@react-three/fiber";
import { AnimationMixer } from "three";
function Avatar() {
const fbx = useLoader(FBXLoader, process.env.PUBLIC_URL + "/assets/idle.fbx");
const mixer = useRef<AnimationMixer>();
// アニメーションを適用
if (fbx.animations.length > 0 && !mixer.current) {
mixer.current = new AnimationMixer(fbx);
const action = mixer.current.clipAction(fbx.animations[0]);
action.play();
}
// アニメーションの制御を行うupdate関数
const update = (delta: number) => {
if (mixer.current) {
mixer.current.update(delta);
}
};
// useFrameフックを使用して、update関数を毎フレーム実行
useFrame((_, delta) => {
update(delta);
});
return <primitive object={fbx} />;
}
export default Avatar;
import React, { Suspense } from "react";
import { Canvas } from "@react-three/fiber";
import { PerspectiveCamera, OrbitControls } from "@react-three/drei";
import Avatar from "./Avatar";
const App = () => {
return (
<Canvas style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }}>
<Suspense fallback={null}>
<Avatar />
</Suspense>
<PerspectiveCamera makeDefault position={[0, 150, 300]} />
<OrbitControls target={[0, 50, 0]} />
<ambientLight intensity={0.5} />
<directionalLight color="white" intensity={0.6} position={[0, 50, 50]} />
</Canvas>
);
};
export default App;
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
body {
margin: 0;
overflow: hidden;
}
#root,
canvas {
display: block;
width: 100%;
height: 100%;
}