notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FAVORITE BY
:
:
:
:
:
:
:
:
こんなん。あり得るんすね
:
Video
:
:
:
:
:
超絶変形凄驚効果これすごぃ
:
:
ぐにゃぐにゃ
:
:
:
:
:
BitmapDatafluids
:
:
:
flv fluid
:
:
:
ウオター
:
面白い!
:
awesome
:
:
:
effectFun Video Effect
:
BitmapData Distortion
:
bitmapdatafilterターミネーターみたい
:
:
:
おもしろい!
:
:
うにゃにゃにゃにゃぁ!
:
[fx]gunegune
:
うにゃうにゃぁ!
:
:
videoeffectperlinFluid video
:
:
すごい
:
ぐにゃぐにゃ
FORKED
  1. // forked from alumican_net's Fluid on the Video
  2. /**
  3.  * Fluid on the Video
  4.  * 
  5.  * @author http://alumican.net
  6.  * 
  7.  * 動画はこちらからお借りしています
  8.  * http://www.nicovideo.jp/watch/sm3605606
  9.  */
  10. package
  11. {
  12.     import flash.display.Bitmap;
  13.     import flash.display.BitmapData;
  14.     import flash.display.BlendMode;
  15.     import flash.display.Graphics;
  16.     import flash.display.Sprite;
  17.     import flash.events.Event;
  18.     import flash.events.MouseEvent;
  19.     import flash.events.NetStatusEvent;
  20.     import flash.filters.BlurFilter;
  21.     import flash.filters.ColorMatrixFilter;
  22.     import flash.filters.DisplacementMapFilter;
  23.     import flash.geom.ColorTransform;
  24.     import flash.geom.Matrix;
  25.     import flash.geom.Point;
  26.     import flash.geom.Rectangle;
  27.     import flash.media.SoundTransform;
  28.     import flash.media.Video;
  29.     import flash.net.NetConnection;
  30.     import flash.net.NetStream;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFieldAutoSize;
  33.     import flash.text.TextFormat;
  34.     import net.hires.debug.Stats;
  35.     
  36.     public class FlashTest extends Sprite
  37.     {
  38.         //CLASS CONSTANTS
  39.         private const MAP_WIDTH:Number        = 465;
  40.         private const MAP_HEIGHT:Number       = 465;
  41.         private const MAP_GRID_SIZE:Number    = 20;
  42.         private const MAP_FLOW_SIZE:Number    = 2;
  43.         private const MAP_INTENSITY:Number    = 0.25;
  44.         private const MAP_SCALE:Number        = 150;
  45.         private const MAP_USE_DECAY:Boolean   = true;
  46.         private const MAP_BLUR_INTENSITY:uint = 32;
  47.         private const MAP_BLUR_QUALITY:uint   = 2;
  48.         
  49.         private const ZERO_POINT:Point = new Point(0,0);
  50.         
  51.         //VARIABLES
  52.         private var _container:Sprite;
  53.         
  54.         private var _canvas:BitmapData;
  55.         private var _canvasTone:ColorMatrixFilter;
  56.         
  57.         private var _fluidMap:FluidMap;
  58.         private var _fluidBmp:Bitmap;
  59.         
  60.         private var _mapBmd:BitmapData;
  61.         private var _mapFilter:DisplacementMapFilter;
  62.         
  63.         private var _oldX:Number = 0;
  64.         private var _oldY:Number = 0;
  65.         private var _isMouseMove:Boolean = false;
  66.         
  67.         private var _ns:NetStream;
  68.         private var _nc:NetConnection;
  69.         private var _video:Video;
  70.         private var _videoMatrix:Matrix;
  71.         
  72.         private var _background:Sprite;
  73.         
  74.         private var _stats:Stats;
  75.         private var _usage:TextField;
  76.         
  77.         //CONSTRUCTOR
  78.         public function FlashTest():void
  79.         {
  80.             Wonderfl.disable_capture();
  81.             addEventListener(Event.ADDED_TO_STAGE, _initialize);
  82.         }
  83.         
  84.         //METHODS
  85.         private function _initialize(e:Event):void
  86.         {
  87.             removeEventListener(Event.ADDED_TO_STAGE, _initialize);
  88.             
  89.             _background = new Sprite();
  90.             addChild(_background);
  91.             
  92.             _container = new Sprite();
  93.             addChild(_container);
  94.             
  95.             _canvas = new BitmapData(MAP_WIDTH, MAP_HEIGHT, false, 0xffffff);
  96.             _container.addChild(new Bitmap(_canvas));
  97.             
  98.             _fluidMap = new FluidMap(MAP_WIDTH, MAP_HEIGHT, MAP_GRID_SIZE, MAP_FLOW_SIZE, MAP_INTENSITY, MAP_USE_DECAY, MAP_SCALE, MAP_BLUR_INTENSITY, MAP_BLUR_QUALITY);
  99.             
  100.             _mapFilter = _fluidMap.mapFilter;
  101.             
  102.             _canvasTone = new ColorMatrixFilter([
  103.                 10005,
  104.                 01005,
  105.                 00105,
  106.                 00000
  107.             ]);
  108.             
  109.             _stats = new Stats( {
  110.                 bg:0xffffff,
  111.                 fps:0x333333,
  112.                 ms:0x333333,
  113.                 mem:0x333333,
  114.                 memmax:0x333333
  115.             });
  116.             _stats.blendMode = BlendMode.DARKEN;
  117.             addChild(_stats);
  118.             
  119.             _usage = new TextField();
  120.             _usage.defaultTextFormat = new TextFormat("MS Gothic"11, 0x0);
  121.             _usage.text = "Move mouse on movie";
  122.             _usage.autoSize = TextFieldAutoSize.RIGHT;
  123.             _usage.selectable = false;
  124.             _usage.blendMode = BlendMode.INVERT;
  125.             _usage.visible = false;
  126.             _container.addChild(_usage);
  127.             
  128.             stage.addEventListener(Event.RESIZE, _resizeHandler);
  129.             
  130.             _resizeHandler();
  131.             
  132.             _loadVideo("http://lab.alumican.net/wonderfl/liquid_video_input.flv");
  133.         }
  134.         
  135.         private function _loadVideo(url:String):void
  136.         {
  137.             _nc = new NetConnection();
  138.             _nc.connect(null);
  139.             
  140.             _ns = new NetStream(_nc);
  141.             _ns.addEventListener(NetStatusEvent.NET_STATUS, _videoNetStatusHandler);
  142.             _ns.client = {
  143.                 onMetaData:function(param:Object):void
  144.                 {
  145.                 }
  146.             };
  147.                         _ns.checkPolicyFile = true;
  148.             _ns.play(url);
  149.             
  150.             _video = new Video();
  151.             _video.width  = 300;
  152.             _video.height = 220;
  153.             _video.attachNetStream(_ns);
  154.         }
  155.         
  156.         private function _videoNetStatusHandler(e:NetStatusEvent):void 
  157.         {
  158.                     if(e.info.code == "NetStream.Buffer.Full")
  159.                     {
  160.             _videoMatrix = new Matrix();
  161.             _videoMatrix.scale(_video.width / 320, _video.height / 240);
  162.             
  163.             _videoMatrix.tx = uint( (MAP_WIDTH  - _video.width ) / 2 );
  164.             _videoMatrix.ty = uint( (MAP_HEIGHT - _video.height) / 2 );
  165.             
  166.             _usage.x = _videoMatrix.tx + _video.width - _usage.textWidth;
  167.             _usage.y = _videoMatrix.ty - 20;
  168.             _usage.visible = true;
  169.             
  170.             var st:SoundTransform = _ns.soundTransform;
  171.             st.volume = 0.3;
  172.             _ns.soundTransform = st;
  173.             
  174.             addEventListener(Event.ENTER_FRAME, _update);
  175.             stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
  176.             }
  177.         }
  178.         
  179.         private function _drawCanvas():void
  180.         {
  181.             _canvas.lock();
  182.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _canvasTone);
  183.             _canvas.draw(_video, _videoMatrix);
  184.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _mapFilter);
  185.             
  186.             _canvas.unlock();
  187.         }
  188.         
  189.         private function _refillBackground():void
  190.         {
  191.             var sw:int = stage.stageWidth;
  192.             var sh:int = stage.stageHeight;
  193.             
  194.             var g:Graphics = _background.graphics;
  195.             g.clear();
  196.             g.beginFill(0xffffff);
  197.             g.moveTo(0 , 0 );
  198.             g.lineTo(sw, 0 );
  199.             g.lineTo(sw, sh);
  200.             g.lineTo(0 , sh);
  201.             g.lineTo(0 , 0 );
  202.             g.endFill();
  203.         }
  204.         
  205.         private function _update(e:Event):void
  206.         {
  207.             if (_isMouseMove)
  208.             {
  209.                 var speedX:Number = _container.mouseX - _oldX;
  210.                 var speedY:Number = _container.mouseY - _oldY;
  211.                 
  212.                 _fluidMap.addOrientedForce(_container.mouseX, _container.mouseY, speedX, speedY);
  213.                 
  214.                 _isMouseMove = false;
  215.             }
  216.             
  217.             _fluidMap.updateMap();
  218.             
  219.             _drawCanvas();
  220.             
  221.             _oldX = _container.mouseX;
  222.             _oldY = _container.mouseY;
  223.         }
  224.         
  225.         private function _mouseMoveHandler(e:MouseEvent):void
  226.         {
  227.             _isMouseMove = true;
  228.         }
  229.         
  230.         private function _resizeHandler(e:Event = null):void
  231.         {
  232.             _refillBackground();
  233.             
  234.             _container.x = uint((stage.stageWidth  - MAP_WIDTH ) / 2);
  235.             _container.y = uint((stage.stageHeight - MAP_HEIGHT) / 2);
  236.             
  237.             _stats.x = 2;
  238.             _stats.y = stage.stageHeight - 45;
  239.         }
  240.     }
  241. }
  242. import flash.display.Bitmap;
  243. import flash.display.BitmapData;
  244. import flash.display.BitmapDataChannel;
  245. import flash.filters.BlurFilter;
  246. import flash.filters.DisplacementMapFilter;
  247. import flash.filters.DisplacementMapFilterMode;
  248. import flash.geom.Point;
  249. import flash.geom.Rectangle;
  250. internal class FluidMap
  251. {
  252.     //CLASS CONSTANTS
  253.     private static const ZERO_POINT:Point = new Point(00);
  254.     
  255.     //VARIABLES
  256.     private var _cells:Array;
  257.     private var _cellCount:uint;
  258.     
  259.     private var _allCells:Array;
  260.     private var _allCellCount:uint;
  261.     
  262.     private var _width:uint;
  263.     private var _height:uint;
  264.     
  265.     private var _widthCount:uint;
  266.     private var _heightCount:uint;
  267.     
  268.     private var _gridSize:uint;
  269.     private var _flowSize:Number;
  270.     private var _intensity:Number;
  271.     private var _useDecay:Boolean;
  272.     
  273.     private var _mapBmd:BitmapData;
  274.     private var _mapFilter:DisplacementMapFilter;
  275.     private var _mapScale:Number;
  276.     private var _mapBlurIntensity:uint;
  277.     private var _mapBlurQuality:uint;
  278.     private var _blurFilter:BlurFilter;
  279.     
  280.     private var _calculator:FluidCalculator;
  281.     
  282.     private var _colorAdjust:Number;
  283.     private var _distMin2:Number;
  284.     private var _flowSize2:Number;
  285.     
  286.     public function get mapFilter():DisplacementMapFilter { return _mapFilter; }
  287.     
  288.     //CONSTRUCTOR
  289.     public function FluidMap(
  290.         width:uint,
  291.         height:uint,
  292.         gridSize:uint         = 10,
  293.         flowSize:Number       = 2,
  294.         intensity:Number      = 0.25,
  295.         useDecay:Boolean      = true,
  296.         mapScale:Number       = 100,
  297.         mapBlurIntensity:uint = 32,
  298.         mapBlurQuality:uint   = 2
  299.     ):void
  300.     {
  301.         _width            = width;
  302.         _height           = height;
  303.         _gridSize         = gridSize;
  304.         _flowSize         = flowSize;
  305.         _useDecay         = useDecay;
  306.         _intensity        = intensity;
  307.         _mapScale         = mapScale;
  308.         _mapBlurIntensity = mapBlurIntensity;
  309.         _mapBlurQuality   = mapBlurQuality;
  310.         
  311.         _widthCount  = uint( Math.ceil(width  / gridSize) );
  312.         _heightCount = uint( Math.ceil(height / gridSize) );
  313.         
  314.         _allCellCount = _widthCount * _heightCount;
  315.         _allCells = new Array(_allCellCount);
  316.         var p:uint = 0;
  317.         
  318.                 var i:uint;
  319.                 var j:uint;
  320.                 var data:FluidMapData;
  321.                 
  322.         var cells2d:Array = new Array(_widthCount);
  323.         for (i = 0; i < _widthCount; ++i)
  324.         {
  325.             cells2d[i] = new Array(_heightCount);
  326.             for (j = 0; j < _heightCount; ++j)
  327.             {
  328.                 data = new FluidMapData(i, j);
  329.                 cells2d[i][j] = data;
  330.                 _allCells[p] = data;
  331.                 
  332.                 ++p;
  333.             }
  334.         }
  335.         
  336.         _cells = new Array();
  337.         
  338.         var w:uint = _widthCount  - 1;
  339.         var h:uint = _heightCount - 1;
  340.         var pi:uint = 0;
  341.         var pj:uint = 0;
  342.         for (i = 1; i < w; ++i)
  343.         {
  344.             for (j = 1; j < h; ++j)
  345.             {
  346.                 data = cells2d[i][j] as FluidMapData;
  347.                 
  348.                 data.n00 = cells2d[i - 1][j - 1as FluidMapData;
  349.                 data.n10 = cells2d[i    ][j - 1as FluidMapData;
  350.                 data.n20 = cells2d[i + 1][j - 1as FluidMapData;
  351.                 
  352.                 data.n01 = cells2d[i - 1][j    ] as FluidMapData;
  353.                 data.n21 = cells2d[i + 1][j    ] as FluidMapData;
  354.                 
  355.                 data.n02 = cells2d[i - 1][j + 1as FluidMapData;
  356.                 data.n12 = cells2d[i    ][j + 1as FluidMapData;
  357.                 data.n22 = cells2d[i + 1][j + 1as FluidMapData;
  358.                 
  359.                 if (pi != 0)
  360.                 {
  361.                     data.prev = cells2d[pi][pj] as FluidMapData;
  362.                     data.prev.next = data;
  363.                 }
  364.                 
  365.                 _cells.push(data);
  366.                 
  367.                 pi = i;
  368.                 pj = j;
  369.             }
  370.         }
  371.         _cellCount = _cells.length;
  372.         
  373.         _mapBmd = new BitmapData(_width, _height, false, 0x008080);
  374.         
  375.         _mapFilter = new DisplacementMapFilter();
  376.         _mapFilter.mapBitmap  = _mapBmd;
  377.         _mapFilter.mapPoint   = ZERO_POINT;
  378.         _mapFilter.componentX = BitmapDataChannel.GREEN;
  379.         _mapFilter.componentY = BitmapDataChannel.BLUE;
  380.         _mapFilter.mode       = DisplacementMapFilterMode.CLAMP;
  381.         _mapFilter.scaleX     = _mapScale;
  382.         _mapFilter.scaleY     = _mapScale;
  383.         
  384.         _blurFilter = new BlurFilter(_mapBlurIntensity, _mapBlurIntensity, _mapBlurQuality);
  385.         
  386.         _colorAdjust = -128 * _intensity;
  387.         _distMin2    = 4 * _gridSize * _gridSize;
  388.         _flowSize2   = _flowSize * _flowSize;
  389.         
  390.         _calculator = new FluidCalculator();
  391.     }
  392.     
  393.     //METHODS
  394.     public function addOrientedForce(x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  395.     {
  396.         var s:uint = _gridSize;
  397.         var n:uint = _cellCount;
  398.         var cell:FluidMapData = _cells[0as FluidMapData;
  399.         
  400.         for (var i:uint = 0; i < n; ++i)
  401.         {
  402.             _calcOrientedForce(cell, x / s, y / s, forceX, forceY);
  403.             cell = cell.next;
  404.         }
  405.     }
  406.     
  407.     public function updateMap():BitmapData
  408.     {
  409.         var s:uint            = _gridSize;
  410.         var n:uint            = _cellCount;
  411.         var cell:FluidMapData = _cells[0as FluidMapData;
  412.         var map:BitmapData    = _mapBmd;
  413.         
  414.         _decay();
  415.         _updatePressure();
  416.         _updateVelocity();
  417.         
  418.         map.lock();
  419.         map.fillRect(map.rect, 0x008080);
  420.         for (var i:uint = 0; i < n; ++i)
  421.         {
  422.             map.fillRect(
  423.                 new Rectangle(cell.x * s, cell.y * s, s, s),
  424.                 _calcMapColor(cell)
  425.             );
  426.             cell = cell.next;
  427.         }
  428.         map.applyFilter(map, map.rect, ZERO_POINT, _blurFilter);
  429.         map.unlock();
  430.         
  431.         return map;
  432.     }
  433.     
  434.     private function _decay():void
  435.     {
  436.         if (_useDecay)
  437.         {    
  438.             var n:uint = _allCellCount;
  439.             var cell:FluidMapData = _allCells[0as FluidMapData;
  440.             
  441.             for (var i:uint = 0; i < n; ++i)
  442.             {
  443.                 _calcDecay(_allCells[i]);
  444.                 //cell = cell.next;
  445.             }
  446.         }
  447.     }
  448.     
  449.     private function _updatePressure():void
  450.     {
  451.         var n:uint = _cellCount;
  452.         var cell:FluidMapData = _cells[0as FluidMapData;
  453.         
  454.         for (var i:uint = 0; i < n; ++i)
  455.         {
  456.             _calcPressure(cell);
  457.             cell = cell.next;
  458.         }
  459.     }
  460.     
  461.     private function _updateVelocity():void
  462.     {
  463.         var n:uint = _cellCount;
  464.         var cell:FluidMapData = _cells[0as FluidMapData;
  465.         
  466.         for (var i:uint = 0; i < n; ++i)
  467.         {
  468.             _calcVelocity(cell);
  469.             cell = cell.next;
  470.         }
  471.     }
  472.     
  473.     private function _calcDecay(data:FluidMapData):void
  474.     {
  475.         var r:Number = data.colorX;
  476.         var b:Number = data.colorY;
  477.         
  478.         if (r != 128) data.colorX += (r < 128) ? 1 : -1;
  479.         if (b != 128) data.colorY += (b < 128) ? 1 : -1;
  480.     }
  481.     
  482.     private function _calcPressure(data:FluidMapData):void
  483.     { 
  484.         data.pressure += _calculator.calcPressure(data);
  485.         data.pressure *= 0.7;
  486.     }
  487.     
  488.     private function _calcVelocity(data:FluidMapData):void
  489.     {
  490.         data.vx += _calculator.calcVelocityX(data);
  491.         data.vy += _calculator.calcVelocityY(data);
  492.         data.vx *= 0.7;
  493.         data.vy *= 0.7;
  494.     }
  495.     
  496.     private function _calcOrientedForce(data:FluidMapData, x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  497.     {
  498.         var dx:int       = data.x - x;
  499.         var dy:int       = data.y - y;
  500.         var dist2:Number = dx * dx + dy * dy;
  501.         
  502.         if (dist2 < _flowSize2)
  503.         {
  504.             var f:Number = (dist2 < _distMin2) ? 1.0 : (_flowSize / Math.sqrt(dist2));
  505.             
  506.             data.vx += forceX * f;
  507.             data.vy += forceY * f;
  508.         }
  509.     }
  510.     
  511.     private function _calcMapColor(data:FluidMapData):uint
  512.     {
  513.         var vx:Number = data.vx;
  514.         var vy:Number = data.vy;
  515.         var g:int  = data.colorX;
  516.         var b:int  = data.colorY;
  517.         
  518.         g = Math.round( _colorAdjust * vx + g );
  519.         b = Math.round( _colorAdjust * vy + b );
  520.         
  521.         g = (g < 0) ? 0 : (g > 255) ? 255 : g;
  522.         b = (b < 0) ? 0 : (b > 255) ? 255 : b;
  523.         
  524.         data.colorX = g;
  525.         data.colorY = b;
  526.         
  527.         return data.color = g << 8 | b;
  528.     }
  529. }
  530. internal class FluidCalculator
  531. {
  532.     //CONSTRUCTOR
  533.     public function FluidCalculator():void
  534.     {
  535.     }
  536.     
  537.     //METHODS
  538.     public function calcVelocityX(data:FluidMapData):Number
  539.     {
  540.         return (  data.n00.pressure * 0.5
  541.                 + data.n01.pressure
  542.                 + data.n02.pressure * 0.5
  543.                 
  544.                 - data.n20.pressure * 0.5
  545.                 - data.n21.pressure
  546.                 - data.n22.pressure * 0.5
  547.                 
  548.                 ) * 0.25;
  549.     }
  550.     
  551.     public function calcVelocityY(data:FluidMapData):Number
  552.     {
  553.         return (  data.n00.pressure * 0.5
  554.                 + data.n10.pressure
  555.                 + data.n20.pressure * 0.5
  556.                 
  557.                 - data.n02.pressure * 0.5
  558.                 - data.n12.pressure
  559.                 - data.n22.pressure * 0.5
  560.                 
  561.                 ) * 0.25;
  562.     }
  563.     
  564.     public function calcPressure(data:FluidMapData):Number
  565.     {
  566.         return (  data.n00.vx * 0.5
  567.                 + data.n01.vx
  568.                 + data.n02.vx * 0.5
  569.                 - data.n20.vx * 0.5
  570.                 - data.n21.vx
  571.                 - data.n22.vx * 0.5
  572.                 
  573.                 + data.n00.vy * 0.5
  574.                 + data.n10.vy
  575.                 + data.n20.vy * 0.5
  576.                 - data.n02.vy * 0.5
  577.                 - data.n12.vy
  578.                 - data.n22.vy * 0.5
  579.                 
  580.                 ) * 0.20;
  581.     }
  582. }
  583. internal class FluidMapData
  584. {
  585.     //VARIABLES
  586.     public function get x():uint { return _x; }
  587.     public function set x(value:uint):void { _x = value; }
  588.     private var _x:uint;
  589.     
  590.     public function get y():uint { return _y; }
  591.     public function set y(value:uint):void { _y = value; }
  592.     private var _y:uint;
  593.     
  594.     public function get vx():Number { return _vx; }
  595.     public function set vx(value:Number):void { _vx = value; }
  596.     private var _vx:Number;
  597.     
  598.     public function get vy():Number { return _vy; }
  599.     public function set vy(value:Number):void { _vy = value; }
  600.     private var _vy:Number;
  601.     
  602.     public function get pressure():Number { return _pressure; }
  603.     public function set pressure(value:Number):void { _pressure = value; }
  604.     private var _pressure:Number;
  605.     
  606.     public function get color():uint { return _color; }
  607.     public function set color(value:uint):void { _color = value; }
  608.     private var _color:uint;
  609.     
  610.     public function get colorX():uint { return _colorX; }
  611.     public function set colorX(value:uint):void { _colorX = value; }
  612.     private var _colorX:uint;
  613.     
  614.     public function get colorY():uint { return _colorY; }
  615.     public function set colorY(value:uint):void { _colorY = value; }
  616.     private var _colorY:uint;
  617.     
  618.     public function get next():FluidMapData { return _next; }
  619.     public function set next(value:FluidMapData):void { _next = value; }
  620.     private var _next:FluidMapData;
  621.     
  622.     public function get prev():FluidMapData { return _prev; }
  623.     public function set prev(value:FluidMapData):void { _prev = value; }
  624.     private var _prev:FluidMapData;
  625.     
  626.     private var _n00:FluidMapData;
  627.     private var _n01:FluidMapData;
  628.     private var _n02:FluidMapData;
  629.     private var _n10:FluidMapData;
  630.     private var _n12:FluidMapData;
  631.     private var _n20:FluidMapData;
  632.     private var _n21:FluidMapData;
  633.     private var _n22:FluidMapData;
  634.     
  635.     public function get n00():FluidMapData { return _n00; }
  636.     public function get n01():FluidMapData { return _n01; }
  637.     public function get n02():FluidMapData { return _n02; }
  638.     public function get n10():FluidMapData { return _n10; }
  639.     public function get n12():FluidMapData { return _n12; }
  640.     public function get n20():FluidMapData { return _n20; }
  641.     public function get n21():FluidMapData { return _n21; }
  642.     public function get n22():FluidMapData { return _n22; }
  643.     
  644.     public function set n00(value:FluidMapData):void { _n00 = value; }
  645.     public function set n01(value:FluidMapData):void { _n01 = value; }
  646.     public function set n02(value:FluidMapData):void { _n02 = value; }
  647.     public function set n10(value:FluidMapData):void { _n10 = value; }
  648.     public function set n12(value:FluidMapData):void { _n12 = value; }
  649.     public function set n20(value:FluidMapData):void { _n20 = value; }
  650.     public function set n21(value:FluidMapData):void { _n21 = value; }
  651.     public function set n22(value:FluidMapData):void { _n22 = value; }
  652.     
  653.     //CONSTRUCTOR
  654.     public function FluidMapData(x:uint, y:uint):void
  655.     {
  656.         _x        = x;
  657.         _y        = y;
  658.         _vx       = 0;
  659.         _vy       = 0;
  660.         _pressure = 0;
  661.         _color    = 0x008080;
  662.         _colorX   = 128;
  663.         _colorY   = 128;
  664.     }
  665. }
noswf
  1. // forked from alumican_net's Fluid on the Video
  2. /**
  3.  * Fluid on the Video
  4.  * 
  5.  * @author http://alumican.net
  6.  * 
  7.  * 動画はこちらからお借りしています
  8.  * http://www.nicovideo.jp/watch/sm3605606
  9.  */
  10. package
  11. {
  12.     import flash.display.Bitmap;
  13.     import flash.display.BitmapData;
  14.     import flash.display.BlendMode;
  15.     import flash.display.Graphics;
  16.     import flash.display.Sprite;
  17.     import flash.events.Event;
  18.     import flash.events.MouseEvent;
  19.     import flash.events.NetStatusEvent;
  20.     import flash.filters.BlurFilter;
  21.     import flash.filters.ColorMatrixFilter;
  22.     import flash.filters.DisplacementMapFilter;
  23.     import flash.geom.ColorTransform;
  24.     import flash.geom.Matrix;
  25.     import flash.geom.Point;
  26.     import flash.geom.Rectangle;
  27.     import flash.media.SoundTransform;
  28.     import flash.media.Video;
  29.     import flash.net.NetConnection;
  30.     import flash.net.NetStream;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFieldAutoSize;
  33.     import flash.text.TextFormat;
  34.     import net.hires.debug.Stats;
  35.     
  36.     public class FlashTest extends Sprite
  37.     {
  38.         //CLASS CONSTANTS
  39.         private const MAP_WIDTH:Number        = 465;
  40.         private const MAP_HEIGHT:Number       = 465;
  41.         private const MAP_GRID_SIZE:Number    = 20;
  42.         private const MAP_FLOW_SIZE:Number    = 2;
  43.         private const MAP_INTENSITY:Number    = 0.25;
  44.         private const MAP_SCALE:Number        = 150;
  45.         private const MAP_USE_DECAY:Boolean   = true;
  46.         private const MAP_BLUR_INTENSITY:uint = 32;
  47.         private const MAP_BLUR_QUALITY:uint   = 2;
  48.         
  49.         private const ZERO_POINT:Point = new Point(0,0);
  50.         
  51.         //VARIABLES
  52.         private var _container:Sprite;
  53.         
  54.         private var _canvas:BitmapData;
  55.         private var _canvasTone:ColorMatrixFilter;
  56.         
  57.         private var _fluidMap:FluidMap;
  58.         private var _fluidBmp:Bitmap;
  59.         
  60.         private var _mapBmd:BitmapData;
  61.         private var _mapFilter:DisplacementMapFilter;
  62.         
  63.         private var _oldX:Number = 0;
  64.         private var _oldY:Number = 0;
  65.         private var _isMouseMove:Boolean = false;
  66.         
  67.         private var _ns:NetStream;
  68.         private var _nc:NetConnection;
  69.         private var _video:Video;
  70.         private var _videoMatrix:Matrix;
  71.         
  72.         private var _background:Sprite;
  73.         
  74.         private var _stats:Stats;
  75.         private var _usage:TextField;
  76.         
  77.         //CONSTRUCTOR
  78.         public function FlashTest():void
  79.         {
  80.             Wonderfl.disable_capture();
  81.             addEventListener(Event.ADDED_TO_STAGE, _initialize);
  82.         }
  83.         
  84.         //METHODS
  85.         private function _initialize(e:Event):void
  86.         {
  87.             removeEventListener(Event.ADDED_TO_STAGE, _initialize);
  88.             
  89.             _background = new Sprite();
  90.             addChild(_background);
  91.             
  92.             _container = new Sprite();
  93.             addChild(_container);
  94.             
  95.             _canvas = new BitmapData(MAP_WIDTH, MAP_HEIGHT, false, 0xffffff);
  96.             _container.addChild(new Bitmap(_canvas));
  97.             
  98.             _fluidMap = new FluidMap(MAP_WIDTH, MAP_HEIGHT, MAP_GRID_SIZE, MAP_FLOW_SIZE, MAP_INTENSITY, MAP_USE_DECAY, MAP_SCALE, MAP_BLUR_INTENSITY, MAP_BLUR_QUALITY);
  99.             
  100.             _mapFilter = _fluidMap.mapFilter;
  101.             
  102.             _canvasTone = new ColorMatrixFilter([
  103.                 10005,
  104.                 01005,
  105.                 00105,
  106.                 00000
  107.             ]);
  108.             
  109.             _stats = new Stats( {
  110.                 bg:0xffffff,
  111.                 fps:0x333333,
  112.                 ms:0x333333,
  113.                 mem:0x333333,
  114.                 memmax:0x333333
  115.             });
  116.             _stats.blendMode = BlendMode.DARKEN;
  117.             addChild(_stats);
  118.             
  119.             _usage = new TextField();
  120.             _usage.defaultTextFormat = new TextFormat("MS Gothic"11, 0x0);
  121.             _usage.text = "Move mouse on movie";
  122.             _usage.autoSize = TextFieldAutoSize.RIGHT;
  123.             _usage.selectable = false;
  124.             _usage.blendMode = BlendMode.INVERT;
  125.             _usage.visible = false;
  126.             _container.addChild(_usage);
  127.             
  128.             stage.addEventListener(Event.RESIZE, _resizeHandler);
  129.             
  130.             _resizeHandler();
  131.             
  132.             _loadVideo("http://lab.alumican.net/wonderfl/liquid_video_input.flv");
  133.         }
  134.         
  135.         private function _loadVideo(url:String):void
  136.         {
  137.             _nc = new NetConnection();
  138.             _nc.connect(null);
  139.             
  140.             _ns = new NetStream(_nc);
  141.             _ns.addEventListener(NetStatusEvent.NET_STATUS, _videoNetStatusHandler);
  142.             _ns.client = {
  143.                 onMetaData:function(param:Object):void
  144.                 {
  145.                 }
  146.             };
  147.                         _ns.checkPolicyFile = true;
  148.             _ns.play(url);
  149.             
  150.             _video = new Video();
  151.             _video.width  = 300;
  152.             _video.height = 220;
  153.             _video.attachNetStream(_ns);
  154.         }
  155.         
  156.         private function _videoNetStatusHandler(e:NetStatusEvent):void 
  157.         {
  158.                     if(e.info.code == "NetStream.Buffer.Full")
  159.                     {
  160.             _videoMatrix = new Matrix();
  161.             _videoMatrix.scale(_video.width / 320, _video.height / 240);
  162.             
  163.             _videoMatrix.tx = uint( (MAP_WIDTH  - _video.width ) / 2 );
  164.             _videoMatrix.ty = uint( (MAP_HEIGHT - _video.height) / 2 );
  165.             
  166.             _usage.x = _videoMatrix.tx + _video.width - _usage.textWidth;
  167.             _usage.y = _videoMatrix.ty - 20;
  168.             _usage.visible = true;
  169.             
  170.             var st:SoundTransform = _ns.soundTransform;
  171.             st.volume = 0.3;
  172.             _ns.soundTransform = st;
  173.             
  174.             addEventListener(Event.ENTER_FRAME, _update);
  175.             stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
  176.             }
  177.         }
  178.         
  179.         private function _drawCanvas():void
  180.         {
  181.             _canvas.lock();
  182.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _canvasTone);
  183.             _canvas.draw(_video, _videoMatrix);
  184.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _mapFilter);
  185.             
  186.             _canvas.unlock();
  187.         }
  188.         
  189.         private function _refillBackground():void
  190.         {
  191.             var sw:int = stage.stageWidth;
  192.             var sh:int = stage.stageHeight;
  193.             
  194.             var g:Graphics = _background.graphics;
  195.             g.clear();
  196.             g.beginFill(0xffffff);
  197.             g.moveTo(0 , 0 );
  198.             g.lineTo(sw, 0 );
  199.             g.lineTo(sw, sh);
  200.             g.lineTo(0 , sh);
  201.             g.lineTo(0 , 0 );
  202.             g.endFill();
  203.         }
  204.         
  205.         private function _update(e:Event):void
  206.         {
  207.             if (_isMouseMove)
  208.             {
  209.                 var speedX:Number = _container.mouseX - _oldX;
  210.                 var speedY:Number = _container.mouseY - _oldY;
  211.                 
  212.                 _fluidMap.addOrientedForce(_container.mouseX, _container.mouseY, speedX, speedY);
  213.                 
  214.                 _isMouseMove = false;
  215.             }
  216.             
  217.             _fluidMap.updateMap();
  218.             
  219.             _drawCanvas();
  220.             
  221.             _oldX = _container.mouseX;
  222.             _oldY = _container.mouseY;
  223.         }
  224.         
  225.         private function _mouseMoveHandler(e:MouseEvent):void
  226.         {
  227.             _isMouseMove = true;
  228.         }
  229.         
  230.         private function _resizeHandler(e:Event = null):void
  231.         {
  232.             _refillBackground();
  233.             
  234.             _container.x = uint((stage.stageWidth  - MAP_WIDTH ) / 2);
  235.             _container.y = uint((stage.stageHeight - MAP_HEIGHT) / 2);
  236.             
  237.             _stats.x = 2;
  238.             _stats.y = stage.stageHeight - 45;
  239.         }
  240.     }
  241. }
  242. import flash.display.Bitmap;
  243. import flash.display.BitmapData;
  244. import flash.display.BitmapDataChannel;
  245. import flash.filters.BlurFilter;
  246. import flash.filters.DisplacementMapFilter;
  247. import flash.filters.DisplacementMapFilterMode;
  248. import flash.geom.Point;
  249. import flash.geom.Rectangle;
  250. internal class FluidMap
  251. {
  252.     //CLASS CONSTANTS
  253.     private static const ZERO_POINT:Point = new Point(00);
  254.     
  255.     //VARIABLES
  256.     private var _cells:Array;
  257.     private var _cellCount:uint;
  258.     
  259.     private var _allCells:Array;
  260.     private var _allCellCount:uint;
  261.     
  262.     private var _width:uint;
  263.     private var _height:uint;
  264.     
  265.     private var _widthCount:uint;
  266.     private var _heightCount:uint;
  267.     
  268.     private var _gridSize:uint;
  269.     private var _flowSize:Number;
  270.     private var _intensity:Number;
  271.     private var _useDecay:Boolean;
  272.     
  273.     private var _mapBmd:BitmapData;
  274.     private var _mapFilter:DisplacementMapFilter;
  275.     private var _mapScale:Number;
  276.     private var _mapBlurIntensity:uint;
  277.     private var _mapBlurQuality:uint;
  278.     private var _blurFilter:BlurFilter;
  279.     
  280.     private var _calculator:FluidCalculator;
  281.     
  282.     private var _colorAdjust:Number;
  283.     private var _distMin2:Number;
  284.     private var _flowSize2:Number;
  285.     
  286.     public function get mapFilter():DisplacementMapFilter { return _mapFilter; }
  287.     
  288.     //CONSTRUCTOR
  289.     public function FluidMap(
  290.         width:uint,
  291.         height:uint,
  292.         gridSize:uint         = 10,
  293.         flowSize:Number       = 2,
  294.         intensity:Number      = 0.25,
  295.         useDecay:Boolean      = true,
  296.         mapScale:Number       = 100,
  297.         mapBlurIntensity:uint = 32,
  298.         mapBlurQuality:uint   = 2
  299.     ):void
  300.     {
  301.         _width            = width;
  302.         _height           = height;
  303.         _gridSize         = gridSize;
  304.         _flowSize         = flowSize;
  305.         _useDecay         = useDecay;
  306.         _intensity        = intensity;
  307.         _mapScale         = mapScale;
  308.         _mapBlurIntensity = mapBlurIntensity;
  309.         _mapBlurQuality   = mapBlurQuality;
  310.         
  311.         _widthCount  = uint( Math.ceil(width  / gridSize) );
  312.         _heightCount = uint( Math.ceil(height / gridSize) );
  313.         
  314.         _allCellCount = _widthCount * _heightCount;
  315.         _allCells = new Array(_allCellCount);
  316.         var p:uint = 0;
  317.         
  318.                 var i:uint;
  319.                 var j:uint;
  320.                 var data:FluidMapData;
  321.                 
  322.         var cells2d:Array = new Array(_widthCount);
  323.         for (i = 0; i < _widthCount; ++i)
  324.         {
  325.             cells2d[i] = new Array(_heightCount);
  326.             for (j = 0; j < _heightCount; ++j)
  327.             {
  328.                 data = new FluidMapData(i, j);
  329.                 cells2d[i][j] = data;
  330.                 _allCells[p] = data;
  331.                 
  332.                 ++p;
  333.             }
  334.         }
  335.         
  336.         _cells = new Array();
  337.         
  338.         var w:uint = _widthCount  - 1;
  339.         var h:uint = _heightCount - 1;
  340.         var pi:uint = 0;
  341.         var pj:uint = 0;
  342.         for (i = 1; i < w; ++i)
  343.         {
  344.             for (j = 1; j < h; ++j)
  345.             {
  346.                 data = cells2d[i][j] as FluidMapData;
  347.                 
  348.                 data.n00 = cells2d[i - 1][j - 1as FluidMapData;
  349.                 data.n10 = cells2d[i    ][j - 1as FluidMapData;
  350.                 data.n20 = cells2d[i + 1][j - 1as FluidMapData;
  351.                 
  352.                 data.n01 = cells2d[i - 1][j    ] as FluidMapData;
  353.                 data.n21 = cells2d[i + 1][j    ] as FluidMapData;
  354.                 
  355.                 data.n02 = cells2d[i - 1][j + 1as FluidMapData;
  356.                 data.n12 = cells2d[i    ][j + 1as FluidMapData;
  357.                 data.n22 = cells2d[i + 1][j + 1as FluidMapData;
  358.                 
  359.                 if (pi != 0)
  360.                 {
  361.                     data.prev = cells2d[pi][pj] as FluidMapData;
  362.                     data.prev.next = data;
  363.                 }
  364.                 
  365.                 _cells.push(data);
  366.                 
  367.                 pi = i;
  368.                 pj = j;
  369.             }
  370.         }
  371.         _cellCount = _cells.length;
  372.         
  373.         _mapBmd = new BitmapData(_width, _height, false, 0x008080);
  374.         
  375.         _mapFilter = new DisplacementMapFilter();
  376.         _mapFilter.mapBitmap  = _mapBmd;
  377.         _mapFilter.mapPoint   = ZERO_POINT;
  378.         _mapFilter.componentX = BitmapDataChannel.GREEN;
  379.         _mapFilter.componentY = BitmapDataChannel.BLUE;
  380.         _mapFilter.mode       = DisplacementMapFilterMode.CLAMP;
  381.         _mapFilter.scaleX     = _mapScale;
  382.         _mapFilter.scaleY     = _mapScale;
  383.         
  384.         _blurFilter = new BlurFilter(_mapBlurIntensity, _mapBlurIntensity, _mapBlurQuality);
  385.         
  386.         _colorAdjust = -128 * _intensity;
  387.         _distMin2    = 4 * _gridSize * _gridSize;
  388.         _flowSize2   = _flowSize * _flowSize;
  389.         
  390.         _calculator = new FluidCalculator();
  391.     }
  392.     
  393.     //METHODS
  394.     public function addOrientedForce(x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  395.     {
  396.         var s:uint = _gridSize;
  397.         var n:uint = _cellCount;
  398.         var cell:FluidMapData = _cells[0as FluidMapData;
  399.         
  400.         for (var i:uint = 0; i < n; ++i)
  401.         {
  402.             _calcOrientedForce(cell, x / s, y / s, forceX, forceY);
  403.             cell = cell.next;
  404.         }
  405.     }
  406.     
  407.     public function updateMap():BitmapData
  408.     {
  409.         var s:uint            = _gridSize;
  410.         var n:uint            = _cellCount;
  411.         var cell:FluidMapData = _cells[0as FluidMapData;
  412.         var map:BitmapData    = _mapBmd;
  413.         
  414.         _decay();
  415.         _updatePressure();
  416.         _updateVelocity();
  417.         
  418.         map.lock();
  419.         map.fillRect(map.rect, 0x008080);
  420.         for (var i:uint = 0; i < n; ++i)
  421.         {
  422.             map.fillRect(
  423.                 new Rectangle(cell.x * s, cell.y * s, s, s),
  424.                 _calcMapColor(cell)
  425.             );
  426.             cell = cell.next;
  427.         }
  428.         map.applyFilter(map, map.rect, ZERO_POINT, _blurFilter);
  429.         map.unlock();
  430.         
  431.         return map;
  432.     }
  433.     
  434.     private function _decay():void
  435.     {
  436.         if (_useDecay)
  437.         {    
  438.             var n:uint = _allCellCount;
  439.             var cell:FluidMapData = _allCells[0as FluidMapData;
  440.             
  441.             for (var i:uint = 0; i < n; ++i)
  442.             {
  443.                 _calcDecay(_allCells[i]);
  444.                 //cell = cell.next;
  445.             }
  446.         }
  447.     }
  448.     
  449.     private function _updatePressure():void
  450.     {
  451.         var n:uint = _cellCount;
  452.         var cell:FluidMapData = _cells[0as FluidMapData;
  453.         
  454.         for (var i:uint = 0; i < n; ++i)
  455.         {
  456.             _calcPressure(cell);
  457.             cell = cell.next;
  458.         }
  459.     }
  460.     
  461.     private function _updateVelocity():void
  462.     {
  463.         var n:uint = _cellCount;
  464.         var cell:FluidMapData = _cells[0as FluidMapData;
  465.         
  466.         for (var i:uint = 0; i < n; ++i)
  467.         {
  468.             _calcVelocity(cell);
  469.             cell = cell.next;
  470.         }
  471.     }
  472.     
  473.     private function _calcDecay(data:FluidMapData):void
  474.     {
  475.         var r:Number = data.colorX;
  476.         var b:Number = data.colorY;
  477.         
  478.         if (r != 128) data.colorX += (r < 128) ? 1 : -1;
  479.         if (b != 128) data.colorY += (b < 128) ? 1 : -1;
  480.     }
  481.     
  482.     private function _calcPressure(data:FluidMapData):void
  483.     { 
  484.         data.pressure += _calculator.calcPressure(data);
  485.         data.pressure *= 0.7;
  486.     }
  487.     
  488.     private function _calcVelocity(data:FluidMapData):void
  489.     {
  490.         data.vx += _calculator.calcVelocityX(data);
  491.         data.vy += _calculator.calcVelocityY(data);
  492.         data.vx *= 0.7;
  493.         data.vy *= 0.7;
  494.     }
  495.     
  496.     private function _calcOrientedForce(data:FluidMapData, x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  497.     {
  498.         var dx:int       = data.x - x;
  499.         var dy:int       = data.y - y;
  500.         var dist2:Number = dx * dx + dy * dy;
  501.         
  502.         if (dist2 < _flowSize2)
  503.         {
  504.             var f:Number = (dist2 < _distMin2) ? 1.0 : (_flowSize / Math.sqrt(dist2));
  505.             
  506.             data.vx += forceX * f;
  507.             data.vy += forceY * f;
  508.         }
  509.     }
  510.     
  511.     private function _calcMapColor(data:FluidMapData):uint
  512.     {
  513.         var vx:Number = data.vx;
  514.         var vy:Number = data.vy;
  515.         var g:int  = data.colorX;
  516.         var b:int  = data.colorY;
  517.         
  518.         g = Math.round( _colorAdjust * vx + g );
  519.         b = Math.round( _colorAdjust * vy + b );
  520.         
  521.         g = (g < 0) ? 0 : (g > 255) ? 255 : g;
  522.         b = (b < 0) ? 0 : (b > 255) ? 255 : b;
  523.         
  524.         data.colorX = g;
  525.         data.colorY = b;
  526.         
  527.         return data.color = g << 8 | b;
  528.     }
  529. }
  530. internal class FluidCalculator
  531. {
  532.     //CONSTRUCTOR
  533.     public function FluidCalculator():void
  534.     {
  535.     }
  536.     
  537.     //METHODS
  538.     public function calcVelocityX(data:FluidMapData):Number
  539.     {
  540.         return (  data.n00.pressure * 0.5
  541.                 + data.n01.pressure
  542.                 + data.n02.pressure * 0.5
  543.                 
  544.                 - data.n20.pressure * 0.5
  545.                 - data.n21.pressure
  546.                 - data.n22.pressure * 0.5
  547.                 
  548.                 ) * 0.25;
  549.     }
  550.     
  551.     public function calcVelocityY(data:FluidMapData):Number
  552.     {
  553.         return (  data.n00.pressure * 0.5
  554.                 + data.n10.pressure
  555.                 + data.n20.pressure * 0.5
  556.                 
  557.                 - data.n02.pressure * 0.5
  558.                 - data.n12.pressure
  559.                 - data.n22.pressure * 0.5
  560.                 
  561.                 ) * 0.25;
  562.     }
  563.     
  564.     public function calcPressure(data:FluidMapData):Number
  565.     {
  566.         return (  data.n00.vx * 0.5
  567.                 + data.n01.vx
  568.                 + data.n02.vx * 0.5
  569.                 - data.n20.vx * 0.5
  570.                 - data.n21.vx
  571.                 - data.n22.vx * 0.5
  572.                 
  573.                 + data.n00.vy * 0.5
  574.                 + data.n10.vy
  575.                 + data.n20.vy * 0.5
  576.                 - data.n02.vy * 0.5
  577.                 - data.n12.vy
  578.                 - data.n22.vy * 0.5
  579.                 
  580.                 ) * 0.20;
  581.     }
  582. }
  583. internal class FluidMapData
  584. {
  585.     //VARIABLES
  586.     public function get x():uint { return _x; }
  587.     public function set x(value:uint):void { _x = value; }
  588.     private var _x:uint;
  589.     
  590.     public function get y():uint { return _y; }
  591.     public function set y(value:uint):void { _y = value; }
  592.     private var _y:uint;
  593.     
  594.     public function get vx():Number { return _vx; }
  595.     public function set vx(value:Number):void { _vx = value; }
  596.     private var _vx:Number;
  597.     
  598.     public function get vy():Number { return _vy; }
  599.     public function set vy(value:Number):void { _vy = value; }
  600.     private var _vy:Number;
  601.     
  602.     public function get pressure():Number { return _pressure; }
  603.     public function set pressure(value:Number):void { _pressure = value; }
  604.     private var _pressure:Number;
  605.     
  606.     public function get color():uint { return _color; }
  607.     public function set color(value:uint):void { _color = value; }
  608.     private var _color:uint;
  609.     
  610.     public function get colorX():uint { return _colorX; }
  611.     public function set colorX(value:uint):void { _colorX = value; }
  612.     private var _colorX:uint;
  613.     
  614.     public function get colorY():uint { return _colorY; }
  615.     public function set colorY(value:uint):void { _colorY = value; }
  616.     private var _colorY:uint;
  617.     
  618.     public function get next():FluidMapData { return _next; }
  619.     public function set next(value:FluidMapData):void { _next = value; }
  620.     private var _next:FluidMapData;
  621.     
  622.     public function get prev():FluidMapData { return _prev; }
  623.     public function set prev(value:FluidMapData):void { _prev = value; }
  624.     private var _prev:FluidMapData;
  625.     
  626.     private var _n00:FluidMapData;
  627.     private var _n01:FluidMapData;
  628.     private var _n02:FluidMapData;
  629.     private var _n10:FluidMapData;
  630.     private var _n12:FluidMapData;
  631.     private var _n20:FluidMapData;
  632.     private var _n21:FluidMapData;
  633.     private var _n22:FluidMapData;
  634.     
  635.     public function get n00():FluidMapData { return _n00; }
  636.     public function get n01():FluidMapData { return _n01; }
  637.     public function get n02():FluidMapData { return _n02; }
  638.     public function get n10():FluidMapData { return _n10; }
  639.     public function get n12():FluidMapData { return _n12; }
  640.     public function get n20():FluidMapData { return _n20; }
  641.     public function get n21():FluidMapData { return _n21; }
  642.     public function get n22():FluidMapData { return _n22; }
  643.     
  644.     public function set n00(value:FluidMapData):void { _n00 = value; }
  645.     public function set n01(value:FluidMapData):void { _n01 = value; }
  646.     public function set n02(value:FluidMapData):void { _n02 = value; }
  647.     public function set n10(value:FluidMapData):void { _n10 = value; }
  648.     public function set n12(value:FluidMapData):void { _n12 = value; }
  649.     public function set n20(value:FluidMapData):void { _n20 = value; }
  650.     public function set n21(value:FluidMapData):void { _n21 = value; }
  651.     public function set n22(value:FluidMapData):void { _n22 = value; }
  652.     
  653.     //CONSTRUCTOR
  654.     public function FluidMapData(x:uint, y:uint):void
  655.     {
  656.         _x        = x;
  657.         _y        = y;
  658.         _vx       = 0;
  659.         _vy       = 0;
  660.         _pressure = 0;
  661.         _color    = 0x008080;
  662.         _colorX   = 128;
  663.         _colorY   = 128;
  664.     }
  665. }
noswf
  1. // forked from alumican_net's Fluid on the Video
  2. /**
  3.  * Fluid on the Video
  4.  * 
  5.  * @author http://alumican.net
  6.  * 
  7.  * 動画はこちらからお借りしています
  8.  * http://www.nicovideo.jp/watch/sm3605606
  9.  */
  10. package
  11. {
  12.     import flash.display.Bitmap;
  13.     import flash.display.BitmapData;
  14.     import flash.display.BlendMode;
  15.     import flash.display.Graphics;
  16.     import flash.display.Sprite;
  17.     import flash.events.Event;
  18.     import flash.events.MouseEvent;
  19.     import flash.events.NetStatusEvent;
  20.     import flash.filters.BlurFilter;
  21.     import flash.filters.ColorMatrixFilter;
  22.     import flash.filters.DisplacementMapFilter;
  23.     import flash.geom.ColorTransform;
  24.     import flash.geom.Matrix;
  25.     import flash.geom.Point;
  26.     import flash.geom.Rectangle;
  27.     import flash.media.SoundTransform;
  28.     import flash.media.Video;
  29.     import flash.net.NetConnection;
  30.     import flash.net.NetStream;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFieldAutoSize;
  33.     import flash.text.TextFormat;
  34.     import net.hires.debug.Stats;
  35.     
  36.     public class FlashTest extends Sprite
  37.     {
  38.         //CLASS CONSTANTS
  39.         private const MAP_WIDTH:Number        = 465;
  40.         private const MAP_HEIGHT:Number       = 465;
  41.         private const MAP_GRID_SIZE:Number    = 20;
  42.         private const MAP_FLOW_SIZE:Number    = 2;
  43.         private const MAP_INTENSITY:Number    = 0.25;
  44.         private const MAP_SCALE:Number        = 150;
  45.         private const MAP_USE_DECAY:Boolean   = true;
  46.         private const MAP_BLUR_INTENSITY:uint = 32;
  47.         private const MAP_BLUR_QUALITY:uint   = 2;
  48.         
  49.         private const ZERO_POINT:Point = new Point(0,0);
  50.         
  51.         //VARIABLES
  52.         private var _container:Sprite;
  53.         
  54.         private var _canvas:BitmapData;
  55.         private var _canvasTone:ColorMatrixFilter;
  56.         
  57.         private var _fluidMap:FluidMap;
  58.         private var _fluidBmp:Bitmap;
  59.         
  60.         private var _mapBmd:BitmapData;
  61.         private var _mapFilter:DisplacementMapFilter;
  62.         
  63.         private var _oldX:Number = 0;
  64.         private var _oldY:Number = 0;
  65.         private var _isMouseMove:Boolean = false;
  66.         
  67.         private var _ns:NetStream;
  68.         private var _nc:NetConnection;
  69.         private var _video:Video;
  70.         private var _videoMatrix:Matrix;
  71.         
  72.         private var _background:Sprite;
  73.         
  74.         private var _stats:Stats;
  75.         private var _usage:TextField;
  76.         
  77.         //CONSTRUCTOR
  78.         public function FlashTest():void
  79.         {
  80.             Wonderfl.disable_capture();
  81.             addEventListener(Event.ADDED_TO_STAGE, _initialize);
  82.         }
  83.         
  84.         //METHODS
  85.         private function _initialize(e:Event):void
  86.         {
  87.             removeEventListener(Event.ADDED_TO_STAGE, _initialize);
  88.             
  89.             _background = new Sprite();
  90.             addChild(_background);
  91.             
  92.             _container = new Sprite();
  93.             addChild(_container);
  94.             
  95.             _canvas = new BitmapData(MAP_WIDTH, MAP_HEIGHT, false, 0xffffff);
  96.             _container.addChild(new Bitmap(_canvas));
  97.             
  98.             _fluidMap = new FluidMap(MAP_WIDTH, MAP_HEIGHT, MAP_GRID_SIZE, MAP_FLOW_SIZE, MAP_INTENSITY, MAP_USE_DECAY, MAP_SCALE, MAP_BLUR_INTENSITY, MAP_BLUR_QUALITY);
  99.             
  100.             _mapFilter = _fluidMap.mapFilter;
  101.             
  102.             _canvasTone = new ColorMatrixFilter([
  103.                 10005,
  104.                 01005,
  105.                 00105,
  106.                 00000
  107.             ]);
  108.             
  109.             _stats = new Stats( {
  110.                 bg:0xffffff,
  111.                 fps:0x333333,
  112.                 ms:0x333333,
  113.                 mem:0x333333,
  114.                 memmax:0x333333
  115.             });
  116.             _stats.blendMode = BlendMode.DARKEN;
  117.             addChild(_stats);
  118.             
  119.             _usage = new TextField();
  120.             _usage.defaultTextFormat = new TextFormat("MS Gothic"11, 0x0);
  121.             _usage.text = "Move mouse on movie";
  122.             _usage.autoSize = TextFieldAutoSize.RIGHT;
  123.             _usage.selectable = false;
  124.             _usage.blendMode = BlendMode.INVERT;
  125.             _usage.visible = false;
  126.             _container.addChild(_usage);
  127.             
  128.             stage.addEventListener(Event.RESIZE, _resizeHandler);
  129.             
  130.             _resizeHandler();
  131.             
  132.             _loadVideo("http://www.planet-ape.net/wonderfl/video_new.flv");
  133.         }
  134.         
  135.         private function _loadVideo(url:String):void
  136.         {
  137.             _nc = new NetConnection();
  138.             _nc.connect(null);
  139.             
  140.             _ns = new NetStream(_nc);
  141.             _ns.addEventListener(NetStatusEvent.NET_STATUS, _videoNetStatusHandler);
  142.             _ns.client = {
  143.                 onMetaData:function(param:Object):void
  144.                 {
  145.                 }
  146.             };
  147.                         _ns.checkPolicyFile = true;
  148.             _ns.play(url);
  149.             
  150.             _video = new Video();
  151.             _video.width  = 300;
  152.             _video.height = 220;
  153.             _video.attachNetStream(_ns);
  154.         }
  155.         
  156.         private function _videoNetStatusHandler(e:NetStatusEvent):void 
  157.         {
  158.                     if(e.info.code == "NetStream.Buffer.Full")
  159.                     {
  160.             _videoMatrix = new Matrix();
  161.             _videoMatrix.scale(_video.width / 320, _video.height / 240);
  162.             
  163.             _videoMatrix.tx = uint( (MAP_WIDTH  - _video.width ) / 2 );
  164.             _videoMatrix.ty = uint( (MAP_HEIGHT - _video.height) / 2 );
  165.             
  166.             _usage.x = _videoMatrix.tx + _video.width - _usage.textWidth;
  167.             _usage.y = _videoMatrix.ty - 20;
  168.             _usage.visible = true;
  169.             
  170.             var st:SoundTransform = _ns.soundTransform;
  171.             st.volume = 0.3;
  172.             _ns.soundTransform = st;
  173.             
  174.             addEventListener(Event.ENTER_FRAME, _update);
  175.             stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
  176.             }
  177.         }
  178.         
  179.         private function _drawCanvas():void
  180.         {
  181.             _canvas.lock();
  182.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _canvasTone);
  183.             _canvas.draw(_video, _videoMatrix);
  184.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _mapFilter);
  185.             
  186.             _canvas.unlock();
  187.         }
  188.         
  189.         private function _refillBackground():void
  190.         {
  191.             var sw:int = stage.stageWidth;
  192.             var sh:int = stage.stageHeight;
  193.             
  194.             var g:Graphics = _background.graphics;
  195.             g.clear();
  196.             g.beginFill(0xffffff);
  197.             g.moveTo(0 , 0 );
  198.             g.lineTo(sw, 0 );
  199.             g.lineTo(sw, sh);
  200.             g.lineTo(0 , sh);
  201.             g.lineTo(0 , 0 );
  202.             g.endFill();
  203.         }
  204.         
  205.         private function _update(e:Event):void
  206.         {
  207.             if (_isMouseMove)
  208.             {
  209.                 var speedX:Number = _container.mouseX - _oldX;
  210.                 var speedY:Number = _container.mouseY - _oldY;
  211.                 
  212.                 _fluidMap.addOrientedForce(_container.mouseX, _container.mouseY, speedX, speedY);
  213.                 
  214.                 _isMouseMove = false;
  215.             }
  216.             
  217.             _fluidMap.updateMap();
  218.             
  219.             _drawCanvas();
  220.             
  221.             _oldX = _container.mouseX;
  222.             _oldY = _container.mouseY;
  223.         }
  224.         
  225.         private function _mouseMoveHandler(e:MouseEvent):void
  226.         {
  227.             _isMouseMove = true;
  228.         }
  229.         
  230.         private function _resizeHandler(e:Event = null):void
  231.         {
  232.             _refillBackground();
  233.             
  234.             _container.x = uint((stage.stageWidth  - MAP_WIDTH ) / 2);
  235.             _container.y = uint((stage.stageHeight - MAP_HEIGHT) / 2);
  236.             
  237.             _stats.x = 2;
  238.             _stats.y = stage.stageHeight - 45;
  239.         }
  240.     }
  241. }
  242. import flash.display.Bitmap;
  243. import flash.display.BitmapData;
  244. import flash.display.BitmapDataChannel;
  245. import flash.filters.BlurFilter;
  246. import flash.filters.DisplacementMapFilter;
  247. import flash.filters.DisplacementMapFilterMode;
  248. import flash.geom.Point;
  249. import flash.geom.Rectangle;
  250. internal class FluidMap
  251. {
  252.     //CLASS CONSTANTS
  253.     private static const ZERO_POINT:Point = new Point(00);
  254.     
  255.     //VARIABLES
  256.     private var _cells:Array;
  257.     private var _cellCount:uint;
  258.     
  259.     private var _allCells:Array;
  260.     private var _allCellCount:uint;
  261.     
  262.     private var _width:uint;
  263.     private var _height:uint;
  264.     
  265.     private var _widthCount:uint;
  266.     private var _heightCount:uint;
  267.     
  268.     private var _gridSize:uint;
  269.     private var _flowSize:Number;
  270.     private var _intensity:Number;
  271.     private var _useDecay:Boolean;
  272.     
  273.     private var _mapBmd:BitmapData;
  274.     private var _mapFilter:DisplacementMapFilter;
  275.     private var _mapScale:Number;
  276.     private var _mapBlurIntensity:uint;
  277.     private var _mapBlurQuality:uint;
  278.     private var _blurFilter:BlurFilter;
  279.     
  280.     private var _calculator:FluidCalculator;
  281.     
  282.     private var _colorAdjust:Number;
  283.     private var _distMin2:Number;
  284.     private var _flowSize2:Number;
  285.     
  286.     public function get mapFilter():DisplacementMapFilter { return _mapFilter; }
  287.     
  288.     //CONSTRUCTOR
  289.     public function FluidMap(
  290.         width:uint,
  291.         height:uint,
  292.         gridSize:uint         = 10,
  293.         flowSize:Number       = 2,
  294.         intensity:Number      = 0.25,
  295.         useDecay:Boolean      = true,
  296.         mapScale:Number       = 100,
  297.         mapBlurIntensity:uint = 32,
  298.         mapBlurQuality:uint   = 2
  299.     ):void
  300.     {
  301.         _width            = width;
  302.         _height           = height;
  303.         _gridSize         = gridSize;
  304.         _flowSize         = flowSize;
  305.         _useDecay         = useDecay;
  306.         _intensity        = intensity;
  307.         _mapScale         = mapScale;
  308.         _mapBlurIntensity = mapBlurIntensity;
  309.         _mapBlurQuality   = mapBlurQuality;
  310.         
  311.         _widthCount  = uint( Math.ceil(width  / gridSize) );
  312.         _heightCount = uint( Math.ceil(height / gridSize) );
  313.         
  314.         _allCellCount = _widthCount * _heightCount;
  315.         _allCells = new Array(_allCellCount);
  316.         var p:uint = 0;
  317.         
  318.                 var i:uint;
  319.                 var j:uint;
  320.                 var data:FluidMapData;
  321.                 
  322.         var cells2d:Array = new Array(_widthCount);
  323.         for (i = 0; i < _widthCount; ++i)
  324.         {
  325.             cells2d[i] = new Array(_heightCount);
  326.             for (j = 0; j < _heightCount; ++j)
  327.             {
  328.                 data = new FluidMapData(i, j);
  329.                 cells2d[i][j] = data;
  330.                 _allCells[p] = data;
  331.                 
  332.                 ++p;
  333.             }
  334.         }
  335.         
  336.         _cells = new Array();
  337.         
  338.         var w:uint = _widthCount  - 1;
  339.         var h:uint = _heightCount - 1;
  340.         var pi:uint = 0;
  341.         var pj:uint = 0;
  342.         for (i = 1; i < w; ++i)
  343.         {
  344.             for (j = 1; j < h; ++j)
  345.             {
  346.                 data = cells2d[i][j] as FluidMapData;
  347.                 
  348.                 data.n00 = cells2d[i - 1][j - 1as FluidMapData;
  349.                 data.n10 = cells2d[i    ][j - 1as FluidMapData;
  350.                 data.n20 = cells2d[i + 1][j - 1as FluidMapData;
  351.                 
  352.                 data.n01 = cells2d[i - 1][j    ] as FluidMapData;
  353.                 data.n21 = cells2d[i + 1][j    ] as FluidMapData;
  354.                 
  355.                 data.n02 = cells2d[i - 1][j + 1as FluidMapData;
  356.                 data.n12 = cells2d[i    ][j + 1as FluidMapData;
  357.                 data.n22 = cells2d[i + 1][j + 1as FluidMapData;
  358.                 
  359.                 if (pi != 0)
  360.                 {
  361.                     data.prev = cells2d[pi][pj] as FluidMapData;
  362.                     data.prev.next = data;
  363.                 }
  364.                 
  365.                 _cells.push(data);
  366.                 
  367.                 pi = i;
  368.                 pj = j;
  369.             }
  370.         }
  371.         _cellCount = _cells.length;
  372.         
  373.         _mapBmd = new BitmapData(_width, _height, false, 0x008080);
  374.         
  375.         _mapFilter = new DisplacementMapFilter();
  376.         _mapFilter.mapBitmap  = _mapBmd;
  377.         _mapFilter.mapPoint   = ZERO_POINT;
  378.         _mapFilter.componentX = BitmapDataChannel.GREEN;
  379.         _mapFilter.componentY = BitmapDataChannel.BLUE;
  380.         _mapFilter.mode       = DisplacementMapFilterMode.CLAMP;
  381.         _mapFilter.scaleX     = _mapScale;
  382.         _mapFilter.scaleY     = _mapScale;
  383.         
  384.         _blurFilter = new BlurFilter(_mapBlurIntensity, _mapBlurIntensity, _mapBlurQuality);
  385.         
  386.         _colorAdjust = -128 * _intensity;
  387.         _distMin2    = 4 * _gridSize * _gridSize;
  388.         _flowSize2   = _flowSize * _flowSize;
  389.         
  390.         _calculator = new FluidCalculator();
  391.     }
  392.     
  393.     //METHODS
  394.     public function addOrientedForce(x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  395.     {
  396.         var s:uint = _gridSize;
  397.         var n:uint = _cellCount;
  398.         var cell:FluidMapData = _cells[0as FluidMapData;
  399.         
  400.         for (var i:uint = 0; i < n; ++i)
  401.         {
  402.             _calcOrientedForce(cell, x / s, y / s, forceX, forceY);
  403.             cell = cell.next;
  404.         }
  405.     }
  406.     
  407.     public function updateMap():BitmapData
  408.     {
  409.         var s:uint            = _gridSize;
  410.         var n:uint            = _cellCount;
  411.         var cell:FluidMapData = _cells[0as FluidMapData;
  412.         var map:BitmapData    = _mapBmd;
  413.         
  414.         _decay();
  415.         _updatePressure();
  416.         _updateVelocity();
  417.         
  418.         map.lock();
  419.         map.fillRect(map.rect, 0x008080);
  420.         for (var i:uint = 0; i < n; ++i)
  421.         {
  422.             map.fillRect(
  423.                 new Rectangle(cell.x * s, cell.y * s, s, s),
  424.                 _calcMapColor(cell)
  425.             );
  426.             cell = cell.next;
  427.         }
  428.         map.applyFilter(map, map.rect, ZERO_POINT, _blurFilter);
  429.         map.unlock();
  430.         
  431.         return map;
  432.     }
  433.     
  434.     private function _decay():void
  435.     {
  436.         if (_useDecay)
  437.         {    
  438.             var n:uint = _allCellCount;
  439.             var cell:FluidMapData = _allCells[0as FluidMapData;
  440.             
  441.             for (var i:uint = 0; i < n; ++i)
  442.             {
  443.                 _calcDecay(_allCells[i]);
  444.                 //cell = cell.next;
  445.             }
  446.         }
  447.     }
  448.     
  449.     private function _updatePressure():void
  450.     {
  451.         var n:uint = _cellCount;
  452.         var cell:FluidMapData = _cells[0as FluidMapData;
  453.         
  454.         for (var i:uint = 0; i < n; ++i)
  455.         {
  456.             _calcPressure(cell);
  457.             cell = cell.next;
  458.         }
  459.     }
  460.     
  461.     private function _updateVelocity():void
  462.     {
  463.         var n:uint = _cellCount;
  464.         var cell:FluidMapData = _cells[0as FluidMapData;
  465.         
  466.         for (var i:uint = 0; i < n; ++i)
  467.         {
  468.             _calcVelocity(cell);
  469.             cell = cell.next;
  470.         }
  471.     }
  472.     
  473.     private function _calcDecay(data:FluidMapData):void
  474.     {
  475.         var r:Number = data.colorX;
  476.         var b:Number = data.colorY;
  477.         
  478.         if (r != 128) data.colorX += (r < 128) ? 1 : -1;
  479.         if (b != 128) data.colorY += (b < 128) ? 1 : -1;
  480.     }
  481.     
  482.     private function _calcPressure(data:FluidMapData):void
  483.     { 
  484.         data.pressure += _calculator.calcPressure(data);
  485.         data.pressure *= 0.7;
  486.     }
  487.     
  488.     private function _calcVelocity(data:FluidMapData):void
  489.     {
  490.         data.vx += _calculator.calcVelocityX(data);
  491.         data.vy += _calculator.calcVelocityY(data);
  492.         data.vx *= 0.7;
  493.         data.vy *= 0.7;
  494.     }
  495.     
  496.     private function _calcOrientedForce(data:FluidMapData, x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  497.     {
  498.         var dx:int       = data.x - x;
  499.         var dy:int       = data.y - y;
  500.         var dist2:Number = dx * dx + dy * dy;
  501.         
  502.         if (dist2 < _flowSize2)
  503.         {
  504.             var f:Number = (dist2 < _distMin2) ? 1.0 : (_flowSize / Math.sqrt(dist2));
  505.             
  506.             data.vx += forceX * f;
  507.             data.vy += forceY * f;
  508.         }
  509.     }
  510.     
  511.     private function _calcMapColor(data:FluidMapData):uint
  512.     {
  513.         var vx:Number = data.vx;
  514.         var vy:Number = data.vy;
  515.         var g:int  = data.colorX;
  516.         var b:int  = data.colorY;
  517.         
  518.         g = Math.round( _colorAdjust * vx + g );
  519.         b = Math.round( _colorAdjust * vy + b );
  520.         
  521.         g = (g < 0) ? 0 : (g > 255) ? 255 : g;
  522.         b = (b < 0) ? 0 : (b > 255) ? 255 : b;
  523.         
  524.         data.colorX = g;
  525.         data.colorY = b;
  526.         
  527.         return data.color = g << 8 | b;
  528.     }
  529. }
  530. internal class FluidCalculator
  531. {
  532.     //CONSTRUCTOR
  533.     public function FluidCalculator():void
  534.     {
  535.     }
  536.     
  537.     //METHODS
  538.     public function calcVelocityX(data:FluidMapData):Number
  539.     {
  540.         return (  data.n00.pressure * 0.5
  541.                 + data.n01.pressure
  542.                 + data.n02.pressure * 0.5
  543.                 
  544.                 - data.n20.pressure * 0.5
  545.                 - data.n21.pressure
  546.                 - data.n22.pressure * 0.5
  547.                 
  548.                 ) * 0.25;
  549.     }
  550.     
  551.     public function calcVelocityY(data:FluidMapData):Number
  552.     {
  553.         return (  data.n00.pressure * 0.5
  554.                 + data.n10.pressure
  555.                 + data.n20.pressure * 0.5
  556.                 
  557.                 - data.n02.pressure * 0.5
  558.                 - data.n12.pressure
  559.                 - data.n22.pressure * 0.5
  560.                 
  561.                 ) * 0.25;
  562.     }
  563.     
  564.     public function calcPressure(data:FluidMapData):Number
  565.     {
  566.         return (  data.n00.vx * 0.5
  567.                 + data.n01.vx
  568.                 + data.n02.vx * 0.5
  569.                 - data.n20.vx * 0.5
  570.                 - data.n21.vx
  571.                 - data.n22.vx * 0.5
  572.                 
  573.                 + data.n00.vy * 0.5
  574.                 + data.n10.vy
  575.                 + data.n20.vy * 0.5
  576.                 - data.n02.vy * 0.5
  577.                 - data.n12.vy
  578.                 - data.n22.vy * 0.5
  579.                 
  580.                 ) * 0.20;
  581.     }
  582. }
  583. internal class FluidMapData
  584. {
  585.     //VARIABLES
  586.     public function get x():uint { return _x; }
  587.     public function set x(value:uint):void { _x = value; }
  588.     private var _x:uint;
  589.     
  590.     public function get y():uint { return _y; }
  591.     public function set y(value:uint):void { _y = value; }
  592.     private var _y:uint;
  593.     
  594.     public function get vx():Number { return _vx; }
  595.     public function set vx(value:Number):void { _vx = value; }
  596.     private var _vx:Number;
  597.     
  598.     public function get vy():Number { return _vy; }
  599.     public function set vy(value:Number):void { _vy = value; }
  600.     private var _vy:Number;
  601.     
  602.     public function get pressure():Number { return _pressure; }
  603.     public function set pressure(value:Number):void { _pressure = value; }
  604.     private var _pressure:Number;
  605.     
  606.     public function get color():uint { return _color; }
  607.     public function set color(value:uint):void { _color = value; }
  608.     private var _color:uint;
  609.     
  610.     public function get colorX():uint { return _colorX; }
  611.     public function set colorX(value:uint):void { _colorX = value; }
  612.     private var _colorX:uint;
  613.     
  614.     public function get colorY():uint { return _colorY; }
  615.     public function set colorY(value:uint):void { _colorY = value; }
  616.     private var _colorY:uint;
  617.     
  618.     public function get next():FluidMapData { return _next; }
  619.     public function set next(value:FluidMapData):void { _next = value; }
  620.     private var _next:FluidMapData;
  621.     
  622.     public function get prev():FluidMapData { return _prev; }
  623.     public function set prev(value:FluidMapData):void { _prev = value; }
  624.     private var _prev:FluidMapData;
  625.     
  626.     private var _n00:FluidMapData;
  627.     private var _n01:FluidMapData;
  628.     private var _n02:FluidMapData;
  629.     private var _n10:FluidMapData;
  630.     private var _n12:FluidMapData;
  631.     private var _n20:FluidMapData;
  632.     private var _n21:FluidMapData;
  633.     private var _n22:FluidMapData;
  634.     
  635.     public function get n00():FluidMapData { return _n00; }
  636.     public function get n01():FluidMapData { return _n01; }
  637.     public function get n02():FluidMapData { return _n02; }
  638.     public function get n10():FluidMapData { return _n10; }
  639.     public function get n12():FluidMapData { return _n12; }
  640.     public function get n20():FluidMapData { return _n20; }
  641.     public function get n21():FluidMapData { return _n21; }
  642.     public function get n22():FluidMapData { return _n22; }
  643.     
  644.     public function set n00(value:FluidMapData):void { _n00 = value; }
  645.     public function set n01(value:FluidMapData):void { _n01 = value; }
  646.     public function set n02(value:FluidMapData):void { _n02 = value; }
  647.     public function set n10(value:FluidMapData):void { _n10 = value; }
  648.     public function set n12(value:FluidMapData):void { _n12 = value; }
  649.     public function set n20(value:FluidMapData):void { _n20 = value; }
  650.     public function set n21(value:FluidMapData):void { _n21 = value; }
  651.     public function set n22(value:FluidMapData):void { _n22 = value; }
  652.     
  653.     //CONSTRUCTOR
  654.     public function FluidMapData(x:uint, y:uint):void
  655.     {
  656.         _x        = x;
  657.         _y        = y;
  658.         _vx       = 0;
  659.         _vy       = 0;
  660.         _pressure = 0;
  661.         _color    = 0x008080;
  662.         _colorX   = 128;
  663.         _colorY   = 128;
  664.     }
  665. }
noswf
  1. // forked from alumican_net's Fluid on the Video
  2. /**
  3.  * Fluid on the Video
  4.  * 
  5.  * @author http://alumican.net
  6.  * 
  7.  * 動画はこちらからお借りしています
  8.  * http://www.nicovideo.jp/watch/sm3605606
  9.  */
  10. package
  11. {
  12.     import flash.display.Bitmap;
  13.     import flash.display.BitmapData;
  14.     import flash.display.BlendMode;
  15.     import flash.display.Graphics;
  16.     import flash.display.Sprite;
  17.     import flash.events.Event;
  18.     import flash.events.MouseEvent;
  19.     import flash.events.NetStatusEvent;
  20.     import flash.filters.BlurFilter;
  21.     import flash.filters.ColorMatrixFilter;
  22.     import flash.filters.DisplacementMapFilter;
  23.     import flash.geom.ColorTransform;
  24.     import flash.geom.Matrix;
  25.     import flash.geom.Point;
  26.     import flash.geom.Rectangle;
  27.     import flash.media.SoundTransform;
  28.     import flash.media.Video;
  29.     import flash.net.NetConnection;
  30.     import flash.net.NetStream;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFieldAutoSize;
  33.     import flash.text.TextFormat;
  34.     import net.hires.debug.Stats;
  35.     
  36.     public class FlashTest extends Sprite
  37.     {
  38.         //CLASS CONSTANTS
  39.         private const MAP_WIDTH:Number        = 465;
  40.         private const MAP_HEIGHT:Number       = 465;
  41.         private const MAP_GRID_SIZE:Number    = 20;
  42.         private const MAP_FLOW_SIZE:Number    = 2;
  43.         private const MAP_INTENSITY:Number    = 0.25;
  44.         private const MAP_SCALE:Number        = 150;
  45.         private const MAP_USE_DECAY:Boolean   = true;
  46.         private const MAP_BLUR_INTENSITY:uint = 32;
  47.         private const MAP_BLUR_QUALITY:uint   = 2;
  48.         
  49.         private const ZERO_POINT:Point = new Point(0,0);
  50.         
  51.         //VARIABLES
  52.         private var _container:Sprite;
  53.         
  54.         private var _canvas:BitmapData;
  55.         private var _canvasTone:ColorMatrixFilter;
  56.         
  57.         private var _fluidMap:FluidMap;
  58.         private var _fluidBmp:Bitmap;
  59.         
  60.         private var _mapBmd:BitmapData;
  61.         private var _mapFilter:DisplacementMapFilter;
  62.         
  63.         private var _oldX:Number = 0;
  64.         private var _oldY:Number = 0;
  65.         private var _isMouseMove:Boolean = false;
  66.         
  67.         private var _ns:NetStream;
  68.         private var _nc:NetConnection;
  69.         private var _video:Video;
  70.         private var _videoMatrix:Matrix;
  71.         
  72.         private var _background:Sprite;
  73.         
  74.         private var _stats:Stats;
  75.         private var _usage:TextField;
  76.         
  77.         //CONSTRUCTOR
  78.         public function FlashTest():void
  79.         {
  80.             Wonderfl.disable_capture();
  81.             addEventListener(Event.ADDED_TO_STAGE, _initialize);
  82.         }
  83.         
  84.         //METHODS
  85.         private function _initialize(e:Event):void
  86.         {
  87.             removeEventListener(Event.ADDED_TO_STAGE, _initialize);
  88.             
  89.             _background = new Sprite();
  90.             addChild(_background);
  91.             
  92.             _container = new Sprite();
  93.             addChild(_container);
  94.             
  95.             _canvas = new BitmapData(MAP_WIDTH, MAP_HEIGHT, false, 0xffffff);
  96.             _container.addChild(new Bitmap(_canvas));
  97.             
  98.             _fluidMap = new FluidMap(MAP_WIDTH, MAP_HEIGHT, MAP_GRID_SIZE, MAP_FLOW_SIZE, MAP_INTENSITY, MAP_USE_DECAY, MAP_SCALE, MAP_BLUR_INTENSITY, MAP_BLUR_QUALITY);
  99.             
  100.             _mapFilter = _fluidMap.mapFilter;
  101.             
  102.             _canvasTone = new ColorMatrixFilter([
  103.                 10005,
  104.                 01005,
  105.                 00105,
  106.                 00000
  107.             ]);
  108.             
  109.             _stats = new Stats( {
  110.                 bg:0xffffff,
  111.                 fps:0x333333,
  112.                 ms:0x333333,
  113.                 mem:0x333333,
  114.                 memmax:0x333333
  115.             });
  116.             _stats.blendMode = BlendMode.DARKEN;
  117.             addChild(_stats);
  118.             
  119.             _usage = new TextField();
  120.             _usage.defaultTextFormat = new TextFormat("MS Gothic"11, 0x0);
  121.             _usage.text = "Move mouse on movie";
  122.             _usage.autoSize = TextFieldAutoSize.RIGHT;
  123.             _usage.selectable = false;
  124.             _usage.blendMode = BlendMode.INVERT;
  125.             _usage.visible = false;
  126.             _container.addChild(_usage);
  127.             
  128.             stage.addEventListener(Event.RESIZE, _resizeHandler);
  129.             
  130.             _resizeHandler();
  131.             
  132.             _loadVideo("http://lab.alumican.net/wonderfl/liquid_video_input.flv");
  133.         }
  134.         
  135.         private function _loadVideo(url:String):void
  136.         {
  137.             _nc = new NetConnection();
  138.             _nc.connect(null);
  139.             
  140.             _ns = new NetStream(_nc);
  141.             _ns.addEventListener(NetStatusEvent.NET_STATUS, _videoNetStatusHandler);
  142.             _ns.client = {
  143.                 onMetaData:function(param:Object):void
  144.                 {
  145.                 }
  146.             };
  147.                         _ns.checkPolicyFile = true;
  148.             _ns.play(url);
  149.             
  150.             _video = new Video();
  151.             _video.width  = 300;
  152.             _video.height = 220;
  153.             _video.attachNetStream(_ns);
  154.         }
  155.         
  156.         private function _videoNetStatusHandler(e:NetStatusEvent):void 
  157.         {
  158.                     if(e.info.code == "NetStream.Buffer.Full")
  159.                     {
  160.             _videoMatrix = new Matrix();
  161.             _videoMatrix.scale(_video.width / 320, _video.height / 240);
  162.             
  163.             _videoMatrix.tx = uint( (MAP_WIDTH  - _video.width ) / 2 );
  164.             _videoMatrix.ty = uint( (MAP_HEIGHT - _video.height) / 2 );
  165.             
  166.             _usage.x = _videoMatrix.tx + _video.width - _usage.textWidth;
  167.             _usage.y = _videoMatrix.ty - 20;
  168.             _usage.visible = true;
  169.             
  170.             var st:SoundTransform = _ns.soundTransform;
  171.             st.volume = 0.3;
  172.             _ns.soundTransform = st;
  173.             
  174.             addEventListener(Event.ENTER_FRAME, _update);
  175.             stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
  176.             }
  177.         }
  178.         
  179.         private function _drawCanvas():void
  180.         {
  181.             _canvas.lock();
  182.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _canvasTone);
  183.             _canvas.draw(_video, _videoMatrix);
  184.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _mapFilter);
  185.             
  186.             _canvas.unlock();
  187.         }
  188.         
  189.         private function _refillBackground():void
  190.         {
  191.             var sw:int = stage.stageWidth;
  192.             var sh:int = stage.stageHeight;
  193.             
  194.             var g:Graphics = _background.graphics;
  195.             g.clear();
  196.             g.beginFill(0xffffff);
  197.             g.moveTo(0 , 0 );
  198.             g.lineTo(sw, 0 );
  199.             g.lineTo(sw, sh);
  200.             g.lineTo(0 , sh);
  201.             g.lineTo(0 , 0 );
  202.             g.endFill();
  203.         }
  204.         
  205.         private function _update(e:Event):void
  206.         {
  207.             if (_isMouseMove)
  208.             {
  209.                 var speedX:Number = _container.mouseX - _oldX;
  210.                 var speedY:Number = _container.mouseY - _oldY;
  211.                 
  212.                 _fluidMap.addOrientedForce(_container.mouseX, _container.mouseY, speedX, speedY);
  213.                 
  214.                 _isMouseMove = false;
  215.             }
  216.             
  217.             _fluidMap.updateMap();
  218.             
  219.             _drawCanvas();
  220.             
  221.             _oldX = _container.mouseX;
  222.             _oldY = _container.mouseY;
  223.         }
  224.         
  225.         private function _mouseMoveHandler(e:MouseEvent):void
  226.         {
  227.             _isMouseMove = true;
  228.         }
  229.         
  230.         private function _resizeHandler(e:Event = null):void
  231.         {
  232.             _refillBackground();
  233.             
  234.             _container.x = uint((stage.stageWidth  - MAP_WIDTH ) / 2);
  235.             _container.y = uint((stage.stageHeight - MAP_HEIGHT) / 2);
  236.             
  237.             _stats.x = 2;
  238.             _stats.y = stage.stageHeight - 45;
  239.         }
  240.     }
  241. }
  242. import flash.display.Bitmap;
  243. import flash.display.BitmapData;
  244. import flash.display.BitmapDataChannel;
  245. import flash.filters.BlurFilter;
  246. import flash.filters.DisplacementMapFilter;
  247. import flash.filters.DisplacementMapFilterMode;
  248. import flash.geom.Point;
  249. import flash.geom.Rectangle;
  250. internal class FluidMap
  251. {
  252.     //CLASS CONSTANTS
  253.     private static const ZERO_POINT:Point = new Point(00);
  254.     
  255.     //VARIABLES
  256.     private var _cells:Array;
  257.     private var _cellCount:uint;
  258.     
  259.     private var _allCells:Array;
  260.     private var _allCellCount:uint;
  261.     
  262.     private var _width:uint;
  263.     private var _height:uint;
  264.     
  265.     private var _widthCount:uint;
  266.     private var _heightCount:uint;
  267.     
  268.     private var _gridSize:uint;
  269.     private var _flowSize:Number;
  270.     private var _intensity:Number;
  271.     private var _useDecay:Boolean;
  272.     
  273.     private var _mapBmd:BitmapData;
  274.     private var _mapFilter:DisplacementMapFilter;
  275.     private var _mapScale:Number;
  276.     private var _mapBlurIntensity:uint;
  277.     private var _mapBlurQuality:uint;
  278.     private var _blurFilter:BlurFilter;
  279.     
  280.     private var _calculator:FluidCalculator;
  281.     
  282.     private var _colorAdjust:Number;
  283.     private var _distMin2:Number;
  284.     private var _flowSize2:Number;
  285.     
  286.     public function get mapFilter():DisplacementMapFilter { return _mapFilter; }
  287.     
  288.     //CONSTRUCTOR
  289.     public function FluidMap(
  290.         width:uint,
  291.         height:uint,
  292.         gridSize:uint         = 10,
  293.         flowSize:Number       = 2,
  294.         intensity:Number      = 0.25,
  295.         useDecay:Boolean      = true,
  296.         mapScale:Number       = 100,
  297.         mapBlurIntensity:uint = 32,
  298.         mapBlurQuality:uint   = 2
  299.     ):void
  300.     {
  301.         _width            = width;
  302.         _height           = height;
  303.         _gridSize         = gridSize;
  304.         _flowSize         = flowSize;
  305.         _useDecay         = useDecay;
  306.         _intensity        = intensity;
  307.         _mapScale         = mapScale;
  308.         _mapBlurIntensity = mapBlurIntensity;
  309.         _mapBlurQuality   = mapBlurQuality;
  310.         
  311.         _widthCount  = uint( Math.ceil(width  / gridSize) );
  312.         _heightCount = uint( Math.ceil(height / gridSize) );
  313.         
  314.         _allCellCount = _widthCount * _heightCount;
  315.         _allCells = new Array(_allCellCount);
  316.         var p:uint = 0;
  317.         
  318.                 var i:uint;
  319.                 var j:uint;
  320.                 var data:FluidMapData;
  321.                 
  322.         var cells2d:Array = new Array(_widthCount);
  323.         for (i = 0; i < _widthCount; ++i)
  324.         {
  325.             cells2d[i] = new Array(_heightCount);
  326.             for (j = 0; j < _heightCount; ++j)
  327.             {
  328.                 data = new FluidMapData(i, j);
  329.                 cells2d[i][j] = data;
  330.                 _allCells[p] = data;
  331.                 
  332.                 ++p;
  333.             }
  334.         }
  335.         
  336.         _cells = new Array();
  337.         
  338.         var w:uint = _widthCount  - 1;
  339.         var h:uint = _heightCount - 1;
  340.         var pi:uint = 0;
  341.         var pj:uint = 0;
  342.         for (i = 1; i < w; ++i)
  343.         {
  344.             for (j = 1; j < h; ++j)
  345.             {
  346.                 data = cells2d[i][j] as FluidMapData;
  347.                 
  348.                 data.n00 = cells2d[i - 1][j - 1as FluidMapData;
  349.                 data.n10 = cells2d[i    ][j - 1as FluidMapData;
  350.                 data.n20 = cells2d[i + 1][j - 1as FluidMapData;
  351.                 
  352.                 data.n01 = cells2d[i - 1][j    ] as FluidMapData;
  353.                 data.n21 = cells2d[i + 1][j    ] as FluidMapData;
  354.                 
  355.                 data.n02 = cells2d[i - 1][j + 1as FluidMapData;
  356.                 data.n12 = cells2d[i    ][j + 1as FluidMapData;
  357.                 data.n22 = cells2d[i + 1][j + 1as FluidMapData;
  358.                 
  359.                 if (pi != 0)
  360.                 {
  361.                     data.prev = cells2d[pi][pj] as FluidMapData;
  362.                     data.prev.next = data;
  363.                 }
  364.                 
  365.                 _cells.push(data);
  366.                 
  367.                 pi = i;
  368.                 pj = j;
  369.             }
  370.         }
  371.         _cellCount = _cells.length;
  372.         
  373.         _mapBmd = new BitmapData(_width, _height, false, 0x008080);
  374.         
  375.         _mapFilter = new DisplacementMapFilter();
  376.         _mapFilter.mapBitmap  = _mapBmd;
  377.         _mapFilter.mapPoint   = ZERO_POINT;
  378.         _mapFilter.componentX = BitmapDataChannel.GREEN;
  379.         _mapFilter.componentY = BitmapDataChannel.BLUE;
  380.         _mapFilter.mode       = DisplacementMapFilterMode.CLAMP;
  381.         _mapFilter.scaleX     = _mapScale;
  382.         _mapFilter.scaleY     = _mapScale;
  383.         
  384.         _blurFilter = new BlurFilter(_mapBlurIntensity, _mapBlurIntensity, _mapBlurQuality);
  385.         
  386.         _colorAdjust = -128 * _intensity;
  387.         _distMin2    = 4 * _gridSize * _gridSize;
  388.         _flowSize2   = _flowSize * _flowSize;
  389.         
  390.         _calculator = new FluidCalculator();
  391.     }
  392.     
  393.     //METHODS
  394.     public function addOrientedForce(x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  395.     {
  396.         var s:uint = _gridSize;
  397.         var n:uint = _cellCount;
  398.         var cell:FluidMapData = _cells[0as FluidMapData;
  399.         
  400.         for (var i:uint = 0; i < n; ++i)
  401.         {
  402.             _calcOrientedForce(cell, x / s, y / s, forceX, forceY);
  403.             cell = cell.next;
  404.         }
  405.     }
  406.     
  407.     public function updateMap():BitmapData
  408.     {
  409.         var s:uint            = _gridSize;
  410.         var n:uint            = _cellCount;
  411.         var cell:FluidMapData = _cells[0as FluidMapData;
  412.         var map:BitmapData    = _mapBmd;
  413.         
  414.         _decay();
  415.         _updatePressure();
  416.         _updateVelocity();
  417.         
  418.         map.lock();
  419.         map.fillRect(map.rect, 0x008080);
  420.         for (var i:uint = 0; i < n; ++i)
  421.         {
  422.             map.fillRect(
  423.                 new Rectangle(cell.x * s, cell.y * s, s, s),
  424.                 _calcMapColor(cell)
  425.             );
  426.             cell = cell.next;
  427.         }
  428.         map.applyFilter(map, map.rect, ZERO_POINT, _blurFilter);
  429.         map.unlock();
  430.         
  431.         return map;
  432.     }
  433.     
  434.     private function _decay():void
  435.     {
  436.         if (_useDecay)
  437.         {    
  438.             var n:uint = _allCellCount;
  439.             var cell:FluidMapData = _allCells[0as FluidMapData;
  440.             
  441.             for (var i:uint = 0; i < n; ++i)
  442.             {
  443.                 _calcDecay(_allCells[i]);
  444.                 //cell = cell.next;
  445.             }
  446.         }
  447.     }
  448.     
  449.     private function _updatePressure():void
  450.     {
  451.         var n:uint = _cellCount;
  452.         var cell:FluidMapData = _cells[0as FluidMapData;
  453.         
  454.         for (var i:uint = 0; i < n; ++i)
  455.         {
  456.             _calcPressure(cell);
  457.             cell = cell.next;
  458.         }
  459.     }
  460.     
  461.     private function _updateVelocity():void
  462.     {
  463.         var n:uint = _cellCount;
  464.         var cell:FluidMapData = _cells[0as FluidMapData;
  465.         
  466.         for (var i:uint = 0; i < n; ++i)
  467.         {
  468.             _calcVelocity(cell);
  469.             cell = cell.next;
  470.         }
  471.     }
  472.     
  473.     private function _calcDecay(data:FluidMapData):void
  474.     {
  475.         var r:Number = data.colorX;
  476.         var b:Number = data.colorY;
  477.         
  478.         if (r != 128) data.colorX += (r < 128) ? 1 : -1;
  479.         if (b != 128) data.colorY += (b < 128) ? 1 : -1;
  480.     }
  481.     
  482.     private function _calcPressure(data:FluidMapData):void
  483.     { 
  484.         data.pressure += _calculator.calcPressure(data);
  485.         data.pressure *= 0.7;
  486.     }
  487.     
  488.     private function _calcVelocity(data:FluidMapData):void
  489.     {
  490.         data.vx += _calculator.calcVelocityX(data);
  491.         data.vy += _calculator.calcVelocityY(data);
  492.         data.vx *= 0.7;
  493.         data.vy *= 0.7;
  494.     }
  495.     
  496.     private function _calcOrientedForce(data:FluidMapData, x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  497.     {
  498.         var dx:int       = data.x - x;
  499.         var dy:int       = data.y - y;
  500.         var dist2:Number = dx * dx + dy * dy;
  501.         
  502.         if (dist2 < _flowSize2)
  503.         {
  504.             var f:Number = (dist2 < _distMin2) ? 1.0 : (_flowSize / Math.sqrt(dist2));
  505.             
  506.             data.vx += forceX * f;
  507.             data.vy += forceY * f;
  508.         }
  509.     }
  510.     
  511.     private function _calcMapColor(data:FluidMapData):uint
  512.     {
  513.         var vx:Number = data.vx;
  514.         var vy:Number = data.vy;
  515.         var g:int  = data.colorX;
  516.         var b:int  = data.colorY;
  517.         
  518.         g = Math.round( _colorAdjust * vx + g );
  519.         b = Math.round( _colorAdjust * vy + b );
  520.         
  521.         g = (g < 0) ? 0 : (g > 255) ? 255 : g;
  522.         b = (b < 0) ? 0 : (b > 255) ? 255 : b;
  523.         
  524.         data.colorX = g;
  525.         data.colorY = b;
  526.         
  527.         return data.color = g << 8 | b;
  528.     }
  529. }
  530. internal class FluidCalculator
  531. {
  532.     //CONSTRUCTOR
  533.     public function FluidCalculator():void
  534.     {
  535.     }
  536.     
  537.     //METHODS
  538.     public function calcVelocityX(data:FluidMapData):Number
  539.     {
  540.         return (  data.n00.pressure * 0.5
  541.                 + data.n01.pressure
  542.                 + data.n02.pressure * 0.5
  543.                 
  544.                 - data.n20.pressure * 0.5
  545.                 - data.n21.pressure
  546.                 - data.n22.pressure * 0.5
  547.                 
  548.                 ) * 0.25;
  549.     }
  550.     
  551.     public function calcVelocityY(data:FluidMapData):Number
  552.     {
  553.         return (  data.n00.pressure * 0.5
  554.                 + data.n10.pressure
  555.                 + data.n20.pressure * 0.5
  556.                 
  557.                 - data.n02.pressure * 0.5
  558.                 - data.n12.pressure
  559.                 - data.n22.pressure * 0.5
  560.                 
  561.                 ) * 0.25;
  562.     }
  563.     
  564.     public function calcPressure(data:FluidMapData):Number
  565.     {
  566.         return (  data.n00.vx * 0.5
  567.                 + data.n01.vx
  568.                 + data.n02.vx * 0.5
  569.                 - data.n20.vx * 0.5
  570.                 - data.n21.vx
  571.                 - data.n22.vx * 0.5
  572.                 
  573.                 + data.n00.vy * 0.5
  574.                 + data.n10.vy
  575.                 + data.n20.vy * 0.5
  576.                 - data.n02.vy * 0.5
  577.                 - data.n12.vy
  578.                 - data.n22.vy * 0.5
  579.                 
  580.                 ) * 0.20;
  581.     }
  582. }
  583. internal class FluidMapData
  584. {
  585.     //VARIABLES
  586.     public function get x():uint { return _x; }
  587.     public function set x(value:uint):void { _x = value; }
  588.     private var _x:uint;
  589.     
  590.     public function get y():uint { return _y; }
  591.     public function set y(value:uint):void { _y = value; }
  592.     private var _y:uint;
  593.     
  594.     public function get vx():Number { return _vx; }
  595.     public function set vx(value:Number):void { _vx = value; }
  596.     private var _vx:Number;
  597.     
  598.     public function get vy():Number { return _vy; }
  599.     public function set vy(value:Number):void { _vy = value; }
  600.     private var _vy:Number;
  601.     
  602.     public function get pressure():Number { return _pressure; }
  603.     public function set pressure(value:Number):void { _pressure = value; }
  604.     private var _pressure:Number;
  605.     
  606.     public function get color():uint { return _color; }
  607.     public function set color(value:uint):void { _color = value; }
  608.     private var _color:uint;
  609.     
  610.     public function get colorX():uint { return _colorX; }
  611.     public function set colorX(value:uint):void { _colorX = value; }
  612.     private var _colorX:uint;
  613.     
  614.     public function get colorY():uint { return _colorY; }
  615.     public function set colorY(value:uint):void { _colorY = value; }
  616.     private var _colorY:uint;
  617.     
  618.     public function get next():FluidMapData { return _next; }
  619.     public function set next(value:FluidMapData):void { _next = value; }
  620.     private var _next:FluidMapData;
  621.     
  622.     public function get prev():FluidMapData { return _prev; }
  623.     public function set prev(value:FluidMapData):void { _prev = value; }
  624.     private var _prev:FluidMapData;
  625.     
  626.     private var _n00:FluidMapData;
  627.     private var _n01:FluidMapData;
  628.     private var _n02:FluidMapData;
  629.     private var _n10:FluidMapData;
  630.     private var _n12:FluidMapData;
  631.     private var _n20:FluidMapData;
  632.     private var _n21:FluidMapData;
  633.     private var _n22:FluidMapData;
  634.     
  635.     public function get n00():FluidMapData { return _n00; }
  636.     public function get n01():FluidMapData { return _n01; }
  637.     public function get n02():FluidMapData { return _n02; }
  638.     public function get n10():FluidMapData { return _n10; }
  639.     public function get n12():FluidMapData { return _n12; }
  640.     public function get n20():FluidMapData { return _n20; }
  641.     public function get n21():FluidMapData { return _n21; }
  642.     public function get n22():FluidMapData { return _n22; }
  643.     
  644.     public function set n00(value:FluidMapData):void { _n00 = value; }
  645.     public function set n01(value:FluidMapData):void { _n01 = value; }
  646.     public function set n02(value:FluidMapData):void { _n02 = value; }
  647.     public function set n10(value:FluidMapData):void { _n10 = value; }
  648.     public function set n12(value:FluidMapData):void { _n12 = value; }
  649.     public function set n20(value:FluidMapData):void { _n20 = value; }
  650.     public function set n21(value:FluidMapData):void { _n21 = value; }
  651.     public function set n22(value:FluidMapData):void { _n22 = value; }
  652.     
  653.     //CONSTRUCTOR
  654.     public function FluidMapData(x:uint, y:uint):void
  655.     {
  656.         _x        = x;
  657.         _y        = y;
  658.         _vx       = 0;
  659.         _vy       = 0;
  660.         _pressure = 0;
  661.         _color    = 0x008080;
  662.         _colorX   = 128;
  663.         _colorY   = 128;
  664.     }
  665. }
noswf
  1. // forked from alumican_net's Fluid on the Video
  2. /**
  3.  * Fluid on the Video
  4.  * 
  5.  * @author http://alumican.net
  6.  * 
  7.  * 動画はこちらからお借りしています
  8.  * http://www.nicovideo.jp/watch/sm3605606
  9.  */
  10. package
  11. {
  12.     import flash.display.Bitmap;
  13.     import flash.display.BitmapData;
  14.     import flash.display.BlendMode;
  15.     import flash.display.Graphics;
  16.     import flash.display.Sprite;
  17.     import flash.events.Event;
  18.     import flash.events.MouseEvent;
  19.     import flash.events.NetStatusEvent;
  20.     import flash.filters.BlurFilter;
  21.     import flash.filters.ColorMatrixFilter;
  22.     import flash.filters.DisplacementMapFilter;
  23.     import flash.geom.ColorTransform;
  24.     import flash.geom.Matrix;
  25.     import flash.geom.Point;
  26.     import flash.geom.Rectangle;
  27.     import flash.media.SoundTransform;
  28.     import flash.media.Video;
  29.     import flash.net.NetConnection;
  30.     import flash.net.NetStream;
  31.     import flash.text.TextField;
  32.     import flash.text.TextFieldAutoSize;
  33.     import flash.text.TextFormat;
  34.     import net.hires.debug.Stats;
  35.     
  36.     public class FlashTest extends Sprite
  37.     {
  38.         //CLASS CONSTANTS
  39.         private const MAP_WIDTH:Number        = 465;
  40.         private const MAP_HEIGHT:Number       = 465;
  41.         private const MAP_GRID_SIZE:Number    = 20;
  42.         private const MAP_FLOW_SIZE:Number    = 2;
  43.         private const MAP_INTENSITY:Number    = 0.25;
  44.         private const MAP_SCALE:Number        = 150;
  45.         private const MAP_USE_DECAY:Boolean   = true;
  46.         private const MAP_BLUR_INTENSITY:uint = 32;
  47.         private const MAP_BLUR_QUALITY:uint   = 2;
  48.         
  49.         private const ZERO_POINT:Point = new Point(0,0);
  50.         
  51.         //VARIABLES
  52.         private var _container:Sprite;
  53.         
  54.         private var _canvas:BitmapData;
  55.         private var _canvasTone:ColorMatrixFilter;
  56.         
  57.         private var _fluidMap:FluidMap;
  58.         private var _fluidBmp:Bitmap;
  59.         
  60.         private var _mapBmd:BitmapData;
  61.         private var _mapFilter:DisplacementMapFilter;
  62.         
  63.         private var _oldX:Number = 0;
  64.         private var _oldY:Number = 0;
  65.         private var _isMouseMove:Boolean = false;
  66.         
  67.         private var _ns:NetStream;
  68.         private var _nc:NetConnection;
  69.         private var _video:Video;
  70.         private var _videoMatrix:Matrix;
  71.         
  72.         private var _background:Sprite;
  73.         
  74.         private var _stats:Stats;
  75.         private var _usage:TextField;
  76.         
  77.         //CONSTRUCTOR
  78.         public function FlashTest():void
  79.         {
  80.             Wonderfl.disable_capture();
  81.             addEventListener(Event.ADDED_TO_STAGE, _initialize);
  82.         }
  83.         
  84.         //METHODS
  85.         private function _initialize(e:Event):void
  86.         {
  87.             removeEventListener(Event.ADDED_TO_STAGE, _initialize);
  88.             
  89.             _background = new Sprite();
  90.             addChild(_background);
  91.             
  92.             _container = new Sprite();
  93.             addChild(_container);
  94.             
  95.             _canvas = new BitmapData(MAP_WIDTH, MAP_HEIGHT, false, 0xffffff);
  96.             _container.addChild(new Bitmap(_canvas));
  97.             
  98.             _fluidMap = new FluidMap(MAP_WIDTH, MAP_HEIGHT, MAP_GRID_SIZE, MAP_FLOW_SIZE, MAP_INTENSITY, MAP_USE_DECAY, MAP_SCALE, MAP_BLUR_INTENSITY, MAP_BLUR_QUALITY);
  99.             
  100.             _mapFilter = _fluidMap.mapFilter;
  101.             
  102.             _canvasTone = new ColorMatrixFilter([
  103.                 10005,
  104.                 01005,
  105.                 00105,
  106.                 00000
  107.             ]);
  108.             
  109.             _stats = new Stats( {
  110.                 bg:0xffffff,
  111.                 fps:0x333333,
  112.                 ms:0x333333,
  113.                 mem:0x333333,
  114.                 memmax:0x333333
  115.             });
  116.             _stats.blendMode = BlendMode.DARKEN;
  117.             addChild(_stats);
  118.             
  119.             _usage = new TextField();
  120.             _usage.defaultTextFormat = new TextFormat("MS Gothic"11, 0x0);
  121.             _usage.text = "Move mouse on movie";
  122.             _usage.autoSize = TextFieldAutoSize.RIGHT;
  123.             _usage.selectable = false;
  124.             _usage.blendMode = BlendMode.INVERT;
  125.             _usage.visible = false;
  126.             _container.addChild(_usage);
  127.             
  128.             stage.addEventListener(Event.RESIZE, _resizeHandler);
  129.             
  130.             _resizeHandler();
  131.             
  132.             _loadVideo("http://www.youtube.com/watch?v=U49_tSB_HrE&feature=rec-HM-r2");
  133.         }
  134.         
  135.         private function _loadVideo(url:String):void
  136.         {
  137.             _nc = new NetConnection();
  138.             _nc.connect(null);
  139.             
  140.             _ns = new NetStream(_nc);
  141.             _ns.addEventListener(NetStatusEvent.NET_STATUS, _videoNetStatusHandler);
  142.             _ns.client = {
  143.                 onMetaData:function(param:Object):void
  144.                 {
  145.                 }
  146.             };
  147.                         _ns.checkPolicyFile = true;
  148.             _ns.play(url);
  149.             
  150.             _video = new Video();
  151.             _video.width  = 300;
  152.             _video.height = 220;
  153.             _video.attachNetStream(_ns);
  154.         }
  155.         
  156.         private function _videoNetStatusHandler(e:NetStatusEvent):void 
  157.         {
  158.                     if(e.info.code == "NetStream.Buffer.Full")
  159.                     {
  160.             _videoMatrix = new Matrix();
  161.             _videoMatrix.scale(_video.width / 320, _video.height / 240);
  162.             
  163.             _videoMatrix.tx = uint( (MAP_WIDTH  - _video.width ) / 2 );
  164.             _videoMatrix.ty = uint( (MAP_HEIGHT - _video.height) / 2 );
  165.             
  166.             _usage.x = _videoMatrix.tx + _video.width - _usage.textWidth;
  167.             _usage.y = _videoMatrix.ty - 20;
  168.             _usage.visible = true;
  169.             
  170.             var st:SoundTransform = _ns.soundTransform;
  171.             st.volume = 0.3;
  172.             _ns.soundTransform = st;
  173.             
  174.             addEventListener(Event.ENTER_FRAME, _update);
  175.             stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
  176.             }
  177.         }
  178.         
  179.         private function _drawCanvas():void
  180.         {
  181.             _canvas.lock();
  182.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _canvasTone);
  183.             _canvas.draw(_video, _videoMatrix);
  184.             _canvas.applyFilter(_canvas, _canvas.rect, ZERO_POINT, _mapFilter);
  185.             
  186.             _canvas.unlock();
  187.         }
  188.         
  189.         private function _refillBackground():void
  190.         {
  191.             var sw:int = stage.stageWidth;
  192.             var sh:int = stage.stageHeight;
  193.             
  194.             var g:Graphics = _background.graphics;
  195.             g.clear();
  196.             g.beginFill(0xffffff);
  197.             g.moveTo(0 , 0 );
  198.             g.lineTo(sw, 0 );
  199.             g.lineTo(sw, sh);
  200.             g.lineTo(0 , sh);
  201.             g.lineTo(0 , 0 );
  202.             g.endFill();
  203.         }
  204.         
  205.         private function _update(e:Event):void
  206.         {
  207.             if (_isMouseMove)
  208.             {
  209.                 var speedX:Number = _container.mouseX - _oldX;
  210.                 var speedY:Number = _container.mouseY - _oldY;
  211.                 
  212.                 _fluidMap.addOrientedForce(_container.mouseX, _container.mouseY, speedX, speedY);
  213.                 
  214.                 _isMouseMove = false;
  215.             }
  216.             
  217.             _fluidMap.updateMap();
  218.             
  219.             _drawCanvas();
  220.             
  221.             _oldX = _container.mouseX;
  222.             _oldY = _container.mouseY;
  223.         }
  224.         
  225.         private function _mouseMoveHandler(e:MouseEvent):void
  226.         {
  227.             _isMouseMove = true;
  228.         }
  229.         
  230.         private function _resizeHandler(e:Event = null):void
  231.         {
  232.             _refillBackground();
  233.             
  234.             _container.x = uint((stage.stageWidth  - MAP_WIDTH ) / 2);
  235.             _container.y = uint((stage.stageHeight - MAP_HEIGHT) / 2);
  236.             
  237.             _stats.x = 2;
  238.             _stats.y = stage.stageHeight - 45;
  239.         }
  240.     }
  241. }
  242. import flash.display.Bitmap;
  243. import flash.display.BitmapData;
  244. import flash.display.BitmapDataChannel;
  245. import flash.filters.BlurFilter;
  246. import flash.filters.DisplacementMapFilter;
  247. import flash.filters.DisplacementMapFilterMode;
  248. import flash.geom.Point;
  249. import flash.geom.Rectangle;
  250. internal class FluidMap
  251. {
  252.     //CLASS CONSTANTS
  253.     private static const ZERO_POINT:Point = new Point(00);
  254.     
  255.     //VARIABLES
  256.     private var _cells:Array;
  257.     private var _cellCount:uint;
  258.     
  259.     private var _allCells:Array;
  260.     private var _allCellCount:uint;
  261.     
  262.     private var _width:uint;
  263.     private var _height:uint;
  264.     
  265.     private var _widthCount:uint;
  266.     private var _heightCount:uint;
  267.     
  268.     private var _gridSize:uint;
  269.     private var _flowSize:Number;
  270.     private var _intensity:Number;
  271.     private var _useDecay:Boolean;
  272.     
  273.     private var _mapBmd:BitmapData;
  274.     private var _mapFilter:DisplacementMapFilter;
  275.     private var _mapScale:Number;
  276.     private var _mapBlurIntensity:uint;
  277.     private var _mapBlurQuality:uint;
  278.     private var _blurFilter:BlurFilter;
  279.     
  280.     private var _calculator:FluidCalculator;
  281.     
  282.     private var _colorAdjust:Number;
  283.     private var _distMin2:Number;
  284.     private var _flowSize2:Number;
  285.     
  286.     public function get mapFilter():DisplacementMapFilter { return _mapFilter; }
  287.     
  288.     //CONSTRUCTOR
  289.     public function FluidMap(
  290.         width:uint,
  291.         height:uint,
  292.         gridSize:uint         = 10,
  293.         flowSize:Number       = 2,
  294.         intensity:Number      = 0.25,
  295.         useDecay:Boolean      = true,
  296.         mapScale:Number       = 100,
  297.         mapBlurIntensity:uint = 32,
  298.         mapBlurQuality:uint   = 2
  299.     ):void
  300.     {
  301.         _width            = width;
  302.         _height           = height;
  303.         _gridSize         = gridSize;
  304.         _flowSize         = flowSize;
  305.         _useDecay         = useDecay;
  306.         _intensity        = intensity;
  307.         _mapScale         = mapScale;
  308.         _mapBlurIntensity = mapBlurIntensity;
  309.         _mapBlurQuality   = mapBlurQuality;
  310.         
  311.         _widthCount  = uint( Math.ceil(width  / gridSize) );
  312.         _heightCount = uint( Math.ceil(height / gridSize) );
  313.         
  314.         _allCellCount = _widthCount * _heightCount;
  315.         _allCells = new Array(_allCellCount);
  316.         var p:uint = 0;
  317.         
  318.                 var i:uint;
  319.                 var j:uint;
  320.                 var data:FluidMapData;
  321.                 
  322.         var cells2d:Array = new Array(_widthCount);
  323.         for (i = 0; i < _widthCount; ++i)
  324.         {
  325.             cells2d[i] = new Array(_heightCount);
  326.             for (j = 0; j < _heightCount; ++j)
  327.             {
  328.                 data = new FluidMapData(i, j);
  329.                 cells2d[i][j] = data;
  330.                 _allCells[p] = data;
  331.                 
  332.                 ++p;
  333.             }
  334.         }
  335.         
  336.         _cells = new Array();
  337.         
  338.         var w:uint = _widthCount  - 1;
  339.         var h:uint = _heightCount - 1;
  340.         var pi:uint = 0;
  341.         var pj:uint = 0;
  342.         for (i = 1; i < w; ++i)
  343.         {
  344.             for (j = 1; j < h; ++j)
  345.             {
  346.                 data = cells2d[i][j] as FluidMapData;
  347.                 
  348.                 data.n00 = cells2d[i - 1][j - 1as FluidMapData;
  349.                 data.n10 = cells2d[i    ][j - 1as FluidMapData;
  350.                 data.n20 = cells2d[i + 1][j - 1as FluidMapData;
  351.                 
  352.                 data.n01 = cells2d[i - 1][j    ] as FluidMapData;
  353.                 data.n21 = cells2d[i + 1][j    ] as FluidMapData;
  354.                 
  355.                 data.n02 = cells2d[i - 1][j + 1as FluidMapData;
  356.                 data.n12 = cells2d[i    ][j + 1as FluidMapData;
  357.                 data.n22 = cells2d[i + 1][j + 1as FluidMapData;
  358.                 
  359.                 if (pi != 0)
  360.                 {
  361.                     data.prev = cells2d[pi][pj] as FluidMapData;
  362.                     data.prev.next = data;
  363.                 }
  364.                 
  365.                 _cells.push(data);
  366.                 
  367.                 pi = i;
  368.                 pj = j;
  369.             }
  370.         }
  371.         _cellCount = _cells.length;
  372.         
  373.         _mapBmd = new BitmapData(_width, _height, false, 0x008080);
  374.         
  375.         _mapFilter = new DisplacementMapFilter();
  376.         _mapFilter.mapBitmap  = _mapBmd;
  377.         _mapFilter.mapPoint   = ZERO_POINT;
  378.         _mapFilter.componentX = BitmapDataChannel.GREEN;
  379.         _mapFilter.componentY = BitmapDataChannel.BLUE;
  380.         _mapFilter.mode       = DisplacementMapFilterMode.CLAMP;
  381.         _mapFilter.scaleX     = _mapScale;
  382.         _mapFilter.scaleY     = _mapScale;
  383.         
  384.         _blurFilter = new BlurFilter(_mapBlurIntensity, _mapBlurIntensity, _mapBlurQuality);
  385.         
  386.         _colorAdjust = -128 * _intensity;
  387.         _distMin2    = 4 * _gridSize * _gridSize;
  388.         _flowSize2   = _flowSize * _flowSize;
  389.         
  390.         _calculator = new FluidCalculator();
  391.     }
  392.     
  393.     //METHODS
  394.     public function addOrientedForce(x:uint, y:uint, forceX:Number = 0, forceY:Number = 0):void
  395.     {
  396.         var s:uint = _gridSize;
  397.         var n:uint = _cellCount;
  398.         var cell:FluidMapData = _cells[0as FluidMapData;
  399.         
  400.         for (var i:uint = 0; i < n; ++i)
  401.         {
  402.             _calcOrientedForce(cell, x / s, y / s, forceX, forceY);
  403.             cell = cell.next;
  404.         }
  405.     }
  406.     
  407.     public function updateMap():BitmapData
  408.     {
  409.         var s:uint            = _gridSize;
  410.         var n:uint            = _cellCount;
  411.         var cell:FluidMapData = _cells[0as FluidMapData;
  412.         var map:BitmapData    = _mapBmd;
  413.         
  414.         _decay();
  415.         _updatePressure();
  416.         _updateVelocity();
  417.         
  418.         map.lock();
  419.         map.fillRect(map.rect, 0x008080);
  420.         for (var i:uint = 0; i < n; ++i)
  421.         {
  422.             map.fillRect(
  423.                 new Rectangle(cell.x * s, cell.y * s, s, s),
  424.                 _calcMapColor(cell)
  425.             );
  426.             cell = cell.next;
  427.         }
  428.         map.applyFilter(map, map.rect, ZERO_POINT, _blurFilter);
  429.         map.unlock();
  430.         
  431.         return map;
  432.     }
  433.     
  434.     private function _decay():void
  435.     {
  436.         if (_useDecay)
  437.         {    
  438.             var n:uint = _allCellCount;
  439.             var cell:FluidMapData = _allCells[0as FluidMapData;
  440.             
  441.             for (var i:uint = 0; i < n; ++i)
  442.             {
  443.                 _calcDecay(_allCells[i]);
  444.                 //cell = cell.next;
  445.             }
  446.         }
  447.     }
  448.     
  449.     private function _updatePressure():void
  450.     {
  451.         var n:uint = _cellCount;
  452.         var cell:FluidMapData = _cells[0as FluidMapData;
  453.         
  454.         for (var i:uint = 0; i < n; ++i)
  455.         {
  456.             _calcPressure(cell);
  457.             cell = cell.next;
  458.         }
  459.     }
  460.     
  461.     private function _updateVelocity():void
  462.     {
  463.         var n:uint = _cellCount;
  464.         var cell:FluidMapData = _cells[0as FluidMapData;
  465.         
  466.         for (var i:uint = 0; i < n; ++i)
  467.         {
  468.             _calcVelocity(cell);
  469.             cell = cell.next;
  470.         }
  471.