-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathambient.js
More file actions
274 lines (249 loc) · 8.87 KB
/
Copy pathambient.js
File metadata and controls
274 lines (249 loc) · 8.87 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
/* Ambient background for the wizard — Rubik's cubes drifting on
tracks. Muted palette so it never competes with the wizard chrome.
Three.js via CDN. Honors prefers-reduced-motion. */
(function () {
"use strict";
if (typeof THREE === "undefined") {
// Three failed to load (offline file:// on some setups). Fail silent.
// The wizard still works fine without the backdrop.
return;
}
const canvas = document.getElementById("ambient");
if (!canvas) return;
const reduceMotion = window.matchMedia &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// --- Renderer / scene / camera ---
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setClearColor(0x000000, 0);
const scene = new THREE.Scene();
// Thin atmospheric fog so far-away cubes fade into the paper.
scene.fog = new THREE.Fog(0xeef1f6, 22, 70);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
120
);
camera.position.set(0, 0, 14);
camera.lookAt(0, 0, 0);
// --- Lighting: soft, even, no harsh specular. ---
scene.add(new THREE.AmbientLight(0xffffff, 0.85));
const key = new THREE.DirectionalLight(0xffffff, 0.45);
key.position.set(4, 6, 8);
scene.add(key);
const fill = new THREE.DirectionalLight(0xc8d4ff, 0.25);
fill.position.set(-6, -2, 4);
scene.add(fill);
// --- Build a single Rubik's cube as a Group of 27 mini-cubies. ---
// Colors are intentionally desaturated so they never pop forward.
const CUBE_COLORS = {
U: 0xffffff, // top - white
D: 0xf3d96b, // bottom - muted yellow
F: 0x7fb77f, // front - muted green
B: 0x6c9ad6, // back - muted blue
R: 0xd98383, // right - muted red / salmon
L: 0xe5a86a // left - muted orange
};
const BLACK = 0x1a1b1e;
function makeCubie(x, y, z) {
const size = 0.92;
const gap = 0.04;
const geo = new THREE.BoxGeometry(size, size, size);
const materials = [
new THREE.MeshLambertMaterial({ color: x === 1 ? CUBE_COLORS.R : BLACK }),
new THREE.MeshLambertMaterial({ color: x === -1 ? CUBE_COLORS.L : BLACK }),
new THREE.MeshLambertMaterial({ color: y === 1 ? CUBE_COLORS.U : BLACK }),
new THREE.MeshLambertMaterial({ color: y === -1 ? CUBE_COLORS.D : BLACK }),
new THREE.MeshLambertMaterial({ color: z === 1 ? CUBE_COLORS.F : BLACK }),
new THREE.MeshLambertMaterial({ color: z === -1 ? CUBE_COLORS.B : BLACK })
];
const mesh = new THREE.Mesh(geo, materials);
mesh.position.set(x * (size + gap), y * (size + gap), z * (size + gap));
return mesh;
}
function makeRubiksCube() {
const group = new THREE.Group();
const cubies = [];
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
const cubie = makeCubie(x, y, z);
group.add(cubie);
cubies.push(cubie);
}
}
}
return { group, cubies };
}
// Face-turn metadata. Predicates run against cubies' current local
// positions inside the cube group, so they remain valid after past turns.
const FACE_INFO = {
U: { axis: "y", sign: +1, predicate: (p) => p.y > 0.4 },
D: { axis: "y", sign: -1, predicate: (p) => p.y < -0.4 },
R: { axis: "x", sign: +1, predicate: (p) => p.x > 0.4 },
L: { axis: "x", sign: -1, predicate: (p) => p.x < -0.4 },
F: { axis: "z", sign: +1, predicate: (p) => p.z > 0.4 },
B: { axis: "z", sign: -1, predicate: (p) => p.z < -0.4 },
};
const FACE_KEYS = ["U", "D", "R", "L", "F", "B"];
function startFaceTurn(c, t) {
const face = FACE_KEYS[(Math.random() * FACE_KEYS.length) | 0];
const dir = Math.random() < 0.5 ? +1 : -1;
const info = FACE_INFO[face];
const layer = new THREE.Group();
c.mesh.add(layer);
// layer is at identity transform, so just reparenting cubies preserves
// their visual position (they continue to render where they were).
const movers = c.cubies.filter((cu) => info.predicate(cu.position));
movers.forEach((cu) => layer.add(cu));
c.move = {
layer: layer,
cubies: movers,
axis: info.axis,
target: dir * info.sign * Math.PI / 2,
startT: t,
// 120–300ms per turn — snappy so cubes can twist near-continuously.
durMs: 120 + Math.random() * 180
};
}
function tickFaceTurn(c, t) {
if (c.move) {
const dt = (t - c.move.startT) * 1000;
const u = Math.min(1, dt / c.move.durMs);
// smoothstep
const eased = u * u * (3 - 2 * u);
c.move.layer.rotation[c.move.axis] = c.move.target * eased;
if (u >= 1) finishFaceTurn(c, t);
return;
}
if (t >= c.nextMoveAt) startFaceTurn(c, t);
}
function finishFaceTurn(c, t) {
const { layer, cubies } = c.move;
layer.updateMatrix();
const tmp = new THREE.Matrix4();
cubies.forEach((cu) => {
cu.updateMatrix();
tmp.multiplyMatrices(layer.matrix, cu.matrix);
tmp.decompose(cu.position, cu.quaternion, cu.scale);
// Reparent back to the cube group; cu's local transform now bakes
// in the layer's rotation, so visual position is unchanged.
c.mesh.add(cu);
});
c.mesh.remove(layer);
c.move = null;
// Idle window before the next move: 0–80ms. With 120-300ms turns,
// this gives ~3-4 turns/sec per cube — visibly twisting constantly.
c.nextMoveAt = t + Math.random() * 0.08;
}
// Tracks: lissajous-ish slow parametric curves. Different phases/scales
// per cube so they never visibly sync up.
function makeTrack(seed) {
const rnd = mulberry32(seed);
const ax = 7 + rnd() * 4;
const ay = 3 + rnd() * 2.5;
const az = 5 + rnd() * 3;
const px = rnd() * Math.PI * 2;
const py = rnd() * Math.PI * 2;
const pz = rnd() * Math.PI * 2;
const fx = 0.11 + rnd() * 0.08;
const fy = 0.07 + rnd() * 0.06;
const fz = 0.09 + rnd() * 0.07;
const zOffset = -6 - rnd() * 18;
const speed = 0.186 + rnd() * 0.166;
return function (t) {
const tt = t * speed;
return {
x: Math.sin(tt * fx * 6 + px) * ax,
y: Math.sin(tt * fy * 6 + py) * ay,
z: Math.sin(tt * fz * 6 + pz) * az * 0.4 + zOffset
};
};
}
// Deterministic small RNG so the scene feels stable across reloads.
function mulberry32(a) {
return function () {
a |= 0;
a = (a + 0x6D2B79F5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
// --- Populate scene with N cubes ---
const N_CUBES = 9;
const cubes = [];
for (let i = 0; i < N_CUBES; i++) {
const { group: mesh, cubies } = makeRubiksCube();
const rnd = mulberry32(1000 + i * 17);
// Wider spin range than v1; combined with the higher per-frame
// multiplier in tick(), cubes now visibly tumble.
const spin = new THREE.Vector3(
(rnd() - 0.5) * 0.9,
(rnd() - 0.5) * 0.9,
(rnd() - 0.5) * 0.7
);
mesh.rotation.set(rnd() * Math.PI, rnd() * Math.PI, rnd() * Math.PI);
const scale = 0.55 + rnd() * 0.55;
mesh.scale.setScalar(scale);
scene.add(mesh);
cubes.push({
mesh: mesh,
cubies: cubies,
track: makeTrack(42 + i * 31),
spin: spin,
scale: scale,
move: null,
// Tiny stagger on first move so the 9 cubes don't all turn in lockstep.
nextMoveAt: rnd() * 0.4
});
}
// Canvas-level opacity keeps the backdrop soft behind the wizard.
canvas.style.opacity = "0.22";
function onResize() {
const w = window.innerWidth;
const h = window.innerHeight;
renderer.setSize(w, h, false);
camera.aspect = w / h;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", onResize);
onResize();
const start = performance.now();
function tick() {
const now = performance.now();
const t = (now - start) / 1000;
for (let i = 0; i < cubes.length; i++) {
const c = cubes[i];
const p = c.track(t);
c.mesh.position.set(p.x, p.y, p.z);
if (!reduceMotion) {
// tumbleMult 0.015 — very slight drift-rotation; slice turns dominate.
c.mesh.rotation.x += c.spin.x * 0.015;
c.mesh.rotation.y += c.spin.y * 0.015;
c.mesh.rotation.z += c.spin.z * 0.015;
// Rubik's-style face turns layered on top of the tumble.
tickFaceTurn(c, t);
}
}
renderer.render(scene, camera);
requestAnimationFrame(tick);
}
if (reduceMotion) {
for (let i = 0; i < cubes.length; i++) {
const c = cubes[i];
const p = c.track(0);
c.mesh.position.set(p.x, p.y, p.z);
}
renderer.render(scene, camera);
canvas.style.opacity = "0.16";
} else {
requestAnimationFrame(tick);
}
})();