- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
指南目标
在这个指南里,我们决定展示Sandy关于灯光的功能。当你想使用每个对象的内置的灯光效果来渲染你的对象时。你可能满足于默认的设置,但你也可能希望可以修改灯光的强度或灯光的方向。在这里你将会学到怎么实现这些。
怎么做?
代码如下:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.ui.*;
import sandy.core.Scene3D;
import sandy.core.data.*;
import sandy.core.scenegraph.*;
import sandy.materials.*;
import sandy.materials.attributes.*;
import sandy.primitive.*;
public class Example0041 extends Sprite {
private var scene:Scene3D;
private var camera:Camera3D;
private var tg:TransformGroup;
private var lightX = 0;
private var lightY = 0;
private var lightZ = 10;
public function Example0041() {
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( ***e, 0.1)
);
var material:Material = new ColorMaterial( 0xFFCC33, 1, materialAttr );
material.lightingEnable = ***e;
var app:Appearance = new Appearance( material );
var materialAttr2:MaterialAttributes = new MaterialAttributes(
new LineAttributes( 0, 0x2111BB, 0 ),
new LightAttributes( ***e, 0.1)
);
var material2:Material = new ColorMaterial( 0xCC0000, 1, materialAttr );
material2.lightingEnable = ***e;
var app2:Appearance = new Appearance( material2 );
var materialAttr3:MaterialAttributes = new MaterialAttributes(
new LineAttributes( 0, 0x2111BB, 0 ),
new LightAttributes( ***e, 0.1)
);
var material3:Material = new ColorMaterial( 0x008AE6, 1, materialAttr );
material3.lightingEnable = ***e;
var app3:Appearance = new Appearance( material3 );
var table = new Box( "table", 10, 150, 200, PrimitiveMode.QUAD, 1 );
//table.enableBackFaceCulling = false;
table.useSingleContainer = false;
table.appearance = app;
table.rotateY = 90;
table.rotateX = 90;
var leg01:Cylinder = new Cylinder( "leg01", 5, 80 );
leg01.appearance = app;
leg01.x = -80;
leg01.y = -45;
leg01.z = 50;
var leg02:Cylinder = new Cylinder( "leg02", 5, 80 );
leg02.appearance = app;
leg02.x = 80;
leg02.y = -45;
leg02.z = 50;
var leg03:Cylinder = new Cylinder( "leg02", 5, 80 );
leg03.appearance = app;
leg03.x = -80;
leg03.y = -45;
leg03.z = -50;
var leg04:Cylinder = new Cylinder( "leg02", 5, 80 );
leg04.appearance = app;
leg04.x = 80;
leg04.y = -45;
leg04.z = -50;
var mySphere:Sphere = new Sphere( "theSphere", 20, 20, 8);
mySphere.useSingleContainer = ***e;
mySphere.appearance = app2;
mySphere.y = 25;
mySphere.x = -30;
var myBox:Box = new Box( "theBox", 60, 60, 60, PrimitiveMode.TRI, 3 );
myBox.useSingleContainer = ***e;
myBox.appearance = app3;
myBox.rotateY = 30;
myBox.y = 35;
myBox.x = 40;
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( event : Event ) : void {
scene.render();
}
private function keyPressed(event:KeyboardEvent):void {
switch(event.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;
}
}
}
}
让我们看看在代码里做了什么
X,Y,Z变量
因为要控制灯光的方向,所以我们在3D的世界里需要三个变量X,Y,Z,我们把它们高为成员变量。这样我们就可以通过键盘的方向键来控制它们的值了。
private var lightX = 0;
private var lightY = 0;
private var lightZ = 10;
设置灯光的位置
scene.light.setDirection(lightX, lightY, lightZ);
如你所见,你只需获取Scene3D对象的light属性然后调置它的值就可以了。
然后在createScene()方法里,我放入了很多对象来模拟一张台和一个球和一个圆环。这张台是一个Box和四个Cylinder(作为四条腿),还有那个球是一个Sphere对象。我将跳过这部份的代码说明,因为我想在之前的指南中,你能很容易理解这些。
创建输入控制器
现在让我们做最重要的东西就是放在控制函数:keyPressed(event:KeyboardEvent).中的代码:
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;
使用pageUp和pageDown键可以控制灯光的强度,方向键可以控制灯光的方向。
下面让我们看看最终的效果:
更多分享尽在web3D纳金网www.narkii.com |
|