3d Earth with clouds alternativa 8

One of my attempts to recreate some code from wonderfl ended in this pretty neat earth example. The original code consisted of the earth spinning in daylight on one side and the earth in darkness on the other showing the night lights on the earth. The example I was recreating from used just as3 and AGAL no external 3d libraries. I put together a close example of it but couldn’t quite figure out how to setup the night lights on the earth without creating the extra AGAL code somewhere. earth

package {
    	
    	import alternativa.engine3d.core.Camera3D;
    	import alternativa.engine3d.core.Object3D;
    	import alternativa.engine3d.core.View;
    	import alternativa.engine3d.materials.FillMaterial;
    	import alternativa.engine3d.primitives.GeoSphere;
    	import alternativa.engine3d.core.Resource;
    	import alternativa.engine3d.materials.*;
    	import alternativa.engine3d.resources.BitmapTextureResource;
    	import alternativa.engine3d.lights.*;
    	
    	import flash.display.Sprite;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.display.Stage3D;
    	import flash.display.StageAlign;
    	import flash.display.StageScaleMode;
    	import flash.geom.Vector3D;
    	import flash.geom.Matrix3D;
    	import flash.display.BitmapData;
    	
    	[SWF(backgroundColor="#000000", frameRate="60", width="800", height="600")]
    	
    	public class world extends Sprite
    	{
    		[Embed(source="world_diffuse.jpg")] static private const wdiff:Class;
    		private var wdiffmap:BitmapTextureResource = new BitmapTextureResource(new wdiff().bitmapData);  
    		
    		[Embed(source="world_normal.jpg")] static private const wnorm:Class;
    		private var wnormmap:BitmapTextureResource = new BitmapTextureResource(new wnorm().bitmapData);  
    		
    		[Embed(source="world_specular.jpg")] static private const wspec:Class;
    		private var wspecmap:BitmapTextureResource = new BitmapTextureResource(new wspec().bitmapData);  
    		
    		[Embed(source="world_lights.jpg")] static private const wlights:Class;
    		private var wlightsmap:BitmapTextureResource = new BitmapTextureResource(new wlights().bitmapData);  
    		
    		[Embed(source="clouds.png")] static private const clouds:Class;
    		private var cloudsmap:BitmapTextureResource = new BitmapTextureResource(new clouds().bitmapData);  
    		
    		private var normalMapData:BitmapData = new BitmapData(1,1,false,0x7f7fff);
    		private var dummynormalMap:BitmapTextureResource = new BitmapTextureResource(normalMapData); 
    		
    		private var specMapData:BitmapData = new BitmapData(1,1,false,0x000000);
    		private var dummyspecularMap:BitmapTextureResource = new BitmapTextureResource(specMapData);  
    		
    		private var scene:Object3D = new Object3D();
    		private var camera:Camera3D;
    		private var stage3D:Stage3D;
    		private var earthcontainer:Object3D = new Object3D();
    		private var earth:GeoSphere;
    		private var cloud:GeoSphere;
    		private var dl:DirectionalLight;
    		
    		private var press:Boolean =false;
    		private var rvx:Number =0;
                    private var rvy:Number =0;
    		private var tz:Number =0;
                    private var rx:Number =0;
                    private var ry:Number =0;
    		private var pmouseX:Number =0;
                    private var pmouseY:Number =0;
    		
    		public function world():void
    		{
    			this.addEventListener(Event.ADDED_TO_STAGE,onAdd);
    		}
    		
    		private function onAdd(e:Event):void
    		{
    			this.removeEventListener(Event.ADDED_TO_STAGE,onAdd);
    			
    			stage.addChild(new Output());
    			
    			stage.align = StageAlign.TOP_LEFT;
    			stage.scaleMode = StageScaleMode.NO_SCALE;
    			
    			camera = new Camera3D(1, 100000);
    			camera.x = -300;
    			
    			var view:View = new View(stage.stageWidth, stage.stageHeight, false, 0, 0, 4);
    			view.hideLogo();
    			camera.view = view;
    			addChild(camera.view);
    			addChild(camera.diagram);
    			camera.debug = true;
    			scene.addChild(camera);
    			
    			camera.rotationX = -90*Math.PI/180;
    			camera.rotationY = 0;
    			camera.rotationZ = -90*Math.PI/180;
    			
    			dl = new DirectionalLight(0xFFFFFF);
    			dl.x = -1000;
                            dl.intensity = 1;
    			dl.lookAt(0, 0, 0);
    			earthcontainer.addChild(dl);
    			
    			stage3D = stage.stage3Ds[0];
    			stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
    			stage3D.requestContext3D();
    		}
    		
    		private function onContextCreate(e:Event):void {
    			stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
    			
    			init();
    			
    			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    			stage.addEventListener(Event.RESIZE, onResize);
    			onResize();
    		}
    		
    		private function onMouseDown(e:MouseEvent):void { press=true; }
    		private function onMouseUp(e:MouseEvent):void { press=false; }
    		
    		private function init():void
    		{
    			var mat1:StandardMaterial = new StandardMaterial(wdiffmap, wnormmap, wspecmap, null, null);
    			mat1.specularPower = 0.1;
    			earth = new GeoSphere(100, 5, false, mat1);
    			uploadResources(earth.getResources(true));
    			earthcontainer.addChild(earth);
    						
    			var mat3:StandardMaterial = new StandardMaterial(cloudsmap, dummynormalMap, dummyspecularMap, null, null);
    			mat3.alphaThreshold = 1;
    			mat3.alpha = 0.7;
    			mat3.transparentPass = true
    			mat3.specularPower = 0;
    			cloud = new GeoSphere(102, 5, false, mat3);
    			uploadResources(cloud.getResources(true));
    			earthcontainer.addChild(cloud);
    			
    			scene.addChild(earthcontainer);
    		}
    		
    		private function onEnterFrame(e:Event=null):void
    		{
    			if (press) {
                                rvx -= (mouseY - pmouseY) * 0.0625;
                                rvy += (mouseX - pmouseX) * 0.0625;
                            }
    			pmouseX = mouseX;
                            pmouseY = mouseY;
    			
    			rvx *= 0.9;
                            rvy *= 0.9;
                            rx += rvx;
                            ry += rvy;
                            if (rx > 90) rx += (90 - rx) * 0.5;
                            if (rx < -90) rx += ( -90 - rx) * 0.5;
    			var mdl:Matrix3D = new Matrix3D();
                            mdl.prependRotation(rx, Vector3D.Y_AXIS);
                            mdl.prependRotation(ry, Vector3D.Z_AXIS);
    
    			earthcontainer.matrix = mdl;
    			
    			cloud.rotationZ=cloud.rotationZ-0.001;
    			dl.rotationZ=dl.rotationZ+0.001;
    			camera.render(stage3D);
    		}
    		
    		private function onResize(e:Event = null):void {
    			camera.view.width = stage.stageWidth;
    			camera.view.height = stage.stageHeight;
    			onEnterFrame();
    		}
    		
    		private function uploadResources(resources:Vector.):void {
    			for each (var resource:Resource in resources) {
    				resource.upload(stage3D.context3D);
    			}
    		}
    	}
}

You can download the source files here

Downloads

Comments

  • avatar-firos
    # firos
    Hi very nice out put. Which version of Alternativa3d you use here. I have tried with 8.27 but didn’t worked. Thanks Firos
  • avatar-davidejones
    # davidejones
    if you right click on the flash demo it shows you the version, this was made with 8.30, if you post any errors it says or what happens i can try assist you in getting it to work for 8.27

Comments are currently closed