-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluidglass.txt
More file actions
333 lines (290 loc) · 9.77 KB
/
Copy pathFluidglass.txt
File metadata and controls
333 lines (290 loc) · 9.77 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
## Integrate the <FluidGlass /> component from React Bits
You are helping integrate an open-source React component into an existing application.
### Component: FluidGlass
### Variant: JavaScript + CSS
### Dependencies: three @react-three/fiber @react-three/drei maath
---
### Usage Example
```jsx
// IMPORTANT INFO BELOW
// This component requires a 3D model to function correctly.
// You can find three example models in the 'public/assets/3d' directory of the repository:
// - 'lens.glb'
// - 'bar.glb'
// - 'cube.glb'
// Make sure to place these models in the correct directory or update the paths accordingly.
import FluidGlass from './FluidGlass'
<div style={{ height: '600px', position: 'relative' }}>
<FluidGlass
mode="lens" // or "bar", "cube"
lensProps={{
scale: 0.25,
ior: 1.15,
thickness: 5,
chromaticAberration: 0.1,
anisotropy: 0.01
}}
barProps={} // add specific props if using bar mode
cubeProps={} // add specific props if using cube mode
/>
</div>
```
### Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| mode | string | 'lens' | Display mode of the fluid glass effect. Options: 'lens', 'bar', 'cube' |
| lensProps | object | {} | Props specific to lens mode including material properties like ior, thickness, transmission |
| barProps | object | {} | Props specific to bar mode including navItems array and material properties |
| cubeProps | object | {} | Props specific to cube mode including material properties and interaction settings |
### Full Component Source
```jsx
/* eslint-disable react/no-unknown-property */
import * as THREE from 'three';
import { useRef, useState, useEffect, memo } from 'react';
import { Canvas, createPortal, useFrame, useThree } from '@react-three/fiber';
import {
useFBO,
useGLTF,
useScroll,
Image,
Scroll,
Preload,
ScrollControls,
MeshTransmissionMaterial,
Text
} from '@react-three/drei';
import { easing } from 'maath';
export default function FluidGlass({ mode = 'lens', lensProps = {}, barProps = {}, cubeProps = {} }) {
const Wrapper = mode === 'bar' ? Bar : mode === 'cube' ? Cube : Lens;
const rawOverrides = mode === 'bar' ? barProps : mode === 'cube' ? cubeProps : lensProps;
const {
navItems = [
{ label: 'Home', link: '' },
{ label: 'About', link: '' },
{ label: 'Contact', link: '' }
],
...modeProps
} = rawOverrides;
return (
<Canvas camera={{ position: [0, 0, 20], fov: 15 }} gl={{ alpha: true }}>
<ScrollControls damping={0.2} pages={3} distance={0.4}>
{mode === 'bar' && <NavItems items={navItems} />}
<Wrapper modeProps={modeProps}>
<Scroll>
<Typography />
<Images />
</Scroll>
<Scroll html />
<Preload />
</Wrapper>
</ScrollControls>
</Canvas>
);
}
const ModeWrapper = memo(function ModeWrapper({
children,
glb,
geometryKey,
lockToBottom = false,
followPointer = true,
modeProps = {},
...props
}) {
const ref = useRef();
const { nodes } = useGLTF(glb);
const buffer = useFBO();
const { viewport: vp } = useThree();
const [scene] = useState(() => new THREE.Scene());
const geoWidthRef = useRef(1);
useEffect(() => {
const geo = nodes[geometryKey]?.geometry;
geo.computeBoundingBox();
geoWidthRef.current = geo.boundingBox.max.x - geo.boundingBox.min.x || 1;
}, [nodes, geometryKey]);
useFrame((state, delta) => {
const { gl, viewport, pointer, camera } = state;
const v = viewport.getCurrentViewport(camera, [0, 0, 15]);
const destX = followPointer ? (pointer.x * v.width) / 2 : 0;
const destY = lockToBottom ? -v.height / 2 + 0.2 : followPointer ? (pointer.y * v.height) / 2 : 0;
easing.damp3(ref.current.position, [destX, destY, 15], 0.15, delta);
if (modeProps.scale == null) {
const maxWorld = v.width * 0.9;
const desired = maxWorld / geoWidthRef.current;
ref.current.scale.setScalar(Math.min(0.15, desired));
}
gl.setRenderTarget(buffer);
gl.render(scene, camera);
gl.setRenderTarget(null);
// Background Color
gl.setClearColor(0x5227ff, 1);
});
const { scale, ior, thickness, anisotropy, chromaticAberration, ...extraMat } = modeProps;
return (
<>
{createPortal(children, scene)}
<mesh scale={[vp.width, vp.height, 1]}>
<planeGeometry />
<meshBasicMaterial map={buffer.texture} transparent />
</mesh>
<mesh ref={ref} scale={scale ?? 0.15} rotation-x={Math.PI / 2} geometry={nodes[geometryKey]?.geometry} {...props}>
<MeshTransmissionMaterial
buffer={buffer.texture}
ior={ior ?? 1.15}
thickness={thickness ?? 5}
anisotropy={anisotropy ?? 0.01}
chromaticAberration={chromaticAberration ?? 0.1}
{...extraMat}
/>
</mesh>
</>
);
});
function Lens({ modeProps, ...p }) {
return <ModeWrapper glb="/assets/3d/lens.glb" geometryKey="Cylinder" followPointer modeProps={modeProps} {...p} />;
}
function Cube({ modeProps, ...p }) {
return <ModeWrapper glb="/assets/3d/cube.glb" geometryKey="Cube" followPointer modeProps={modeProps} {...p} />;
}
function Bar({ modeProps = {}, ...p }) {
const defaultMat = {
transmission: 1,
roughness: 0,
thickness: 10,
ior: 1.15,
color: '#ffffff',
attenuationColor: '#ffffff',
attenuationDistance: 0.25
};
return (
<ModeWrapper
glb="/assets/3d/bar.glb"
geometryKey="Cube"
lockToBottom
followPointer={false}
modeProps={{ ...defaultMat, ...modeProps }}
{...p}
/>
);
}
function NavItems({ items }) {
const group = useRef();
const { viewport, camera } = useThree();
const DEVICE = {
mobile: { max: 639, spacing: 0.2, fontSize: 0.035 },
tablet: { max: 1023, spacing: 0.24, fontSize: 0.035 },
desktop: { max: Infinity, spacing: 0.3, fontSize: 0.035 }
};
const getDevice = () => {
const w = window.innerWidth;
return w <= DEVICE.mobile.max ? 'mobile' : w <= DEVICE.tablet.max ? 'tablet' : 'desktop';
};
const [device, setDevice] = useState(getDevice());
useEffect(() => {
const onResize = () => setDevice(getDevice());
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const { spacing, fontSize } = DEVICE[device];
useFrame(() => {
if (!group.current) return;
const v = viewport.getCurrentViewport(camera, [0, 0, 15]);
group.current.position.set(0, -v.height / 2 + 0.2, 15.1);
group.current.children.forEach((child, i) => {
child.position.x = (i - (items.length - 1) / 2) * spacing;
});
});
const handleNavigate = link => {
if (!link) return;
link.startsWith('#') ? (window.location.hash = link) : (window.location.href = link);
};
return (
<group ref={group} renderOrder={10}>
{items.map(({ label, link }) => (
<Text
key={label}
fontSize={fontSize}
color="white"
anchorX="center"
anchorY="middle"
depthWrite={false}
outlineWidth={0}
outlineBlur="20%"
outlineColor="#000"
outlineOpacity={0.5}
depthTest={false}
renderOrder={10}
onClick={e => {
e.stopPropagation();
handleNavigate(link);
}}
onPointerOver={() => (document.body.style.cursor = 'pointer')}
onPointerOut={() => (document.body.style.cursor = 'auto')}
>
{label}
</Text>
))}
</group>
);
}
function Images() {
const group = useRef();
const data = useScroll();
const { height } = useThree(s => s.viewport);
useFrame(() => {
group.current.children[0].material.zoom = 1 + data.range(0, 1 / 3) / 3;
group.current.children[1].material.zoom = 1 + data.range(0, 1 / 3) / 3;
group.current.children[2].material.zoom = 1 + data.range(1.15 / 3, 1 / 3) / 2;
group.current.children[3].material.zoom = 1 + data.range(1.15 / 3, 1 / 3) / 2;
group.current.children[4].material.zoom = 1 + data.range(1.15 / 3, 1 / 3) / 2;
});
return (
<group ref={group}>
<Image position={[-2, 0, 0]} scale={[3, height / 1.1, 1]} url="/assets/demo/cs1.webp" />
<Image position={[2, 0, 3]} scale={3} url="/assets/demo/cs2.webp" />
<Image position={[-2.05, -height, 6]} scale={[1, 3, 1]} url="/assets/demo/cs3.webp" />
<Image position={[-0.6, -height, 9]} scale={[1, 2, 1]} url="/assets/demo/cs1.webp" />
<Image position={[0.75, -height, 10.5]} scale={1.5} url="/assets/demo/cs2.webp" />
</group>
);
}
function Typography() {
const DEVICE = {
mobile: { fontSize: 0.2 },
tablet: { fontSize: 0.4 },
desktop: { fontSize: 0.6 }
};
const getDevice = () => {
const w = window.innerWidth;
return w <= 639 ? 'mobile' : w <= 1023 ? 'tablet' : 'desktop';
};
const [device, setDevice] = useState(getDevice());
useEffect(() => {
const onResize = () => setDevice(getDevice());
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
}, []);
const { fontSize } = DEVICE[device];
return (
<Text
position={[0, 0, 12]}
fontSize={fontSize}
letterSpacing={-0.05}
outlineWidth={0}
outlineBlur="20%"
outlineColor="#000"
outlineOpacity={0.5}
color="white"
anchorX="center"
anchorY="middle"
>
React Bits
</Text>
);
}
```
### Integration Instructions
1. Install any listed dependencies.
2. Copy the component source into the appropriate directory in the project.
3. Import and render the component using the usage example above as a starting point.
4. Adjust props as needed for the specific use case — refer to the props table for all available options.