Author Topic: [FLASH] Simple 3D Starfield  (Read 2614 times)

0 Members and 1 Guest are viewing this topic.

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4380
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
[FLASH] Simple 3D Starfield
« on: August 24, 2008 »
Hi,

after googling around and reading more and more about
the upcoming features of the FLASH V10.0 (Codename:
ASTRO) platform (pixelshader-like additions, audio/synth
features ...
) I decided to do some research on it and give
it a try.

Well, as a simple HelloWorld effect I did a basic 3D star-
field (Flash Player 9.0 required).

http://labs.weltenkonstrukteur.de/simplestarfield

It is written in pure ActionScript 3.0.

Source is of course available:

stars.as
Code: [Select]
package 
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;

/**
* ...
* @author benny!weltenkonstrukteur.de
*/
public class Stars extends Sprite
{

private var bitmap:Bitmap;
private var canvas:BitmapData;
private var sw:int, sh:int, snum:int;
private var stars:Array;
private var colors:Array;

public function Stars() {
snum = 250;
sw = stage.stageWidth;
sh = stage.stageHeight;
colors = new Array(
"0x000000", // never used
"0xFFFFFF",
"0xBBBBBB",
"0x888888",
"0x444444"
);

init();

addEventListener( "enterFrame", run );
}

private function init():void {
canvas = new BitmapData( sw, sh, false );
bitmap = new Bitmap( canvas );
stars = new Array( snum );
for ( var i:int = 0; i < snum ; i++ ) {
stars[i] = new StarModel();
}
addChild( bitmap );
}

private function run( e:Event ):void {
canvas.lock();

canvas.fillRect( canvas.rect, 0x111122 );
for ( var i:int = 0; i < snum ; i++ ) {
stars[i].move();
canvas.setPixel( stars[i].sx, stars[i].sy, colors[ stars[i].scol ] );
}

canvas.unlock();
}


}

}

StarModel.as
Code: [Select]
package 
{

/**
* ...
* @author benny!weltenkonstrukteur.de
*/
public class StarModel
{

private var x:Number, y:Number, z:Number, zv:Number;
public var sx:int, sy:int, scol:int;

public function StarModel() {
init();
}

public function move():void {
z = z - zv;
sx = (int)(x / z * 100 + 320 );
sy = (int)(y / z * 100 + 200 );
scol = Math.round( z / 100 );
if ( sx < 0 || sx > 640
|| sy < 0 || sy > 400
|| z < 1 ) {
init();
}
}

private function init():void {
this.x = Math.random() * 1000 - 500
this.y = Math.random() * 1000 - 500 ;
this.z = Math.random() * 500 + 100;
this.zv = Math.random() * 5 + 1;
this.scol = Math.round(this.z / 100);
}
}

}
« Last Edit: August 24, 2008 by benny! »
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: [FLASH] Simple 3D Starfield
« Reply #1 on: August 24, 2008 »
I just found the perfect song to listen to while watching a starfeild like that

'Dont Fear the Reaper' - Blue Oyster Cult


Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4380
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [FLASH] Simple 3D Starfield
« Reply #2 on: August 25, 2008 »
lol .. you are right. I opened this:

http://de.youtube.com/watch?v=z5rW-YvYmUE

in a new tab - fits good the an endless star flight ;-)
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17378
  • Karma: 497
  • evil/good
    • View Profile
    • My Homepage
Re: [FLASH] Simple 3D Starfield
« Reply #3 on: August 25, 2008 »
That looks nice and deep, just like a starfield should :D

And thanks for not opening a tiny screen and making it a good size too! It looks very nice :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4380
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: [FLASH] Simple 3D Starfield
« Reply #4 on: August 26, 2008 »
Thanks mate.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won: