123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <!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">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #aaa;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://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 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 { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { SSAOPass } from './jsm/postprocessing/SSAOPass.js';
- var container, stats;
- var camera, scene, renderer;
- var composer;
- 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;
- composer = new EffectComposer( renderer );
- var ssaoPass = new SSAOPass( scene, camera, width, height );
- ssaoPass.kernelRadius = 16;
- composer.addPass( ssaoPass );
- // Init gui
- var gui = new GUI();
- gui.add( ssaoPass, 'output', {
- 'Default': SSAOPass.OUTPUT.Default,
- 'SSAO Only': SSAOPass.OUTPUT.SSAO,
- 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
- 'Beauty': SSAOPass.OUTPUT.Beauty,
- 'Depth': SSAOPass.OUTPUT.Depth,
- 'Normal': 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 );
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- renderer.setSize( width, height );
- composer.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;
- composer.render();
- }
- </script>
- </body>
- </html>
|