-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (135 loc) · 5.43 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Just scroll out and in / Procedural Low Poly World</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #87CEEB;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script>
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.body.appendChild(renderer.domElement);
renderer.shadowMap.enabled = true;
const ambientLight = new THREE.AmbientLight("#b9d5ff", 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight("#ffffff", 0.5);
directionalLight.position.set(10, 10, 10);
directionalLight.castShadow = true;
scene.add(directionalLight);
const fog = new THREE.Fog("#87CEEB", 1, 100);
scene.fog = fog;
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(200, 200),
new THREE.MeshStandardMaterial({ color: "#a9c388" })
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const generatedPositions = new Set();
function getPositionKey(x, z) {
return `${Math.floor(x)},${Math.floor(z)}`;
}
function createHouse(x, z) {
const house = new THREE.Group();
const walls = new THREE.Mesh(
new THREE.BoxGeometry(4, 4 + Math.random(), 4),
new THREE.MeshStandardMaterial({ color: "#8A4A2F", flatShading: true })
);
walls.position.y = 2;
walls.castShadow = true;
house.add(walls);
const roof = new THREE.Mesh(
new THREE.ConeGeometry(3.5, 2, 4),
new THREE.MeshStandardMaterial({ color: "#D2691E", flatShading: true })
);
roof.position.y = 5;
roof.rotation.y = Math.PI * 0.25;
roof.castShadow = true;
house.add(roof);
const windowGeometry = new THREE.PlaneGeometry(1, 1.5);
const windowMaterial = new THREE.MeshStandardMaterial({ color: "#FFFFFF" });
for (let i = 0; i < 4; i++) {
const windowMesh = new THREE.Mesh(windowGeometry, windowMaterial);
windowMesh.position.set(i % 2 === 0 ? -1.5 : 1.5, 1.5, i < 2 ? 2.01 : -2.01);
house.add(windowMesh);
}
house.position.set(x, 0, z);
return house;
}
function generateHouses(range) {
for (let x = -range; x <= range; x += 20) {
for (let z = -range; z <= range; z += 20) {
const key = getPositionKey(x, z);
if (!generatedPositions.has(key)) {
if (Math.random() > 0.5) {
const house = createHouse(x + Math.random() * 5, z + Math.random() * 5);
scene.add(house);
generatedPositions.add(key);
}
}
}
}
}
generateHouses(100);
camera.position.set(50, 20, 50);
camera.lookAt(0, 0, 0);
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
window.addEventListener('mousedown', () => isDragging = true);
window.addEventListener('mouseup', () => isDragging = false);
window.addEventListener('mousemove', (event) => {
if (isDragging) {
const deltaMove = {
x: event.clientX - previousMousePosition.x,
y: event.clientY - previousMousePosition.y
};
camera.rotation.y -= deltaMove.x * 0.002;
camera.rotation.x -= deltaMove.y * 0.002;
}
previousMousePosition = {
x: event.clientX,
y: event.clientY
};
});
window.addEventListener('wheel', (event) => {
const zoomAmount = event.deltaY * 0.1;
const direction = new THREE.Vector3();
camera.getWorldDirection(direction);
camera.position.addScaledVector(direction, zoomAmount);
camera.updateProjectionMatrix();
generateHouses(Math.abs(camera.position.z));
});
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script><canvas style="display: block; width: 2560px; height: 1355px;" width="2560" height="1355"></canvas>
</body></html>