하나의 HTML 파일로 3D 메카 고질라 만들어
- User
하나의 HTML 파일로 3D 메카 고질라 만들어
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
하나의 HTML 파일로 3D 메카 고질라 만들어
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html>
<head>
<title>메카 고질라 3D</title>
<style>
body {
margin: 0;
overflow: hidden; /* 스크롤바 제거 */
background-color: #000; /* 배경색 검정 */
}
#container {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://threejs.org/examples/js/loaders/GLTFLoader.js"></script>
<script>
// Three.js 시작
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('container').appendChild(renderer.domElement);
// 조명 추가
const ambientLight = new THREE.AmbientLight(0x404040); // 은은한 전체 조명
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5); // 방향성 조명 (태양과 유사)
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
const pointLight = new THREE.PointLight(0xffffff, 0.8); // 점 조명 (광원 위치 지정)
pointLight.position.set(0, 5, 0);
scene.add(pointLight);
// GLTF 로더 생성
const loader = new THREE.GLTFLoader();
// 메카 고질라 모델 로드 (GLTF 파일 경로를 실제 파일 경로로 변경!)
loader.load('mecha_godzilla.glb', // **이 부분을 메카 고질라 GLTF 파일 경로로 변경하세요!**
function (gltf) {
const model = gltf.scene;
// 모델 크기 조정 및 위치 설정 (원하는 대로 변경)
model.scale.set(0.1, 0.1, 0.1); // 크기 조정 (예: 1/10 크기로)
model.position.set(0, -1, 0); // 위치 조정 (x, y, z)
scene.add(model);
mechaGodzilla = model; // 애니메이션을 위해 전역 변수에 저장
},
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded'); // 로딩 진행 상황 표시
},
function (error) {
console.error('An error happened', error); // 오류 처리
}
);
// 배경 추가 (선택 사항)
const textureLoader = new THREE.TextureLoader();
textureLoader.load('background.jpg', function (texture) { // **이 부분을 배경 이미지 파일 경로로 변경하세요!**
scene.background = texture;
});
// 카메라 위치 설정
camera.position.z = 5;
// 애니메이션 변수
let mechaGodzilla; // 로드된 모델을 저장할 변수
let rotationSpeed = 0.01; // 회전 속도
let animationFrame = 0;
// 애니메이션 루프
function animate() {
requestAnimationFrame(animate);
// 모델이 로드되었는지 확인 후 회전 (필요한 애니메이션을 여기에 추가)
if (mechaGodzilla) {
mechaGodzilla.rotation.y += rotationSpeed; // Y축 회전
// 다른 애니메이션 효과 (걷기, 공격 등)를 여기에 추가 가능
// 예: 다리 움직임, 팔 움직임 등
// animationFrame 값을 사용하여 애니메이션의 단계 조절
animationFrame++;
}
renderer.render(scene, camera);
}
// 창 크기 변경 시 렌더러 및 카메라 업데이트
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate(); // 애니메이션 시작
</script>
</body>
</html>
사용 방법:
<script> 태그를 사용하여 Three.js 라이브러리를 CDN에서 가져옵니다. (CDN 링크는 최신 버전을 사용하는 것이 좋습니다.)GLTFLoader.js 를 CDN에서 가져옵니다. Three.js examples 폴더에서 가져와야 합니다.container div 요소 안에 Three.js 렌더링이 추가됩니다.body와 container에 대한 간단한 스타일을 설정합니다. overflow: hidden은 스크롤바를 제거하고, background-color는 배경색을 설정합니다.GLTFLoader를 사용하여 메카 고질라 모델 (mecha_godzilla.glb)을 로드합니다. 중요: mecha_godzilla.glb를 실제 파일 경로로 바꿔야 합니다. 모델 파일이 웹 서버에 있어야 합니다.animate() 함수는 매 프레임마다 호출되어 모델을 회전시킵니다. 이 함수 안에 다른 애니메이션 로직을 추가할 수 있습니다.window.addEventListener('resize', ...)는 창 크기가 변경될 때 렌더러와 카메라를 업데이트합니다.필수 사항:
mecha_godzilla.glb를 모델 파일의 실제 경로로 바꿔야 합니다!http-server, live-server, 또는 Node.js를 사용한 간단한 서버)를 통해 제공해야 합니다. 브라우저는 로컬 파일 시스템에서 GLTF 파일을 직접 로드하는 데 제한이 있을 수 있습니다.background.jpg 파일을 배경 이미지로 사용합니다. 이 부분도 이미지 파일의 실제 경로로 바꿔야 합니다!개선 사항 및 추가 기능:
OrbitControls와 같은 Three.js 컨트롤을 사용하여 카메라를 움직이고 장면을 상호 작용적으로 탐색할 수 있습니다.예제 GLTF 모델 및 배경 이미지:
무료 GLTF 모델은 Sketchfab, CGTrader 등에서 찾을 수 있습니다. "mecha godzilla gltf"와 같은 검색어를 사용해 보세요. 무료 배경 이미지는 Unsplash, Pexels 등에서 찾을 수 있습니다.
중요: 이 코드는 기본적인 3D 메카 고질라를 표시하는 예제입니다. 더욱 복잡하고 멋진 모델을 만들려면 Three.js에 대한 더 많은 지식과 경험이 필요합니다. Three.js 공식 문서와 튜토리얼을 참고하세요. 또한, 웹 서버를 설정하고 GLTF 모델을 로드하는 데 문제가 있다면 검색 엔진을 사용하여 관련 정보를 찾아보세요.