flash on 2011-8-15
/**
* Copyright bongiovi015 ( http://wonderfl.net/user/bongiovi015 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/o6Xf
*/
package
{
import com.bit101.components.HSlider;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Shader;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.ShaderFilter;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
public class WonderPixelateMap extends Sprite
{
public var loaderImage : Loader = new Loader;
public var loaderShader : URLLoader = new URLLoader;
public var bmp : Bitmap;
public var shader : Shader = new Shader;
private var slider:HSlider;
public function WonderPixelateMap(){
graphics.beginFill(0, 1);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
loaderImage.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
loaderImage.load(new URLRequest("http://www.bongiovi.tw/wonderfl/mood.jpg"), new LoaderContext(true));
}
private function imgLoaded(e:Event) : void {
bmp = Bitmap(loaderImage.content);
addChild(bmp).y = ( 465 - bmp.height ) * .5;
loaderShader.addEventListener(Event.COMPLETE, shaderLoaded);
loaderShader.dataFormat = URLLoaderDataFormat.BINARY;
loaderShader.load(new URLRequest("http://www.bongiovi.tw/wonderfl/PixelateMap.pbj"));
}
private function shaderLoaded(e:Event) : void {
shader.byteCode = ByteArray(loaderShader.data);
slider = new HSlider(this, 5, 5, render);
slider.value = 50;
render();
}
public function render(e:Event=null) : void {
shader.data.span.value = [slider.value/100 * 15 + 5];
bmp.filters = [new ShaderFilter(shader)];
}
}
}
/*
<languageVersion : 1.0;>
kernel PixelateMap
< namespace : "bongiovi.tw";
vendor : "bongiovi";
version : 1;
description : "Pixelate with Brightness";
>
{
input image4 src;
output pixel4 dst;
parameter float span
<
minValue:1.0;
maxValue:100.0;
defaultValue:15.0;
>;
#define MAX_LENGTH length(float3(1.0, 1.0, 1.0));
#define BLACK float4(0.0, 0.0, 0.0, 1.0);
void
evaluatePixel()
{
float2 posOrg = outCoord();
float2 pos = floor(posOrg/span) * span;
float2 posCenter = pos+float2(span/2.0, span/2.0);
float4 color0 = sampleLinear(src, pos);
float4 color1 = sampleLinear(src, pos+float2(span, 0));
float4 color2 = sampleLinear(src, pos+float2(0, span));
float4 color3 = sampleLinear(src, pos+float2(span, span));
float4 color4 = sampleLinear(src, posCenter);
float4 color = (color0 + color1 + color2 + color3 + color4) / 5.0;
float range = length(color.rgb) / MAX_LENGTH;
if(distance(posOrg, posCenter) < range*span*.5) {
dst = float4(range, range, range, 1.0);
}else {
dst = BLACK;
}
}
}
*/