forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_postprocessing_godrays.html
439 lines (285 loc) · 13.9 KB
/
webgl_postprocessing_godrays.html
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - postprocessing - godrays</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
text-align:center;
}
a { color:#0078ff; }
#info {
color:#fff;
position: absolute;
top: 0px; width: 100%;
padding: 5px;
z-index:100;
}
</style>
</head>
<body>
<script src="../build/three.js"></script>
<script src="js/loaders/OBJLoader.js"></script>
<script src="js/ShaderGodRays.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl god-rays example - tree by <a href="http://www.turbosquid.com/3d-models/free-tree-3d-model/592617" target="_blank" rel="noopener">stanloshka</a>
</div>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var container, stats;
var camera, scene, renderer, materialDepth;
var sphereMesh;
var sunPosition = new THREE.Vector3( 0, 1000, - 1000 );
var screenSpacePosition = new THREE.Vector3();
var mouseX = 0, mouseY = 0;
var postprocessing = { enabled: true };
var orbitRadius = 200;
var bgColor = 0x000511;
var sunColor = 0xffee00;
// Use a smaller size for some of the god-ray render targets for better performance.
var godrayRenderTargetResolutionMultiplier = 1.0 / 4.0;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
//
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
camera.position.z = 200;
scene = new THREE.Scene();
//
materialDepth = new THREE.MeshDepthMaterial();
var materialScene = new THREE.MeshBasicMaterial( { color: 0x000000 } );
// tree
var loader = new THREE.OBJLoader();
loader.load( 'models/obj/tree.obj', function ( object ) {
object.material = materialScene;
object.position.set( 0, - 150, - 150 );
object.scale.multiplyScalar( 400 );
scene.add( object );
} );
// sphere
var geo = new THREE.SphereBufferGeometry( 1, 20, 10 );
sphereMesh = new THREE.Mesh( geo, materialScene );
sphereMesh.scale.multiplyScalar( 20 );
scene.add( sphereMesh );
//
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xffffff );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer.autoClear = false;
//
stats = new Stats();
container.appendChild( stats.dom );
//
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
window.addEventListener( 'resize', onWindowResize, false );
//
initPostprocessing( window.innerWidth, window.innerHeight );
}
//
function onDocumentMouseMove( event ) {
mouseX = event.clientX - window.innerWidth / 2;
mouseY = event.clientY - window.innerHeight / 2;
}
function onDocumentTouchStart( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - window.innerWidth / 2;
mouseY = event.touches[ 0 ].pageY - window.innerHeight / 2;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - window.innerWidth / 2;
mouseY = event.touches[ 0 ].pageY - window.innerHeight / 2;
}
}
//
function onWindowResize() {
var renderTargetWidth = window.innerWidth;
var renderTargetHeight = window.innerHeight;
camera.aspect = renderTargetWidth / renderTargetHeight;
camera.updateProjectionMatrix();
renderer.setSize( renderTargetWidth, renderTargetHeight );
postprocessing.rtTextureColors.setSize( renderTargetWidth, renderTargetHeight );
postprocessing.rtTextureDepth.setSize( renderTargetWidth, renderTargetHeight );
postprocessing.rtTextureDepthMask.setSize( renderTargetWidth, renderTargetHeight );
var adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier;
var adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier;
postprocessing.rtTextureGodRays1.setSize( adjustedWidth, adjustedHeight );
postprocessing.rtTextureGodRays2.setSize( adjustedWidth, adjustedHeight );
}
function initPostprocessing( renderTargetWidth, renderTargetHeight ) {
postprocessing.scene = new THREE.Scene();
postprocessing.camera = new THREE.OrthographicCamera( - 0.5, 0.5, 0.5, - 0.5, - 10000, 10000 );
postprocessing.camera.position.z = 100;
postprocessing.scene.add( postprocessing.camera );
var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
postprocessing.rtTextureColors = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
// Switching the depth formats to luminance from rgb doesn't seem to work. I didn't
// investigate further for now.
// pars.format = THREE.LuminanceFormat;
// I would have this quarter size and use it as one of the ping-pong render
// targets but the aliasing causes some temporal flickering
postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
postprocessing.rtTextureDepthMask = new THREE.WebGLRenderTarget( renderTargetWidth, renderTargetHeight, pars );
// The ping-pong render targets can use an adjusted resolution to minimize cost
var adjustedWidth = renderTargetWidth * godrayRenderTargetResolutionMultiplier;
var adjustedHeight = renderTargetHeight * godrayRenderTargetResolutionMultiplier;
postprocessing.rtTextureGodRays1 = new THREE.WebGLRenderTarget( adjustedWidth, adjustedHeight, pars );
postprocessing.rtTextureGodRays2 = new THREE.WebGLRenderTarget( adjustedWidth, adjustedHeight, pars );
// god-ray shaders
var godraysMaskShader = THREE.ShaderGodRays[ "godrays_depthMask" ];
postprocessing.godrayMaskUniforms = THREE.UniformsUtils.clone( godraysMaskShader.uniforms );
postprocessing.materialGodraysDepthMask = new THREE.ShaderMaterial( {
uniforms: postprocessing.godrayMaskUniforms,
vertexShader: godraysMaskShader.vertexShader,
fragmentShader: godraysMaskShader.fragmentShader
} );
var godraysGenShader = THREE.ShaderGodRays[ "godrays_generate" ];
postprocessing.godrayGenUniforms = THREE.UniformsUtils.clone( godraysGenShader.uniforms );
postprocessing.materialGodraysGenerate = new THREE.ShaderMaterial( {
uniforms: postprocessing.godrayGenUniforms,
vertexShader: godraysGenShader.vertexShader,
fragmentShader: godraysGenShader.fragmentShader
} );
var godraysCombineShader = THREE.ShaderGodRays[ "godrays_combine" ];
postprocessing.godrayCombineUniforms = THREE.UniformsUtils.clone( godraysCombineShader.uniforms );
postprocessing.materialGodraysCombine = new THREE.ShaderMaterial( {
uniforms: postprocessing.godrayCombineUniforms,
vertexShader: godraysCombineShader.vertexShader,
fragmentShader: godraysCombineShader.fragmentShader
} );
var godraysFakeSunShader = THREE.ShaderGodRays[ "godrays_fake_sun" ];
postprocessing.godraysFakeSunUniforms = THREE.UniformsUtils.clone( godraysFakeSunShader.uniforms );
postprocessing.materialGodraysFakeSun = new THREE.ShaderMaterial( {
uniforms: postprocessing.godraysFakeSunUniforms,
vertexShader: godraysFakeSunShader.vertexShader,
fragmentShader: godraysFakeSunShader.fragmentShader
} );
postprocessing.godraysFakeSunUniforms.bgColor.value.setHex( bgColor );
postprocessing.godraysFakeSunUniforms.sunColor.value.setHex( sunColor );
postprocessing.godrayCombineUniforms.fGodRayIntensity.value = 0.75;
postprocessing.quad = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 1.0, 1.0 ),
postprocessing.materialGodraysGenerate
);
postprocessing.quad.position.z = - 9900;
postprocessing.scene.add( postprocessing.quad );
}
function animate() {
requestAnimationFrame( animate, renderer.domElement );
stats.begin();
render();
stats.end();
}
function getStepSize( filterLen, tapsPerPass, pass ) {
return filterLen * Math.pow( tapsPerPass, - pass );
}
function filterGodRays( inputTex, renderTarget, stepSize ) {
postprocessing.scene.overrideMaterial = postprocessing.materialGodraysGenerate;
postprocessing.godrayGenUniforms[ "fStepSize" ].value = stepSize;
postprocessing.godrayGenUniforms[ "tInput" ].value = inputTex;
renderer.setRenderTarget( renderTarget );
renderer.render( postprocessing.scene, postprocessing.camera );
postprocessing.scene.overrideMaterial = null;
}
function render() {
var time = Date.now() / 4000;
sphereMesh.position.x = orbitRadius * Math.cos( time );
sphereMesh.position.z = orbitRadius * Math.sin( time ) - 100;
camera.position.x += ( mouseX - camera.position.x ) * 0.036;
camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
camera.lookAt( scene.position );
if ( postprocessing.enabled ) {
// Find the screenspace position of the sun
screenSpacePosition.copy( sunPosition ).project( camera );
screenSpacePosition.x = ( screenSpacePosition.x + 1 ) / 2;
screenSpacePosition.y = ( screenSpacePosition.y + 1 ) / 2;
// Give it to the god-ray and sun shaders
postprocessing.godrayGenUniforms[ "vSunPositionScreenSpace" ].value.x = screenSpacePosition.x;
postprocessing.godrayGenUniforms[ "vSunPositionScreenSpace" ].value.y = screenSpacePosition.y;
postprocessing.godraysFakeSunUniforms[ "vSunPositionScreenSpace" ].value.x = screenSpacePosition.x;
postprocessing.godraysFakeSunUniforms[ "vSunPositionScreenSpace" ].value.y = screenSpacePosition.y;
// -- Draw sky and sun --
// Clear colors and depths, will clear to sky color
renderer.setRenderTarget( postprocessing.rtTextureColors );
renderer.clear( true, true, false );
// Sun render. Runs a shader that gives a brightness based on the screen
// space distance to the sun. Not very efficient, so i make a scissor
// rectangle around the suns position to avoid rendering surrounding pixels.
var sunsqH = 0.74 * window.innerHeight; // 0.74 depends on extent of sun from shader
var sunsqW = 0.74 * window.innerHeight; // both depend on height because sun is aspect-corrected
screenSpacePosition.x *= window.innerWidth;
screenSpacePosition.y *= window.innerHeight;
renderer.setScissor( screenSpacePosition.x - sunsqW / 2, screenSpacePosition.y - sunsqH / 2, sunsqW, sunsqH );
renderer.setScissorTest( true );
postprocessing.godraysFakeSunUniforms[ "fAspect" ].value = window.innerWidth / window.innerHeight;
postprocessing.scene.overrideMaterial = postprocessing.materialGodraysFakeSun;
renderer.setRenderTarget( postprocessing.rtTextureColors );
renderer.render( postprocessing.scene, postprocessing.camera );
renderer.setScissorTest( false );
// -- Draw scene objects --
// Colors
scene.overrideMaterial = null;
renderer.setRenderTarget( postprocessing.rtTextureColors );
renderer.render( scene, camera );
// Depth
scene.overrideMaterial = materialDepth;
renderer.setRenderTarget( postprocessing.rtTextureDepth );
renderer.clear();
renderer.render( scene, camera );
//
postprocessing.godrayMaskUniforms[ "tInput" ].value = postprocessing.rtTextureDepth.texture;
postprocessing.scene.overrideMaterial = postprocessing.materialGodraysDepthMask;
renderer.setRenderTarget( postprocessing.rtTextureDepthMask );
renderer.render( postprocessing.scene, postprocessing.camera );
// -- Render god-rays --
// Maximum length of god-rays (in texture space [0,1]X[0,1])
var filterLen = 1.0;
// Samples taken by filter
var TAPS_PER_PASS = 6.0;
// Pass order could equivalently be 3,2,1 (instead of 1,2,3), which
// would start with a small filter support and grow to large. however
// the large-to-small order produces less objectionable aliasing artifacts that
// appear as a glimmer along the length of the beams
// pass 1 - render into first ping-pong target
filterGodRays( postprocessing.rtTextureDepthMask.texture, postprocessing.rtTextureGodRays2, getStepSize( filterLen, TAPS_PER_PASS, 1.0 ) );
// pass 2 - render into second ping-pong target
filterGodRays( postprocessing.rtTextureGodRays2.texture, postprocessing.rtTextureGodRays1, getStepSize( filterLen, TAPS_PER_PASS, 2.0 ) );
// pass 3 - 1st RT
filterGodRays( postprocessing.rtTextureGodRays1.texture, postprocessing.rtTextureGodRays2, getStepSize( filterLen, TAPS_PER_PASS, 3.0 ) );
// final pass - composite god-rays onto colors
postprocessing.godrayCombineUniforms[ "tColors" ].value = postprocessing.rtTextureColors.texture;
postprocessing.godrayCombineUniforms[ "tGodRays" ].value = postprocessing.rtTextureGodRays2.texture;
postprocessing.scene.overrideMaterial = postprocessing.materialGodraysCombine;
renderer.setRenderTarget( null );
renderer.render( postprocessing.scene, postprocessing.camera );
postprocessing.scene.overrideMaterial = null;
} else {
renderer.setRenderTarget( null );
renderer.clear();
renderer.render( scene, camera );
}
}
</script>
</body>
</html>