※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

3D表現


機会あってflashで3D表現してみました。
ちなみにActionScript3.0ですのでご注意。


半径(radius)20、色(color)"#99cc00"の球を、
以下の座標の9点に配置しました。
(0,0,0)
(50,50,50)
(-50,50,50)
(50,-50,50)
(-50,-50,50)
(50,50,-50)
(-50,50,-50)
(50,-50,-50)
(-50,-50,-50)
マウスのx座標によって回転のスピードが増します。



  • RotateY.as
package {
  import flash.display.Sprite;
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.events.Event;
 
  public class RotateY extends Sprite
  {
    private var balls:Array;
    private var numBalls:uint = 9; //50
    private var fl:Number = 200;
    private var vpX:Number = stage.stageWidth / 2;
    private var vpY:Number = stage.stageHeight / 2;
 
    public function RotateY()
    {
      init();
    }
 
    private function init():void
    {
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;
 
      var xlist:Array = [0, 50, -50, 50, -50, 50, -50, 50, -50];
      var ylist:Array = [0, 50, 50, -50, -50, 50, 50, -50, -50];
      var zlist:Array = [0, 50, 50, 50, 50, -50, -50, -50, -50];
 
      balls = new Array();
      for(var i:uint = 0; i < numBalls; i++)
      {
        var ball:Ball3D = new Ball3D(15);
        balls.push(ball);
        ball.xpos = xlist[i]; //100>値>-100
        ball.ypos = ylist[i];
        ball.zpos = zlist[i];
        addChild(ball);
      }
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
   }
 
   private function onEnterFrame(event:Event):void
   {
     var angleY:Number = (mouseX - vpX) * .001;
     for(var i:uint = 0; i < numBalls; i++)
     {
       var ball:Ball3D = balls[i];
       rotateY(ball, angleY);
     }
     sortZ();
  }
 
  private function rotateY(ball:Ball3D, angleY:Number):void
  {
    var cosY:Number = Math.cos(angleY);
    var sinY:Number = Math.sin(angleY);
 
    var x1:Number = ball.xpos * cosY - ball.zpos * sinY;
    var z1:Number = ball.zpos * cosY + ball.xpos * sinY;
 
    ball.xpos = x1;
    ball.zpos = z1;
 
    if(ball.zpos > -fl)
    {
      var scale:Number = fl / (fl + ball.zpos);
      ball.scaleX = ball.scaleY = scale;
      ball.x = vpX + ball.xpos * scale;
      ball.y = vpY + ball.ypos * scale;
      ball.visible = true;
    }
    else
    {
      ball.visible = false;
    }
  }
 
  private function sortZ():void
  {
    balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
    for(var i:uint = 0; i < numBalls; i++)
    {
      var ball:Ball3D = balls[i];
      setChildIndex(ball, i);
    }
  }
}
 


  • Ball3D.as
package {
  import flash.display.Sprite;
 
  public class Ball3D extends Sprite {
    public var radius:Number;
    private var color:uint;
    public var xpos:Number = 0;
    public var ypos:Number = 0;
    public var zpos:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var vz:Number = 0;
    public var mass:Number = 1;
 
    public function Ball3D(radius:Number=20, color:uint=0x99cc00) {
      this.radius = radius;
      this.color = color;
      init();
    }
    public function init():void {
      graphics.lineStyle(0);
      graphics.beginFill(color);
      graphics.drawCircle(0, 0, radius);
      graphics.endFill();
    }
  }
}
 








today: -
yesterday: -