• [ActionScript 3.0] Away3D 灯光的使用


      1 package
      2 {
      3     import away3d.containers.View3D;
      4     import away3d.entities.Mesh;
      5     import away3d.events.MouseEvent3D;
      6     import away3d.lights.DirectionalLight;
      7     import away3d.lights.PointLight;
      8     import away3d.materials.ColorMaterial;
      9     import away3d.materials.TextureMaterial;
     10     import away3d.materials.lightpickers.StaticLightPicker;
     11     import away3d.materials.methods.FilteredShadowMapMethod;
     12     import away3d.primitives.CubeGeometry;
     13     import away3d.primitives.PlaneGeometry;
     14     import away3d.utils.Cast;
     15     
     16     import flash.display.Sprite;
     17     import flash.display.StageAlign;
     18     import flash.display.StageScaleMode;
     19     import flash.events.Event;
     20     
     21     /**
     22      * @author Frost.Yen
     23      * @E-mail 871979853@qq.com
     24      * @create 2015-9-23 下午3:27:28
     25      *
     26      */
     27     [SWF(width='800',height='600',frameRate="60", backgroundColor="0x000000")]
     28     public class Away3dLight extends Sprite
     29     {
     30         
     31         //floor的贴图图片
     32         [Embed(source = "assets/1.jpg")]
     33         private static var FloorMaterial:Class;
     34         //声明视口
     35         private var _view:View3D;
     36         //声明平面几何对象
     37         private var _planeGeometry:PlaneGeometry;
     38         //声明平面对象的贴图
     39         private var _planeMaterial:TextureMaterial;
     40         //声明平面几何对象的容器
     41         private var _planeMesh:Mesh;
     42         //控制旋转方向的变量
     43         private var _direction:Boolean;
     44         //声明cube对象
     45         private var _cubeGeometry:CubeGeometry;
     46         //声明cube对象容器
     47         private var _cubeMesh:Mesh;
     48         
     49         //灯光
     50         private var _directionalLight:DirectionalLight;
     51         private var _pointLight:PointLight;
     52         //灯光容器
     53         private var _light:StaticLightPicker;
     54         
     55 
     56         public function Away3dLight()
     57         {
     58             if (stage) {
     59                 init();
     60             }else {
     61                 this.addEventListener(Event.ADDED_TO_STAGE, init);
     62             }
     63 
     64         }
     65         private function init(e:Event=null):void {
     66             //trace("舞台初始化完成!");
     67             //设置舞台缩放模式和对齐方式
     68             stage.scaleMode = StageScaleMode.NO_SCALE;
     69             stage.align = StageAlign.TOP_LEFT;
     70             this.removeEventListener(Event.ADDED_TO_STAGE, init);
     71             
     72             //实例化视口
     73             _view = new View3D();
     74             addChild(_view);
     75             //设置抗锯齿参数
     76             _view.antiAlias = 6;
     77             
     78             //实例化一个长宽都是300的平面对象
     79             _planeGeometry = new PlaneGeometry(300, 300);
     80             //实例化贴图对象
     81             _planeMaterial = new TextureMaterial(Cast.bitmapTexture(FloorMaterial));
     82             //实例化平面几何对象的容器,第一个参数是平面几何对象,第二个参数是贴图数据
     83             _planeMesh = new Mesh(_planeGeometry, _planeMaterial);
     84             //设置容器可交互
     85             _planeMesh.mouseEnabled = true;
     86             //容器侦听鼠标点击事件
     87             _planeMesh.addEventListener(MouseEvent3D.CLICK, clickHandler);
     88             
     89             //将容器添加到视口的场景中
     90             _view.scene.addChild(_planeMesh);
     91             
     92             _cubeGeometry = new CubeGeometry();
     93             _cubeMesh = new Mesh(_cubeGeometry, new ColorMaterial(0xcccccc));
     94             _cubeMesh.mouseEnabled = true;
     95             _cubeMesh.y = 150;
     96             _cubeMesh.z = -40;
     97             _view.scene.addChild(_cubeMesh);
     98             
     99             //添加平行光源
    100             _directionalLight = new DirectionalLight();
    101             _directionalLight.diffuse = .8;
    102             _directionalLight.ambient = .3;
    103             _directionalLight.castsShadows = true;
    104             
    105             //添加点光源
    106             _pointLight = new PointLight();
    107             _pointLight.ambient = 0.4;
    108             //_pointLight.diffuse = 10;
    109             
    110             //实例化灯光容器
    111             _light = new StaticLightPicker([_directionalLight,_pointLight]);
    112             
    113             //给地面添加阴影效果
    114             _planeMaterial.shadowMethod = new FilteredShadowMapMethod(_directionalLight);
    115             
    116             //给两个模型的材质添加灯光
    117             _planeMesh.material.lightPicker = _light;
    118             _cubeMesh.material.lightPicker = _light;
    119             
    120             //将灯光添加到场景
    121             _view.scene.addChild(_directionalLight);
    122             
    123             //设置摄像机的位置
    124             _view.camera.z = -400;
    125             _view.camera.y = 200;    //可以把这个值改成1试试看,这样可以有更加直观的感受
    126             //_view.camera.x = 90;
    127             //设置摄像机始终指向平面
    128             _view.camera.lookAt(_planeMesh.position);
    129             //_view.camera.lookAt(new Vector3D());
    130             
    131             this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    132             stage.addEventListener(Event.RESIZE, onResize);
    133             onResize();
    134         }
    135         
    136         private function clickHandler(e:MouseEvent3D):void {
    137             //鼠标点击变换运动方向
    138             _direction = !_direction;
    139         }
    140         
    141         private function onResize(e:Event = null):void {
    142             //调整视口尺寸以适应新的窗口大小
    143             _view.width = stage.stageWidth;
    144             _view.height = stage.stageHeight;
    145         }
    146         
    147         private function onEnterFrame(e:Event):void {
    148             //判断方向旋转
    149             _cubeMesh.rotationX += 1;
    150             _cubeMesh.rotationY += 1;
    151             if (!_direction) {
    152                 _planeMesh.rotationY += 1;
    153             }else {
    154                 _planeMesh.rotationY -= 1;
    155             }
    156             //渲染3D世界
    157             _view.render();
    158         }
    159 
    160     }
    161 }
  • 相关阅读:
    vue2.0动态添加组件
    Kali Linux信息收集工具全
    Kali Linux 弱点分析工具全集
    如何DIY一个简单的反弹Shell脚本
    深入理解DIP、IoC、DI以及IoC容器
    Intellij IDEA常用配置详解
    GIT 的常规操作
    Nodejs学习笔记(一)--- 简介及安装Node.js开发环境
    什么是“对用户友好”
    Facebook为什么使用PHP编程语言?
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5000032.html
Copyright © 2020-2023  润新知