Spaces:
Running
Running
// Simple Three.js test application | |
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js'; | |
class SimpleVillageApp { | |
constructor() { | |
this.scene = null; | |
this.camera = null; | |
this.renderer = null; | |
this.controls = null; | |
this.init(); | |
} | |
init() { | |
console.log('Initializing Simple Village App...'); | |
this.initThreeJS(); | |
this.createEnvironment(); | |
this.animate(); | |
console.log('Simple Village App initialized successfully'); | |
} | |
initThreeJS() { | |
// Scene | |
this.scene = new THREE.Scene(); | |
this.scene.background = new THREE.Color(0x87CEEB); | |
// Camera | |
this.camera = new THREE.PerspectiveCamera( | |
75, | |
window.innerWidth / window.innerHeight, | |
0.1, | |
1000 | |
); | |
this.camera.position.set(20, 20, 20); | |
this.camera.lookAt(0, 0, 0); | |
// Renderer | |
this.renderer = new THREE.WebGLRenderer({ antialias: true }); | |
this.renderer.setSize(window.innerWidth, window.innerHeight); | |
this.renderer.shadowMap.enabled = true; | |
const container = document.getElementById('container'); | |
if (container) { | |
container.appendChild(this.renderer.domElement); | |
console.log('Renderer added to DOM'); | |
} | |
// Lighting | |
const ambientLight = new THREE.AmbientLight(0x404040, 0.6); | |
this.scene.add(ambientLight); | |
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); | |
directionalLight.position.set(10, 10, 5); | |
directionalLight.castShadow = true; | |
this.scene.add(directionalLight); | |
// Ground plane | |
const groundGeometry = new THREE.PlaneGeometry(100, 100); | |
const groundMaterial = new THREE.MeshLambertMaterial({ | |
color: 0x228B22, | |
transparent: true, | |
opacity: 0.8 | |
}); | |
const ground = new THREE.Mesh(groundGeometry, groundMaterial); | |
ground.rotation.x = -Math.PI / 2; | |
ground.receiveShadow = true; | |
this.scene.add(ground); | |
// Grid helper | |
const gridHelper = new THREE.GridHelper(100, 100, 0x444444, 0x222222); | |
this.scene.add(gridHelper); | |
window.addEventListener('resize', () => this.onWindowResize()); | |
} | |
createEnvironment() { | |
// Create some simple buildings | |
this.createBuilding(5, 0, 5, 0x8B4513); | |
this.createBuilding(-5, 0, -5, 0x696969); | |
this.createBuilding(0, 0, 0, 0xFFD700); | |
// Create some villagers | |
this.createVillager(0, 0, 0, 0xff0000); | |
this.createVillager(5, 0, 5, 0x00ff00); | |
this.createVillager(-3, 0, -3, 0x0000ff); | |
} | |
createBuilding(x, y, z, color) { | |
const geometry = new THREE.BoxGeometry(3, 3, 3); | |
const material = new THREE.MeshLambertMaterial({ color: color }); | |
const mesh = new THREE.Mesh(geometry, material); | |
mesh.position.set(x, y + 1.5, z); | |
mesh.castShadow = true; | |
mesh.receiveShadow = true; | |
this.scene.add(mesh); | |
} | |
createVillager(x, y, z, color) { | |
const geometry = new THREE.SphereGeometry(0.5, 16, 16); | |
const material = new THREE.MeshLambertMaterial({ color: color }); | |
const mesh = new THREE.Mesh(geometry, material); | |
mesh.position.set(x, y + 0.5, z); | |
mesh.castShadow = true; | |
mesh.receiveShadow = true; | |
this.scene.add(mesh); | |
} | |
onWindowResize() { | |
this.camera.aspect = window.innerWidth / window.innerHeight; | |
this.camera.updateProjectionMatrix(); | |
this.renderer.setSize(window.innerWidth, window.innerHeight); | |
} | |
animate() { | |
requestAnimationFrame(() => this.animate()); | |
// Simple rotation for testing | |
this.scene.rotation.y += 0.001; | |
this.renderer.render(this.scene, this.camera); | |
} | |
} | |
// Initialize the application when the page loads | |
document.addEventListener('DOMContentLoaded', () => { | |
new SimpleVillageApp(); | |
}); |