2009年6月17日 星期三

Tutorial 03: 使用Camera3D

重點API
sandy.core.scenegraph.Camera3D
sandy.core.data.Point3D
sandy.primitive.Line3D
sandy.primitive.Torus
  1. package
  2. {
  3. import flash.display.InteractiveObject;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.events.KeyboardEvent;
  7. import flash.ui.Keyboard;
  8. import sandy.core.data.Point3D;
  9. import sandy.core.Scene3D;
  10. import sandy.core.scenegraph.Camera3D;
  11. import sandy.core.scenegraph.Group;
  12. import sandy.materials.Appearance;
  13. import sandy.materials.attributes.LightAttributes;
  14. import sandy.materials.attributes.LineAttributes;
  15. import sandy.materials.attributes.MaterialAttributes;
  16. import sandy.materials.ColorMaterial;
  17. import sandy.materials.Material;
  18. import sandy.primitive.Line3D;
  19. import sandy.primitive.Torus;
  20. public class Example003 extends Sprite
  21. {
  22. private var scene:Scene3D;
  23. private var camera:Camera3D;
  24. public function Example003()
  25. {
  26. camera = new Camera3D(300, 300);
  27. //camera.x = 100;
  28. //camera.y = 100;
  29. camera.z = -400;
  30. // 使得 camera 物件朝某點拍攝
  31. //camera.lookAt(0, 0, 0);
  32. var root:Group = createScene();
  33. scene = new Scene3D("scene", this, camera, root);
  34. addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  35. // 為整個舞台添加鍵盤的 Key Down 事件聆聽器
  36. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  37. }
  38. private function createScene():Group
  39. {
  40. var g:Group = new Group();
  41. // 產生 3DLine 物件, 並設定其參數,分別為:
  42. // 識別名稱,及 Point3D 物件以定議線條的折點
  43. var myXLine:Line3D = new Line3D("x-coord", new Point3D( -50, 0, 0),
  44. new Point3D(50, 0, 0));
  45. var myYLine:Line3D = new Line3D("y-coord", new Point3D(0, -50, 0),
  46. new Point3D(0, 50, 0));
  47. var myZLine:Line3D = new Line3D("z-coord", new Point3D(0, 0, -50),
  48. new Point3D(0, 0, 50));
  49. // 產生 Torus 物件,並設定其參數,分別為:
  50. // 識別名稱、大半徑及小半徑
  51. var torus:Torus = new Torus("theTorus", 120, 20);
  52. var materialAttr:MaterialAttributes = new MaterialAttributes(
  53. new LineAttributes(0.5, 0x2111BB, 0.4),
  54. new LightAttributes(true, 0.1)
  55. );
  56. var material:Material = new ColorMaterial(0xFFCC33, 1, materialAttr);
  57. material.lightingEnable = true;
  58. var app:Appearance = new Appearance(material);
  59. // 將外觀物件套用到顯示物件 tours
  60. torus.appearance = app;
  61. torus.rotateX = 30;
  62. torus.rotateY = 30;
  63. // 將顯示物件加入舞台顯示群組中
  64. g.addChild(myXLine);
  65. g.addChild(myYLine);
  66. g.addChild(myZLine);
  67. g.addChild(torus);
  68. return g;
  69. }
  70. private function enterFrameHandler(evt:Event):void
  71. {
  72. scene.render();
  73. }
  74. private function keyPressed(evt:KeyboardEvent):void
  75. {
  76. // 依據使用者所按下的鍵,做不用的處理
  77. switch(evt.keyCode) {
  78. case Keyboard.UP:
  79. // 調整 camera 傾斜的幅度
  80. camera.tilt += 2;
  81. break;
  82. case Keyboard.DOWN:
  83. camera.tilt -= 2;
  84. break;
  85. case Keyboard.RIGHT:
  86. // 調整 camera 沿Y軸轉向的幅度
  87. camera.pan -= 2;
  88. break;
  89. case Keyboard.LEFT:
  90. camera.pan += 2;
  91. break;
  92. case Keyboard.CONTROL:
  93. // 調整 camera 沿Z軸卷動的幅度
  94. camera.roll += 2;
  95. break;
  96. case Keyboard.PAGE_DOWN:
  97. // 調整 camera 鏡頭的遠近幅度
  98. camera.z -= 5;
  99. break;
  100. case Keyboard.PAGE_UP:
  101. camera.z += 5;
  102. break;
  103. }
  104. }
  105. }
  106. }

產出:



程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut03

沒有留言:

張貼留言