123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing manual msaa</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 {
- margin: 0px;
- background-color: #000;
- overflow: hidden;
- font-family:Monospace;
- font-size:13px;
- margin: 0px;
- text-align:center;
- overflow: hidden;
- }
- a { color: #88f; }
- #info {
- color: #fff;
- position: absolute;
- top: 10px;
- width: 100%;
- text-align: center;
- display:block;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="http://threejs.org" target="_blank">three.js</a> - Unbiased Manual Multi-Sample Anti-Aliasing (MSAA) pass by <a href="https://clara.io" target="_blank">Ben Houston</a><br/><br/>
- This example shows how to unbias the rounding errors accumulated using high number of MSAA samples on a 8-bit per channel buffer.<br/><br/>
- Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors.
- </div>
- <div id="container"></div>
- <script src="../build/three.js"></script>
- <script src="js/libs/stats.min.js"></script>
- <script src="js/libs/dat.gui.min.js"></script>
- <script src="js/shaders/CopyShader.js"></script>
- <script src="js/postprocessing/EffectComposer.js"></script>
- <script src="js/postprocessing/ManualMSAARenderPass.js"></script>
- <script src="js/postprocessing/RenderPass.js"></script>
- <script src="js/postprocessing/MaskPass.js"></script>
- <script src="js/postprocessing/ShaderPass.js"></script>
- <script>
- var scene, renderer, composer, copyPass;
- var cameraP, msaaRenderPassP;
- var cameraO, msaaRenderPassO;
- var gui, stats, texture;
- var params = {
- sampleLevel: 4,
- renderToScreen: false,
- unbiased: true,
- camera: 'perspective',
- clearColor: 'black',
- clearAlpha: 1.0,
- autoRotate: true
-
- };
- init();
- animate();
- clearGui();
- function clearGui() {
- if ( gui ) gui.destroy();
- gui = new dat.GUI();
- gui.add( params, "unbiased" );
- gui.add( params, "renderToScreen" );
- gui.add( params, 'sampleLevel', {
- 'Level 0: 1 Sample': 0,
- 'Level 1: 2 Samples': 1,
- 'Level 2: 4 Samples': 2,
- 'Level 3: 8 Samples': 3,
- 'Level 4: 16 Samples': 4,
- 'Level 5: 32 Samples': 5
- } );
- gui.add( params, 'camera', [ 'perspective', 'orthographic' ] );
- gui.add( params, "clearColor", [ 'black', 'white', 'blue', 'green', 'red' ] );
- gui.add( params, "clearAlpha", 0, 1 );
- gui.add( params, "autoRotate" );
-
- gui.open();
- }
- function init() {
- container = document.getElementById( "container" );
- var width = window.innerWidth || 1;
- var height = window.innerHeight || 1;
- var aspect = width / height;
- var devicePixelRatio = window.devicePixelRatio || 1;
- renderer = new THREE.WebGLRenderer( { antialias: false } );
- renderer.setPixelRatio( devicePixelRatio );
- renderer.setSize( width, height );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- cameraP = new THREE.PerspectiveCamera( 65, aspect, 3, 10 );
- cameraP.position.z = 7;
- cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 3, 10 );
- cameraO.position.z = 7;
- var fov = THREE.Math.degToRad( cameraP.fov );
- var hyperfocus = ( cameraP.near + cameraP.far ) / 2;
- var _height = 2 * Math.tan( fov / 2 ) * hyperfocus;
- cameraO.zoom = height / _height;
- scene = new THREE.Scene();
- group = new THREE.Object3D();
- scene.add( group );
- var light = new THREE.PointLight( 0xddffdd, 1.0 );
- light.position.z = 70;
- light.position.y = -70;
- light.position.x = -70;
- scene.add( light );
- var light2 = new THREE.PointLight( 0xffdddd, 1.0 );
- light2.position.z = 70;
- light2.position.x = -70;
- light2.position.y = 70;
- scene.add( light2 );
- var light3 = new THREE.PointLight( 0xddddff, 1.0 );
- light3.position.z = 70;
- light3.position.x = 70;
- light3.position.y = -70;
- scene.add( light3 );
- var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
- scene.add( light3 );
- var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
- for ( var i = 0; i < 120; i ++ ) {
- var material = new THREE.MeshStandardMaterial();
- material.roughness = 0.5 * Math.random() + 0.25;
- material.metalness = 0;
- material.color.setHSL( Math.random(), 1.0, 0.3 );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = Math.random() * 4 - 2;
- mesh.position.y = Math.random() * 4 - 2;
- mesh.position.z = Math.random() * 4 - 2;
- 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() * 0.2 + 0.05;
- group.add( mesh );
- }
- // postprocessing
- composer = new THREE.EffectComposer( renderer );
- msaaRenderPassP = new THREE.ManualMSAARenderPass( scene, cameraP );
- composer.addPass( msaaRenderPassP );
- msaaRenderPassO = new THREE.ManualMSAARenderPass( scene, cameraO );
- composer.addPass( msaaRenderPassO );
- copyPass = new THREE.ShaderPass( THREE.CopyShader );
- copyPass.renderToScreen = true;
- composer.addPass( copyPass );
-
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- var width = window.innerWidth;
- var height = window.innerHeight;
- var aspect = width / height;
- cameraP.aspect = aspect;
- cameraO.updateProjectionMatrix();
- cameraO.left = - height * aspect;
- cameraO.right = height * aspect;
- cameraO.top = height;
- cameraO.bottom = - height;
- cameraO.updateProjectionMatrix();
- renderer.setSize( width, height );
- var pixelRatio = renderer.getPixelRatio();
- var newWidth = Math.floor( width / pixelRatio ) || 1;
- var newHeight = Math.floor( height / pixelRatio ) || 1;
- composer.setSize( newWidth, newHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- stats.begin();
- if( params.autoRotate ) {
- for ( var i = 0; i < scene.children.length; i ++ ) {
- var child = scene.children[ i ];
- child.rotation.x += 0.005;
- child.rotation.y += 0.01;
- }
- }
- var newColor = msaaRenderPassP.clearColor;
- switch( params.clearColor ) {
- case 'blue': newColor = 0x0000ff; break;
- case 'red': newColor = 0xff0000; break;
- case 'green': newColor = 0x00ff00; break;
- case 'white': newColor = 0xffffff; break;
- case 'black': newColor = 0x000000; break;
- }
- msaaRenderPassP.clearColor = msaaRenderPassO.clearColor = newColor;
- msaaRenderPassP.clearAlpha = msaaRenderPassO.clearAlpha = params.clearAlpha;
- msaaRenderPassP.sampleLevel = msaaRenderPassO.sampleLevel = params.sampleLevel;
- msaaRenderPassP.unbiased = msaaRenderPassO.unbiased = params.unbiased;
- msaaRenderPassP.enabled = ( params.camera === 'perspective' );
- msaaRenderPassO.enabled = ( params.camera === 'orthographic' );
- msaaRenderPassP.renderToScreen = msaaRenderPassO.renderToScreen = params.renderToScreen;
- copyPass.enabled = !params.renderToScreen;
- composer.render();
- stats.end();
- }
- </script>
- <div>
- </body>
- </html>
|