Update index.html
Browse files- index.html +27 -22
index.html
CHANGED
@@ -17,67 +17,72 @@
|
|
17 |
</head>
|
18 |
<body>
|
19 |
<!-- Load Three.js -->
|
20 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/
|
21 |
<!-- Load OrbitControls -->
|
22 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/
|
23 |
|
24 |
<script>
|
25 |
-
// Create
|
26 |
const scene = new THREE.Scene();
|
27 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
28 |
const renderer = new THREE.WebGLRenderer();
|
29 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
30 |
document.body.appendChild(renderer.domElement);
|
31 |
|
32 |
-
// Add
|
33 |
const controls = new THREE.OrbitControls(camera, renderer.domElement);
|
34 |
controls.enableDamping = true; // Smooth movement
|
|
|
|
|
|
|
35 |
|
36 |
-
// Create Earth
|
37 |
const earthGeometry = new THREE.SphereGeometry(10, 32, 32);
|
38 |
const earthMaterial = new THREE.MeshStandardMaterial({ color: 0x2233ff });
|
39 |
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
|
40 |
scene.add(earth);
|
41 |
|
42 |
-
// Create Rocket (
|
43 |
const rocketGeometry = new THREE.SphereGeometry(1, 16, 16);
|
44 |
const rocketMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
|
45 |
const rocket = new THREE.Mesh(rocketGeometry, rocketMaterial);
|
46 |
scene.add(rocket);
|
47 |
|
48 |
-
//
|
49 |
-
const
|
50 |
-
const orbitSpeed = 0.002; // Orbit speed
|
51 |
-
|
52 |
-
// Lighting
|
53 |
-
const ambientLight = new THREE.AmbientLight(0xffffff, 0.3);
|
54 |
scene.add(ambientLight);
|
55 |
|
56 |
-
const pointLight = new THREE.PointLight(0xffffff, 1.
|
57 |
-
pointLight.position.set(
|
58 |
scene.add(pointLight);
|
59 |
|
60 |
-
//
|
61 |
camera.position.set(0, 30, 50);
|
62 |
camera.lookAt(earth.position);
|
63 |
|
64 |
-
//
|
|
|
|
|
|
|
|
|
|
|
65 |
function animate() {
|
66 |
requestAnimationFrame(animate);
|
67 |
-
|
68 |
-
//
|
69 |
-
|
70 |
rocket.position.x = orbitRadius * Math.cos(time);
|
71 |
rocket.position.z = orbitRadius * Math.sin(time);
|
72 |
-
rocket.position.y = 0; //
|
73 |
|
|
|
74 |
controls.update();
|
75 |
renderer.render(scene, camera);
|
76 |
}
|
77 |
|
78 |
-
animate();
|
79 |
|
80 |
-
//
|
81 |
window.addEventListener("resize", () => {
|
82 |
camera.aspect = window.innerWidth / window.innerHeight;
|
83 |
camera.updateProjectionMatrix();
|
|
|
17 |
</head>
|
18 |
<body>
|
19 |
<!-- Load Three.js -->
|
20 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
|
21 |
<!-- Load OrbitControls -->
|
22 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/examples/js/controls/OrbitControls.js"></script>
|
23 |
|
24 |
<script>
|
25 |
+
// 1️⃣ Create Scene, Camera, and Renderer
|
26 |
const scene = new THREE.Scene();
|
27 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
28 |
const renderer = new THREE.WebGLRenderer();
|
29 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
30 |
document.body.appendChild(renderer.domElement);
|
31 |
|
32 |
+
// 2️⃣ Add Camera Controls
|
33 |
const controls = new THREE.OrbitControls(camera, renderer.domElement);
|
34 |
controls.enableDamping = true; // Smooth movement
|
35 |
+
controls.dampingFactor = 0.05;
|
36 |
+
controls.minDistance = 20;
|
37 |
+
controls.maxDistance = 100;
|
38 |
|
39 |
+
// 3️⃣ Create Earth (Blue Sphere)
|
40 |
const earthGeometry = new THREE.SphereGeometry(10, 32, 32);
|
41 |
const earthMaterial = new THREE.MeshStandardMaterial({ color: 0x2233ff });
|
42 |
const earth = new THREE.Mesh(earthGeometry, earthMaterial);
|
43 |
scene.add(earth);
|
44 |
|
45 |
+
// 4️⃣ Create Rocket (Red Sphere)
|
46 |
const rocketGeometry = new THREE.SphereGeometry(1, 16, 16);
|
47 |
const rocketMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
|
48 |
const rocket = new THREE.Mesh(rocketGeometry, rocketMaterial);
|
49 |
scene.add(rocket);
|
50 |
|
51 |
+
// 5️⃣ Lighting (Improved for Better Visibility)
|
52 |
+
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
|
|
|
|
|
|
|
|
|
53 |
scene.add(ambientLight);
|
54 |
|
55 |
+
const pointLight = new THREE.PointLight(0xffffff, 1.5, 100);
|
56 |
+
pointLight.position.set(30, 30, 30);
|
57 |
scene.add(pointLight);
|
58 |
|
59 |
+
// 6️⃣ Camera Positioning
|
60 |
camera.position.set(0, 30, 50);
|
61 |
camera.lookAt(earth.position);
|
62 |
|
63 |
+
// 7️⃣ Orbit Parameters
|
64 |
+
const orbitRadius = 30; // Distance from Earth
|
65 |
+
const orbitSpeed = 0.002; // Speed of Orbit
|
66 |
+
let time = 0; // Time tracker
|
67 |
+
|
68 |
+
// 8️⃣ Animation Loop
|
69 |
function animate() {
|
70 |
requestAnimationFrame(animate);
|
71 |
+
|
72 |
+
// Calculate Rocket's Orbit Position
|
73 |
+
time += orbitSpeed;
|
74 |
rocket.position.x = orbitRadius * Math.cos(time);
|
75 |
rocket.position.z = orbitRadius * Math.sin(time);
|
76 |
+
rocket.position.y = 5 * Math.sin(time * 0.5); // Small vertical oscillation
|
77 |
|
78 |
+
// Update camera controls and render
|
79 |
controls.update();
|
80 |
renderer.render(scene, camera);
|
81 |
}
|
82 |
|
83 |
+
animate(); // Start animation
|
84 |
|
85 |
+
// 9️⃣ Window Resize Handling
|
86 |
window.addEventListener("resize", () => {
|
87 |
camera.aspect = window.innerWidth / window.innerHeight;
|
88 |
camera.updateProjectionMatrix();
|