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


embed

FAVORITE BY
:
:
:
:
:
:
AS3ClassSmokeParticleMake Smoke Interactive with AS3
:
:
:
bitmapDatafuméeeffet fond
:
:
:
:
particle勉強になります
:
:
:
:
霧?煙?的なパーティクル
:
:
:
:
particlesparticlesmokeFITC Particle
:
FITC Particle
:
:
:
BitmapDatafladdict
:
:
:
:
:
:
:
:
:
きれい!
:
:
:
FICC
:
particle
:
:
:
fitc
:
:
fitc
:
particlesfladdict FITC DEMO
:
particles forces bitmapdata
FORKED
  1. // forked from fladdict's FITC: Cool Japan side A / Particle sample of SMOKE
  2. /**
  3. * Sample code for FITC
  4. * Smoke particle effect
  5. */
  6. package {
  7.     import flash.display.Sprite;    
  8.     import flash.display.Bitmap;
  9.     import flash.display.BitmapData;
  10.     import flash.display.BlendMode;
  11.     import flash.events.Event;
  12.     import flash.filters.BlurFilter;
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.utils.ByteArray;
  17.     
  18.     public class FlashTest extends Sprite {
  19.        //View
  20.         protected var canvas:BitmapData;
  21.         protected var canvasBitmap:Bitmap;
  22.         
  23.         //BitmapData that contains the actual froce that is applied to particles.
  24.         protected var forceMap:BitmapData;
  25.         
  26.         //BitmapData that contains the force of the storm.
  27.         protected var stormMap0:BitmapData;
  28.         protected var stormMap1:BitmapData;
  29.         
  30.         //BitmapData that contains the mouse repulsion force of the stage.
  31.         protected var repulsionMap:BitmapData;
  32.         protected var repulsionHistoryMap:BitmapData;
  33.         protected var repulsionFadeMap:BitmapData;
  34.         
  35.         //the number of particles. 5000-200000
  36.         protected var particleNum:int = 10000;
  37.         protected var particles:Vector.<Particle>
  38.         
  39.         //
  40.         protected var stormScaleRatioX:Number;
  41.         protected var stormScaleRatioY:Number;
  42.         protected var stormCycle:Number = 0;
  43.         
  44.         //color transform that guradually changes canvas.
  45.         protected var fadeCanvasColt:ColorTransform;
  46.         protected var canvasBlurFilter:BlurFilter;
  47.         
  48.         //temporaly variables
  49.         protected var tempColt:ColorTransform = new ColorTransform();
  50.         protected var tempPt:Point = new Point();
  51.         protected var tempMat:Matrix = new Matrix();
  52.         
  53.         public function FlashTest() {
  54.             init();
  55.             reset();
  56.         }
  57.         
  58.         protected function init():void
  59.         {
  60.             //build View
  61.             canvasBitmap = new Bitmap(null"auto"false);
  62.             // because canvas is 25% size of screen, we make view 400% bigger.
  63.             canvasBitmap.scaleX = 4;
  64.             canvasBitmap.scaleY = 4;
  65.             addChild(canvasBitmap);
  66.             
  67.             //initialize the map that contains force of the storm;
  68.             stormMap0 = new BitmapData(256,256,false,0x000000);
  69.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  70.             stormMap1 = new BitmapData(256,256,false,0x000000);
  71.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  72.             
  73.             //
  74.             forceMap = new BitmapData(256,256,false,0x000000);
  75.             
  76.             //initialize repulsionMap
  77.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  78.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  79.             repulsionMap = new BitmapData(256,256,false,0);
  80.             
  81.             /**
  82.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  83.             * Red channel of bitmap data contains horizontal force.
  84.             * Green channel of bitmap data contains vertical force. 
  85.             */
  86.             var dx:int, dy:int;
  87.             var dist:Number, fx:Number, fy:Number;
  88.             var col:int;
  89.             for(var yy:int=0; yy<256; yy++)
  90.             {
  91.                 for(var xx:int=0; xx<256; xx++){
  92.                     dx = xx-128;
  93.                     dy = yy-128;
  94.                     dist = Math.sqrt(dx*dx+dy*dy);
  95.                     if(dist<1){
  96.                         fx = 0;
  97.                         fy = 0;
  98.                     }else{
  99.                         fx = dx / dist / dist / dist * 50000;
  100.                         fy = dy / dist / dist / dist * 50000;
  101.                     }
  102.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  103.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  104.                     fx += 128;
  105.                     fy += 128;
  106.                     col = (fx<<16) + (fy<<8)
  107.                     repulsionMap.setPixel(xx,yy,col);
  108.                 }
  109.             }
  110.             //You can see 
  111.             canvasBitmap.bitmapData = repulsionMap;
  112.             
  113.             //initialize particle
  114.             particles = new Vector.<Particle>(particleNum);
  115.             
  116.             var particle:Particle;
  117.             var rr:int;
  118.             for(var i:int=0; i<particleNum; i++)
  119.             {
  120.                 particle = new Particle();
  121.                 particle.x = Math.random() * stage.stageWidth >> 2;
  122.                 particle.y = Math.random() * stage.stageHeight >> 2;
  123.                 
  124.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  125.                 particle.r = particle.g = particle.b = Math.min(Math.random()*8+4,255);  
  126.                 
  127.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  128.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.003;
  129.                 particles[i] = particle;
  130.             }
  131.             
  132.             fadeCanvasColt = new ColorTransform(1,1,1,1,-2,-1,-1,0);
  133.             canvasBlurFilter = new BlurFilter(2,2,3);
  134.             
  135.             //set event listeners
  136.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  137.             stage.addEventListener(Event.RESIZE, resizeHandler);
  138.         }
  139.         
  140.         /**
  141.         *
  142.         * Called when you need reset the view. E.G. resizing screen.
  143.         */
  144.         protected function reset():void
  145.         {
  146.             if(canvas)
  147.                 canvas.dispose();
  148.                 
  149.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  150.             canvas = new BitmapData((stage.stageWidth>>2)+1, (stage.stageHeight>>2)+1false, 0x000000);
  151.             canvasBitmap.bitmapData = canvas;
  152.             
  153.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  154.             stormScaleRatioX = 1 / (stage.stageWidth>>2)*255;
  155.             stormScaleRatioY = 1 / (stage.stageHeight>>2)*255;
  156.             
  157.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  158.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  159.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  160.            
  161.            //canvasBitmap.scaleX = 1;
  162.            //canvasBitmap.scaleY = 1;
  163.         }
  164.         
  165.         
  166.         //We rebuild screen when swf is reseized.
  167.         //It is not required if you only run it on wonderfl.
  168.         protected function resizeHandler(e:Event):void
  169.         {
  170.             reset();
  171.         }
  172.         
  173.         //Update storm and particle every frame.
  174.         protected function enterFrameHandler(e:Event):void
  175.         { 
  176.            updateStormMap();
  177.            updateRepulsionMap();
  178.            updateForceMap();
  179.            updateParticles();
  180.         }
  181.         
  182.         /**
  183.         * We calculate current forcefielde's state by blending two stormMap.
  184.         * Therefore force field gradually changes every frame.
  185.         */
  186.         protected function updateStormMap():void
  187.         {
  188.             stormCycle++;
  189.             if(stormCycle == 180){
  190.                 stormMap0.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  191.             }else if(stormCycle==360){
  192.                 stormMap1.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  193.                 stormCycle = 0;
  194.             }
  195.         }
  196.         
  197.         /**
  198.         * We update repulsionMap according to the current mouse position.
  199.         */
  200.         protected function updateRepulsionMap():void
  201.         {
  202.             tempMat.a = 1;
  203.             tempMat.b = 0;
  204.             tempMat.c = 0;
  205.             tempMat.d = 1;
  206.             tempMat.tx = 0;
  207.             tempMat.ty = 0;
  208.             
  209.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  210.             
  211.             tempMat.translate(-128,-128);
  212.             
  213.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  214.             tempMat.translate(mouseX/4*stormScaleRatioX, mouseY/4*stormScaleRatioY);
  215.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  216.         }
  217.         
  218.         protected function updateForceMap():void
  219.         {
  220.             //generate current force fieldes state with blending two stormMaps.
  221.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  222.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  223.             forceMap.draw(stormMap0, null, tempColt);
  224.             
  225.             //add mouse repulsion force
  226.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  227.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  228.         }
  229.         
  230.         protected function updateParticles():void
  231.         {
  232.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  233.             
  234.             var stageW:int = canvas.width;
  235.             var stageH:int = canvas.height;
  236.             var loopW:int = stageW-1;
  237.             var loopH:int = stageH-1;
  238.             var byteIndex:int;
  239.             
  240.             canvas.lock();
  241.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  242.             
  243.             //Update Paritcle position and draw.
  244.             var col:int, r:int, g:int, b:int;
  245.             for(var i:int=0; i<particleNum; i++)
  246.             {
  247.                 var prt:Particle = particles[i];
  248.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  249.                 prt.vx = prt.vx * 0.96 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  250.                 prt.vy = prt.vy * 0.96 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  251.                 prt.x += prt.vx;
  252.                 prt.y += prt.vy;
  253.                 if(prt.x<0){
  254.                     prt.x = loopW;
  255.                 }else if(prt.x > loopW){
  256.                     prt.x = 1;
  257.                 }
  258.                 if(prt.y<0){
  259.                     prt.y = loopH;
  260.                 }else if(prt.y > loopH){
  261.                     prt.y = 1;
  262.                 }
  263.                 
  264.                 //Self implimentation of addtive color blend mode.
  265.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  266.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  267.                 r = (col>>16&0xff) + prt.r;
  268.                 g = (col>>8&0xff) + prt.g;
  269.                 b = (col&0xff) + prt.b;
  270.                 r = (r<0xff)? r : 0xff;
  271.                 g = (g<0xff)? g : 0xff;
  272.                 b = (b<0xff)? b : 0xff;
  273.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  274.             }
  275.             canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  276.             canvas.unlock();
  277.         }
  278.     }
  279.     
  280. }
  281. class Particle
  282. {
  283.     //particle position
  284.     public var x:Number = 0;
  285.     public var y:Number = 0;
  286.     public var vx:Number = 0;
  287.     public var vy:Number = 0;
  288.     
  289.     //particle color
  290.     public var r:int;
  291.     public var g:int;
  292.     public var b:int;
  293.     
  294.     //scale factor that affects the force applied to the particle.
  295.     public var scaleFactor:Number;
  296. }
