sandy.primitive.Box;
sandy.primitive.Cylinder;
sandy.primitive.PrimitiveMode;
sandy.primitive.Sphere;
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import sandy.core.Scene3D;
- import sandy.core.scenegraph.Camera3D;
- import sandy.core.scenegraph.Group;
- import sandy.core.scenegraph.TransformGroup;
- import sandy.materials.Appearance;
- import sandy.materials.attributes.CylinderEnvMap;
- import sandy.materials.attributes.LightAttributes;
- import sandy.materials.attributes.LineAttributes;
- import sandy.materials.attributes.MaterialAttributes;
- import sandy.materials.ColorMaterial;
- import sandy.materials.Material;
- import sandy.primitive.Box;
- import sandy.primitive.Cylinder;
- import sandy.primitive.PrimitiveMode;
- import sandy.primitive.Sphere;
- public class Example004a extends Sprite
- {
- private var scene:Scene3D;
- private var camera:Camera3D;
- private var tg:TransformGroup;
- // 設定燈光 x, y, z 軸參數
- private var lightX:Number = 0;
- private var lightY:Number = 0;
- private var lightZ:Number = 10;
- public function Example004a()
- {
- camera = new Camera3D(300, 300);
- camera.z = -400;
- var root:Group = createScene();
- scene = new Scene3D("scene", this, camera, root);
- // 設定場景燈光的方向位置
- scene.light.setDirection(lightX, lightY, lightZ);
- addEventListener(Event.ENTER_FRAME,
- enterFrameHandler);
- stage.addEventListener(KeyboardEvent.KEY_DOWN,
- keyPressed);
- }
- private function createScene():Group
- {
- var g:Group = new Group();
- tg = new TransformGroup();
- var materialAttr:MaterialAttributes =
- new MaterialAttributes(
- new LineAttributes(0, 0x2111BB, 0),
- new LightAttributes(true, 0.1)
- );
- var material:Material = new ColorMaterial(
- 0xFFCC33, 1, materialAttr
- );
- material.lightingEnable = true;
- var app:Appearance = new Appearance(material);
- var materialAttr2:MaterialAttributes =
- new MaterialAttributes(
- new LineAttributes(0, 0x2111BB, 0),
- new LightAttributes(true, 0.1)
- );
- var material2:Material = new ColorMaterial(
- 0xCC0000, 1, materialAttr
- );
- material2.lightingEnable = true;
- var app2:Appearance = new Appearance(material2);
- var materialAttr3:MaterialAttributes =
- new MaterialAttributes(
- new LineAttributes(0, 0x2111BB, 0),
- new LightAttributes(true, 0.1)
- );
- var material3:Material = new ColorMaterial(
- 0x008AE6, 1, materialAttr
- );
- material3.lightingEnable = true;
- var app3:Appearance = new Appearance(material3);
- // 產生一 Box 物件,並設定其參數,分別為:
- // 識別名稱、寬、高、深度、face generation 及 Quility
- var table:Box = new Box(
- "table", 10, 150, 200, PrimitiveMode.QUAD, 1
- );
- //table.enableBackFaceCulling = false;
- // 設定是否容許使用者改變呈現此 table 的方式
- table.useSingleContainer = false;
- // 設定 table 的外觀
- table.appearance = app;
- // 旋轉 table
- table.rotateY = 90;
- table.rotateX = 90;
- // 產生一 Cylinder 物件,並設定其參數,分別為
- // 識別名稱、半徑及高
- var leg01:Cylinder = new Cylinder("leg01", 5, 80);
- // 設定 leg01 的外觀
- leg01.appearance = app;
- // 設定 leg01 的 x,y,z 座標
- leg01.x = -80;
- leg01.y = -45;
- leg01.z = 50;
- var leg02:Cylinder = new Cylinder("lig02", 5, 80);
- leg02.appearance = app;
- leg02.x = 80;
- leg02.y = -45;
- leg02.z = 50;
- var leg03:Cylinder = new Cylinder("leg03", 5, 80);
- leg03.appearance = app;
- leg03.x = -80;
- leg03.y = -45;
- leg03.z = -50;
- var leg04:Cylinder = new Cylinder("leg04", 5, 80);
- leg04.appearance = app;
- leg04.x = 80;
- leg04.y = -45;
- leg04.z = -50;
- // 產生一 Sphere 物件,並設定其參數,分別為:
- // 識別名稱、半徑、水平切割數及垂直切割數
- var mySphere:Sphere = new Sphere("theSphere", 20, 20, 8);
- mySphere.useSingleContainer = true;
- mySphere.appearance = app2;
- mySphere.y = 25;
- mySphere.x = -30;
- var myBox:Box = new Box(
- "theBox", 60, 60, 60, PrimitiveMode.TRI, 3
- );
- myBox.useSingleContainer = true;
- myBox.appearance = app3;
- myBox.rotateY = 30;
- myBox.x = 40;
- myBox.y = 35;
- tg.addChild(table);
- tg.addChild(leg01);
- tg.addChild(leg02);
- tg.addChild(leg03);
- tg.addChild(leg04);
- tg.addChild(mySphere);
- tg.addChild(myBox);
- tg.rotateX = 5;
- g.addChild(tg);
- return g;
- }
- private function enterFrameHandler(evt:Event):void
- {
- // 將場景顯示快取設定為 false
- scene.render(false);
- }
- private function keyPressed(evt:KeyboardEvent):void
- {
- switch(evt.keyCode)
- {
- case Keyboard.PAGE_DOWN:
- // 減少場景燈光的亮度
- scene.light.setPower(scene.light.getPower() - 5);
- break;
- case Keyboard.PAGE_UP:
- // 增加場景燈光的亮度
- scene.light.setPower(scene.light.getPower() + 5);
- break;
- case Keyboard.UP:
- lightY += 10;
- scene.light.setDirection(lightX, lightY, lightZ);
- break;
- case Keyboard.DOWN:
- lightY -= 10;
- scene.light.setDirection(lightX, lightY, lightZ);
- break;
- case Keyboard.RIGHT:
- lightX += 10;
- scene.light.setDirection(lightX, lightY, lightZ);
- break;
- case Keyboard.LEFT:
- lightX -= 10;
- scene.light.setDirection(lightX, lightY, lightZ);
- break;
- }
- }
- }
- }
產出:
程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut041
沒有留言:
張貼留言