(转)使用Away3D创建天空盒(Skybox)

资讯类型: 翻译 
来源页面: http://jasonbejot.com/?p=295
资讯原标题: Create a Skybox in Away3D
资讯原作者: jasonbejot



使用Away3D创建天空盒(Skybox)

jasonbejot, 2009年8月24日

背景

最近我一直在寻找Papervision 3D的替代品,我决定寻找所有Away3D令人兴奋的特性所在。在我花了大量的时间阅读所有API文档后,Skybox和Skybox6这两个类让我非常欣喜。尽管在我的黄金年龄时期,CS(译者:没错,就是反恐!)让我非常沉迷,如今这两个类再次让我沉醉其中。

Away3D的Skybox例子并没有很好的解释如何使用它们,再次我将作更加详细的解释。


描述

如果你玩3D射击游戏,天空盒常常用于天空的绘制,表达远距离的场景和环境。理论上说,就一个内侧面贴了无缝贴图的矩形盒子。这个盒子随着摄像机一起运动,永远位于远端。


类描述

Away3D使用两个类非常简单的实现这个效果:Skybox和Skybox6。二者的区别是:Skybox使用6张独立的图片(每个内侧面一张)而Skybox6使用一张由6个面图像构成的图(译者:就是一张大图)。

Skybox类非常直接,就是输入6张图像并在每一个面上贴一个,接下来,我将详细描述Skybox6这个类。


建立图像

Skybox6使用一张图像贴图,该图像可以理解为一张3x2的贴图(3个面宽,2个面高)。Away3D并没有解释如何建立这样的贴图。实际映射如下:



-左上:前面
-中上:右面
-右上:后面
-左下:左面
-中下:上面
-右下:下面

由于Skybox是立方体,所以每一面的图像都应该是正方形。在我的例子中,每一面的图像都是512x512的,3x2的贴图就是1536x1024的。有点大,但是工作得很好。


使用方法

代码:
  1. var skyboxMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(new SkyBoxTutorial.jpgSkymap()));
  2. var skybox:Skybox6 = new Skybox6(skyboxMaterial);
  3. this.view.scene.addChild(skybox);
复制代码
如果你愿意,可以把代码整理到只有一行,很爽吧?(译者:老外真bt)


例子

下面是我使用上面代码所做的例子,使用了一个网格来表示其位置关系。


代码下载
这里。注意该工程默认设置的是Flash 10版本的player,可以根据需要做调整。

===============================================================================

原文地址:
http://www.jasonsturges.com/2012/07/skybox-in-away3d/

