-
Notifications
You must be signed in to change notification settings - Fork 0
/
eternal.js
117 lines (97 loc) · 3.04 KB
/
eternal.js
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
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
let scene, camera, renderer, controls;
let fractalHexagon;
function init() {
// Set up scene, camera, renderer, and controls
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.z = 10;
controls = new OrbitControls(camera, renderer.domElement);
// Create and add fractal hexagon
fractalHexagon = createFractalHexagon(5, 5);
scene.add(fractalHexagon);
// Add some light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xffffff, 1);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
// Handle window resize
window.addEventListener("resize", onWindowResize, false);
// Start animation loop
animate();
}
function createFractalHexagon(size, depth) {
const group = new THREE.Group();
function createHexagonShape(radius) {
const shape = new THREE.Shape();
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.lineTo(radius, 0); // Close the shape
return shape;
}
function createLayer(radius, depth) {
if (depth === 0) return null;
const layerGroup = new THREE.Group();
// Create main hexagon
const shape = createHexagonShape(radius);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.2,
bevelEnabled: false,
});
const color = new THREE.Color().setHSL(0.6 - depth * 0.1, 1, 0.5);
const material = new THREE.MeshPhongMaterial({
color,
transparent: true,
opacity: 0.7,
});
const hexMesh = new THREE.Mesh(geometry, material);
layerGroup.add(hexMesh);
// Create child hexagons
if (depth > 1) {
const childRadius = radius * 0.38;
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const x = Math.cos(angle) * radius * 0.5;
const y = Math.sin(angle) * radius * 0.5;
const childLayer = createLayer(childRadius, depth - 1);
if (childLayer) {
childLayer.position.set(x, y, 0.2);
layerGroup.add(childLayer);
}
}
}
return layerGroup;
}
return createLayer(size, depth);
}
function animate() {
requestAnimationFrame(animate);
// Rotate the fractal hexagon
if (fractalHexagon) {
fractalHexagon.rotation.z += 0.005;
}
controls.update();
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Initialize the scene
init();