flash on 2012-8-7

by mutantleg
alternativa raycast test
♥0 | Line 117 | Modified 2012-10-23 16:11:54 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1HAF
 */



package {
    import flash.text.TextField;
    import flash.geom.Vector3D;
    import flash.utils.Dictionary;
    import flash.display.BlendMode;
    import flash.display.BitmapData;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    //import flash.media.Camera;
    
    //http://alternativaplatform.com/en/docs/7.7.0/index.html
    
    import alternativ7.*;
    import alternativ7.engine3d.*;
    import alternativ7.engine3d.core.*;
    import alternativ7.engine3d.containers.*;
    import alternativ7.engine3d.objects.*;
    import alternativ7.engine3d.primitives.*;
    import alternativ7.engine3d.materials.*;
    import alternativ7.engine3d.lights.*;

    import flash.display.Sprite;
    
    public class FlashTest extends Sprite {
        
        public var cam:Camera3D;
        public var cont:Object3DContainer = new ConflictContainer();
        public var gBox:Box;
        public var deb:TextField;
        
        public function FlashTest() {
            // write as3 code here..
               cam = new Camera3D();
                cam.view = new View(500,500); 
                cam.z = -1000;
                
                
                
               addChild(cam.view);
                cont.addChild(cam);
                deb = new TextField();
                addChild(deb);
                deb.text = "Debug";
                
                
                var bm:BitmapData;
                bm = new BitmapData(64,64,false,0);
                bm.noise(123213,0,16,3,true);
                
                var p:Plane;
                
                p = new Plane(1000, 1000, 8, 8);
                p.setMaterialToAllFaces( new TextureMaterial( bm ) );
                p.rotationX = 1.57;
                p.y = 200;
                cont.addChild(p);
                
                bm = new BitmapData(64,64,false,0);
                bm.noise(123123);
               
               var amb:AmbientLight;
               amb = new AmbientLight(0xFF0f0F0f);
               cont.addChild(amb);
               
               var light:OmniLight;
               light = new OmniLight(0xffff0000,500,1200);
               //light.intensity = 1000;
               light.x =-400
              // light.y = -40
               light.z = -400;
               cont.addChild(light);
                
                var box:Box;
                box = new Box(20,20,20,1,1,1);
                //box.x = 400;
               // box.setMaterialToAllFaces( new FillMaterial(0) );
              //  box.setMaterialToAllFaces( new TextureMaterial( bm) );
                box.setMaterialToAllFaces( new VertexLightMaterial( bm ) );
               //box.setMaterialToAllFaces( new FlatShadingMaterial( bm ) );
               box.x = 30;
               
                  box.calculateVerticesNormals();
                  cont.addChild(box);
                  gBox = box;
                  
                  stage.addEventListener(Event.ENTER_FRAME, onEnter);  
                  stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
                  stage.addEventListener(KeyboardEvent.KEY_UP, kup);    
               
               
          dictExclude[gBox] = gBox;
        }//ctor


        public var dictExclude:Dictionary = new Dictionary();
        public var start:Vector3D = new Vector3D();
        public var dir:Vector3D = new Vector3D();

        public function onEnter(e:Event):void
        {
            
            //ref
         //http://www.dakmm.com/?p=272
            if (cKeyMan.isKeyDown(39) ) { cam.x -= 20;}
            if (cKeyMan.isKeyDown(37) ) { cam.x += 20;}
            if (cKeyMan.isKeyDown(38) ) { cam.y += 20;}
            if (cKeyMan.isKeyDown(40) ) { cam.y -= 20;}
            
            //gBox.rotationY += 0.1;
            
             cam.render();

            var ret:RayIntersectionData;

            start.x = 0;
            start.y = 0;
            start.z = 0;
            dir.x = 0;
            dir.y = 1;
            dir.z = 0;
            
            ret = cont.intersectRay(start, dir, dictExclude, null);
            
            deb.text = "t: " + ret.time;
            
            var ix:Number, iy:Number, iz:Number;
            
            ix = start.x + (dir.x * ret.time);
            iy = start.y + (dir.y * ret.time);
            iz = start.z + (dir.z * ret.time);
            
            gBox.x = ix;
            gBox.y = iy - 10;
            gBox.z = iz;

//public override function intersectRay(origin:Vector3D, direction:Vector3D,   excludedObjects:Dictionary = null, camera:Camera3D = null):RayIntersectionData
             
        }//onenter
        
        public function kdown(e:KeyboardEvent):void
        {
         cKeyMan.setKey(e.keyCode, true);   
        }//kdown
        
        public function kup(e:KeyboardEvent):void
        {
          cKeyMan.setKey(e.keyCode, false);  
        }//kup
        
        
    }//flashtest
}//package


internal class cKeyMan
   {
       public function cKeyMan() {}//ctor (unused)
       
       public static var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,true);
       
       public static function setKey(k:int, b:Boolean):void
       {
           if (k < 0) return;
           if (k >= 512) return;
           vecKey[k] = b;
       }//setkey
       
       public static function isKeyDown(k:int):Boolean
       {
           if  (k < 0) return false;
           if (k >= 512) return false;
           return vecKey[k];
       }//iskey
       
   }//keyman