gpt-4.1-mini-2025-04-14· OpenAI
LMArena Human Preference 140K
threejs를 사용하는 웹게임. 조작가능한 3인칭 월드오브워크래프트 RPG 스타일의 웹페이지 게임을 만들어줘. WASD로 조작하고 SPACE바로…
- User
threejs를 사용하는 웹게임. 조작가능한 3인칭 월드오브워크래프트 RPG 스타일의 웹페이지 게임을 만들어줘. WASD로 조작하고 SPACE바로 점프할수있어야하며, 마우스를 통해 시점을 회전할수있어야해.
- Assistant
3인칭 월드오브워크래프트 스타일의 RPG 웹게임을 Three.js로 만드는 기본 예제를 만들어 드리겠습니다.
- WASD로 캐릭터 이동
- SPACE로 점프
- 마우스로 카메라 시점 회전
아래 코드는 Three.js와 기본적인 캐릭터 컨트롤, 중력, 점프, 마우스 시점 회전을 포함한 최소한의 예제입니다.
1. HTML + JS 코드
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8" /> <title>3인칭 RPG 스타일 게임 (Three.js)</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <script src="https://cdn.jsdelivr.net/npm/three@0.152.2/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/three@0.152.2/examples/js/controls/PointerLockControls.js"></script> <script> let scene, camera, renderer; let controls; let player, playerVelocity, playerOnGround; let keys = {}; let clock = new THREE.Clock(); init(); animate(); function init() { // 씬, 카메라, 렌더러 scene = new THREE.Scene(); scene.background = new THREE.Color(0xa0a0a0); camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); renderer = new THREE.WebGLRenderer({antialias:true}); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 바닥 const floorGeometry = new THREE.PlaneGeometry(100, 100); const floorMaterial = new THREE.MeshStandardMaterial({color: 0x228822}); const floor = new THREE.Mesh(floorGeometry, floorMaterial); floor.rotation.x = -Math.PI / 2; floor.receiveShadow = true; scene.add(floor); // 조명 const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444); hemiLight.position.set(0, 20, 0); scene.add(hemiLight); const dirLight = new THREE.DirectionalLight(0xffffff); dirLight.position.set(-3, 10, -10); dirLight.castShadow = true; scene.add(dirLight); // 플레이어 (간단한 박스) const playerGeometry = new THREE.BoxGeometry(1, 2, 1); const playerMaterial = new THREE.MeshStandardMaterial({color: 0x0000ff}); player = new THREE.Mesh(playerGeometry, playerMaterial); player.position.y = 1; // 바닥 위에 위치 player.castShadow = true; scene.add(player); // 플레이어 물리 변수 playerVelocity = new THREE.Vector3(); playerOnGround = false; // 카메라 위치 초기화 (3인칭) updateCameraPosition(); // 키보드 이벤트 window.addEventListener('keydown', (e) => { keys[e.code] = true; }); window.addEventListener('keyup', (e) => { keys[e.code] = false; }); // 마우스 시점 제어 (Pointer Lock) controls = new THREE.PointerLockControls(camera, document.body); document.body.addEventListener('click', () => { controls.lock(); }); controls.addEventListener('lock', () => { console.log('Pointer locked'); }); controls.addEventListener('unlock', () => { console.log('Pointer unlocked'); }); window.addEventListener('resize', onWindowResize); } function onWindowResize() { camera.aspect = window.innerWidth/window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } function updateCameraPosition() { // 3인칭 카메라 위치: 플레이어 뒤쪽 위에 위치 const offset = new THREE.Vector3(0, 3, 5); const playerDirection = new THREE.Vector3(); player.getWorldDirection(playerDirection); playerDirection.y = 0; playerDirection.normalize(); const cameraPos = player.position.clone().add(offset.clone().applyAxisAngle(new THREE.Vector3(0,1,0), Math.atan2(playerDirection.x, playerDirection.z))); camera.position.lerp(cameraPos, 0.1); camera.lookAt(player.position.x, player.position.y + 1, player.position.z); } function animate() { requestAnimationFrame(animate); const delta = clock.getDelta(); // 플레이어 이동 처리 const speed = 5; const jumpSpeed = 8; const gravity = 20; // 방향 벡터 let moveDirection = new THREE.Vector3(); if (keys['KeyW']) moveDirection.z -= 1; if (keys['KeyS']) moveDirection.z += 1; if (keys['KeyA']) moveDirection.x -= 1; if (keys['KeyD']) moveDirection.x += 1; moveDirection.normalize(); // 카메라 방향 기준으로 이동 방향 변환 if (moveDirection.length() > 0) { // 카메라가 바라보는 방향의 y축 회전만 고려 const angle = controls.getObject().rotation.y; const sin = Math.sin(angle); const cos = Math.cos(angle); const dx = moveDirection.x * cos - moveDirection.z * sin; const dz = moveDirection.x * sin + moveDirection.z * cos; moveDirection.set(dx, 0, dz); // 플레이어 방향 회전 (y축) const targetRotation = Math.atan2(moveDirection.x, moveDirection.z); player.rotation.y += (targetRotation - player.rotation.y) * 0.1; } // 수평 이동 player.position.x += moveDirection.x * speed * delta; player.position.z += moveDirection.z * speed * delta; // 점프 if (keys['Space'] && playerOnGround) { playerVelocity.y = jumpSpeed; playerOnGround = false; } // 중력 적용 playerVelocity.y -= gravity * delta; player.position.y += playerVelocity.y * delta; // 바닥 충돌 체크 if (player.position.y < 1) { player.position.y = 1; playerVelocity.y = 0; playerOnGround = true; } // 카메라 위치 업데이트 updateCameraPosition(); renderer.render(scene, camera); } </script> </body> </html>
설명
- PointerLockControls를 사용해 마우스 움직임으로 시점 회전 가능
- WASD 키로 캐릭터 이동 (카메라 방향 기준)
- SPACE로 점프 (중력과 바닥 충돌 처리 포함)
- 3인칭 카메라가 캐릭터 뒤쪽 위에 위치하며 부드럽게 따라감
- 간단한 박스가 플레이어 캐릭터 역할
실행 방법
- 위 코드를
index.html로 저장 - 로컬 서버에서 실행 (브라우저 보안 정책 때문에 파일 직접 열면 PointerLock이 안될 수 있음)
- 예: VSCode Live Server, Python
python -m http.server등
- 예: VSCode Live Server, Python
- 페이지 클릭 후 마우스 움직여 시점 조작, WASD로 이동, SPACE로 점프 가능
필요하면 캐릭터 모델, 애니메이션, 맵, 몬스터 등 추가 기능도 단계별로 도와드릴 수 있습니다!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.