第二個問題:建立了一個立方體後,怎樣為為這個立方體添加材質?
首先最簡單的材質,我們會想到顏色,顏色是最容易理解。因此我們在這裡應該學會如何為我們的物體添加材質。
- package
- {
- import flash.display.Sprite;
- import flash.events.*;
- import sandy.core.Scene3D;
- import sandy.core.scenegraph.*;
- import sandy.primitive.*;
- import sandy.core.data.*;
- import sandy.materials.*;
- import sandy.materials.attributes.*;
- public class My3D2 extends Sprite
- {
- private var scenne:Scene3D;
- private var camera:Camera3D;
- private var ranY:Number=0.5;
- private var box:Box;
- public function My3D2()
- {
- camera=new Camera3D(400,400);//設定攝影機大小和位置
- camera.z=-300;
- var root:Group=createScene();
- scenne=new Scene3D("scene1",this,camera,root);//建立帶攝像機的情境
- addEventListener(Event.ENTER_FRAME,Run);
- }
- private function createScene():Group
- {
- var g:Group=new Group();
- box=new Box("box",100,100,100);
- var materialAttr:MaterialAttributes = new MaterialAttributes(
- new LineAttributes( 0.5, 0x2111BB, 0.4 ),
- new LightAttributes( true, 0.1)
- );//建立材質屬性,線性光,
- var material:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr );//建立顏色材質
- material.lightingEnable = true;
- var app:Appearance = new Appearance( material );//為物體建立材質表面
- box.rotateX=10;
- box.rotateY=0;
- box.appearance = app;//指定材質表面
- g.addChild(box);
- return g;
- }
- private function Run(event:Event):void
- {
- scenne.render();
- box.rotateX+=1;//讓立方體會在x軸上旋轉
- box.rotateY+=1;//讓立方體會在y軸上旋轉
- }
- }
- }
說明:建立了立方體後,我們可以為我們的立方體添加材質或者指定立方體的顏色屬性,這時候我們需要使用到顯示顏色材質的類和函數,sandy為我們提供了一個類。
- var materialAttr:MaterialAttributes = new MaterialAttributes(
- new LineAttributes( 0.5, 0x2111BB, 0.4 ),
- new LightAttributes( true, 0.1)
- );//建立材質屬性,線性光,
- var material:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr );//建立顏色材質
- material.lightingEnable = true;
- var app:Appearance = new Appearance( material );//為物體建立材質表面
ColorMaterial( 0xFFCC33, 1, materialAttr );//建立顏色材質,
參數一,指定顏色, 透明度,材質屬性,這個參數預設為null,我們為這個立方體製作一個可見光和線性光
| ColorMaterial |
() |
constructor |
//函數原型
public function ColorMaterial(p_nColor:uint = 0x00, p_nAlpha:Number = 1, p_oAttr:MaterialAttributes = null)
Creates a new ColorMaterial.
建立了顏色的屬性,為了指定物體,這時候還要製作一個表面賦給立方體。
var app:Appearance = new Appearance( material );//為物體建立材質表面
box.appearance = app;//指定材質表面,
最後情境渲染;
scenne.render();
這裡就創造出一個具有顏色的立方體了