PV3Dtest

by maxcaffy
PV3dの復習中
♥0 | Line 100 | Modified 2011-01-03 03:44:41 | MIT License
play

ActionScript3 source code

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

//復習

package {
    import flash.display.Shape;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.events.Event;
    import flash.display.Sprite;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.utils.*;
    import org.papervision3d.materials.shadematerials.*;
    import org.papervision3d.lights.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.*;
    import flash.display.Stage;
    public class FlashTest extends BasicView {
        
        private var sph:Sphere; 
        private var lig:PointLight3D;
        private var mat:FlatShadeMaterial; 
        private var f_left:Boolean = false;
        private var f_right:Boolean = false;
        private var d_dry:Number = 0;
        private var dx:Number = 0;
        private var dy:Number = 0;
        private var dz:Number = 0;
        private const BEAT:int = 10;
        private var count:int = 0;
        
        public function FlashTest() {
            // write as3 code here..

            lig = new PointLight3D(true);
            mat = new FlatShadeMaterial(lig,0x00FF00,0xFF0000);
            sph = new Sphere(mat,500,20,20);

            var test:Shape = new Shape();
            test.graphics.beginFill(0xffff00);
            test.graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,100);
            stage.addChild(test);
            
            stage.addChild(viewport);            
            
            with(lig){
                x = 100;
                y = 100;
                z = -50;
            }
            
            addEventListener(Event.ENTER_FRAME,entFunc);
            stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunc);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunc);
            
            scene.addChild(sph);
            startRendering();
        }
        
        private function entFunc(e:Event):void{
           sph.rotationX ++;
           count ++;
           if(count > BEAT){//ビート間隔
               count = 0;
               dx += Math.random()*50 - 25;
               dy += Math.random()*50 - 25;
               dz += Math.random()*50 -25;
           }

           sph.scaleX = sph.scaleY = sph.scaleZ = 0.05*(count/BEAT)+0.06;
           
           if(f_left){
               d_dry += 1;
           }if(f_right){
               d_dry -= 1;
           }
           
           sph.rotationY += d_dry;
           sph.x += dx;
           sph.y += dy;
           sph.z += dz;
           
           sph.x *= 0.95;
           sph.y *= 0.95;
           sph.z *= 0.95;
           
           if(Math.abs(d_dry) > 0.2){
               d_dry *= 0.95;
           }if(Math.abs(dx) > 0.2){
               dx *= 0.95;
           }if(Math.abs(dy) > 0.2){
               dy *= 0.95;
           }if(Math.abs(dz) > 0.2){
               dz *= 0.95;
           }
        }
        
        private function keyUpFunc(e:KeyboardEvent):void{
           switch(e.keyCode){
               case Keyboard.LEFT:
                   f_left = true;
                   break;
               case Keyboard.RIGHT:
                   f_right = true;
                   break;
           }
        }
        
        private function keyDownFunc(e:KeyboardEvent):void{
           switch(e.keyCode){
               case Keyboard.LEFT:
                   f_left = false;
                   break;
               case Keyboard.RIGHT:
                   f_right = false;
                   break;
           }
        }

    }
}