Skip to content

Commit

Permalink
added basic tone mapping and exposure shaders #31
Browse files Browse the repository at this point in the history
  • Loading branch information
cansik committed Apr 18, 2018
1 parent d1bf6f8 commit 00efdde
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 6 deletions.
Binary file added data/hdr.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions shader/exposureFrag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;

varying vec4 vertColor;
varying vec4 vertTexCoord;

vec3 n;

uniform float exposure = 0.0;

void main() {
vec4 c = texture2D(texture, vertTexCoord.st) * vertColor;
vec3 n = c.rgb * pow(2.0, exposure);
gl_FragColor = vec4(n, c.a);
}
88 changes: 88 additions & 0 deletions shader/toneMappingFrag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

#define PROCESSING_TEXTURE_SHADER

uniform sampler2D texture;

varying vec4 vertColor;
varying vec4 vertTexCoord;

vec3 n;

uniform float gamma = 2.2;

vec3 linearToneMapping(vec3 color)
{
float exposure = 1.;
color = clamp(exposure * color, 0., 1.);
color = pow(color, vec3(1. / gamma));
return color;
}

vec3 simpleReinhardToneMapping(vec3 color)
{
float exposure = 1.5;
color *= exposure/(1. + color / exposure);
color = pow(color, vec3(1. / gamma));
return color;
}

vec3 lumaBasedReinhardToneMapping(vec3 color)
{
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
float toneMappedLuma = luma / (1. + luma);
color *= toneMappedLuma / luma;
color = pow(color, vec3(1. / gamma));
return color;
}

vec3 whitePreservingLumaBasedReinhardToneMapping(vec3 color)
{
float white = 2.;
float luma = dot(color, vec3(0.2126, 0.7152, 0.0722));
float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma);
color *= toneMappedLuma / luma;
color = pow(color, vec3(1. / gamma));
return color;
}

vec3 RomBinDaHouseToneMapping(vec3 color)
{
color = exp( -1.0 / ( 2.72*color + 0.15 ) );
color = pow(color, vec3(1. / gamma));
return color;
}

vec3 filmicToneMapping(vec3 color)
{
color = max(vec3(0.), color - vec3(0.004));
color = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06);
return color;
}

vec3 Uncharted2ToneMapping(vec3 color)
{
float A = 0.15;
float B = 0.50;
float C = 0.10;
float D = 0.20;
float E = 0.02;
float F = 0.30;
float W = 11.2;
float exposure = 2.;
color *= exposure;
color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;
float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F;
color /= white;
color = pow(color, vec3(1. / gamma));
return color;
}

void main() {
vec4 c = texture2D(texture, vertTexCoord.st) * vertColor;
vec3 n = linearToneMapping(c.rgb);
gl_FragColor = vec4(n, c.a);
}
24 changes: 24 additions & 0 deletions src/main/java/ch/bildspur/postfx/builder/PostFXBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ public PostFXBuilder invert() {
return this;
}

/**
* Run an exposure pass on the texture.
*
* @return Builder object.
*/
public PostFXBuilder exposure(float exposure) {
ExposurePass pass = getPass(ExposurePass.class);
pass.setExposure(exposure);
supervisor.pass(pass);
return this;
}

/**
* Run an tone mapping pass on the texture.
*
* @return Builder object.
*/
public PostFXBuilder toneMapping(float gamma) {
ToneMappingPass pass = getPass(ToneMappingPass.class);
pass.setGamma(gamma);
supervisor.pass(pass);
return this;
}

/**
* Run a brightness and contrast correction pass on the texture.
*
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ch/bildspur/postfx/pass/ExposurePass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.bildspur.postfx.pass;

import ch.bildspur.postfx.Supervisor;
import processing.core.PApplet;

/**
* Created by cansik on 27.03.17.
*/
public class ExposurePass extends BasePass {
private static final String PASS_NAME = "exposureFrag";

private float exposure;

public ExposurePass(PApplet sketch) {
this(sketch, 0.0f);
}

public ExposurePass(PApplet sketch, float exposure) {
super(sketch, PASS_NAME);

this.exposure = exposure;
}

@Override
public void prepare(Supervisor supervisor) {
shader.set("exposure", exposure);
}

public float getExposure() {
return exposure;
}

public void setExposure(float exposure) {
this.exposure = exposure;
}
}
36 changes: 36 additions & 0 deletions src/main/java/ch/bildspur/postfx/pass/ToneMappingPass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.bildspur.postfx.pass;

import ch.bildspur.postfx.Supervisor;
import processing.core.PApplet;

/**
* Created by cansik on 14.05.17.
*/
public class ToneMappingPass extends BasePass {
private static final String PASS_NAME = "toneMappingFrag";

private float gamma;

public ToneMappingPass(PApplet sketch) {
this(sketch, 2.2f);
}

public ToneMappingPass(PApplet sketch, float gamma) {
super(sketch, PASS_NAME);

this.gamma = gamma;
}

@Override
public void prepare(Supervisor supervisor) {
shader.set("gamma", gamma);
}

public float getGamma() {
return gamma;
}

public void setGamma(float gamma) {
this.gamma = gamma;
}
}
20 changes: 14 additions & 6 deletions src/test/java/ch/bildspur/postfx/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Sketch extends PApplet {
PostFX fx;

PImage lenna;
PImage hdrImage;

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

// load test image
lenna = this.loadImage("data/Lenna.png");
hdrImage = this.loadImage("data/hdr.jpg");

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

drawChessBoard(canvas, 8);
//drawBackgroundImage(canvas);
//drawChessBoard(canvas, 8);
drawHDRImage(canvas);

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

// add effect
fx.render(canvas)
.brightnessContrast(0.1f, 1.0f)
.bloom(0.8f, 30, 50)
.vignette(1, 0)
.binaryGlitch(0.5f)
//.brightnessContrast(0.1f, 1.0f)
//.bloom(0.8f, 30, 50)
//.vignette(1, 0)
//.binaryGlitch(0.5f)
.toneMapping(1.2f)
.exposure(1.0f)
.compose(passResult);

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

void drawHDRImage(PGraphics pg) {
pg.image(hdrImage, 0, 0, pg.width, pg.height);
}

void drawChessBoard(PGraphics pg, int amount) {

float blockX = pg.width / (float) amount;
Expand Down

0 comments on commit 00efdde

Please sign in to comment.