noswf

FITC: Cool Japan side A / Particle sample of SMOKE forked from: FITC: Cool Japan side A / Particle sample of SMOKE [diff(1)]

  1. // forked from fladdict's FITC: Cool Japan side A / Particle sample of SMOKE
  2. /**
  3. * Sample code for FITC
  4. * Smoke particle effect
  5. */
  6. package {
  7.     import flash.display.Sprite;    
  8.     import flash.display.Bitmap;
  9.     import flash.display.BitmapData;
  10.     import flash.display.BlendMode;
  11.     import flash.events.Event;
  12.     import flash.filters.BlurFilter;
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.utils.ByteArray;
  17.     
  18.     public class FlashTest extends Sprite {
  19.        //View
  20.         protected var canvas:BitmapData;
  21.         protected var canvasBitmap:Bitmap;
  22.         
  23.         //BitmapData that contains the actual froce that is applied to particles.
  24.         protected var forceMap:BitmapData;
  25.         
  26.         //BitmapData that contains the force of the storm.
  27.         protected var stormMap0:BitmapData;
  28.         protected var stormMap1:BitmapData;
  29.         
  30.         //BitmapData that contains the mouse repulsion force of the stage.
  31.         protected var repulsionMap:BitmapData;
  32.         protected var repulsionHistoryMap:BitmapData;
  33.         protected var repulsionFadeMap:BitmapData;
  34.         
  35.         //the number of particles. 5000-200000
  36.         protected var particleNum:int = 10000;
  37.         protected var particles:Vector.<Particle>
  38.         
  39.         //
  40.         protected var stormScaleRatioX:Number;
  41.         protected var stormScaleRatioY:Number;
  42.         protected var stormCycle:Number = 0;
  43.         
  44.         //color transform that guradually changes canvas.
  45.         protected var fadeCanvasColt:ColorTransform;
  46.         protected var canvasBlurFilter:BlurFilter;
  47.         
  48.         //temporaly variables
  49.         protected var tempColt:ColorTransform = new ColorTransform();
  50.         protected var tempPt:Point = new Point();
  51.         protected var tempMat:Matrix = new Matrix();
  52.         
  53.         public function FlashTest() {
  54.             init();
  55.             reset();
  56.         }
  57.         
  58.         protected function init():void
  59.         {
  60.             //build View
  61.             canvasBitmap = new Bitmap(null"auto"false);
  62.             // because canvas is 25% size of screen, we make view 400% bigger.
  63.             canvasBitmap.scaleX = 4;
  64.             canvasBitmap.scaleY = 4;
  65.             addChild(canvasBitmap);
  66.             
  67.             //initialize the map that contains force of the storm;
  68.             stormMap0 = new BitmapData(256,256,false,0x000000);
  69.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  70.             stormMap1 = new BitmapData(256,256,false,0x000000);
  71.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  72.             
  73.             //
  74.             forceMap = new BitmapData(256,256,false,0x000000);
  75.             
  76.             //initialize repulsionMap
  77.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  78.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  79.             repulsionMap = new BitmapData(256,256,false,0);
  80.             
  81.             /**
  82.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  83.             * Red channel of bitmap data contains horizontal force.
  84.             * Green channel of bitmap data contains vertical force. 
  85.             */
  86.             var dx:int, dy:int;
  87.             var dist:Number, fx:Number, fy:Number;
  88.             var col:int;
  89.             for(var yy:int=0; yy<256; yy++)
  90.             {
  91.                 for(var xx:int=0; xx<256; xx++){
  92.                     dx = xx-128;
  93.                     dy = yy-128;
  94.                     dist = Math.sqrt(dx*dx+dy*dy);
  95.                     if(dist<1){
  96.                         fx = 0;
  97.                         fy = 0;
  98.                     }else{
  99.                         fx = dx / dist / dist / dist * 50000;
  100.                         fy = dy / dist / dist / dist * 50000;
  101.                     }
  102.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  103.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  104.                     fx += 128;
  105.                     fy += 128;
  106.                     col = (fx<<16) + (fy<<8)
  107.                     repulsionMap.setPixel(xx,yy,col);
  108.                 }
  109.             }
  110.             //You can see 
  111.             canvasBitmap.bitmapData = repulsionMap;
  112.             
  113.             //initialize particle
  114.             particles = new Vector.<Particle>(particleNum);
  115.             
  116.             var particle:Particle;
  117.             var rr:int;
  118.             for(var i:int=0; i<particleNum; i++)
  119.             {
  120.                 particle = new Particle();
  121.                 particle.x = Math.random() * stage.stageWidth >> 2;
  122.                 particle.y = Math.random() * stage.stageHeight >> 2;
  123.                 
  124.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  125.                 particle.r = particle.g = particle.b = Math.min(Math.random()*8+4,255);  
  126.                 
  127.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  128.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.003;
  129.                 particles[i] = particle;
  130.             }
  131.             
  132.             fadeCanvasColt = new ColorTransform(1,1,1,1,-2,-1,-1,0);
  133.             canvasBlurFilter = new BlurFilter(2,2,3);
  134.             
  135.             //set event listeners
  136.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  137.             stage.addEventListener(Event.RESIZE, resizeHandler);
  138.         }
  139.         
  140.         /**
  141.         *
  142.         * Called when you need reset the view. E.G. resizing screen.
  143.         */
  144.         protected function reset():void
  145.         {
  146.             if(canvas)
  147.                 canvas.dispose();
  148.                 
  149.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  150.             canvas = new BitmapData((stage.stageWidth>>2)+1, (stage.stageHeight>>2)+1false, 0x000000);
  151.             canvasBitmap.bitmapData = canvas;
  152.             
  153.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  154.             stormScaleRatioX = 1 / (stage.stageWidth>>2)*255;
  155.             stormScaleRatioY = 1 / (stage.stageHeight>>2)*255;
  156.             
  157.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  158.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  159.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  160.            
  161.            //canvasBitmap.scaleX = 1;
  162.            //canvasBitmap.scaleY = 1;
  163.         }
  164.         
  165.         
  166.         //We rebuild screen when swf is reseized.
  167.         //It is not required if you only run it on wonderfl.
  168.         protected function resizeHandler(e:Event):void
  169.         {
  170.             reset();
  171.         }
  172.         
  173.         //Update storm and particle every frame.
  174.         protected function enterFrameHandler(e:Event):void
  175.         { 
  176.            updateStormMap();
  177.            updateRepulsionMap();
  178.            updateForceMap();
  179.            updateParticles();
  180.         }
  181.         
  182.         /**
  183.         * We calculate current forcefielde's state by blending two stormMap.
  184.         * Therefore force field gradually changes every frame.
  185.         */
  186.         protected function updateStormMap():void
  187.         {
  188.             stormCycle++;
  189.             if(stormCycle == 180){
  190.                 stormMap0.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  191.             }else if(stormCycle==360){
  192.                 stormMap1.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  193.                 stormCycle = 0;
  194.             }
  195.         }
  196.         
  197.         /**
  198.         * We update repulsionMap according to the current mouse position.
  199.         */
  200.         protected function updateRepulsionMap():void
  201.         {
  202.             tempMat.a = 1;
  203.             tempMat.b = 0;
  204.             tempMat.c = 0;
  205.             tempMat.d = 1;
  206.             tempMat.tx = 0;
  207.             tempMat.ty = 0;
  208.             
  209.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  210.             
  211.             tempMat.translate(-128,-128);
  212.             
  213.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  214.             tempMat.translate(mouseX/4*stormScaleRatioX, mouseY/4*stormScaleRatioY);
  215.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  216.         }
  217.         
  218.         protected function updateForceMap():void
  219.         {
  220.             //generate current force fieldes state with blending two stormMaps.
  221.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  222.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  223.             forceMap.draw(stormMap0, null, tempColt);
  224.             
  225.             //add mouse repulsion force
  226.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  227.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  228.         }
  229.         
  230.         protected function updateParticles():void
  231.         {
  232.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  233.             
  234.             var stageW:int = canvas.width;
  235.             var stageH:int = canvas.height;
  236.             var loopW:int = stageW-1;
  237.             var loopH:int = stageH-1;
  238.             var byteIndex:int;
  239.             
  240.             canvas.lock();
  241.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  242.             
  243.             //Update Paritcle position and draw.
  244.             var col:int, r:int, g:int, b:int;
  245.             for(var i:int=0; i<particleNum; i++)
  246.             {
  247.                 var prt:Particle = particles[i];
  248.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  249.                 prt.vx = prt.vx * 0.96 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  250.                 prt.vy = prt.vy * 0.96 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  251.                 prt.x += prt.vx;
  252.                 prt.y += prt.vy;
  253.                 if(prt.x<0){
  254.                     prt.x = loopW;
  255.                 }else if(prt.x > loopW){
  256.                     prt.x = 1;
  257.                 }
  258.                 if(prt.y<0){
  259.                     prt.y = loopH;
  260.                 }else if(prt.y > loopH){
  261.                     prt.y = 1;
  262.                 }
  263.                 
  264.                 //Self implimentation of addtive color blend mode.
  265.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  266.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  267.                 r = (col>>16&0xff) + prt.r;
  268.                 g = (col>>8&0xff) + prt.g;
  269.                 b = (col&0xff) + prt.b;
  270.                 r = (r<0xff)? r : 0xff;
  271.                 g = (g<0xff)? g : 0xff;
  272.                 b = (b<0xff)? b : 0xff;
  273.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  274.             }
  275.             canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  276.             canvas.unlock();
  277.         }
  278.     }
  279.     
  280. }
  281. class Particle
  282. {
  283.     //particle position
  284.     public var x:Number = 0;
  285.     public var y:Number = 0;
  286.     public var vx:Number = 0;
  287.     public var vy:Number = 0;
  288.     
  289.     //particle color
  290.     public var r:int;
  291.     public var g:int;
  292.     public var b:int;
  293.     
  294.     //scale factor that affects the force applied to the particle.
  295.     public var scaleFactor:Number;
  296. }
