Skip to content

Commit 00efdde

Browse files
committed
added basic tone mapping and exposure shaders #31
1 parent d1bf6f8 commit 00efdde

File tree

7 files changed

+219
-6
lines changed

7 files changed

+219
-6
lines changed

data/hdr.jpg

117 KB
Loading

shader/exposureFrag.glsl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture;
9+
10+
varying vec4 vertColor;
11+
varying vec4 vertTexCoord;
12+
13+
vec3 n;
14+
15+
uniform float exposure = 0.0;
16+
17+
void main() {
18+
vec4 c = texture2D(texture, vertTexCoord.st) * vertColor;
19+
vec3 n = c.rgb * pow(2.0, exposure);
20+
gl_FragColor = vec4(n, c.a);
21+
}

shader/toneMappingFrag.glsl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
#define PROCESSING_TEXTURE_SHADER
7+
8+
uniform sampler2D texture;
9+
10+
varying vec4 vertColor;
11+
varying vec4 vertTexCoord;
12+
13+
vec3 n;
14+
15+
uniform float gamma = 2.2;
16+
17+
vec3 linearToneMapping(vec3 color)
18+
{
19+
float exposure = 1.;
20+
color = clamp(exposure * color, 0., 1.);
21+
color = pow(color, vec3(1. / gamma));
22+
return color;
23+
}
24+
25+
vec3 simpleReinhardToneMapping(vec3 color)
26+
{
27+
float exposure = 1.5;
28+
color *= exposure/(1. + color / exposure);
29+
color = pow(color, vec3(1. / gamma));
30+
return color;
31+
}
32+
33+
vec3 lumaBasedReinhardToneMapping(vec3 color)
34+
{
35+
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
36+
float toneMappedLuma = luma / (1. + luma);
37+
color *= toneMappedLuma / luma;
38+
color = pow(color, vec3(1. / gamma));
39+
return color;
40+
}
41+
42+
vec3 whitePreservingLumaBasedReinhardToneMapping(vec3 color)
43+
{
44+
float white = 2.;
45+
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
46+
float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma);
47+
color *= toneMappedLuma / luma;
48+
color = pow(color, vec3(1. / gamma));
49+
return color;
50+
}
51+
52+
vec3 RomBinDaHouseToneMapping(vec3 color)
53+
{
54+
color = exp( -1.0 / ( 2.72*color + 0.15 ) );
55+
color = pow(color, vec3(1. / gamma));
56+
return color;
57+
}
58+
59+
vec3 filmicToneMapping(vec3 color)
60+
{
61+
color = max(vec3(0.), color - vec3(0.004));
62+
color = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06);
63+
return color;
64+
}
65+
66+
vec3 Uncharted2ToneMapping(vec3 color)
67+
{
68+
float A = 0.15;
69+
float B = 0.50;
70+
float C = 0.10;
71+
float D = 0.20;
72+
float E = 0.02;
73+
float F = 0.30;
74+
float W = 11.2;
75+
float exposure = 2.;
76+
color *= exposure;
77+
color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
78+
float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F;
79+
color /= white;
80+
color = pow(color, vec3(1. / gamma));
81+
return color;
82+
}
83+
84+
void main() {
85+
vec4 c = texture2D(texture, vertTexCoord.st) * vertColor;
86+
vec3 n = linearToneMapping(c.rgb);
87+
gl_FragColor = vec4(n, c.a);
88+
}

src/main/java/ch/bildspur/postfx/builder/PostFXBuilder.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ public PostFXBuilder invert() {
160160
return this;
161161
}
162162

