I found the FXAA post-processing shader effect in the THREE.js library, and it worked like a charm to smooth the rough edges. However, the THREE.EffectComposer utility doesn't automatically handle different pixel densities, and by default, the aliasing actually became twice as bad on the Retina screen of my Mac. After some fiddling, I found that you simply have to adjust the uniforms for the shader effect if it depends on knowing your screen size, as well as set the screen size for the EffectsComposer object.
See below, where I detect the pixel density, and use that to multiply your screen dimensions in the shader and EffectComposer:
var composer, dpr, effectFXAA, renderScene; dpr = 1; if (window.devicePixelRatio !== undefined) { dpr = window.devicePixelRatio; } renderScene = new THREE.RenderPass(scene, camera); effectFXAA = new THREE.ShaderPass(THREE.FXAAShader); effectFXAA.uniforms['resolution'].value.set(1 / (window.innerWidth * dpr), 1 / (window.innerHeight * dpr)); effectFXAA.renderToScreen = true; composer = new THREE.EffectComposer(renderer); composer.setSize(window.innerWidth * dpr, window.innerHeight * dpr); composer.addPass(renderScene); composer.addPass(effectFXAA);You'll also probably want to update these settings if the window size changes, like so:
$(window).on('resize', onWindowResize); function onWindowResize(e) { effectFXAA.uniforms['resolution'].value.set(1 / (window.innerWidth * dpr), 1 / (window.innerHeight * dpr)); composer.setSize(window.innerWidth * dpr, window.innerHeight * dpr); }
Thanks, very helpful. This didn't use to be necessary, but sometime in the last couple of years, a default setting in Three.js must have changed. Now I know how to fix it. :-)
ReplyDeletePerhaps you should mention that you need to include the following scripts...
ReplyDeletesrc="/threejs/examples/js/postprocessing/EffectComposer.js"
src="/threejs/examples/js/postprocessing/RenderPass.js"
src="/threejs/examples/js/postprocessing/MaskPass.js"
src="/threejs/examples/js/postprocessing/ShaderPass.js"
src="/threejs/examples/js/shaders/FXAAShader.js"
src="/threejs/examples/js/shaders/CopyShader.js"
And call composer.render() in the main animation loop.
Brilliant! Thanks for the tips.
ReplyDelete