Papervision3Dメモ #6
2007.04.02
Papervision3Dのblogで紹介されてるBouncingCubesを改造しつつ、複数のオブジェクトを扱う勉強。
BouncingCubes.swf(要:FlashPlayer9)
画面クリックでキューブが跳ねる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | package { import flash.display.*; import flash.events.*; import org.papervision3d.scenes.*; import org.papervision3d.objects.*; import org.papervision3d.cameras.*; import org.papervision3d.materials.*; [SWF(backgroundColor=0xffffff)] public class BouncingCubes extends Sprite { //___________________________________________________________________ // 3D vars private var container :Sprite; private var scene :Scene3D; private var camera :Camera3D; private var rootNode :DisplayObject3D; static public var MAXX:int = 10; static public var MAXY:int = 10; static public var SIZE:Number = 90; static public var SIZE2:Number = 100; private var vbox:int = 0; //___________________________________________________________________ // main public function BouncingCubes() { stage.frameRate = 60; stage.quality = "MEDIUM"; stage.scaleMode = "noScale"; stage.align = StageAlign.TOP_LEFT; this.addEventListener( Event.ENTER_FRAME, loop3D ); this.stage.addEventListener(Event.RESIZE, onStageResize); this.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); init3D(); } //____________________________________________________________________ // init3D private function init3D():void { //コンテナ生成 this.container = new Sprite(); addChild( this.container ); this.container.x = this.stage.stageWidth / 2; this.container.y = this.stage.stageHeight / 2; //シーン生成 this.scene = new Scene3D( this.container ); //カメラ設定 camera = new Camera3D(); camera.x = 3000; camera.z = -300; camera.y = 3000; camera.zoom = 3; camera.focus = 500; //rootNode生成 rootNode = scene.addChild( new DisplayObject3D( "rootNode" ) ); //Cubeオブジェクト生成 for(var x:int = 0; x < MAXX; x++) { for(var y:int = 0; y < MAXY; y++) { var boxcolor:ColorMaterial = new ColorMaterial(Math.floor(Math.random() * 0xFFFFFF),0.8); var materials:MaterialsList = new MaterialsList( { front: boxcolor, back: boxcolor, right: boxcolor, left: boxcolor, top: boxcolor, bottom: boxcolor }); var box0:Cube = new Cube(materials, SIZE,SIZE,SIZE); var box:DisplayObject3D = rootNode.addChild(box0, "box" + vbox); box.x = x * SIZE2 - 500; box.z = y * SIZE2 - 600; box.y = 0; box.extra = {vy:0.0, y:Math.floor(Math.random()*200*5) - 300}; vbox += 1; } } } //____________________________________________________________________ // loop3D private function loop3D( event :Event ):void { //マウス座標でカメラ移動 camera.z += ((-300 + scene.container.mouseX * 10) - camera.z) / 10; camera.y += ((Math.max( 0, this.mouseY ) * 10) - camera.y) / 10 ; var box:DisplayObject3D; for (var y:int = 0; y < vbox; y++) { box = rootNode.getChildByName("box" + y); box.extra.y += box.extra.vy; if (box.extra.y <= -300.0) { box.extra.y = -300.0; box.extra.vy = (box.extra.vy) * -0.9; } box.extra.vy -= 15.0; box.y = box.extra.y; switch (y % 3) { case 0: box.rotationX = (box.y + 300) >> 3; break; case 1: box.rotationY = (box.y + 300) >> 3; break; case 2: box.rotationZ = (box.y + 300) >> 3; break; } } //レンダリング this.scene.renderCamera( camera ); } // ___________________________________________________________________ // onStageResize private function onStageResize(event:Event):void { this.container.x = this.stage.stageWidth / 2; this.container.y = this.stage.stageHeight / 2; } //____________________________________________________________________ // mouseDownHandler public function mouseDownHandler(event:MouseEvent):void { for(var y:int = 0; y < vbox; y++) { var box:DisplayObject3D = rootNode.getChildByName("box"+y); box.extra.vy = Math.random()*250 - 100; } } } } |
さらに応用して、こんなものを作ってみた。「L」「M」「K」キーで絵が変化。
追記:上記をPV3D2.0に対応させたソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | package { import flash.display.*; import flash.events.*; import org.papervision3d.cameras.Camera3D; import org.papervision3d.render.BasicRenderEngine; import org.papervision3d.scenes.Scene3D; import org.papervision3d.view.Viewport3D; import org.papervision3d.objects.DisplayObject3D; import org.papervision3d.objects.primitives.Cube; import org.papervision3d.materials.ColorMaterial; import org.papervision3d.materials.utils.MaterialsList; [SWF(backgroundColor=0xffffff)] public class BouncingCubes extends Sprite { //___________________________________________________________________ // 3D vars private var viewport : Viewport3D; private var scene : Scene3D; private var camera : Camera3D; private var renderer : BasicRenderEngine; private var rootNode : DisplayObject3D; static public var MAXX:int = 10; static public var MAXY:int = 10; static public var SIZE:Number = 90; static public var SIZE2:Number = 100; private var boxArray:Array = []; //___________________________________________________________________ // main public function BouncingCubes() { stage.frameRate = 60; stage.quality = "BEST"; stage.scaleMode = "noScale"; stage.align = StageAlign.TOP_LEFT; addEventListener( Event.ENTER_FRAME, loop3D ); stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); init3D(); } //____________________________________________________________________ // init3D private function init3D():void { //ビューポート定義 viewport = new Viewport3D(0,0,true); viewport.opaqueBackground = 0xFFFFFF; addChild(viewport); //レンダラ定義 renderer = new BasicRenderEngine(); //シーン定義 scene = new Scene3D(); //カメラ設定 camera = new Camera3D(); camera.x = -600; camera.y = 300; camera.z = -300; camera.fov = 30; camera.target = DisplayObject3D.ZERO; //Cubeオブジェクト生成 for(var x:int = 0; x < MAXX; x++) { for(var y:int = 0; y < MAXY; y++) { var boxcolor:ColorMaterial = new ColorMaterial(Math.floor(Math.random() * 0xFFFFFF),0.8); var materials:MaterialsList = new MaterialsList( { all: boxcolor }); var box:Cube = new Cube(materials, SIZE,SIZE,SIZE); scene.addChild(box); box.x = x * SIZE2 - 500; box.z = y * SIZE2 - 600; box.y = 0; box.extra = {vy:0.0, y:Math.floor(Math.random()*200*5) - 300}; boxArray.push(box); } } } //____________________________________________________________________ // loop3D private function loop3D( event :Event ):void { //マウス座標でカメラ移動 camera.z += ((-2000 + (stage.mouseX - (stage.stageWidth * 0.5))*5) - camera.z ) / 10; camera.y += (Math.max( 0, (stage.mouseY - (stage.stageHeight * 0.5))*5) - camera.y) / 10; var count:int = 0; for each (var item:DisplayObject3D in boxArray) { count++; item.extra.y += item.extra.vy; if (item.extra.y <= -300.0) { item.extra.y = -300.0; item.extra.vy = (item.extra.vy) * -0.9; } item.extra.vy -= 15.0; item.y = item.extra.y; switch (count % 3) { case 0: item.rotationX = (item.y + 300) >> 3; break; case 1: item.rotationY = (item.y + 300) >> 3; break; case 2: item.rotationZ = (item.y + 300) >> 3; break; } } //レンダリング renderer.renderScene(scene,camera,viewport); } //____________________________________________________________________ // mouseDownHandler public function mouseDownHandler(event:MouseEvent):void { for each (var item:DisplayObject3D in boxArray) { item.extra.vy = Math.random()*250 - 100; } } } } |
関連する投稿
Trackback URL : http://blog.r3c7.net/wp-trackback.php?p=63
[...] 2.0てわけじゃないけど、これなんかすごい!こ、このキャラは!w(ソースした、こんなもの、のリンク) いやー、面白い。 [...]
[...] 2.0てわけじゃないけど、これなんかすごい!こ、このキャラは!w(ソースした、こんなもの、のリンク) いやー、面白い。 [...]