In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: Lets make something pretty using papervsion partcles, in steps!

Papervsion Particles step 1
@author Mario Gonzalez
/**
 * Copyright onedayitwillmake ( http://wonderfl.net/user/onedayitwillmake )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/495u
 */

// forked from onedayitwillmake's forked from: Lets make something pretty using papervsion partcles, in steps!
package  
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.PixelSnapping;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.utils.Timer;
	import frocessing.color.ColorHSV;
	import gs.easing.Sine;
	import gs.TweenMax;
	import org.papervision3d.cameras.SpringCamera3D;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.Particles;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Particle;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.core.render.data.RenderHitData;
	import org.papervision3d.core.utils.Mouse3D;
	import org.papervision3d.materials.special.BitmapParticleMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.BasicView;
	
	/**
	 * Papervsion Particles step 1
	 * @author Mario Gonzalez
	 */
	[SWF(width="465", height="465", backgroundColor="#000000", frameRate="35")]
	public class ParticleOrbit extends BasicView
	{
		private var _mouse3D			:Mouse3D;
		private var _particles			:Particles = new Particles();
		private var _currentParticle	:Particle;
		
		private var _lines				:Lines3D;
		private var _currentLine		:Line3D;
		private var _lineMaterial		:LineMaterial;
		
		private var PARTICLE_COUNT		:int = 80;
		
		private var _extraRotation		:Number = 0;
		private var _rotationSpeed		:Number = 0.01;
		private var _springCamera		:SpringCamera3D;
		
		private var _currentParticleInArray	:int = 0;
		private var _camTarget			:DisplayObject3D = new DisplayObject3D();
		private var _timer:Timer;
		
		
		private var colorHSV:ColorHSV = new ColorHSV(360, 1, 1);
		private var data:BitmapData;
		
		public function ParticleOrbit() 
		{
			Wonderfl.capture_delay(2);
			Mouse3D.enabled = true;
			super.viewport.interactive = true;
			_mouse3D = viewport.interactiveSceneManager.mouse3D;
			
			//trace(super.camera.focus)
			//super.camera.fov = 160;
			//super.camera.z = -2000;
			//super.camera.focus = 13
			stage.addEventListener(MouseEvent.CLICK, explode);
			
			createParticleRing();
			//createLines3D();
			//_springCamera = new SpringCamera3D(60, 10, 5000, false);
			//_springCamera.focus = 300;
			//_springCamera.zoom = 1;
			//_springCamera.target = _camTarget;
			//_springCamera.positionOffset = new Number3D(0, -100, -1000);
			//_springCamera.mass = 30
			//_springCamera.damping = 30;
			//_springCamera
			//
			//_timer = new Timer(4000, 0);
			//_timer.addEventListener(TimerEvent.TIMER, changeTarget);
			//_timer.start();
			//_timer.dispatchEvent(new TimerEvent(TimerEvent.TIMER));
			startRendering();
		}
		
		private function explode(e:MouseEvent):void 
		{
			for (var i:int = 0; i < PARTICLE_COUNT; i++)
			{
				_currentParticle = _particles.particles[i] as Particle;
				var force:Number = 300;
				ParticlePhysicsInfo(_currentParticle._userData).addForce(Utils.randRange( -force, force), Utils.randRange( -force, force), Utils.randRange( -force, force));
			}
		}
		
		private function changeTarget(e:TimerEvent):void 
		{
			var p:Particle = Utils.randInArray(_particles.particles) as Particle
			TweenMax.to(_camTarget, 1, { x: p.x, y: p.y, z: p.z, ease: Sine.easeInOut } );
		}
		
		private function createLines3D():void
		{
			_lines = new Lines3D();
			_lineMaterial = new LineMaterial(0xffff);
			
			for (var i:int = 0; i < PARTICLE_COUNT; i++)
			{
				var startParticle	:Particle = Utils.randInArray(_particles.particles);
				var endParticle		:Particle = Utils.randInArray(_particles.particles);
				
				var line	:Line3D = new Line3D(_lines, _lineMaterial, 2, startParticle.vertex3D, endParticle.vertex3D);
					//line._userData = new LineInfo(startParticle, endParticle);
				_lines.addLine(line);
			}
			
			
			scene.addChild(_lines);
		}
		
		private function createParticleRing():void
		{
			// Make the particle example
			var shape:Shape = new Shape();
			var g:Graphics = shape.graphics;
				g.beginFill(0xffffff, 0.5);
				g.drawCircle(16, 16, 16);
				g.beginFill(0xffffff);
				g.drawCircle(16, 16, 8);
				g.endFill();
			data = new BitmapData(shape.width, shape.height, true, 0xFFFFFF);
			data.draw(shape);
			
			var material:BitmapParticleMaterial = new BitmapParticleMaterial(data, 1);
			var image:BitmapData = new BitmapData(data.width, data.height, true);
			
			//Createa a few particles
			for (var i:int = 0; i < PARTICLE_COUNT; i++)
			{
				var theta:Number = (i / PARTICLE_COUNT) * Math.PI * 2;
				var force:Number = 300;
					colorHSV.h = theta * 180 / Math.PI;
					
				// Change the color of our particle
				image = data.clone();
				image.applyFilter(image, image.rect, image.rect.topLeft, new ColorMatrixFilter(ColorHelper.colorize(colorHSV.value)))
				material = new BitmapParticleMaterial(image, 3);
				
				// Create the particle using the new colored material
				_currentParticle = new Particle(material);
				_currentParticle.x = Math.cos(theta) * 232;
				_currentParticle.y = Math.sin(theta) * 232;
				_currentParticle._userData = new ParticlePhysicsInfo(_currentParticle, Utils.randRange(-force, force), Utils.randRange(-force, force), Utils.randRange(-force, force), 0, 0, 0);
				_particles.addParticle(_currentParticle);
			}			
			scene.addChild(_particles);
		}
		
		override protected function onRenderTick(e:Event = null):void
		{
			_extraRotation += _rotationSpeed;
			
			if (Math.random() < 0.6)
			{
				_currentParticleInArray++;
				_currentParticleInArray %= PARTICLE_COUNT;
			}			
			for (var i:int = 0; i < PARTICLE_COUNT; i++)
			{
				var theta:Number = (i / PARTICLE_COUNT) * Math.PI * 2 //+ _extraRotation;
				//_currentParticle = _particles[i] as Particle;
				
				_currentParticle = _particles.particles[i] as Particle;
				ParticlePhysicsInfo(_currentParticle._userData).update();
				
			
				if (Math.random() < 0.02)
				{
					var force:Number = 200;
					ParticlePhysicsInfo(_currentParticle._userData).addForce(Utils.randRange(-force, force), Utils.randRange( -force, force), Utils.randRange( -force, force));
				}
			}
			
			_particles.rotationY -= super.viewport.containerSprite.mouseX / 50;
			_particles.rotationZ = -30;
			
			super.onRenderTick(e);
			//super.renderer.renderScene(scene, _springCamera, viewport);
		}
	}
}

import org.papervision3d.core.data.UserData;
import org.papervision3d.core.geom.renderables.Particle;

internal class ParticlePhysicsInfo extends UserData
{
	public var _particle:Particle;
	
	public var _vx:Number = 0;
	public var _vy:Number = 0;
	public var _vz:Number = 0;
	
	public var _ax:Number = 0;
	public var _ay:Number = 0;
	public var _az:Number = 0;
	
	public var _ix:Number;
	public var _iy:Number;
	public var _iz:Number;
	
	public static var ACC_PARAM:Number = 0.1;
	public static var VELOCITY_PARAM:Number = 0.8
	
	public function ParticlePhysicsInfo(particle:Particle, vx:Number, vy:Number, vz:Number, ax:Number, ay:Number, az:Number):void
	{
		_particle = particle;
		
		_ix = _particle.x
		_iy = _particle.y
		_iz = _particle.z
		
		_vx = vx;
		_vy = vy;
		_vz = vz;
		
		_ax = ax;
		_ay = ay;
		_az = az;
	}
	
	public function update():void
	{
		_ax += (_ix - _particle.x) * ACC_PARAM;
		_ay += (_iy - _particle.y) * ACC_PARAM;
		_az += (_iz - _particle.z) * ACC_PARAM;
		
		_vx = (_vx + _ax) * VELOCITY_PARAM;
		_vy = (_vy + _ay) * VELOCITY_PARAM;
		_vz = (_vz + _az) * VELOCITY_PARAM;
		
		_particle.x += _vx;
		_particle.y += _vy;
		_particle.z += _vz;
		
		_ax = _ay = _az = 0;
	}
	
	public function addForce(fx:Number, fy:Number, fz:Number):void
	{
		_ax += fx + Math.random() * 8 - 4;
		_ay += fy + Math.random() * 8 - 4;
		_az += fz + Math.random() * 8 - 4;
	}
	
}

internal class Utils
{
	/**
	 * Return a random element inside an array
	 * @param	array, The array you want to get a random element from
	 * @return	*, An object in that array
	 */
	public static function randInArray(array:*):*
	{
		if (array) return array[randRange(0, array.length - 1)];
	}
	
	/**
	 * Random interger within range.
	 * @param	min
	 * @param	max
	 * @return
	 */
	public static function randRange(min:int, max:int):int
	{
		var fmin:Number = min - .4999;
		var fmax:Number = max + .4999;
		return int(Math.round(fmin + (fmax - fmin) * Math.random()));
	}
}


/**
 * Modify the color of an asset without destroying color contrast / shading in the asset.
 * Uses hue/saturation/brightness/contrast to modify a color keeping contrast between colors in the asset intact
 * @version 1.3
 */
internal class ColorHelper
{
	/**
	 * Colorize an asset based on an RGB value
	 * @param	rgb		Hex color value
	 * @param	amount	How much of the original color to keep. [0.0-1.0], 1.0 means none. Range can exceed 1.0 for experimental results
	 */
	public static function colorize(rgb:Number, amount:Number=1):Array
	{
		var r:Number;
		var g:Number;
		var b:Number;
		var inv_amount:Number;
		
		// Found after some googling - @ http://www.faqs.org/faqs/graphics/colorspace-faq/ (ctrl+f luminance)
		var LUMA_R:Number = 0.4086;
		var LUMA_G:Number = 0.7094;
		var LUMA_B:Number = 0.0920;
		
		r = (((rgb >> 16) & 0xFF) / 0xFF);
		g = (((rgb >> 8) & 0xFF) / 0xFF);
		b = ((rgb & 0xFF) / 0xFF);
		
		inv_amount = (1 - amount);
		
		return concat([(inv_amount + ((amount * r) * LUMA_R)), ((amount * r) * LUMA_G), ((amount * r) * LUMA_B), 0, 0,
				((amount * g) * LUMA_R), (inv_amount + ((amount * g) * LUMA_G)), ((amount * g) * LUMA_B), 0, 0, 
				((amount * b) * LUMA_R), ((amount * b) * LUMA_G), (inv_amount + ((amount * b) * LUMA_B)), 0, 0, 
				0, 0, 0, 1, 0]);
	}
	
	/**
	 * Concat two matrices
	 * Could be used to mix colors, but for now it only concacts with an identy matrix
	 * @param	mat	Matrix we want to concact
	 */
	public static function concat( mat:Array ):Array
	{
		// Identity matrix
		var matrix:Array = [1, 0, 0, 0, 0, // RED
							0, 1, 0, 0, 0, // GREEN
							0, 0, 1, 0, 0, // BLUE
							0, 0, 0, 1, 0]; // ALPHA
							
		var temp:Array = new Array();
		
		var i:int = 0;
		var x:int, y:int;
		
		
		// Loop through the matrice
		for (y = 0; y < 4; y++ )
		{
			
			for (x = 0; x < 5; x++ )
			{
				temp[ int( i + x) ] =  Number(mat[i])      * Number(matrix[x]) + 
									   Number(mat[int(i + 1)]) * Number(matrix[int(x +  5)]) + 
									   Number(mat[int(i + 2)]) * Number(matrix[int(x + 10)]) + 
									   Number(mat[int(i + 3)]) * Number(matrix[int(x + 15)]) +
									   (x == 4 ? Number(mat[int(i + 4)]) : 0);
			}
			i+=5;
		}
		
		return temp;
	}
}