forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_postprocessing_ssao.html
194 lines (140 loc) · 4.69 KB
/
webgl_postprocessing_ssao.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - postprocessing - Screen Space Ambient Occlusion</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;
}
a { color:#00ff78; }
#info {
color: #fff;
position: absolute;
top: 0px;
width: 100%;
padding: 5px;
}
.dg.ac {
z-index: 1 !important; /* FIX DAT.GUI */
}
</style>
</head>
<body>
<script src="../build/three.js"></script>
<script src="js/shaders/SSAOShader.js"></script>
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/SSAOPass.js"></script>
<script src="js/shaders/CopyShader.js"></script>
<script src="js/SimplexNoise.js"></script>
<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src='js/libs/dat.gui.min.js'></script>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - screen space ambient occlusion<br/>
<div id="error" style="display: none;">
Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
This demo will not work.
</div>
</div>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var container, stats;
var camera, scene, renderer;
var effectComposer;
var ssaoPass;
var group;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
document.querySelector( '#error' ).style.display = 'block';
return;
}
camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
camera.position.z = 500;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xaaaaaa );
scene.add( new THREE.DirectionalLight() );
scene.add( new THREE.HemisphereLight() );
group = new THREE.Group();
scene.add( group );
var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
for ( var i = 0; i < 100; i ++ ) {
var material = new THREE.MeshLambertMaterial( {
color: Math.random() * 0xffffff
} );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = Math.random() * 400 - 200;
mesh.position.y = Math.random() * 400 - 200;
mesh.position.z = Math.random() * 400 - 200;
mesh.rotation.x = Math.random();
mesh.rotation.y = Math.random();
mesh.rotation.z = Math.random();
mesh.scale.setScalar( Math.random() * 10 + 2 );
group.add( mesh );
}
stats = new Stats();
container.appendChild( stats.dom );
var width = window.innerWidth;
var height = window.innerHeight;
ssaoPass = new THREE.SSAOPass( scene, camera, width, height );
ssaoPass.kernelRadius = 16;
effectComposer = new THREE.EffectComposer( renderer );
effectComposer.addPass( ssaoPass );
// Init gui
var gui = new dat.GUI();
gui.add( ssaoPass, 'output', {
'Default': THREE.SSAOPass.OUTPUT.Default,
'SSAO Only': THREE.SSAOPass.OUTPUT.SSAO,
'SSAO Only + Blur': THREE.SSAOPass.OUTPUT.Blur,
'Beauty': THREE.SSAOPass.OUTPUT.Beauty,
'Depth': THREE.SSAOPass.OUTPUT.Depth,
'Normal': THREE.SSAOPass.OUTPUT.Normal
} ).onChange( function ( value ) {
ssaoPass.output = parseInt( value );
} );
gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
}
function onWindowResize() {
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
ssaoPass.setSize( width, height );
}
function animate() {
requestAnimationFrame( animate );
stats.begin();
render();
stats.end();
}
function render() {
var timer = performance.now();
group.rotation.x = timer * 0.0002;
group.rotation.y = timer * 0.0001;
effectComposer.render();
}
</script>
</body>
</html>