ToneMapShader.js 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**
 * @author miibond
 *
 * Full-screen tone-mapping shader based on http://www.cis.rit.edu/people/faculty/ferwerda/publications/sig02_paper.pdf
 */



var ToneMapShader = {

	uniforms: {

		"tDiffuse": { value: null },
		"averageLuminance": { value: 1.0 },
		"luminanceMap": { value: null },
		"maxLuminance": { value: 16.0 },
		"minLuminance": { value: 0.01 },
		"middleGrey": { value: 0.6 }
	},

	vertexShader: [

		"varying vec2 vUv;",

		"void main() {",

G
Garrett Johnson 已提交
27 28
		"	vUv = uv;",
		"	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

		"}"

	].join( "\n" ),

	fragmentShader: [

		"#include <common>",

		"uniform sampler2D tDiffuse;",

		"varying vec2 vUv;",

		"uniform float middleGrey;",
		"uniform float minLuminance;",
		"uniform float maxLuminance;",
		"#ifdef ADAPTED_LUMINANCE",
G
Garrett Johnson 已提交
46
		"	uniform sampler2D luminanceMap;",
47
		"#else",
G
Garrett Johnson 已提交
48
		"	uniform float averageLuminance;",
49 50 51
		"#endif",

		"vec3 ToneMap( vec3 vColor ) {",
G
Garrett Johnson 已提交
52
		"	#ifdef ADAPTED_LUMINANCE",
53
				// Get the calculated average luminance
G
Garrett Johnson 已提交
54 55 56 57
		"		float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;",
		"	#else",
		"		float fLumAvg = averageLuminance;",
		"	#endif",
58 59

			// Calculate the luminance of the current pixel
G
Garrett Johnson 已提交
60
		"	float fLumPixel = linearToRelativeLuminance( vColor );",
61 62

			// Apply the modified operator (Eq. 4)
G
Garrett Johnson 已提交
63
		"	float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );",
64

G
Garrett Johnson 已提交
65 66
		"	float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);",
		"	return fLumCompressed * vColor;",
67 68 69 70
		"}",

		"void main() {",

G
Garrett Johnson 已提交
71
		"	vec4 texel = texture2D( tDiffuse, vUv );",
72

G
Garrett Johnson 已提交
73
		"	gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );",
74 75 76 77 78 79 80 81

		"}"

	].join( "\n" )

};

export { ToneMapShader };