Paper3DWorld
http://milkmidi.com
http://milkmidi.blogspot.com
/*
http://milkmidi.com
http://milkmidi.blogspot.com
*/
package{
import flash.display.*;
import flash.events.*;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.materials.*;
import org.papervision3d.events.InteractiveScene3DEvent;
import org.papervision3d.view.BasicView;
import caurina.transitions.Tweener;
public class Paper3Dworld extends Sprite{
private var view :BasicView;
private var itemOfNumber:int = 20;//物件數量
private var radius :int = 1000; //範圍半徑
public function Paper3Dworld():void{
init3DEngine();
init3DObject();
}
private function init3DEngine():void{
view = new BasicView(0, 0, true, true, "Free");
view.buttonMode = true;
this.addChild(view);
this.addEventListener(Event.ENTER_FRAME, onEventRender3D);
}
private function init3DObject():void {
for (var i:int = 0 ; i < itemOfNumber; i++) {
//var bmpMat:BitmapFileMaterial = new BitmapFileMaterial("images/" + i + ".jpg", true);
var bmpMat:ColorMaterial = new ColorMaterial(0xffffff * Math.random());
bmpMat.doubleSided = true; //雙面模式
bmpMat.interactive = true; //互動模式
var plane:Plane = new Plane(bmpMat, 200, 150);
//建構Plane物件,預設的座標為0,0,0
//再使用Tweener,移動到指定的位置。
Tweener.addTween(plane,
{
x : Math.random() * radius - radius / 2,
y : Math.random() * radius - radius / 2,
z : Math.random() * radius - radius / 2,
/*在這兒我們希望x的範圍是 -500到500的亂數值
* Math.random()會回傳0到1的亂數浮點數,乘上radius變數,
* 即可得到0到1000的亂數,再減去500,即可達到我們要的範圍。
**/
rotationX : Math.random() * 360,
rotationY : Math.random() * 360,
rotationZ : Math.random() * 360,
time : 3,
transition : "easeOutExpo",
delay : i * 0.02
} );
plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onInteractivePress);
view.scene.addChild(plane);
}
}
private function onInteractivePress(e:InteractiveScene3DEvent):void {
//透過e.displayObject3D屬性,可以得到擴播者物件。
var _emptyObj3D:DisplayObject3D = new DisplayObject3D();
//宣告一個空的DisplayObject3D物件
_emptyObj3D.copyTransform( e.displayObject3D );
//copy擴播者物件的6個屬性,分別為:x,y,z,rotationX,rotationY,rotationZ;
_emptyObj3D.moveBackward( 300 );
//讓空的物件,往後移動300;
Tweener.addTween( view.camera,
{
x :_emptyObj3D.x,
y :_emptyObj3D.y,
z :_emptyObj3D.z,
rotationX :_emptyObj3D.rotationX,
rotationY :_emptyObj3D.rotationY,
rotationZ :_emptyObj3D.rotationZ,
time :3,
transition :"easeOutExpo"
} );
}
private function onEventRender3D(e:Event):void{
view.singleRender();
}
}
}