原文地址 http://bvu.javaeye.com/blog/393116
有了这个,就可以做三维物体的浏览了。
1
package
2 {
3 import flash.events.MouseEvent;
4
5 import org.papervision3d.lights.PointLight3D;
6 import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
7 import org.papervision3d.objects.DisplayObject3D;
8 import org.papervision3d.objects.primitives.Sphere;
9 import org.papervision3d.view.BasicView;
10
11 [SWF(width = " 640 " , height = " 480 " , backgroundColor = " #000000 " , frameRate = " 60 " )]
12 public class OrbitingCameraExample extends BasicView
13 {
14 private var isOribiting:Boolean;
15 private var cameraPitch:Number = 90 ;
16 private var cameraYaw:Number = 270 ;
17 private var cameraTarget:DisplayObject3D = DisplayObject3D.ZERO;
18 private var previousMouseX:Number;
19 private var previousMouseY:Number;
20
21 private var light:PointLight3D;
22
23 public function OrbitingCameraExample()
24 {
25 light = new PointLight3D();
26 var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000 );
27
28 var sphere1:Sphere = new Sphere(material, 300 , 10 , 10 );
29 var sphere2:Sphere = new Sphere(material, 100 , 10 , 10 );
30 sphere2.x = 300 ;
31 sphere2.y = 300 ;
32 sphere2.z = 700
33 var sphere3:Sphere = new Sphere(material, 100 , 10 , 10 );
34 sphere3.x = 600 ;
35 sphere3.y = - 400 ;
36 sphere3.z = - 200 ;
37 var sphere4:Sphere = new Sphere(material, 100 , 10 , 10 );
38 sphere4.x = - 700 ;
39 sphere3.z = - 100 ;
40
41 scene.addChild(sphere1);
42 scene.addChild(sphere2);
43 scene.addChild(sphere3);
44 scene.addChild(sphere4);
45
46 stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
47 stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
48 stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
49
50 startRendering();
51 }
52
53 private function onMouseDown(event:MouseEvent): void
54 {
55 isOribiting = true ;
56 previousMouseX = event.stageX;
57 previousMouseY = event.stageY;
58 }
59
60 private function onMouseUp(event:MouseEvent): void
61 {
62 isOribiting = false ;
63 }
64
65 private function onMouseMove(event:MouseEvent): void
66 {
67 var differenceX:Number = event.stageX - previousMouseX;
68 var differenceY:Number = event.stageY - previousMouseY;
69
70 if (isOribiting)
71 {
72 cameraPitch += differenceY;
73 cameraYaw += differenceX;
74
75 cameraPitch %= 360 ;
76 cameraYaw %= 360 ;
77
78 cameraPitch = cameraPitch > 0 ? cameraPitch : 0.0001 ;
79 cameraPitch = cameraPitch < 90 ? cameraPitch : 89.9999 ;
80
81 previousMouseX = event.stageX;
82 previousMouseY = event.stageY;
83
84 camera.orbit(cameraPitch, cameraYaw, true , cameraTarget);
85 }
86 }
87 }
88 }
2 {
3 import flash.events.MouseEvent;
4
5 import org.papervision3d.lights.PointLight3D;
6 import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
7 import org.papervision3d.objects.DisplayObject3D;
8 import org.papervision3d.objects.primitives.Sphere;
9 import org.papervision3d.view.BasicView;
10
11 [SWF(width = " 640 " , height = " 480 " , backgroundColor = " #000000 " , frameRate = " 60 " )]
12 public class OrbitingCameraExample extends BasicView
13 {
14 private var isOribiting:Boolean;
15 private var cameraPitch:Number = 90 ;
16 private var cameraYaw:Number = 270 ;
17 private var cameraTarget:DisplayObject3D = DisplayObject3D.ZERO;
18 private var previousMouseX:Number;
19 private var previousMouseY:Number;
20
21 private var light:PointLight3D;
22
23 public function OrbitingCameraExample()
24 {
25 light = new PointLight3D();
26 var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000 );
27
28 var sphere1:Sphere = new Sphere(material, 300 , 10 , 10 );
29 var sphere2:Sphere = new Sphere(material, 100 , 10 , 10 );
30 sphere2.x = 300 ;
31 sphere2.y = 300 ;
32 sphere2.z = 700
33 var sphere3:Sphere = new Sphere(material, 100 , 10 , 10 );
34 sphere3.x = 600 ;
35 sphere3.y = - 400 ;
36 sphere3.z = - 200 ;
37 var sphere4:Sphere = new Sphere(material, 100 , 10 , 10 );
38 sphere4.x = - 700 ;
39 sphere3.z = - 100 ;
40
41 scene.addChild(sphere1);
42 scene.addChild(sphere2);
43 scene.addChild(sphere3);
44 scene.addChild(sphere4);
45
46 stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
47 stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
48 stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
49
50 startRendering();
51 }
52
53 private function onMouseDown(event:MouseEvent): void
54 {
55 isOribiting = true ;
56 previousMouseX = event.stageX;
57 previousMouseY = event.stageY;
58 }
59
60 private function onMouseUp(event:MouseEvent): void
61 {
62 isOribiting = false ;
63 }
64
65 private function onMouseMove(event:MouseEvent): void
66 {
67 var differenceX:Number = event.stageX - previousMouseX;
68 var differenceY:Number = event.stageY - previousMouseY;
69
70 if (isOribiting)
71 {
72 cameraPitch += differenceY;
73 cameraYaw += differenceX;
74
75 cameraPitch %= 360 ;
76 cameraYaw %= 360 ;
77
78 cameraPitch = cameraPitch > 0 ? cameraPitch : 0.0001 ;
79 cameraPitch = cameraPitch < 90 ? cameraPitch : 89.9999 ;
80
81 previousMouseX = event.stageX;
82 previousMouseY = event.stageY;
83
84 camera.orbit(cameraPitch, cameraYaw, true , cameraTarget);
85 }
86 }
87 }
88 }