noswf

FITC: Cool Japan side A / Particle sample of SMOKE forked from: FITC: Cool Japan side A / Particle sample of SMOKE [diff(1)]

  1. // forked from fladdict's FITC: Cool Japan side A / Particle sample of SMOKE
  2. /**
  3. * Sample code for FITC
  4. * Smoke particle effect
  5. */
  6. package {
  7.     import flash.display.Sprite;    
  8.     import flash.display.Bitmap;
  9.     import flash.display.BitmapData;
  10.     import flash.display.BlendMode;
  11.     import flash.events.Event;
  12.     import flash.filters.BlurFilter;
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.utils.ByteArray;
  17.     
  18.     public class FlashTest extends Sprite {
  19.        //View
  20.         protected var canvas:BitmapData;
  21.         protected var canvasBitmap:Bitmap;
  22.         
  23.         //BitmapData that contains the actual froce that is applied to particles.
  24.         protected var forceMap:BitmapData;
  25.         
  26.         //BitmapData that contains the force of the storm.
  27.         protected var stormMap0:BitmapData;
  28.         protected var stormMap1:BitmapData;
  29.         
  30.         //BitmapData that contains the mouse repulsion force of the stage.
  31.         protected var repulsionMap:BitmapData;
  32.         protected var repulsionHistoryMap:BitmapData;
  33.         protected var repulsionFadeMap:BitmapData;
  34.         
  35.         //the number of particles. 5000-200000
  36.         protected var particleNum:int = 10000;
  37.         protected var particles:Vector.<Particle>
  38.         
  39.         //
  40.         protected var stormScaleRatioX:Number;
  41.         protected var stormScaleRatioY:Number;
  42.         protected var stormCycle:Number = 0;
  43.         
  44.         //color transform that guradually changes canvas.
  45.         protected var fadeCanvasColt:ColorTransform;
  46.         protected var canvasBlurFilter:BlurFilter;
  47.         
  48.         //temporaly variables
  49.         protected var tempColt:ColorTransform = new ColorTransform();
  50.         protected var tempPt:Point = new Point();
  51.         protected var tempMat:Matrix = new Matrix();
  52.         
  53.         public function FlashTest() {
  54.             init();
  55.             reset();
  56.         }
  57.         
  58.         protected function init():void
  59.         {
  60.             //build View
  61.             canvasBitmap = new Bitmap(null"auto"false);
  62.             // because canvas is 25% size of screen, we make view 400% bigger.
  63.             canvasBitmap.scaleX = 4;
  64.             canvasBitmap.scaleY = 4;
  65.             addChild(canvasBitmap);
  66.             
  67.             //initialize the map that contains force of the storm;
  68.             stormMap0 = new BitmapData(256,256,false,0x000000);
  69.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  70.             stormMap1 = new BitmapData(256,256,false,0x000000);
  71.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  72.             
  73.             //
  74.             forceMap = new BitmapData(256,256,false,0x000000);
  75.             
  76.             //initialize repulsionMap
  77.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  78.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  79.             repulsionMap = new BitmapData(256,256,false,0);
  80.             
  81.             /**
  82.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  83.             * Red channel of bitmap data contains horizontal force.
  84.             * Green channel of bitmap data contains vertical force. 
  85.             */
  86.             var dx:int, dy:int;
  87.             var dist:Number, fx:Number, fy:Number;
  88.             var col:int;
  89.             for(var yy:int=0; yy<256; yy++)
  90.             {
  91.                 for(var xx:int=0; xx<256; xx++){
  92.                     dx = xx-128;
  93.                     dy = yy-128;
  94.                     dist = Math.sqrt(dx*dx+dy*dy);
  95.                     if(dist<1){
  96.                         fx = 0;
  97.                         fy = 0;
  98.                     }else{
  99.                         fx = dx / dist / dist / dist * 50000;
  100.                         fy = dy / dist / dist / dist * 50000;
  101.                     }
  102.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  103.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  104.                     fx += 128;
  105.                     fy += 128;
  106.                     col = (fx<<16) + (fy<<8)
  107.                     repulsionMap.setPixel(xx,yy,col);
  108.                 }
  109.             }
  110.             //You can see 
  111.             canvasBitmap.bitmapData = repulsionMap;
  112.             
  113.             //initialize particle
  114.             particles = new Vector.<Particle>(particleNum);
  115.             
  116.             var particle:Particle;
  117.             var rr:int;
  118.             for(var i:int=0; i<particleNum; i++)
  119.             {
  120.                 particle = new Particle();
  121.                 particle.x = Math.random() * stage.stageWidth >> 2;
  122.                 particle.y = Math.random() * stage.stageHeight >> 2;
  123.                 
  124.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  125.                 particle.r = particle.g = particle.b = Math.min(Math.random()*8+4,255);  
  126.                 
  127.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  128.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.003;
  129.                 particles[i] = particle;
  130.             }
  131.             
  132.             fadeCanvasColt = new ColorTransform(1,1,1,1,-2,-1,-1,0);
  133.             canvasBlurFilter = new BlurFilter(2,2,3);
  134.             
  135.             //set event listeners
  136.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  137.             stage.addEventListener(Event.RESIZE, resizeHandler);
  138.         }
  139.         
  140.         /**
  141.         *
  142.         * Called when you need reset the view. E.G. resizing screen.
  143.         */
  144.         protected function reset():void
  145.         {
  146.             if(canvas)
  147.                 canvas.dispose();
  148.                 
  149.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  150.             canvas = new BitmapData((stage.stageWidth>>2)+1, (stage.stageHeight>>2)+1false, 0x000000);
  151.             canvasBitmap.bitmapData = canvas;
  152.             
  153.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  154.             stormScaleRatioX = 1 / (stage.stageWidth>>2)*255;
  155.             stormScaleRatioY = 1 / (stage.stageHeight>>2)*255;
  156.             
  157.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  158.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  159.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  160.            
  161.            //canvasBitmap.scaleX = 1;
  162.            //canvasBitmap.scaleY = 1;
  163.         }
  164.         
  165.         
  166.         //We rebuild screen when swf is reseized.
  167.         //It is not required if you only run it on wonderfl.
  168.         protected function resizeHandler(e:Event):void
  169.         {
  170.             reset();
  171.         }
  172.         
  173.         //Update storm and particle every frame.
  174.         protected function enterFrameHandler(e:Event):void
  175.         { 
  176.            updateStormMap();
  177.            updateRepulsionMap();
  178.            updateForceMap();
  179.            updateParticles();
  180.         }
  181.         
  182.         /**
  183.         * We calculate current forcefielde's state by blending two stormMap.
  184.         * Therefore force field gradually changes every frame.
  185.         */
  186.         protected function updateStormMap():void
  187.         {
  188.             stormCycle++;
  189.             if(stormCycle == 180){
  190.                 stormMap0.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  191.             }else if(stormCycle==360){
  192.                 stormMap1.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  193.                 stormCycle = 0;
  194.             }
  195.         }
  196.         
  197.         /**
  198.         * We update repulsionMap according to the current mouse position.
  199.         */
  200.         protected function updateRepulsionMap():void
  201.         {
  202.             tempMat.a = 1;
  203.             tempMat.b = 0;
  204.             tempMat.c = 0;
  205.             tempMat.d = 1;
  206.             tempMat.tx = 0;
  207.             tempMat.ty = 0;
  208.             
  209.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  210.             
  211.             tempMat.translate(-128,-128);
  212.             
  213.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  214.             tempMat.translate(mouseX/4*stormScaleRatioX, mouseY/4*stormScaleRatioY);
  215.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  216.         }
  217.         
  218.         protected function updateForceMap():void
  219.         {
  220.             //generate current force fieldes state with blending two stormMaps.
  221.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  222.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  223.             forceMap.draw(stormMap0, null, tempColt);
  224.             
  225.             //add mouse repulsion force
  226.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  227.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  228.         }
  229.         
  230.         protected function updateParticles():void
  231.         {
  232.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  233.             
  234.             var stageW:int = canvas.width;
  235.             var stageH:int = canvas.height;
  236.             var loopW:int = stageW-1;
  237.             var loopH:int = stageH-1;
  238.             var byteIndex:int;
  239.             
  240.             canvas.lock();
  241.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  242.             
  243.             //Update Paritcle position and draw.
  244.             var col:int, r:int, g:int, b:int;
  245.             for(var i:int=0; i<particleNum; i++)
  246.             {
  247.                 var prt:Particle = particles[i];
  248.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  249.                 prt.vx = prt.vx * 0.96 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  250.                 prt.vy = prt.vy * 0.96 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  251.                 prt.x += prt.vx;
  252.                 prt.y += prt.vy;
  253.                 if(prt.x<0){
  254.                     prt.x = loopW;
  255.                 }else if(prt.x > loopW){
  256.                     prt.x = 1;
  257.                 }
  258.                 if(prt.y<0){
  259.                     prt.y = loopH;
  260.                 }else if(prt.y > loopH){
  261.                     prt.y = 1;
  262.                 }
  263.                 
  264.                 //Self implimentation of addtive color blend mode.
  265.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  266.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  267.                 r = (col>>16&0xff) + prt.r;
  268.                 g = (col>>8&0xff) + prt.g;
  269.                 b = (col&0xff) + prt.b;
  270.                 r = (r<0xff)? r : 0xff;
  271.                 g = (g<0xff)? g : 0xff;
  272.                 b = (b<0xff)? b : 0xff;
  273.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  274.             }
  275.             canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  276.             canvas.unlock();
  277.         }
  278.     }
  279.     
  280. }
  281. class Particle
  282. {
  283.     //particle position
  284.     public var x:Number = 0;
  285.     public var y:Number = 0;
  286.     public var vx:Number = 0;
  287.     public var vy:Number = 0;
  288.     
  289.     //particle color
  290.     public var r:int;
  291.     public var g:int;
  292.     public var b:int;
  293.     
  294.     //scale factor that affects the force applied to the particle.
  295.     public var scaleFactor:Number;
  296. }
