Branchwell

PulseCanvas / website · pulsecanvas.ai / octagons.js

Raw 5.4 KB on 0170e6db89f8ad397e79a6aca0a88e5ed0b67bb1
1import * as THREE from 'three';
2import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
3import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
4import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';
5import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js';
6
7const BACKGROUND = 0x0f0b12;
8const TOP_COLOR = new THREE.Color('#d9c2ff');
9const MIDDLE_COLOR = new THREE.Color('#8a3cff');
10const BOTTOM_COLOR = new THREE.Color('#ff52d9');
11
12function octagonPath(path, radius) {
13 // Vertex at the top, matching the app icon.
14 for (let index = 0; index <= 8; index += 1) {
15 const angle = Math.PI / 2 + (index * Math.PI) / 4;
16 const x = Math.cos(angle) * radius;
17 const y = Math.sin(angle) * radius;
18 if (index === 0) path.moveTo(x, y);
19 else path.lineTo(x, y);
20 }
21 return path;
22}
23
24function createRingGeometry() {
25 const shape = octagonPath(new THREE.Shape(), 1);
26 shape.holes.push(octagonPath(new THREE.Path(), 0.76));
27 const geometry = new THREE.ExtrudeGeometry(shape, {
28 depth: 0.12,
29 bevelEnabled: true,
30 bevelThickness: 0.07,
31 bevelSize: 0.06,
32 bevelSegments: 1,
33 });
34 geometry.center();
35 // Faceted shading needs unshared vertices, then a vertical lilac-to-pink gradient.
36 const faceted = geometry.toNonIndexed();
37 faceted.computeVertexNormals();
38 const positions = faceted.attributes.position;
39 const colors = new Float32Array(positions.count * 3);
40 const color = new THREE.Color();
41 for (let index = 0; index < positions.count; index += 1) {
42 const t = (positions.getY(index) + 1.1) / 2.2;
43 if (t > 0.5) color.copy(MIDDLE_COLOR).lerp(TOP_COLOR, (t - 0.5) * 2);
44 else color.copy(BOTTOM_COLOR).lerp(MIDDLE_COLOR, t * 2);
45 color.toArray(colors, index * 3);
46 }
47 faceted.setAttribute('color', new THREE.BufferAttribute(colors, 3));
48 return faceted;
49}
50
51function createMaterial(emissiveIntensity) {
52 return new THREE.MeshStandardMaterial({
53 vertexColors: true,
54 flatShading: true,
55 metalness: 0.25,
56 roughness: 0.32,
57 emissive: new THREE.Color('#6d28d9'),
58 emissiveIntensity,
59 transparent: true,
60 });
61}
62
63export function mountOctagons(container, { animate }) {
64 const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
65 renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
66 renderer.setClearColor(BACKGROUND, 1);
67 container.append(renderer.domElement);
68
69 const scene = new THREE.Scene();
70 scene.background = new THREE.Color(BACKGROUND);
71 scene.fog = new THREE.Fog(BACKGROUND, 7, 26);
72 const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 60);
73 camera.position.set(0, 0, 8);
74
75 scene.add(new THREE.AmbientLight(0xc5b3df, 0.25));
76 const keyLight = new THREE.PointLight(0xf2b394, 22, 30);
77 keyLight.position.set(4, 4, 6);
78 const fillLight = new THREE.PointLight(0xff52d9, 18, 30);
79 fillLight.position.set(-5, -3, 4);
80 scene.add(keyLight, fillLight);
81
82 const geometry = createRingGeometry();
83 const heroRing = new THREE.Mesh(geometry, createMaterial(0.35));
84 heroRing.scale.setScalar(3.3);
85 heroRing.position.z = -0.6;
86 heroRing.material.transparent = false;
87 scene.add(heroRing);
88
89
90 const composer = new EffectComposer(renderer);
91 composer.addPass(new RenderPass(scene, camera));
92 const bloom = new UnrealBloomPass(new THREE.Vector2(1, 1), 0.4, 0.25, 0.45);
93 composer.addPass(bloom);
94 composer.addPass(new OutputPass());
95
96 const pointer = { x: 0, y: 0, targetX: 0, targetY: 0 };
97 function onPointerMove(event) {
98 pointer.targetX = (event.clientX / window.innerWidth) * 2 - 1;
99 pointer.targetY = (event.clientY / window.innerHeight) * 2 - 1;
100 }
101 window.addEventListener('pointermove', onPointerMove, { passive: true });
102
103 function resize() {
104 const width = container.clientWidth;
105 const height = container.clientHeight;
106 if (!width || !height) return;
107 camera.aspect = width / height;
108 // Keep the front ring framing the headline on narrow, tall screens.
109 camera.position.z = camera.aspect < 1 ? 8 / Math.max(camera.aspect, 0.5) : 8;
110 camera.updateProjectionMatrix();
111 renderer.setSize(width, height, false);
112 composer.setSize(width, height);
113 }
114 const resizeObserver = new ResizeObserver(resize);
115 resizeObserver.observe(container);
116 resize();
117
118 const clock = new THREE.Clock();
119 let running = false;
120 let frame = 0;
121 let elapsed = 4;
122
123 function render(elapsed) {
124 pointer.x += (pointer.targetX - pointer.x) * 0.04;
125 pointer.y += (pointer.targetY - pointer.y) * 0.04;
126 heroRing.rotation.x = Math.sin(elapsed * 0.35) * 0.22 + pointer.y * 0.12;
127 heroRing.rotation.y = Math.cos(elapsed * 0.28) * 0.3 + pointer.x * 0.18;
128 heroRing.rotation.z = elapsed * 0.06;
129 composer.render();
130 }
131
132 function loop() {
133 elapsed += Math.min(clock.getDelta(), 0.05);
134 render(elapsed);
135 frame = requestAnimationFrame(loop);
136 }
137
138 render(4);
139 return {
140 setRunning(next) {
141 const shouldRun = next && animate();
142 if (shouldRun === running) return;
143 running = shouldRun;
144 if (running) {
145 clock.getDelta();
146 loop();
147 } else {
148 cancelAnimationFrame(frame);
149 }
150 },
151 renderStill() {
152 render(elapsed);
153 },
154 };
155}
156
157export function supportsWebGL() {
158 try {
159 const canvas = document.createElement('canvas');
160 return Boolean(canvas.getContext('webgl2') || canvas.getContext('webgl'));
161 } catch {
162 return false;
163 }
164}