Skip to content

Commit 4193589

Browse files
authored
Add files via upload
1 parent df61efe commit 4193589

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

docs/PostProcessing/Bloom.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Bloom
2+
3+
// Coming soon
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Chromatic Aberration
2+
3+
Chromatic Aberration is an effect commonly observed in images, when the camera can't focus all color channels onto a point. This results in different colors being in slightly different positions.
4+
5+
In shaders, this effect can be simulated by offsetting the position at which we sample each color channel. This is quite simple, but the value by which you offset needs to be quite small in order to look any good.
6+
7+
Let's first assume a couple variables we have access to.
8+
9+
* `fragCoord`
10+
* `iResolution`
11+
12+
These are explained on the home page, under variables.
13+
14+
Let's grab the normalized coordinates of our pixel. Since `fragCoord` ranges from 0 to the screen dimensions, we just need to divide by the screen dimensions.
15+
16+
```js
17+
vec2 normalizedCoords = fragCoord/iResolution.xy;
18+
```
19+
20+
Now let's define some small offsets.
21+
22+
```js
23+
vec2 redOffset = vec2(0.001, 0.002);
24+
vec2 greenOffset = vec2(-0.002, 0.001);
25+
vec2 blueOffset = vec2(0.003, 0.002);
26+
```
27+
28+
Now that we have some offsets, we can just sample our texture with them. Assuming we have a texture sampler called `iChannel0`:
29+
30+
```js
31+
float red = texture(iChannel0, normalizedCoords + redOffset).r;
32+
float green = texture(iChannel0, normalizedCoords + greenOffset).g;
33+
vec2 blueAlpha = texture(iChannel0, normalizedCoords + blueOffset).ba;
34+
```
35+
36+
Using [swizzling](https://en.wikipedia.org/wiki/Swizzling_(computer_graphics)), or accessing only a specific component of a vector, we can easily get only the value we need. Notice that for the last channel, I use a vec2 instead of a float, because, of course, we need to get the alpha channel as well. We'll just use the blue offset to sample the alpha channel.
37+
38+
Now, we can join it all together.
39+
40+
```js
41+
fragColor = vec4(red, green, blueAlpha);
42+
```
43+
44+
45+
46+
## Edge Strengthing
47+
48+
So, we've constructed our output color with the offset channels. This is oftentimes enough, but realistically, you'll only see chromatic aberration near the very edges of images, and not so much at the center.
49+
50+
To make this effect more realistic, we can simulate that.
51+
52+
We have the current normalized position in `normalizedCoords`. At the moment this ranges from 0 to 1, but with a bit of clever math we can get something cool.
53+
54+
First, let's shift our coordinates over.
55+
56+
```js
57+
vec2 shiftedCoords = normalizedCoords - 0.5;
58+
```
59+
60+
So now, the coordinates range from -0.5 to 0.5. We're close now, we just need to take the absolute value of the coordinates to get rid of that negative.
61+
62+
```js
63+
shiftedCoords = abs(shiftedCoords);
64+
```
65+
66+
Now, let's edit our previous texture sampling code.
67+
68+
```js
69+
float red = texture(iChannel0, normalizedCoords + (shiftedCoords * redOffset)).r;
70+
float green = texture(iChannel0, normalizedCoords + (shiftedCoords * greenOffset)).g;
71+
vec2 blueAlpha = texture(iChannel0, normalizedCoords + (shiftedCoords * blueOffset)).ba;
72+
```
73+
74+
So, you see, the values range from 0.5 at the bottom left, to 0.5 at the top right. This means that in the center, the values are 0.0, or around it. We're effectively reducing our offset by how close it is to the center.
75+
76+
This means that the effect will only really be visible around the edges, which is exactly what we want.
77+
78+
However, it's probably not going to be that visible, as we've effectively halved it, seeing as how we're multiplying by at most, 0.5. So, we can modify our absolute value line to increase it.
79+
80+
```js
81+
shiftedCoords = abs(shiftedCoords) * 2.0;
82+
```
83+
84+
Doubling it brings it back to the original strength, but you can increase or decrease this however you want to tweak the strength.
85+
86+
87+
## Full Program
88+
89+
```js
90+
const float STRENGTH = 2.0;
91+
92+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
93+
{
94+
vec2 normalizedCoords = fragCoord/iResolution.xy;
95+
96+
vec2 redOffset = vec2(0.001, 0.002);
97+
vec2 greenOffset = vec2(-0.002, 0.001);
98+
vec2 blueOffset = vec2(0.003, 0.002);
99+
100+
vec2 shiftedCoords = normalizedCoords - 0.5;
101+
102+
shiftedCoords = abs(shiftedCoords) * STRENGTH;
103+
104+
float red = texture(iChannel0, normalizedCoords + (shiftedCoords * redOffset)).r;
105+
float green = texture(iChannel0, normalizedCoords + (shiftedCoords * greenOffset)).g;
106+
vec2 blueAlpha = texture(iChannel0, normalizedCoords + (shiftedCoords * blueOffset)).ba;
107+
108+
fragColor = vec4(red, green, blueAlpha);
109+
}
110+
```
111+
112+
113+
114+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Gaussian Blur
2+
3+
// Coming soon

docs/PostProcessing/Vignette.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Vignette
2+
3+
// Coming soon

0 commit comments

Comments
 (0)