noswf

FITC: Cool Japan side A / Particle sample of SMOKE forked from: FITC: Cool Japan side A / Particle sample of SMOKE [diff(1)]

  1. // forked from fladdict's FITC: Cool Japan side A / Particle sample of SMOKE
  2. /**
  3. * Sample code for FITC
  4. * Smoke particle effect
  5. */
  6. package {
  7.     import flash.display.Sprite;    
  8.     import flash.display.Bitmap;
  9.     import flash.display.BitmapData;
  10.     import flash.display.BlendMode;
  11.     import flash.events.Event;
  12.     import flash.filters.BlurFilter;
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.utils.ByteArray;
  17.     
  18.     public class FlashTest extends Sprite {
  19.        //View
  20.         protected var canvas:BitmapData;
  21.         protected var canvasBitmap:Bitmap;
  22.         
  23.         //BitmapData that contains the actual froce that is applied to particles.
  24.         protected var forceMap:BitmapData;
  25.         
  26.         //BitmapData that contains the force of the storm.
  27.         protected var stormMap0:BitmapData;
  28.         protected var stormMap1:BitmapData;
  29.         
  30.         //BitmapData that contains the mouse repulsion force of the stage.
  31.         protected var repulsionMap:BitmapData;
  32.         protected var repulsionHistoryMap:BitmapData;
  33.         protected var repulsionFadeMap:BitmapData;
  34.         
  35.         //the number of particles. 5000-200000
  36.         protected var particleNum:int = 10000;
  37.         protected var particles:Vector.<Particle>
  38.         
  39.         //
  40.         protected var stormScaleRatioX:Number;
  41.         protected var stormScaleRatioY:Number;
  42.         protected var stormCycle:Number = 0;
  43.         
  44.         //color transform that guradually changes canvas.
  45.         protected var fadeCanvasColt:ColorTransform;
  46.         protected var canvasBlurFilter:BlurFilter;
  47.         
  48.         //temporaly variables
  49.         protected var tempColt:ColorTransform = new ColorTransform();
  50.         protected var tempPt:Point = new Point();
  51.         protected var tempMat:Matrix = new Matrix();
  52.         
  53.         public function FlashTest() {
  54.             init();
  55.             reset();
  56.         }
  57.         
  58.         protected function init():void
  59.         {
  60.             //build View
  61.             canvasBitmap = new Bitmap(null"auto"false);
  62.             // because canvas is 25% size of screen, we make view 400% bigger.
  63.             canvasBitmap.scaleX = 4;
  64.             canvasBitmap.scaleY = 4;
  65.             addChild(canvasBitmap);
  66.             
  67.             //initialize the map that contains force of the storm;
  68.             stormMap0 = new BitmapData(256,256,false,0x000000);
  69.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  70.             stormMap1 = new BitmapData(256,256,false,0x000000);
  71.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  72.             
  73.             //
  74.             forceMap = new BitmapData(256,256,false,0x000000);
  75.             
  76.             //initialize repulsionMap
  77.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  78.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  79.             repulsionMap = new BitmapData(256,256,false,0);
  80.             
  81.             /**
  82.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  83.             * Red channel of bitmap data contains horizontal force.
  84.             * Green channel of bitmap data contains vertical force. 
  85.             */
  86.             var dx:int, dy:int;
  87.             var dist:Number, fx:Number, fy:Number;
  88.             var col:int;
  89.             for(var yy:int=0; yy<256; yy++)
  90.             {
  91.                 for(var xx:int=0; xx<256; xx++){
  92.                     dx = xx-128;
  93.                     dy = yy-128;
  94.                     dist = Math.sqrt(dx*dx+dy*dy);
  95.                     if(dist<1){
  96.                         fx = 0;
  97.                         fy = 0;
  98.                     }else{
  99.                         fx = dx / dist / dist / dist * 50000;
  100.                         fy = dy / dist / dist / dist * 50000;
  101.                     }
  102.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  103.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  104.                     fx += 128;
  105.                     fy += 128;
  106.                     col = (fx<<16) + (fy<<8)
  107.                     repulsionMap.setPixel(xx,yy,col);
  108.                 }
  109.             }
  110.             //You can see 
  111.             canvasBitmap.bitmapData = repulsionMap;
  112.             
  113.             //initialize particle
  114.             particles = new Vector.<Particle>(particleNum);
  115.             
  116.             var particle:Particle;
  117.             var rr:int;
  118.             for(var i:int=0; i<particleNum; i++)
  119.             {
  120.                 particle = new Particle();
  121.                 particle.x = Math.random() * stage.stageWidth >> 2;
  122.                 particle.y = Math.random() * stage.stageHeight >> 2;
  123.                 
  124.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  125.                 particle.r = particle.g = particle.b = Math.min(Math.random()*8+4,255);  
  126.                 
  127.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  128.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.003;
  129.                 particles[i] = particle;
  130.             }
  131.             
  132.             fadeCanvasColt = new ColorTransform(1,1,1,1,-2,-1,-1,0);
  133.             canvasBlurFilter = new BlurFilter(2,2,3);
  134.             
  135.             //set event listeners
  136.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  137.             stage.addEventListener(Event.RESIZE, resizeHandler);
  138.         }
  139.         
  140.         /**
  141.         *
  142.         * Called when you need reset the view. E.G. resizing screen.
  143.         */
  144.         protected function reset():void
  145.         {
  146.             if(canvas)
  147.                 canvas.dispose();
  148.                 
  149.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  150.             canvas = new BitmapData((stage.stageWidth>>2)+1, (stage.stageHeight>>2)+1false, 0x000000);
  151.             canvasBitmap.bitmapData = canvas;
  152.             
  153.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  154.             stormScaleRatioX = 1 / (stage.stageWidth>>2)*255;
  155.             stormScaleRatioY = 1 / (stage.stageHeight>>2)*255;
  156.             
  157.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  158.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  159.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  160.            
  161.            //canvasBitmap.scaleX = 1;
  162.            //canvasBitmap.scaleY = 1;
  163.         }
  164.         
  165.         
  166.         //We rebuild screen when swf is reseized.
  167.         //It is not required if you only run it on wonderfl.
  168.         protected function resizeHandler(e:Event):void
  169.         {
  170.             reset();
  171.         }
  172.         
  173.         //Update storm and particle every frame.
  174.         protected function enterFrameHandler(e:Event):void
  175.         { 
  176.            updateStormMap();
  177.            updateRepulsionMap();
  178.            updateForceMap();
  179.            updateParticles();
  180.         }
  181.         
  182.         /**
  183.         * We calculate current forcefielde's state by blending two stormMap.
  184.         * Therefore force field gradually changes every frame.
  185.         */
  186.         protected function updateStormMap():void
  187.         {
  188.             stormCycle++;
  189.             if(stormCycle == 180){
  190.                 stormMap0.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  191.             }else if(stormCycle==360){
  192.                 stormMap1.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  193.                 stormCycle = 0;
  194.             }
  195.         }
  196.         
  197.         /**
  198.         * We update repulsionMap according to the current mouse position.
  199.         */
  200.         protected function updateRepulsionMap():void
  201.         {
  202.             tempMat.a = 1;
  203.             tempMat.b = 0;
  204.             tempMat.c = 0;
  205.             tempMat.d = 1;
  206.             tempMat.tx = 0;
  207.             tempMat.ty = 0;
  208.             
  209.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  210.             
  211.             tempMat.translate(-128,-128);
  212.             
  213.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  214.             tempMat.translate(mouseX/4*stormScaleRatioX, mouseY/4*stormScaleRatioY);
  215.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  216.         }
  217.         
  218.         protected function updateForceMap():void
  219.         {
  220.             //generate current force fieldes state with blending two stormMaps.
  221.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  222.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  223.             forceMap.draw(stormMap0, null, tempColt);
  224.             
  225.             //add mouse repulsion force
  226.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  227.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  228.         }
  229.         
  230.         protected function updateParticles():void
  231.         {
  232.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  233.             
  234.             var stageW:int = canvas.width;
  235.             var stageH:int = canvas.height;
  236.             var loopW:int = stageW-1;
  237.             var loopH:int = stageH-1;
  238.             var byteIndex:int;
  239.             
  240.             canvas.lock();
  241.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  242.             
  243.             //Update Paritcle position and draw.
  244.             var col:int, r:int, g:int, b:int;
  245.             for(var i:int=0; i<particleNum; i++)
  246.             {
  247.                 var prt:Particle = particles[i];
  248.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  249.                 prt.vx = prt.vx * 0.96 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  250.                 prt.vy = prt.vy * 0.96 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  251.                 prt.x += prt.vx;
  252.                 prt.y += prt.vy;
  253.                 if(prt.x<0){
  254.                     prt.x = loopW;
  255.                 }else if(prt.x > loopW){
  256.                     prt.x = 1;
  257.                 }
  258.                 if(prt.y<0){
  259.                     prt.y = loopH;
  260.                 }else if(prt.y > loopH){
  261.                     prt.y = 1;
  262.                 }
  263.                 
  264.                 //Self implimentation of addtive color blend mode.
  265.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  266.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  267.                 r = (col>>16&0xff) + prt.r;
  268.                 g = (col>>8&0xff) + prt.g;
  269.                 b = (col&0xff) + prt.b;
  270.                 r = (r<0xff)? r : 0xff;
  271.                 g = (g<0xff)? g : 0xff;
  272.                 b = (b<0xff)? b : 0xff;
  273.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  274.             }
  275.             canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  276.             canvas.unlock();
  277.         }
  278.     }
  279.     
  280. }
  281. class Particle
  282. {
  283.     //particle position
  284.     public var x:Number = 0;
  285.     public var y:Number = 0;
  286.     public var vx:Number = 0;
  287.     public var vy:Number = 0;
  288.     
  289.     //particle color
  290.     public var r:int;
  291.     public var g:int;
  292.     public var b:int;
  293.     
  294.     //scale factor that affects the force applied to the particle.
  295.     public var scaleFactor:Number;
  296. }
