forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webgl_postprocessing_ssr.html
349 lines (232 loc) · 9.16 KB
/
webgl_postprocessing_ssr.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
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<title>three.js webgl - postprocessing - Screen Space Reflection</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
SSRPass demo by <a href="https://github.com/gonnavis" target="_blank">Vis</a>.<br />
based on <a href="https://threejs.org/examples/?q=physic#webgl_shading_physical" target="_blank">this scene</a>
</div>
<!-- <script src="https://cdn.bootcdn.net/ajax/libs/vConsole/3.3.4/vconsole.min.js"></script>
<script>
var vConsole = new VConsole();
</script> -->
<script type="module">
import * as THREE from '../build/three.module.js';
import Stats from './jsm/libs/stats.module.js';
import { GUI } from './jsm/libs/dat.gui.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js'
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
import { SSRPass } from './jsm/postprocessing/SSRPass.js';
// var initialFrustumSize = 1200;
var params = {
enableSSR: true,
}
var composer
var ssrPass
var container, stats;
var camera, scene, renderer;
var mesh;
var controls;
var sunLight, pointLight, ambientLight;
var mixer;
var clock = new THREE.Clock();
var gui, shadowCameraHelper, shadowConfig = {
shadowCameraVisible: false,
shadowCameraNear: 750,
shadowCameraFar: 4000,
shadowBias: - 0.0002
};
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
// CAMERA
// var cameraNear = 1
// var cameraFar = 2000
// var aspect = window.innerWidth / window.innerHeight;
// camera = new THREE.OrthographicCamera(initialFrustumSize * aspect / - 2, initialFrustumSize * aspect / 2, initialFrustumSize / 2, initialFrustumSize / - 2, cameraNear, cameraFar);
// camera.position.set(394.8768477903498, 424.03752576248496, 815.0242032138592)
camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 2, 10000 );
camera.position.set( 307.043866722481, 340.37194439741677, 673.1047168390921);
window.camera=camera
// SCENE
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0, 1000, 10000 );
// TEXTURES
var textureLoader = new THREE.TextureLoader();
var textureSquares = textureLoader.load( "textures/patterns/bright_squares256.png" );
textureSquares.repeat.set( 50, 50 );
textureSquares.wrapS = textureSquares.wrapT = THREE.RepeatWrapping;
textureSquares.magFilter = THREE.NearestFilter;
textureSquares.encoding = THREE.sRGBEncoding;
var textureNoiseColor = textureLoader.load( "textures/disturb.jpg" );
textureNoiseColor.repeat.set( 1, 1 );
textureNoiseColor.wrapS = textureNoiseColor.wrapT = THREE.RepeatWrapping;
textureNoiseColor.encoding = THREE.sRGBEncoding;
var textureLava = textureLoader.load( "textures/lava/lavatile.jpg" );
textureLava.repeat.set( 6, 2 );
textureLava.wrapS = textureLava.wrapT = THREE.RepeatWrapping;
textureLava.encoding = THREE.sRGBEncoding;
// GROUND
var groundMaterial = new THREE.MeshPhongMaterial( {
shininess: 80,
color: 0xffffff,
specular: 0xffffff,
map: textureSquares
} );
var planeGeometry = new THREE.PlaneBufferGeometry( 100, 100 );
var ground = new THREE.Mesh( planeGeometry, groundMaterial );
ground.position.set( 0, 0, 0 );
ground.rotation.x = - Math.PI / 2;
ground.scale.set( 1000, 1000, 1000 );
ground.receiveShadow = true;
scene.add( ground );
// MATERIALS
var materialLambert = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, map: textureNoiseColor } );
var materialPhong = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, map: textureLava } );
var materialPhongCube = new THREE.MeshPhongMaterial( { shininess: 50, color: 0xffffff, specular: 0x999999, } );
// OBJECTS
var sphereGeometry = new THREE.SphereBufferGeometry( 100, 64, 32 );
var torusGeometry = new THREE.TorusBufferGeometry( 240, 60, 32, 64 );
var cubeGeometry = new THREE.BoxBufferGeometry( 150, 150, 150 );
addObject( torusGeometry, materialPhong, 0, 100, 0, 0 );
addObject( cubeGeometry, materialLambert, 350, 75, 300, 0 );
mesh = addObject( sphereGeometry, materialPhongCube, 350, 100, - 350, 0 );
function addObjectColor( geometry, color, x, y, z, ry ) {
var material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
return addObject( geometry, material, x, y, z, ry );
}
function addObject( geometry, material, x, y, z, ry ) {
var tmpMesh = new THREE.Mesh( geometry, material );
tmpMesh.material.color.offsetHSL( 0.1, - 0.1, 0 );
tmpMesh.position.set( x, y, z );
tmpMesh.rotation.y = ry;
tmpMesh.castShadow = true;
tmpMesh.receiveShadow = true;
scene.add( tmpMesh );
return tmpMesh;
}
var bigCube = new THREE.BoxBufferGeometry( 50, 500, 50 );
var midCube = new THREE.BoxBufferGeometry( 50, 200, 50 );
var smallCube = new THREE.BoxBufferGeometry( 100, 100, 100 );
addObjectColor( bigCube, 0xff0000, - 500, 250, 0, 0 );
addObjectColor( smallCube, 0xff0000, - 500, 50, - 150, 0 );
addObjectColor( midCube, 0x00ff00, 500, 100, 0, 0 );
addObjectColor( smallCube, 0x00ff00, 500, 50, - 150, 0 );
addObjectColor( midCube, 0x0000ff, 0, 100, - 500, 0 );
addObjectColor( smallCube, 0x0000ff, - 150, 50, - 500, 0 );
addObjectColor( midCube, 0xff00ff, 0, 100, 500, 0 );
addObjectColor( smallCube, 0xff00ff, - 150, 50, 500, 0 );
addObjectColor( new THREE.BoxBufferGeometry( 500, 10, 10 ), 0xffff00, 0, 600, 0, Math.PI / 4 );
addObjectColor( new THREE.BoxBufferGeometry( 250, 10, 10 ), 0xffff00, 0, 600, 0, 0 );
addObjectColor( new THREE.SphereBufferGeometry( 100, 32, 26 ), 0xffffff, - 300, 100, 300, 0 );
// MORPHS
var loader = new GLTFLoader();
loader.load( "models/gltf/SittingBox.glb", function ( gltf ) {
var mesh = gltf.scene.children[ 0 ];
mixer = new THREE.AnimationMixer( mesh );
mixer.clipAction( gltf.animations[ 0 ] ).setDuration( 10 ).play();
var s = 200;
mesh.scale.set( s, s, s );
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
} );
// LIGHTS
ambientLight = new THREE.AmbientLight( 0x3f2806 );
scene.add( ambientLight );
pointLight = new THREE.PointLight( 0xffaa00, 1, 5000 );
scene.add( pointLight );
sunLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
sunLight.position.set( 1000, 2000, 1000 );
sunLight.castShadow = true;
sunLight.shadow.camera.top = 750;
sunLight.shadow.camera.bottom = - 750;
sunLight.shadow.camera.left = - 750;
sunLight.shadow.camera.right = 750;
sunLight.shadow.camera.near = shadowConfig.shadowCameraNear;
sunLight.shadow.camera.far = shadowConfig.shadowCameraFar;
sunLight.shadow.mapSize.set( 1024, 1024 );
sunLight.shadow.bias = shadowConfig.shadowBias;
scene.add( sunLight );
// SHADOW CAMERA HELPER
shadowCameraHelper = new THREE.CameraHelper( sunLight.shadow.camera );
shadowCameraHelper.visible = shadowConfig.shadowCameraVisible;
scene.add( shadowCameraHelper );
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: false } );
// renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputEncoding = THREE.sRGBEncoding;
//
controls = new OrbitControls( camera, renderer.domElement );
// STATS
stats = new Stats();
container.appendChild( stats.dom );
// EVENTS
window.addEventListener( 'resize', onWindowResize, false );
// composer
composer = new EffectComposer(renderer);
ssrPass = new SSRPass(scene, camera, innerWidth, innerHeight);
composer.addPass(ssrPass);
// GUI
gui = new GUI( { width: 400 } );
ssrPass.surfDist=4.2
gui.add(ssrPass, 'output', {
'Default': SSRPass.OUTPUT.Default,
// 'DefaultBlur': SSRPass.OUTPUT.DefaultBlur,
'SSR Only': SSRPass.OUTPUT.SSR,
'SSR Only + Blur': SSRPass.OUTPUT.Blur,
'Beauty': SSRPass.OUTPUT.Beauty,
'Depth': SSRPass.OUTPUT.Depth,
'Normal': SSRPass.OUTPUT.Normal
}).onChange(function (value) {
ssrPass.output = parseInt(value);
});
gui.add(ssrPass, 'maxDistance').min(0).max(1000);
gui.add(ssrPass, 'surfDist').min(0).max(10).step(.1);
}
//
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
composer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate() {
requestAnimationFrame( animate );
stats.begin();
render();
stats.end();
}
function render() {
// update
var delta = clock.getDelta();
controls.update();
if ( mixer ) {
mixer.update( delta );
}
if(params.enableSSR){
// ssrPass.setFrustumSize(initialFrustumSize/camera.zoom)
composer.render();
}else{
renderer.render(scene,camera)
}
}
</script>
</body>
</html>