package { import away3d.containers.View3D; import away3d.containers.Scene3D; import away3d.materials.ColorMaterial; import away3d.materials.MovieMaterial; import away3d.materials.WireColorMaterial; import away3d.core.math.Number3D; import away3d.primitives.Cube; import away3d.primitives.Plane; import away3d.primitives.Cylinder; import away3d.core.render.Renderer; import away3d.loaders.Collada; import away3d.loaders.Loader3D; import away3d.containers.ObjectContainer3D; import away3d.cameras.Camera3D; import away3d.core.base.*; import away3d.core.utils.Cast; import flash.display.Sprite; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; [SWF(width="500", height="400", frameRate="40", backgroundColor="#777777")] public class Water extends Sprite { private static const ORBITAL_RADIUS:Number = 150; private static const CAMERA_ORBIT:Number = 600; private var scene:Scene3D; private var surfaceMovie:Class; private var view:View3D; private var ocean:Cube; private var doRotation:Boolean = false; private var lastMouseX:int; private var lastMouseY:int; private var camera:Camera3D; private var cameraPitch:Number = 60; private var cameraYaw:Number = -60; public function Water() { buildWorld(); //create a rig and put it on the stage var loader:Loader3D = Collada.load("rig.dae"); //create two objectcontainer3D's var ocs:ObjectContainer3D = new ObjectContainer3D(); var oilRig:ObjectContainer3D = new ObjectContainer3D(); //Materials var rigColor:ColorMaterial = new ColorMaterial(0x660088); var platColor:ColorMaterial = new ColorMaterial(0x001144); var seaColor:ColorMaterial = new ColorMaterial(0x007777,alpha = 0.8); var materialMovie:MovieClip = new Surface() as MovieClip; var bedMaterialMovie = new Caustic() as MovieClip; var surfaceMaterial:MovieMaterial = new MovieMaterial(materialMovie); var seabedMaterial:MovieMaterial = new MovieMaterial(bedMaterialMovie); // Listen to mouse up and down events on the stage addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); addEventListener(MouseEvent.MOUSE_UP, onMouseUp); // create a cube and put it on stage ocean = new Cube({width:300,height:70,depth:300,bothsides:true}); loader.scale(45); loader.y = -15; loader.yaw(90); oilRig.addChild(loader); //add the collada to the oil rig 3D object var mat:ColorMaterial = new ColorMaterial(); mat.color = 0x228800; //mat.alpha = 1; //mat.wirecolor = 0x228800; var support:Cylinder = new Cylinder({material:platColor, radius:40, height:270}); oilRig.addChild(support); support.y = 20; support.segmentsW = 10; support.segmentsH = 8; support.material = platColor; var deck1:Plane = new Plane({width:200, depth:200, bothsides:true}); deck1.material = mat; deck1.moveUp(80); oilRig.addChild(deck1); var deck2:Plane = new Plane({width:200, depth:200, bothsides:true}); deck2.material = mat; deck2.moveUp(90); oilRig.addChild(deck2); var deck3:Plane = new Plane({width:200, depth:200, bothsides:true}); deck3.material = mat; deck3.moveUp(100); oilRig.addChild(deck3); var accom = new Cube({width:55, height:40, depth:80, bothsides:true}); accom.material = rigColor; accom.moveUp(120); accom.moveLeft(70); oilRig.addChild(accom); var util = new Cube({width:30, height:60, depth:40, bothsides:true}); util.material = rigColor; util.moveUp(100); oilRig.addChild(util); var proc = new Cube({width:40, height:30, depth:80, bothsides:true}); proc.material = rigColor; proc.moveUp(115); proc.moveRight(60); oilRig.addChild(proc); var body = new Cube({width:180, height:18, depth:80, bothsides:true}); body.material = rigColor; body.moveUp(90); //proc.moveRight(100); oilRig.addChild(body); // Set one of the cubeMaterials to a partially transparent white material ocean.cubeMaterials.top = surfaceMaterial; ocean.cubeMaterials.bottom = seabedMaterial; ocean.cubeMaterials.right = seaColor; ocean.cubeMaterials.left = seaColor; ocean.cubeMaterials.back = seaColor; ocean.cubeMaterials.front = seaColor; ocs.addChild(ocean); ocs.addChild(oilRig); ocs.scale(0.7); view.scene.addChild(ocs); // render on enterframe addEventListener(Event.ENTER_FRAME,render); } private function buildWorld():void { // Create a new scene where all the 3D object will be rendered scene = new Scene3D(); // Create a new camera, passing some initialisation parameters camera = new Camera3D({zoom:25, focus:30}); setCameraPosition(); // Create a new view that encapsulates the scene and the camera view = new View3D({scene:scene, camera:camera, renderer:Renderer.CORRECT_Z_ORDER}); // center the viewport to the middle of the stage view.x = stage.stageWidth / 2; view.y = stage.stageHeight / 2; //view.renderer = Renderer.CORRECT_Z_ORDER; addChild(view); } private function render(e:Event):void{ updateCamera(); view.render(); } // Each frame called and updates the camera position private function updateCamera():void { // If the mouse button has been clicked then update the camera position if (doRotation) { // convert the change in mouse position into a change in camera angle var dragPitch:Number = (mouseY - lastMouseY) / 2; var dragYaw:Number = (mouseX - lastMouseX) / 2; // update the camera angles cameraPitch -= dragPitch; cameraYaw -= dragYaw; // limit the pitch of the camera if (cameraPitch <= 0) { cameraPitch = 0.1; } else if (cameraPitch >= 180) { cameraPitch = 179.9; } // reset the last mouse position lastMouseX = mouseX; lastMouseY = mouseY; // reposition the camera setCameraPosition(); } } // sets the camera position given pitch and yaw angles private function setCameraPosition():void { camera.y = CAMERA_ORBIT * Math.cos(cameraPitch * Math.PI / 180); camera.x = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.cos(cameraYaw * Math.PI / 180); camera.z = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.sin(cameraYaw * Math.PI / 180); // keep the camera looking at the origin camera.lookAt(new Number3D(0, 0, 0)); } // called when mouse down on stage private function onMouseDown(event:MouseEvent):void { doRotation = true; lastMouseX = event.stageX; lastMouseY = event.stageY; } // called when mouse up on stage private function onMouseUp(event:MouseEvent):void { doRotation = false; } } }