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

Endless Candy

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/oYmJ
 */

package  
{
	import gs.TweenMax;
	import gs.easing.Sine;
	
	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.TimerEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.utils.Timer;
	
	import frocessing.color.ColorHSV;
	
	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.Number2D;
	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="0x000000", frameRate="60")]
	public class EndlessCandy extends BasicView
	{
		//new com.greensock.TweenMax
		private var _mouse3D			:Mouse3D;
		private var _particles			:Particles = new Particles();
		private var _particlesArray		:Array = [];
		
		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 image:BitmapData
		
		private var colorHSV:ColorHSV = new ColorHSV(360, 0.9, 1);
		private var data:BitmapData;
		
		public function EndlessCandy() 
		{
			Mouse3D.enabled = true;
			super.viewport.interactive = true
			_mouse3D = viewport.interactiveSceneManager.mouse3D;
					
			createParticleRing();
			_springCamera = new SpringCamera3D(100, 10, 19000, false);
			_springCamera.focus = 75;
			_springCamera.zoom = 1;
			//_springCamera.fov = 150;
		//	trace(_springCamera.fov)
			_springCamera.target = _camTarget;
			_springCamera.positionOffset = new Number3D(0,0,0);
			//trace(_springCamera.mass, _springCamera.damping)
			_springCamera.mass = 50;
			_springCamera.damping = 5;
			//_springCamera
			
			scene.addChild(_particles);
			startRendering();
		}
		
		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(32, 32, 32);
			g.beginFill(0xffffff);
			g.drawCircle(32, 32, 16);
			g.endFill();
			
			data = new BitmapData(shape.width, shape.height, true, 0xFFFFFF);
			data.draw(shape);
			var material:BitmapParticleMaterial = new BitmapParticleMaterial(data, 1);
			
			//_currentParticle = new Particle(material, 1);
			//_particles.addParticle(_currentParticle);
			
			image = new BitmapData(data.width, data.height, true);		
		}
		
		override protected function onRenderTick(e:Event = null):void
		{
			var glide:Number = 0.1;
			
			//_camTarget.z += 10;
			_camTarget.x -= (stage.stageWidth/2 - stage.mouseX) * glide;
			_camTarget.z += 55;
			//trace((stage.mouseY - stage.stageHeight) * 0.1)
			//_camTarget.y += Math.random() * 5;
			//_camTarget.z += Math.random() * 5;
			
			
			
			
			//trace(xDiff)
			while(Math.random() < 0.7)
				createParticle(null);
			
			//trace(_mouse3D.)
			super.renderer.renderScene(scene, _springCamera, viewport);
			
			if(_particlesArray.length > 100)
				_particles.removeParticle(_particlesArray.shift() as Particle);
		}
		
		protected function createParticle(position:Number3D):Particle
		{
			// No position given make one up
			if(position == null) {
				var position:Number3D = new Number3D();
				position.x = _camTarget.x + Utils.randRange(-2000, 2000);
				position.y = _camTarget.y+ Utils.randRange(-2000, 2000);
				position.z = _camTarget.z + Utils.randRange(3000, 8000);
			}
			
			colorHSV.h += Math.random();
			
			image = data.clone();
			image.applyFilter(image, image.rect, image.rect.topLeft, new ColorMatrixFilter(ColorHelper.colorize(colorHSV.value)))
			
			var material:BitmapParticleMaterial = new BitmapParticleMaterial(image, 1);
			_currentParticle = new Particle(material, Math.random() * 2 + 1);
			_currentParticle.x = position.x;
			_currentParticle.y = position.y;
			_currentParticle.z = position.z;
		//	_currentParticle.
			
			
			
			_particles.addParticle(_currentParticle);
			_particlesArray.push(_currentParticle); 
			return _currentParticle;
		}
	
	}
}

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

internal class LineInfo extends UserData
{
	public var _startParticle	:Particle;
	public var _endParticle		:Particle;
	
	public function LineInfo(s:Particle, e:Particle)
	{
		_startParticle = s;
		_endParticle = e;
	}
}
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;
	}
}