Archive for August 30th, 2007

Papervision 3D + Line3D + Tweener - Part 1

Here's my initial experiment using the new Line3D class addition to papervision from Andy Zupko. My example is in its early stages, needs some fade and remove functionality, possibly use of a Null 3D object for the lines to draw from,. Becuase of this it has obvious performance issues after it draw for a bit. Hence the "Part 1″ ;)

Update - I decided to remove the example as it bogs down the page if it's running for a while. It's in dire need of incremental removal and garbage collection.

I don't think the Line3D object has been merged into the regular updates to pv3d, but you can get the class in the source files below and from Andy's site as well.

Back Story: I came up with an AS3 version of a 2D version of this inspired by Felix Turner and his Bezier Tween AS2 Class. I'll post the code for that soon since I seem to be in the spirit of doing everything backwards these days.

Here's the code:

package com.hydrotik {

        import flash.events.Event;
        import flash.display.MovieClip;
        import flash.display.Stage;
        import flash.display.Sprite;
        import caurina.transitions.Tweener;
        import com.ascb.util.NumberUtilities;
        import org.papervision3d.core.proto.*;
        import org.papervision3d.core.geom.*;
        import org.papervision3d.scenes.*;
        import org.papervision3d.cameras.*;
        import org.papervision3d.objects.*;
        import org.papervision3d.materials.*;

        public class Hydro3D extends Sprite {
               
                private const STAGE_WIDTH              :Number = 500;
                private const STAGE_HEIGHT            :Number = 300;
                private const ANIMATION_TIME    :Number = 8;
                private const ANIMATION_TYPE    :String = "easeoutquad";
                private var controlPoints              :Array;
                private var itemArray         :Array;
                private var trailArray      :Array;
                private var currTrailArray            :Array;
                private var lineArray         :Array;
                private var count                            :int = 0;
                private var container       :Sprite;
                private var scene           :MovieScene3D;
                private var camera          :Camera3D;

                public function Hydro3D():void {

                        controlPoints = [];
                        itemArray = [];
                        trailArray = [];
                        currTrailArray = [];
                        lineArray = [];
                       
                       
                        //container
                        container = new Sprite();
                        addChild( container );
                        container.x = STAGE_WIDTH/2;
                        container.y = STAGE_HEIGHT/2;
                       
                        //scene
                        scene = new MovieScene3D( container );
                       
                        //camera
                        camera = new Camera3D();
                        camera.z = -200;
                       
                        //Dot color
                        var color:ColorMaterial = new ColorMaterial(0×666666, .5);

                        for (var i:uint = 0; i < 2; i++) {
                               
                                trailArray[i] = new Sphere(color, 1, 2, 2);
                                scene.addChild(trailArray[i]);
                               
                                itemArray[i] = new Sphere(color, 5, 5, 10);
                                scene.addChild(itemArray[i]);
                               
                                lineArray[i] = new Line3D([new Vertex3D(0, 0, 0), new Vertex3D(0, 0, 0)], 0×333333, 1, 1);
                                scene.addChild(lineArray[i]);
                        }
                       
                        addEventListener( Event.ENTER_FRAME, onEnterFrame );
                       
                        moveIt();
                }
               
                private function moveIt():void {
                        var deltaX:Number = NumberUtilities.random(-800, 800);
                        var deltaY:Number = NumberUtilities.random(-800, 800);
                        var deltaZ:Number = NumberUtilities.random(-800, 800);
                       
                        for (var i:uint = 0; i < itemArray.length; i++) {

                                var trailList:Array = [];
                                for (var q:int = 0; q < 10; q++) {
                                        trailList.push({
                                                x:NumberUtilities.random(0, STAGE_WIDTH),
                                                y:NumberUtilities.random(0, STAGE_HEIGHT),
                                                z:NumberUtilities.random(-200, 200)
                                        });
                                }
                                Tweener.addTween(trailArray[i], {
                                        x:deltaX,
                                        y:deltaY,
                                        z:deltaZ,
                                        _bezier:trailList,
                                        time:ANIMATION_TIME,
                                        transition:ANIMATION_TYPE
                                });
                               
                               
                                var bezierList:Array = [];
                                for (var v:int = 0; v < 10; v++) {
                                        bezierList.push({
                                                x:NumberUtilities.random(0, STAGE_WIDTH),
                                                y:NumberUtilities.random(0, STAGE_HEIGHT),
                                                z:NumberUtilities.random(-200, 200)
                                        });
                                }
                       
                                if(i == itemArray.length - 1){
                                        Tweener.addTween(itemArray[i], {
                                                x:deltaX,
                                                y:deltaY,
                                                z:deltaZ,
                                                _bezier:bezierList,
                                                time:ANIMATION_TIME,
                                                transition:ANIMATION_TYPE,
                                                onComplete: moveIt
                                        });
                                }else{
                                        Tweener.addTween(itemArray[i], {
                                                x:deltaX,
                                                y:deltaY,
                                                z:deltaZ,
                                                _bezier:bezierList,
                                                time:ANIMATION_TIME,
                                                transition:ANIMATION_TYPE
                                        });
                                }
                               
                        }
                       
                }
               
                private function onEnterFrame( event: Event ):void {
                        camera.x +=((container.mouseX*10) - camera.x) * 0.005;
                        camera.y +=((container.mouseY*10) - camera.y) * 0.005;
       
       
                        for (var i:int = 0; i < itemArray.length; i++) {
                                lineArray[i].addVertex(new Vertex3D(trailArray[i].x, trailArray[i].y, trailArray[i].z));
                        }
                       
                        scene.renderCamera( camera );
                };

        }
}

I'm almost positive there is a smoother and more efficient way to do this mathematically but I'm no math whiz. So in the meantime…


Lines3D Source

Thursday, August 30th, 2007