Flash三维粒子光线变化4

三维粒子 everyinch 3516℃ 0评论
package{
	import __AS3__.vec.Vector;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import net.everyinch.geom.Particle;
	
	[SWF(width="800",height="600",frameRate="32",backgroundColor="0x000000")]
	public class ParticleLight4 extends Sprite{
		private const PI2:Number = Math.PI*2;
		private var bmpd:BitmapData = new BitmapData(800,600,false,0);
		private var buffer:Vector.<uint> = new Vector.<uint>(800*600,true);
		private var focalLength:Number;
		private var matrix:Matrix3D = new Matrix3D();
		private var particle:Particle = new Particle();
		
		private var phi:Number = -Math.PI/4;
		private var theta:Number = Math.PI*3/2;
		
		public function ParticleLight4(){
			init();
			createParticles();
			
			addEventListener(Event.ENTER_FRAME,onEnterframe);
		}
		
		private function init():void{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.frameRate = 62;
			//stage.quality = StageQuality.LOW;
			stage.fullScreenSourceRect = new Rectangle(0,0,550,400);
						
			var perspectiveProjection:PerspectiveProjection = new PerspectiveProjection();
			perspectiveProjection.fieldOfView = 60;
			perspectiveProjection.projectionCenter = new Point(400,300);
			focalLength = perspectiveProjection.focalLength;
			
			addChild(new Bitmap(bmpd,PixelSnapping.NEVER,false));
		}
		
		private function createParticles():void{
			var radius:Number;
			var s:Number;
			var t:Number;
			var sMin:Number = 0;
			var tMin:Number = 0;
			var sMax:Number = Math.PI*2;
			var tMax:Number = Math.PI;
			var numS:int = 200;
			var numT:int = 300;
			var sInc:Number = (sMax - sMin)/numS;// 0.02094395
			var tInc:Number = (tMax - tMin)/numT;// 0.01570796
			var s0:Number;
			var t0:Number;
			var sFuzz:Number = 0;
			var tFuzz:Number = 0;
			
			var p:Particle = particle;
			for(var j:int=0;j<numS;j++){
				s0 = sMin + sInc*j;
				for(var i:int=0;i<numT;i++){
					t0 = tMin + tInc*i;
					s = s0+sFuzz*(Math.random()*2-1);
					t = t0+tFuzz*(Math.random()*2-1);
					
					radius = 100*(1+0.2*Math.sin(t*6)*Math.sin(s*5));

					p.x = radius*Math.sin(s)*Math.sin(t);
					p.y = radius*Math.sin(s)*Math.cos(t);
					p.z = radius*Math.cos(s);
					p.next = new Particle();
					p = p.next;
				}
			}
		}
		
		private function onEnterframe(e:Event):void{
			var fLen:Number = 250;
			var m:Number;
			var i:int;
			var cx:Number = bmpd.width/2;
			var cy:Number = bmpd.height/2;
			var ix:int;
			var iy:int;
			
			var dphi:Number = 0.015*Math.cos(getTimer()*0.000132);
			var dtheta:Number = 0.017*Math.cos(getTimer()*0.000244);			
			phi = (phi+dphi) % PI2
			theta = (theta+dtheta) % PI2;
			var cosp:Number = Math.cos(phi);
			var sinp:Number = Math.sin(phi);
			var cost:Number = Math.cos(theta);
			var sint:Number = Math.sin(theta);
			
			var M11:Number = cost*sinp;
			var M12:Number = sint*sinp;
			var M31:Number = -cost*cosp;
			var M32:Number = -sint*cosp;
			
			var depth1:Number = 120;
			var depth0:Number = -120;
			var maxLevel:Number = 13;
			var fadeRate:Number = (maxLevel-1)/(depth1-depth0);
			var level:Number;
			var increment:int;
			var color:uint;
			var blur:BlurFilter = new BlurFilter(3,3,5);
			
			var n:int = buffer.length;
			while(--n > -1){
				buffer[n] = 0x000000;
			}
			var p:Particle = particle;
			do{
				var u:Number = M11*p.x + M12*p.y + cosp*p.z;
				//if(u < fLen){
					m = fLen/(fLen-u)
					ix = int((-sint*p.x+cost*p.y)*m+cx);
					iy = int((M31*p.x+M32*p.y+sinp*p.z)*m+cy);
					i = int(ix+iy*bmpd.width);
					color = bmpd.getPixel(ix,iy);
					increment = fadeRate*(u-depth0)+1;
					increment = (increment > maxLevel) ? maxLevel : (increment < 0 ? 0 : increment);
					level = (color >> 16)+increment;
					//level = level << 1;
					level = (level > 255)?255:level;
					color = (level << 16) | (level << 8) | level >> 1;
					buffer[i] = color;
				//}
				p = p.next;
			}while(p);
			bmpd.lock();
			bmpd.setVector(bmpd.rect,buffer);
			bmpd.unlock(bmpd.rect);
		}
	}
}

分享&收藏

转载请注明:陈童的博客 » Flash三维粒子光线变化4

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>