Rendering of 3d colorful sphere rotation using Flash:

Step-1: Open a new document in flash is about the size of 400 X 410.

Step-2:In layer-1 type the text which ever you want. Here I gave “Move the Mouse over the surface  to rotate the sphere”.


Step-3:Create layer-2 and write the following action script in keyframe-1(F9).

var boardSize:Number=350;
var cubeSize:Number=Math.round(boardSize/3);
var rotateFactor:Number=1.2*320/boardSize;
var fLen:Number=2000;
var spBoard:Sprite=new Sprite();
this.addChild(spBoard);
spBoard.x=200;
spBoard.y=200;
var shBack:Shape=new Shape();
spBoard.addChild(shBack);
drawBack();
var shCube:Shape=new Shape();
spBoard.addChild(shCube);
var doRotate:Boolean = false;
var prevX:Number;
var prevY:Number;
var curTheta:Number=-10;
var curPhi:Number=60;
var examplesTot:uint=11;
var exampleNum:uint=3;
var nMesh:Number=40;
var tilesArray:Array=[];
var firstRun:Boolean=true;
var tilesColors:Array=[];
function drawBack():void {
shBack.graphics.lineStyle(2,0xFFFFFF);
shBack.graphics.beginFill(0x000000);
shBack.graphics.drawRect(-boardSize/2,-boardSize/2,boardSize,boardSize);
shBack.graphics.endFill();
}
setTilesArray();
renderView(curTheta,curPhi);
function setTilesArray():void {
var i:int;
var j:int;
var istep:Number;
var jstep:Number;
var tmin:Number;
var tmax:Number;
var smin:Number;
var smax:Number;
var curt:Number;
var curs:Number;
shCube.graphics.clear();
tilesArray=[];
tilesColors=[];
firstRun=true;
curTheta=-10;
curPhi=90;
if(exampleNum==3){
tmin=0;
tmax=1.5*Math.PI;
smin=0;
smax=2*Math.PI;
}
istep=(smax-smin)/nMesh;
jstep=(tmax-tmin)/nMesh;
for(i=0;i<=nMesh;i++){
curs=istep*i+smin;
tilesArray[i]=[];
for(j=0;j<=nMesh;j++){
curt=jstep*j+tmin;
if(exampleNum==3){
tilesArray[i][j]=[cubeSize/4.3*Math.cos(curs)*(2+Math.cos(curt)*2),cubeSize/4.3*Math.sin(curs)*(2+Math.cos(curt)*2),cubeSize/2.6*(3-curt)-0.3*cubeSize];
}
}
}
}
function renderView(t:Number,p:Number):void {
var i:int;
var j:int;
var n:int;
var distArray=[];
var dispArray=[];
var tilesNewArray=[];
var midPoint:Array=[];
var dist:Number;
var depLen:Number;
var coloFactor=100/cubeSize;
t=t*Math.PI/180;
p=p*Math.PI/90;
shCube.graphics.clear();
for(i=0;i<=nMesh;i++){
tilesNewArray[i]=[];
for(j=0;j<=nMesh;j++){
tilesNewArray[i][j]=pointNewView(tilesArray[i][j],t,p);
}
}
for(i=0;i<nMesh;i++){
if(firstRun){
tilesColors[i]=[];
}
for(j=0;j<nMesh;j++){
midPoint[0]=(tilesNewArray[i][j][0]+tilesNewArray[i+1][j][0]+tilesNewArray[i][j+1][0]+tilesNewArray[i+1][j+1][0])/4;
midPoint[1]=(tilesNewArray[i][j][1]+tilesNewArray[i+1][j][1]+tilesNewArray[i][j+1][1]+tilesNewArray[i+1][j+1][1])/4;
midPoint[2]=(tilesNewArray[i][j][2]+tilesNewArray[i+1][j][2]+tilesNewArray[i][j+1][2]+tilesNewArray[i+1][j+1][2])/4;
dist=Math.sqrt(Math.pow(fLen-midPoint[0],2)+Math.pow(midPoint[1],2)+Math.pow(midPoint[2],2));
distArray.push([dist,i,j]);
if(firstRun){
tilesColors[i][j]=combineRGB((2*coloFactor*midPoint[2]+100)*0.8+70,(2*coloFactor*midPoint[1]+100)*0.8+70,(2*coloFactor*midPoint[0]+100)*0.8+70);
}
}
}
distArray.sort(byDist);
for(i=0;i<=nMesh;i++){
dispArray[i]=[];
for(j=0;j<=nMesh;j++){
dispArray[i][j]=[fLen/(fLen-tilesNewArray[i][j][0])*tilesNewArray[i][j][1],-fLen/(fLen-tilesNewArray[i][j][0])*tilesNewArray[i][j][2]];
}
}
depLen=distArray.length;
shCube.graphics.lineStyle(1,0×111111);
for(n=0;n<depLen;n++){
i=distArray[n][1];
j=distArray[n][2];
shCube.graphics.moveTo(dispArray[i][j][0],dispArray[i][j][1]);
shCube.graphics.beginFill(tilesColors[i][j],1.0);
shCube.graphics.lineTo(dispArray[i][j+1][0],dispArray[i][j+1][1]);
shCube.graphics.lineTo(dispArray[i+1][j+1][0],dispArray[i+1][j+1][1]);
shCube.graphics.lineTo(dispArray[i+1][j][0],dispArray[i+1][j][1]);
shCube.graphics.lineTo(dispArray[i][j][0],dispArray[i][j][1]);
shCube.graphics.endFill();
}
firstRun=false;
}
function byDist(v:Array,w:Array):Number {
if (v[0]>w[0]){
return -1;
} else if (v[0]<w[0]){
return 1;
} else {
return 0;
}
}
function pointNewView(v:Array,theta:Number,phi:Number):Array {
var newCoords:Array=[];
newCoords[0]=v[0]*Math.cos(theta)*Math.sin(phi)+v[1]*Math.sin(theta)*Math.sin(phi)+v[2]*Math.cos(phi);
newCoords[1]=-v[0]*Math.sin(theta)+v[1]*Math.cos(theta);
newCoords[2]=-v[0]*Math.cos(theta)*Math.cos(phi)-v[1]*Math.sin(theta)*Math.cos(phi)+v[2]*Math.sin(phi);
return newCoords;
}
function combineRGB(red:Number,green:Number,blue:Number):Number {
var RGB:Number;
if(red>255){red=255;}
if(green>255){green=255;}
if(blue>255){blue=255;}
if(red<0){red=0;}
if(green<0){green=0;}
if(blue<0){blue=0;}
RGB=(red<<16) | (green<<8) | (blue<<0);
return RGB;
}
spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut);
spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
spBoard.addEventListener(MouseEvent.ROLL_OVER,boardOver);
function boardOut(e:MouseEvent):void {
doRotate=false;
}
function boardOver(e:MouseEvent):void {
prevX=spBoard.mouseX;
prevY=spBoard.mouseY;
doRotate=true;
}
function boardMove(e:MouseEvent):void {
var locX:Number=prevX;
var locY:Number=prevY;
if(doRotate){
prevX=spBoard.mouseX;
prevY=spBoard.mouseY;
curTheta+=(prevX-locX)*rotateFactor;
curPhi+=(prevY-locY)*rotateFactor;
renderView(curTheta,curPhi);
e.updateAfterEvent();
}
}

Now run the program.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *