[ActionScript 3.0] Away3D 灯光的使用

package
{
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.events.MouseEvent3D;
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; import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-9-23 下午3:27:28
*
*/
[SWF(width='800',height='600',frameRate="60", backgroundColor="0x000000")]
public class Away3dLight extends Sprite
{ //floor的贴图图片
[Embed(source = "assets/1.jpg")]
private static var FloorMaterial:Class;
//声明视口
private var _view:View3D;
//声明平面几何对象
private var _planeGeometry:PlaneGeometry;
//声明平面对象的贴图
private var _planeMaterial:TextureMaterial;
//声明平面几何对象的容器
private var _planeMesh:Mesh;
//控制旋转方向的变量
private var _direction:Boolean;
//声明cube对象
private var _cubeGeometry:CubeGeometry;
//声明cube对象容器
private var _cubeMesh:Mesh; //灯光
private var _directionalLight:DirectionalLight;
private var _pointLight:PointLight;
//灯光容器
private var _light:StaticLightPicker; public function Away3dLight()
{
if (stage) {
init();
}else {
this.addEventListener(Event.ADDED_TO_STAGE, init);
} }
private function init(e:Event=null):void {
//trace("舞台初始化完成!");
//设置舞台缩放模式和对齐方式
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
this.removeEventListener(Event.ADDED_TO_STAGE, init); //实例化视口
_view = new View3D();
addChild(_view);
//设置抗锯齿参数
_view.antiAlias = 6; //实例化一个长宽都是300的平面对象
_planeGeometry = new PlaneGeometry(300, 300);
//实例化贴图对象
_planeMaterial = new TextureMaterial(Cast.bitmapTexture(FloorMaterial));
//实例化平面几何对象的容器,第一个参数是平面几何对象,第二个参数是贴图数据
_planeMesh = new Mesh(_planeGeometry, _planeMaterial);
//设置容器可交互
_planeMesh.mouseEnabled = true;
//容器侦听鼠标点击事件
_planeMesh.addEventListener(MouseEvent3D.CLICK, clickHandler); //将容器添加到视口的场景中
_view.scene.addChild(_planeMesh); _cubeGeometry = new CubeGeometry();
_cubeMesh = new Mesh(_cubeGeometry, new ColorMaterial(0xcccccc));
_cubeMesh.mouseEnabled = true;
_cubeMesh.y = 150;
_cubeMesh.z = -40;
_view.scene.addChild(_cubeMesh); //添加平行光源
_directionalLight = new DirectionalLight();
_directionalLight.diffuse = .8;
_directionalLight.ambient = .3;
_directionalLight.castsShadows = true; //添加点光源
_pointLight = new PointLight();
_pointLight.ambient = 0.4;
//_pointLight.diffuse = 10; //实例化灯光容器
_light = new StaticLightPicker([_directionalLight,_pointLight]); //给地面添加阴影效果
_planeMaterial.shadowMethod = new FilteredShadowMapMethod(_directionalLight); //给两个模型的材质添加灯光
_planeMesh.material.lightPicker = _light;
_cubeMesh.material.lightPicker = _light; //将灯光添加到场景
_view.scene.addChild(_directionalLight); //设置摄像机的位置
_view.camera.z = -400;
_view.camera.y = 200; //可以把这个值改成1试试看,这样可以有更加直观的感受
//_view.camera.x = 90;
//设置摄像机始终指向平面
_view.camera.lookAt(_planeMesh.position);
//_view.camera.lookAt(new Vector3D()); this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, onResize);
onResize();
} private function clickHandler(e:MouseEvent3D):void {
//鼠标点击变换运动方向
_direction = !_direction;
} private function onResize(e:Event = null):void {
//调整视口尺寸以适应新的窗口大小
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
} private function onEnterFrame(e:Event):void {
//判断方向旋转
_cubeMesh.rotationX += 1;
_cubeMesh.rotationY += 1;
if (!_direction) {
_planeMesh.rotationY += 1;
}else {
_planeMesh.rotationY -= 1;
}
//渲染3D世界
_view.render();
} }
}
[ActionScript 3.0] Away3D 灯光的使用的更多相关文章
- [ActionScript 3.0] Away3D 官网实例
/* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...
- [ActionScript 3.0] Away3D 非skybox的全景例子
package { import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d. ...
- [ActionScript 3.0] Away3D 天空盒(skybox)例子2
所谓skybox就是六个面即六张图能够无缝的拼成一个正方体的盒子. package { import away3d.cameras.Camera3D; import away3d.cameras.le ...
- [ActionScript 3.0] Away3D 天空盒(skybox)例子
/* SkyBox example in Away3d Demonstrates: How to use a CubeTexture to create a SkyBox object. How to ...
- [ActionScript 3.0] Away3D 旋转效果
package { import away3d.containers.View3D; import away3d.entities.Mesh; import away3d.events.MouseEv ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- ActionScript 3.0 for the Lunder Algorithm
package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...
- [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...
- [ActionScript 3.0] AS3.0 动态加载显示内容
可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...
随机推荐
- Tengine:基于Nginx的衍生版
engine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验 ...
- Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件
Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件 下载地址 插件的操作很简单,下面是一些简单的实例. 1.安装 在谷歌应用商城搜索postman,如下图1-1所 ...
- .Net HttpPost的发送和接收示例代码
/// <summary> /// 模拟http 发送post或get请求 /// </summary> /// <param name="Url"& ...
- web.xml中contextConfigLocation的作用
如果在web.xml里给该Listener指定要加载的xml,如: xml代码如下: <!-- spring config --> <context-param> <pa ...
- mysqlbinlog快速遍历搜索记录 (转)
目标,开发人员说有个数据莫名其妙添加了,但是不知道是从哪里添加的,而且应用功能里面不应该添加这样的数据,为了查清楚来源,所 以我就准备去binlog里面找了,但是binlog有好几个月的数,我这样一个 ...
- Struts2 - Rest(1)
Struts2提供了一个restful的插件:struts2-rest-plugin-2.3.16.1.jar 这个插件可以把Struts2当做restful来使用,不过它的rest功能目前来说有点“ ...
- OC-设计模式KVC+KVO定义及使用
一.KVC Key-Value-Coding 键值编码(KVC:是一种存取值的方式,通过key存value 或者通过key获取value key从哪里来的呢? key 把对象里面的属性名.变量名当作了 ...
- 151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- spring4.0整合mongodb3.0.4项目实践(用户验证)
我们的项目用到了spring框架和mongdb数据库,随着mongodb升级到3.0已有半年时间,我们也开始随之升级,但是3.0的用户验证有所更改,导致原来的很多配置无法再用. 经过几天的尝试后,终于 ...
- 我的Android最佳实践之—— ImageView中图片拉伸显示
通过设置android:scaleType="fitXY"使得图片拉伸显示.补充:scaleType的属性有matrix(默认).center.centerCrop.centerI ...