away3D操纵三维物体之各方式绕球旋转

/*在fb下新建as项目运行此实例*/
Basic07_apply.as


package
{
import away3d.containers.View3D;
import away3d.core.math.Number3D;
import away3d.core.render.Renderer;
import away3d.primitives.Cube;
import away3d.primitives.Sphere;
import away3d.test.Button;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.*;

[SWF(width="500", height="400", frameRate="60", backgroundColor="#FFFFFF")]
public class Basic07_apply extends Sprite
{
private var view:View3D;
private var cube:Cube;
private var toggle:uint = 0;
private var cubeSpeed:Number = 5;
private var label:TextField;

public function Basic07_apply()
{
// add a button to re-set the rotation (rotate mesh, but not object valuse)
var bt1:Button = new Button("Apply the current rotation as new Zero",260,20);
bt1.x = 10;
bt1.y = 60;
bt1.addEventListener(MouseEvent.CLICK,applyRotations);
this.addChild(bt1);

// add a button to toggle between the two ways of moving
var bt2:Button = new Button("Toggle pivot point",260,20);
bt2.x = 10;
bt2.y = 90;
bt2.addEventListener(MouseEvent.CLICK,applyPivot);
this.addChild(bt2);

// Add a textfield
label = new TextField();
label.autoSize = TextFieldAutoSize.LEFT;
label.multiline = true;
label.wordWrap = true;
label.x = 10;
label.y = 10;
label.width = 480;
label.defaultTextFormat = new TextFormat("Arial", 14, 0x000000);
this.addChild(label);

// create a viewport
view = new View3D({x:250,y:200,renderer:Renderer.INTERSECTING_OBJECTS});
addChild(view);
view.camera.y = 300; // Move camera up
view.camera.lookAt( new Number3D(0,0,0)); // Point it toward scene center again, so the target is right (setting y does not change where the camera is pointing)

// create a rectangular cube
cube = new Cube({material:"green#black",depth:200,width:10,height:10});
// add cube to scene
view.scene.addChild(cube);

// create a sphere for reference
var sphere:Sphere = new Sphere({material:"red#black",radius:25});
view.scene.addChild(sphere);

// render the view
this.addEventListener(Event.ENTER_FRAME,update);
}
private function applyRotations(e:MouseEvent):void
{
cube.rotationY = 45;
cube.applyRotations();
toggle = 1;
}
// simply applies a new position to the mesh
private function applyPivot(e:MouseEvent):void
{
if(toggle == 2){ // reset pivot
toggle = 0;
cube.movePivot(0,0,0);
} else {
toggle = 2;
cube.movePivot(0,0,-120);
}
}
private function update(e:Event):void
{
// Keep cube within bounds
cube.rotationY += 1;
if(toggle == 1){
label.text = "Using cube.applyRotations. RotationY is now: "+Math.floor(cube.rotationY);
} else if (toggle == 2){
label.text = "The altered pivot point of 0,0,-120 is now used. Cube.z is now: "+Math.floor(cube.x);
} else {
label.text = "The standard pivot point of 0,0,0 is now used. Cube.z is now: "+Math.floor(cube.x);
}

// re-render the view
view.render();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值