-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
218 lines (180 loc) · 8.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
<title>3D Galaxy with Three.js</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
background: radial-gradient(circle, #000428, #004e92);
}
canvas {
display: block;
}
</style>
</head>
<body>
<script>
// Ensure Three.js is loaded before running the script
if (THREE) {
console.log("Three.js is loaded successfully!");
} else {
console.error("Three.js failed to load!");
}
// Scene, Camera, Renderer
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);
// Add Fog for Atmospheric Effect
scene.fog = new THREE.Fog(0x000033, 500, 1000);
// Galaxy Background
const starGeometry = new THREE.BufferGeometry();
const starVertices = [];
for (let i = 0; i < 10000; i++) {
const x = (Math.random() - 0.5) * 2000;
const y = (Math.random() - 0.5) * 2000;
const z = (Math.random() - 0.5) * 2000;
starVertices.push(x, y, z);
}
starGeometry.addAttribute('position', new THREE.BufferAttribute(starVertices, 3));
const starMaterial = new THREE.PointsMaterial({ color: 0xffffff, size: 1 });
const stars = new THREE.Points(starGeometry, starMaterial);
scene.add(stars);
// Central Star
const starTexture = new THREE.TextureLoader().load('https://threejs.org/examples/textures/uv_grid_opengl.jpg');
const starGeometry2 = new THREE.SphereGeometry(5, 32, 32);
const starMaterial2 = new THREE.MeshBasicMaterial({ map: starTexture, color: 0xffff00 });
const centralStar = new THREE.Mesh(starGeometry2, starMaterial2);
scene.add(centralStar);
// Planets
const planets = [];
const planetTextures = [
'https://threejs.org/examples/textures/planets/earth_atmos_2048.jpg', // Earth texture
'https://threejs.org/examples/textures/planets/moon_1024.jpg', // Moon texture (replaced Mars)
'https://threejs.org/examples/textures/uv_grid_opengl.jpg' // Sun texture (replaced Jupiter)
];
const planetDistances = [30, 60, 90];
const planetSizes = [3, 4, 5];
planetTextures.forEach((texture, index) => {
const planetGeometry = new THREE.SphereGeometry(planetSizes[index], 32, 32);
const planetMaterial = new THREE.MeshStandardMaterial({
map: new THREE.TextureLoader().load(texture),
roughness: 1,
metalness: 0
});
const planet = new THREE.Mesh(planetGeometry, planetMaterial);
planet.position.x = planetDistances[index];
planets.push(planet);
scene.add(planet);
});
// Lighting
const pointLight = new THREE.PointLight(0xffffff, 1, 1000);
pointLight.position.set(0, 0, 0);
scene.add(pointLight);
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
// Space Dust (Particle System)
const dustGeometry = new THREE.BufferGeometry();
const dustVertices = [];
for (let i = 0; i < 5000; i++) {
const x = (Math.random() - 0.5) * 2000;
const y = (Math.random() - 0.5) * 2000;
const z = (Math.random() - 0.5) * 2000;
dustVertices.push(x, y, z);
}
dustGeometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(dustVertices), 3));
const dustMaterial = new THREE.PointsMaterial({ color: 0x888888, size: 0.5, transparent: true, opacity: 0.8 });
const dust = new THREE.Points(dustGeometry, dustMaterial);
scene.add(dust);
// Camera Position
camera.position.z = 150;
// Mouse Wheel for Zooming
const zoomSpeed = 1.5;
document.addEventListener('wheel', (e) => {
const delta = e.deltaY * 0.01; // Normalize wheel delta
camera.position.x += Math.sin(camera.rotation.y) * delta * zoomSpeed;
camera.position.z += Math.cos(camera.rotation.y) * delta * zoomSpeed;
});
// Mouse Free Look
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
document.addEventListener('mousedown', (e) => {
isDragging = true;
});
document.addEventListener('mousemove', (e) => {
if (isDragging) {
const deltaX = e.clientX - previousMousePosition.x;
const deltaY = e.clientY - previousMousePosition.y;
camera.rotation.y -= deltaX * 0.002; // Rotate horizontally
camera.rotation.x -= deltaY * 0.002; // Rotate vertically
camera.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, camera.rotation.x)); // Limit vertical rotation
}
previousMousePosition = { x: e.clientX, y: e.clientY };
});
document.addEventListener('mouseup', () => {
isDragging = false;
});
// ZQSD Key Movement
const moveSpeed = 0.5;
const keys = { z: false, q: false, s: false, d: false };
document.addEventListener('keydown', (e) => {
if (e.key === 'z' || e.key === 'Z') keys.z = true;
if (e.key === 'q' || e.key === 'Q') keys.q = true;
if (e.key === 's' || e.key === 'S') keys.s = true;
if (e.key === 'd' || e.key === 'D') keys.d = true;
});
document.addEventListener('keyup', (e) => {
if (e.key === 'z' || e.key === 'Z') keys.z = false;
if (e.key === 'q' || e.key === 'Q') keys.q = false;
if (e.key === 's' || e.key === 'S') keys.s = false;
if (e.key === 'd' || e.key === 'D') keys.d = false;
});
const updateCameraPosition = () => {
if (keys.z) { // Move forward
camera.position.x -= Math.sin(camera.rotation.y) * moveSpeed;
camera.position.z -= Math.cos(camera.rotation.y) * moveSpeed;
}
if (keys.s) { // Move backward
camera.position.x += Math.sin(camera.rotation.y) * moveSpeed;
camera.position.z += Math.cos(camera.rotation.y) * moveSpeed;
}
if (keys.q) { // Move left
camera.position.x -= Math.cos(camera.rotation.y) * moveSpeed;
camera.position.z += Math.sin(camera.rotation.y) * moveSpeed;
}
if (keys.d) { // Move right
camera.position.x += Math.cos(camera.rotation.y) * moveSpeed;
camera.position.z -= Math.sin(camera.rotation.y) * moveSpeed;
}
};
// Animation Loop
const animate = () => {
requestAnimationFrame(animate);
// Rotate Planets
planets.forEach((planet, index) => {
planet.rotation.y += 0.01;
const angle = Date.now() * 0.0005 * (index + 1);
planet.position.x = Math.cos(angle) * planetDistances[index];
planet.position.z = Math.sin(angle) * planetDistances[index];
});
// Render Scene
renderer.render(scene, camera);
};
animate();
// Handle Window Resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>