Update: Fixed a minor bug when dealing with transparent sources.
Source code and download after the jump.
Here is the pbk source code for the filter:
- kernel ReflectionFilter
- < namespace : "com.squidder";
- vendor : "Jon Reiling / Squidder.com";
- version : 1;
- description : "Filter for creating an apple-like reflection.";
- >
- {
- input image4 src;
- output pixel4 dst;
- parameter float imageheight
- <
- minValue: 0.0;
- maxValue : 1000.0;
- defaultValue : 300.0;
- >;
- parameter float fadeheight
- <
- minValue : 0.0;
- maxValue: 1000.0;
- defaultValue: 100.0;
- >;
- parameter float fadealpha
- <
- minValue : 0.0;
- maxValue : 1.0;
- defaultValue : 0.5;
- >;
- void
- evaluatePixel()
- {
- float2 coord = outCoord();
- if ( coord[ 1 ] < imageheight ) {
- dst = sampleNearest(src, coord );
- } else {
- float alpha = 1.0 - ( coord[ 1 ] - imageheight ) / fadeheight;
- coord[ 1 ] = imageheight - ( coord[ 1 ] - imageheight );
- dst = sampleNearest( src, coord );
- alpha *= fadealpha;
- dst.a *= alpha;
- dst.r *= alpha;
- dst.b *= alpha;
- dst.g *= alpha;
- }
- }
- }
As you may be able to tell, there are two unfortunate aspects to this filter. The first is that you have to manually pass in the height of the image. I have yet to figure out how to get the overall height of the image being passed in -- if anyone knows, please hit us up in the comments!
Secondly, if you try this out, you'll see that the reflection actually overwrites the bottom part of the source image. This is because, as far as I can tell, you can't add height to your output. So to counter this, you'll need to artificially add height to your target clip. You can do this easily by adding a transparent shape the height of your reflection. Again, if you have any notions on this, please drop a comment.
Finally, here is the flash code to read in and apply the filter:
- var reflectionHeight : int = 50;
- var urlLoader : URLLoader = new URLLoader();
- urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
- urlLoader.addEventListener( Event.COMPLETE , _applyFilter );
- urlLoader.load( new URLRequest( "ReflectionFilter.pbj" ) );
- function _applyFilter( event : Event ) : void {
- var shader:Shader = new Shader( URLLoader( event.target ).data );
- shader.data.imageheight.value = [ target.height - reflectionHeight ];
- shader.data.fadeheight.value = [ reflectionHeight ];
- shader.data.fadealpha.value = [ 0.4 ];
- var filter : ShaderFilter = new ShaderFilter( shader );
- target.filters = [ filter ];
- }

Leave a comment