-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_three_js.html
More file actions
486 lines (415 loc) · 18.2 KB
/
interactive_three_js.html
File metadata and controls
486 lines (415 loc) · 18.2 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Amazing 3D Interactive Animation</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
background: #000;
}
#canvas-container {
position: relative;
width: 100vw;
height: 100vh;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 15px;
border-radius: 10px;
user-select: none;
z-index: 100;
}
#info h3 {
margin: 0 0 10px 0;
color: #00ff88;
}
#info p {
margin: 5px 0;
font-size: 14px;
}
.control-btn {
background: #00ff88;
border: none;
color: black;
padding: 8px 15px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
transition: all 0.3s;
}
.control-btn:hover {
background: #00cc66;
transform: scale(1.05);
}
</style>
</head>
<body>
<div id="canvas-container">
<div id="info">
<h3>🎨 Interactive 3D Animation</h3>
<p>🖱️ Move mouse to interact</p>
<p>🖱️ Click and drag to rotate camera</p>
<p>📜 Scroll to zoom in/out</p>
<p>🎯 Click objects to explode them</p>
<button class="control-btn" onclick="toggleAnimation()">⏯️ Toggle Animation</button>
<button class="control-btn" onclick="changeColorScheme()">🎨 Change Colors</button>
<button class="control-btn" onclick="addMoreObjects()">➕ Add Objects</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// Global variables
let scene, camera, renderer, controls;
let objects = [];
let particles;
let animationEnabled = true;
let colorScheme = 0;
let mouseX = 0, mouseY = 0;
let raycaster, mouse;
// Color schemes
const colorSchemes = [
['#ff006e', '#fb5607', '#ffbe0b', '#8338ec', '#3a86ff'],
['#ff4365', '#00d9ff', '#72ff00', '#ff00ff', '#ffaa00'],
['#f72585', '#b5179e', '#7209b7', '#560bad', '#480ca8'],
['#ff0a54', '#ff477e', '#ff5c8a', '#ff7096', '#ff85a1']
];
// Initialize the scene
function init() {
// Create scene
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x000000, 1, 100);
// Create camera
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 0, 30);
// Create renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById('canvas-container').appendChild(renderer.domElement);
// Add controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.maxDistance = 50;
controls.minDistance = 10;
// Add lights
addLights();
// Create objects
createObjects();
// Create particles
createParticles();
// Raycaster for mouse interaction
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
// Event listeners
window.addEventListener('resize', onWindowResize);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('click', onMouseClick);
// Start animation
animate();
}
function addLights() {
// Ambient light
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
scene.add(ambientLight);
// Directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(10, 10, 5);
directionalLight.castShadow = true;
directionalLight.shadow.camera.near = 0.1;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -20;
directionalLight.shadow.camera.right = 20;
directionalLight.shadow.camera.top = 20;
directionalLight.shadow.camera.bottom = -20;
scene.add(directionalLight);
// Point lights
const colors = [0xff0080, 0x00ff80, 0x8000ff];
for (let i = 0; i < 3; i++) {
const pointLight = new THREE.PointLight(colors[i], 0.5, 30);
pointLight.position.set(
Math.cos(i * Math.PI * 2 / 3) * 15,
Math.sin(i * Math.PI * 2 / 3) * 15,
5
);
scene.add(pointLight);
}
}
function createObjects() {
const geometries = [
new THREE.BoxGeometry(3, 3, 3),
new THREE.SphereGeometry(2, 32, 32),
new THREE.ConeGeometry(2, 4, 32),
new THREE.TorusGeometry(2, 0.8, 16, 100),
new THREE.OctahedronGeometry(2.5),
new THREE.TetrahedronGeometry(3),
new THREE.DodecahedronGeometry(2.5),
new THREE.IcosahedronGeometry(2.5)
];
const colors = colorSchemes[colorScheme];
for (let i = 0; i < 8; i++) {
const geometry = geometries[i % geometries.length];
const material = new THREE.MeshPhongMaterial({
color: colors[i % colors.length],
shininess: 100,
specular: 0x222222,
emissive: colors[i % colors.length],
emissiveIntensity: 0.1
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(
(Math.random() - 0.5) * 20,
(Math.random() - 0.5) * 20,
(Math.random() - 0.5) * 10
);
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
mesh.castShadow = true;
mesh.receiveShadow = true;
// Store original properties for animation
mesh.userData = {
originalPosition: mesh.position.clone(),
rotationSpeed: {
x: (Math.random() - 0.5) * 0.02,
y: (Math.random() - 0.5) * 0.02,
z: (Math.random() - 0.5) * 0.02
},
floatSpeed: Math.random() * 0.01 + 0.005,
floatHeight: Math.random() * 2 + 1,
phase: Math.random() * Math.PI * 2
};
objects.push(mesh);
scene.add(mesh);
}
}
function createParticles() {
const particlesGeometry = new THREE.BufferGeometry();
const particlesCount = 500;
const positions = new Float32Array(particlesCount * 3);
const colors = new Float32Array(particlesCount * 3);
for (let i = 0; i < particlesCount * 3; i += 3) {
positions[i] = (Math.random() - 0.5) * 50;
positions[i + 1] = (Math.random() - 0.5) * 50;
positions[i + 2] = (Math.random() - 0.5) * 50;
colors[i] = Math.random();
colors[i + 1] = Math.random();
colors[i + 2] = Math.random();
}
particlesGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
particlesGeometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
const particlesMaterial = new THREE.PointsMaterial({
size: 0.1,
vertexColors: true,
transparent: true,
opacity: 0.8,
blending: THREE.AdditiveBlending
});
particles = new THREE.Points(particlesGeometry, particlesMaterial);
scene.add(particles);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onMouseMove(event) {
mouseX = (event.clientX / window.innerWidth) * 2 - 1;
mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
mouse.x = mouseX;
mouse.y = mouseY;
}
function onMouseClick(event) {
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
const object = intersects[0].object;
explodeObject(object);
}
}
function explodeObject(object) {
const explosionForce = 5;
// Create explosion effect
const particleCount = 50;
const explosionGeometry = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
const velocities = [];
for (let i = 0; i < particleCount; i++) {
const angle = (i / particleCount) * Math.PI * 2;
const velocity = new THREE.Vector3(
Math.cos(angle) * explosionForce + (Math.random() - 0.5),
(Math.random() - 0.5) * explosionForce,
Math.sin(angle) * explosionForce + (Math.random() - 0.5)
);
velocities.push(velocity);
positions[i * 3] = object.position.x;
positions[i * 3 + 1] = object.position.y;
positions[i * 3 + 2] = object.position.z;
}
explosionGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const explosionMaterial = new THREE.PointsMaterial({
color: object.material.color,
size: 0.3,
transparent: true,
opacity: 1,
blending: THREE.AdditiveBlending
});
const explosionParticles = new THREE.Points(explosionGeometry, explosionMaterial);
scene.add(explosionParticles);
// Animate explosion
let frame = 0;
const animateExplosion = () => {
frame++;
const positions = explosionParticles.geometry.attributes.position.array;
for (let i = 0; i < particleCount; i++) {
positions[i * 3] += velocities[i].x * 0.1;
positions[i * 3 + 1] += velocities[i].y * 0.1;
positions[i * 3 + 2] += velocities[i].z * 0.1;
velocities[i].y -= 0.01; // gravity
}
explosionParticles.geometry.attributes.position.needsUpdate = true;
explosionMaterial.opacity = 1 - frame / 100;
if (frame < 100) {
requestAnimationFrame(animateExplosion);
} else {
scene.remove(explosionParticles);
}
};
animateExplosion();
// Reset object position after explosion
setTimeout(() => {
object.position.copy(object.userData.originalPosition);
}, 1000);
}
function animate() {
requestAnimationFrame(animate);
if (animationEnabled) {
const time = Date.now() * 0.001;
// Animate objects
objects.forEach((object, index) => {
// Rotation
object.rotation.x += object.userData.rotationSpeed.x;
object.rotation.y += object.userData.rotationSpeed.y;
object.rotation.z += object.userData.rotationSpeed.z;
// Floating motion
const floatY = Math.sin(time * object.userData.floatSpeed + object.userData.phase) *
object.userData.floatHeight;
object.position.y = object.userData.originalPosition.y + floatY;
// React to mouse
const distance = object.position.distanceTo(camera.position);
const influence = Math.max(0, 1 - distance / 30);
object.position.x += mouseX * influence * 0.1;
object.position.z += mouseY * influence * 0.1;
// Scale pulsing
const scale = 1 + Math.sin(time * 2 + index) * 0.1;
object.scale.set(scale, scale, scale);
});
// Animate particles
if (particles) {
particles.rotation.y += 0.001;
particles.rotation.x += 0.0005;
// Wave effect on particles
const positions = particles.geometry.attributes.position.array;
for (let i = 0; i < positions.length; i += 3) {
positions[i + 1] += Math.sin(time + positions[i] * 0.1) * 0.01;
}
particles.geometry.attributes.position.needsUpdate = true;
}
}
controls.update();
renderer.render(scene, camera);
}
// Control functions
function toggleAnimation() {
animationEnabled = !animationEnabled;
}
function changeColorScheme() {
colorScheme = (colorScheme + 1) % colorSchemes.length;
const colors = colorSchemes[colorScheme];
objects.forEach((object, index) => {
const color = colors[index % colors.length];
object.material.color.set(color);
object.material.emissive.set(color);
});
}
function addMoreObjects() {
const colors = colorSchemes[colorScheme];
const geometries = [
new THREE.BoxGeometry(2, 2, 2),
new THREE.SphereGeometry(1.5, 32, 32),
new THREE.ConeGeometry(1.5, 3, 32),
new THREE.TorusKnotGeometry(1.5, 0.5, 100, 16)
];
for (let i = 0; i < 4; i++) {
const geometry = geometries[i % geometries.length];
const material = new THREE.MeshPhongMaterial({
color: colors[i % colors.length],
shininess: 100,
specular: 0x222222,
emissive: colors[i % colors.length],
emissiveIntensity: 0.1
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(
(Math.random() - 0.5) * 30,
(Math.random() - 0.5) * 30,
(Math.random() - 0.5) * 20
);
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData = {
originalPosition: mesh.position.clone(),
rotationSpeed: {
x: (Math.random() - 0.5) * 0.02,
y: (Math.random() - 0.5) * 0.02,
z: (Math.random() - 0.5) * 0.02
},
floatSpeed: Math.random() * 0.01 + 0.005,
floatHeight: Math.random() * 2 + 1,
phase: Math.random() * Math.PI * 2
};
objects.push(mesh);
scene.add(mesh);
// Entrance animation
mesh.scale.set(0, 0, 0);
const targetScale = 1 + Math.random() * 0.5;
const animateIn = () => {
mesh.scale.x = Math.min(mesh.scale.x + 0.05, targetScale);
mesh.scale.y = Math.min(mesh.scale.y + 0.05, targetScale);
mesh.scale.z = Math.min(mesh.scale.z + 0.05, targetScale);
if (mesh.scale.x < targetScale) {
requestAnimationFrame(animateIn);
}
};
animateIn();
}
}
// Initialize everything
init();
</script>
</body>
</html>