※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

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


forked from : fladdict's 250000 particle flow shimulation [diff(75)]

FORKED

ナウシカのラストの王蟲のグジャグジャってこんな感じだよね forked from: ナウシカのラストの王蟲のグジャグジャってこんな感じだよね [diff(1)]

  1. // forked from fladdict's ナウシカのラストの王蟲のグジャグジャってこんな感じだよね
  2. // forked from fladdict's 250000 particle flow shimulation
  3. // forked from fladdict's 20万個ぱーてぃくる 途中で飽きたけど 25万個は狙えるはず
  4. // forked from beinteractive's forked from: 10万個ぱーてぃくる - 軽く高速化
  5. // forked from bkzen's 10万個ぱーてぃくる
  6. package  
  7. {
  8.     import flash.display.Sprite;
  9.     import flash.display.StageAlign;
  10.     import flash.display.StageScaleMode;
  11.     import flash.events.Event;
  12.     import flash.events.MouseEvent;
  13.     import net.hires.debug.Stats;
  14.     
  15.     /**
  16.      * なんかいっぱい動かすテスト
  17.      * 洗濯機みたいになってます。
  18.      * @author jc at bk-zen.com
  19.      */
  20.     [SWF(backgroundColor = "0x000000", frameRate = "30")]
  21.     public class Test3 extends Sprite
  22.     {
  23.         protected const NUM_OF_PARTICLES: int = 3000;
  24.         private var main: TestMain;
  25.         
  26.         public function Test3() 
  27.         {
  28.             if (stage) init();
  29.             else addEventListener(Event.ADDED_TO_STAGE, init);
  30.         }
  31.         
  32.         private function init(e:Event = null):void 
  33.         {
  34.             removeEventListener(Event.ADDED_TO_STAGE, init);
  35.             //
  36.             main = new TestMain(stage.stageWidth, stage.stageHeight, NUM_OF_PARTICLES);
  37.             addChild(main.view);
  38.             addChild(new Stats());
  39.             stage.scaleMode = StageScaleMode.NO_SCALE;
  40.             stage.align = StageAlign.TOP_LEFT;
  41.             stage.addEventListener(Event.RESIZE, onResize);
  42.             stage.addEventListener(MouseEvent.CLICK, onClick);
  43.             addEventListener(Event.ENTER_FRAME, onEnter);
  44.             
  45.         }
  46.         
  47.         private function onClick(e:MouseEvent):void 
  48.         {
  49.             main.change();
  50.         }
  51.         
  52.         private function onEnter(e:Event):void 
  53.         {
  54.             main.draw(mouseX, mouseY);
  55.         }
  56.         // りサイズ (未実装)
  57.         private function onResize(e:Event):void 
  58.         {
  59.             main.resize(stage.stageWidth, stage.stageHeight);
  60.         }
  61.     }
  62. }
  63. import flash.display.Bitmap;
  64. import flash.display.BitmapData;
  65. import flash.geom.ColorTransform;
  66. import flash.geom.Point;
  67. import flash.utils.ByteArray;
  68. class TestMain
  69. {
  70.     private var w: int;
  71.     private var h: int;
  72.     private var mw: int;
  73.     private var mh: int;
  74.     public var view: Bitmap;
  75.     private var bmpData: BitmapData;
  76.     private var forceMap: BitmapData;
  77.         private var forceCache: Array;
  78.     private var randomSeed: int;
  79.     private var particles: Particle;
  80.     private var num: int;
  81.     private var color: uint = 0xF0F0FF;
  82.     private var count: int = 0;
  83.     private var colorTr: ColorTransform;
  84.         private var cnt:int = 0;
  85.         //forcemap を getPixel で計算すると重いので、色はキャッシュする。
  86.         private var forceVector:Vector.<int>;
  87.         private var fxVector:Vector.<int>;
  88.         private var fyVector:Vector.<int>;
  89.     
  90.     public function TestMain(w: int, h: int, numOfParticles: int)
  91.     {
  92.         this.w = w;
  93.         mw = w >> 1;
  94.         this.h = h;
  95.         mh = h >> 1;
  96.         bmpData = new BitmapData(w, h, false, 0x00000000);
  97.         forceMap = new BitmapData(mw, mh, false);
  98.         
  99.                 
  100.         view = new Bitmap(bmpData);
  101.         num = numOfParticles;
  102.         var i: int;
  103.         var prev: Particle = particles = new Particle();
  104.         var p: Particle;
  105.         while (++i <= num)
  106.         {
  107.             p = new Particle();
  108.             p.x = Math.random() * w;
  109.             p.y = Math.random() * h;
  110.                         p.rnd = (Math.random()*0.2 + 0.9) * 0.003;
  111.             prev.next = p;
  112.             prev = p;
  113.         }
  114.         colorTr = new ColorTransform(1111, -2, -1, -1);
  115.                 change();
  116.                 
  117.                 
  118.     }
  119.     // 描画、マウスの判定を後で追加予定
  120.     public function draw(mouseX: Number, mouseY: Number): void
  121.     {
  122.                 cnt++;
  123.                 if(cnt>100){
  124.                    change();
  125.                    cnt = 0;
  126.                 }
  127.         var p: Particle = particles;
  128.         var col: uint;
  129.                 var forceIndex:int;
  130.         bmpData.lock();
  131.         bmpData.colorTransform(bmpData.rect, colorTr);
  132.         while ((p = p.next) != null)
  133.         {
  134.                         forceIndex = (p.y>>2) * mw + (p.x>>2);
  135.                         //2万個以上はパーティクルの座標が重なった場合の対策をしないと、
  136.                         //まったく同じ動きをするので意味がないので p.rndに乱数を持たせて、力に揺らぎを与える。                     
  137.             p.vx = p.vx * 0.99 + fxVector[forceIndex] * p.rnd;
  138.                         p.vy = p.vy * 0.99 + fyVector[forceIndex] * p.rnd;
  139.                         p.px = p.x;
  140.                         p.py = p.y;
  141.                         p.x += p.vx;
  142.             p.y += p.vy;
  143.                         var isDraw:Boolean = true;
  144.             if (p.x < 0){
  145.                             p.x += w;
  146.                             isDraw = false;
  147.                         }else if ((p.x >= w) )
  148.                         {
  149.                             p.x -= w
  150.                             isDraw = false;
  151.                         };
  152.             if(p.y < 0)
  153.                         { 
  154.                             p.y += h;
  155.                             isDraw = false;
  156.                         }else if (p.y >= h)
  157.                         { 
  158.                             p.y -= h;
  159.                             isDraw = false;
  160.                         }
  161.             //bmpData.setPixel(p.x >> 0, p.y >> 0, p.col);
  162.                         if(isDraw){
  163.                             lineTo(p.px, p.py, p.x, p.y);
  164.                         }
  165.         }
  166.         bmpData.unlock();
  167.     }
  168.     
  169.     public function change(): void
  170.     {
  171.                 /**
  172.                 この処理は重いので、本当は次のforcemapの生成をバックグラウンドで少しづつ行って、完成したら切り替えるようにする。
  173.                 */
  174.         forceMap.perlinNoise(646416, Math.random() * 0xFFFF, falsetrue7false);
  175.                 if(!forceVector){
  176.                     fxVector = new Vector.<int>(mw*mh,true);
  177.                     fyVector = new Vector.<int>(mw*mh,true);
  178.                 }
  179.                 //force mapをキャッシュする
  180.                 for(var yy:int=0; yy<mh; yy++){
  181.                     for(var xx:int=0; xx<mw; xx++){
  182.                         var col:int = forceMap.getPixel(xx,yy);
  183.                         var pos:int = yy*mw+xx;
  184.                         fxVector[pos] = (col>>16&0xff)-128;
  185.                         fyVector[pos] = (col>>8&0xff)-128;
  186.                     }
  187.                 }
  188.     }
  189.         public function lineTo(x0:Number, y0:Number, x1:Number, y1:Number): void
  190.         {
  191.             var dx:int = ( x1 > x0 ) ? x1 - x0 : x0 - x1;
  192.             var dy:int = ( y1 > y0 ) ? y1 - y0 : y0 - y1;
  193.             var sx:int = ( x1 > x0 ) ? 1 : -1;
  194.             var sy:int = ( y1 > y0 ) ? 1 : -1;
  195.             var i:int, E:int;
  196.             var col:int;
  197.             var r:int, g:int, b:int;
  198.             if ( dx > dy ) {
  199.                 E = -dx;
  200.             for (i = 0 ; i <= dx ; i++ ) {
  201.               col = bmpData.getPixel(x0,y0);
  202.               r = ((col>>16&0xff) *0.7 + 0xff*0.3);
  203.               g = ((col>>8&0xff) *0.7 + 0xff*0.3);
  204.               b = ((col>>0xff)*0.6 + 0xff*0.2);
  205.               bmpData.setPixel(x0, y0,(r<<16)|(g<<8)|b);
  206.               x0 += sx;
  207.               E += 2 * dy;
  208.               if ( E >= 0 ) {
  209.                 y0 += sy;
  210.                    E -= 2 * dx;
  211.               }
  212.             }
  213.           } else {
  214.             E = -dy;
  215.             for (i = 0 ; i <= dy ; i++ ) {
  216.               col = bmpData.getPixel(x0,y0);
  217.               r = ((col>>16&0xff) + 0xff) >> 1;
  218.               g = ((col>>8&0xff) + 0xff) >> 1;
  219.               b = ((col>>0xff) + 0xff) >> 1;
  220.               bmpData.setPixel(x0, y0,(r<<16)|(g<<8)|b);
  221.               y0 += sy;
  222.               E += 2 * dx;
  223.               if ( E >= 0 ) {
  224.                 x0 += sx;
  225.                 E -= 2 * dy;
  226.           }
  227.     }
  228.   }
  229.         }
  230.     
  231.     public function resize(w: Number, h: Number): void
  232.     {
  233.         this.w = w;
  234.         this.h = h;
  235.         if (bmpData) 
  236.         {
  237.             bmpData.dispose();
  238.         }
  239.         bmpData = new BitmapData(w, h, false, 0x00000000);
  240.         view.bitmapData = bmpData;
  241.     }
  242. }
  243. class Particle
  244. {
  245.     public var x: Number = 0;
  246.     public var y: Number = 0;
  247.         public var px:Number = 0;
  248.         public var py:Number = 0;
  249.     public var vx: Number = 0;
  250.     public var vy: Number = 0;
  251.         public var rnd: Number = 0;
  252.         public var col:int= 0xffffff;
  253.     public var next: Particle;
  254.     
  255.     public function Particle()
  256.     {
  257.         
  258.     }
  259. }
noswf

ナウシカのラストの王蟲のグジャグジャってこんな感じだよね forked from: ナウシカのラストの王蟲のグジャグジャってこんな感じだよね [diff(4)]

  1. // forked from fladdict's ナウシカのラストの王蟲のグジャグジャってこんな感じだよね
  2. // forked from fladdict's 250000 particle flow shimulation
  3. // forked from fladdict's 20万個ぱーてぃくる 途中で飽きたけど 25万個は狙えるはず
  4. // forked from beinteractive's forked from: 10万個ぱーてぃくる - 軽く高速化
  5. // forked from bkzen's 10万個ぱーてぃくる
  6. package  
  7. {    import flash.display.Sprite;
  8.     import flash.display.StageAlign;
  9.     import flash.display.StageScaleMode;
  10.     import flash.events.Event;
  11.     import flash.events.MouseEvent;
  12.     import net.hires.debug.Stats;
  13.     
  14.     /**
  15.      * なんかいっぱい動かすテスト
  16.      * 洗濯機みたいになってます。
  17.      * @author jc at bk-zen.com
  18.      */
  19.     [SWF(backgroundColor = "0x000000", frameRate = "30")]
  20.     public class Test3 extends Sprite
  21.     {
  22.         protected const NUM_OF_PARTICLES: int = 3000;
  23.         private var main: TestMain;
  24.         
  25.         public function Test3() 
  26.         {
  27.             if (stage) init();
  28.             else addEventListener(Event.ADDED_TO_STAGE, init);
  29.         }
  30.         
  31.         private function init(e:Event = null):void 
  32.         {
  33.             removeEventListener(Event.ADDED_TO_STAGE, init);
  34.             //
  35.             main = new TestMain(stage.stageWidth, stage.stageHeight, NUM_OF_PARTICLES);
  36.             addChild(main.view);
  37.             addChild(new Stats());
  38.             stage.scaleMode = StageScaleMode.NO_SCALE;
  39.             stage.align = StageAlign.TOP_LEFT;
  40.             stage.addEventListener(Event.RESIZE, onResize);
  41.             stage.addEventListener(MouseEvent.CLICK, onClick);
  42.             addEventListener(Event.ENTER_FRAME, onEnter);
  43.             
  44.         }
  45.         
  46.         private function onClick(e:MouseEvent):void 
  47.         {
  48.             main.change();
  49.         }
  50.         
  51.         private function onEnter(e:Event):void 
  52.         {
  53.             main.draw(mouseX, mouseY);
  54.         }
  55.         // りサイズ (未実装)
  56.         private function onResize(e:Event):void 
  57.         {
  58.             main.resize(stage.stageWidth, stage.stageHeight);
  59.         }
  60.     }
  61. }
  62. import flash.display.Bitmap;
  63. import flash.display.BitmapData;
  64. import flash.geom.ColorTransform;
  65. import flash.geom.Point;
  66. import flash.utils.ByteArray;
  67. class TestMain
  68. {
  69.     private var w: int;
  70.     private var h: int;
  71.     private var mw: int;
  72.     private var mh: int;
  73.     public var view: Bitmap;
  74.     private var bmpData: BitmapData;
  75.     private var forceMap: BitmapData;
  76.         private var forceCache: Array;
  77.     private var randomSeed: int;
  78.     private var particles: Particle;
  79.     private var num: int;
  80.     private var color: uint = 0xF0F0FF;
  81.     private var count: int = 0;
  82.     private var colorTr: ColorTransform;
  83.         private var cnt:int = 0;
  84.         //forcemap を getPixel で計算すると重いので、色はキャッシュする。
  85.         private var forceVector:Vector.<int>;
  86.         private var fxVector:Vector.<int>;
  87.         private var fyVector:Vector.<int>;
  88.     
  89.     public function TestMain(w: int, h: int, numOfParticles: int)
  90.     {
  91.         this.w = w;
  92.         mw = w >> 1;
  93.         this.h = h;
  94.         mh = h >> 1;
  95.         bmpData = new BitmapData(w, h, false, 0x00000000);
  96.         forceMap = new BitmapData(mw, mh, false);
  97.         
  98.                 
  99.         view = new Bitmap(bmpData);
  100.         num = numOfParticles;
  101.         var i: int;
  102.         var prev: Particle = particles = new Particle();
  103.         var p: Particle;
  104.         while (++i <= num)
  105.         {
  106.             p = new Particle();
  107.             p.x = Math.random() * w;
  108.             p.y = Math.random() * h;
  109.                         p.rnd = (Math.random()*0.2 + 0.9) * 0.003;
  110.             prev.next = p;
  111.             prev = p;
  112.         }
  113.         colorTr = new ColorTransform(1111, -2, -1, -1);
  114.                 change();
  115.                 
  116.                 
  117.     }
  118.     // 描画、マウスの判定を後で追加予定
  119.     public function draw(mouseX: Number, mouseY: Number): void
  120.     {
  121.                 cnt++;
  122.                 if(cnt>100){
  123.                    change();
  124.                    cnt = 0;
  125.                 }
  126.         var p: Particle = particles;
  127.         var col: uint;
  128.                 var forceIndex:int;
  129.         bmpData.lock();
  130.         bmpData.colorTransform(bmpData.rect, colorTr);
  131.         while ((p = p.next) != null)
  132.         {
  133.                         forceIndex = (p.y>>2) * mw + (p.x>>2);
  134.                         //2万個以上はパーティクルの座標が重なった場合の対策をしないと、
  135.                         //まったく同じ動きをするので意味がないので p.rndに乱数を持たせて、力に揺らぎを与える。                     
  136.             p.vx = p.vx * 0.99 + fxVector[forceIndex] * p.rnd;
  137.                         p.vy = p.vy * 0.99 + fyVector[forceIndex] * p.rnd;
  138.                         p.px = p.x;
  139.                         p.py = p.y;
  140.                         p.x += p.vx;
  141.             p.y += p.vy;
  142.                         var isDraw:Boolean = true;
  143.             if (p.x < 0){
  144.                             p.x += w;
  145.                             isDraw = false;
  146.                         }else if ((p.x >= w) )
  147.                         {
  148.                             p.x -= w
  149.                             isDraw = false;
  150.                         };
  151.             if(p.y < 0)
  152.                         { 
  153.                             p.y += h;
  154.                             isDraw = false;
  155.                         }else if (p.y >= h)
  156.                         { 
  157.                             p.y -= h;
  158.                             isDraw = false;
  159.                         }
  160.             //bmpData.setPixel(p.x >> 0, p.y >> 0, p.col);
  161.                         if(isDraw){
  162.                             lineTo(p.px, p.py, p.x, p.y);
  163.                         }
  164.         }
  165.         bmpData.unlock();
  166.     }
  167.     
  168.     public function change(): void
  169.     {
  170.                 /**
  171.                 この処理は重いので、本当は次のforcemapの生成をバックグラウンドで少しづつ行って、完成したら切り替えるようにする。
  172.                 */
  173.         forceMap.perlinNoise(646416, Math.random() * 0xFFFF, falsetrue7false);
  174.                 if(!forceVector){
  175.                     fxVector = new Vector.<int>(mw*mh,true);
  176.                     fyVector = new Vector.<int>(mw*mh,true);
  177.                 }
  178.                 //force mapをキャッシュする
  179.                 for(var yy:int=0; yy<mh; yy++){
  180.                     for(var xx:int=0; xx<mw; xx++){
  181.                         var col:int = forceMap.getPixel(xx,yy);
  182.                         var pos:int = yy*mw+xx;
  183.                         fxVector[pos] = (col>>16&0xff)-128;
  184.                         fyVector[pos] = (col>>8&0xff)-128;
  185.                     }
  186.                 }
  187.     }
  188.         public function lineTo(x0:Number, y0:Number, x1:Number, y1:Number): void
  189.         {
  190.             var dx:int = ( x1 > x0 ) ? x1 - x0 : x0 - x1;
  191.             var dy:int = ( y1 > y0 ) ? y1 - y0 : y0 - y1;
  192.             var sx:int = ( x1 > x0 ) ? 1 : -1;
  193.             var sy:int = ( y1 > y0 ) ? 1 : -1;
  194.             var i:int, E:int;
  195.             var col:int;
  196.             var r:int, g:int, b:int;
  197.             if ( dx > dy ) {
  198.                 E = -dx;
  199.             for (i = 0 ; i <= dx ; i++ ) {
  200.               col = bmpData.getPixel(x0,y0);
  201.               r = ((col>>16&0xff) *0.7 + 0xff*0.3);
  202.               g = ((col>>8&0xff) *0.7 + 0xff*0.3);
  203.               b = ((col>>0xff)*0.6 + 0xff*0.2);
  204.               bmpData.setPixel(x0, y0,(r<<16)|(g<<8)|b);
  205.               x0 += sx;
  206.               E += 2 * dy;
  207.               if ( E >= 0 ) {
  208.                 y0 += sy;
  209.                    E -= 2 * dx;
  210.               }
  211.             }
  212.           } else {
  213.             E = -dy;
  214.             for (i = 0 ; i <= dy ; i++ ) {
  215.               col = bmpData.getPixel(x0,y0);
  216.               r = ((col>>16&0xff) + 0xff) >> 1;
  217.               g = ((col>>8&0xff) + 0xff) >> 1;
  218.               b = ((col>>0xff) + 0xff) >> 1;
  219.               bmpData.setPixel(x0, y0,(r<<16)|(g<<8)|b);
  220.               y0 += sy;
  221.               E += 2 * dx;
  222.               if ( E >= 0 ) {
  223.                 x0 += sx;
  224.                 E -= 2 * dy;
  225.           }
  226.     }
  227.   }
  228.         }
  229.     
  230.     public function resize(w: Number, h: Number): void
  231.     {
  232.         this.w = w;
  233.         this.h = h;
  234.         if (bmpData) 
  235.         {
  236.             bmpData.dispose();
  237.         }
  238.         bmpData = new BitmapData(w, h, false, 0x00000000);
  239.         view.bitmapData = bmpData;
  240.     }
  241. }
  242. class Particle
  243. {
  244.     public var x: Number = 0;
  245.     public var y: Number = 0;
  246.         public var px:Number = 0;
  247.         public var py:Number = 0;
  248.     public var vx: Number = 0;
  249.     public var vy: Number = 0;
  250.         public var rnd: Number = 0;
  251.         public var col:int= 0xffffff;
  252.     public var next: Particle;
  253.     
  254.     public function Particle()
  255.     {
  256.         
  257.     }
  258. }
noswf
Get Adobe Flash Player