実現したいこと
Next.jsとThree.jsで3D空間に部屋を作り、
その部屋内にある扉をクリックすると開くようにしたいです。
発生している問題・分からないこと
クリックしても扉が回転しません。
consoleに"Clicked"という出力はされます。
該当のソースコード
TypeScript
1"use client"; 2 3import { useEffect, useRef } from "react"; 4import * as THREE from "three"; 5 6export default function RoomPage() { 7 // divを後から取得するための変数 8 const mountRef = useRef<HTMLDivElement>(null); 9 10 useEffect(() => { 11 // 描画先の div がまだ存在しないなら終了 12 if (!mountRef.current) return; 13 14 // シーン(3D空間)を作る 15 const scene = new THREE.Scene(); 16 17 // カメラを作る 18 const camera = new THREE.PerspectiveCamera( 19 75, // 視野角(FOV) 20 window.innerWidth / window.innerHeight, // 画面の縦横比 21 0.1, // 最小描画距離 22 1000 // 最大描画距離 23 ); 24 25 // レンダラーを作る 26 const renderer = new THREE.WebGLRenderer({ 27 antialias: true, 28 }); 29 // 描画サイズを画面いっぱいにする 30 renderer.setSize(window.innerWidth, window.innerHeight); 31 32 // <canvas>を作り、Reactのdivに追加 33 mountRef.current?.appendChild(renderer.domElement); 34 35 // 部屋 ------------------------------------------------------------------------------------------ 36 // 床 37 const floor = new THREE.Mesh( 38 new THREE.PlaneGeometry(18, 96), 39 new THREE.MeshStandardMaterial({ 40 color: 0x888888, 41 side: THREE.DoubleSide, 42 }) 43 ); 44 floor.rotation.x = -Math.PI / 2; 45 scene.add(floor); 46 47 // 天井 48 const ceiling = new THREE.Mesh( 49 new THREE.PlaneGeometry(18, 96), 50 new THREE.MeshStandardMaterial({ 51 color: 0x888888, 52 side: THREE.DoubleSide, 53 }) 54 ); 55 ceiling.rotation.x = Math.PI / 2; 56 ceiling.position.set(0, 48, 0); 57 scene.add(ceiling); 58 59 // 奥の壁 60 const backWall = new THREE.Mesh( 61 new THREE.PlaneGeometry(18, 48), 62 new THREE.MeshStandardMaterial({ 63 color: 0x444444, 64 side: THREE.DoubleSide 65 }) 66 ); 67 backWall.position.set(0, 24, -48); 68 scene.add(backWall); 69 70 // 左の壁 71 const leftWall = new THREE.Mesh( 72 new THREE.PlaneGeometry(96, 48), 73 new THREE.MeshStandardMaterial({ 74 color: 0x888888, 75 side: THREE.DoubleSide, 76 }) 77 ); 78 leftWall.rotation.y = Math.PI / 2; 79 leftWall.position.set(-9, 24, 0); 80 scene.add(leftWall); 81 82 // 右の壁 83 const rightWall = new THREE.Mesh( 84 new THREE.PlaneGeometry(96, 48), 85 new THREE.MeshStandardMaterial({ 86 color: 0x888888, 87 side: THREE.DoubleSide, 88 }) 89 ); 90 rightWall.rotation.y = -Math.PI / 2; 91 rightWall.position.set(9, 24, 0); 92 scene.add(rightWall); 93 94 // 扉 ------------------------------------------------------------------------------------------ 95 // 壁上部 96 const doorWallTop = new THREE.Mesh( 97 new THREE.BoxGeometry(18, 28, 1), 98 new THREE.MeshStandardMaterial({ 99 color: 0x444444, 100 side: THREE.DoubleSide 101 }) 102 ); 103 doorWallTop.position.set(0, 34, 0); 104 scene.add(doorWallTop); 105 106 // 壁左部 107 const doorWallLeft = new THREE.Mesh( 108 new THREE.BoxGeometry(4, 20, 1), 109 new THREE.MeshStandardMaterial({ 110 color: 0x444444, 111 side: THREE.DoubleSide 112 }) 113 ); 114 doorWallLeft.position.set(8, 10, 0); 115 scene.add(doorWallLeft); 116 117 // 壁右部 118 const doorWallRight = new THREE.Mesh( 119 new THREE.BoxGeometry(4, 20, 1), 120 new THREE.MeshStandardMaterial({ 121 color: 0x444444, 122 side: THREE.DoubleSide 123 }) 124 ); 125 doorWallRight.position.set(-8, 10, 0); 126 scene.add(doorWallRight); 127 128 // 左の扉 129 const leftDoor = new THREE.Mesh( 130 new THREE.BoxGeometry(6, 20, 0.5), 131 new THREE.MeshStandardMaterial({ 132 color: 0x5a3a22, 133 }) 134 ); 135 leftDoor.position.set(3, 10, 0); 136 scene.add(leftDoor); 137 console.log("create", leftDoor.uuid); 138 139 // 右の扉 140 const rightDoor = new THREE.Mesh( 141 new THREE.BoxGeometry(6, 20, 0.5), 142 new THREE.MeshStandardMaterial({ 143 color: 0x223a5a, 144 }) 145 ); 146 rightDoor.position.set(-3, 10, 0); 147 scene.add(rightDoor); 148 149 // 光 ------------------------------------------------------------------------------------------ 150 // 光源 151 const light = new THREE.DirectionalLight(0xffffff, 3); 152 light.position.set(0, 6, 48); 153 light.lookAt(0, 8, 24); 154 scene.add(light); 155 156 // 環境光 157 const ambientLight = new THREE.AmbientLight(0xffffff, 0.3); 158 scene.add(ambientLight); 159 160 const helper = new THREE.DirectionalLightHelper(light, 1, 0xffff00); 161 scene.add(helper); 162 163 // カメラ ------------------------------------------------------------------------------------------ 164 camera.position.set(-7, 6, 20); 165 camera.lookAt(0, 8, 0); 166 167 // Raycaster ------------------------------------------------------------------------------------------ 168 const raycaster = new THREE.Raycaster(); 169 const mouse = new THREE.Vector2(); 170 171 // イベント ------------------------------------------------------------------------------------------ 172 function onClick(event: MouseEvent) { 173 mouse.x = (event.clientX / window.innerWidth) * 2 - 1; 174 mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; 175 176 raycaster.setFromCamera(mouse, camera); 177 178 const intersects = raycaster.intersectObjects([ 179 leftDoor, 180 rightDoor, 181 ]); 182 183 if (intersects.length > 0) { 184 leftDoor.rotation.y = Math.PI / 2; 185 rightDoor.rotation.y = -Math.PI / 2; 186 } 187 188 console.log("Clicked"); 189 } 190 191 // イベント登録 192 window.addEventListener("click", onClick); 193 194 // アニメーション ------------------------------------------------------------------------------------------ 195 function animate() { 196 // 次のフレームでまたanimateを呼ぶ → 無限ループ 197 requestAnimationFrame(animate); 198 199 // 描画 200 renderer.render(scene, camera); 201 } 202 203 // アニメーション開始 204 animate(); 205 206 return () => { 207 // ページを離れた時の後始末 208 window.removeEventListener("click", onClick); 209 mountRef.current?.removeChild(renderer.domElement); 210 renderer.dispose(); 211 console.log( 212 "cleanup" 213 ); 214 }; 215 }, []); 216 217 return ( 218 <div 219 ref={mountRef} 220 style={{ 221 width: "100vw", 222 height: "100vh", 223 overflow: "hidden", 224 }} 225 /> 226 ); 227}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
・Copilotに聞きながら作成しています
・animate内にleftDoor.rotation.y += 0.01;を記述すると、扉は回った
・初期値falseの変数isClickedを用意して、onClick内でtrueにし、animate内でisClicked === trueの時のみ、leftDoor.rotation.y += 0.01;を実行するようにしたが、扉は動かなかった
・animate内でconsole.log(leftDoor.rotation.y);を実行した時0と1.5707963267948966を交互に出力したが、扉は動かなかった
補足
初心者のため、暗黙の了解的なルールがあれば落としているかもしれません
回答1件
あなたの回答
tips
プレビュー