Posted on 2009-07-07 20:46
Tommy Liang 阅读(1109)
评论(0) 编辑 收藏 引用
package
{
import away3d.containers.*;
import away3d.core.base.*;
import away3d.core.math.*;
import away3d.core.utils.Cast;
import away3d.events.*;
import away3d.materials.*;
import away3d.primitives.*;
import away3d.core.render.Renderer;
import com.bit101.components.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width="500", height="400", frameRate="30", backgroundColor="#FFFFFF")]
public class Textures extends Sprite
{
// "global" vars
private var plane:Plane;
private var planeBorder:Plane;
private var planeComplexity:Number = 1;
private var my_material:IMaterial;
private var cover:Cover;
private var View:View3D;
private var panel:Panel;
private var toggle:Boolean = false;
// Embed texture for use in Flex
[Embed(source="resources/chess.jpg")]
private var chessBitmap:Class;
private var chessTexture:BitmapData;
public function Textures()
{
super();
// create a 3D-viewport
View = new View3D({x:250, y:200});
View.renderer = Renderer.CORRECT_Z_ORDER;
// add viewport to the stage
addChild(View);
// Store texture and create plane
var bitmap:Bitmap = new chessBitmap() as Bitmap;
chessTexture = bitmap.bitmapData;
my_material = new BitmapMaterial(Cast.bitmap(chessTexture));
makePlane();
// position camera
View.camera.position = new Number3D(400, 500, 400);
View.camera.lookAt( new Number3D(0, 0, 0) );
// add some controls
addControls();
// Update the view and display the "cover"
View.render();
cover = new Cover(this);
addChild(cover);
// Turn on rotation
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
// functions used
private function onEnterFrame(e:Event):void
{
update();
}
private function update(e:MouseEvent = null):void
{
if(!cover.visible)
{
// rerender viewport
View.render();
plane.rotationY = planeBorder.rotationY += 1;
}
}
private function makePlane():void
{
var rot:Number;
if(plane){
rot = plane.rotationY;
destroy();
}
// Add new plane
plane = new Plane({material:my_material,width:250,height:250,x:0,y:30,segmentsH:planeComplexity, segmentsW:planeComplexity});
planeBorder = new Plane({material:"grey",width:265,height:265,x:0,y:28,segmentsH:1, segmentsW:1});
// add plane to the scene
View.scene.addChild(planeBorder);
View.scene.addChild(plane);
if(rot){
plane.rotationY = planeBorder.rotationY = rot;
}
}
private function addControls():void
{
var pad:Number = 10;
var label1:Label = new Label(this, pad, pad);
label1.autoSize = false;
label1.width = 180 -(pad*2);
label1.text = "Increase/decrease complexity";
var plusButt:PushButton = new PushButton(this, pad, label1.height+label1.y, "+", increaseComplexity);
plusButt.width = plusButt.height = 20;
var minButt:PushButton = new PushButton(this, 50, label1.height+label1.y, "-", decreaseComplexity);
minButt.width = minButt.height = 20;
var toggleButt:PushButton = new PushButton(this, pad, plusButt.height+plusButt.y+pad, "Toggle texture", wiresTextureToggle);
toggleButt.height = 20;
}
private function decreaseComplexity(e:MouseEvent):void
{
planeComplexity > 0 ? planeComplexity -= 1 : planeComplexity = 1;
makePlane();
}
private function increaseComplexity(e:MouseEvent):void
{
planeComplexity += 1;
makePlane();
}
private function wiresTextureToggle(e:MouseEvent):void
{
trace("wiresTextureToggle"+toggle);
if(!toggle){
my_material = new WireColorMaterial({color:"blue", lighting:true});
toggle = true;
} else {
my_material = new BitmapMaterial(Cast.bitmap(chessTexture));
toggle = false;
}
makePlane();
}
private function destroy():void
{
if(plane){
plane.material = null;
View.scene.removeChild(plane);
View.scene.removeChild(planeBorder);
plane = null;
planeBorder = null;
}
}
}
}