- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
本帖转载于网络,分享展示Sandy关于灯光的功能。当你想使用每个对象的内置的灯光效果来渲染你的对象时。你可能满足于默认的设置,但你也可能希望可以修改灯光的强度或灯光的方向。在这里你将会学到怎么实现这些。
怎么做?
代码如下:
package{importflash.display.Sprite; importflash.events.*; importflash.ui.*; importsandy.core.Scene3D; importsandy.core.data.*; importsandy.core.scenegraph.*; importsandy.materials.*; importsandy.materials.attributes.*; importsandy.primitive.*; publicclass Example0041 extendsSprite{privatevarscene:Scene3D; privatevarcamera:Camera3D; privatevar tg:TransformGroup; privatevar lightX = 0; privatevar lightY = 0; privatevar lightZ = 10; publicfunction Example0041(){camera = newCamera3D(300, 300); camera.z = -400; varroot:Group = createScene(); scene = newScene3D("scene", this, camera, root); scene.light.setDirection(lightX, lightY, lightZ); addEventListener(Event.ENTER_FRAME, enterFrameHandler ); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); } privatefunctioncreateScene():Group{var g:Group = newGroup(); tg = newTransformGroup(); var materialAttr:MaterialAttributes = newMaterialAttributes(newLineAttributes(0, 0x2111BB, 0), newLightAttributes(***e, 0.1)); var material:Material = newColorMaterial( 0xFFCC33, 1, materialAttr ); material.lightingEnable = ***e; var app:Appearance = newAppearance( material ); var materialAttr2:MaterialAttributes = newMaterialAttributes(newLineAttributes(0, 0x2111BB, 0), newLightAttributes(***e, 0.1)); var material2:Material = newColorMaterial( 0xCC0000, 1, materialAttr ); material2.lightingEnable = ***e; var app2:Appearance = newAppearance( material2 ); var materialAttr3:MaterialAttributes = newMaterialAttributes(newLineAttributes(0, 0x2111BB, 0), newLightAttributes(***e, 0.1)); var material3:Material = newColorMaterial( 0x008AE6, 1, materialAttr ); material3.lightingEnable = ***e; var app3:Appearance = newAppearance( material3 ); vartable = newBox("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 = newCylinder("leg01", 5, 80); leg01.appearance = app; leg01.x = -80; leg01.y = -45; leg01.z = 50; var leg02:Cylinder = newCylinder("leg02", 5, 80); leg02.appearance = app; leg02.x = 80; leg02.y = -45; leg02.z = 50; var leg03:Cylinder = newCylinder("leg02", 5, 80); leg03.appearance = app; leg03.x = -80; leg03.y = -45; leg03.z = -50; var leg04:Cylinder = newCylinder("leg02", 5, 80); leg04.appearance = app; leg04.x = 80; leg04.y = -45; leg04.z = -50; var mySphere:Sphere = newSphere("theSphere", 20, 20, 8); mySphere.useSingleContainer = ***e; mySphere.appearance = app2; mySphere.y = 25; mySphere.x = -30; var myBox:Box = newBox("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; } privatefunction enterFrameHandler(event : Event) : void{scene.render(); } privatefunction keyPressed(event:KeyboardEvent):void{switch(event.keyCode){caseKeyboard.PAGE_DOWN: scene.light.setPower(scene.light.getPower() - 5); break; caseKeyboard.PAGE_UP: scene.light.setPower(scene.light.getPower() + 5); break; caseKeyboard.UP: lightY+=10; scene.light.setDirection(lightX, lightY, lightZ); break; caseKeyboard.DOWN: lightY-=10; scene.light.setDirection(lightX, lightY, lightZ); break; caseKeyboard.RIGHT: lightX+=10; scene.light.setDirection(lightX, lightY, lightZ); break; caseKeyboard.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键可以控制灯光的强度,方向键可以控制灯光的方向。
|
|