<!DOCTYPE html>
<html>
<head>
<title>16th Century Town Masterpiece</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaaccff); // Light blue sky
// Camera setup
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(80, 50, 100); // Adjusted starting position
camera.lookAt(0, 0, 0);
// Renderer setup
const renderer = new THREE.WebGLRenderer({ antialias: true }); // Enable antialiasing
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping; // Tone mapping for better lighting
renderer.toneMappingExposure = 1.0;
// Orbit Controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 20;
controls.maxDistance = 500;
// Lighting
const ambientLight = new THREE.AmbientLight(0x505050);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffeedd, 0.9);
directionalLight.position.set(60, 80, 40);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 4096; // Higher resolution
directionalLight.shadow.mapSize.height = 4096;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 300;
directionalLight.shadow.camera.left = -100;
directionalLight.shadow.camera.right = 100;
directionalLight.shadow.camera.top = 100;
directionalLight.shadow.camera.bottom = -100;
scene.add(directionalLight);
// Ground (Detailed)
const groundGeometry = new THREE.PlaneGeometry(300, 300, 64, 64); // Subdivide for detail
const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x6b8e23 }); // Olive drab
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
// Add slight variations in height to the ground
const groundVertices = groundGeometry.attributes.position.array;
for (let i = 0; i < groundVertices.length; i += 3) {
groundVertices[i + 1] += Math.random() * 2 - 1; // Add random height
}
groundGeometry.attributes.position.needsUpdate = true;
groundGeometry.computeVertexNormals();
// Function to create a tree
function createTree(x, z) {
const trunkHeight = 7 + Math.random() * 3;
const trunkRadius = 0.5 + Math.random() * 0.3;
const trunkGeometry = new THREE.CylinderGeometry(trunkRadius, trunkRadius, trunkHeight, 8);
const trunkMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // Brown
const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
trunk.position.set(x, trunkHeight / 2, z);
trunk.castShadow = true;
trunk.receiveShadow = true;
scene.add(trunk);
const leavesHeight = 5 + Math.random() * 2;
const leavesRadius = 3 + Math.random() * 1.5;
const leavesGeometry = new THREE.ConeGeometry(leavesRadius, leavesHeight, 8);
const leavesMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 }); // Forest green
const leaves = new THREE.Mesh(leavesGeometry, leavesMaterial);
leaves.position.set(x, trunkHeight + leavesHeight / 2 -1, z);
leaves.castShadow = true;
leaves.receiveShadow = true;
scene.add(leaves);
}
// Function to create a fence section
function createFenceSection(x, z) {
const postHeight = 4;
const postRadius = 0.2;
const railHeight = 0.3;
const railWidth = 5;
// Post Geometry and Material
const postGeometry = new THREE.CylinderGeometry(postRadius, postRadius, postHeight, 8);
const postMaterial = new THREE.MeshLambertMaterial({ color: 0xA0522D }); // Brown
const post1 = new THREE.Mesh(postGeometry, postMaterial);
post1.position.set(x - railWidth / 2, postHeight / 2, z);
post1.castShadow = true;
post1.receiveShadow = true;
scene.add(post1);
const post2 = new THREE.Mesh(postGeometry, postMaterial);
post2.position.set(x + railWidth / 2, postHeight / 2, z);
post2.castShadow = true;
post2.receiveShadow = true;
scene.add(post2);
// Rail Geometry and Material
const railGeometry = new THREE.BoxGeometry(railWidth, railHeight, railHeight / 2);
const railMaterial = new THREE.MeshLambertMaterial({ color: 0xA0522D }); // Brown
// Create two rails
const rail1 = new THREE.Mesh(railGeometry, railMaterial);
rail1.position.set(x, postHeight * 0.3, z);
rail1.castShadow = true;
rail1.receiveShadow = true;
scene.add(rail1);
const rail2 = new THREE.Mesh(railGeometry, railMaterial);
rail2.position.set(x, postHeight * 0.7, z);
rail2.castShadow = true;
rail2.receiveShadow = true;
scene.add(rail2);
}
// Function to create a detailed house
function createDetailedHouse(x, z, height, width, depth) {
const baseColor = new THREE.Color(0xDEB887); // Light Wood
const trimColor = new THREE.Color(0xA0522D); // Dark Wood
const roofColor = new THREE.Color(0x8B4513); // Brown
// Base of the House
const baseGeometry = new THREE.BoxGeometry(width, height, depth);
const baseMaterial = new THREE.MeshLambertMaterial({ color: baseColor });
const base = new THREE.Mesh(baseGeometry, baseMaterial);
base.position.set(x, height / 2, z);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
// Roof (Gabled Roof)
const roofHeight = height / 2;
const roofGeometry = new THREE.BufferGeometry();
const roofVertices = new Float32Array([
-width / 2, 0, -depth / 2, // vertex 1
width / 2, 0, -depth / 2, // vertex 2
0, roofHeight, -depth / 2, // vertex 3
-width / 2, 0, depth / 2, // vertex 4
width / 2, 0, depth / 2, // vertex 5
0, roofHeight, depth / 2 // vertex 6
]);
const roofIndices = new Uint32Array([
0, 1, 2,
3, 4, 5
]);
roofGeometry.setAttribute('position', new THREE.BufferAttribute(roofVertices, 3));
roofGeometry.setIndex(new THREE.BufferAttribute(roofIndices, 1));
roofGeometry.computeVertexNormals();
const roofMaterial = new THREE.MeshLambertMaterial({ color: roofColor, side: THREE.DoubleSide });
const roof1 = new THREE.Mesh(roofGeometry, roofMaterial);
roof1.position.set(x, height, z);
roof1.castShadow = true;
roof1.receiveShadow = true;
scene.add(roof1);
// Sides of the roof (Triangles)
const sideRoofGeometry = new THREE.BufferGeometry();
const sideRoofVertices = new Float32Array([
0, roofHeight, -depth / 2, // vertex 1
width / 2, 0, -depth / 2, // vertex 2
width / 2, 0, depth / 2, // vertex 3
0, roofHeight, -depth / 2, // vertex 4
-width / 2, 0, -depth / 2, // vertex 5
-width / 2, 0, depth / 2, // vertex 6
0, roofHeight, depth / 2, // vertex 7
width / 2, 0, depth / 2, // vertex 8
width / 2, 0, -depth / 2, // vertex 9
0, roofHeight, depth / 2, // vertex 10
-width / 2, 0, depth / 2, // vertex 11
-width / 2, 0, -depth / 2 // vertex 12
]);
const sideRoofIndices = new Uint32Array([
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11
]);
sideRoofGeometry.setAttribute('position', new THREE.BufferAttribute(sideRoofVertices, 3));
sideRoofGeometry.setIndex(new THREE.BufferAttribute(sideRoofIndices, 1));
sideRoofGeometry.computeVertexNormals();
const sideRoof = new THREE.Mesh(sideRoofGeometry, roofMaterial);
sideRoof.position.set(x, height, z);
sideRoof.castShadow = true;
sideRoof.receiveShadow = true;
scene.add(sideRoof);
// Window (Simple Rectangular Window)
const windowWidth = width / 3;
const windowHeight = height / 3;
const windowGeometry = new THREE.PlaneGeometry(windowWidth, windowHeight);
const windowMaterial = new THREE.MeshLambertMaterial({ color: 0x87CEEB, transparent: true, opacity: 0.6 }); // Light blue glass
const window = new THREE.Mesh(windowGeometry, windowMaterial);
window.position.set(x, height / 2, z + depth / 2 + 0.01); // Position on the front face
window.castShadow = false;
window.receiveShadow = false;
scene.add(window);
// Door
const doorWidth = width / 4;
const doorHeight = height / 2;
const doorGeometry = new THREE.BoxGeometry(doorWidth, doorHeight, 0.1);
const doorMaterial = new THREE.MeshLambertMaterial({ color: trimColor });
const door = new THREE.Mesh(doorGeometry, doorMaterial);
door.position.set(x, doorHeight / 2, z - depth / 2 - 0.01); // Position on the back face
door.castShadow = true;
door.receiveShadow = true;
scene.add(door);
}
// Function to create a windmill
function createWindmill(x, z) {
const baseHeight = 8;
const baseRadius = 3;
const sailLength = 5;
const sailWidth = 0.5;
// Base of the Windmill (Cylinder)
const baseGeometry = new THREE.CylinderGeometry(baseRadius, baseRadius, baseHeight, 16);
const baseMaterial = new THREE.MeshLambertMaterial({ color: 0xD2B48C }); // Tan
const base = new THREE.Mesh(baseGeometry, baseMaterial);
base.position.set(x, baseHeight / 2, z);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
// Top of the Windmill (Cylinder)
const topGeometry = new THREE.CylinderGeometry(baseRadius * 0.8, baseRadius * 0.8, baseHeight / 3, 16);
const topMaterial = new THREE.MeshLambertMaterial({ color: 0xC04000 });
const top = new THREE.Mesh(topGeometry, topMaterial);
top.position.set(x, baseHeight + baseHeight / 6, z);
top.castShadow = true;
top.receiveShadow = true;
scene.add(top);
// Create Axle
const axleGeometry = new THREE.CylinderGeometry(0.3, 0.3, 3, 8);
const axleMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // Brown
const axle = new THREE.Mesh(axleGeometry, axleMaterial);
axle.position.set(x, baseHeight + baseHeight / 3, z);
axle.rotation.z = Math.PI / 2; // Rotate 90 degrees
axle.castShadow = true;
axle.receiveShadow = true;
scene.add(axle);
// Create Sails
const sailGeometry = new THREE.PlaneGeometry(sailLength, sailWidth);
const sailMaterial = new THREE.MeshLambertMaterial({ color: 0xF0F8FF, side: THREE.DoubleSide }); // AliceBlue
const sail1 = new THREE.Mesh(sailGeometry, sailMaterial);
sail1.position.set(x + sailLength / 2 + 1.5, baseHeight + baseHeight / 3, z);
sail1.castShadow = true;
sail1.receiveShadow = true;
scene.add(sail1);
const sail2 = new THREE.Mesh(sailGeometry, sailMaterial);
sail2.position.set(x - sailLength / 2 - 1.5, baseHeight + baseHeight / 3, z);
sail2.rotation.y = Math.PI; // Rotate 180 degrees
sail2.castShadow = true;
sail2.receiveShadow = true;
scene.add(sail2);
const sail3 = new THREE.Mesh(sailGeometry, sailMaterial);
sail3.position.set(x, baseHeight + baseHeight / 3, z + sailLength / 2 + 1.5);
sail3.rotation.y = Math.PI / 2; // Rotate 90 degrees
sail3.castShadow = true;
sail3.receiveShadow = true;
scene.add(sail3);
const sail4 = new THREE.Mesh(sailGeometry, sailMaterial);
sail4.position.set(x, baseHeight + baseHeight / 3, z - sailLength / 2 - 1.5);
sail4.rotation.y = -Math.PI / 2; // Rotate -90 degrees
sail4.castShadow = true;
sail4.receiveShadow = true;
scene.add(sail4);
// Store sails in an array to rotate later
const sails = [sail1, sail2, sail3, sail4];
return { base: base, sails: sails }; // Return the windmill object and its sails
}
// Create the windmill
const windmill = createWindmill(50, 20); //Create Windmill and store returned values
// Create trees
for (let i = -100; i < 100; i += 20) {
for (let j = -100; j < 100; j += 20) {
if (Math.random() > 0.7) { // Sparse trees
createTree(i, j);
}
}
}
// Create detailed houses
createDetailedHouse(-20, -10, 10, 12, 10);
createDetailedHouse(10, 5, 9, 11, 9);
createDetailedHouse(-5, 15, 11, 13, 11);
createDetailedHouse(25, -15, 12, 14, 12);
// Create fence around some houses
for (let i = -30; i < -10; i += 5) {
createFenceSection(i, -20);
}
for (let i = -20; i < 0; i += 5) {
createFenceSection(-30, i);
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
// Rotate the sails
windmill.sails.forEach(sail => {
sail.rotation.z += 0.01; // Adjust rotation speed
});
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize, false);
animate();
</script>
</body>
</html>