2009年7月22日 星期三

Tutorial 04a - 燈光的使用

重點API:
sandy.primitive.Box;
sandy.primitive.Cylinder;
sandy.primitive.PrimitiveMode;
sandy.primitive.Sphere;


  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.KeyboardEvent;
  6. import flash.ui.Keyboard;
  7. import sandy.core.Scene3D;
  8. import sandy.core.scenegraph.Camera3D;
  9. import sandy.core.scenegraph.Group;
  10. import sandy.core.scenegraph.TransformGroup;
  11. import sandy.materials.Appearance;
  12. import sandy.materials.attributes.CylinderEnvMap;
  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.Box;
  19. import sandy.primitive.Cylinder;
  20. import sandy.primitive.PrimitiveMode;
  21. import sandy.primitive.Sphere;
  22. public class Example004a extends Sprite
  23. {
  24. private var scene:Scene3D;
  25. private var camera:Camera3D;
  26. private var tg:TransformGroup;
  27. // 設定燈光 x, y, z 軸參數
  28. private var lightX:Number = 0;
  29. private var lightY:Number = 0;
  30. private var lightZ:Number = 10;
  31. public function Example004a()
  32. {
  33. camera = new Camera3D(300, 300);
  34. camera.z = -400;
  35. var root:Group = createScene();
  36. scene = new Scene3D("scene", this, camera, root);
  37. // 設定場景燈光的方向位置
  38. scene.light.setDirection(lightX, lightY, lightZ);
  39. addEventListener(Event.ENTER_FRAME,
  40. enterFrameHandler);
  41. stage.addEventListener(KeyboardEvent.KEY_DOWN,
  42. keyPressed);
  43. }
  44. private function createScene():Group
  45. {
  46. var g:Group = new Group();
  47. tg = new TransformGroup();
  48. var materialAttr:MaterialAttributes =
  49. new MaterialAttributes(
  50. new LineAttributes(0, 0x2111BB, 0),
  51. new LightAttributes(true, 0.1)
  52. );
  53. var material:Material = new ColorMaterial(
  54. 0xFFCC33, 1, materialAttr
  55. );
  56. material.lightingEnable = true;
  57. var app:Appearance = new Appearance(material);
  58. var materialAttr2:MaterialAttributes =
  59. new MaterialAttributes(
  60. new LineAttributes(0, 0x2111BB, 0),
  61. new LightAttributes(true, 0.1)
  62. );
  63. var material2:Material = new ColorMaterial(
  64. 0xCC0000, 1, materialAttr
  65. );
  66. material2.lightingEnable = true;
  67. var app2:Appearance = new Appearance(material2);
  68. var materialAttr3:MaterialAttributes =
  69. new MaterialAttributes(
  70. new LineAttributes(0, 0x2111BB, 0),
  71. new LightAttributes(true, 0.1)
  72. );
  73. var material3:Material = new ColorMaterial(
  74. 0x008AE6, 1, materialAttr
  75. );
  76. material3.lightingEnable = true;
  77. var app3:Appearance = new Appearance(material3);
  78. // 產生一 Box 物件,並設定其參數,分別為:
  79. // 識別名稱、寬、高、深度、face generation 及 Quility
  80. var table:Box = new Box(
  81. "table", 10, 150, 200, PrimitiveMode.QUAD, 1
  82. );
  83. //table.enableBackFaceCulling = false;
  84. // 設定是否容許使用者改變呈現此 table 的方式
  85. table.useSingleContainer = false;
  86. // 設定 table 的外觀
  87. table.appearance = app;
  88. // 旋轉 table
  89. table.rotateY = 90;
  90. table.rotateX = 90;
  91. // 產生一 Cylinder 物件,並設定其參數,分別為
  92. // 識別名稱、半徑及高
  93. var leg01:Cylinder = new Cylinder("leg01", 5, 80);
  94. // 設定 leg01 的外觀
  95. leg01.appearance = app;
  96. // 設定 leg01 的 x,y,z 座標
  97. leg01.x = -80;
  98. leg01.y = -45;
  99. leg01.z = 50;
  100. var leg02:Cylinder = new Cylinder("lig02", 5, 80);
  101. leg02.appearance = app;
  102. leg02.x = 80;
  103. leg02.y = -45;
  104. leg02.z = 50;
  105. var leg03:Cylinder = new Cylinder("leg03", 5, 80);
  106. leg03.appearance = app;
  107. leg03.x = -80;
  108. leg03.y = -45;
  109. leg03.z = -50;
  110. var leg04:Cylinder = new Cylinder("leg04", 5, 80);
  111. leg04.appearance = app;
  112. leg04.x = 80;
  113. leg04.y = -45;
  114. leg04.z = -50;
  115. // 產生一 Sphere 物件,並設定其參數,分別為:
  116. // 識別名稱、半徑、水平切割數及垂直切割數
  117. var mySphere:Sphere = new Sphere("theSphere", 20, 20, 8);
  118. mySphere.useSingleContainer = true;
  119. mySphere.appearance = app2;
  120. mySphere.y = 25;
  121. mySphere.x = -30;
  122. var myBox:Box = new Box(
  123. "theBox", 60, 60, 60, PrimitiveMode.TRI, 3
  124. );
  125. myBox.useSingleContainer = true;
  126. myBox.appearance = app3;
  127. myBox.rotateY = 30;
  128. myBox.x = 40;
  129. myBox.y = 35;
  130. tg.addChild(table);
  131. tg.addChild(leg01);
  132. tg.addChild(leg02);
  133. tg.addChild(leg03);
  134. tg.addChild(leg04);
  135. tg.addChild(mySphere);
  136. tg.addChild(myBox);
  137. tg.rotateX = 5;
  138. g.addChild(tg);
  139. return g;
  140. }
  141. private function enterFrameHandler(evt:Event):void
  142. {
  143. // 將場景顯示快取設定為 false
  144. scene.render(false);
  145. }
  146. private function keyPressed(evt:KeyboardEvent):void
  147. {
  148. switch(evt.keyCode)
  149. {
  150. case Keyboard.PAGE_DOWN:
  151. // 減少場景燈光的亮度
  152. scene.light.setPower(scene.light.getPower() - 5);
  153. break;
  154. case Keyboard.PAGE_UP:
  155. // 增加場景燈光的亮度
  156. scene.light.setPower(scene.light.getPower() + 5);
  157. break;
  158. case Keyboard.UP:
  159. lightY += 10;
  160. scene.light.setDirection(lightX, lightY, lightZ);
  161. break;
  162. case Keyboard.DOWN:
  163. lightY -= 10;
  164. scene.light.setDirection(lightX, lightY, lightZ);
  165. break;
  166. case Keyboard.RIGHT:
  167. lightX += 10;
  168. scene.light.setDirection(lightX, lightY, lightZ);
  169. break;
  170. case Keyboard.LEFT:
  171. lightX -= 10;
  172. scene.light.setDirection(lightX, lightY, lightZ);
  173. break;
  174. }
  175. }
  176. }
  177. }


產出:



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

沒有留言:

張貼留言