Hatena::Groupactionscript

dann@actionscript

 | 

2008-08-23

Papervision3D + Tweenerで球場の物体を動かす

23:42

複数の球をCubeの頂点にTweenerで移動させるとサンプルを作ってみました。

Papervision3D Greate Whiteを使っています。

package net.mashup4u {
    import flash.display.*;
    import flash.events.*;
    import net.mashup4u.SphereFactory;
    import org.papervision3d.core.proto.*;
    import org.papervision3d.scenes.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.objects.primitives.*;
    import caurina.transitions.Tweener;
    import net.mashup4u.*;
    import org.osflash.thunderbolt.Logger;

    public class Main extends BasicView {
        private var _rootDisplay: DisplayObject3D;

        public function Flickr3D():void {
            this.setup();
            this.createSpheres();
            this.alignAllSpheresAsCube();
            startRendering();
        }

        private function setup(): void {
            this.setupStage();
            this.setupRootDisplay();
            this.setupListeners();
        }

        private function setupStage(): void {
            stage.quality = "MEDIUM";
        }

        private function setupRootDisplay(): void {
            this._rootDisplay = new DisplayObject3D();  
            scene.addChild(this._rootDisplay);  
        }

        // Refactor: move to a class
        private function createSpheres():void {
            for(var i:int = 0; i < 640; i++ ){
                var sphere:Sphere = SphereFactory.create(i);
                this._rootDisplay.addChild(sphere);
            }
        }

        private function alignAllSpheresAsCube(): void {
            SphereManager.alignAllSpheresAsCube(this._rootDisplay);
        }

        // Refactor: Who is responsible for this method?
        private function moveCameraAtRandom(e:Event): void {
            Tweener.addTween(camera,
                {
                    x:Math.random()*2000,
                    y:Math.random()*2000,
                    z:Math.random()*2000,
                    time:1,
                    transition:"easeOutExpo"
                }
            );
        }

        private function setupListeners():void {
            stage.addEventListener(MouseEvent.CLICK, moveCameraAtRandom);
        }

    }

}
package net.mashup4u {
    import flash.filters.GlowFilter;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.core.proto.*;

    public class SphereFactory {
        public static function create(i:int): Sphere {
            var mm: MaterialObject3D = new ColorMaterial(0xff0000, 0.5);
            var item:Sphere = new Sphere( mm, 7, 4, 4);
            item.name = 'item'+i;
            mm.oneSide = false;
            item.useOwnContainer = true;
            item.filters = [
                new GlowFilter(0xFFFF00, 1, 16, 16, 2, 1, false, false),
            ]; 
            item.alpha = Math.random() + 0.1;
            item.x = Math.random()*3000-1500;
            item.y = Math.random()*3000-1500;
            item.z = Math.random()*3000-1500;
            return item;
        }
    }
}
package net.mashup4u {
    import org.papervision3d.objects.*;
    import caurina.transitions.Tweener;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.utils.*;
    import org.papervision3d.objects.primitives.*;

    public class SphereManager {

        public static function alignAllSpheresAsCube(rootDisplay:DisplayObject3D): void {
            var cube:Cube = new Cube(
                new MaterialsList({all:new ColorMaterial(0xFF0000,100)}),
                800,800,800,10,10,10
            );
            var i:int = 0;
            while(i++ < cube.geometry.vertices.length) {
                var item:Object = rootDisplay.getChildByName('sphere'+i);
                Tweener.addTween(
                    item, {
                        x:cube.geometry.vertices[i-1].x,
                        y:cube.geometry.vertices[i-1].y,
                        z:cube.geometry.vertices[i-1].z,
                        time:4,
                        transition:"easeInOutExpo"
                    }
                );
            }
        }
    }

}

TODO

Papervision3Dが大分重い感じですね。置く物体の量によって動作速度が極端にかわるようなので、どれくらいで現実的に動くのか調べたいと思ってます。

 |