File size: 7,907 Bytes
f3968a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebGPU Three.js Spaceship Fly-Through</title>
    <style>
        body { margin: 0; overflow: hidden; }
        canvas { display: block; }
    </style>
</head>
<body>
    <script type="importmap">
        {
            "imports": {
                "three": "https://unpkg.com/[email protected]/build/three.module.js",
                "three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
            }
        }
    </script>
    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import { WebGPURenderer } from 'three/addons/renderers/webgpu/WebGPURenderer.js';

        // Scene setup
        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 5, 10);

        const renderer = new WebGPURenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.setPixelRatio(window.devicePixelRatio);
        document.body.appendChild(renderer.domElement);

        // Lighting
        const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
        scene.add(ambientLight);
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
        directionalLight.position.set(1, 1, 1);
        scene.add(directionalLight);

        // Spaceship (simple geometry)
        const shipGeometry = new THREE.ConeGeometry(0.5, 2, 32);
        const shipMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00, metalness: 0.8, roughness: 0.2 });
        const spaceship = new THREE.Mesh(shipGeometry, shipMaterial);
        spaceship.position.set(0, 0, 0);
        scene.add(spaceship);

        // Particle system for buildings
        const buildingCount = 100;
        const buildingGeometry = new THREE.BufferGeometry();
        const buildingPositions = new Float32Array(buildingCount * 3);
        const buildingColors = new Float32Array(buildingCount * 3);
        const buildingSizes = new Float32Array(buildingCount);

        for (let i = 0; i < buildingCount; i++) {
            buildingPositions[i * 3] = (Math.random() - 0.5) * 200;
            buildingPositions[i * 3 + 1] = Math.random() * 10;
            buildingPositions[i * 3 + 2] = (Math.random() - 0.5) * 200;
            buildingColors[i * 3] = 0.2;
            buildingColors[i * 3 + 1] = 0.6;
            buildingColors[i * 3 + 2] = 1.0;
            buildingSizes[i] = Math.random() * 2 + 1;
        }

        buildingGeometry.setAttribute('position', new THREE.BufferAttribute(buildingPositions, 3));
        buildingGeometry.setAttribute('color', new THREE.BufferAttribute(buildingColors, 3));
        buildingGeometry.setAttribute('size', new THREE.BufferAttribute(buildingSizes, 1));

        const buildingMaterial = new THREE.PointsMaterial({
            size: 2,
            vertexColors: true,
            transparent: true,
            opacity: 0.7,
            sizeAttenuation: true
        });

        const buildings = new THREE.Points(buildingGeometry, buildingMaterial);
        scene.add(buildings);

        // Particle system for people
        const peopleCount = 200;
        const peopleGeometry = new THREE.BufferGeometry();
        const peoplePositions = new Float32Array(peopleCount * 3);
        const peopleColors = new Float32Array(peopleCount * 3);
        const peopleSizes = new Float32Array(peopleCount);

        for (let i = 0; i < peopleCount; i++) {
            peoplePositions[i * 3] = (Math.random() - 0.5) * 150;
            peoplePositions[i * 3 + 1] = Math.random() * 5;
            peoplePositions[i * 3 + 2] = (Math.random() - 0.5) * 150;
            peopleColors[i * 3] = 1.0;
            peopleColors[i * 3 + 1] = 0.8;
            peopleColors[i * 3 + 2] = 0.8;
            peopleSizes[i] = Math.random() * 0.5 + 0.2;
        }

        peopleGeometry.setAttribute('position', new THREE.BufferAttribute(peoplePositions, 3));
        peopleGeometry.setAttribute('color', new THREE.BufferAttribute(peopleColors, 3));
        peopleGeometry.setAttribute('size', new THREE.BufferAttribute(peopleSizes, 1));

        const peopleMaterial = new THREE.PointsMaterial({
            size: 0.5,
            vertexColors: true,
            transparent: true,
            opacity: 0.6,
            sizeAttenuation: true
        });

        const people = new THREE.Points(peopleGeometry, peopleMaterial);
        scene.add(people);

        // Instanced geometry for repeating objects (cubes)
        const cubeCount = 50;
        const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
        const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff00ff, metalness: 0.5, roughness: 0.4 });
        const cubeMesh = new THREE.InstancedMesh(cubeGeometry, cubeMaterial, cubeCount);

        const dummy = new THREE.Object3D();
        for (let i = 0; i < cubeCount; i++) {
            dummy.position.set(
                (Math.random() - 0.5) * 100,
                Math.random() * 10,
                (Math.random() - 0.5) * 100
            );
            dummy.scale.setScalar(Math.random() * 0.5 + 0.5);
            dummy.updateMatrix();
            cubeMesh.setMatrixAt(i, dummy.matrix);
        }
        scene.add(cubeMesh);

        // Camera movement
        let time = 0;
        function updateCamera() {
            time += 0.01;
            camera.position.z -= 0.5; // Continuous forward movement
            camera.position.x = Math.sin(time * 0.5) * 5; // Gentle side-to-side
            camera.position.y = Math.cos(time * 0.3) * 2 + 5; // Up and down
            camera.lookAt(new THREE.Vector3(0, 0, camera.position.z - 100));
        }

        // Parallax and repetition
        function updateObjects() {
            const positions = buildings.geometry.attributes.position.array;
            for (let i = 0; i < buildingCount; i++) {
                positions[i * 3 + 2] += 0.5; // Move towards camera
                if (positions[i * 3 + 2] > camera.position.z + 100) {
                    positions[i * 3 + 2] -= 200; // Reset to back
                }
            }
            buildings.geometry.attributes.position.needsUpdate = true;

            const peoplePos = people.geometry.attributes.position.array;
            for (let i = 0; i < peopleCount; i++) {
                peoplePos[i * 3 + 2] += 0.7; // Faster movement for parallax
                if (peoplePos[i * 3 + 2] > camera.position.z + 100) {
                    peoplePos[i * 3 + 2] -= 200;
                }
            }
            people.geometry.attributes.position.needsUpdate = true;

            for (let i = 0; i < cubeCount; i++) {
                cubeMesh.getMatrixAt(i, dummy.matrix);
                dummy.matrix.decompose(dummy.position, dummy.quaternion, dummy.scale);
                dummy.position.z += 0.6;
                if (dummy.position.z > camera.position.z + 100) {
                    dummy.position.z -= 200;
                }
                dummy.updateMatrix();
                cubeMesh.setMatrixAt(i, dummy.matrix);
            }
            cubeMesh.instanceMatrix.needsUpdate = true;
        }

        // Animation loop
        function animate() {
            requestAnimationFrame(animate);
            updateCamera();
            updateObjects();
            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>