forked from: Pixel Bender (液晶ディスプレイ風フィルタ)

by jmbyh521 forked from Pixel Bender (液晶ディスプレイ風フィルタ) (diff: 1)
-------------------------------------------------

液晶ディスプレイ風フィルタ

FULL SCREEN 表示にしてブラウザのサイズを変えても
Flash スクリーンのライン表示が維持されている事が確認できます。

矩形中央の値を取得するように変更しました。
外部から動画を読み込めるように修正しました。
-------------------------------------------------
♥0 | Line 329 | Modified 2015-09-18 15:04:19 | MIT License
play

ActionScript3 source code

/**
 * Copyright jmbyh521 ( http://wonderfl.net/user/jmbyh521 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/WoQB
 */

// forked from Hakuhin's Pixel Bender (液晶ディスプレイ風フィルタ)
// -------------------------------------------------
//
// 液晶ディスプレイ風フィルタ
//
//
// FULL SCREEN 表示にしてブラウザのサイズを変えても
// Flash スクリーンのライン表示が維持されている事が確認できます。
//
// 矩形中央の値を取得するように変更しました。
// 外部から動画を読み込めるように修正しました。
// -------------------------------------------------
package {
    import flash.events.*;
    import flash.display.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
    import flash.system.*;
    import flash.geom.*;
    import flash.filters.*;
    import flash.ui.*;
    import flash.media.*;

    public class Main extends Sprite {
        public function Main() {



// -------------------------------------------------
// コンストラクタ
// -------------------------------------------------

// 動画のURL
var movie_url:String = "http://actionscript.web.officelive.com/wonderfl/movie000.mp4";

// 100%表示
stage.scaleMode = StageScaleMode.NO_SCALE;

// 左上
stage.align = StageAlign.TOP_LEFT;
stage.align = "TL";

// ビデオを配置
var video_obj : Video = new Video();
addChild(video_obj);

// ストリーム
var connection:NetConnection = new NetConnection();
connection.connect(null);    
var netStream:NetStream = new NetStream(connection);

// サウンドをOFF
var trans : SoundTransform = netStream.soundTransform;
trans.volume = 0.0;
netStream.soundTransform = trans;

// ビデオとストリームを関連付け
video_obj.attachNetStream(netStream);

// メタデータ取得
var obj:Object = new Object();
obj.onMetaData = function(param:Object):void{
}
netStream.client = obj;

// リザルト作成
var result : Sprite = new Sprite();
addChild(result);
result.x = 0;

// ボタン作成
var button:Button = new Button(stage);
button.y = 10;
button.setSize(60,20);
button.setLabel("開く");
result.addChild(button);

// ボタンが押された
button.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
    load(text.text);
});

// テキストフィールド作成
var text:TextField = new TextField();
text.x = 10;
text.y = 10;
text.height = 20;
text.type = TextFieldType.INPUT;
text.border = true;

// 書式
var format:TextFormat = new TextFormat();
format.font = "MS ゴシック";    // フォント名
text.defaultTextFormat = format;

// テキスト表示
text.text = movie_url;
result.addChild(text);

// 初期化
init();

// リソース読み込み開始
load(movie_url);


// -------------------------------------------------
// 読み込み
// -------------------------------------------------
function load(url:String):void{

    // 再生開始
    netStream.play(url);
}


// -------------------------------------------------
// 初期化
// -------------------------------------------------
function init():void{

    // ステージサイズ
    var w:uint;
    var h:uint;

    // リサイズ時にフィット
    stage.addEventListener(Event.RESIZE,ResizeFunc);
    function ResizeFunc(e:Event):void{
        w = stage.stageWidth;
        h = stage.stageHeight;
        video_obj.width = w;
        video_obj.height = h - 40;
        result.y = h - 40;
    
        // テキストフィールド位置
        text.width = w - 10 - 10 - button.width - 10;
    
        // ボタン位置
        button.x = w - button.width - 10;
    }
    ResizeFunc(null);

    // シェーダバイトコード
    var binary : ByteArray = CustomFilterGetByteArray();
    
    // シェーダ作成
    var shader:Shader = new Shader();
    shader.byteCode = binary;
    var filter:ShaderFilter = new ShaderFilter(shader); 

    // パラメータアクセス用
    var data : ShaderData  = shader.data;
    var param : ShaderParameter;

    // パラメータ
    var width:Number = 4;        // ドットの幅
    var height:Number = 4;        // ドットの高さ
    var interval_h:Number = 1;    // 水平方向のドット間隔
    var interval_v:Number = 1;    // 垂直方向のドット間隔
    var bits_per_pixel:Number = 4;    //  色深度
    
    addEventListener(Event.ENTER_FRAME,function(e:Event):void{

        //  ドットの幅
        param = data.width;
        param.value = [width];

        // ドットの高さ
        param = data.height;
        param.value = [height];

        // 水平方向のドット間隔
        param = data.interval_h;
        param.value = [interval_h];

        // 垂直方向のドット間隔
        param = data.interval_v;
        param.value = [interval_v];

        // 色深度
        param = data.bits_per_pixel;
        param.value = [bits_per_pixel];

        // 適応
        video_obj.filters = [filter];
    });

    // テキスト表示
    var tf : TextField = new TextField();
    tf.x = 5;
    tf.y = 5;
    tf.width = 300;
    tf.height = 70;
    tf.border = true;
    tf.background = true;
    tf.alpha = 0.9;
    
    // 書式
    var format : TextFormat = new TextFormat();
    format.font = "MS ゴシック";
    tf.defaultTextFormat = format;

    addChild(tf);
    addEventListener(Event.ENTER_FRAME,function(e:Event):void{
        var str:String = "";
        str += "ドットの幅     :" + width + " (上下キーで変更)\n";
        str += "ドットの高さ    :" + height + " (左右キーで変更)\n";
        str += "水平方向のドット間隔:" + interval_h + " (1,2キーで変更)\n";
        str += "垂直方向のドット間隔:" + interval_v + " (3,4キーで変更)\n";
        str += "色深度       :" + bits_per_pixel + " (5,6キーで変更)\n";

        tf.text = str;
    });
    
    
    // キー操作
    stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
    function KeyDown(event:KeyboardEvent):void{
        if(event.keyCode == Keyboard.UP)        height -= 1;
        if(event.keyCode == Keyboard.DOWN)        height += 1;
        if(event.keyCode == Keyboard.LEFT)        width -= 1;
        if(event.keyCode == Keyboard.RIGHT)        width += 1;
        if(event.keyCode == 49)        interval_h -= 1;
        if(event.keyCode == 50)        interval_h += 1;
        if(event.keyCode == 51)        interval_v -= 1;
        if(event.keyCode == 52)        interval_v += 1;
        if(event.keyCode == 53)        bits_per_pixel -= 1;
        if(event.keyCode == 54)        bits_per_pixel += 1;
        
        if(width < 1)                width = 1;
        if(height < 1)                height = 1;
        if(interval_h < 0)            interval_h = 0;
        if(interval_v < 0)            interval_v = 0;
        if(bits_per_pixel < 1)        bits_per_pixel = 1;
        if(bits_per_pixel > 255)    bits_per_pixel = 255;
    };

}


// -------------------------------------------------
// シェーダバイトコード
// -------------------------------------------------
function CustomFilterGetByteArray():ByteArray{

    var a:ByteArray = new ByteArray();
    var f:Function;

    f = a.writeUnsignedInt;
    f(0xa5010000); f(0x00a41a00); f(0x4c697175); f(0x69644372); 
    f(0x79737461); f(0x6c446973); f(0x706c6179); f(0x46696c74); 
    f(0x6572a00c); f(0x6e616d65); f(0x73706163); f(0x65006c69); 
    f(0x71756964); f(0x20637279); f(0x7374616c); f(0x20646973); 
    f(0x706c6179); f(0x00a00c76); f(0x656e646f); f(0x72004861); 
    f(0x6b756869); f(0x6e00a008); f(0x76657273); f(0x696f6e00); 
    f(0x0100a00c); f(0x64657363); f(0x72697074); f(0x696f6e00); 
    f(0x89748fbb); f(0x83668342); f(0x83588376); f(0x838c8343); 
    f(0x95978374); f(0x8342838b); f(0x835e00a1); f(0x01020000); 
    f(0x0c5f4f75); f(0x74436f6f); f(0x726400a3); f(0x00047372); 
    f(0x6300a102); f(0x0401000f); f(0x64737400); f(0xa1010100); 
    f(0x00027769); f(0x64746800); f(0xa2016d69); f(0x6e56616c); 
    f(0x7565003f); f(0x800000a2); f(0x01646566); f(0x61756c74); 
    f(0x56616c75); f(0x65004040); f(0x0000a20c); f(0x64657363); 
    f(0x72697074); f(0x696f6e00); f(0x83688362); f(0x836782cc); 
    f(0x959d00a1); f(0x01010000); f(0x01686569); f(0x67687400); 
    f(0xa2016d69); f(0x6e56616c); f(0x7565003f); f(0x800000a2); 
    f(0x01646566); f(0x61756c74); f(0x56616c75); f(0x65004040); 
    f(0x0000a20c); f(0x64657363); f(0x72697074); f(0x696f6e00); 
    f(0x83688362); f(0x836782cc); f(0x8d8282b3); f(0x00a10101); 
    f(0x02000869); f(0x6e746572); f(0x76616c5f); f(0x6800a201); 
    f(0x6d696e56); f(0x616c7565); f(0x00000000); f(0x00a20164); 
    f(0x65666175); f(0x6c745661); f(0x6c756500); f(0x3f800000); 
    f(0xa20c6465); f(0x73637269); f(0x7074696f); f(0x6e009085); 
    f(0x95bd95fb); f(0x8cfc82cc); f(0x83688362); f(0x83678ad4); 
    f(0x8a7500a1); f(0x01010200); f(0x04696e74); f(0x65727661); 
    f(0x6c5f7600); f(0xa2016d69); f(0x6e56616c); f(0x75650000); 
    f(0x000000a2); f(0x01646566); f(0x61756c74); f(0x56616c75); 
    f(0x65003f80); f(0x0000a20c); f(0x64657363); f(0x72697074); 
    f(0x696f6e00); f(0x908292bc); f(0x95fb8cfc); f(0x82cc8368); 
    f(0x83628367); f(0x8ad48a75); f(0x00a10104); f(0x03000f69); 
    f(0x6e746572); f(0x76616c5f); f(0x636f6c6f); f(0x7200a204); 
    f(0x64656661); f(0x756c7456); f(0x616c7565); f(0x00000000); 
    f(0x00000000); f(0x00000000); f(0x003f8000); f(0x00a20c64); 
    f(0x65736372); f(0x69707469); f(0x6f6e0083); f(0x68836283); 
    f(0x678ad48a); f(0x7582cc90); f(0x4600a101); f(0x01020002); 
    f(0x62697473); f(0x5f706572); f(0x5f706978); f(0x656c00a2); 
    f(0x016d696e); f(0x56616c75); f(0x65003f80); f(0x0000a201); 
    f(0x6d617856); f(0x616c7565); f(0x00438000); f(0x00a20164); 
    f(0x65666175); f(0x6c745661); f(0x6c756500); f(0x43800000); 
    f(0xa20c6465); f(0x73637269); f(0x7074696f); f(0x6e009046); 
    f(0x905b9378); f(0x001d0400); f(0xc1000010); f(0x001d0200); 
    f(0x10000080); f(0x00010200); f(0x10020000); f(0x001d0400); 
    f(0x200200c0); f(0x00040200); f(0x10040080); f(0x00030200); 
    f(0x10040000); f(0x001a0400); f(0x100200c0); f(0x001d0200); 
    f(0x100400c0); f(0x001d0400); f(0x100200c0); f(0x00030400); 
    f(0x10040080); f(0x001d0500); f(0x80040000); f(0x00020500); 
    f(0x800400c0); f(0x001d0400); f(0x10050000); f(0x001d0500); 
    f(0x800200c0); f(0x00030500); f(0x80040080); f(0x00320500); 
    f(0x40400000); f(0x00040500); f(0x20050040); f(0x00030500); 
    f(0x20000080); f(0x001d0500); f(0x40050000); f(0x00010500); 
    f(0x40050080); f(0x001d0500); f(0x80050040); f(0x00010500); 
    f(0x80020000); f(0x001d0400); f(0x80050000); f(0x001d0500); 
    f(0x800000c0); f(0x00010500); f(0x80020040); f(0x001d0400); 
    f(0x20050000); f(0x00040500); f(0x80040080); f(0x00030500); 
    f(0x80040040); f(0x001a0500); f(0x40050000); f(0x001d0500); 
    f(0x80050040); f(0x001d0500); f(0x40050000); f(0x00030500); 
    f(0x40040080); f(0x001d0500); f(0x20040040); f(0x00020500); 
    f(0x20050040); f(0x001d0500); f(0x40050080); f(0x001d0500); 
    f(0x20050000); f(0x00030500); f(0x20040080); f(0x00320500); 
    f(0x10400000); f(0x00040600); f(0x800500c0); f(0x00030600); 
    f(0x800000c0); f(0x001d0500); f(0x10050080); f(0x00010500); 
    f(0x10060000); f(0x001d0500); f(0x200500c0); f(0x00010500); 
    f(0x20020040); f(0x001d0400); f(0x40050080); f(0x002a0400); 
    f(0x10020000); f(0x001d0180); f(0x80008000); f(0x00340000); 
    f(0x00018000); f(0x001d0100); f(0xf303001b); f(0x00350000); 
    f(0x00000000); f(0x002a0500); f(0x40020040); f(0x001d0180); 
    f(0x40008000); f(0x00340000); f(0x00018040); f(0x001d0100); 
    f(0xf303001b); f(0x00350000); f(0x00000000); f(0x00310600); 
    f(0xf1040010); f(0x001d0700); f(0xf306001b); f(0x00320500); 
    f(0x203f8000); f(0x001d0500); f(0x10020080); f(0x00020500); 
    f(0x10050080); f(0x001d0500); f(0x200500c0); f(0x001d0500); 
    f(0x10070000); f(0x00030500); f(0x10020080); f(0x001a0600); 
    f(0x800500c0); f(0x001d0500); f(0x10060000); f(0x002a0500); 
    f(0x200500c0); f(0x001d0180); f(0x20008000); f(0x00340000); 
    f(0x00018080); f(0x001d0500); f(0x10050080); f(0x00360000); 
    f(0x00000000); f(0x00040600); f(0x80050080); f(0x00030600); 
    f(0x800500c0); f(0x001d0700); f(0x80060000); f(0x001d0600); 
    f(0x80070040); f(0x00030600); f(0x80020080); f(0x001a0600); 
    f(0x40060000); f(0x001d0500); f(0x10060040); f(0x002a0500); 
    f(0x200500c0); f(0x001d0180); f(0x20008000); f(0x00340000); 
    f(0x00018080); f(0x001d0500); f(0x10050080); f(0x00360000); 
    f(0x00000000); f(0x00040600); f(0x80050080); f(0x00030600); 
    f(0x800500c0); f(0x001d0700); f(0x40060000); f(0x001d0600); 
    f(0x80070080); f(0x00030600); f(0x80020080); f(0x001a0600); 
    f(0x40060000); f(0x001d0500); f(0x10060040); f(0x002a0500); 
    f(0x200500c0); f(0x001d0180); f(0x20008000); f(0x00340000); 
    f(0x00018080); f(0x001d0500); f(0x10050080); f(0x00360000); 
    f(0x00000000); f(0x00040600); f(0x80050080); f(0x00030600); 
    f(0x800500c0); f(0x001d0700); f(0x20060000); f(0x001d0600); 
    f(0x800700c0); f(0x00030600); f(0x80020080); f(0x001a0600); 
    f(0x40060000); f(0x001d0500); f(0x10060040); f(0x002a0500); 
    f(0x200500c0); f(0x001d0180); f(0x20008000); f(0x00340000); 
    f(0x00018080); f(0x001d0500); f(0x10050080); f(0x00360000); 
    f(0x00000000); f(0x00040600); f(0x80050080); f(0x00030600); 
    f(0x800500c0); f(0x001d0700); f(0x10060000); f(0x001d0100); 
    f(0xf307001b); f(0x00360000); f(0x00000000); f(0x00360000); 
    f(0x00000000); 

    f = a.writeByte;
    f(0x00); 

    return a;

}


           
        }
    }
}


