|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgl - postprocessing manual msaa</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + margin: 0px; |
| 10 | + background-color: #000; |
| 11 | + overflow: hidden; |
| 12 | + font-family:Monospace; |
| 13 | + font-size:13px; |
| 14 | + margin: 0px; |
| 15 | + text-align:center; |
| 16 | + overflow: hidden; |
| 17 | + } |
| 18 | + |
| 19 | + #info { |
| 20 | + color: #fff; |
| 21 | + position: absolute; |
| 22 | + top: 10px; |
| 23 | + width: 100%; |
| 24 | + text-align: center; |
| 25 | + display:block; |
| 26 | + } |
| 27 | + </style> |
| 28 | + </head> |
| 29 | + <body> |
| 30 | + <div id="info"> |
| 31 | + <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/> |
| 32 | + 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/> |
| 33 | + Turn off the "unbiased" feature to see the banding that results from accumulated rounding errors. |
| 34 | + </div> |
| 35 | + |
| 36 | + <div id="container"></div> |
| 37 | + |
| 38 | + <script src="../build/three.js"></script> |
| 39 | + <script src="js/libs/stats.min.js"></script> |
| 40 | + <script src="js/libs/dat.gui.min.js"></script> |
| 41 | + |
| 42 | + <script src="js/shaders/CopyShader.js"></script> |
| 43 | + |
| 44 | + <script src="js/postprocessing/EffectComposer.js"></script> |
| 45 | + <script src="js/postprocessing/ManualMSAARenderPass.js"></script> |
| 46 | + <script src="js/postprocessing/RenderPass.js"></script> |
| 47 | + <script src="js/postprocessing/MaskPass.js"></script> |
| 48 | + <script src="js/postprocessing/ShaderPass.js"></script> |
| 49 | + |
| 50 | + |
| 51 | + <script> |
| 52 | + |
| 53 | + var camera, scene, renderer, composer, copyPass, msaaRenderPass; |
| 54 | + var gui, stats, texture; |
| 55 | + |
| 56 | + var param = { |
| 57 | + sampleLevel: 4, |
| 58 | + unbiased: true |
| 59 | + }; |
| 60 | + |
| 61 | + init(); |
| 62 | + animate(); |
| 63 | + |
| 64 | + clearGui(); |
| 65 | + |
| 66 | + function clearGui() { |
| 67 | + |
| 68 | + if ( gui ) gui.destroy(); |
| 69 | + |
| 70 | + gui = new dat.GUI(); |
| 71 | + |
| 72 | + gui.add( param, "unbiased" ); |
| 73 | + gui.add( param, 'sampleLevel', { |
| 74 | + 'Level 0: 1 Sample': 0, |
| 75 | + 'Level 1: 2 Samples': 1, |
| 76 | + 'Level 2: 4 Samples': 2, |
| 77 | + 'Level 3: 8 Samples': 3, |
| 78 | + 'Level 4: 16 Samples': 4, |
| 79 | + 'Level 5: 32 Samples': 5 |
| 80 | + } ); |
| 81 | + |
| 82 | + gui.open(); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + function init() { |
| 87 | + |
| 88 | + container = document.getElementById( "container" ); |
| 89 | + |
| 90 | + var width = window.innerWidth || 1; |
| 91 | + var height = window.innerHeight || 1; |
| 92 | + var devicePixelRatio = window.devicePixelRatio || 1; |
| 93 | + |
| 94 | + renderer = new THREE.WebGLRenderer( { antialias: false } ); |
| 95 | + renderer.setPixelRatio( devicePixelRatio ); |
| 96 | + renderer.setSize( width, height ); |
| 97 | + document.body.appendChild( renderer.domElement ); |
| 98 | + |
| 99 | + stats = new Stats(); |
| 100 | + container.appendChild( stats.dom ); |
| 101 | + |
| 102 | + // |
| 103 | + |
| 104 | + camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 ); |
| 105 | + camera.position.z = 7; |
| 106 | + |
| 107 | + scene = new THREE.Scene(); |
| 108 | + |
| 109 | + group = new THREE.Object3D(); |
| 110 | + scene.add( group ); |
| 111 | + |
| 112 | + var light = new THREE.PointLight( 0xddffdd, 1.0 ); |
| 113 | + light.position.z = 70; |
| 114 | + light.position.y = -70; |
| 115 | + light.position.x = -70; |
| 116 | + scene.add( light ); |
| 117 | + |
| 118 | + var light2 = new THREE.PointLight( 0xffdddd, 1.0 ); |
| 119 | + light2.position.z = 70; |
| 120 | + light2.position.x = -70; |
| 121 | + light2.position.y = 70; |
| 122 | + scene.add( light2 ); |
| 123 | + |
| 124 | + var light3 = new THREE.PointLight( 0xddddff, 1.0 ); |
| 125 | + light3.position.z = 70; |
| 126 | + light3.position.x = 70; |
| 127 | + light3.position.y = -70; |
| 128 | + scene.add( light3 ); |
| 129 | + |
| 130 | + var light3 = new THREE.AmbientLight( 0xffffff, 0.05 ); |
| 131 | + scene.add( light3 ); |
| 132 | + |
| 133 | + var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 ); |
| 134 | + for ( var i = 0; i < 120; i ++ ) { |
| 135 | + |
| 136 | + var material = new THREE.MeshStandardMaterial(); |
| 137 | + material.roughness = 0.5 * Math.random() + 0.25; |
| 138 | + material.metalness = 0; |
| 139 | + material.color.setHSL( Math.random(), 1.0, 0.3 ); |
| 140 | + |
| 141 | + var mesh = new THREE.Mesh( geometry, material ); |
| 142 | + mesh.position.x = Math.random() * 4 - 2; |
| 143 | + mesh.position.y = Math.random() * 4 - 2; |
| 144 | + mesh.position.z = Math.random() * 4 - 2; |
| 145 | + mesh.rotation.x = Math.random(); |
| 146 | + mesh.rotation.y = Math.random(); |
| 147 | + mesh.rotation.z = Math.random(); |
| 148 | + |
| 149 | + mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05; |
| 150 | + group.add( mesh ); |
| 151 | + } |
| 152 | + |
| 153 | + // postprocessing |
| 154 | + |
| 155 | + composer = new THREE.EffectComposer( renderer ); |
| 156 | + |
| 157 | + msaaRenderPass = new THREE.ManualMSAARenderPass( scene, camera ); |
| 158 | + composer.addPass( msaaRenderPass ); |
| 159 | + |
| 160 | + copyPass = new THREE.ShaderPass( THREE.CopyShader ); |
| 161 | + copyPass.renderToScreen = true; |
| 162 | + composer.addPass( copyPass ); |
| 163 | + |
| 164 | + window.addEventListener( 'resize', onWindowResize, false ); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + function onWindowResize() { |
| 169 | + |
| 170 | + var width = window.innerWidth; |
| 171 | + var height = window.innerHeight; |
| 172 | + |
| 173 | + camera.aspect = width / height; |
| 174 | + camera.updateProjectionMatrix(); |
| 175 | + |
| 176 | + renderer.setSize( width, height ); |
| 177 | + |
| 178 | + var pixelRatio = renderer.getPixelRatio(); |
| 179 | + var newWidth = Math.floor( width / pixelRatio ) || 1; |
| 180 | + var newHeight = Math.floor( height / pixelRatio ) || 1; |
| 181 | + composer.setSize( newWidth, newHeight ); |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + function animate() { |
| 186 | + |
| 187 | + requestAnimationFrame( animate ); |
| 188 | + |
| 189 | + stats.begin(); |
| 190 | + |
| 191 | + for ( var i = 0; i < scene.children.length; i ++ ) { |
| 192 | + |
| 193 | + var child = scene.children[ i ]; |
| 194 | + |
| 195 | + child.rotation.x += 0.005; |
| 196 | + child.rotation.y += 0.01; |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + msaaRenderPass.sampleLevel = param.sampleLevel; |
| 201 | + msaaRenderPass.unbiased = param.unbiased; |
| 202 | + |
| 203 | + composer.render(); |
| 204 | + stats.end(); |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + </script> |
| 209 | + <div> |
| 210 | + </body> |
| 211 | +</html> |
0 commit comments