import * as THREE from 'three'; import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; import { buildWindmill } from './subject.js'; import { buildEnvironment, makeGroundHeight } from './environment.js'; const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' }); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.toneMappingExposure = 0.94; renderer.outputColorSpace = THREE.SRGBColorSpace; document.getElementById('app').appendChild(renderer.domElement); const scene = new THREE.Scene(); scene.background = new THREE.Color(0xb8cbd6); const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 700); camera.position.set(24, 14.2, 34); const target = new THREE.Vector3(0, 10.2, 0); camera.lookAt(target); /* soft neutral IBL for the timber / iron */ const pmrem = new THREE.PMREMGenerator(renderer); const roomEnv = pmrem.fromScene(new RoomEnvironment(renderer), 0.04).texture; scene.environment = roomEnv; scene.environmentIntensity = 0.25; pmrem.dispose(); const groundH = makeGroundHeight(); const env = buildEnvironment(scene, renderer, groundH); const subject = buildWindmill(); subject.position.y = groundH(0, 0); scene.add(subject); const sails = subject.getObjectByName('sails'); const clouds = scene.getObjectByName('clouds'); const sun = env.sun; sun.target.position.set(0, 7, 0); sun.target.updateMatrixWorld(); // keep the shadow frustum centred on the mill sun.position.copy(sun.position).normalize().multiplyScalar(48); sun.position.y = Math.max(sun.position.y, 34); /* subtle breeze sway on the reeds is not needed; sails turn steadily */ const clock = new THREE.Clock(); let firstFrame = true; function animate() { const t = clock.getElapsedTime(); if (sails) sails.rotation.y = Math.PI / 4 + t * 0.22; if (clouds) clouds.rotation.y = t * 0.0015; renderer.render(scene, camera); if (firstFrame) { firstFrame = false; window.__three = { scene, camera, renderer }; } } renderer.setAnimationLoop(animate); window.addEventListener('resize', () => { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); }); window.addEventListener('error', (e) => { const el = document.getElementById('err'); if (el) { el.style.display = 'block'; el.textContent = 'Error: ' + e.message; } });