
Код AS3:
package earth {
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.events.MouseEvent3D;
import alternativa.engine3d.core.Object3D;
import alternativa.engine3d.core.Resource;
import alternativa.engine3d.core.View;
import alternativa.engine3d.lights.OmniLight;
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.materials.VertexLightTextureMaterial;
import alternativa.engine3d.objects.Mesh;
import alternativa.engine3d.objects.Sprite3D;
import alternativa.engine3d.primitives.GeoSphere;
import alternativa.engine3d.primitives.Plane;
import alternativa.engine3d.resources.BitmapTextureResource;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.events.Event;
/**
* ...
* @author stweet
* @mail stweetart@gmail.com
*/
public class AEarth extends Sprite {
private var st3D:Stage3D;
private var ca3D:Camera3D;
private var rc3D:Object3D;
private var rx3D:Object3D;
private var ry3D:Object3D;
private var gw3D:Mesh;
private var sp3D:GeoSphere;
private var fx3D:GeoSphere;
private var sb3D:Plane;
private var ol3D:OmniLight;
private var sm3D:VertexLightTextureMaterial;
private var cx3D:BitmapTextureResource;
private var op3D:BitmapTextureResource;
private var mp3D:TextureMaterial;
private var boolean:Boolean = false;
private var zoom:int = -650;
private var zoomMax:int = -400;
private var zoomMin:int = -650;
private var xPos:Number = 0;
private var yPos:Number = 0;
private var xMouse:Number = 0;
private var yMouse:Number = 0;
private var speedRotation:Number = 0.08;
private var speedRotationMax:Number = 0.18;
private var speedRotationMin:Number = 0.08;
public function AEarth() {
addEventListener(Event.ADDED_TO_STAGE, ADD_TO_STAGE);
}
private function ADD_TO_STAGE(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, ADD_TO_STAGE);
rc3D = new Object3D();
rx3D = new Object3D();
ry3D = new Object3D();
ca3D = new Camera3D(0.1, 1000);
ca3D.rotationX = Math.PI / 180 * -90;
ca3D.y = zoom;
ca3D.view = new View(stage.stageWidth, stage.stageHeight);
ca3D.view.backgroundAlpha = 0;
ca3D.view.hideLogo();
sm3D = new VertexLightTextureMaterial(new BitmapTextureResource(ParseXML.getGraphics("earth")));
sp3D = new GeoSphere(300, 12, false, sm3D);
sp3D.addEventListener(MouseEvent3D.MOUSE_DOWN, MOUSE_DOWN);
sp3D.addEventListener(MouseEvent3D.MOUSE_OVER, MOUSE_OVER);
sp3D.addEventListener(MouseEvent3D.MOUSE_OUT, MOUSE_OUT);
op3D = new BitmapTextureResource(ParseXML.getGraphics("mask"));
cx3D = new BitmapTextureResource(new BitmapData(2048, 1024, false, 0xF0F8FF));
mp3D = new TextureMaterial(cx3D, op3D);
mp3D.alphaThreshold = 1;
mp3D.alpha = 0.5;
fx3D = new GeoSphere(301, 12, false, mp3D);
fx3D.mouseEnabled = false;
sb3D = new Plane(1600, 1600, 1, 1, false, false, null, new TextureMaterial(new BitmapTextureResource(ParseXML.getGraphics("sky"))));
sb3D.rotationX = 1.6;
sb3D.y = 300;
var map:TextureMaterial = new TextureMaterial(new BitmapTextureResource(new BitmapData(1024, 1024, false, 0xF0F8FF)), new BitmapTextureResource(ParseXML.getGraphics("glow")));
map.alphaThreshold = 1;
map.alpha = 0.5;
gw3D = ParseXML.getObjects("glow");
gw3D.setMaterialToAllSurfaces(map);
gw3D.mouseEnabled = false;
ol3D = new OmniLight(0xFFFFFF, 10, 8000);
ol3D.intensity = 2.5;
ol3D.x = -1000;
ol3D.y = -4000;
ol3D.z = 1000;
ry3D.addChild(sp3D);
ry3D.addChild(fx3D);
rx3D.addChild(ry3D);
rc3D.addChild(sb3D);
rc3D.addChild(rx3D);
rc3D.addChild(ca3D);
rc3D.addChild(gw3D);
rc3D.addChild(ol3D);
st3D = stage.stage3Ds[0];
st3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
st3D.requestContext3D();
addChild(ca3D.view);
}
private function MOUSE_DOWN(e:MouseEvent3D):void {
stage.addEventListener(MouseEvent.MOUSE_UP, MOUSE_UP);
xMouse = mouseX;
yMouse = mouseY;
boolean = true;
}
private function MOUSE_UP(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, MOUSE_UP);
boolean = false;
}
private function MOUSE_OVER(e:MouseEvent3D):void {
sp3D.addEventListener(MouseEvent3D.MOUSE_WHEEL, MOUSE_WHEEL);
}
private function MOUSE_OUT(e:MouseEvent3D):void {
sp3D.removeEventListener(MouseEvent3D.MOUSE_WHEEL, MOUSE_WHEEL);
}
private function MOUSE_WHEEL(e:MouseEvent3D):void {
if (e.delta < 0) {
zoom -= 50;
speedRotation -= 0.02;
} else {
zoom += 50;
speedRotation += 0.02;
}
if (speedRotation > speedRotationMax) speedRotation = speedRotationMax;
if (speedRotation < speedRotationMin) speedRotation = speedRotationMin;
}
private function onContextCreate(e:Event):void {
for each (var key:Resource in rc3D.getResources(true))
key.upload(st3D.context3D);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, onResize);
}
private function onResize(e:Event):void {
ca3D.view.width = stage.stageWidth;
ca3D.view.height = stage.stageHeight;
}
private function onEnterFrame(e:Event):void {
ca3D.render(st3D);
fx3D.rotationZ += 0.0002;
if (zoom < zoomMin) zoom = zoomMin;
if (zoom > zoomMax) zoom = zoomMax;
if (yPos != 0) yPos -= yPos * speedRotation;
if (xPos != 0) xPos -= xPos * speedRotation;
if (zoom != ca3D.y) ca3D.y += (zoom - ca3D.y) * 0.05;
if (yPos != ry3D.rotationZ) ry3D.rotationZ -= yPos * 0.0005;
if (xPos != rx3D.rotationX) rx3D.rotationX -= xPos * 0.0005;
if (rx3D.rotationX < -1.6) rx3D.rotationX = -1.6;
if (rx3D.rotationX > 1.6) rx3D.rotationX = 1.6;
if (boolean) {
if (xMouse != mouseX) yPos += xMouse - mouseX;
if (yMouse != mouseY) xPos += yMouse - mouseY;
xMouse = mouseX;
yMouse = mouseY;
}
}
}
}