forked from: forked from: Galaxy (Sandy 3D)
// forked from Razorblade's forked from: Galaxy (Sandy 3D)
// forked from makc3d's Galaxy (Sandy 3D)
// forked from makc3d's Galaxy (Pixels)
package
{
import flash.events.Event;
import flash.filters.GlowFilter;
import sandy.core.*;
import sandy.core.data.*;
import sandy.core.scenegraph.*;
import sandy.primitive.*;
import sandy.materials.*;
import sandy.view.*;
[SWF (width=465, height=465, backgroundColor=0x0, frameRate=20)]
public class Galaxy extends BasicView
{
private var sf:StarField;
public function Galaxy()
{
super (); init (465, 465);
camera.z = -100;
sf = new StarField ();
rootNode.addChild (sf);
var stars:Array = [];
generate (stars);
generate (stars);
generate (stars);
generate (stars);
generate (stars);
generate (stars);
// trying to give central stars more visibility
// by placing them after other stars (works ??)
stars.reverse ();
for each (var obj:Object in stars) {
sf.stars.push (new Vertex (obj.x, obj.y, obj.z));
sf.starColors.push (obj.c + 0x1000000 * obj.a);
}
render ();
filters = [ new GlowFilter (0x007fff, 0.5, 30, 30, 10) ];
}
override public function simpleRender(e:Event = null):void {
sf.pan += 2; sf.roll += 1; super.simpleRender (e);
}
private function generate (stars:Array):void {
// I wrote this code back in late '90s in TurboPascal
// By now, I have no idea how it works, and what those magic numbers are :)
var I:Number, J:Number, K:Number,
s:Number, L:Number, d:Number, R:Number,
dX:Number, dY:Number, dZ:Number,
c2:Number;
var Rm:Number = 20, A:Number = 0.3;
var clr:Array = [0x0080FF, 0x8080E4, 0xB0B0FF];
for (I = 0; I < 101; I++)
{
A = A + 0.03;
R = A * Rm;
for (J = 0; J < 5 - Math.floor(I / 20); J++)
{
for (K = 0; K < 5; K++)
{
L = clr[(R > 3 * Rm * Math.random()) ? 0 : 1];
c2 = (R > 2 * Rm * Math.random()) ? 1 : 2;
if (A < 0.6) L = clr[2];
s = Math.max (2, Rm - R / 3);
dX = R * Math.cos(A + K * 2 * Math.PI / 5) +
0.2 * ((100 - I) * Math.random() + I / 2);
dY = R * Math.sin(A + K * 2 * Math.PI / 5) +
0.2 * ((100 - I) * Math.random() + I / 2);
dZ = s * (Math.random() - Math.random());
stars.push ({ x: dX, y: dY, z: dZ, c: L, a: (255 - 120 * c2) });
}
}
}
}
}
}