// ↓ pbk ファイルのコード
/* -----------------------------------------------------------------------------
    パラメータ
    width           ドットの幅
    height          ドットの高さ
    interval_h      水平方向のドット間隔
    interval_v      垂直方向のドット間隔
    interval_color  ドット間隔の色
    bits_per_pixel  色深度
 ----------------------------------------------------------------------------- */
/*
<languageVersion : 1.0;>
kernel LiquidCrystalDisplayFilter<
    namespace : "liquid crystal display";
    vendor : "Hakuhin";
    version : 1;
    description : "液晶ディスプレイ風フィルタ";
>{
    input image4 src;    // 入力イメージ
    output pixel4 dst;    // 出力ピクセル


    // -------------------------------------
    // パラメータ
    // -------------------------------------
    parameter float width<
        minValue:float(1.0);
        defaultValue:float(3.0);
        description : "ドットの幅";
    >;
    parameter float height<
        minValue:float(1.0);
        defaultValue:float(3.0);
        description : "ドットの高さ";
    >;
    parameter float interval_h<
        minValue:float(0.0);
        defaultValue:float(1.0);
        description : "水平方向のドット間隔";
    >;
    parameter float interval_v<
        minValue:float(0.0);
        defaultValue:float(1.0);
        description : "垂直方向のドット間隔";
    >;
    parameter pixel4 interval_color<
        defaultValue:pixel4(0,0,0,1);
        description : "ドット間隔の色";
    >;
    parameter float bits_per_pixel<
        minValue:float(1);
        maxValue:float(256);
        defaultValue:float(256);
        description : "色深度";
    >;

    // -------------------------------------
    // ピクセルごとに実行される関数
    // -------------------------------------
    void evaluatePixel(){

        // ピクセルの位置を取得
        float2 pos = outCoord();
        
        // 水平方向の位置を周期に丸める
        float length = width + interval_h;
        float x = floor(pos.x / length);
        float h = pos.x - (x * length);
        pos.x = x * length + width / 2.0 + interval_h;
        
        // 垂直方向の位置を周期に丸める
        length = height + interval_v;
        float y = floor(pos.y / length);
        float v = pos.y - (y * length);
        pos.y = y * length + height / 2.0 + interval_v;
        
        if(h < interval_h){
            // ドット間隔の色を出力
            dst = interval_color;
        }else if(v < interval_v){
            // ドット間隔の色を出力
            dst = interval_color;
        }else{

            // 中央の1ドットを表示
            pixel4 color = sampleLinear(src,pos);
            float d;
            float de = bits_per_pixel - 1.0;
            
            d = floor(color[0] * bits_per_pixel);
            if(d > de) d = de;
            color[0] = d / de;
 
            d = floor(color[1] * bits_per_pixel);
            if(d > de) d = de;
            color[1] = d / de;
 
            d = floor(color[2] * bits_per_pixel);
            if(d > de) d = de;
            color[2] = d / de;

            d = floor(color[3] * bits_per_pixel);
            if(d > de) d = de;
            color[3] = d / de;
            
            dst = color;
        }

    }
}
*/





