Sandy引擎学习笔记: 三维效果初试

 

 

 

经过一个上午的学习,感觉到这个sandy 发布的as3.0 真的很强大,看了官方一些介绍,把它做了出来

 

  1. package 
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.*;
  5.     import sandy.core.Scene3D;
  6.     import sandy.core.scenegraph.*;
  7.     import sandy.primitive.*;
  8.     import sandy.core.data.*;
  9.     import sandy.materials.*;
  10.     import sandy.materials.attributes.*;
  11.     import flash.display.BitmapData;
  12.     import flash.display.Bitmap;
  13.     import flash.ui.Keyboard;
  14.     public class DD extends Sprite
  15.     {
  16.         private var scence:Scene3D;
  17.         private var camera:Camera3D;
  18.         private var box:Box;
  19.         private var rot:int=45;
  20.         private var img:Mypic;
  21.         private var bitmap:Bitmap;
  22.         private var up:Boolean;
  23.         private var down:Boolean;
  24.         private var left:Boolean;
  25.         private var right:Boolean;
  26.         public function DD()
  27.         {
  28.             img=new Mypic(0,0);
  29.             bitmap=new Bitmap(img);
  30.             camera=new Camera3D(500,400);//建立一个摄影机
  31.             camera.z=-400;
  32.             var group:Group=createScence();
  33.             scence=new Scene3D("scene1",this,camera,group);//建立一个场景
  34.             scence.root.addChild(camera);
  35.             addEventListener(Event.ENTER_FRAME,Run);
  36.             stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
  37.             stage.addEventListener(KeyboardEvent.KEY_UP,KeyUp);
  38.             stage.addEventListener(Event.ENTER_FRAME,Runing);
  39.         }
  40.         private function createScence():Group
  41.         {
  42.             //添加材质
  43.             //第一步,设置材质属性
  44.             var materia:MaterialAttributes =new MaterialAttributes(
  45.             new LightAttributes(true,0.3),
  46.             new LineAttributes(1,0x2111BB,1)
  47.             );
  48.             //第二步,设置材质属性
  49.             var material:Material = new ColorMaterial( 0xFFCC33, 1, materia );//显示颜色材质属性
  50.             var material2:Material = new BitmapMaterial(bitmap.bitmapData);//显示颜色材质属性
  51.             material.lightingEnable = true;
  52.             material.lightingEnable=true;
  53.             var app:Appearance = new Appearance( material);//指定材质给可视物体
  54.             var app2:Appearance = new Appearance( material2 );//指定材质给可视物体纹理贴图
  55.             var g:Group=new Group();
  56.             box=new Box("mybox",100,100,100);
  57.             box.appearance = app;
  58.             box.appearance = app2;
  59.             g.addChild(box);
  60.             return g;
  61.         }
  62.         private function Run(event:Event):void
  63.         {
  64.             box.rotateX=rot;
  65.             box.rotateY=45;
  66.             rot+=1;
  67.             scence.render();//渲染场景
  68.         }
  69.         private function KeyDown(event:KeyboardEvent):void
  70.         {
  71.             switch (event.keyCode)
  72.             {
  73.                 case Keyboard.UP :
  74.                     up=true;
  75.                     break;
  76.                 case Keyboard.DOWN :
  77.                     down=true;
  78.                     break;
  79.                 case Keyboard.LEFT :
  80.                     left=true;
  81.                     break;
  82.                 case Keyboard.RIGHT :
  83.                     right=true;
  84.                     break;
  85.             }
  86.         }
  87.         private function KeyUp(event:KeyboardEvent):void
  88.         {
  89.             if (event.keyCode==Keyboard.UP)
  90.             {
  91.                 up=false;
  92.             } else if (event.keyCode==Keyboard.DOWN)
  93.             {
  94.                 down=false;
  95.             } else if (event.keyCode==Keyboard.LEFT)
  96.             {
  97.                 left=false;
  98.             } else if (event.keyCode==Keyboard.RIGHT)
  99.             {
  100.                 right=false;
  101.             } else
  102.             {
  103.             }
  104.         }
  105.         private function Runing(event:Event):void
  106.         {
  107.             if (up)
  108.             {
  109.                 camera.z+=8;
  110.                 //camera.scaleY+=4;
  111.             } else if (down)
  112.             {
  113.                 camera.z-=8;
  114.             } else if (left)
  115.             {
  116.                 camera.pan+=2;
  117.             } else if (right)
  118.             {
  119.                 camera.pan-=2;
  120.             } else
  121.             {
  122.             }
  123.         }
  124.     }
  125. }

 

感觉还不错,把这个贴图了一下,三维的效果都出来了,加上了摄影机,效果都不错。

但是感觉到3d计算都很大,当我把这个东西放大的时候,cpu占有率高出了很多,有兴趣的人可以去官方网下载类库,还有帮助文档。

 

这个玩意不是一个玩具,但是热爱flash 的人都会把它记住

你可能感兴趣的:(Sandy引擎学习笔记: 三维效果初试)