package { import flare.basic.*; import flare.core.*; import flare.flsl.*; import flare.materials.*; import flare.materials.filters.*; import flare.primitives.*; import flare.system.*; import flare.utils.*; import flash.display.*; import flash.events.*; import flash.geom.*; import flash.utils.*; /** * @author Ariel Nehmad */ public class Test61_LightScattering extends Sprite { [Embed(source = "../bin/geoSphere.zf3d", mimeType = "application/octet-stream")] private var GeoSphere:Class; [Embed(source = "../bin/radialBlur.flsl.compiled", mimeType = "application/octet-stream")] private var RadialBlur:Class; [Embed(source = "../bin/blurAndBlend.flsl.compiled", mimeType = "application/octet-stream")] private var Blur:Class; private var scene:Scene3D; private var sphere:Sphere; private var radialBlur:FLSLMaterial; private var blur:FLSLMaterial; private var texture:Texture3D; private var target:Texture3D; public function Test61_LightScattering() { scene = new Viewer3D( this, null, 0.2 ); scene.backgroundColor = 0x000000; scene.autoResize = true; texture = new Texture3D( new Point( 512, 512 ) ); texture.upload( scene ); target = new Texture3D( new Point( 512, 512 ) ); target.upload( scene ); blur = new FLSLMaterial( "blur", new Blur ); radialBlur = new FLSLMaterial( "radialBlur", new RadialBlur ); sphere = new Sphere( "light", 30, 24, new Shader3D( "", [new ColorFilter(0xffc0a0)], false ) ); scene.addChildFromFile( new GeoSphere ); scene.addEventListener( Scene3D.RENDER_EVENT, renderEvent ); } private function renderEvent(e:Event):void { scene.camera.rotateY( 0.2, false, Vector3DUtils.ZERO ); // cancel the default render. e.preventDefault(); // renders the lights mask into the texture. scene.context.setRenderToTexture( texture.texture, true ); scene.context.clear(); // we draw into the texture without any color, just to store depth buffer values. scene.context.setColorMask( false, false, false, false ); scene.render(); // then we render the colored spheres which will be the light sources. scene.context.setColorMask( true, true, true, true ); drawSpheres(); // apply the radial blur on the resulted texture. radialBlur.params.inputTexture.value = texture; radialBlur.params.outputTexture.value = target; scene.drawQuadTexture( null, 0, 0, stage.stageWidth, stage.stageHeight, radialBlur ); blur.params.inputTexture.value = target; blur.params.outputTexture.value = texture; scene.drawQuadTexture( null, 0, 0, stage.stageWidth, stage.stageHeight, blur ); // renders the scene normally. scene.context.setRenderToBackBuffer(); scene.render(); drawSpheres(); scene.drawQuadTexture( texture, 0, 0, stage.stageWidth, stage.stageHeight, null, "one", "one" ); } private function drawSpheres():void { var timer:Number = getTimer(); var scale:Number; sphere.x = Math.sin( timer / 800 ) * 40; sphere.y = Math.cos( timer / 450 ) * 40; sphere.draw(); sphere.x = -Math.cos( timer / 400 ) * 40; sphere.y = -Math.sin( timer / 760 ) * 40; sphere.draw(); } } }