(轉)使用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 }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM