Spaces:
Running
Running
Update index.html
Browse files- index.html +28 -19
index.html
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>
|
7 |
<style>
|
8 |
body { margin: 0; overflow: hidden; }
|
9 |
canvas { display: block; }
|
@@ -20,15 +20,14 @@
|
|
20 |
</script>
|
21 |
<script type="module">
|
22 |
import * as THREE from 'three';
|
23 |
-
import {
|
24 |
-
import { WebGPURenderer } from 'three/addons/renderers/webgpu/WebGPURenderer.js';
|
25 |
|
26 |
// Scene setup
|
27 |
const scene = new THREE.Scene();
|
28 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
29 |
camera.position.set(0, 5, 10);
|
30 |
|
31 |
-
const renderer = new
|
32 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
33 |
renderer.setPixelRatio(window.devicePixelRatio);
|
34 |
document.body.appendChild(renderer.domElement);
|
@@ -40,7 +39,7 @@
|
|
40 |
directionalLight.position.set(1, 1, 1);
|
41 |
scene.add(directionalLight);
|
42 |
|
43 |
-
// Spaceship (simple
|
44 |
const shipGeometry = new THREE.ConeGeometry(0.5, 2, 32);
|
45 |
const shipMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00, metalness: 0.8, roughness: 0.2 });
|
46 |
const spaceship = new THREE.Mesh(shipGeometry, shipMaterial);
|
@@ -48,7 +47,7 @@
|
|
48 |
scene.add(spaceship);
|
49 |
|
50 |
// Particle system for buildings
|
51 |
-
const buildingCount =
|
52 |
const buildingGeometry = new THREE.BufferGeometry();
|
53 |
const buildingPositions = new Float32Array(buildingCount * 3);
|
54 |
const buildingColors = new Float32Array(buildingCount * 3);
|
@@ -80,7 +79,7 @@
|
|
80 |
scene.add(buildings);
|
81 |
|
82 |
// Particle system for people
|
83 |
-
const peopleCount =
|
84 |
const peopleGeometry = new THREE.BufferGeometry();
|
85 |
const peoplePositions = new Float32Array(peopleCount * 3);
|
86 |
const peopleColors = new Float32Array(peopleCount * 3);
|
@@ -111,8 +110,8 @@
|
|
111 |
const people = new THREE.Points(peopleGeometry, peopleMaterial);
|
112 |
scene.add(people);
|
113 |
|
114 |
-
// Instanced
|
115 |
-
const cubeCount =
|
116 |
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
|
117 |
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff00ff, metalness: 0.5, roughness: 0.4 });
|
118 |
const cubeMesh = new THREE.InstancedMesh(cubeGeometry, cubeMaterial, cubeCount);
|
@@ -134,9 +133,9 @@
|
|
134 |
let time = 0;
|
135 |
function updateCamera() {
|
136 |
time += 0.01;
|
137 |
-
camera.position.z -= 0.5; //
|
138 |
-
camera.position.x = Math.sin(time * 0.5) * 5; //
|
139 |
-
camera.position.y = Math.cos(time * 0.3) * 2 + 5; // Up
|
140 |
camera.lookAt(new THREE.Vector3(0, 0, camera.position.z - 100));
|
141 |
}
|
142 |
|
@@ -144,16 +143,16 @@
|
|
144 |
function updateObjects() {
|
145 |
const positions = buildings.geometry.attributes.position.array;
|
146 |
for (let i = 0; i < buildingCount; i++) {
|
147 |
-
positions[i * 3 + 2] += 0.5;
|
148 |
if (positions[i * 3 + 2] > camera.position.z + 100) {
|
149 |
-
positions[i * 3 + 2] -= 200;
|
150 |
}
|
151 |
}
|
152 |
buildings.geometry.attributes.position.needsUpdate = true;
|
153 |
|
154 |
const peoplePos = people.geometry.attributes.position.array;
|
155 |
for (let i = 0; i < peopleCount; i++) {
|
156 |
-
peoplePos[i * 3 + 2] += 0.7; // Faster
|
157 |
if (peoplePos[i * 3 + 2] > camera.position.z + 100) {
|
158 |
peoplePos[i * 3 + 2] -= 200;
|
159 |
}
|
@@ -175,10 +174,14 @@
|
|
175 |
|
176 |
// Animation loop
|
177 |
function animate() {
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
|
|
|
|
|
|
|
|
182 |
}
|
183 |
|
184 |
animate();
|
@@ -189,6 +192,12 @@
|
|
189 |
camera.updateProjectionMatrix();
|
190 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
191 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
</script>
|
193 |
</body>
|
194 |
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Three.js Spaceship Fly-Through</title>
|
7 |
<style>
|
8 |
body { margin: 0; overflow: hidden; }
|
9 |
canvas { display: block; }
|
|
|
20 |
</script>
|
21 |
<script type="module">
|
22 |
import * as THREE from 'three';
|
23 |
+
import { WebGLRenderer } from 'three';
|
|
|
24 |
|
25 |
// Scene setup
|
26 |
const scene = new THREE.Scene();
|
27 |
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
28 |
camera.position.set(0, 5, 10);
|
29 |
|
30 |
+
const renderer = new WebGLRenderer({ antialias: true });
|
31 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
32 |
renderer.setPixelRatio(window.devicePixelRatio);
|
33 |
document.body.appendChild(renderer.domElement);
|
|
|
39 |
directionalLight.position.set(1, 1, 1);
|
40 |
scene.add(directionalLight);
|
41 |
|
42 |
+
// Spaceship (simple cone)
|
43 |
const shipGeometry = new THREE.ConeGeometry(0.5, 2, 32);
|
44 |
const shipMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00, metalness: 0.8, roughness: 0.2 });
|
45 |
const spaceship = new THREE.Mesh(shipGeometry, shipMaterial);
|
|
|
47 |
scene.add(spaceship);
|
48 |
|
49 |
// Particle system for buildings
|
50 |
+
const buildingCount = 50; // Reduced for performance
|
51 |
const buildingGeometry = new THREE.BufferGeometry();
|
52 |
const buildingPositions = new Float32Array(buildingCount * 3);
|
53 |
const buildingColors = new Float32Array(buildingCount * 3);
|
|
|
79 |
scene.add(buildings);
|
80 |
|
81 |
// Particle system for people
|
82 |
+
const peopleCount = 100; // Reduced for performance
|
83 |
const peopleGeometry = new THREE.BufferGeometry();
|
84 |
const peoplePositions = new Float32Array(peopleCount * 3);
|
85 |
const peopleColors = new Float32Array(peopleCount * 3);
|
|
|
110 |
const people = new THREE.Points(peopleGeometry, peopleMaterial);
|
111 |
scene.add(people);
|
112 |
|
113 |
+
// Instanced cubes
|
114 |
+
const cubeCount = 30; // Reduced for performance
|
115 |
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
|
116 |
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff00ff, metalness: 0.5, roughness: 0.4 });
|
117 |
const cubeMesh = new THREE.InstancedMesh(cubeGeometry, cubeMaterial, cubeCount);
|
|
|
133 |
let time = 0;
|
134 |
function updateCamera() {
|
135 |
time += 0.01;
|
136 |
+
camera.position.z -= 0.5; // Forward movement
|
137 |
+
camera.position.x = Math.sin(time * 0.5) * 5; // Side-to-side
|
138 |
+
camera.position.y = Math.cos(time * 0.3) * 2 + 5; // Up-down
|
139 |
camera.lookAt(new THREE.Vector3(0, 0, camera.position.z - 100));
|
140 |
}
|
141 |
|
|
|
143 |
function updateObjects() {
|
144 |
const positions = buildings.geometry.attributes.position.array;
|
145 |
for (let i = 0; i < buildingCount; i++) {
|
146 |
+
positions[i * 3 + 2] += 0.5;
|
147 |
if (positions[i * 3 + 2] > camera.position.z + 100) {
|
148 |
+
positions[i * 3 + 2] -= 200;
|
149 |
}
|
150 |
}
|
151 |
buildings.geometry.attributes.position.needsUpdate = true;
|
152 |
|
153 |
const peoplePos = people.geometry.attributes.position.array;
|
154 |
for (let i = 0; i < peopleCount; i++) {
|
155 |
+
peoplePos[i * 3 + 2] += 0.7; // Faster for parallax
|
156 |
if (peoplePos[i * 3 + 2] > camera.position.z + 100) {
|
157 |
peoplePos[i * 3 + 2] -= 200;
|
158 |
}
|
|
|
174 |
|
175 |
// Animation loop
|
176 |
function animate() {
|
177 |
+
try {
|
178 |
+
requestAnimationFrame(animate);
|
179 |
+
updateCamera();
|
180 |
+
updateObjects();
|
181 |
+
renderer.render(scene, camera);
|
182 |
+
} catch (error) {
|
183 |
+
console.error('Rendering error:', error);
|
184 |
+
}
|
185 |
}
|
186 |
|
187 |
animate();
|
|
|
192 |
camera.updateProjectionMatrix();
|
193 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
194 |
});
|
195 |
+
|
196 |
+
// Error handling for WebGL
|
197 |
+
if (!renderer.getContext()) {
|
198 |
+
console.error('WebGL is not supported in this browser.');
|
199 |
+
alert('WebGL is not supported. Please use a modern browser like Chrome or Firefox.');
|
200 |
+
}
|
201 |
</script>
|
202 |
</body>
|
203 |
</html>
|