Particle Wave in 3D + 3D Trails

by Dan0 forked from Particle Wave in 3D (diff: 66)
This is what it SHOULD look like http://dan0.net/temp/particlewave/wave_3d_trails.html
*
* Created by Dan0
* Credits to:
* fladdict's "FITC Cool Japan side A / Particle simple 40000"
* sake's "3Dでアトラクタっぽいの"
*
♥2 | Line 164 | Modified 2009-09-06 01:42:34 | MIT License
play

ActionScript3 source code

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

// forked from Dan0's Particle Wave in 3D
/**
* 
* This is what it SHOULD look like http://dan0.net/temp/particlewave/wave_3d_trails.html
*
* Created by Dan0
* Credits to:
* fladdict's "FITC Cool Japan side A / Particle simple 40000"
* sake's "3Dでアトラクタっぽいの"
*
*/
package {
    import org.papervision3d.view.BasicView;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    import flash.geom.Point;
    import flash.geom.ColorTransform;
    
    import org.papervision3d.core.effects.utils.BitmapClearMode;
    import org.papervision3d.core.geom.Pixels;
    import org.papervision3d.core.geom.renderables.Pixel3D;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.view.layer.BitmapEffectLayer;
    import org.papervision3d.core.effects.BitmapLayerEffect;
    import org.papervision3d.core.effects.BitmapColorEffect;
    
    import org.papervision3d.core.geom.renderables.Vertex3DInstance;
    import org.papervision3d.core.geom.renderables.Vertex3D;
    
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.materials.WireframeMaterial;
    
    import org.papervision3d.materials.special.FogMaterial;
    import org.papervision3d.core.render.filter.FogFilter;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.StageDisplayState;
   
    [SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")] 
    
    public class wave_3d_trails extends BasicView {
        //
        private var layer:BitmapEffectLayer;
        private var pixels:Pixels;
        private var container:DisplayObject3D;
        //
        private var particleNum:int = 500;
        private var particleTrailNum:int = 20;
        private var particleForceScale:Number = 0.0008;
        private var particles:Vector.<Particle>
        
        private var loopX:Number = 200;
        private var loopY:Number  = 200;
        private var loopZ:Number  = 200;
        
        private var stormMap0:BitmapData;
        private var stormMap1:BitmapData;
        private var forceMap:BitmapData;
        
        private var stormScaleX:Number;
        private var stormScaleY:Number;
        private var stormScaleZ:Number;
        
        private var tempColt:ColorTransform = new ColorTransform();
        private var tempPt:Point = new Point();
        private var stormCycle:Number =0;
        
        private var fogMaterial:FogMaterial;
        
        public function wave_3d_trails() {
            Wonderfl.capture_delay(5);
            //setup stage
            super(0, 0, true, true);
            camera.focus=50;
            camera.zoom=40;
            
            //add rendering layers, pixels create trail by fading out using ColorMatrixFilter
            layer = new BitmapEffectLayer(viewport, 600, 600, true, 0, BitmapClearMode.CLEAR_PRE, true, true);
            viewport.containerSprite.addLayer(layer);
            layer.addEffect(new BitmapColorEffect(1, 1, 1, 0.98));
            
            container= new DisplayObject3D;
            scene.addChild(container);
            
            pixels = new Pixels(layer);
            container.addChild(pixels);
            
            var cubeMaterial:WireframeMaterial = new WireframeMaterial(0xFF3300, 0.2, 1);
            cubeMaterial.doubleSided = true;
            var cube:Cube = new Cube(new MaterialsList({all:cubeMaterial}), loopX, loopZ, loopY, 1,1,1,0,0);
            container.addChild(cube);
            
            //create particles
            particles = new Vector.<Particle>(particleNum);
            
            var particle:Particle;
            var rr:int;
            for(var i:int=0; i<particleNum; i++)
            {
                particle = new Particle();
                
               // var ang:Number = Math.random() * Math.PI*2;
                particle.x = new Array(particleTrailNum);
                particle.y = new Array(particleTrailNum);
                particle.z = new Array(particleTrailNum);
                
                particle.x[0] = Math.random() *loopX -(loopX/2);
                particle.y[0] = Math.random() *loopY -(loopY/2);
                particle.z[0] = Math.random() *loopZ -(loopZ/2);
                
                particles[i] = particle;
                
                particle.vfri = 0.98+Math.random()*0.01;
            }
            //
            forceMap = new BitmapData(256,256,false,0x000000);
            
            stormMap0 = new BitmapData(256,256,false,0x000000);
            stormMap0.perlinNoise(128,128,5,Math.random()*100,true,true,1|2|4,false);
            stormMap1 = new BitmapData(256,256,false,0x000000);
            stormMap1.perlinNoise(128,128,5,Math.random()*100,true,true,1|2|4,false);
            
            stormScaleX = 1 / loopX*255;
            stormScaleY = 1 / loopY*255;
            stormScaleZ = 1 / loopZ*255;
            
            //
            addEventListener(MouseEvent.CLICK ,function(e:Event):void{stage.displayState=StageDisplayState.FULL_SCREEN});
            
            //
            addEventListener(Event.ENTER_FRAME, updateParticles);
            
            fogMaterial = new FogMaterial(0x000000);
            //renderer.filter = new FogFilter(fogMaterial, 20, 500, 600); 
        }
        
        public function updateParticles(e:Event):void{
            stormCycle++;
            if(stormCycle == 180){
                stormMap0.perlinNoise(128,128,5,Math.random()*100,true,true,1|2|4,false);
            }else if(stormCycle==360){
                stormMap1.perlinNoise(128,128,5,Math.random()*100,true,true,1|2|4,false);
                
                stormCycle = 0;
            }
            
            forceMap.copyPixels(stormMap1, stormMap0.rect, tempPt);
            tempColt.alphaMultiplier = Math.cos(stormCycle*Math.PI/180)*0.5+0.5;
            forceMap.draw(stormMap0, null, tempColt);
            
            ///////////////////////////////////////
            ///////////////////////////////////////
            
            pixels.removeAllpixels();
            
            var byteIndex:int;
            var col:uint;
            var colZ:uint;
            
            var p:Pixel3D;
            var prt:Particle;
            var intensity:Number, r:int, g:int, b:int;
            for(var i:int=0; i<particleNum; i++){
                prt = particles[i];
                
                col = forceMap.getPixel(int((prt.x[0]+loopX/2)*stormScaleX),int((prt.y[0]+loopY/2)*stormScaleY));
                colZ = forceMap.getPixel(int((prt.z[0]+loopZ/2)*stormScaleZ),int((prt.y[0]+loopY/2)*stormScaleY));
                
                prt.x.unshift(0);
                prt.x.pop();
                prt.y.unshift(0);
                prt.y.pop();
                prt.z.unshift(0);
                prt.z.pop();
                
                prt.vx = prt.vx * prt.vfri + ((col>>16&0xff)-128)*particleForceScale;
                prt.vy = prt.vy * prt.vfri + ((col>>8&0xff)-128)*particleForceScale;
                prt.vz = prt.vz * prt.vfri + ((colZ&0xff)-128)*particleForceScale;
                prt.x[0] = prt.x[1]+prt.vx;
                prt.y[0] = prt.y[1]+prt.vy;
                prt.z[0] = prt.z[1]+prt.vz;
                
                if(prt.x[0]<-loopX/2){
                    prt.x[0] += loopX;
                }else if(prt.x[0] > loopX/2){
                    prt.x[0] -= loopX;
                }
                if(prt.y[0]<-loopY/2){
                    prt.y[0] += loopY;
                }else if(prt.y[0] > loopY/2){
                    prt.y[0] -= loopY;
                }
                if(prt.z[0]< -loopZ/2){
                    prt.z[0] += loopZ;
                }else if(prt.z[0] > loopZ/2){
                    prt.z[0] -= loopZ;
                }
                
                for(var j:int=0; j<particleTrailNum; j++){
                    intensity = (particleTrailNum-j)/particleTrailNum;
                    r = intensity *60;
                    g = intensity *100;
                    b = intensity *255;
                    p = new Pixel3D((0xFF<<24)|(r<<16)|(g<<8)|b,prt.x[j],prt.y[j],prt.z[j]);
                pixels.addPixel3D(p);
                }
                
            }

           //container.rotationY += 0.1;
           //container.rotationZ += 0.1;
           container.yaw((stage.stageWidth/2-mouseX)/stage.stageWidth*10);
           container.pitch((stage.stageHeight/2-mouseY)/stage.stageHeight*10);
           
           //renderer.filter = new FogFilter(fogMaterial, 20, 600,900); 
           singleRender();
        }
    }
}

class Particle
{
    //particle position
    public var x:Array;
    public var y:Array;
    public var z:Array;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var vz:Number = 0;
    
    public var vfri:Number = 1;
}