import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.geom.*;

// -------------------------------------------------
// ボタン
// -------------------------------------------------
internal class Button extends Sprite {

    private var _width:Number;
    private var _height:Number;
    
    private var _text:TextField;
    private var _background:Sprite;
    

    public function Button(stage:Stage) {
        var slider:Button = this;

        // 背景用スプライト作成
        _background = new Sprite();
        addChild(_background);

        // テキストフィールド
        _text = new TextField();
        addChild(_text);
    
        _text.x = 0;
        _text.y = 0;
        _text.selectable = false;

        // 書式
        var format:TextFormat = new TextFormat();
        format.align = TextFormatAlign.CENTER;    // 整列
        format.font = "MS ゴシック";    // フォント名
        format.size = 14;                // 文字のポイントサイズ
        format.color = 0x202020;        // 文字の色
        _text.defaultTextFormat = format;
    
        // マウスオーバーで少し明るく
        addEventListener(MouseEvent.MOUSE_OVER,function(e:MouseEvent):void{
            var color : ColorTransform = new ColorTransform(1,1,1,1,8,8,8,0);
            transform.colorTransform = color;
        });

        // マウスアウトで元に戻す
        addEventListener(MouseEvent.MOUSE_OUT,function(e:MouseEvent):void{
            var color : ColorTransform = new ColorTransform(1,1,1,1,0,0,0,0);
            transform.colorTransform = color;                                                           
        });

        // デフォルト値
        setSize(100,100);
        update();
    }
    // リサイズ
    public function setSize(w:Number,h:Number):void{
        // 背景リサイズ
        _width = w;
        _height = h;
        update();
    }
    // ラベルセット
    public function setLabel(str:String):void{
        _text.text = str;
        update();
    }
    // 描画更新
    private function update():void{
        // 背景描画
        var g:Graphics = _background.graphics;
        
        // 角丸矩形描画
        g.clear();
        g.lineStyle ( 0 , 0x808080 , 1.0,false,LineScaleMode.NONE,CapsStyle.ROUND,JointStyle.ROUND);
        g.beginFill ( 0xF0F0F0 , 1.0 );
        g.drawRoundRect ( 0 , 0 , _width , _height , 5 , 5 );
        g.endFill();
        
        // テキスト位置修正
        _text.width  = _width;
        _text.height = _height;
    }
}


