|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as THREE from "three"; |
| 4 | +import { Environment, OrbitControls, useGLTF, useFBX } from "@react-three/drei"; |
| 5 | +import { useFrame, useThree, useLoader } from "@react-three/fiber"; |
| 6 | +import { useStickers } from "./useStickers"; |
| 7 | + |
| 8 | +export const Playground = () => { |
| 9 | + const { stickerMap, setStickerState, normalMap } = useStickers(); |
| 10 | + // const normalMap = useLoader(THREE.TextureLoader, [ |
| 11 | + // "/stickers/decal-normal.jpg", |
| 12 | + // ]); |
| 13 | + // const banana = useFBX(`/model/apple.fbx`) as any; |
| 14 | + // const { nodes } = useGLTF( |
| 15 | + // "https://vazxmixjsiawhamofees.supabase.co/storage/v1/object/public/models/suzanne-high-poly/model.gltf" |
| 16 | + // ); |
| 17 | + // console.log(nodes.Suzanne); |
| 18 | + |
| 19 | + return ( |
| 20 | + <mesh> |
| 21 | + <mesh onClick={(e) => setStickerState(e.uv!)}> |
| 22 | + <icosahedronGeometry args={[2, 20]} /> |
| 23 | + <meshPhysicalMaterial |
| 24 | + map={stickerMap} |
| 25 | + normalMap={normalMap} |
| 26 | + normalScale={new THREE.Vector2(1, 1)} |
| 27 | + // displacementMap={normalMap} |
| 28 | + // displacementScale={2} |
| 29 | + // ここからの設定はmapのalphaを乗算することでmapだけに適用される仕組み |
| 30 | + roughness={1} |
| 31 | + clearcoat={1} |
| 32 | + iridescence={1} |
| 33 | + metalness={1} |
| 34 | + iridescenceIOR={1} |
| 35 | + iridescenceThicknessRange={[0, 1400]} |
| 36 | + // ここまで |
| 37 | + onBeforeCompile={(shader) => { |
| 38 | + // mapのalphaをfloat変数に格納 |
| 39 | + shader.fragmentShader = shader.fragmentShader.replace( |
| 40 | + "#include <map_fragment>", |
| 41 | + ` |
| 42 | + #include <map_fragment> |
| 43 | + float customMapAlpha = sampledDiffuseColor.a; |
| 44 | + ` |
| 45 | + ); |
| 46 | + // iridescenceにmapのalphaをかける(mapだけiridescenceする) |
| 47 | + shader.fragmentShader = shader.fragmentShader.replace( |
| 48 | + "#include <lights_fragment_begin>", |
| 49 | + ` |
| 50 | + #include <lights_fragment_begin> |
| 51 | + material.iridescenceFresnel *= customMapAlpha; |
| 52 | + material.iridescenceF0 *= customMapAlpha; |
| 53 | + ` |
| 54 | + ); |
| 55 | + // クリアコートにmapのalphaをかける(mapだけclearcoatする) |
| 56 | + shader.fragmentShader = shader.fragmentShader.replace( |
| 57 | + "outgoingLight = outgoingLight * ( 1.0 - material.clearcoat * Fcc ) + ( clearcoatSpecularDirect + clearcoatSpecularIndirect ) * material.clearcoat;", |
| 58 | + ` |
| 59 | + outgoingLight = outgoingLight * ( 1.0 - material.clearcoat * Fcc ) + ( clearcoatSpecularDirect + clearcoatSpecularIndirect ) * material.clearcoat * customMapAlpha; |
| 60 | + ` |
| 61 | + ); |
| 62 | + // roughnessにmapのalphaをかける(mapだけroughnessする) |
| 63 | + shader.fragmentShader = shader.fragmentShader.replace( |
| 64 | + "#include <lights_physical_fragment>", |
| 65 | + ` |
| 66 | + #include <lights_physical_fragment> |
| 67 | + material.roughness = clamp(material.roughness * customMapAlpha,.24,1.); |
| 68 | + ` |
| 69 | + ); |
| 70 | + |
| 71 | + // metalnessにmapのalphaをかける(mapだけmetalnessする。あるいは強くする) |
| 72 | + shader.fragmentShader = shader.fragmentShader.replace( |
| 73 | + "#include <metalnessmap_fragment>", |
| 74 | + ` |
| 75 | + #include <metalnessmap_fragment> |
| 76 | + metalnessFactor *= customMapAlpha; |
| 77 | + ` |
| 78 | + ); |
| 79 | + }} |
| 80 | + /> |
| 81 | + </mesh> |
| 82 | + <Environment preset="warehouse" environmentIntensity={0.8} /> |
| 83 | + {/* <mesh scale={100}> |
| 84 | + <sphereGeometry args={[2, 64, 64]} /> |
| 85 | + <meshBasicMaterial map={sticker} side={THREE.BackSide} /> |
| 86 | + </mesh> */} |
| 87 | + <OrbitControls /> |
| 88 | + </mesh> |
| 89 | + ); |
| 90 | +}; |
0 commit comments