Skip to content

Commit

Permalink
feat: マテリアルをトゥーン調に変更
Browse files Browse the repository at this point in the history
  • Loading branch information
shun-shobon committed Nov 5, 2024
1 parent 5fa3771 commit a41859d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
38 changes: 34 additions & 4 deletions app/components/ModelLoad.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Environment, OrbitControls, useGLTF } from "@react-three/drei";
import { OrbitControls, useGLTF } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { Bloom, EffectComposer } from "@react-three/postprocessing";
import { type ReactNode, useEffect, useRef } from "react";
import type { Group, MeshStandardMaterial } from "three";
import * as THREE from "three";
Expand All @@ -10,6 +11,7 @@ const HAIR_MESH_NAMES = [
"hairback",
"hairfront",
"hairtail",
"hairside",
"hairsid",
"hair",
] as const;
Expand All @@ -36,6 +38,29 @@ function useCharacterSetting(setting: CharacterSetting) {
const modelPath = `/models/web_${setting.character}.glb`;

const { scene } = useGLTF(modelPath);

useEffect(() => {
const threeTone = new THREE.TextureLoader().load("/assets/threeTone.jpg");
threeTone.minFilter = THREE.NearestFilter;
threeTone.magFilter = THREE.NearestFilter;

scene.traverse((child) => {
if (!(child as THREE.Mesh).isMesh) return;
const mesh = child as THREE.Mesh;
const oldMaterial = mesh.material as MeshStandardMaterial;
const newMaterial = new THREE.MeshToonMaterial({
name: oldMaterial.name,
color: oldMaterial.color,
gradientMap: threeTone,
map: oldMaterial.map,
emissive: oldMaterial.emissive,
emissiveIntensity: oldMaterial.emissiveIntensity,
emissiveMap: oldMaterial.emissiveMap,
});
mesh.material = newMaterial;
});
}, [scene]);

useEffect(() => {
scene.traverse((child) => {
if (!(child as THREE.Mesh).isMesh) return;
Expand Down Expand Up @@ -83,15 +108,20 @@ export function ModelViewer({ characterSetting }: ModelProps): ReactNode {
<Canvas
className="aspect-square h-auto w-full sm:max-h-[50dvh]"
scene={{
background: new THREE.Color(0xffffff),
background: new THREE.Color("#000000"),
}}
camera={{
position: [0, 0, 2],
fov: 30,
}} // カメラの初期位置と視野角を設定
>
{/* 環境を設定*/}
<Environment preset="lobby" />
{/* ライトを設定 */}
<ambientLight />
<directionalLight position={[6, 5, 5]} intensity={1} />
{/* ポストプロセッシング */}
<EffectComposer>
<Bloom intensity={1} luminanceThreshold={1} radius={0.8} mipmapBlur />
</EffectComposer>
{/* GLBモデルの読み込みと表示 */}
<Model characterSetting={characterSetting} />
{/* カメラコントロールの追加(ユーザーが自由にカメラを操作できるようにする) */}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@radix-ui/react-slot": "1.1.0",
"@react-three/drei": "9.114.6",
"@react-three/fiber": "8.17.10",
"@react-three/postprocessing": "2.16.3",
"@remix-run/react": "2.13.1",
"@supabase/supabase-js": "2.45.6",
"@types/three": "0.169.0",
Expand Down
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added public/assets/threeTone.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a41859d

Please sign in to comment.