Loading…
← All components

Isolated · Chrome

Chrome Formation

Thirty chrome cubes assembling into formations, ready to be driven by scroll.

<ChromePyramidObject />Live

Customize

Color#f0f3f8

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add chrome-pyramid
01

Install

The 3D packages this component is built on.

$ npm install three @react-three/fiber @react-three/drei three-stdlib
02

Add the component

One self-contained file: create components/ChromePyramidObject.js and paste it in.

components/ChromePyramidObject.js
1"use client";
2
3import { Suspense, useEffect, useMemo, useRef, useState } from "react";
4import * as THREE from "three";
5import { Canvas, useFrame } from "@react-three/fiber";
6import { Environment } from "@react-three/drei";
7import { RoundedBoxGeometry } from "three-stdlib";
8
9/**
10 * Reusable <Canvas> shell for every scene in /components/3d.
11 *
12 * - Pauses the render loop (frameloop="never") while the canvas is
13 * scrolled out of view, so idle scenes stop burning GPU/battery.
14 * - Caps devicePixelRatio to [1, 2] to avoid over-rendering on retina/4K.
15 * - WebGL resources (geometries, materials, the GL context itself) are
16 * disposed automatically by R3F when the Canvas unmounts.
17 */
18function Canvas3DWrapper({
19 children,
20 className = "",
21 camera = { position: [0, 0, 5], fov: 45 },
22 dpr = [1, 2],
23 fallback = null,
24 ...canvasProps
25}) {
26 const containerRef = useRef(null);
27 // Start "in view" so every scene renders at least one real frame before
28 // a canvas below the fold can be paused — some WebGL backends show an
29 // opaque black canvas if frameloop="never" is set before anything has
30 // ever been drawn, instead of the expected transparent clear color.
31 const [inView, setInView] = useState(true);
32
33 useEffect(() => {
34 const el = containerRef.current;
35 if (!el) return;
36 const observer = new IntersectionObserver(
37 ([entry]) => setInView(entry.isIntersecting),
38 { threshold: 0.1 }
39 );
40 observer.observe(el);
41 return () => observer.disconnect();
42 }, []);
43
44 return (
45 <div ref={containerRef} className={className}>
46 <Canvas
47 dpr={dpr}
48 camera={camera}
49 frameloop={inView ? "always" : "never"}
50 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}
51 onCreated={({ gl }) => gl.setClearColor(0x000000, 0)}
52 {...canvasProps}
53 >
54 <Suspense fallback={fallback}>{children}</Suspense>
55 </Canvas>
56 </div>
57 );
58}
59
60export const FORMATION_COUNT = 4;
61
62function useWindowPointerParallax(targetRef, intensity = 0.3, damping = 0.06) {
63 const pointerTarget = useRef({ x: 0, y: 0 });
64 const current = useRef({ x: 0, y: 0 });
65
66 useEffect(() => {
67 const handleMove = event => {
68 pointerTarget.current.x = (event.clientX / window.innerWidth) * 2 - 1;
69 pointerTarget.current.y = -((event.clientY / window.innerHeight) * 2 - 1);
70 };
71 window.addEventListener("pointermove", handleMove, { passive: true });
72 return () => window.removeEventListener("pointermove", handleMove);
73 }, []);
74
75 useFrame(() => {
76 current.current.x += (pointerTarget.current.x - current.current.x) * damping;
77 current.current.y += (pointerTarget.current.y - current.current.y) * damping;
78
79 const obj = targetRef.current;
80 if (!obj) return;
81 obj.rotation.y = current.current.x * intensity;
82 obj.rotation.x = -current.current.y * intensity * 0.6;
83 });
84}
85
86const CUBE_SIZE = 0.46;
87const SPACING = CUBE_SIZE * 1.18;
88
89function pyramidFormation() {
90 const layers = [4, 3, 2, 1];
91 const positions = [];
92 let y = -SPACING * 1.1;
93 for (const side of layers) {
94 const offset = ((side - 1) * SPACING) / 2;
95 for (let i = 0; i < side; i++) {
96 for (let j = 0; j < side; j++) {
97 positions.push(new THREE.Vector3(i * SPACING - offset, y, j * SPACING - offset));
98 }
99 }
100 y += SPACING;
101 }
102 return positions;
103}
104
105function sphereFormation(count) {
106 const golden = Math.PI * (3 - Math.sqrt(5));
107 const radius = SPACING * 2.6;
108 const positions = [];
109 for (let i = 0; i < count; i++) {
110 const yFrac = 1 - (i / (count - 1)) * 2;
111 const r = Math.sqrt(1 - yFrac * yFrac);
112 const theta = golden * i;
113 positions.push(new THREE.Vector3(Math.cos(theta) * r * radius, yFrac * radius, Math.sin(theta) * r * radius));
114 }
115 return positions;
116}
117
118function ringFormation(count) {
119 const radius = SPACING * 2.4;
120 const positions = [];
121 for (let i = 0; i < count; i++) {
122 const a = (i / count) * Math.PI * 2;
123 const wobble = Math.sin(a * 3) * 0.18;
124 positions.push(
125 new THREE.Vector3(Math.cos(a) * (radius + wobble * radius), Math.sin(a * 2) * SPACING * 0.6, Math.sin(a) * (radius + wobble * radius))
126 );
127 }
128 return positions;
129}
130
131function gridFormation(count) {
132 const cols = 6;
133 const rows = Math.ceil(count / cols);
134 const positions = [];
135 for (let i = 0; i < count; i++) {
136 const c = i % cols;
137 const r = Math.floor(i / cols);
138 positions.push(new THREE.Vector3((c - (cols - 1) / 2) * SPACING, (r - (rows - 1) / 2) * SPACING, 0));
139 }
140 return positions;
141}
142
143/**
144 * Génère une carte d'environnement HDRI haute dynamique enrichie
145 * avec softboxes de studio et gradients de rim-lighting contrastés.
146 */
147function createStudioEnvironmentTexture() {
148 const canvas = document.createElement("canvas");
149 canvas.width = 2048;
150 canvas.height = 1024;
151 const ctx = canvas.getContext("2d");
152
153 // Fond studio sombre à très haut contraste
154 const sky = ctx.createLinearGradient(0, 0, 0, canvas.height);
155 sky.addColorStop(0, "#090a0f");
156 sky.addColorStop(0.45, "#181a20");
157 sky.addColorStop(0.5, "#ffffff"); // Horizon blanc très vif pour la ligne de réflexion
158 sky.addColorStop(0.55, "#181a20");
159 sky.addColorStop(1, "#090a0f");
160 ctx.fillStyle = sky;
161 ctx.fillRect(0, 0, canvas.width, canvas.height);
162
163 // Panneaux lumineux (Softboxes) ultra-brillants
164 const softboxes = [
165 { x: 220, width: 140, color: "255,255,255", alpha: 1.0 },
166 { x: 500, width: 90, color: "180,220,255", alpha: 0.8 },
167 { x: 780, width: 220, color: "255,255,255", alpha: 0.95 },
168 { x: 1150, width: 70, color: "10,12,18", alpha: 1.0 }, // Cache noir net
169 { x: 1380, width: 160, color: "255,255,255", alpha: 1.0 },
170 { x: 1720, width: 110, color: "255,220,200", alpha: 0.85 },
171 { x: 1940, width: 90, color: "255,255,255", alpha: 0.9 },
172 ];
173
174 for (const box of softboxes) {
175 const grad = ctx.createLinearGradient(box.x - box.width, 0, box.x + box.width, 0);
176 grad.addColorStop(0, `rgba(${box.color},0)`);
177 grad.addColorStop(0.5, `rgba(${box.color},${box.alpha})`);
178 grad.addColorStop(1, `rgba(${box.color},0)`);
179 ctx.fillStyle = grad;
180 ctx.fillRect(box.x - box.width, 0, box.width * 2, canvas.height);
181 }
182
183 // Reflet rose / néon cyan en dégradé périphérique pour la richesse chromatique
184 const accentGradient = ctx.createLinearGradient(0, canvas.height * 0.6, 0, canvas.height);
185 accentGradient.addColorStop(0, "rgba(253,164,175,0)");
186 accentGradient.addColorStop(0.6, "rgba(244,114,182,0.25)");
187 accentGradient.addColorStop(1, "rgba(168,85,247,0.4)");
188 ctx.fillStyle = accentGradient;
189 ctx.fillRect(0, canvas.height * 0.6, canvas.width, canvas.height * 0.4);
190
191 const texture = new THREE.CanvasTexture(canvas);
192 texture.mapping = THREE.EquirectangularReflectionMapping;
193 texture.colorSpace = THREE.SRGBColorSpace;
194 return texture;
195}
196
197function Cubes({ targetFormation, targetFormationRef, parallaxIntensity, cubeColor, roughness, envMapIntensity, zoom }) {
198 const groupRef = useRef(null);
199 const meshRef = useRef(null);
200 const floatFormation = useRef(0);
201 useWindowPointerParallax(groupRef, parallaxIntensity);
202
203 const count = 30;
204 const formations = useMemo(
205 () => [pyramidFormation(), sphereFormation(count), ringFormation(count), gridFormation(count)],
206 []
207 );
208
209 const cubes = useMemo(
210 () =>
211 Array.from({ length: count }, () => ({
212 delay: Math.random() * 0.35,
213 spinAxis: new THREE.Vector3(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5).normalize(),
214 spinAmount: (Math.random() - 0.5) * 6,
215 floatSpeed: 0.8 + Math.random() * 1.2,
216 floatAmpY: 0.04 + Math.random() * 0.05,
217 floatAmpX: 0.02 + Math.random() * 0.03,
218 floatRotSpeed: 0.3 + Math.random() * 0.5,
219 phase: Math.random() * Math.PI * 2,
220 })),
221 [count]
222 );
223
224 const dummy = useMemo(() => new THREE.Object3D(), []);
225 const tempEuler = useMemo(() => new THREE.Euler(), []);
226
227 useFrame((state, delta) => {
228 if (groupRef.current) groupRef.current.rotation.y += delta * 0.12;
229
230 const wanted = targetFormationRef ? targetFormationRef.current : targetFormation;
231 const diff = wanted - floatFormation.current;
232 floatFormation.current += diff * Math.min(1, delta * 2.6);
233
234 const stage = Math.floor(floatFormation.current);
235 const stageIndex = ((stage % FORMATION_COUNT) + FORMATION_COUNT) % FORMATION_COUNT;
236 const nextIndex = (stageIndex + 1) % FORMATION_COUNT;
237 const blend = floatFormation.current - stage;
238
239 const from = formations[stageIndex];
240 const to = formations[nextIndex];
241 const mesh = meshRef.current;
242 if (!mesh) return;
243
244 const t = state.clock.elapsedTime;
245 for (let i = 0; i < count; i++) {
246 const cube = cubes[i];
247 const local = THREE.MathUtils.clamp((blend - cube.delay) / (1 - cube.delay), 0, 1);
248 const eased = local < 0.5 ? 4 * local ** 3 : 1 - (-2 * local + 2) ** 3 / 2;
249
250 dummy.position.lerpVectors(from[i], to[i], eased);
251
252 const floatTime = t * cube.floatSpeed + cube.phase;
253 dummy.position.y += Math.sin(floatTime) * cube.floatAmpY;
254 dummy.position.x += Math.cos(floatTime * 0.7) * cube.floatAmpX;
255 dummy.position.z += Math.sin(floatTime * 0.5) * cube.floatAmpX;
256
257 const rotX = Math.sin(floatTime * cube.floatRotSpeed) * 0.08;
258 const rotY = Math.cos(floatTime * cube.floatRotSpeed * 0.8) * 0.08;
259 const rotZ = Math.sin(floatTime * cube.floatRotSpeed * 1.2) * 0.05;
260 tempEuler.set(rotX, rotY, rotZ);
261 dummy.quaternion.setFromEuler(tempEuler);
262
263 const flight = Math.sin(Math.PI * local);
264 if (flight > 0.001) {
265 const flightQuat = new THREE.Quaternion().setFromAxisAngle(cube.spinAxis, cube.spinAmount * flight);
266 dummy.quaternion.multiply(flightQuat);
267 }
268
269 const wobble = flight * 0.45;
270 dummy.scale.set(
271 1 + wobble * Math.sin(t * 9 + i * 1.7),
272 1 + wobble * Math.sin(t * 10 + i * 2.3 + 2.1),
273 1 + wobble * Math.sin(t * 8 + i * 1.1 + 4.2)
274 );
275 dummy.updateMatrix();
276 mesh.setMatrixAt(i, dummy.matrix);
277 }
278 mesh.instanceMatrix.needsUpdate = true;
279 });
280
281 // Augmentation du rayon du biseau (0.06) et des segments (6) pour capturer des highlights plus doux et réalistes
282 const geometry = useMemo(() => new RoundedBoxGeometry(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, 6, 0.06), []);
283 useEffect(() => () => geometry.dispose(), [geometry]);
284
285 return (
286 <group ref={groupRef} scale={zoom}>
287 <instancedMesh ref={meshRef} args={[geometry, undefined, count]} frustumCulled={false}>
288 {/* Matériau Chrome Physiquement Réaliste d'Ultra-Haute Qualité */}
289 <meshPhysicalMaterial
290 color={cubeColor}
291 metalness={1.0}
292 roughness={roughness} // Surface miroir quasi-parfaite
293 clearcoat={1.0}
294 clearcoatRoughness={0.008}
295 envMapIntensity={envMapIntensity} // Puissance d'intensité de la carte studio
296
297 // Subtile iridescence prismatique sur les arrêtes de réflexion
298 iridescence={0.3}
299 iridescenceIOR={1.6}
300 iridescenceThicknessRange={[100, 400]}
301
302 // Légère anisotropie pour imiter un micro-polissage usiné
303 anisotropy={0.15}
304 anisotropyRotation={Math.PI / 4}
305 />
306 </instancedMesh>
307 </group>
308 );
309}
310
311/**
312 * Every visual knob is a prop — formation, cube color, roughness/reflection
313 * intensity, zoom and the cursor-tilt strength — so a demo page can drive
314 * it live from a Leva panel. Defaults match the tuned studio look above.
315 */
316export default function ChromePyramidObject({
317 targetFormation = 0,
318 targetFormationRef,
319 parallaxIntensity = 0.25,
320 cubeColor = "#f0f3f8",
321 roughness = 0.005,
322 envMapIntensity = 4.0,
323 zoom = 1,
324 className = "",
325}) {
326 const envMap = useMemo(() => createStudioEnvironmentTexture(), []);
327 useEffect(() => () => envMap.dispose(), [envMap]);
328
329 return (
330 <Canvas3DWrapper className={className} camera={{ position: [1.2, 1.6, 6.8], fov: 36 }}>
331 <Environment map={envMap} background={false} />
332
333 {/* Configuration Studio multi-lumières pour maximiser les éclats spéculaires */}
334 <ambientLight intensity={0.55} />
335 <directionalLight position={[2, 4, 6]} intensity={3.5} color="#ffffff" />
336 <directionalLight position={[-4, -2, -3]} intensity={1.8} color="#fda4af" />
337 <directionalLight position={[-3, 5, 2]} intensity={2.2} color="#e0f2fe" />
338 <pointLight position={[0, -2, 3]} intensity={1.5} color="#ffffff" />
339
340 <Cubes
341 targetFormation={targetFormation}
342 targetFormationRef={targetFormationRef}
343 parallaxIntensity={parallaxIntensity}
344 cubeColor={cubeColor}
345 roughness={roughness}
346 envMapIntensity={envMapIntensity}
347 zoom={zoom}
348 />
349 </Canvas3DWrapper>
350 );
351}
03

Drop it in a page

Updated live with the settings above.

app/page.jsx
1import ChromePyramidObject from "@/components/ChromePyramidObject";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <ChromePyramidObject
8 targetFormation={0}
9 roughness={0.005}
10 envMapIntensity={4}
11 zoom={1}
12 parallaxIntensity={0.25}
13 cubeColor="#f0f3f8"
14 />
15 </div>
16 );
17}
Next componentChrome Rings→