163+
/**
164+
* Run an exposure pass on the texture.
165+
*
166+
* @return Builder object.
167+
*/
168+
public PostFXBuilder exposure(float exposure) {
169+
ExposurePass pass = getPass(ExposurePass.class);
170+
pass.setExposure(exposure);
171+
supervisor.pass(pass);
172+
return this;
173+
}
174+
175+
/**
176+
* Run an tone mapping pass on the texture.
177+
*
178+
* @return Builder object.
179+
*/
180+
public PostFXBuilder toneMapping(float gamma) {
181+
ToneMappingPass pass = getPass(ToneMappingPass.class);
182+
pass.setGamma(gamma);
183+
supervisor.pass(pass);
184+
return this;
185+
}
186+
163187
/**
164188
* Run a brightness and contrast correction pass on the texture.
165189
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ch.bildspur.postfx.pass;
2+
3+
import ch.bildspur.postfx.Supervisor;
4+
import processing.core.PApplet;
5+
6+
/**
7+
* Created by cansik on 27.03.17.
8+
*/
9+
public class ExposurePass extends BasePass {
10+
private static final String PASS_NAME = "exposureFrag";
11+
12+
private float exposure;
13+
14+
public ExposurePass(PApplet sketch) {
15+
this(sketch, 0.0f);
16+
}
17+
18+
public ExposurePass(PApplet sketch, float exposure) {
19+
super(sketch, PASS_NAME);
20+
21+
this.exposure = exposure;
22+
}
23+
24+
@Override
25+
public void prepare(Supervisor supervisor) {
26+
shader.set("exposure", exposure);
27+
}
28+
29+
public float getExposure() {
30+
return exposure;
31+
}
32+
33+
public void setExposure(float exposure) {
34+
this.exposure = exposure;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ch.bildspur.postfx.pass;
2+
3+
import ch.bildspur.postfx.Supervisor;
4+
import processing.core.PApplet;
5+
6+
/**
7+
* Created by cansik on 14.05.17.
8+
*/
9+
public class ToneMappingPass extends BasePass {
10+
private static final String PASS_NAME = "toneMappingFrag";
11+
12+
private float gamma;
13+
14+
public ToneMappingPass(PApplet sketch) {
15+
this(sketch, 2.2f);
16+
}
17+
18+
public ToneMappingPass(PApplet sketch, float gamma) {
19+
super(sketch, PASS_NAME);
20+
21+
this.gamma = gamma;
22+
}
23+
24+
@Override
25+
public void prepare(Supervisor supervisor) {
26+
shader.set("gamma", gamma);
27+
}
28+
29+
public float getGamma() {
30+
return gamma;
31+
}
32+
33+
public void setGamma(float gamma) {
34+
this.gamma = gamma;
35+
}
36+
}

src/test/java/ch/bildspur/postfx/Sketch.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class Sketch extends PApplet {
3030
PostFX fx;
3131

3232
PImage lenna;
33+
PImage hdrImage;
3334

3435
public void settings() {
3536
size(OUTPUT_WIDTH, OUTPUT_HEIGHT, P2D);
@@ -51,6 +52,7 @@ public void setup() {
5152

5253
// load test image
5354
lenna = this.loadImage("data/Lenna.png");
55+
hdrImage = this.loadImage("data/hdr.jpg");
5456

5557
// initialise pass results
5658
passResult = createGraphics(width, height, P2D);
@@ -63,8 +65,8 @@ public void draw() {
6365
canvas.beginDraw();
6466
canvas.background(55);
6567

66-
drawChessBoard(canvas, 8);
67-
//drawBackgroundImage(canvas);
68+
//drawChessBoard(canvas, 8);
69+
drawHDRImage(canvas);
6870

6971
// render simple cube
7072
canvas.pushMatrix();
@@ -85,10 +87,12 @@ public void draw() {
8587

8688
// add effect
8789
fx.render(canvas)
88-
.brightnessContrast(0.1f, 1.0f)
89-
.bloom(0.8f, 30, 50)
90-
.vignette(1, 0)
91-
.binaryGlitch(0.5f)
90+
//.brightnessContrast(0.1f, 1.0f)
91+
//.bloom(0.8f, 30, 50)
92+
//.vignette(1, 0)
93+
//.binaryGlitch(0.5f)
94+
.toneMapping(1.2f)
95+
.exposure(1.0f)
9296
.compose(passResult);
9397

9498
blendMode(BLEND);
@@ -107,6 +111,10 @@ void drawBackgroundImage(PGraphics pg) {
107111
pg.image(lenna, 0, 0, pg.width, pg.height);
108112
}
109113

114+
void drawHDRImage(PGraphics pg) {
115+
pg.image(hdrImage, 0, 0, pg.width, pg.height);
116+
}
117+
110118
void drawChessBoard(PGraphics pg, int amount) {
111119

112120
float blockX = pg.width / (float) amount;

0 commit comments

Comments
 (0)