2010年7月14日 星期三

Away3D 雙面貼圖

Away3D 雙面貼圖



http://tmp.webco.tw/flash/sphereMapping/sphereMap.swf


產生PNG格式的透明圖片 做為貼圖





AS3 的程式碼



package {

import flash.display.*;

import flash.events.*;
import away3d.core.base.*;
import away3d.core.math.*;
import away3d.containers.*;
import away3d.primitives.*;
import away3d.cameras.*;
import away3d.core.render.*;
import away3d.materials.*;
import away3d.core.utils.*;

import flash.display.*;


public class sphereMap extends Sprite {

var view:View3D;
var sphereA:Sphere;

static var preX = 0;
static var preY = 0;

var globe;


[Embed(source="img/earth512.png")] private var EarthImage:Class;
     private var earthBitmap:Bitmap = new EarthImage();

[Embed(source="img/earthmask.png")] private var EarthMaskImage:Class;
     private var earthMask:Bitmap = new EarthMaskImage();

public function sphereMap() {

createscene();
addEventListener(Event.ENTER_FRAME,loop);
}

function createscene():void {

//建立一個 View 如果一個視窗
addChild(view=new View3D({x:300, y:200}));


//create material
//globe.material = new BitmapMaterial(Cast.bitmap(earthBitmap));

var earthMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(earthBitmap));
//earthMaterial.alpha = 1;
//earthMaterial.blendMode = 'alpha';

var earthMaskMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(earthMask));
//earthMaskMaterial.showNormals = true;
earthMaskMaterial.smooth = true;
//earthMaskMaterial.blendMode = 'invert';
//earthMaskMaterial.alpha = 0;
//var cmat =new CompositeMaterial();
//cmat.
var _compositeMaterial = new CompositeMaterial();
            //_compositeMaterial.addMaterial(earthMaterial);
            _compositeMaterial.addMaterial(earthMaskMaterial);


//建立一個球體
//sphereA=new Sphere({material:new WireColorMaterial()});
sphereA = new Sphere({material:_compositeMaterial,radius:100,segmentsW:20,segmentsH:20});
sphereA.alpha = 0;
sphereA.bothsides = true;

//將球體加入場景
view.scene.addChild(sphereA);
}


function loop(event:Event):void {
var offset = 0.2;


if(preX==0) preX = mouseX;
if(preY==0) preY = mouseY;

var offsetX = mouseX -preX;
var offsetY = mouseY - preY;
view.camera.moveTo(sphereA.x,sphereA.y,sphereA.z);

//view.camera.rotationY=mouseX/2;
view.camera.rotationX=mouseY/2/5;
view.camera.moveBackward(500);


if(offsetX!=0||offsetY!=0){
//trace(offsetX, offsetY);
sphereA.rotationY+=offsetX/1;
//view.camera.rotationX+=offsetY/10;
//trace(sphereA.rotationY, sphereA.rotationX);

}else{
sphereA.rotationY+=  offset;

}

view.render();

preX = mouseX;
preY = mouseY;

}
}
}

沒有留言: