Universe with sound
/**
* Copyright bongiovi015 ( http://wonderfl.net/user/bongiovi015 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gVGB
*/
package {
import flash.display.GradientType;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class WonderUniverse1 extends Sprite {
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------
private static const POLICY_FILE:String = "http://www.bongiovi.tw/crossdomain.xml";
private static const SOUND_FILE:String = "http://www.bongiovi.tw/wonderfl/essbe/House_Me.mp3";
public static const W : int = 465;
public static const R : int = 300;
public static const MAX : int = 200;
public static const SPEED : Number = 5;
public static const COLORS : Array = [0xE7D89D, 0xC0B483];
private var __particles : Vector.<Particle> = new Vector.<Particle>();
private var __sContainer : Shape = new Shape;
private var __sound : Sound = new Sound;
private var __soundChannel : SoundChannel;
private var __bytes : ByteArray = new ByteArray();
private var __vrx : Number = 0;
private var __vry : Number = 0;
private var __vrz : Number = 0;
private var __radius : Number = 20;
private var __bOffset : Number = 0;
private var __sLoading : Shape = new Shape;
private var __sbg : Shape = new Shape;
/**
* @constructor
*/
public function WonderUniverse1(){
addChild(__sbg);
var mtx:Matrix = new Matrix;
mtx.createGradientBox(465, 465);
__sbg.graphics.beginGradientFill(GradientType.RADIAL, [0x62410d, 0x000000], [1, 1], [25, 255], mtx);
__sbg.graphics.drawRect(0, 0, 465, 465);
__sbg.graphics.endFill();
addChild(__sLoading);
__sound.addEventListener(Event.COMPLETE, __onSoundLoaded);
__sound.addEventListener(ProgressEvent.PROGRESS, __onProgress);
__sound.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent) : void { trace("Error Loading Sound File"); });
__sound.load(new URLRequest(SOUND_FILE), new SoundLoaderContext(10000, true));
}
private function __onProgress(e:ProgressEvent) : void {
__sLoading.graphics.clear();
__sLoading.y = 465 / 2;
__sLoading.graphics.beginFill(0xFFFFFF, 1);
__sLoading.graphics.drawRect(0, 0, 465 * e.bytesLoaded / e.bytesTotal, 1);
__sLoading.graphics.endFill();
}
private function __onSoundLoaded(e:Event) : void {
__sound.removeEventListener(ProgressEvent.PROGRESS, __onProgress);
removeChild(__sLoading);
__initBg();
__initParticles();
addChild(__sContainer);
__soundChannel = __sound.play();
__soundChannel.addEventListener(Event.SOUND_COMPLETE, function(e:Event) : void {__soundChannel = __sound.play(); } );
addEventListener(Event.ENTER_FRAME, render);
}
public function render(e:Event=null) : void {
const RADIUS : int = 18;
SoundMixer.computeSpectrum(__bytes, true);
var ary:Array = [];
for (var j:int = 0; j < 128; j++) ary[j] = __bytes.readFloat();
__sContainer.graphics.clear();
var p:Particle;
var pos:Vector3D;
var aTemp:Array = [];
__vrx += ary[2] == 0 ? 0 : (ary[2] - .3) * .8;
__vry += ary[14] == 0 ? 0 : (ary[14] - .3) * .8;
__vrz += ary[26] == 0 ? 0 : (ary[26] - .3) * .8;
__radius = RADIUS + (ary[4] - .3) * 10;
__bOffset = (ary[8] - .3) * .5;
if(__radius < 1) __radius = 1;
else if (__radius > 40) __radius = 40;
for each(p in __particles) {
p.matrix.appendRotation(__vry, Vector3D.Y_AXIS);
p.matrix.appendRotation(__vrx, Vector3D.X_AXIS);
p.matrix.appendRotation(__vrz, Vector3D.Z_AXIS);
aTemp.push(p);
}
__vrx *=.95;
__vry *=.95;
__vrz *=.95;
aTemp.sortOn("z", Array.NUMERIC | Array.DESCENDING);
var i:int =0;
for(i=0; i<aTemp.length; i++) {
p = aTemp[i];
pos = p.matrix.position;
if(pos.z > R * .6) continue;
__sContainer.graphics.beginFill(getCircleColor(p.color, pos), 1);
__sContainer.graphics.drawCircle(pos.x + W*.5, pos.y + W*.5, getLength(pos.z));
__sContainer.graphics.endFill();
}
}
public function getCircleColor(color:uint, pos:Vector3D) : uint {
const BRIGHTNESS : Number = 1.5;
const BRIGHTNESS_OFFSET : Number = .3;
var ctOffset:Number = Math.sqrt( Math.pow(pos.x, 2) + Math.pow(pos.y, 2) + Math.pow(pos.z, 2) ) / R;
return brightnessColor(color, (1- ctOffset) * BRIGHTNESS + BRIGHTNESS_OFFSET + __bOffset);
}
public function getLength(depth:Number) : Number {
var sizeOffset:Number = Math.pow( (1 - (depth + R) / (2 * R)) * .8 + .4, 3);
return Math.floor(__radius*sizeOffset * 100) / 100;
}
private function __initParticles() : void {
var i : int = 0;
for(i=0; i<MAX; i++) {
var p:Particle = new Particle();
p.matrix.identity();
p.matrix.appendTranslation(Math.random() * R, 0, 0);
p.matrix.appendRotation(Math.random() * 360, Vector3D.X_AXIS);
p.matrix.appendRotation(Math.random() * 360, Vector3D.Y_AXIS);
p.matrix.appendRotation(Math.random() * 360, Vector3D.Z_AXIS);
p.color = getRandomColorFrom(COLORS);
__particles.push(p);
}
}
private function __initBg() : void {
var mtx:Matrix = new Matrix;
mtx.createGradientBox(600, 600, 0, (W-600) * .5);
graphics.beginGradientFill(GradientType.RADIAL, [0x62410d, 0x000000], [1, 1], [0, 255], mtx);
graphics.drawRect((W-600) * .5, 0, 600, 600);
graphics.endFill();
}
public static function getRandomColorFrom(colors:Array) : uint { return colors[int(Math.random() * colors.length)]; }
public static function brightnessColor(color:uint, level:Number) : uint {
var a:uint = color >> 24 & 0xFF;
var r:uint = color >> 16 & 0xFF;
var g:uint = color >> 8 & 0xFF;
var b:uint = color & 0xFF;
r *= level;
g *= level;
b *= level;
if(r > 0xFF) r = 0xFF;
if(g > 0xFF) g = 0xFF;
if(b > 0xFF) b = 0xFF;
var newColor:uint = a << 24 | r << 16 | g << 8 | b;
return newColor;
}
}
}
import flash.geom.Matrix3D;
class Particle {
public var matrix : Matrix3D = new Matrix3D;
public var mtxOrg : Matrix3D = new Matrix3D;
public var color : uint;
public var angle : Number;
public var length : Number;
public function get z() : Number { return matrix.position.z; }
}