|
@@ -0,0 +1,226 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+
|
|
|
+<!--Reference:
|
|
|
+SSAO algo: http://devlog-martinsh.blogspot.tw/2011/12/ssao-shader-update-v12.html?showComment=1398158188712#c1563204765906693531
|
|
|
+log depth http://outerra.blogspot.tw/2013/07/logarithmic-depth-buffer-optimizations.html
|
|
|
+convert the exponential depth to a linear value: http://www.ozone3d.net/blogs/lab/20090206/how-to-linearize-the-depth-value/
|
|
|
+Spiral sampling http://web.archive.org/web/20120421191837/http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere-->
|
|
|
+
|
|
|
+<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:#0078ff;
|
|
|
+ }
|
|
|
+
|
|
|
+ #info {
|
|
|
+ color:#fff;
|
|
|
+ position: relative;
|
|
|
+ top: 0px;
|
|
|
+ width: 50em;
|
|
|
+ margin: 0 auto -2.1em;
|
|
|
+ padding: 5px;
|
|
|
+ z-index:100;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+ <script src="../build/three.js"></script>
|
|
|
+ <script src="js/shaders/SSAOShader.js"></script>
|
|
|
+ <script src="js/Detector.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">three.js</a> - webgl screen space ambient occlusion example -
|
|
|
+ shader by <a href="http://alteredqualia.com">alteredq</a>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <script>
|
|
|
+
|
|
|
+ if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
|
|
|
+
|
|
|
+ var container, stats;
|
|
|
+ var camera, scene, renderer,
|
|
|
+ depthMaterial;
|
|
|
+ var group;
|
|
|
+ var depthScale = 1.0;
|
|
|
+ var height = window.innerHeight;
|
|
|
+ var postprocessing = { enabled : true, renderMode: 'framebuffer' }; // renderMode: 'framebuffer', 'onlyAO'
|
|
|
+
|
|
|
+ init();
|
|
|
+ animate();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ container = document.createElement( 'div' );
|
|
|
+ document.body.appendChild( container );
|
|
|
+
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: false } );
|
|
|
+ renderer.setClearColor( 0xa0a0a0 );
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
+ var depthShader = THREE.ShaderLib[ "depthRGBA" ];
|
|
|
+ var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
|
|
|
+
|
|
|
+ depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader,
|
|
|
+ uniforms: depthUniforms, blending: THREE.NoBlending } );
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 100, 700 );
|
|
|
+ camera.position.z = 500;
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ group = new THREE.Object3D();
|
|
|
+ scene.add( group );
|
|
|
+
|
|
|
+ var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
|
|
|
+ directionalLight.position.set( 2, 1.2, 10 ).normalize();
|
|
|
+ scene.add( directionalLight );
|
|
|
+
|
|
|
+ var geometry = new THREE.SphereBufferGeometry(5, 100, 100); //new THREE.BoxGeometry( 10, 10, 10 );
|
|
|
+ for ( var i = 0; i < 100; i ++ ) {
|
|
|
+
|
|
|
+ //var material = new THREE.MeshLambertMaterial({color: 0xaa0000}); //new
|
|
|
+ var material = new THREE.MeshBasicMaterial();
|
|
|
+ material.color.r = Math.random();
|
|
|
+ material.color.g = Math.random();
|
|
|
+ material.color.b = Math.random();
|
|
|
+
|
|
|
+ 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.x = mesh.scale.y = mesh.scale.z = Math.random() * 10 + 1;
|
|
|
+ group.add( mesh );
|
|
|
+ }
|
|
|
+
|
|
|
+ stats = new Stats();
|
|
|
+ stats.domElement.style.position = 'absolute';
|
|
|
+ stats.domElement.style.top = '0px';
|
|
|
+ container.appendChild( stats.domElement );
|
|
|
+
|
|
|
+ // postprocessing
|
|
|
+ initPostprocessing();
|
|
|
+
|
|
|
+ // Init gui
|
|
|
+ var gui = new dat.GUI();
|
|
|
+ gui.add( postprocessing, "enabled" ).onChange();
|
|
|
+ // gui.add( postprocessing, "type" ).onChange( postprocessChange );
|
|
|
+ gui.add( postprocessing, "renderMode", { framebuffer: 0, onlyAO: 1 } ).onChange( renderModeChange ).listen();
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize, false );
|
|
|
+ }
|
|
|
+
|
|
|
+ function renderModeChange( value ) {
|
|
|
+
|
|
|
+ if ( value == 0 ) { // framebuffer
|
|
|
+ postprocessing.ssao_uniforms[ 'onlyAO' ].value = false;
|
|
|
+ } else if ( value == 1 ) { // onlyAO
|
|
|
+ postprocessing.ssao_uniforms[ 'onlyAO' ].value = true;
|
|
|
+ } else {
|
|
|
+ console.error( "Not define renderModeChange type: " + value );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ }
|
|
|
+
|
|
|
+ function initPostprocessing() {
|
|
|
+
|
|
|
+ postprocessing.scene = new THREE.Scene();
|
|
|
+ postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -1000, 1000 );
|
|
|
+ postprocessing.camera.position.z = 1;
|
|
|
+
|
|
|
+ postprocessing.scene.add( postprocessing.camera );
|
|
|
+
|
|
|
+ var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter };
|
|
|
+ postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth * depthScale, height * depthScale, pars );
|
|
|
+ postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, height, pars );
|
|
|
+
|
|
|
+ // Setup SSAO material
|
|
|
+ var ssao_shader = THREE.SSAOShader;
|
|
|
+ postprocessing.ssao_uniforms = THREE.UniformsUtils.clone( ssao_shader.uniforms );
|
|
|
+ postprocessing.ssao_uniforms[ "tDiffuse" ].value = postprocessing.rtTextureColor;
|
|
|
+ postprocessing.ssao_uniforms[ "tDepth" ].value = postprocessing.rtTextureDepth;
|
|
|
+ postprocessing.ssao_uniforms[ 'size' ].value.set( window.innerWidth*depthScale, window.innerHeight*depthScale );
|
|
|
+ postprocessing.ssao_uniforms[ 'cameraNear' ].value = camera.near;
|
|
|
+ postprocessing.ssao_uniforms[ 'cameraFar' ].value = camera.far;
|
|
|
+ postprocessing.ssao_uniforms[ 'onlyAO' ].value = false;
|
|
|
+
|
|
|
+ postprocessing.materialSSAO = new THREE.ShaderMaterial( {
|
|
|
+
|
|
|
+ uniforms: postprocessing.ssao_uniforms,
|
|
|
+ vertexShader: ssao_shader.vertexShader,
|
|
|
+ fragmentShader: ssao_shader.fragmentShader
|
|
|
+ });
|
|
|
+
|
|
|
+ postprocessing.ssaoQuad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialSSAO );
|
|
|
+ postprocessing.ssaoQuad.position.z = -500;
|
|
|
+ postprocessing.scene.add( postprocessing.ssaoQuad );
|
|
|
+ }
|
|
|
+
|
|
|
+ function shaderUpdate() {
|
|
|
+ postprocessing.materialSSAO.needsUpdate = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+ requestAnimationFrame( animate );
|
|
|
+
|
|
|
+ render();
|
|
|
+ stats.update();
|
|
|
+ }
|
|
|
+
|
|
|
+ function render() {
|
|
|
+ var timer = performance.now();
|
|
|
+ group.rotation.x = timer * 0.0002;
|
|
|
+ group.rotation.y = timer * 0.0001;
|
|
|
+
|
|
|
+ if ( postprocessing.enabled ) {
|
|
|
+
|
|
|
+ renderer.clear();
|
|
|
+
|
|
|
+ // Render scene into texture
|
|
|
+ scene.overrideMaterial = null;
|
|
|
+ renderer.render( scene, camera, postprocessing.rtTextureColor , true );
|
|
|
+
|
|
|
+ // Render depth into texture
|
|
|
+ scene.overrideMaterial = depthMaterial;
|
|
|
+ renderer.render( scene, camera, postprocessing.rtTextureDepth, true );
|
|
|
+
|
|
|
+ // Render SSAO composite
|
|
|
+ renderer.render( postprocessing.scene, postprocessing.camera );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ scene.overrideMaterial = null;
|
|
|
+ renderer.clear();
|
|
|
+ renderer.render( scene, camera );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|