Spaces:
Running
Running
File size: 30,566 Bytes
e5d943e f81eabd e5d943e f81eabd e5d943e f81eabd e5d943e f81eabd e5d943e f81eabd e5d943e |
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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
/**
* 🌿 Ivy's Creative Studio
* Tab 6: Three.js 3D Renderer
*
* Interactive 3D scenes with Three.js
* Now with 8 scenes, 8 palettes, 6 materials, and effects!
*/
class ThreeJSRenderer {
constructor() {
this.scene = null;
this.camera = null;
this.renderer = null;
this.controls = null;
this.objects = [];
this.animationId = null;
this.isActive = false;
this.clock = new THREE.Clock();
// Parameters
this.params = {
sceneType: "cubes",
materialType: "standard",
palette: "rainbow",
objectCount: 50,
speed: 1.0,
scale: 1.0,
wireframe: false,
autoRotate: true,
shadows: false,
bloom: false
};
}
init(canvas) {
this.canvas = canvas;
// Create scene
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0x0a0a0f);
this.scene.fog = new THREE.Fog(0x0a0a0f, 10, 50);
// Use fallback dimensions if canvas is hidden (will be resized properly on start())
const width = canvas.clientWidth || 800;
const height = canvas.clientHeight || 500;
// Create camera
this.camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
this.camera.position.set(0, 5, 15);
// Create renderer
this.renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true
});
this.renderer.setSize(width, height);
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
// Add lights
this.setupLights();
// Add controls
this.controls = new THREE.OrbitControls(this.camera, canvas);
this.controls.enableDamping = true;
this.controls.dampingFactor = 0.05;
this.controls.autoRotate = this.params.autoRotate;
this.controls.autoRotateSpeed = 0.5;
// Create initial scene
this.createScene();
// Handle resize
this.handleResize = this.handleResize.bind(this);
window.addEventListener("resize", this.handleResize);
}
setupLights() {
// Ambient light
const ambient = new THREE.AmbientLight(0x404040, 0.5);
this.scene.add(ambient);
// Directional light
const directional = new THREE.DirectionalLight(0xffffff, 1);
directional.position.set(5, 10, 7);
this.scene.add(directional);
// Point lights for color
const pointLight1 = new THREE.PointLight(0x6366f1, 1, 50);
pointLight1.position.set(-10, 5, 0);
this.scene.add(pointLight1);
const pointLight2 = new THREE.PointLight(0x8b5cf6, 1, 50);
pointLight2.position.set(10, 5, 0);
this.scene.add(pointLight2);
this.lights = { ambient, directional, pointLight1, pointLight2 };
}
clearScene() {
// Remove all objects except lights and camera
for (const obj of this.objects) {
this.scene.remove(obj);
if (obj.geometry) obj.geometry.dispose();
if (obj.material) {
if (Array.isArray(obj.material)) {
obj.material.forEach(m => m.dispose());
} else {
obj.material.dispose();
}
}
}
this.objects = [];
}
createScene() {
this.clearScene();
switch (this.params.sceneType) {
case "cubes":
this.createCubesScene();
break;
case "particles":
this.createParticlesScene();
break;
case "terrain":
this.createTerrainScene();
break;
case "galaxy":
this.createGalaxyScene();
break;
case "ivy":
this.createIvyScene();
break;
case "torus":
this.createTorusScene();
break;
case "crystals":
this.createCrystalsScene();
break;
case "ocean":
this.createOceanScene();
break;
}
}
getColor(index, total) {
const t = index / total;
switch (this.params.palette) {
case "ivy":
return new THREE.Color().setHSL(0.35 + t * 0.1, 0.7, 0.4 + t * 0.2);
case "rainbow":
return new THREE.Color().setHSL(t, 0.8, 0.5);
case "neon":
const neonHues = [0.85, 0.15, 0.55, 0.75];
return new THREE.Color().setHSL(neonHues[index % 4], 1.0, 0.5);
case "fire":
return new THREE.Color().setHSL(0.05 + t * 0.08, 1.0, 0.4 + t * 0.2);
case "ocean":
return new THREE.Color().setHSL(0.55 + t * 0.1, 0.7, 0.4 + t * 0.3);
case "pastel":
return new THREE.Color().setHSL(t, 0.5, 0.75);
case "cosmic":
return new THREE.Color().setHSL(0.7 + t * 0.2, 0.8, 0.3 + t * 0.4);
case "monochrome":
return new THREE.Color().setHSL(0.7, 0.0, 0.3 + t * 0.5);
default:
return new THREE.Color().setHSL(t, 0.8, 0.5);
}
}
createMaterial(color) {
const baseProps = {
color: color,
wireframe: this.params.wireframe
};
switch (this.params.materialType) {
case "standard":
return new THREE.MeshStandardMaterial({
...baseProps,
metalness: 0.3,
roughness: 0.6
});
case "phong":
return new THREE.MeshPhongMaterial({
...baseProps,
shininess: 100,
specular: 0x444444
});
case "toon":
return new THREE.MeshToonMaterial(baseProps);
case "glass":
return new THREE.MeshPhysicalMaterial({
...baseProps,
metalness: 0.0,
roughness: 0.0,
transmission: 0.9,
transparent: true,
opacity: 0.8
});
case "metal":
return new THREE.MeshStandardMaterial({
...baseProps,
metalness: 1.0,
roughness: 0.2
});
case "emissive":
return new THREE.MeshStandardMaterial({
...baseProps,
metalness: 0.0,
roughness: 0.5,
emissive: color,
emissiveIntensity: 0.5
});
default:
return new THREE.MeshStandardMaterial(baseProps);
}
}
createCubesScene() {
const count = this.params.objectCount;
const scale = this.params.scale;
for (let i = 0; i < count; i++) {
const size = (Math.random() * 1 + 0.5) * scale;
const geometry = new THREE.BoxGeometry(size, size, size);
const material = this.createMaterial(this.getColor(i, count));
const cube = new THREE.Mesh(geometry, material);
// Random position in sphere
const radius = 8 + Math.random() * 8;
const theta = Math.random() * Math.PI * 2;
const phi = Math.random() * Math.PI;
cube.position.set(
radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi) - 2,
radius * Math.sin(phi) * Math.sin(theta)
);
cube.rotation.set(Math.random() * Math.PI, Math.random() * Math.PI, Math.random() * Math.PI);
// Store animation data
cube.userData = {
rotationSpeed: {
x: (Math.random() - 0.5) * 0.02,
y: (Math.random() - 0.5) * 0.02,
z: (Math.random() - 0.5) * 0.02
},
floatOffset: Math.random() * Math.PI * 2,
floatSpeed: Math.random() * 0.5 + 0.5,
originalY: cube.position.y
};
if (this.params.shadows) {
cube.castShadow = true;
cube.receiveShadow = true;
}
this.scene.add(cube);
this.objects.push(cube);
}
}
createParticlesScene() {
const count = this.params.objectCount * 100;
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(count * 3);
const colors = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
const i3 = i * 3;
// Spherical distribution
const radius = 5 + Math.random() * 10;
const theta = Math.random() * Math.PI * 2;
const phi = Math.random() * Math.PI;
positions[i3] = radius * Math.sin(phi) * Math.cos(theta);
positions[i3 + 1] = radius * Math.cos(phi);
positions[i3 + 2] = radius * Math.sin(phi) * Math.sin(theta);
const color = this.getColor(i, count);
colors[i3] = color.r;
colors[i3 + 1] = color.g;
colors[i3 + 2] = color.b;
}
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
const material = new THREE.PointsMaterial({
size: 0.1,
vertexColors: true,
transparent: true,
opacity: 0.8,
blending: THREE.AdditiveBlending
});
const particles = new THREE.Points(geometry, material);
particles.userData = { isParticles: true };
this.scene.add(particles);
this.objects.push(particles);
}
createTerrainScene() {
const size = 30;
const segments = this.params.objectCount;
const geometry = new THREE.PlaneGeometry(size, size, segments, segments);
const positions = geometry.attributes.position;
// Generate terrain heights using noise
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
// Simple noise-like height
let height = 0;
height += Math.sin(x * 0.5) * Math.cos(y * 0.5) * 2;
height += Math.sin(x * 0.2 + y * 0.3) * 1.5;
height += Math.random() * 0.2;
positions.setZ(i, height);
}
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: this.getColor(0, 1),
wireframe: this.params.wireframe,
side: THREE.DoubleSide,
flatShading: true
});
const terrain = new THREE.Mesh(geometry, material);
terrain.rotation.x = -Math.PI / 2;
terrain.position.y = -3;
terrain.userData = { isTerrain: true };
this.scene.add(terrain);
this.objects.push(terrain);
// Add water plane
const waterGeometry = new THREE.PlaneGeometry(size, size);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x1e90ff,
transparent: true,
opacity: 0.6,
metalness: 0.8,
roughness: 0.2
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.rotation.x = -Math.PI / 2;
water.position.y = -2;
water.userData = { isWater: true };
this.scene.add(water);
this.objects.push(water);
}
createGalaxyScene() {
const count = this.params.objectCount * 200;
const branches = 5;
const spin = 2;
const radius = 15;
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(count * 3);
const colors = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
const i3 = i * 3;
const r = Math.random() * radius;
const branchAngle = ((i % branches) / branches) * Math.PI * 2;
const spinAngle = r * spin;
// Add randomness
const randomX = (Math.random() - 0.5) * (radius - r) * 0.3;
const randomY = (Math.random() - 0.5) * (radius - r) * 0.1;
const randomZ = (Math.random() - 0.5) * (radius - r) * 0.3;
positions[i3] = Math.cos(branchAngle + spinAngle) * r + randomX;
positions[i3 + 1] = randomY;
positions[i3 + 2] = Math.sin(branchAngle + spinAngle) * r + randomZ;
// Color gradient from center (white) to edge (colored)
const mixRatio = r / radius;
const branchColor = this.getColor(i % branches, branches);
colors[i3] = 1 - mixRatio * (1 - branchColor.r);
colors[i3 + 1] = 1 - mixRatio * (1 - branchColor.g);
colors[i3 + 2] = 1 - mixRatio * (1 - branchColor.b);
}
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
const material = new THREE.PointsMaterial({
size: 0.05,
vertexColors: true,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const galaxy = new THREE.Points(geometry, material);
galaxy.userData = { isGalaxy: true };
this.scene.add(galaxy);
this.objects.push(galaxy);
// Center glow
const glowGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.8
});
const glow = new THREE.Mesh(glowGeometry, glowMaterial);
this.scene.add(glow);
this.objects.push(glow);
}
// 🌿 IVY SCENE - Organic vines in 3D!
createIvyScene() {
const ivyGreen = new THREE.Color(0x22c55e);
const darkGreen = new THREE.Color(0x166534);
const leafGreen = new THREE.Color(0x4ade80);
// Create multiple vine spirals
const numVines = Math.min(this.params.objectCount, 8);
for (let v = 0; v < numVines; v++) {
const vineAngle = (v / numVines) * Math.PI * 2;
const vineRadius = 3 + Math.random() * 2;
// Vine stem - spiral tube
const curve = new THREE.CatmullRomCurve3([]);
const segments = 50;
for (let i = 0; i < segments; i++) {
const t = i / segments;
const y = t * 15 - 7;
const spiralAngle = vineAngle + t * Math.PI * 4;
const r = vineRadius * (1 - t * 0.3);
curve.points.push(new THREE.Vector3(Math.cos(spiralAngle) * r, y, Math.sin(spiralAngle) * r));
}
const tubeGeometry = new THREE.TubeGeometry(curve, 64, 0.1, 8, false);
const tubeMaterial = new THREE.MeshStandardMaterial({
color: darkGreen,
roughness: 0.8,
metalness: 0.1
});
const vine = new THREE.Mesh(tubeGeometry, tubeMaterial);
vine.userData = { isVine: true, vineIndex: v };
this.scene.add(vine);
this.objects.push(vine);
// Add leaves along the vine
const numLeaves = 15 + Math.floor(Math.random() * 10);
for (let l = 0; l < numLeaves; l++) {
const t = (l / numLeaves) * 0.9 + 0.05;
const point = curve.getPoint(t);
// Leaf shape (heart-like)
const leafShape = new THREE.Shape();
const leafSize = 0.3 + Math.random() * 0.2;
leafShape.moveTo(0, 0);
leafShape.bezierCurveTo(
leafSize * 0.5,
leafSize * 0.3,
leafSize * 0.8,
leafSize * 0.8,
0,
leafSize * 1.2
);
leafShape.bezierCurveTo(-leafSize * 0.8, leafSize * 0.8, -leafSize * 0.5, leafSize * 0.3, 0, 0);
const leafGeometry = new THREE.ShapeGeometry(leafShape);
const leafMaterial = new THREE.MeshStandardMaterial({
color: leafGreen.clone().lerp(ivyGreen, Math.random()),
side: THREE.DoubleSide,
roughness: 0.6,
metalness: 0.0
});
const leaf = new THREE.Mesh(leafGeometry, leafMaterial);
leaf.position.copy(point);
// Random rotation
leaf.rotation.x = Math.random() * Math.PI;
leaf.rotation.y = Math.random() * Math.PI * 2;
leaf.rotation.z = Math.random() * 0.5;
leaf.userData = { isLeaf: true, initialRotation: leaf.rotation.clone() };
this.scene.add(leaf);
this.objects.push(leaf);
}
}
// Add some floating particles (pollen/sparkles)
const particleCount = 500;
const particleGeometry = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
positions[i * 3] = (Math.random() - 0.5) * 20;
positions[i * 3 + 1] = (Math.random() - 0.5) * 20;
positions[i * 3 + 2] = (Math.random() - 0.5) * 20;
}
particleGeometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
const particleMaterial = new THREE.PointsMaterial({
size: 0.05,
color: 0xffffff,
transparent: true,
opacity: 0.6,
blending: THREE.AdditiveBlending
});
const particles = new THREE.Points(particleGeometry, particleMaterial);
particles.userData = { isParticles: true };
this.scene.add(particles);
this.objects.push(particles);
}
start() {
this.isActive = true;
this.canvas.classList.remove("hidden");
// Wait for the canvas to be visible and have dimensions before resizing
// Double RAF ensures the browser has completed layout calculations
requestAnimationFrame(() => {
requestAnimationFrame(() => {
this.handleResize();
this.animate();
});
});
}
stop() {
this.isActive = false;
this.canvas.classList.add("hidden");
if (this.animationId) {
cancelAnimationFrame(this.animationId);
this.animationId = null;
}
}
reset() {
this.camera.position.set(0, 5, 15);
this.camera.lookAt(0, 0, 0);
this.controls.reset();
}
setSceneType(type) {
this.params.sceneType = type;
this.createScene();
}
setMaterialType(type) {
this.params.materialType = type;
this.createScene();
}
setPalette(palette) {
this.params.palette = palette;
this.createScene();
}
setObjectCount(count) {
this.params.objectCount = count;
this.createScene();
}
setSpeed(speed) {
this.params.speed = speed;
}
setScale(scale) {
this.params.scale = scale;
this.createScene();
}
setWireframe(enabled) {
this.params.wireframe = enabled;
for (const obj of this.objects) {
if (obj.material && !obj.userData.isParticles && !obj.userData.isGalaxy) {
obj.material.wireframe = enabled;
}
}
}
setAutoRotate(enabled) {
this.params.autoRotate = enabled;
if (this.controls) {
this.controls.autoRotate = enabled;
}
}
setShadows(enabled) {
this.params.shadows = enabled;
this.renderer.shadowMap.enabled = enabled;
if (this.lights && this.lights.directional) {
this.lights.directional.castShadow = enabled;
}
this.createScene();
}
setBloom(enabled) {
this.params.bloom = enabled;
// Note: Full bloom requires post-processing pass
// For now we enhance emissive materials
if (enabled) {
this.scene.background = new THREE.Color(0x050508);
} else {
this.scene.background = new THREE.Color(0x0a0a0f);
}
}
// === NEW SCENES ===
createTorusScene() {
const count = Math.floor(this.params.objectCount / 5);
const scale = this.params.scale;
for (let i = 0; i < count; i++) {
const radius = (1 + Math.random() * 0.5) * scale;
const tube = (0.3 + Math.random() * 0.2) * scale;
const geometry = new THREE.TorusKnotGeometry(radius, tube, 100, 16, 2 + (i % 5), 3 + (i % 7));
const material = this.createMaterial(this.getColor(i, count));
const torus = new THREE.Mesh(geometry, material);
const angle = (i / count) * Math.PI * 2;
const distance = 5 + Math.random() * 5;
torus.position.set(Math.cos(angle) * distance, (Math.random() - 0.5) * 6, Math.sin(angle) * distance);
torus.userData = {
rotationSpeed: {
x: (Math.random() - 0.5) * 0.01,
y: (Math.random() - 0.5) * 0.02,
z: (Math.random() - 0.5) * 0.01
}
};
this.scene.add(torus);
this.objects.push(torus);
}
}
createCrystalsScene() {
const count = this.params.objectCount;
const scale = this.params.scale;
for (let i = 0; i < count; i++) {
// Create crystal-like geometry (octahedron or icosahedron)
const geometryType =
Math.random() > 0.5
? new THREE.OctahedronGeometry((0.5 + Math.random()) * scale, 0)
: new THREE.IcosahedronGeometry((0.4 + Math.random() * 0.6) * scale, 0);
const material = this.createMaterial(this.getColor(i, count));
if (material.transparent === undefined) {
material.transparent = true;
material.opacity = 0.7 + Math.random() * 0.3;
}
const crystal = new THREE.Mesh(geometryType, material);
// Cluster formation
const cluster = Math.floor(i / 10);
const clusterAngle = cluster * 0.8;
const clusterRadius = 3 + cluster * 0.5;
crystal.position.set(
Math.cos(clusterAngle) * clusterRadius + (Math.random() - 0.5) * 3,
(Math.random() - 0.5) * 8 - 2,
Math.sin(clusterAngle) * clusterRadius + (Math.random() - 0.5) * 3
);
crystal.rotation.set(Math.random() * Math.PI, Math.random() * Math.PI, Math.random() * Math.PI);
crystal.userData = {
rotationSpeed: {
x: (Math.random() - 0.5) * 0.005,
y: (Math.random() - 0.5) * 0.01,
z: (Math.random() - 0.5) * 0.005
},
floatOffset: Math.random() * Math.PI * 2,
floatSpeed: Math.random() * 0.3 + 0.2,
originalY: crystal.position.y
};
this.scene.add(crystal);
this.objects.push(crystal);
}
}
createOceanScene() {
const scale = this.params.scale;
// Create water plane
const waterGeometry = new THREE.PlaneGeometry(40 * scale, 40 * scale, 128, 128);
const waterMaterial = new THREE.MeshPhongMaterial({
color: this.getColor(0, 1),
shininess: 100,
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.rotation.x = -Math.PI / 2;
water.position.y = -2;
water.userData = { isWater: true, geometry: waterGeometry };
this.scene.add(water);
this.objects.push(water);
// Add floating objects
const floatCount = Math.floor(this.params.objectCount / 3);
for (let i = 0; i < floatCount; i++) {
const size = (0.3 + Math.random() * 0.5) * scale;
const geometry = new THREE.SphereGeometry(size, 16, 16);
const material = this.createMaterial(this.getColor(i, floatCount));
const sphere = new THREE.Mesh(geometry, material);
sphere.position.set((Math.random() - 0.5) * 30, -1.5 + Math.random() * 0.5, (Math.random() - 0.5) * 30);
sphere.userData = {
floatOffset: Math.random() * Math.PI * 2,
floatSpeed: Math.random() * 0.5 + 0.3,
originalY: sphere.position.y,
waveX: sphere.position.x,
waveZ: sphere.position.z
};
this.scene.add(sphere);
this.objects.push(sphere);
}
}
handleResize() {
if (!this.canvas || !this.isActive) return;
// Get dimensions from parent container for proper sizing
const container = this.canvas.parentElement;
const width = container?.clientWidth || this.canvas.clientWidth || window.innerWidth;
const height = container?.clientHeight || this.canvas.clientHeight || window.innerHeight;
// Skip if dimensions are still zero
if (width === 0 || height === 0) return;
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
// Set renderer size (this sets canvas width/height attributes)
this.renderer.setSize(width, height, false);
// Force canvas to fill container via CSS (Three.js setSize overrides this)
this.canvas.style.width = "100%";
this.canvas.style.height = "100%";
}
animate() {
if (!this.isActive) return;
this.animationId = requestAnimationFrame(() => this.animate());
const delta = this.clock.getDelta();
const elapsed = this.clock.getElapsedTime();
const speed = this.params.speed;
// Update controls
this.controls.update();
// Animate objects
for (const obj of this.objects) {
if (obj.userData.rotationSpeed) {
obj.rotation.x += obj.userData.rotationSpeed.x * speed;
obj.rotation.y += obj.userData.rotationSpeed.y * speed;
obj.rotation.z += obj.userData.rotationSpeed.z * speed;
// Float animation
if (obj.userData.floatOffset !== undefined) {
obj.position.y =
obj.userData.originalY +
Math.sin(elapsed * obj.userData.floatSpeed * speed + obj.userData.floatOffset) * 0.5;
}
}
if (obj.userData.isParticles || obj.userData.isGalaxy) {
obj.rotation.y += 0.001 * speed;
}
if (obj.userData.isWater && obj.userData.geometry) {
// Animate water waves
const positions = obj.userData.geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const wave =
Math.sin(x * 0.5 + elapsed * speed) * 0.3 + Math.sin(y * 0.3 + elapsed * speed * 0.7) * 0.2;
positions.setZ(i, wave);
}
positions.needsUpdate = true;
obj.userData.geometry.computeVertexNormals();
}
// Floating objects on water
if (obj.userData.waveX !== undefined) {
const wx = obj.userData.waveX;
const wz = obj.userData.waveZ;
const wave =
Math.sin(wx * 0.5 + elapsed * speed) * 0.3 + Math.sin(wz * 0.3 + elapsed * speed * 0.7) * 0.2;
obj.position.y = obj.userData.originalY + wave;
obj.rotation.x = Math.sin(elapsed * speed + obj.userData.floatOffset) * 0.1;
obj.rotation.z = Math.cos(elapsed * speed * 0.7 + obj.userData.floatOffset) * 0.1;
}
// 🌿 Ivy leaf animation
if (obj.userData.isLeaf) {
const init = obj.userData.initialRotation;
obj.rotation.x = init.x + Math.sin(elapsed * 0.5 * speed + obj.position.y) * 0.1;
obj.rotation.y = init.y + Math.sin(elapsed * 0.3 * speed + obj.position.x) * 0.15;
obj.rotation.z = init.z + Math.cos(elapsed * 0.4 * speed) * 0.1;
}
}
// Animate point lights
if (this.lights) {
this.lights.pointLight1.position.x = Math.sin(elapsed * 0.5) * 10;
this.lights.pointLight2.position.x = Math.cos(elapsed * 0.5) * 10;
}
this.renderer.render(this.scene, this.camera);
}
dispose() {
this.stop();
this.clearScene();
if (this.renderer) {
this.renderer.dispose();
}
window.removeEventListener("resize", this.handleResize);
}
}
// Export
window.ThreeJSRenderer = ThreeJSRenderer;
|