| 1 | #version 100 |
|---|
| 2 | precision mediump int; |
|---|
| 3 | precision mediump float; |
|---|
| 4 | |
|---|
| 5 | uniform sampler2D inRTT; |
|---|
| 6 | uniform sampler2D inBloom; |
|---|
| 7 | uniform sampler2D inLum; |
|---|
| 8 | |
|---|
| 9 | varying vec2 uv; |
|---|
| 10 | |
|---|
| 11 | const float MIDDLE_GREY = 0.72; |
|---|
| 12 | const float FUDGE = 0.001; |
|---|
| 13 | const float L_WHITE = 1.5; |
|---|
| 14 | |
|---|
| 15 | /** Tone mapping function |
|---|
| 16 | @note Only affects rgb, not a |
|---|
| 17 | @param inColour The HDR colour |
|---|
| 18 | @param lum The scene lumninence |
|---|
| 19 | @returns Tone mapped colour |
|---|
| 20 | */ |
|---|
| 21 | vec4 toneMap(in vec4 inColour, in float lum) |
|---|
| 22 | { |
|---|
| 23 | // From Reinhard et al |
|---|
| 24 | // "Photographic Tone Reproduction for Digital Images" |
|---|
| 25 | |
|---|
| 26 | // Initial luminence scaling (equation 2) |
|---|
| 27 | inColour.rgb *= MIDDLE_GREY / (FUDGE + lum); |
|---|
| 28 | |
|---|
| 29 | // Control white out (equation 4 nom) |
|---|
| 30 | inColour.rgb *= (1.0 + inColour.rgb / L_WHITE); |
|---|
| 31 | |
|---|
| 32 | // Final mapping (equation 4 denom) |
|---|
| 33 | inColour.rgb /= (1.0 + inColour.rgb); |
|---|
| 34 | |
|---|
| 35 | return inColour; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void main(void) |
|---|
| 39 | { |
|---|
| 40 | // Get main scene colour |
|---|
| 41 | vec4 sceneCol = texture2D(inRTT, uv); |
|---|
| 42 | |
|---|
| 43 | // Get luminence value |
|---|
| 44 | vec4 lum = texture2D(inLum, vec2(0.5, 0.5)); |
|---|
| 45 | |
|---|
| 46 | // tone map this |
|---|
| 47 | vec4 toneMappedSceneCol = toneMap(sceneCol, lum.r); |
|---|
| 48 | |
|---|
| 49 | // Get bloom colour |
|---|
| 50 | vec4 bloom = texture2D(inBloom, uv); |
|---|
| 51 | |
|---|
| 52 | // Add scene & bloom |
|---|
| 53 | gl_FragColor = vec4(toneMappedSceneCol.rgb + bloom.rgb, 1.0); |
|---|
| 54 | } |
|---|