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) The Away Foundation http://www.theawayfoundation.org 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 }