Loading…
← All components

Extras · Divider

Terrain Divider

A rolling terrain between two sections that starts in the color above and lands exactly on the color below.

Section above

Section below

<TerrainDivider3D />Live

Customize

Above#000000
Below#120e1f
Grid#8b5cf6

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add terrain-divider
01

Install

The 3D packages this component is built on.

$ npm install three @react-three/fiber && npm install -D @types/three
02

Add the component

One self-contained file: create components/TerrainDivider3D.tsx and paste it in.

components/TerrainDivider3D.tsx
1"use client";
2
3import { memo, useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from "react";
4import * as THREE from "three";
5import { Canvas, useFrame, type CanvasProps } from "@react-three/fiber";
6
7/**
8 * The shared props API of DeepUI's individual 3D components (not the full-page templates, which keep
9 * their own tailored scenes).
10 *
11 * The props are grouped by concern. A component extends ONLY the groups it really applies, so its
12 * type never advertises a knob that does nothing (a card has no particles, a background has no tilt):
13 *
14 * interface MyCardProps extends CursorProps, TiltProps, MaterialProps, PerformanceProps { … }
15 *
16 * `Base3DComponentProps` is every group at once, for tooling that handles any component. Same name,
17 * same unit and same meaning everywhere; the default values live in DEFAULT_3D_PROPS. These files are
18 * inlined into each component's single-file bundle (lib/bundle-source.mjs), only what it uses.
19 */
20
21/** Which rotation axes follow the pointer. "x-only": the object nods (up / down); "y-only": it turns (left / right). */
22export type TiltAxis = "both" | "x-only" | "y-only" | "none";
23
24/** 1. Cursor: how the pointer pulls, pushes and drags the object. */
25interface CursorProps {
26 /** Reach of the pointer's influence, in the component's own space: CSS pixels for pixel-laid-out widgets, world units for 3D scenes (each component says which). Default 220 (px). */
27 cursorRadius?: number;
28 /** Positive pulls things towards the pointer, negative pushes them away (repulsion, shockwave). Default 1. */
29 cursorForce?: number;
30 /** How far the object slides towards the pointer, 0 (stays put) to 1 (follows closely). Default 0. */
31 magneticStrength?: number;
32 /** Share of the remaining distance the motion closes each frame (at 60 fps), 0.01 (heavy, slow) to 1 (instant). Default 0.1. */
33 cursorDamping?: number;
34}
35
36/** 2. Tilt and parallax: the object leans towards the pointer, its layers slide against each other. */
37interface TiltProps {
38 /** Largest lean towards the pointer, in degrees. Default 15. */
39 maxTiltAngle?: number;
40 /** Axes that follow the pointer. Default "both". */
41 tiltAxis?: TiltAxis;
42 /** Lean away from the pointer instead of towards it. Default false. */
43 invertTilt?: boolean;
44 /** How far apart the depth layers slide when the object tilts, 0 (flat) to 2. Default 1. */
45 parallaxOffset?: number;
46}
47
48/** 3. Text and 3D typography, for components that draw a label. */
49interface Text3DProps {
50 /** The label drawn by the component. */
51 text?: string;
52 /** Scale of the lettering. Default 1. */
53 fontSize?: number;
54 /** Extrusion depth of 3D letters. Default 0.2. */
55 textDepth?: number;
56 /** Bevelled, light-catching edges on 3D letters. Default true. */
57 bevelEnabled?: boolean;
58 /** Custom font: a .woff / .ttf (flat text) or a typeface .json (extruded text). */
59 fontUrl?: string;
60}
61
62/** 4. Rendering, materials and glow. */
63interface MaterialProps {
64 /** Main color of the object. Default "#6366f1". */
65 primaryColor?: string;
66 /** Color of the glow. Default "#00f0ff". */
67 emissiveColor?: string;
68 /** Strength of the glow, 0 (none) to 3. Default 1. */
69 emissiveIntensity?: number;
70 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
71 roughness?: number;
72 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
73 metalness?: number;
74 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
75 transmission?: number;
76 /** Technical wireframe rendering. Default false. */
77 wireframe?: boolean;
78}
79
80/** 5. Particles, idle motion and shader animation. */
81interface MotionProps {
82 /** Number of instanced elements. Default depends on the component. */
83 particleCount?: number;
84 /** Spin on its own, in radians per second (negative: the other way; 0.5 ≈ one turn in 12 s). Default 0. */
85 autoRotateSpeed?: number;
86 /** Pulses or ripples per second. Default 1. */
87 pulseRate?: number;
88 /** Amplitude of the noise deformation, 0 (none) to 2. Default 1. */
89 displacementScale?: number;
90}
91
92/** 6. Environment and camera. */
93interface EnvironmentProps {
94 /** Camera field of view, in degrees. Default depends on the component. */
95 cameraFov?: number;
96 /** Color of the depth fog. Default "#0a0a0a". */
97 fogColor?: string;
98 /** Thickness of the fog, 0 (none) to 0.2. Default 0. */
99 fogDensity?: number;
100}
101
102/** 7. Performance and accessibility. */
103interface PerformanceProps {
104 /** Lighter rendering for weak devices: pixel ratio 1, fewer instances, smaller environment maps. Default false. */
105 lowPowerMode?: boolean;
106 /** Under the OS "reduce motion" setting, stop idle spin, pulses and deformation (pointer feedback stays). Default true. */
107 respectReducedMotion?: boolean;
108 /** Stop the render loop while the component is scrolled out of view. Default true. */
109 pauseWhenOffscreen?: boolean;
110}
111
112/** Every group at once. Components extend only the groups they apply (see the top of this file). */
113interface Base3DComponentProps
114 extends CursorProps,
115 TiltProps,
116 Text3DProps,
117 MaterialProps,
118 MotionProps,
119 EnvironmentProps,
120 PerformanceProps {}
121
122type Defaults = Required<Omit<Base3DComponentProps, "text" | "fontUrl" | "particleCount" | "cameraFov">>;
123
124/** The values a prop takes when it is left out. A component may override one, and then documents it. */
125const DEFAULT_3D_PROPS: Defaults = {
126 cursorRadius: 220,
127 cursorForce: 1,
128 magneticStrength: 0,
129 cursorDamping: 0.1,
130 maxTiltAngle: 15,
131 tiltAxis: "both",
132 invertTilt: false,
133 parallaxOffset: 1,
134 fontSize: 1,
135 textDepth: 0.2,
136 bevelEnabled: true,
137 primaryColor: "#6366f1",
138 emissiveColor: "#00f0ff",
139 emissiveIntensity: 1,
140 roughness: 0.3,
141 metalness: 0.5,
142 transmission: 0,
143 wireframe: false,
144 autoRotateSpeed: 0,
145 pulseRate: 1,
146 displacementScale: 1,
147 fogColor: "#0a0a0a",
148 fogDensity: 0,
149 lowPowerMode: false,
150 respectReducedMotion: true,
151 pauseWhenOffscreen: true,
152};
153
154/** Inline widget: fills its positioned parent and receives pointer events. */
155const FILL_STYLE: CSSProperties = { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" };
156
157/** Cursor overlay: covers the viewport, above everything, never intercepts the page's events. */
158const OVERLAY_STYLE: CSSProperties = {
159 position: "fixed",
160 top: 0,
161 left: 0,
162 width: "100%",
163 height: "100%",
164 pointerEvents: "none",
165 zIndex: 9999,
166};
167
168interface UICanvasProps extends Omit<CanvasProps, "children" | "style" | "gl">, Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {
169 children: ReactNode;
170 /** Full-viewport, click-through overlay (cursors) instead of an inline widget. */
171 overlay?: boolean;
172}
173
174/**
175 * Ready-to-use <Canvas>: pauses its render loop while scrolled out of view (pauseWhenOffscreen), pixel
176 * ratio 1 in lowPowerMode; R3F disposes the renderer and its WebGL context on unmount.
177 */
178function UICanvas({ children, overlay = false, dpr = [1, 2], pauseWhenOffscreen = DEFAULT_3D_PROPS.pauseWhenOffscreen, lowPowerMode = DEFAULT_3D_PROPS.lowPowerMode, ...canvasProps }: UICanvasProps) {
179 const containerRef = useRef<HTMLDivElement>(null);
180 // In view at first: the first frame is always drawn before the loop can be paused
181 const [inView, setInView] = useState(true);
182 useEffect(() => {
183 const el = containerRef.current;
184 if (!el || !pauseWhenOffscreen) return undefined;
185 const observer = new IntersectionObserver(([entry]) => setInView(entry.isIntersecting));
186 observer.observe(el);
187 return () => observer.disconnect();
188 }, [pauseWhenOffscreen]);
189 return (
190 <div ref={containerRef} style={overlay ? OVERLAY_STYLE : FILL_STYLE} aria-hidden="true">
191 <Canvas
192 dpr={lowPowerMode ? 1 : dpr}
193 frameloop={inView || !pauseWhenOffscreen ? "always" : "never"}
194 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}
195 style={overlay ? { pointerEvents: "none" } : undefined}
196 {...canvasProps}
197 >
198 {children}
199 </Canvas>
200 </div>
201 );
202}
203
204export interface TerrainDividerProps extends Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {
205 /** Background of the section above: the sky the terrain fades into. */
206 topColor?: string;
207 /** Background of the section below: the terrain's color, exact at the bottom edge. */
208 bottomColor?: string;
209 /** Density of the waves. Default 1. */
210 waveFrequency?: number;
211 /** Height of the waves. Default 1. */
212 waveAmplitude?: number;
213 speed?: number;
214 /** Color of the faint grid drawn on the terrain. */
215 lineColor?: string;
216}
217
218const WIDTH = 90;
219const DEPTH = 40;
220
221const vertexShader = /* glsl */ `
222uniform float uTime;
223uniform float uFrequency;
224uniform float uAmplitude;
225varying vec2 vGround; // world XZ, for the grid
226varying float vHeight;
227varying float vDepth;
228
229float wave(vec2 p) {
230 float t = uTime;
231 vec2 q = p * uFrequency;
232 float h = sin(q.x * 0.32 + t * 0.9) * cos(q.y * 0.42 - t * 0.6) * 0.55;
233 h += sin((q.x + q.y) * 0.19 + t * 0.5) * 0.45;
234 h += sin(q.x * 0.71 - q.y * 0.33 - t * 1.1) * 0.18;
235 return h * uAmplitude;
236}
237
238void main() {
239 vec3 p = position;
240 float h = wave(p.xz);
241 // Calm at the front edge (it meets the next section), full waves further back
242 h *= smoothstep(-4.0, 2.5, -p.z);
243 p.y += h;
244 vGround = p.xz;
245 vHeight = h;
246 vec4 mvPosition = modelViewMatrix * vec4(p, 1.0);
247 vDepth = -mvPosition.z;
248 gl_Position = projectionMatrix * mvPosition;
249}
250`;
251
252const fragmentShader = /* glsl */ `
253uniform vec3 uTop;
254uniform vec3 uBottom;
255uniform vec3 uLine;
256varying vec2 vGround;
257varying float vHeight;
258varying float vDepth;
259
260float gridLine(float x) {
261 float w = fwidth(x) * 1.2;
262 float f = abs(fract(x) - 0.5); // 0.5 on a grid line, 0 halfway between two
263 return smoothstep(0.5 - w, 0.5, f);
264}
265
266void main() {
267 // The grid fades in away from the front edge, so the bottom row is exactly uBottom
268 float lines = max(gridLine(vGround.x * 0.6), gridLine(vGround.y * 0.6));
269 float gridFade = smoothstep(3.0, 7.0, vDepth) * (1.0 - smoothstep(18.0, 30.0, vDepth));
270 vec3 color = mix(uBottom, uLine, lines * gridFade * 0.55);
271 color += uLine * smoothstep(0.35, 1.2, vHeight) * 0.12 * gridFade; // light on the crests
272
273 // Distance haze into the upper section's color: the two backgrounds blend into each other
274 float haze = smoothstep(9.0, 30.0, vDepth);
275 color = mix(color, uTop, haze);
276 gl_FragColor = vec4(color, 1.0);
277 #include <colorspace_fragment>
278}
279`;
280
281const Terrain = memo(function Terrain({ topColor, bottomColor, waveFrequency, waveAmplitude, speed, lineColor }: Required<Omit<TerrainDividerProps, "pauseWhenOffscreen" | "lowPowerMode">>) {
282 const geometry = useMemo(() => {
283 const g = new THREE.PlaneGeometry(WIDTH, DEPTH, 240, 120);
284 g.rotateX(-Math.PI / 2);
285 g.translate(0, 0, -DEPTH / 2 + 4);
286 return g;
287 }, []);
288 const material = useMemo(
289 () =>
290 new THREE.ShaderMaterial({
291 uniforms: {
292 uTime: { value: 0 },
293 uFrequency: { value: 1 },
294 uAmplitude: { value: 1 },
295 uTop: { value: new THREE.Color() },
296 uBottom: { value: new THREE.Color() },
297 uLine: { value: new THREE.Color() },
298 },
299 vertexShader,
300 fragmentShader,
301 }),
302 []
303 );
304
305 useEffect(() => {
306 const u = material.uniforms;
307 (u.uTop.value as THREE.Color).set(topColor);
308 (u.uBottom.value as THREE.Color).set(bottomColor);
309 (u.uLine.value as THREE.Color).set(lineColor);
310 u.uFrequency.value = waveFrequency;
311 u.uAmplitude.value = waveAmplitude;
312 }, [material, topColor, bottomColor, lineColor, waveFrequency, waveAmplitude]);
313 useEffect(() => () => geometry.dispose(), [geometry]);
314 useEffect(() => () => material.dispose(), [material]);
315
316 useFrame((state, delta) => {
317 material.uniforms.uTime.value += Math.min(delta, 1 / 30) * speed;
318 state.camera.lookAt(0, 0.6, -12);
319 });
320
321 return <mesh geometry={geometry} material={material} frustumCulled={false} />;
322});
323
324/**
325 * Self-contained divider: place it between two sections, in a positioned box ~160–260px high.
326 * Its top edge is the upper section's color, its bottom edge the lower section's.
327 */
328function TerrainDivider3D({
329 topColor = "#000000",
330 bottomColor = "#120e1f",
331 waveFrequency = 1,
332 waveAmplitude = 1,
333 speed = 1,
334 lineColor = "#8b5cf6",
335 pauseWhenOffscreen,
336 lowPowerMode,
337}: TerrainDividerProps) {
338 return (
339 <UICanvas pauseWhenOffscreen={pauseWhenOffscreen} lowPowerMode={lowPowerMode} camera={{ position: [0, 1.7, 6], fov: 32, near: 0.1, far: 60 }} dpr={[1, 1.75]}>
340 <color attach="background" args={[topColor]} />
341 <Terrain
342 topColor={topColor}
343 bottomColor={bottomColor}
344 waveFrequency={waveFrequency}
345 waveAmplitude={waveAmplitude}
346 speed={speed}
347 lineColor={lineColor}
348 />
349 </UICanvas>
350 );
351}
352
353export default memo(TerrainDivider3D);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import TerrainDivider3D from "@/components/TerrainDivider3D";
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 <TerrainDivider3D
8 waveFrequency={1}
9 waveAmplitude={1}
10 speed={1}
11 topColor="#000000"
12 bottomColor="#120e1f"
13 lineColor="#8b5cf6"
14 />
15 </div>
16 );
17}
Next componentWord Cloud Globe→