使用Away3d实现粒子形式的图片

三维粒子 everyinch 4730℃ 0评论

项目源文件下载

package {
	import away3d.animators.*;
	import away3d.animators.data.*;
	import away3d.animators.nodes.*;
	import away3d.containers.*;
	import away3d.controllers.*;
	import away3d.core.base.*;
	import away3d.debug.*;
	import away3d.entities.*;
	import away3d.lights.PointLight;
	import away3d.materials.*;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.primitives.*;
	import away3d.tools.helpers.*;
	import away3d.utils.*;

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.utils.Timer;

	[SWF(backgroundColor="#000000", frameRate="60")]
	public class Away3d_Particles3 extends Sprite{
		[Embed(source="assets/hit.png")]
		private var TextImage:Class;

		private var view:View3D;
		private var cameraController:HoverController;

		private var data:Vector.<Vector3D>;
		private const SIZE:int = 2;
		private const TIME:int = 10000;
		private var state:int;

		private var light:PointLight;
		private var lightPicker:StaticLightPicker;
		private var particleAnimator:ParticleAnimator

		private var move:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;
		private var angle:Number = 0;

		public function Away3d_Particles3(){
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;

			view = new View3D();
			addChild(view);

			cameraController = new HoverController(view.camera, null, 45, 20, 1000,5);

			addChild(new AwayStats(view));

			initTextData();
			initLights();
			initParticles();

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(Event.RESIZE, onResize);
			onResize();

			var timer:Timer = new Timer(TIME);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.start();
			onTimer(null);
		}

		private function onTimer(e:Event):void{
			switch (state){
				case 0:
					particleAnimator.playbackSpeed = 0;
					particleAnimator.resetTime(0);
					break;
				case 1:
					particleAnimator.playbackSpeed = 1;
					break;
				case 2:
					particleAnimator.playbackSpeed = -1;
					break;
			}
			state++;
			state %= 3;
		}

		private function initTextData():void{
			var bitmapData:BitmapData = new TextImage().bitmapData;
			data = new Vector.<Vector3D>;
			var depth:int = 10;
			for (var i:int = 0; i < bitmapData.width; i++){
				for (var j:int = 0; j < bitmapData.height; j++){
					if (bitmapData.getPixel(i, j) == 0){
						for (var k:int = 0; k < depth; k++){
							var point:Vector3D = new Vector3D(i - bitmapData.width / 2, bitmapData.height / 2 - j, k);
							point.scaleBy(SIZE*2);
							data.push(point);
						}
					}
				}
			}
		}

		private function initLights():void{
			light = new PointLight();
			light.color = 0xffffff;
			light.radius = 100;
			light.ambient = 0.6;
			light.specular = 2;
			view.scene.addChild(light);
			lightPicker = new StaticLightPicker([light]);
		}

		private function initParticles():void{
			var cube:Geometry = new CubeGeometry(SIZE,SIZE,SIZE);
			var geometrySet:Vector.<Geometry> = new Vector.<Geometry>();
			for (var i:int = 0; i < data.length; i++){
				geometrySet.push(cube);
			}
			var particleGeometry:Geometry = ParticleGeometryHelper.generateGeometry(geometrySet);

			var particleAnimationSet:ParticleAnimationSet = new ParticleAnimationSet();
			particleAnimationSet.addAnimation(new ParticlePositionNode(ParticlePropertiesMode.LOCAL_STATIC));
			particleAnimationSet.addAnimation(new ParticleVelocityNode(ParticlePropertiesMode.LOCAL_STATIC));
			particleAnimationSet.initParticleFunc = initParticleFunc;

			var material:ColorMaterial = new ColorMaterial(0xffffff);
			material.alphaPremultiplied = true;
			material.lightPicker = lightPicker;

			var particleMesh:Mesh = new Mesh(particleGeometry, material);
			particleAnimator = new ParticleAnimator(particleAnimationSet);
			particleMesh.animator = particleAnimator;
			particleAnimator.start();
			view.scene.addChild(particleMesh);
		}

		private function initParticleFunc(prop:ParticleProperties):void{
			prop.startTime = 0;
			var degree1:Number = Math.random() * Math.PI * 2;
			var degree2:Number = Math.random() * Math.PI * 2;
			var r:Number = 15;
			//设置速度
			prop[ParticleVelocityNode.VELOCITY_VECTOR3D] = new Vector3D(r * Math.sin(degree1) * Math.cos(degree2), r * Math.cos(degree1) * Math.cos(degree2), r * Math.sin(degree2));
			//设置位置
			prop[ParticlePositionNode.POSITION_VECTOR3D] = data[prop.index];
		}

		private function onEnterFrame(event:Event):void{
			if (move){
				cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle;
				cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
			}
			view.render();
		}

		private function onMouseDown(event:MouseEvent):void{
			lastPanAngle = cameraController.panAngle;
			lastTiltAngle = cameraController.tiltAngle;
			lastMouseX = stage.mouseX;
			lastMouseY = stage.mouseY;
			move = true;
			stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}

		private function onMouseUp(event:MouseEvent):void{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}

		private function onStageMouseLeave(event:Event):void{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}

		private function onResize(event:Event = null):void{
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
	}
}
分享&收藏

转载请注明:陈童的博客 » 使用Away3d实现粒子形式的图片

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

表情

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

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