views:

1327

answers:

1

I am currently working on a FlarToolkit / Papervision3D / Quake2 model parsing, that I have successfully loaded, textured, and animated. That being said, the animation calls are merely a guess to me and so far all I know is that "jump" and "run" are available. When i use the built in getAnimationChannels() of the MD2 class, it merely returns and array of MorphChannel3D Objects as follows.

//md2 is a model that is already loaded and waiting utilizing the given events for such.

trace(_md2.getAnimationChannels()); //returns [object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D],[object MorphChannel3D]

From what I have seen in my own searches, this should be returning an array of the channel names in String form. I attempted to extract properties from the MorphChannel3D Object with no success using for each(var p:* in Object);

Where am I going wrong, and how can I obtain the name of the animation channels so that I may call them at will?

Example of the project here

Thanks.

A: 

Interesting project.

I played with MD2 a bit, but I got confused. Didn't know what classes to use and how. I asked the guys on the papervision mailing list, but no clear answers.

Here is my test

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import org.papervision3d.core.animation.channel.AbstractChannel3D;
    import org.papervision3d.core.animation.channel.MorphChannel3D;

    import org.papervision3d.events.AnimationEvent;
    import org.ascollada.utils.FPS;
    import org.papervision3d.cameras.*;
    import org.papervision3d.core.geom.*;
    import org.papervision3d.core.geom.renderables.*;
    import org.papervision3d.events.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.special.*;
    import org.papervision3d.materials.utils.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.parsers.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.render.*;
    import org.papervision3d.scenes.*;
    import org.papervision3d.view.*;
    import org.papervision3d.view.layer.*;

    /**
    * Basic MD2Test Class
    * @author George Profenza
    */
    public class MD2Test extends Sprite
    {

     private var viewport:BasicView;
     private var timer:Timer;
     private var fps:FPS;
     private var container:DisplayObject3D;
     private var box:MD2;
     private var isPlaying:Boolean = true;
     private var _channels:Array;
     private var _numFrames:int;
     private var _currentFrame:int;
     private var frameCount:int = 1;

     public function MD2Test() 
     {
      init();
     }

     private function init():void {

      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.SHOW_ALL;

      viewport = new BasicView(0, 0, true, true, CameraType.FREE);
      addChild(viewport);

      container = new DisplayObject3D();
      viewport.scene.addChild(container);

      fps = new FPS();
      addChild(fps);

      var mat:WireframeMaterial = new WireframeMaterial(0x990000);
      mat.doubleSided = true;

      box = new MD2(false);
      box.rotationX = -90;
      box.load("animations/box_wrap_anim.md2", mat, 6, 10);
      box.addEventListener(FileLoadEvent.LOAD_COMPLETE, boxLoaded);
      box.addEventListener(AnimationEvent.ANIMATION_NEXT_FRAME, boxAnimationComplete);
      container.addChild(box);

      //var channel:AnimationCha = new MorphChannel3D(box);

      var btnPlay:Sprite = new Sprite();
      btnPlay.graphics.beginFill(0x990000);
      btnPlay.graphics.drawRect(0, 0, 60, 20);
      btnPlay.graphics.endFill();
      addChild(btnPlay);
      btnPlay.y = 100;
      btnPlay.addEventListener(MouseEvent.CLICK, playMD2);

      var btnNextFrame:Sprite = new Sprite();
      btnNextFrame.graphics.beginFill(0x990000);
      btnNextFrame.graphics.drawRect(0, 0, 60, 20);
      btnNextFrame.graphics.endFill();
      addChild(btnNextFrame);
      btnNextFrame.y = 200;
      btnNextFrame.addEventListener(MouseEvent.CLICK, playNextFrame);

      addEventListener(Event.ENTER_FRAME, render);
     }

     private function boxAnimationComplete(e:AnimationEvent):void 
     {
      trace('box animation complete');
     }

     private function playNextFrame(e:MouseEvent):void 
     {
      if (frameCount < _numFrames) frameCount++;
      else frameCount = frameCount;
      gotoFrame(frameCount);
     }

     private function boxLoaded(e:FileLoadEvent):void 
     {
      _channels = box.getAnimationChannels();

           trace("channel count", _channels.length);
           rewind();

           _numFrames = 0;
           _currentFrame = 1;

           for each(var channel:AbstractChannel3D in _channels)
           {
               _numFrames = Math.max(_numFrames, channel.keyFrames.length);
               channel.updateToFrame(_currentFrame);
           }
     }

     public function rewind():void
       {
           trace("channels length: " + box.getAnimationChannels().length);
           for each(var channel:AbstractChannel3D in _channels)
           {
               channel.updateToFrame(1);
           }
       }

       public function gotoFrame(frame:Number):void
       {
           for each(var channel:AbstractChannel3D in _channels)
               channel.updateToFrame(frame);
       }


     private function playMD2(e:MouseEvent):void 
     {
      isPlaying = !isPlaying;
      isPlaying ? box.stop() : box.play();
     }

     private function render(e:Event):void 
     {
      viewport.camera.orbit(mouseY * .5, mouseX * .5);
      viewport.singleRender();
     }

    }

}

This was created a few months ago, so it might be worth while checking for updates. In my test, things are done a bit manually. I remember seeing some nice demos on the ascollada website, but never had the chance to see how they implemented md2 animation.

Hope this helps.

George Profenza