Away3D - 光源的添加和使用(PointLight,DirectionalLight)
作者:hangge | 2015-11-21 09:06
为了使三维场景更加真实,通常会给舞台添加灯光进行光影渲染。目前,Away3D 提供两种光源:点光源与平行投射光源。
5,完整样例
1,点光源与平行投射光源
(1)PointLight:点光源。没有方向性,通常用来模拟太阳,月亮,或是台灯的灯炮
(2)DirectionalLight:平行投射光源,或者称之为直射光源。由来模拟探照灯,或是透过窗户的月光等等。
2,灯光容器
我们可以添加多个光源,比如创建多个方向的平行投射光源。再把这些光源放到 StaticLightPicker 这个灯光容器里。
3,光源的使用
需要将灯光运用在这个世界里,模型的贴图可以受到光的影响 不管是TextureMaterial还是ColorMaterial都有lightPicker属性。我们只需要将StaticLightPicker类型对象赋予给它即可。
4,地面阴影
想要平行投射光源照射到物体上,能在地面留下阴影。比如我们用plane来做地面,要想实现投射影子可以如下操作:
planeMaterial.shadowMethod = new FilteredShadowMapMethod(light1);
5,完整样例
(1)使用plane做地面。舞台添加两个方块,一个纯白色,一个使用贴图。
(2)舞台中添加一个点光源,一个直射光源。直射光从左上方下右下方投射。
(3)方块可以看出明暗面区别,地面上也有影子。
package{ import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Vector3D; import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d.entities.Mesh; import away3d.lights.DirectionalLight; import away3d.lights.PointLight; import away3d.materials.ColorMaterial; import away3d.materials.TextureMaterial; import away3d.materials.lightpickers.StaticLightPicker; import away3d.materials.methods.FilteredShadowMapMethod; import away3d.primitives.CubeGeometry; import away3d.primitives.PlaneGeometry; import away3d.utils.Cast; [SWF(frameRate="60", backgroundColor="#FFFFFF")] public class S2 extends Sprite { private var _view3D:View3D; private var cameraController:HoverController;//360全景展示相机控制器 [Embed(source="assets/floor_diffuse.jpg")] public static var FloorDiffuse:Class; [Embed(source="assets/cubeTexture3.jpg")] private var cube2TextureClass : Class; //材质 private var planeMaterial:TextureMaterial; private var cube1Material:ColorMaterial; private var cube2Material:TextureMaterial; //灯光对象 private var light0:PointLight; //点光源 private var light1:DirectionalLight; //平行投射光源 private var lightPicker:StaticLightPicker; //灯光容器 private var _lastX:Number = 0; private var _lastY:Number = 0; public function S2() { initEngine(); initLights(); initMaterials(); initObjects(); initListeners(); } /** * 初始化引擎 */ private function initEngine():void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // 创建一个视口 _view3D = new View3D(); _view3D.antiAlias = 4; //设置抗锯齿级别 //初始化摄像头 cameraController = new HoverController(_view3D.camera); /*cameraController.distance = 1000; cameraController.minTiltAngle = 0; cameraController.maxTiltAngle = 90; cameraController.panAngle = 45;*/ cameraController.tiltAngle = 30; addChild(_view3D); } /** * 初始化灯光 */ private function initLights():void { //点光源 light0 = new PointLight(); light0.ambient = 0.4; //平行投射光源(左 上方 向右下方投射) light1 = new DirectionalLight(); light1.direction = new Vector3D(-1, -1, 0); light1.ambient = 0.3; light1.diffuse = 0.8; light1.castsShadows = true; _view3D.scene.addChild(light1); //添加到灯光容器中 lightPicker = new StaticLightPicker([light0,light1]); } /** * 初始化材质 */ private function initMaterials():void { //地面材质 planeMaterial = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse)); planeMaterial.repeat = true; planeMaterial.lightPicker = lightPicker; //地面添加物体阴影 planeMaterial.shadowMethod = new FilteredShadowMapMethod(light1); //方块1材质 cube1Material = new ColorMaterial(0xffffff); cube1Material.lightPicker = lightPicker; //方块2材质 cube2Material = new TextureMaterial(Cast.bitmapTexture(cube2TextureClass)); cube2Material.lightPicker = lightPicker; } /** * 初始化物体 */ private function initObjects():void { //地面 var plane:Mesh = new Mesh(new PlaneGeometry(900, 900), planeMaterial); plane.geometry.scaleUV(3, 3); (plane.geometry as PlaneGeometry).doubleSided = true; //双面 贴图 _view3D.scene.addChild(plane); //纯色方块 var cube1:Mesh = new Mesh(new CubeGeometry(140, 140, 140), cube1Material); cube1.position = new Vector3D(0,70,0); _view3D.scene.addChild(cube1); //帖图方块 var cube2:Mesh = new Mesh(new CubeGeometry(140, 140, 140, 1, 1, 1, false), cube2Material); cube2.position = new Vector3D(0,70,200); _view3D.scene.addChild(cube2); } /** * 初始化监听 */ private function initListeners():void { addEventListener(Event.ENTER_FRAME, _onEnterFrame); //鼠标事件监听 stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel); stage.addEventListener(Event.RESIZE, onResize); onResize(); } /** * 渲染视图 */ private function _onEnterFrame(e:Event):void { //渲染视图 _view3D.render(); } /** * 使用舞台大小一直全屏 */ private function onResize(event:Event = null):void { _view3D.width = stage.stageWidth; _view3D.height = stage.stageHeight; } /** * 鼠标滚轮事件 */ private function onWheel(e:MouseEvent):void { if(e.delta > 0){ if(cameraController.distance < 1000) cameraController.distance += 100; }else{ if(cameraController.distance > 600) cameraController.distance -= 100; } } /** * 鼠标按下事件 */ private function onMouseDown(event:MouseEvent):void { _view3D.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); _lastX = _view3D.mouseX; _lastY = _view3D.mouseY; } /** * 鼠标弹起事件 */ private function onMouseUp(event:MouseEvent):void { _view3D.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } /** * 鼠标移动事件 */ private function mouseMoveHandler(event:MouseEvent):void { //移动摄像机 var dx:Number = _view3D.mouseX - _lastX; var dy:Number = _view3D.mouseY - _lastY; cameraController.panAngle += dx; cameraController.tiltAngle += dy; _lastX = _view3D.mouseX; _lastY = _view3D.mouseY; } } }源码下载:Amay3DTest8.zip
全部评论(0)