File size: 9,188 Bytes
d8be81d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
219
220
221
222
223
224
225
226
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Bouncing Balls in Sphere</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.min.js"></script>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            font-family: 'Inter', sans-serif;
        }
        #info {
            position: absolute;
            top: 20px;
            left: 20px;
            color: white;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            border-radius: 5px;
            z-index: 100;
        }
        #controls {
            position: absolute;
            bottom: 20px;
            left: 20px;
            color: white;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            border-radius: 5px;
            z-index: 100;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body class="bg-gray-900">
    <div id="info" class="text-sm">
        <h1 class="text-xl font-bold mb-2">Bouncing Balls in Sphere</h1>
        <p>Use mouse to rotate view</p>
        <p>Scroll to zoom in/out</p>
    </div>
    
    <div id="controls">
        <div class="flex items-center mb-2">
            <label class="text-white mr-2">Ball Count:</label>
            <input type="range" id="ballCount" min="10" max="200" value="50" class="w-32">
            <span id="ballCountValue" class="ml-2">50</span>
        </div>
        <div class="flex items-center mb-2">
            <label class="text-white mr-2">Bounce Speed:</label>
            <input type="range" id="bounceSpeed" min="0.1" max="2" step="0.1" value="1" class="w-32">
            <span id="bounceSpeedValue" class="ml-2">1.0</span>
        </div>
        <div class="flex items-center">
            <label class="text-white mr-2">Sphere Opacity:</label>
            <input type="range" id="sphereOpacity" min="0" max="1" step="0.1" value="0.2" class="w-32">
            <span id="sphereOpacityValue" class="ml-2">0.2</span>
        </div>
    </div>

    <script>
        // Scene setup
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x111111);
        
        // Camera setup
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 30;
        
        // Renderer setup
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMap.enabled = true;
        document.body.appendChild(renderer.domElement);
        
        // Orbit controls for camera interaction
        const controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.enableDamping = true;
        controls.dampingFactor = 0.05;
        
        // Lighting
        const ambientLight = new THREE.AmbientLight(0x404040);
        scene.add(ambientLight);
        
        const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
        directionalLight.position.set(1, 1, 1);
        directionalLight.castShadow = true;
        scene.add(directionalLight);
        
        // Create the transparent sphere
        const sphereRadius = 15;
        const sphereGeometry = new THREE.SphereGeometry(sphereRadius, 32, 32);
        const sphereMaterial = new THREE.MeshPhongMaterial({
            color: 0x3399ff,
            transparent: true,
            opacity: 0.2,
            wireframe: true,
            side: THREE.DoubleSide
        });
        const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        scene.add(sphere);
        
        // Create bouncing balls
        const balls = [];
        const ballGeometry = new THREE.SphereGeometry(0.5, 16, 16);
        const ballMaterial = new THREE.MeshPhongMaterial({
            color: 0xff5555,
            shininess: 100
        });
        
        function createBalls(count) {
            // Remove existing balls
            balls.forEach(ball => scene.remove(ball));
            balls.length = 0;
            
            // Create new balls
            for (let i = 0; i < count; i++) {
                const ball = new THREE.Mesh(ballGeometry, ballMaterial.clone());
                ball.castShadow = true;
                ball.receiveShadow = true;
                
                // Random position inside sphere
                const radius = Math.random() * (sphereRadius - 1);
                const theta = Math.random() * Math.PI * 2;
                const phi = Math.random() * Math.PI;
                
                ball.position.x = radius * Math.sin(phi) * Math.cos(theta);
                ball.position.y = radius * Math.sin(phi) * Math.sin(theta);
                ball.position.z = radius * Math.cos(phi);
                
                // Random velocity
                ball.userData.velocity = new THREE.Vector3(
                    (Math.random() - 0.5) * 0.2,
                    (Math.random() - 0.5) * 0.2,
                    (Math.random() - 0.5) * 0.2
                );
                
                // Random color
                ball.material.color.setHSL(Math.random(), 0.7, 0.5);
                
                scene.add(ball);
                balls.push(ball);
            }
        }
        
        // Initial creation
        createBalls(50);
        
        // Animation variables
        let bounceSpeed = 1.0;
        
        // Animation loop
        function animate() {
            requestAnimationFrame(animate);
            
            // Update balls
            balls.forEach(ball => {
                // Move ball
                ball.position.x += ball.userData.velocity.x * bounceSpeed;
                ball.position.y += ball.userData.velocity.y * bounceSpeed;
                ball.position.z += ball.userData.velocity.z * bounceSpeed;
                
                // Check collision with sphere boundary
                const distance = ball.position.length();
                if (distance > sphereRadius - 1) {
                    // Calculate normal vector from sphere center to ball
                    const normal = ball.position.clone().normalize();
                    
                    // Reflect velocity
                    ball.userData.velocity.reflect(normal);
                    
                    // Add some randomness to bounce
                    ball.userData.velocity.add(
                        new THREE.Vector3(
                            (Math.random() - 0.5) * 0.02,
                            (Math.random() - 0.5) * 0.02,
                            (Math.random() - 0.5) * 0.02
                        )
                    );
                    
                    // Dampen velocity slightly
                    ball.userData.velocity.multiplyScalar(0.98);
                }
            });
            
            // Update controls
            controls.update();
            
            // 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);
        });
        
        // UI controls
        document.getElementById('ballCount').addEventListener('input', (e) => {
            const count = parseInt(e.target.value);
            document.getElementById('ballCountValue').textContent = count;
            createBalls(count);
        });
        
        document.getElementById('bounceSpeed').addEventListener('input', (e) => {
            bounceSpeed = parseFloat(e.target.value);
            document.getElementById('bounceSpeedValue').textContent = bounceSpeed.toFixed(1);
        });
        
        document.getElementById('sphereOpacity').addEventListener('input', (e) => {
            const opacity = parseFloat(e.target.value);
            sphere.material.opacity = opacity;
            document.getElementById('sphereOpacityValue').textContent = opacity.toFixed(1);
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - <a href="https://enzostvs-deepsite.hf.space?remix=zackrr/ball-v0-1" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
</html>