ShaderGodRays.js 6.6 KB
Newer Older
H
Huw Bowles 已提交
1 2 3
/**
 * @author huwb / http://huwbowles.com/
 *
H
Huw Bowles 已提交
4 5 6 7 8
 * God-rays (crepuscular rays)
 *
 * Similar implementation to the one used by Crytek for CryEngine 2 [Sousa2008].
 * Blurs a mask generated from the depth map along radial lines emanating from the light
 * source. The blur repeatedly applies a blur filter of increasing support but constant
9
 * sample count to produce a blur filter with large support.
H
Huw Bowles 已提交
10 11 12 13 14 15 16
 *
 * My implementation performs 3 passes, similar to the implementation from Sousa. I found
 * just 6 samples per pass produced acceptible results. The blur is applied three times,
 * with decreasing filter support. The result is equivalent to a single pass with
 * 6*6*6 = 216 samples.
 *
 * References:
17 18
 *
 * Sousa2008 - Crysis Next Gen Effects, GDC2008, http://www.crytek.com/sites/default/files/GDC08_SousaT_CrysisEffects.ppt
H
Huw Bowles 已提交
19
 */
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

THREE.ShaderGodRays = {

	/**
	 * The god-ray generation shader.
	 *
	 * First pass:
	 *
	 * The input is the depth map. I found that the output from the
	 * THREE.MeshDepthMaterial material was directly suitable without
	 * requiring any treatment whatsoever.
	 *
	 * The depth map is blurred along radial lines towards the "sun". The
	 * output is written to a temporary render target (I used a 1/4 sized
	 * target).
	 *
	 * Pass two & three:
	 *
	 * The results of the previous pass are re-blurred, each time with a
	 * decreased distance between samples.
	 */

	'godrays_generate': {

		uniforms: {
45

46
			tInput: {
47
				value: null
48 49 50 51 52 53 54
			},
			fStepSize: {
				value: 1.0
			},
			vSunPositionScreenSpace: {
				value: new THREE.Vector2( 0.5, 0.5 )
			}
55

56 57 58
		},

		vertexShader: [
59

60
			"varying vec2 vUv;",
61

62
			"void main() {",
63

A
alteredq 已提交
64
				"vUv = uv;",
65
				"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
66

67
			"}"
68

G
gero3 已提交
69
		].join( "\n" ),
70 71 72

		fragmentShader: [

73 74
			"#define TAPS_PER_PASS 6.0",

75 76 77 78 79 80 81 82
			"varying vec2 vUv;",

			"uniform sampler2D tInput;",

			"uniform vec2 vSunPositionScreenSpace;",
			"uniform float fStepSize;", // filter step size

			"void main() {",
83

84
				// delta from current pixel to "sun" position
85 86 87 88

				"vec2 delta = vSunPositionScreenSpace - vUv;",
				"float dist = length( delta );",

89
				// Step vector (uv space)
90 91 92

				"vec2 stepv = fStepSize * delta / dist;",

93
				// Number of iterations between pixel and sun
94

95 96 97 98 99
				"float iters = dist/fStepSize;",

				"vec2 uv = vUv.xy;",
				"float col = 0.0;",

100 101 102 103
				// This breaks ANGLE in Chrome 22
				//	- see http://code.google.com/p/chromium/issues/detail?id=153105

				/*
104 105
				// Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),
				// so i've just left the loop
106 107 108

				"for ( float i = 0.0; i < TAPS_PER_PASS; i += 1.0 ) {",

109 110
					// Accumulate samples, making sure we dont walk past the light source.

111
					// The check for uv.y < 1 would not be necessary with "border" UV wrap
M
Magnuz Binder 已提交
112
					// mode, with a black border color. I don't think this is currently
113 114 115
					// exposed by three.js. As a result there might be artifacts when the
					// sun is to the left, right or bottom of screen as these cases are
					// not specifically handled.
116 117

					"col += ( i <= iters && uv.y < 1.0 ? texture2D( tInput, uv ).r : 0.0 );",
118
					"uv += stepv;",
119

120
				"}",
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
				*/

				// Unrolling loop manually makes it work in ANGLE

				"if ( 0.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",

				"if ( 1.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",

				"if ( 2.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",

				"if ( 3.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",

				"if ( 4.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",

				"if ( 5.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
				"uv += stepv;",
142 143 144 145 146 147 148

				// Should technically be dividing by 'iters', but 'TAPS_PER_PASS' smooths out
				// objectionable artifacts, in particular near the sun position. The side
				// effect is that the result is darker than it should be around the sun, as
				// TAPS_PER_PASS is greater than the number of samples actually accumulated.
				// When the result is inverted (in the shader 'godrays_combine', this produces
				// a slight bright spot at the position of the sun, even when it is occluded.
149

150
				"gl_FragColor = vec4( col/TAPS_PER_PASS );",
151 152
				"gl_FragColor.a = 1.0;",

153 154
			"}"

G
gero3 已提交
155
		].join( "\n" )
H
Huw Bowles 已提交
156 157 158

	},

159 160 161 162
	/**
	 * Additively applies god rays from texture tGodRays to a background (tColors).
	 * fGodRayIntensity attenuates the god rays.
	 */
163

164 165 166
	'godrays_combine': {

		uniforms: {
167

168
			tColors: {
169
				value: null
170
			},
171

172
			tGodRays: {
173
				value: null
174
			},
175

176 177 178
			fGodRayIntensity: {
				value: 0.69
			},
179

180 181
			vSunPositionScreenSpace: {
				value: new THREE.Vector2( 0.5, 0.5 )
182 183
			}

184 185 186
		},

		vertexShader: [
187

188
			"varying vec2 vUv;",
189

190
			"void main() {",
191

A
alteredq 已提交
192
				"vUv = uv;",
193
				"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
194

195
			"}"
196

G
gero3 已提交
197
			].join( "\n" ),
198 199 200 201 202 203 204 205 206 207 208 209

		fragmentShader: [

			"varying vec2 vUv;",

			"uniform sampler2D tColors;",
			"uniform sampler2D tGodRays;",

			"uniform vec2 vSunPositionScreenSpace;",
			"uniform float fGodRayIntensity;",

			"void main() {",
210

211 212 213
				// Since THREE.MeshDepthMaterial renders foreground objects white and background
				// objects black, the god-rays will be white streaks. Therefore value is inverted
				// before being combined with tColors
214 215 216 217

				"gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity * vec4( 1.0 - texture2D( tGodRays, vUv ).r );",
				"gl_FragColor.a = 1.0;",

218 219
			"}"

G
gero3 已提交
220
		].join( "\n" )
H
Huw Bowles 已提交
221

H
Huw Bowles 已提交
222
	},
223 224


225 226 227 228
	/**
	 * A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
	 * cheaper/faster/simpler to implement this as a simple sun sprite.
	 */
229

230 231 232
	'godrays_fake_sun': {

		uniforms: {
233

234 235 236
			vSunPositionScreenSpace: {
				value: new THREE.Vector2( 0.5, 0.5 )
			},
237

238 239 240
			fAspect: {
				value: 1.0
			},
241 242 243 244 245 246 247 248 249

			sunColor: {
				value: new THREE.Color( 0xffee00 )
			},

			bgColor: {
				value: new THREE.Color( 0x000000 )
			}

250 251 252
		},

		vertexShader: [
253

254
			"varying vec2 vUv;",
255

256
			"void main() {",
257

A
alteredq 已提交
258
				"vUv = uv;",
259
				"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
260

261
			"}"
262

G
gero3 已提交
263
		].join( "\n" ),
264 265 266 267 268 269 270 271

		fragmentShader: [

			"varying vec2 vUv;",

			"uniform vec2 vSunPositionScreenSpace;",
			"uniform float fAspect;",

272 273 274
			"uniform vec3 sunColor;",
			"uniform vec3 bgColor;",

275
			"void main() {",
276 277 278

				"vec2 diff = vUv - vSunPositionScreenSpace;",

279
				// Correct for aspect ratio
280

281
				"diff.x *= fAspect;",
282 283 284 285 286 287 288

				"float prop = clamp( length( diff ) / 0.5, 0.0, 1.0 );",
				"prop = 0.35 * pow( 1.0 - prop, 3.0 );",

				"gl_FragColor.xyz = mix( sunColor, bgColor, 1.0 - prop );",
				"gl_FragColor.w = 1.0;",

289 290
			"}"

G
gero3 已提交
291
		].join( "\n" )
292

H
Huw Bowles 已提交
293
	}
294 295

};