用强大的pv3d做个可交互的立方体

要先允许浏览器播放flash

以前在不理解oo时写的,代码不干净。

package
{
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Back;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	
	/**
	 *
	 */		
	[SWF(backgroundColor = 0)]
	public class JdBox extends BasicView
	{
		private var box:Cube;
		private var plane : Plane;
		private var plane_line_top: Plane;
		private var plane_line_bottom : Plane;
		private var red : ColorMaterial;
		private var line:WireframeMaterial;
		private var redZu:CompositeMaterial;
		private var material:BitmapMaterial;
		
		private var planeLineList:Array=new Array();
		private var planeList : Array = new Array();
		
		public function JdBox()
		{
			super();
			stage.frameRate = 40;
			init();
			startRendering();
		}
		private function init():void
		{
			initBox();	
			setDefaultRotation();
			initPlanes();
			interactiveBox();
			
		
		}
		private function initBox():void
		{
			var rect:Shape = new Shape(); 
			rect.graphics.beginFill(0xFF0000,.5); 
			rect.graphics.lineStyle(2,0x000000,1);
			rect.graphics.drawRect(0,0,300,300);
			rect.graphics.endFill();
			
			var bmp:BitmapData = new BitmapData(300,300,true,0x0); 
			bmp.draw(rect);
			
			 material = new BitmapMaterial(bmp);
			 material.doubleSided = true;
			
//			line = new WireframeMaterial(0x000000,0.3,1); 		
//			
//			red = new ColorMaterial(0xFF0000,.3);
//		
//			redZu = new CompositeMaterial(); 
//			redZu.doubleSided = false;
//			redZu.addMaterial(red);
//			redZu.addMaterial(line);
			
			var materialsList : MaterialsList = new MaterialsList();
			materialsList.addMaterial(material,"front");
//			materialsList.addMaterial(material,"back");
//			materialsList.addMaterial(material,"left");
//			materialsList.addMaterial(material,"right");
//			materialsList.addMaterial(material,"top");
//			materialsList.addMaterial(material,"bottom");
			
			box = new Cube(materialsList,300,300,300);
			scene.addChild(box);
			
		}
		private function initPlanes():void
		{
			//----top----planeList[0]-----planeLineList[0]-----initPlaneLine[,,,,0,,,]
			initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,150,150,0,90,0,0);  
			
			//----bottom----planeList[1]-----planeLineList[1]-----initPlaneLine[,,,,1,,,]
			initSinglePlane( new Plane(material,300,300),0,-150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,-150,-150,1,90,0,0);  
			
			//----left----planeList[2]-----planeLineList[2]-----initPlaneLine[,,,,2,,,]
			initSinglePlane( new Plane(material,300,300),-150,0,0,0,0,90);
			initPlaneLine(new Plane(material,1,300),-150,0,150,2,0,90,0);  
			
			//----right----planeList[3]-----planeLineList[3]-----initPlaneLine[,,,,3,,,]
			initSinglePlane( new Plane(material,300,300),150,0,0,0,0,90);
			initPlaneLine(new Plane(material,1,300),150,0,-150,3,0,90,0);  
			
			
			//----front----planeList[4]-----planeLineList[4]-----initPlaneLine[,,,,4,,,]
			initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,-150,-150,4,0,0,0);   
			
			interactivePlane();
		}
		private function initSinglePlane(tmpPlane : Plane,
											 x : int,y : int,z : int,
											 rotX : int,rotY : int,rotZ : int):void
		{
			tmpPlane.x = x;
			tmpPlane.y = y;
			tmpPlane.z = z;
			tmpPlane.localRotationX = rotX;
			tmpPlane.localRotationY = rotY;
			tmpPlane.localRotationZ = rotZ;
			planeList.push(tmpPlane);
		}
		private function initPlaneLine(planeLine : Plane , 
									     x : int , y : int , z : int,
										 planeindex:int,
										 rotX : int,rotY : int,rotZ : int):void
		{
			planeLine.x += x;
			planeLine.y += y;
			planeLine.z += z;
			planeLine.localRotationX = rotX;
			planeLine.localRotationY = rotY;
			planeLine.localRotationZ = rotZ;
			planeLine.addChild(planeList[planeindex]);
			planeLineList.push(planeLine);
			box.addChild(planeLine);
			
		}
		private function setDefaultRotation():void
		{
			box.localRotationX = 15;
			box.localRotationY = -30;
			box.localRotationZ = 0;
		}
		private function interactiveBox():void
		{
			viewport.interactive = true;
//			redZu.interactive = true;
			material.interactive = true;
			box.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,onOverHandler);
			box.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,onOutHandler);
			box.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClickHandler);

		}
		private function interactivePlane():void
		{
			for(var i:int=0; i < planeList.length;i++){
			planeList[i].addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,planeClickHandler);
			}
		}
		/**
		 *遍历每个plane,点击后最终目标是先旋转盒子,盒子到达指定位置后再旋转当前的plane
		 */
		private function planeClickHandler(event : InteractiveScene3DEvent):void
		{
			rotateBox(event.target as Plane);
			rotateCrtPlane(event.target as Plane);	
			
		}
		private function rotateBox(tmpPlane : Plane):void
		{
			var tmpIndex:int=planeList.indexOf(tmpPlane,0);
			if(tmpIndex == 3){
				TweenLite.to(box,.2,{localRotationY:box.localRotationY-90,
									 onComplete:onFinishTween1});
				
//				TweenLite.to(clip_mc, 5, {alpha:0.5, x:120, ease:Back.easeOut, delay:2, 
//					onComplete: onFinishTween, onCompleteParams:[5, clip_mc]});
			}
		}
		private function onFinishTween1():void
		{
			TweenLite.to(box,.2,{localRotationZ:box.localRotationZ-90,
								onComplete:onFinishTween2});
		}
		private function onFinishTween2():void
		{
			trace("finish 2");
			TweenLite.to(box,.2,{localRotationX:box.localRotationX-90});
		}
		private function rotateCrtPlane(tmpPlane : Plane):void
		{
			var tmpIndex:int=planeList.indexOf(tmpPlane,0);
			var tmpPlaneLine : Plane = planeLineList[tmpIndex];
			switch(tmpIndex){
				case 0:   //top	 ,the same to bottom					
				case 1:  //bottom
					switch(tmpPlaneLine.localRotationX){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
							break;
					}
					break;
				case 2: //left
				case 3: //right
					switch(tmpPlaneLine.localRotationY){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationY:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationY:0,ease:Back.easeOut});
							break;
					}
					break;
				case 4:  //front
					switch(tmpPlaneLine.localRotationX){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
							break;
					}
					break;
			}
		}
		private function onOverHandler(event : InteractiveScene3DEvent):void
		{
			trace("ok");
			viewport.buttonMode = true;
		//	box.materials.getMaterialByName("back").fillAlpha = .8;
			
			
		}
		private function onOutHandler(event : InteractiveScene3DEvent):void
		{
			trace("no");
			viewport.buttonMode = false;
		//	box.materials.getMaterialByName("back").fillAlpha = .3;
			
		}
		/**
		 * 点击盒子后旋转,这个函数到了后期全部换成plane了就不用了
		 */
		private function onClickHandler(event : InteractiveScene3DEvent):void
		{
			randomRotateBox();		
//			rotationBox();
			
		}
		private function randomRotateBox():void
		{
			var tmpNum : Number = Math.random();
			if(tmpNum > .6){
				TweenLite.to(box,2,{localRotationX:box.localRotationX+50,ease:Back.easeOut});
			}else if(tmpNum < .3){
				TweenLite.to(box,2,{localRotationY:box.localRotationY+50,ease:Back.easeOut});
			}else{
				TweenLite.to(box,2,{localRotationZ:box.localRotationZ+50,ease:Back.easeOut});
			}
		}
		private function rotationBox():void
		{
			TweenLite.to(box,2,{rotationX :box.rotationX +50,ease:Back.easeOut});
//			box.yaw(30);
		}
//		override protected function onRenderTick(e:Event=null):void
//		{
//			//box.localRotationX++;
//			//box.localRotationY++;
//			//box.localRotationZ++;
//			super.onRenderTick();
//		}
		
	}
}

转载于:https://www.cnblogs.com/JD85/archive/2010/05/09/1730923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值