基於flash的360虛擬現實引擎(cubicVR)原始碼及其實現原理
來源:互聯網
上載者:User
flash 360度3D虛擬實境(cubicVR)原始碼及其實現原理
flash的3D虛擬實境最早源自於德國的flash圖形學大牛andre.michelle在其labsite:lab.andre-michelle.com上發表了一篇關於虛擬實境的文章,並提供了一個執行個體(NaN sourcecode).為了深入虛擬實境,在接下來的2個月裡我陸續找到一些cubicVR的源檔案並嘗試破譯了一些源碼,我同時嘗試開發了一個簡單的flash3D引擎以及圖片3D展開演算法,所有這些的思想均來自於andre-michelle在其日誌中提到的一些idea.現在在這篇文章中所用的執行個體依然是我對andre早期版本的整理,雖然目前我開發的新版本有更好的結構以及執行效率,但此版本的編程思想更通俗易懂。我想利用這個假期時間開發出flash球型虛擬實境,使它變得更真實。
在flash裡部署3D虛擬實境無疑是一個革命性進步,我們再也不用去安裝java虛擬機器和QTVR了,事實上,在基於flash的可程式化向量圖形介面基礎上開發出來的虛擬現實可以通過創新的使用者介面以達到完美的使用者體驗。city8.com既採用這種技術來部署他們的全景體驗城市地圖。
原理:
1.1點與投影
通過1點來產生遠小近大的變化以產生空間感,在cubicVR中,設想觀察者camera在一個正六面體中央,通過一點來計算每個面的在空間投影中的位置,以產生效果。
2.細分貼圖
flash的matrix類只支援2D圖形變換:切變、縮放、平面位移。位元影像無法實現3D變換,這與openGL不同,openGL只需要在定義多邊型頂點和貼圖法線,位元影像就會自動貼到一個面片上,而且整個貼圖過程完全由GPU完成。而在flash中必須使用細分貼圖的方法來欺騙人們的眼睛,既:將一張圖片細分為n張小的三角面片,再將小三角面片進行平面切變,正因為如此,flash3D一旦涉及貼圖必然導致大量佔用cpu運算資源。
細分貼圖的demo:http://godson./archives/2006/3708.shtml
3.消隱演算法
如果沒有消隱演算法,那麼觀察者只能看到離螢幕最近的面,而不能看到整個3D空間,理論上消隱演算法有3個步驟:1在一個六面體中,面法線與視錐體夾角大於180度則應設為不可見,2在同一個像素上,只顯示與camera最近的多邊形(Zbuffer演算法),3投影在螢幕以外的多邊型不可見,但這三種演算法非常佔用cpu資源,andrew設計了一種更簡單的演算法:1多邊形的中心與camera重合,在camera座標系中Z值<0的多邊形不可見,2投影在螢幕以外的多邊型不可見。
原始碼:
--------------------------------渲染部分------------------------------------
//讀入位元影像資料,將其存放在數組中
var bitmapAry = new Array();
var bitmapSource = flash.display.BitmapData.loadBitmap("data2");
for (i=0; i<6; i++) {
var face = new flash.display.BitmapData(bitmapSource.width, bitmapSource.width, false);
face.copyPixels(bitmapSource, new flash.geom.Rectangle(0, bitmapSource.width*i, bitmapSource.width, bitmapSource.width*(i+1)), new flash.geom.Point(0, 0));
bitmapAry.push(face);
}
var showLine = true;
var inBitmapWidth = 1/bitmapAry[0].width;
var inBitmapHeight = 1/bitmapAry[0].height;
var bitmapWidth = bitmapAry[0].width;
var bitmapHeight = bitmapAry[0].height;
//滑鼠按下時bitmap的變換距陣
this.bitmapMtrx = new flash.geom.Matrix();
var subBitmapMtix = new flash.geom.Matrix();
//舞台寬度變數申明
StWidth = Stage.width;
StHeight = Stage.height;
ox = StWidth/2;
oy = StHeight/2;
//焦距
focus = 300;
//攝影機角度變數
angU = 0;
angV = 0;
//細分段數
subdiv = 9;
subdivPic = subdiv*subdiv;
//
var subdivVy = new Array();
var subdivVx = new Array();
var subdivVz = new Array();
var subdivX = new Array();
var subdivY = new Array();
var subdivEnable = new Array();
var mtrx = new Array();
var mtrx2 = new Array();
//細分bitmap在map內部的X和Y
var subdivMapX = new Array();
var subdivMapY = new Array();
var subdivWidth = bitmapAry[0].width/(subdiv-1);
var subdivHeight = bitmapAry[0].height/(subdiv-1);
for (var i = 0; i<subdiv; i++) {
for (var j = 0; j<subdiv; j++) {
var subID = j+i*subdiv;
subdivMapX.push(j*subdivWidth);
subdivMapY.push(i*subdivHeight);
}
}
//
for (var i = 0; i<subdivPic; i++) {
mtrx.push(new flash.geom.Matrix());
mtrx2.push(new flash.geom.Matrix());
subdivVy.push(new Array(subdivPic));
subdivVx.push(new Array(subdivPic));
subdivVz.push(new Array(subdivPic));
subdivX.push(new Array(subdivPic));
subdivY.push(new Array(subdivPic));
subdivEnable.push(new Array(subdivPic));
}
// end of for
var mtrxSx = subdivWidth/bitmapWidth;
var mtrxSy = subdivHeight/bitmapHeight;
var mtrxSxy = subdivWidth/bitmapHeight;
var mtrxSyx = subdivHeight/bitmapWidth;
for (var i = 0; i<subdiv-1; ++i) {
for (var j = 0; j<subdiv-1; ++j) {
var subID = j+i*subdiv;
var mtrxTx = subdivMapX[subdiv*i+j];
var mtrxTy = subdivMapY[subdiv*i+j];
mtrx[subID].tx = mtrxTx;
mtrx[subID].ty = mtrxTy;
mtrx[subID].a = mtrxSx;
mtrx[subID].b = 0;
mtrx[subID].c = mtrxSxy;
mtrx[subID].d = mtrxSy;
mtrx[subID].invert();
mtrx2[subID].tx = mtrxTx;
mtrx2[subID].ty = mtrxTy;
mtrx2[subID].a = mtrxSx;
mtrx2[subID].b = mtrxSyx;
mtrx2[subID].c = 0;
mtrx2[subID].d = mtrxSy;
mtrx2[subID].invert();
}
}
//
mtrxA = mtrx[0].a;
mtrxB = mtrx[0].b;
mtrxC = mtrx[0].c;
mtrxD = mtrx[0].d;
mtrx2A = mtrx2[0].a;
mtrx2B = mtrx2[0].b;
mtrx2C = mtrx2[0].c;
mtrx2D = mtrx2[0].d;
// 初始每個bitmap細分結點在正則化3D視見體空間中的空間位置
function fun6(bitmapNo, new19, new20, new21, new22, new25, new23, new26, new24, new27) {
new15 = subdiv-1 >> 1;
for (var i = 0; i<subdiv; i++) {
for (var j = 0; j<subdiv; j++) {
var subID = j+i*subdiv;
subdivVy[bitmapNo][subID] = new19*new15+new22*(j-new15)+new25*(i-new15);
subdivVx[bitmapNo][subID] = new20*new15+new23*(j-new15)+new26*(i-new15);
subdivVz[bitmapNo][subID] = new21*new15+new24*(j-new15)+new27*(i-new15);
//trace(subdivVy[bitmapNo][subID]+"/"+subdivVx[bitmapNo][subID]+"/"+subdivVz[bitmapNo][subID]);
}
}
}
fun6(0, 1, 0, 0, 0, 0, -1, 0, 0, -1);
fun6(1, 0, -1, 0, -1, 0, 0, 0, 0, -1);
fun6(2, -1, 0, 0, 0, 0, 1, 0, 0, -1);
fun6(3, 0, 1, 0, 1, 0, 0, 0, 0, -1);
fun6(4, 0, 0, 1, 0, 1, -1, 0, 0, 0);
fun6(5, 0, 0, -1, 0, -1, -1, 0, 0, 0);
// 求每個subdiv的在螢幕座標系的x和y將每個subdiv可見量儲存於subdivEnable中
function fun7(bitmapNo) {
for (var i = 0; i<subdivPic; i++) {
//此為核心部分:細分貼圖頂點在camera座標空間中的座標系變換運算 _loc5為z值,_loc5與_loc6的兩個方程是對空間向量距陣計算的簡化
var _loc6 = cos_angU*subdivVy[bitmapNo][i]+sin_angU*subdivVx[bitmapNo][i];
var _loc5 = cos_angV*_loc6+sin_angV*subdivVz[bitmapNo][i];
//如果頂點在視角前方則進行投影計算
if (_loc5>=0.1) {
var _loc7 = focus/_loc5;
//計算投影的x,y
subdivX[bitmapNo][i] = (sin_angU*subdivVy[bitmapNo][i]-cos_angU*subdivVx[bitmapNo][i])*_loc7+ox;
subdivY[bitmapNo][i] = (sin_angV*_loc6-cos_angV*subdivVz[bitmapNo][i])*_loc7+oy;
if (subdivX[bitmapNo][i]>0 and subdivX[bitmapNo][i]<StWidth and subdivY[bitmapNo][i]>0 and subdivY[bitmapNo][i]<StHeight) {
subdivEnable[bitmapNo][i] = 1;
} else {
subdivEnable[bitmapNo][i] = 0;
}
}
}
}
//
subdivV = subdiv-1;
function render() {
for (var i = 0; i<6; i++) {
fun7(i);
for (var j = 0; j<subdivV; ++j) {
for (var k = 0; k<subdivV; k++) {
var pointA = k+j*subdiv;
var pointC = k+(j+1)*subdiv;
var pointB = pointA+1;
var pointD = pointC+1;
var _loc2 = subdivEnable[i][pointA]+subdivEnable[i][pointD]+subdivEnable[i][pointB];
var _loc3 = subdivEnable[i][pointA]+subdivEnable[i][pointD]+subdivEnable[i][pointC];
//如果細分表面頂點有1個在螢幕內則渲染該面片
if (_loc2>0) {
//細分表面展開距陣計算
subBitmapMtix.a = mtrxA;
subBitmapMtix.b = mtrxB;
subBitmapMtix.c = mtrxC;
subBitmapMtix.d = mtrxD;
subBitmapMtix.tx = mtrx[pointA].tx;
subBitmapMtix.ty = mtrx[pointA].ty;
renderBitmap(i, subdivX[i][pointA], subdivY[i][pointA], subdivX[i][pointB], subdivY[i][pointB], subdivX[i][pointD], subdivY[i][pointD], subBitmapMtix);
}
//如果細分表面頂點有1個在螢幕內則渲染該面片
if (_loc3>0) {
//細分表面展開距陣計算
subBitmapMtix.a = mtrx2A;
subBitmapMtix.b = mtrx2B;
subBitmapMtix.c = mtrx2C;
subBitmapMtix.d = mtrx2D;
subBitmapMtix.tx = mtrx2[pointA].tx;
subBitmapMtix.ty = mtrx2[pointA].ty;
renderBitmap(i, subdivX[i][pointA], subdivY[i][pointA], subdivX[i][pointD], subdivY[i][pointD], subdivX[i][pointC], subdivY[i][pointC], subBitmapMtix);
}
}
}
}
}
//
renderBitmap = function (bitmapNo, point1X, point1Y, point2X, point2Y, point3X, point3Y, subBitmapMtix) {
//細分表面貼圖距陣計算
this.bitmapMtrx.a = (point2X-point1X)*inBitmapWidth;
this.bitmapMtrx.b = (point2Y-point1Y)*inBitmapWidth;
this.bitmapMtrx.c = (point3X-point1X)*inBitmapHeight;
this.bitmapMtrx.d = (point3Y-point1Y)*inBitmapHeight;
this.bitmapMtrx.tx = point1X;
this.bitmapMtrx.ty = point1Y;
subBitmapMtix.concat(this.bitmapMtrx);
//貼圖渲染
this.photo.beginBitmapFill(bitmapAry[bitmapNo], subBitmapMtix, false, false);
if (showLine == true) {
this.photo.lineStyle(1, 0x000000, 100);
}
this.photo.moveTo(point1X, point1Y);
this.photo.lineTo(point2X, point2Y);
this.photo.lineTo(point3X, point3Y);
this.photo.endFill();
};
//初始渲染
photo.clear();
cos_angU = Math.cos(angU);
sin_angU = Math.sin(angU);
cos_angV = Math.cos(angV);
sin_angV = Math.sin(angV);
render();
--------------------------------------滑鼠控制部分---------------------------------
var myMouseListener = new Object();
var myKeyListener = new Object();
this.createEmptyMovieClip("photo", this.getNextHighestDepth());
this.createEmptyMovieClip("win", this.getNextHighestDepth());
lable.swapDepths(photo);
win.lineStyle(2, 0x000000, 100);
win.moveTo(0, 0);
win.lineTo(Stage.width, 0);
win.lineTo(Stage.width, Stage.height);
win.lineTo(0, Stage.height);
win.lineTo(0, 0);
myMouseListener.onMouseDown = function() {
onEnterFrame = function () {
var xWidth = _xmouse-dx;
var yWidth = _ymouse-dy;
if (xWidth != 0 || yWidth != 0) {
photo.clear();
angU = angU-xWidth*0.001;
angV = angV-yWidth*0.001;
cos_angU = Math.cos(angU);
sin_angU = Math.sin(angU);
cos_angV = Math.cos(angV);
sin_angV = Math.sin(angV);
render();
}
};
dx = _xmouse;
dy = _ymouse;
};
myMouseListener.onMouseUp = function() {
onEnterFrame = undefined;
photo.clear();
render();
};
myMouseListener.onMouseWheel = function(wheelMove) {
i = 0;
focus = focus+wheelMove*20;
if (focus<180) {
focus = 180;
}
photo.clear();
render();
};
Mouse.addListener(myMouseListener);