// -------------------------------------------------
// 外部画像をサムネイルとしてキャプチャ
// -------------------------------------------------
import flash.net.*;
import flash.events.*;
import flash.display.*;
import flash.geom.*;
function ThumbnailCapture(url:String,time:uint,stage:Stage):void{

    // キャプチャタイミング
    Wonderfl.capture_delay( time );

    // スプライト作成
    var sprite : Sprite = new Sprite();

    // ステージ最前面に配置
    stage.addChildAt(sprite,stage.numChildren);
    
    // ローダー
    var loader_obj : Loader = new Loader();
    loader_obj.contentLoaderInfo.addEventListener (Event.INIT,function(e:Event):void{

        // メモリからインスタンス化
        var loader_memory : Loader = new Loader();
        loader_memory.contentLoaderInfo.addEventListener (Event.INIT,function(e:Event):void{

            // キャプチャ
            var bmp : BitmapData = new BitmapData(loader_memory.width,loader_memory.height,true,0);
            sprite.addChild(loader_memory);
            bmp.draw(sprite);
            sprite.removeChild(loader_memory);
            loader_memory.unload();
            loader_obj.unload();
            loader_memory = null;
            loader_obj = null;
            
            
            // 画像を配置
            var bmp_obj : Bitmap = new Bitmap(bmp);
            bmp_obj .width = stage.stageWidth;
            bmp_obj .height = stage.stageHeight;
            stage.addChild(bmp_obj );
            
        });
        
        // 読み込み開始
        loader_memory.loadBytes(loader_obj.contentLoaderInfo.bytes);
    });
    
    // 読み込み開始
    loader_obj.load(new URLRequest(url));
}