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 灯光的使用的更多相关文章

  1. [ActionScript 3.0] Away3D 官网实例

    /* Dynamic tree generation and placement in a night-time scene Demonstrates: How to create a height ...

  2. [ActionScript 3.0] Away3D 非skybox的全景例子

    package { import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d. ...

  3. [ActionScript 3.0] Away3D 天空盒(skybox)例子2

    所谓skybox就是六个面即六张图能够无缝的拼成一个正方体的盒子. package { import away3d.cameras.Camera3D; import away3d.cameras.le ...

  4. [ActionScript 3.0] Away3D 天空盒(skybox)例子

    /* SkyBox example in Away3d Demonstrates: How to use a CubeTexture to create a SkyBox object. How to ...

  5. [ActionScript 3.0] Away3D 旋转效果

    package { import away3d.containers.View3D; import away3d.entities.Mesh; import away3d.events.MouseEv ...

  6. ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...

  7. ActionScript 3.0 for the Lunder Algorithm

    package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...

  8. [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...

  9. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...

随机推荐

  1. 动态进行JQ Validate 的方法

    $.validator.unobtrusive.parse($('form[action = "@Url.Action()"]'));

  2. OpenJudge计算概论-字符串排序

    /*====================================================================== 字符串排序 总时间限制: 1000ms 内存限制: 6 ...

  3. pouchdb 安装使用

    1. 安装: If you are on a Debian flavor of Linux (Ubuntu, Mint, etc.), you can install CouchDB with: $ ...

  4. 在eclipse中的tomcat内存设置

    设置步骤如下: 1.点击eclipse上的debug图标旁边的下拉箭头 2.然后选择Run Configurations, 3.系统弹出设置tomcat配置页面,在Argument中末尾添加参数中的V ...

  5. AndroidStudio 应用(一)

    1.下载安装,用上vpn了都: 2.配置模拟器,出现问题:VT-x is disabled in the BIOS for both all CPU modes (VERR_VMX_MSR_ALL_V ...

  6. js/jquery 操作document对象

    一.获取对象 //js获取的是dom对象,jquery获取的是jquery对象 //jquery对象可以输出dom对象,索引方式输出dom对象,eq()[]方式输出dom对象;eq()输出jquery ...

  7. gdb: multiple process debug

    gdbserver自身不支持multiple process:如果你调试parent process时在子进程上下断点,子进程在运行到那个断点时就会SIGTRAP. 如果你要调试fork出来的子进程: ...

  8. bzoj3035: 导弹防御塔

    Description Freda的城堡——“Freda,城堡外发现了一些入侵者!”“喵...刚刚探究完了城堡建设的方案数,我要歇一会儿嘛lala~”“可是入侵者已经接近城堡了呀!”“别担心,rain ...

  9. windows类书的学习心得(转载)

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  10. sgu233 little kings

    题目大意: 有n*n的棋盘上放k个国王.国王可以攻击与它相邻的八个格子.现在要使国王不相互攻击,有多少种放置的方案数.一个格子不能放两个国王. n<=10,k<=n*n. 分析:简单的状态 ...