范例目的:
1 如何使用一个 CubeTexture 创建一个 SkyBox 对象
2 如何给材料应用 CubeTexture 以用作环境映射

  1 /*
  2  
  3 SkyBox example in Away3d
  4  
  5 Demonstrates:
  6  
  7 How to use a CubeTexture to create a SkyBox object.
  8 How to apply a CubeTexture to a material as an environment map.
  9  
 10 Code by Rob Bateman
 11 rob@infiniteturtles.co.uk
 12 http://www.infiniteturtles.co.uk
 13  
 14 This code is distributed under the MIT License
 15  
 16 Copyright (c)  
 17  
 18 Permission is hereby granted, free of charge, to any person obtaining a copy
 19 of this software and associated documentation files (the “Software”), to deal
 20 in the Software without restriction, including without limitation the rights
 21 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 22 copies of the Software, and to permit persons to whom the Software is
 23 furnished to do so, subject to the following conditions:
 24  
 25 The above copyright notice and this permission notice shall be included in
 26 all copies or substantial portions of the Software.
 27  
 28 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 29 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 30 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 31 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 32 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 33 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 34 THE SOFTWARE.
 35  
 36 */
 37  
 38 package
 39 {
 40         import away3d.cameras.lenses.*;
 41         import away3d.containers.*;
 42         import away3d.entities.*;
 43         import away3d.materials.*;
 44         import away3d.materials.methods.*;
 45         import away3d.primitives.*;
 46         import away3d.textures.*;
 47         import away3d.utils.*;
 48  
 49         import flash.display.*;
 50         import flash.events.*;
 51         import flash.geom.Vector3D;
 52  
 53         [SWF(backgroundColor="#000000", frameRate="60", quality="LOW")]
 54  
 55         public class Basic_SkyBox extends Sprite
 56         {
 57                 // Environment map.
 58                 [Embed(source="../embeds/skybox/snow_positive_x.jpg")]
 59                 private var EnvPosX:Class;
 60                 [Embed(source="../embeds/skybox/snow_positive_y.jpg")]
 61                 private var EnvPosY:Class;
 62                 [Embed(source="../embeds/skybox/snow_positive_z.jpg")]
 63                 private var EnvPosZ:Class;
 64                 [Embed(source="../embeds/skybox/snow_negative_x.jpg")]
 65                 private var EnvNegX:Class;
 66                 [Embed(source="../embeds/skybox/snow_negative_y.jpg")]
 67                 private var EnvNegY:Class;
 68                 [Embed(source="../embeds/skybox/snow_negative_z.jpg")]
 69                 private var EnvNegZ:Class;
 70  
 71                 //engine variables
 72                 private var _view:View3D;
 73  
 74                 //scene objects
 75                 private var _skyBox:SkyBox; 
 76                 private var _torus:Mesh;
 77  
 78                 /**
 79                  * Constructor
 80                  */
 81                 public function Basic_SkyBox()
 82                 {
 83                         stage.scaleMode = StageScaleMode.NO_SCALE;
 84                         stage.align = StageAlign.TOP_LEFT;
 85  
 86                         //setup the view
 87                         _view = new View3D();
 88                         addChild(_view);
 89  
 90                         //setup the camera
 91                         _view.camera.z = -600;
 92                         _view.camera.y = 0;
 93                         _view.camera.lookAt(new Vector3D());
 94                         _view.camera.lens = new PerspectiveLens(90);
 95  
 96                         //setup the cube texture
 97                         var cubeTexture:BitmapCubeTexture = new BitmapCubeTexture(Cast.bitmapData(EnvPosX), Cast.bitmapData(EnvNegX), Cast.bitmapData(EnvPosY), Cast.bitmapData(EnvNegY), Cast.bitmapData(EnvPosZ), Cast.bitmapData(EnvNegZ));
 98  
 99                         //setup the environment map material
100                         var material:ColorMaterial = new ColorMaterial(0xFFFFFF, 1);
101                         material.specular = 0.5;
102                         material.ambient = 0.25;
103                         material.ambientColor = 0x111199;
104                         material.ambient = 1;
105                         material.addMethod(new EnvMapMethod(cubeTexture, 1));
106  
107                         //setup the scene
108                         _torus = new Mesh(new TorusGeometry(150, 60, 40, 20), material);
109                         _view.scene.addChild(_torus);
110  
111                         _skyBox = new SkyBox(cubeTexture);
112                         _view.scene.addChild(_skyBox);
113  
114                         //setup the render loop
115                         addEventListener(Event.ENTER_FRAME, _onEnterFrame);
116                         stage.addEventListener(Event.RESIZE, onResize);
117                         onResize();
118                 }
119  
120                 /**
121                  * render loop
122                  */
123                 private function _onEnterFrame(e:Event):void
124                 {
125                         _torus.rotationX += 2;
126                         _torus.rotationY += 1;
127  
128                         _view.camera.position = new Vector3D();
129                         _view.camera.rotationY += 0.5*(stage.mouseX-stage.stageWidth/2)/800;
130                         _view.camera.moveBackward(600);
131  
132                         _view.render();
133                 }
134  
135                 /**
136                  * stage listener for resize events
137                  */
138                 private function onResize(event:Event = null):void
139                 {
140                         _view.width = stage.stageWidth;
141                         _view.height = stage.stageHeight;
142                 }
143         }
144 }
原文地址:https://www.cnblogs.com/hisiqi/p/2951641.html