tags:

views:

23

answers:

0

I am trying to draw a custom 2-d shape on the screen and then create its 3-d model using Away 3-d. I have included the classes that I have used. I am yet to create the 3-d model using extrusion.The problem is that there is always a small offset when making the drawing. When trying out the code please click on the screen at two different points to get the drawing start and going. The drawing is triggered on MouseDown.

There are the two class files that were used in the project. Earlier it was working when the number of points were fixed. But I find that once I run the code for some time the FLASH -player starts throwing an error. I have no clue as to what is happening. Third problem is how to make the code dynamic. I tried array of objects methods but it is not working. May be I am not doing it correctly. I need to generate the 3-d view for any number of sides.Anybody trying the code should remember that you need the source files for Away 3-d.

These are the class files I used.

package 
{
 import away3d.cameras.*; 
 import away3d.containers.*;

 import flash.display.*; 
 import flash.events.*;

 [SWF(width="800", height="600")]
 public class ExtrusionBaseClass extends Sprite
 {
  protected var _view : View3D; 
  protected var _camera : HoverCamera3D;

  public function ExtrusionBaseClass()
  {
   _createView(); _createScene();
  }

  protected function _createView() : void
  {
   _camera = new HoverCamera3D(); 
   _camera.distance = 800;
   _camera.tiltAngle = 10; 
   _camera.panAngle = 180;

   _view = new View3D(); 
   _view.x = 400; 
   _view.y = 300; 
   _view.camera = _camera;

   addChild(_view); 
   addEventListener(Event.ENTER_FRAME, _onEnterFrame);
  }

  protected function _createScene() : void
  {
  }

  protected function _onEnterFrame(ev : Event) : void 
  {
   //_camera.panAngle -= (stage.mouseX - stage.stageWidth/2) / 100;
     _camera.hover();
     _view.render();
  }
 }
}

package {

import away3d.cameras.*;
 import away3d.containers.*;
 import away3d.core.base.*;
 import away3d.core.geom.*;
 import away3d.core.math.*;
 import away3d.extrusions.*;
 import away3d.materials.*;

 import flash.display.*;
 import flash.events.*;

 public class ExtrusionKitchenProject extends ExtrusionBaseClass
 {
  private var _xpos:Array = new Array(20); // Array for storing the mouse X coordinates.
  private var _ypos:Array = new Array(20); // Array for storing the mouse Y coordinates.
  private var sides:Number = 1;   //No of sides of figure. Incremented as the drawing goes on.
  private var init:Boolean = true;
  private var firstTime:Boolean = true;  
  private var mesh:Mesh = new Mesh();
  private var i:uint=0;
  var segment0:Segment = new Segment();

  public function ExtrusionKitchenProject()
  {
   super();
  }

  override protected function _createScene() : void
  {
   mesh.bothsides = true;
   var material : WireColorMaterial = new WireColorMaterial(0xFF0000);
   material.wireColor = 0x000000;
   material.thickness = 2;
   mesh.material = material;
   _view.scene.addChild(mesh); 

   // Drawing the three coordinate axis.
   segment0.moveTo(-400,100,0);
   segment0.lineTo(200, 100,0);
   segment0.moveTo(-100,300,0);
   segment0.lineTo(-100,-100,0);
   segment0.moveTo(-100,100,-300);
   segment0.lineTo(-100,100,300);
   mesh.addSegment(segment0);

   stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
   stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
  }

  public function onMouseDown(event:MouseEvent):void {

   if(firstTime)
   {

    _xpos[i] = _view.mouseX;
    _ypos[i] = _view.mouseY;
    firstTime = false;

    i++;
   }   
   else
   {
    _xpos[i] = _view.mouseX;
    _ypos[i] = _view.mouseY;

    // Drawing the 2 -d shape
    var segment1:Segment = new Segment();
       segment1.moveTo(_xpos[i-1],- _ypos[i-1],0);
    segment1.lineTo(_xpos[i],-_ypos[i],0);
    mesh.addSegment(segment1);
    i++;
   }

  }  
   public function onKeyUp(event:KeyboardEvent):void
  {
   // changing camera position while generating 3d.
   _camera.tiltAngle = 30; 
   _camera.panAngle = 160;
   mesh.removeSegment(segment0);

   var path : Path = new Path([
   // First curve:
   new Number3D(_xpos[0], 0,-_ypos[0]),
   new Number3D(_xpos[0]/2+_xpos[1]/2, 0,-_ypos[0]/2-_ypos[1]/2),
   new Number3D(_xpos[1], 0, -_ypos[1]),

   new Number3D(_xpos[1], 0,-_ypos[1]),
   new Number3D(_xpos[1]/2+_xpos[2]/2, 0,-_ypos[1]/2-_ypos[2]/2),
   new Number3D(_xpos[2], 0, -_ypos[2]),

   new Number3D(_xpos[2], 0,-_ypos[2]),
   new Number3D(_xpos[2]/2+_xpos[3]/2, 0,-_ypos[2]/2-_ypos[3]/2),
   new Number3D(_xpos[3], 0, -_ypos[3]),

   new Number3D(_xpos[3], 0,-_ypos[3]),
   new Number3D(_xpos[3]/2+_xpos[4]/2, 0,-_ypos[3]/2-_ypos[4]/2),
   new Number3D(_xpos[4], 0, -_ypos[4]),

   new Number3D(_xpos[4], 0,-_ypos[4]),
   new Number3D(_xpos[4]/2+_xpos[5]/2, 0,-_ypos[4]/2-_ypos[5]/2),
   new Number3D(_xpos[5], 0, -_ypos[5]),

   new Number3D(_xpos[5], 0,-_ypos[5]),
   new Number3D(_xpos[5]/2+_xpos[6]/2, 0,-_ypos[5]/2-_ypos[6]/2),
   new Number3D(_xpos[6], 0, -_ypos[6]), 

   new Number3D(_xpos[6], 0,-_ypos[6]),
   new Number3D(_xpos[6]/2+_xpos[7]/2, 0,-_ypos[6]/2-_ypos[7]/2),
   new Number3D(_xpos[7], 0, -_ypos[7]),

   new Number3D(_xpos[7], 0,-_ypos[7]),
   new Number3D(_xpos[7]/2+_xpos[8]/2, 0,-_ypos[7]/2-_ypos[8]/2),
   new Number3D(_xpos[8], 0, -_ypos[8])  
   ]);

 //Creating the profile for extrusion. 

   var profile : Array = [
   new Number3D(0, 0, 0),
   new Number3D(0, 30, 0)
   ]
 // Making  extrusion.   
   var extrusion : PathExtrusion = new PathExtrusion(path, profile);
   extrusion.subdivision = 10;
   extrusion.bothsides = true;
   _view.scene.addChild(extrusion);
  }  
 }
}