Repository navigation
Expand file tree
/
Copy pathengine.js
More file actions
125 lines (109 loc) · 4.8 KB
/
Copy pathengine.js
File metadata and controls
125 lines (109 loc) · 4.8 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
/**
* engine.js — Three.js renderer, scene, camera, material, and shared constants.
* No animate loop — that lives in index.html where all modules are wired together.
*/
import * as THREE from 'three';
/* ── Constants ─────────────────────────────────────────────── */
export const FFT_SIZE = 2048;
export const NUM_BINS = FFT_SIZE / 2;
export const GRID_W = 10;
export const GRID_D = 10;
export const LINE_COLOR = 0xe8d5b0; // warm gold (base)
export const LINE_COLOR2 = 0xb090c8; // cool mauve (peak mids)
export const BG_COLOR = 0x0d0d0d;
export const CAM_BASE = { y: 5.5, z: 14.5 };
/* ── Renderer ──────────────────────────────────────────────── */
export const renderer = typeof document !== 'undefined'
? new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true })
: null;
if (renderer) {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(BG_COLOR, 1);
document.body.appendChild(renderer.domElement);
}
/* ── Scene & Camera ────────────────────────────────────────── */
export const scene = new THREE.Scene();
export const camera = new THREE.PerspectiveCamera(
45,
typeof window !== 'undefined' ? window.innerWidth / window.innerHeight : 16 / 9,
0.1,
100
);
camera.position.set(0, CAM_BASE.y, CAM_BASE.z);
camera.lookAt(0, -0.5, 0);
/* ── Scene Lighting ────────────────────────────────────────── */
export const ambientLight = new THREE.AmbientLight(0xffffff, 1.5);
ambientLight.name = 'GlobalAmbientLight';
scene.add(ambientLight);
export const dirLight = new THREE.DirectionalLight(0xfff8ee, 1.8);
dirLight.name = 'GlobalDirLight';
dirLight.position.set(5, 9, 6);
scene.add(dirLight);
export const fillLight = new THREE.DirectionalLight(0x88bbff, 0.9);
fillLight.name = 'GlobalFillLight';
fillLight.position.set(-5, 4, -4);
scene.add(fillLight);
/* ── Shared material + colour scratch ──────────────────────── */
export const material = new THREE.LineBasicMaterial({ vertexColors: true });
export const _colA = new THREE.Color(LINE_COLOR);
export const _colB = new THREE.Color(LINE_COLOR2);
export const _colScratch = new THREE.Color();
export function setColors(hexA, hexB) {
_colA.set(hexA);
_colB.set(hexB);
material.color.copy(_colA);
}
export function setTheme(isLight) {
const bg = isLight ? 0xf4f5f7 : 0x0d0d0d;
if (renderer) renderer.setClearColor(bg, 1);
if (scene.background && scene.background.isColor) {
scene.background.set(bg);
}
}
/* ── Fade veil ─────────────────────────────────────────────── */
const fadeVeil = typeof document !== 'undefined' ? document.getElementById('fadeVeil') : null;
export function fadeOut() {
return new Promise(resolve => {
if (!fadeVeil) return resolve();
fadeVeil.classList.add('fading');
fadeVeil.addEventListener('transitionend', () => resolve(), { once: true });
});
}
export function fadeIn() {
if (fadeVeil) fadeVeil.classList.remove('fading');
}
/* ── Export resize helpers ─────────────────────────────────── */
let savedDims = null;
export function beginExportResize(width, height) {
savedDims = { w: window.innerWidth, h: window.innerHeight };
renderer.setPixelRatio(1);
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.domElement.style.position = 'fixed';
renderer.domElement.style.left = '-10000px';
}
// Keep the viewport matched to the window. During an export the canvas is
// pinned to the export size, so only remember the new window size for restore.
if (renderer) {
window.addEventListener('resize', () => {
if (savedDims) {
savedDims = { w: window.innerWidth, h: window.innerHeight };
return;
}
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
}
export function endExportResize() {
if (!savedDims) return;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(savedDims.w, savedDims.h);
camera.aspect = savedDims.w / savedDims.h;
camera.updateProjectionMatrix();
renderer.domElement.style.position = '';
renderer.domElement.style.left = '';
savedDims = null;
}