Sandy Spaceship and Starfield
3d Spaceship Model in Sandy
Arrow Keys to move
// 3d Spaceship Model in Sandy
// Arrow Keys to move
package {
import flash.display.*
import flash.events.*;
[SWF(width="465",height="465",backgroundColor="0x000000",frameRate="30")]
public class AS3Code extends Sprite {
public function AS3Code() {
main=this;
initialize();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
addEventListener(Event.ENTER_FRAME, EveryFrame);
}
}
}
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.filters.*
//
import sandy.core.Scene3D;
import sandy.core.data.*;
import sandy.core.scenegraph.*;
import sandy.materials.*;
import sandy.materials.attributes.*;
import sandy.primitive.*;
//
var main:Sprite;
//
var scene:Scene3D;
var camera:Camera3D;
var root:Group;
var lightA:LightAttributes = new LightAttributes (true, 0.5);
var lightB:GouraudAttributes = new GouraudAttributes (true, 0.25);
var arwing:Arwing;
var Ship:ShipVars = new ShipVars();
var stars:Array = [];
var StarAmount:uint = 400;
function initialize():void {
camera = new Camera3D( 465, 465);
camera.z = -1500;
camera.y = 0;
root = new Group();
lightA.diffuse = 0.5;
lightA.specular = 0.5;
lightA.gloss = 1;
lightA.ambient = 0.5;
lightB.diffuse = 2.5;
lightB.specular = 1.5;
lightB.gloss = 1;
lightB.ambient = 0.4;
arwing = new Arwing( "arwing");
var materialAttr:MaterialAttributes = new MaterialAttributes(lightB);
var colorArray:Array = [];
for(var i:int=0;i<212;i++){
if(i > 0 && i <20){
colorArray[i] = 0x212E9A;
}else{
colorArray[i] = 0xAAAAAA;
}
}
var material:Material = new ColorMaterial2( 0xAAAAAA, 1, materialAttr, colorArray);
material.lightingEnable = true;
arwing.appearance = new Appearance(material)
arwing.rotateY = 180;
arwing.boundingSphere.radius = 400;
root.addChild(arwing);
var sf:StarField = new StarField();
for (i = 0; i<StarAmount; i++) {
//var stars[i]:Vertex = new Vertex ();
stars[i] = new Vertex ();
stars[i].x = 5000*(Math.random()-Math.random());
stars[i].z = -2000+10000*(Math.random()-Math.random());
stars[i].y = 5000*(Math.random()-Math.random());
sf.stars.push(stars[i]);
sf.starColors.push(0xFF000000 + Math.floor(Math.random() * 0xFFFFFF));
sf.fadeFrom = 10000;
sf.fadeTo = 15000;
}
sf.container.filters = [ new GlowFilter (0x7FFF, 0.5, 3, 3, 3) ];
root.addChild(sf);
scene = new Scene3D( "scene", main, camera, root );
scene.light.color = 0xFFFFFF;
scene.light.setDirection(0, -100, 0);
scene.light.setPower(2.5);
}
function EveryFrame(event:Event):void{
if(isDown(Keyboard.RIGHT)){
Ship.xspeed += 3;
}
if(isDown(Keyboard.LEFT)){
Ship.xspeed -= 3;
}
if(isDown(Keyboard.UP)){
Ship.yspeed += 3;
}
if(isDown(Keyboard.DOWN)){
Ship.yspeed -= 3;
}
Ship.x += Ship.xspeed;
Ship.y += Ship.yspeed;
Ship.x = Math.min(Ship.x,800);
Ship.x = Math.max(Ship.x,-800);
Ship.y = Math.min(Ship.y,700);
Ship.y = Math.max(Ship.y,-700);
Ship.xspeed *= 0.9;
Ship.yspeed *= 0.9;
arwing.x = Ship.x;
arwing.y = Ship.y;
arwing.rotateY = 0;
arwing.rotateZ = 0;
arwing.lookAt(arwing.x,arwing.y,arwing.z-50);
arwing.rotateY = 0;
arwing.rotateZ += Ship.zrot;
arwing.rotateX -= Ship.yspeed*0.75;
arwing.rotateY = -Ship.xspeed*0.75;
camera.x += ((-arwing.x/2)-camera.x)/5;
camera.y += (((arwing.y+700)/4)-camera.y)/5;
camera.lookAt(arwing.x/5,arwing.y/5,arwing.z);
for (var i:int = 0; i<StarAmount; i++) {
stars[i].z -= 50;
if(stars[i].z < -2000){
stars[i].z += 20000;
}
}
scene.render();
}
class ShipVars{
public var xspeed:Number = 0;
public var yspeed:Number = 0;
public var x:Number = 0;
public var y:Number = 0;
public var zrot:Number = 0;
}
var initialized:Boolean = false;// marks whether or not the class has been initialized
var keysDown:Object = new Object();// stores key codes of all keys pressed
function isDown(keyCode:uint):Boolean {
return Boolean(keyCode in keysDown);
}
function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
class Arwing extends Shape3D implements Primitive3D {
private var l:Geometry3D;
private function f(v1:Number,v2:Number,v3:Number,uv00:Number,uv01:Number,uv10:Number,uv11:Number,uv20:Number,uv21:Number,normX:Number,normY:Number,normZ:Number):void {
var uv1:Number = l.getNextUVCoordID();
var uv2:Number = uv1 + 1;
var uv3:Number = uv2 + 1;
l.setUVCoords(uv1,uv00,1-uv01);
l.setUVCoords(uv2,uv10,1-uv11);
l.setUVCoords(uv3,uv20,1-uv21);
l.setFaceVertexIds(l.getNextFaceID(), v1,v2,v3);
l.setFaceUVCoordsIds(l.getNextFaceUVCoordID(), uv1,uv2,uv3);
l.setFaceNormal(l.getNextFaceNormalID(), normX,normZ,normY);
}
private function f2(v1:Number,v2:Number,v3:Number):void {
l.setFaceVertexIds(l.getNextFaceID(), v1,v2,v3);
}
private function v(vx:Number,vy:Number,vz:Number):void {
l.setVertex(l.getNextVertexID(),vx,vz,vy);
}
public function Arwing( p_Name:String=null ) {
super( p_Name );
geometry = generate();
}
public function generate(... arguments):Geometry3D {
l = new Geometry3D();
v(1.000000,1.000000,-0.683356);
v(0.944110,-1.095600,-0.645164);
v(-0.944111,-1.095600,-0.645164);
v(-1.000000,1.000000,-0.683356);
v(1.000000,0.999999,0.683356);
v(0.944110,-1.095600,0.645164);
v(-0.944111,-1.095600,0.645164);
v(-1.000000,1.000000,0.683356);
v(-0.679618,-4.902941,0.464420);
v(0.679615,-4.902942,0.464420);
v(-0.679618,-4.902941,-0.464421);
v(0.679616,-4.902941,-0.464421);
v(0.065945,-8.797762,-0.045066);
v(-0.065950,-8.797762,-0.045066);
v(0.065945,-8.797762,0.045065);
v(-0.065950,-8.797762,0.045065);
v(-1.000000,-1.000000,0.204147);
v(-1.000000,1.000000,0.204147);
v(1.000000,0.999999,0.204147);
v(0.999999,-1.000001,0.204147);
v(-1.000000,1.000000,-0.204147);
v(-1.000000,-1.000000,-0.204147);
v(1.000000,-1.000000,-0.204147);
v(1.000000,1.000000,-0.204147);
v(2.083799,-1.000000,-0.204147);
v(2.083799,1.000000,-0.204147);
v(2.083800,0.999999,0.204147);
v(2.083798,-1.000001,0.204147);
v(-2.083798,1.000000,-0.204147);
v(-2.083799,-1.000000,-0.204147);
v(-2.083800,-1.000000,0.204147);
v(-2.083798,1.000000,0.204147);
v(-2.222009,-1.535249,0.123641);
v(-2.222008,1.535249,0.123641);
v(2.222010,1.535248,0.123641);
v(2.222007,-1.535251,0.123641);
v(-2.222008,1.535250,-0.123641);
v(-2.222009,-1.535249,-0.123641);
v(2.222008,-1.535249,-0.123641);
v(2.222008,1.535249,-0.123641);
v(4.019095,1.491733,-0.098951);
v(4.019095,-0.121116,-0.098951);
v(4.019097,1.491732,0.098950);
v(4.019093,-0.121116,0.098950);
v(-4.019095,-0.121115,-0.098951);
v(-4.019093,1.491733,-0.098951);
v(-4.019096,-0.121115,0.098950);
v(-4.019094,1.491733,0.098950);
v(-4.111470,-0.121115,0.098950);
v(-4.111467,1.491733,0.098950);
v(4.111471,1.491732,0.098950);
v(4.111466,-0.121116,0.098950);
v(-4.111467,1.491733,-0.098951);
v(-4.111469,-0.121115,-0.098951);
v(4.111469,-0.121116,-0.098951);
v(4.111469,1.491733,-0.098951);
v(5.027250,-0.494357,-0.068746);
v(5.027250,1.118492,-0.068746);
v(5.027252,1.118491,0.068746);
v(5.027246,-0.494357,0.068746);
v(-5.027248,-0.494357,-0.068746);
v(-5.027246,1.118492,-0.068746);
v(-5.027249,-0.494357,0.068746);
v(-5.027246,1.118492,0.068746);
v(-5.693155,-1.072972,0.039673);
v(-5.693151,1.107409,0.039673);
v(5.693157,1.107409,0.039673);
v(5.693150,-1.072973,0.039673);
v(-5.693151,1.107410,-0.039674);
v(-5.693153,-1.072972,-0.039674);
v(5.693154,-1.072972,-0.039674);
v(5.693154,1.107409,-0.039674);
v(7.845545,0.625659,-0.019098);
v(7.845545,0.384196,-0.019098);
v(7.845547,0.625659,0.019098);
v(7.845538,0.384196,0.019098);
v(-7.845544,0.384196,-0.019098);
v(-7.845539,0.625659,-0.019098);
v(-7.845545,0.384196,0.019098);
v(-7.845539,0.625659,0.019098);
v(-0.696005,-0.680794,0.748494);
v(-0.696005,0.770009,1.053621);
v(0.696004,-0.680795,0.748494);
v(0.696005,0.770009,1.053621);
v(0.372146,-0.646706,0.855366);
v(0.372146,0.557490,1.738617);
v(-0.372146,0.557491,1.738617);
v(-0.372146,-0.646706,0.855366);
v(0.045225,3.751054,0.030905);
v(-0.045224,3.751054,0.030905);
v(-0.045224,3.751054,-0.030904);
v(0.045225,3.751054,-0.030904);
v(-1.975420,-0.800000,0.218665);
v(-1.975419,0.800000,0.218665);
v(1.975420,0.799999,0.218665);
v(1.975418,-0.800001,0.218665);
v(-1.108380,0.800000,0.218665);
v(-1.108380,-0.800000,0.218665);
v(1.108379,-0.800001,0.218665);
v(1.108380,0.799999,0.218665);
v(-1.498548,5.517805,1.838372);
v(-1.498548,-5.517756,0.497377);
v(1.498547,-5.517806,0.497371);
v(1.498548,5.517756,1.838366);
v(1.585251,5.517756,1.838366);
v(1.585251,-5.517806,0.497371);
v(-1.585252,-5.517756,0.497377);
v(-1.585251,5.517805,1.838372);
f2(101,100,107);
f2(101,107,106);
f2(103,102,105);
f2(103,105,104);
f2(93,92,106);
f2(93,106,107);
f2(95,94,105);
f2(94,104,105);
f2(96,93,107);
f2(96,107,100);
f2(92,97,101);
f2(92,101,106);
f2(98,95,105);
f2(98,105,102);
f2(94,99,103);
f2(94,103,104);
f2(99,98,102);
f2(99,102,103);
f2(97,96,101);
f2(96,100,101);
f2(16,17,96);
f2(16,96,97);
f2(18,19,98);
f2(18,98,99);
f2(26,18,94);
f2(18,99,94);
f2(19,27,98);
f2(27,95,98);
f2(30,16,92);
f2(16,97,92);
f2(17,31,96);
f2(31,93,96);
f2(27,26,94);
f2(27,94,95);
f2(31,30,92);
f2(31,92,93);
f2(88,91,90);
f2(88,90,89);
f2(0,3,90);
f2(0,90,91);
f2(4,0,88);
f2(0,91,88);
f2(3,7,89);
f2(3,89,90);
f2(7,4,88);
f2(7,88,89);
f2(85,86,87);
f2(85,87,84);
f2(81,80,87);
f2(81,87,86);
f2(80,82,87);
f2(82,84,87);
f2(83,81,85);
f2(81,86,85);
f2(82,83,84);
f2(83,85,84);
f2(5,4,82);
f2(4,83,82);
f2(4,7,81);
f2(4,81,83);
f2(6,5,82);
f2(6,82,80);
f2(7,6,80);
f2(7,80,81);
f2(76,78,79);
f2(76,79,77);
f2(72,74,73);
f2(74,75,73);
f2(64,65,78);
f2(65,79,78);
f2(66,67,75);
f2(66,75,74);
f2(65,68,77);
f2(65,77,79);
f2(69,64,76);
f2(64,78,76);
f2(68,69,76);
f2(68,76,77);
f2(67,70,75);
f2(70,73,75);
f2(71,66,72);
f2(66,74,72);
f2(70,71,73);
f2(71,72,73);
f2(56,57,71);
f2(56,71,70);
f2(57,58,71);
f2(58,66,71);
f2(59,56,67);
f2(56,70,67);
f2(61,60,68);
f2(60,69,68);
f2(60,62,69);
f2(62,64,69);
f2(63,61,68);
f2(63,68,65);
f2(58,59,66);
f2(59,67,66);
f2(62,63,65);
f2(62,65,64);
f2(48,49,63);
f2(48,63,62);
f2(50,51,58);
f2(51,59,58);
f2(49,52,61);
f2(49,61,63);
f2(53,48,60);
f2(48,62,60);
f2(52,53,61);
f2(53,60,61);
f2(51,54,59);
f2(54,56,59);
f2(55,50,57);
f2(50,58,57);
f2(54,55,57);
f2(54,57,56);
f2(41,40,55);
f2(41,55,54);
f2(40,42,55);
f2(42,50,55);
f2(43,41,51);
f2(41,54,51);
f2(45,44,52);
f2(44,53,52);
f2(44,46,53);
f2(46,48,53);
f2(47,45,52);
f2(47,52,49);
f2(42,43,51);
f2(42,51,50);
f2(46,47,49);
f2(46,49,48);
f2(32,33,46);
f2(33,47,46);
f2(34,35,43);
f2(34,43,42);
f2(33,36,45);
f2(33,45,47);
f2(37,32,44);
f2(32,46,44);
f2(36,37,44);
f2(36,44,45);
f2(35,38,43);
f2(38,41,43);
f2(39,34,40);
f2(34,42,40);
f2(38,39,41);
f2(39,40,41);
f2(24,25,39);
f2(24,39,38);
f2(25,26,34);
f2(25,34,39);
f2(27,24,38);
f2(27,38,35);
f2(28,29,37);
f2(28,37,36);
f2(29,30,37);
f2(30,32,37);
f2(31,28,33);
f2(28,36,33);
f2(26,27,34);
f2(27,35,34);
f2(30,31,33);
f2(30,33,32);
f2(17,20,28);
f2(17,28,31);
f2(21,16,29);
f2(16,30,29);
f2(20,21,28);
f2(21,29,28);
f2(19,22,27);
f2(22,24,27);
f2(23,18,25);
f2(18,26,25);
f2(22,23,25);
f2(22,25,24);
f2(1,0,22);
f2(0,23,22);
f2(0,4,18);
f2(0,18,23);
f2(5,1,19);
f2(1,22,19);
f2(3,2,21);
f2(3,21,20);
f2(2,6,21);
f2(6,16,21);
f2(7,3,20);
f2(7,20,17);
f2(4,5,19);
f2(4,19,18);
f2(6,7,16);
f2(7,17,16);
f2(12,14,15);
f2(12,15,13);
f2(9,8,15);
f2(9,15,14);
f2(8,10,15);
f2(10,13,15);
f2(11,9,12);
f2(9,14,12);
f2(10,11,12);
f2(10,12,13);
f2(2,1,11);
f2(2,11,10);
f2(1,5,11);
f2(5,9,11);
f2(6,2,8);
f2(2,10,8);
f2(5,6,8);
f2(5,8,9);
f2(0,1,2);
f2(0,2,3);
this.x = 0.000000;
this.y = 0.000000;
this.z = 0.000000;
this.rotateX = 0.000000;
this.rotateY = 0.000000;
this.rotateZ = 0.000000;
this.scaleX = 50.000000;
this.scaleY = 50.000000;
this.scaleZ = 50.000000;
return (l);
}
}
// using an edited version of Sandy3d's Color Material
class ColorMaterial2 extends Material
{
private var m_nColor:Number;
private var m_nAlpha:Number;
private var m_nArray:Array;
private var firstPolygonId:int = -1;
/**
* Creates a new ColorMaterial.
*
* @param p_nColor The color for this material in hexadecimal notation
* @param p_nAlpha The alpha value in percent of full opacity ( 0 - 1 )
* @param p_oAttr The attributes for this material
*/
public function ColorMaterial2( p_nColor:uint = 0x00, p_nAlpha:Number = 1, p_oAttr:MaterialAttributes = null, p_nArray:Array = null)
{
super(p_oAttr);
// --
m_oType = MaterialType.COLOR;
// --
m_nColor = p_nColor;
m_nAlpha = p_nAlpha;
m_nArray = p_nArray;
}
/**
* Renders this material on the face it dresses
*
* @param p_oScene The current scene
* @param p_oPolygon The face to be rendered
* @param p_mcContainer The container to draw on
*/
public override function renderPolygon( p_oScene:Scene3D, p_oPolygon:Polygon, p_mcContainer:Sprite ):void
{
if(firstPolygonId > p_oPolygon.id || firstPolygonId == -1){
firstPolygonId = p_oPolygon.id;
//trce(firstPolygonId);
}
const l_points:Array = (p_oPolygon.isClipped) ? p_oPolygon.cvertices : p_oPolygon.vertices;
if( !l_points.length ) return;
var l_oVertex:Vertex;
var lId:int = l_points.length;
var l_graphics:Graphics = p_mcContainer.graphics;
// --
l_graphics.lineStyle();
l_graphics.beginFill( m_nArray[p_oPolygon.id-firstPolygonId], m_nAlpha );
l_graphics.moveTo( l_points[0].sx, l_points[0].sy );
while( l_oVertex = l_points[ --lId ] )
l_graphics.lineTo( l_oVertex.sx, l_oVertex.sy );
l_graphics.endFill();
// --
if( attributes ) attributes.draw( l_graphics, p_oPolygon, this, p_oScene ) ;
}
/**
* @private
*/
public function get alpha():Number
{
return m_nAlpha;
}
/**
* @private
*/
public function get color():Number
{
return m_nColor;
}
/**
* The alpha value for this material ( 0 - 1 )
*
* Alpha = 0 means fully transparent, alpha = 1 fully opaque.
*/
public function set alpha(p_nValue:Number):void
{
m_nAlpha = p_nValue;
m_bModified = true;
}
/**
* The color of this material
*/
public function set color(p_nValue:Number):void
{
m_nColor = p_nValue;
m_bModified = true;
}
}