noswf
  1. // forked from fladdict's FITC: Cool Japan side A / Particle sample of SMOKE
  2. /**
  3. * Sample code for FITC
  4. * Smoke particle effect
  5. */
  6. package {
  7.     import flash.display.Sprite;    
  8.     import flash.display.Bitmap;
  9.     import flash.display.BitmapData;
  10.     import flash.display.BlendMode;
  11.     import flash.events.Event;
  12.     import flash.filters.BlurFilter;
  13.     import flash.geom.ColorTransform;
  14.     import flash.geom.Matrix;
  15.     import flash.geom.Point;
  16.     import flash.utils.ByteArray;
  17.     
  18.     public class FlashTest extends Sprite {
  19.        //View
  20.         protected var canvas:BitmapData;
  21.         protected var canvasBitmap:Bitmap;
  22.         
  23.         //BitmapData that contains the actual froce that is applied to particles.
  24.         protected var forceMap:BitmapData;
  25.         
  26.         //BitmapData that contains the force of the storm.
  27.         protected var stormMap0:BitmapData;
  28.         protected var stormMap1:BitmapData;
  29.         
  30.         //BitmapData that contains the mouse repulsion force of the stage.
  31.         protected var repulsionMap:BitmapData;
  32.         protected var repulsionHistoryMap:BitmapData;
  33.         protected var repulsionFadeMap:BitmapData;
  34.         
  35.         //the number of particles. 5000-200000
  36.         protected var particleNum:int = 10000;
  37.         protected var particles:Vector.<Particle>
  38.         
  39.         //
  40.         protected var stormScaleRatioX:Number;
  41.         protected var stormScaleRatioY:Number;
  42.         protected var stormCycle:Number = 0;
  43.         
  44.         //color transform that guradually changes canvas.
  45.         protected var fadeCanvasColt:ColorTransform;
  46.         protected var canvasBlurFilter:BlurFilter;
  47.         
  48.         //temporaly variables
  49.         protected var tempColt:ColorTransform = new ColorTransform();
  50.         protected var tempPt:Point = new Point();
  51.         protected var tempMat:Matrix = new Matrix();
  52.         
  53.         public function FlashTest() {
  54.             init();
  55.             reset();
  56.         }
  57.         
  58.         protected function init():void
  59.         {
  60.             //build View
  61.             canvasBitmap = new Bitmap(null"auto"false);
  62.             // because canvas is 25% size of screen, we make view 400% bigger.
  63.             canvasBitmap.scaleX = 4;
  64.             canvasBitmap.scaleY = 4;
  65.             addChild(canvasBitmap);
  66.             
  67.             //initialize the map that contains force of the storm;
  68.             stormMap0 = new BitmapData(256,256,false,0x000000);
  69.             stormMap0.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  70.             stormMap1 = new BitmapData(256,256,false,0x000000);
  71.             stormMap1.perlinNoise(128,128,3,Math.random()*100,false,true,3,false);
  72.             
  73.             //
  74.             forceMap = new BitmapData(256,256,false,0x000000);
  75.             
  76.             //initialize repulsionMap
  77.             repulsionHistoryMap = new BitmapData(256,256,false,0x000000);
  78.             repulsionFadeMap = new BitmapData(256,256,true,0x20808000);
  79.             repulsionMap = new BitmapData(256,256,false,0);
  80.             
  81.             /**
  82.             * First we preculculate the repulsion force field that mouse cursor generates as a bitmapdata. 
  83.             * Red channel of bitmap data contains horizontal force.
  84.             * Green channel of bitmap data contains vertical force. 
  85.             */
  86.             var dx:int, dy:int;
  87.             var dist:Number, fx:Number, fy:Number;
  88.             var col:int;
  89.             for(var yy:int=0; yy<256; yy++)
  90.             {
  91.                 for(var xx:int=0; xx<256; xx++){
  92.                     dx = xx-128;
  93.                     dy = yy-128;
  94.                     dist = Math.sqrt(dx*dx+dy*dy);
  95.                     if(dist<1){
  96.                         fx = 0;
  97.                         fy = 0;
  98.                     }else{
  99.                         fx = dx / dist / dist / dist * 50000;
  100.                         fy = dy / dist / dist / dist * 50000;
  101.                     }
  102.                     fx = (fx<-128)? -128 : (fx>127)? 127 : fx;
  103.                     fy = (fy<-128)? -128 : (fy>127)? 127 : fy;
  104.                     fx += 128;
  105.                     fy += 128;
  106.                     col = (fx<<16) + (fy<<8)
  107.                     repulsionMap.setPixel(xx,yy,col);
  108.                 }
  109.             }
  110.             //You can see 
  111.             canvasBitmap.bitmapData = repulsionMap;
  112.             
  113.             //initialize particle
  114.             particles = new Vector.<Particle>(particleNum);
  115.             
  116.             var particle:Particle;
  117.             var rr:int;
  118.             for(var i:int=0; i<particleNum; i++)
  119.             {
  120.                 particle = new Particle();
  121.                 particle.x = Math.random() * stage.stageWidth >> 2;
  122.                 particle.y = Math.random() * stage.stageHeight >> 2;
  123.                 
  124.                 //Customize the range of the particle color according to the amount of the particle and stage size.
  125.                 particle.r = particle.g = particle.b = Math.min(Math.random()*8+4,255);  
  126.                 
  127.                 //Customize the rage of the scaleFactor according to the amount of the particle and stage size.
  128.                 particle.scaleFactor = (Math.random()*0.1 + 0.95) * 0.003;
  129.                 particles[i] = particle;
  130.             }
  131.             
  132.             fadeCanvasColt = new ColorTransform(1,1,1,1,-2,-1,-1,0);
  133.             canvasBlurFilter = new BlurFilter(2,2,3);
  134.             
  135.             //set event listeners
  136.             stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  137.             stage.addEventListener(Event.RESIZE, resizeHandler);
  138.         }
  139.         
  140.         /**
  141.         *
  142.         * Called when you need reset the view. E.G. resizing screen.
  143.         */
  144.         protected function reset():void
  145.         {
  146.             if(canvas)
  147.                 canvas.dispose();
  148.                 
  149.             //build view, to optimize we use 25% size BitmapData of actual screensize.
  150.             canvas = new BitmapData((stage.stageWidth>>2)+1, (stage.stageHeight>>2)+1false, 0x000000);
  151.             canvasBitmap.bitmapData = canvas;
  152.             
  153.             //precalculate ratio that converts screen coordinate to stomMapBitmap coodinate;
  154.             stormScaleRatioX = 1 / (stage.stageWidth>>2)*255;
  155.             stormScaleRatioY = 1 / (stage.stageHeight>>2)*255;
  156.             
  157.            //canvasBitmap.bitmapData = stormMap1;  //if you want to see stormMap;
  158.            //canvasBitmap.bitmapData = repulsionHistoryMap;
  159.            //canvasBitmap.bitmapData = forceMap; //if you want to see forceMap;
  160.            
  161.            //canvasBitmap.scaleX = 1;
  162.            //canvasBitmap.scaleY = 1;
  163.         }
  164.         
  165.         
  166.         //We rebuild screen when swf is reseized.
  167.         //It is not required if you only run it on wonderfl.
  168.         protected function resizeHandler(e:Event):void
  169.         {
  170.             reset();
  171.         }
  172.         
  173.         //Update storm and particle every frame.
  174.         protected function enterFrameHandler(e:Event):void
  175.         { 
  176.            updateStormMap();
  177.            updateRepulsionMap();
  178.            updateForceMap();
  179.            updateParticles();
  180.         }
  181.         
  182.         /**
  183.         * We calculate current forcefielde's state by blending two stormMap.
  184.         * Therefore force field gradually changes every frame.
  185.         */
  186.         protected function updateStormMap():void
  187.         {
  188.             stormCycle++;
  189.             if(stormCycle == 180){
  190.                 stormMap0.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  191.             }else if(stormCycle==360){
  192.                 stormMap1.perlinNoise(256,256,3,Math.random()*100,false,true,3,false);
  193.                 stormCycle = 0;
  194.             }
  195.         }
  196.         
  197.         /**
  198.         * We update repulsionMap according to the current mouse position.
  199.         */
  200.         protected function updateRepulsionMap():void
  201.         {
  202.             tempMat.a = 1;
  203.             tempMat.b = 0;
  204.             tempMat.c = 0;
  205.             tempMat.d = 1;
  206.             tempMat.tx = 0;
  207.             tempMat.ty = 0;
  208.             
  209.             repulsionHistoryMap.draw(repulsionFadeMap, tempMat);
  210.             
  211.             tempMat.translate(-128,-128);
  212.             
  213.             tempMat.scale(1/stormScaleRatioX, 1/stormScaleRatioY);
  214.             tempMat.translate(mouseX/4*stormScaleRatioX, mouseY/4*stormScaleRatioY);
  215.             repulsionHistoryMap.draw(repulsionMap, tempMat, null, BlendMode.HARDLIGHT);
  216.         }
  217.         
  218.         protected function updateForceMap():void
  219.         {
  220.             //generate current force fieldes state with blending two stormMaps.
  221.             forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
  222.             tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
  223.             forceMap.draw(stormMap0, null, tempColt);
  224.             
  225.             //add mouse repulsion force
  226.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  227.             forceMap.draw(repulsionHistoryMap,null,null,BlendMode.HARDLIGHT);
  228.         }
  229.         
  230.         protected function updateParticles():void
  231.         {
  232.             var forceBytes:ByteArray = forceMap.getPixels(forceMap.rect);
  233.             
  234.             var stageW:int = canvas.width;
  235.             var stageH:int = canvas.height;
  236.             var loopW:int = stageW-1;
  237.             var loopH:int = stageH-1;
  238.             var byteIndex:int;
  239.             
  240.             canvas.lock();
  241.             canvas.colorTransform(canvas.rect, fadeCanvasColt);
  242.             
  243.             //Update Paritcle position and draw.
  244.             var col:int, r:int, g:int, b:int;
  245.             for(var i:int=0; i<particleNum; i++)
  246.             {
  247.                 var prt:Particle = particles[i];
  248.                 byteIndex = (int(prt.y*stormScaleRatioY)*256 + int(prt.x*stormScaleRatioX))<<2;
  249.                 prt.vx = prt.vx * 0.96 + (forceBytes[byteIndex+1]-128)*prt.scaleFactor;
  250.                 prt.vy = prt.vy * 0.96 + (forceBytes[byteIndex+2]-128)*prt.scaleFactor;
  251.                 prt.x += prt.vx;
  252.                 prt.y += prt.vy;
  253.                 if(prt.x<0){
  254.                     prt.x = loopW;
  255.                 }else if(prt.x > loopW){
  256.                     prt.x = 1;
  257.                 }
  258.                 if(prt.y<0){
  259.                     prt.y = loopH;
  260.                 }else if(prt.y > loopH){
  261.                     prt.y = 1;
  262.                 }
  263.                 
  264.                 //Self implimentation of addtive color blend mode.
  265.                 //Because there is too many particle, blending particle with low brightness color is much effective.
  266.                 col = canvas.getPixel(int(prt.x), int(prt.y));
  267.                 r = (col>>16&0xff) + prt.r;
  268.                 g = (col>>8&0xff) + prt.g;
  269.                 b = (col&0xff) + prt.b;
  270.                 r = (r<0xff)? r : 0xff;
  271.                 g = (g<0xff)? g : 0xff;
  272.                 b = (b<0xff)? b : 0xff;
  273.                 canvas.setPixel(int(prt.x), int(prt.y), (r<<16)|(g<<8)|b);
  274.             }
  275.            // canvas.applyFilter(canvas, canvas.rect, tempPt, canvasBlurFilter);
  276.            canvas.fillRect
  277.             canvas.unlock();
  278.         }
  279.     }
  280.     
  281. }
  282. class Particle
  283. {
  284.     //particle position
  285.     public var x:Number = 0;
  286.     public var y:Number = 0;
  287.     public var vx:Number = 0;
  288.     public var vy:Number = 0;
  289.     
  290.     //particle color
  291.     public var r:int;
  292.     public var g:int;
  293.     public var b:int;
  294.     
  295.     //scale factor that affects the force applied to the particle.
  296.     public var scaleFactor:Number;
  297. }
noswf
Get Adobe Flash Player