Skip to content

Commit 0f51656

Browse files
ektogamatabernier
andauthored
feat: LensFlare (#228)
* 1st commit for LensFlare effect * doc * 1st commit for LensFlare effect * doc * chore: wip LensFlare * chore: simplifying * chore(LensFlare): dirt story * Added MDX docs and minor changes * Duplicated line removed --------- Co-authored-by: Antoine BERNIER <[email protected]>
1 parent 4b5920a commit 0f51656

File tree

6 files changed

+332
-0
lines changed

6 files changed

+332
-0
lines changed
747 KB
Loading

.storybook/public/lensDirtTexture.png

68 KB
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React, { memo } from 'react'
2+
import * as THREE from 'three'
3+
import type { Meta, StoryObj } from '@storybook/react'
4+
import { BackSide } from 'three'
5+
import { Box, useTexture } from '@react-three/drei'
6+
7+
import { Setup } from '../Setup'
8+
import { EffectComposer, LensFlare, Vignette, Bloom } from '../../src'
9+
10+
function SkyBox() {
11+
const texture = useTexture('/digital_painting_golden_hour_sunset.jpg')
12+
13+
return (
14+
<mesh userData={{ lensflare: 'no-occlusion' }} scale={[-1, 1, 1]} castShadow={false} receiveShadow={false}>
15+
<sphereGeometry args={[50, 32, 32]} />
16+
<meshBasicMaterial toneMapped={false} map={texture} side={BackSide} />
17+
</mesh>
18+
)
19+
}
20+
21+
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction
22+
const meta = {
23+
title: 'Effect/LensFlare',
24+
component: LensFlare,
25+
decorators: [
26+
(Story) => (
27+
<Setup cameraPosition={new THREE.Vector3(8, 1, 10)} cameraFov={50}>
28+
{Story()}
29+
</Setup>
30+
),
31+
],
32+
tags: ['autodocs'],
33+
// argTypes: {
34+
// debug: {
35+
// control: { type: 'range', min: 0, max: 1, step: 0.01 },
36+
// },
37+
// },
38+
} satisfies Meta<typeof LensFlare>
39+
40+
export default meta
41+
type Story = StoryObj<typeof meta>
42+
43+
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
44+
export const Primary: Story = {
45+
render: (args) => (
46+
<>
47+
<color attach="background" args={['#303035']} />
48+
49+
<directionalLight intensity={3} position={[-25, 60, -60]} />
50+
51+
<Box />
52+
53+
<SkyBox />
54+
55+
<EffectComposer multisampling={0} disableNormalPass>
56+
<LensFlare {...args} />
57+
</EffectComposer>
58+
</>
59+
),
60+
args: {},
61+
}
62+
63+
function DirtLensFlare(props) {
64+
const texture = useTexture('/lensDirtTexture.png')
65+
66+
return <LensFlare {...props} lensDirtTexture={texture} />
67+
}
68+
69+
export const Secondary: Story = {
70+
render: (args) => (
71+
<>
72+
<color attach="background" args={['#303035']} />
73+
74+
<directionalLight intensity={3} position={[-25, 60, -60]} />
75+
76+
<Box />
77+
78+
<SkyBox />
79+
80+
<EffectComposer multisampling={0} disableNormalPass>
81+
<DirtLensFlare {...args} />
82+
</EffectComposer>
83+
</>
84+
),
85+
args: { starBurst: true },
86+
}

docs/effects/lensflare.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: Lensflare
3+
nav: 1
4+
---
5+
6+
A Lens Flare adds the optical aberration caused by the dispersion of light entering the lens through its edges.
7+
8+
Based on [ektogamat/R3F-Ultimate-Lens-Flare](https://github.com/ektogamat/R3F-Ultimate-Lens-Flare).
9+
10+
```jsx
11+
import { LensFlare } from '@react-three/postprocessing'
12+
13+
return (
14+
<LensFlare
15+
blendFunction={BlendFunction.Screen} // The blend function of this effect.
16+
enabled={true} // Boolean to enable/disable the effect.
17+
opacity={1.0} // The opacity for this effect. Default: 1.0
18+
starBurst={true} // Boolean to enable/disable the start burst effect. Can be disabled to improve performance.
19+
glareSize={0.96} // The glare size. Default: 0.2
20+
followMouse={false} // Set it to follow the mouse, ignoring the lens position. Default: false
21+
lensPosition={[0, 0.5, 0]} // The position of the lens flare in 3d space.
22+
starPoints={6} // The number of points for the star. Default: 6
23+
flareSize={0.01} // The flare side. Default 0.01
24+
flareSpeed={0.01} // The flare animation speed. Default 0.01
25+
flareShape={0.01} // Changes the appearance to anamorphic. Default 0.01
26+
animated={true} // Animated flare. Default: true
27+
anamorphic={false} //Set the appearance to full anamorphic. Default: false
28+
colorGain={new THREE.Color(70, 70, 70)} // Set the color gain for the lens flare. Must be a THREE.Color in RBG format.
29+
lensDirtTexture={'/lensDirtTexture.png'} // Texture to be used as color dirt for starburst effect.
30+
haloScale={0.5} // The halo scale. Default: 0.5
31+
secondaryGhosts={true} // Option to enable/disable secondary ghosts. Default: true.
32+
ghostScale={0.0} // Option to enable/disable secondary ghosts. Default: true.
33+
aditionalStreaks={true} // Option to enable/disable aditional streaks. Default: true.
34+
smoothTime={0.07} // The time that it takes to fade the occlusion. Default: 0.07
35+
/>
36+
)
37+
```
38+
39+
#### Ignoring occlusion on some objects
40+
41+
To disable the occlusion effect, simply add `userData={{ lensflare: 'no-occlusion' }}` to your object/mesh props.
42+
43+
#### Improving performance
44+
45+
Use bvh `<bvh><Scene></bvh>` to enhance the internal raycaster performance.
46+
47+
#### Limitations
48+
49+
The Ultimate Lens Flare leverages the raycaster to examine the material type of objects and determine if they are `MeshTransmissionMaterial` or `MeshPhysicalMaterial`. It checks for the transmission parameter to identify glass-like materials. Therefore, for an object to behave like glass, its material should have either `transmission = 1` or `transparent = true` and `opacity = NUMBER`. The effect automatically interprets the opacity `NUMBER` value to determine the brightness of the flare.
50+
51+
#### Credits
52+
53+
- https://www.shadertoy.com/view/4sK3W3
54+
- https://www.shadertoy.com/view/4sX3Rs
55+
- https://www.shadertoy.com/view/dllSRX
56+
- https://www.shadertoy.com/view/Xlc3D2
57+
- https://www.shadertoy.com/view/XtKfRV

0 commit comments

Comments
 (0)