Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code is a modification of code from
- // http://www.flashandmath.com/flashcs4/cs4simple3d/index.html
- // http://stackoverflow.com/questions/1317992/how-to-rotate-gradient-along-with-sides-of-3d-cube-un-flash-actionscropt-3-0
- import fl.events.SliderEvent;
- var numVertices:int = 8;
- var numFaces:int = 6;
- var objRad:Number = 70;
- var vertsVec:Vector.<Vector3D> = new Vector.<Vector3D>();
- var facesVec:Vector.<Array> = new Vector.<Array>();
- //'fLen' is reponsible for perspective distortion: the larger fLen the less distortion.
- var fLen:Number = 500;
- //We adjust the initial position of the slider that changes perspective.
- perspSlider.value = fLen;
- var spBoard:Sprite = new Sprite();
- this.addChild(spBoard);
- spBoard.x = 180;
- spBoard.y = 205;
- spBoard.filters = [ new DropShadowFilter() ];
- //shBack is the black background drawn by the function 'drawBack'.
- var shBack:Shape = new Shape();
- spBoard.addChild(shBack);
- drawBack();
- var spObject:Sprite = new Sprite();
- var spObjImage:Sprite = new Sprite();
- spBoard.addChild(spObjImage);
- spObject.rotationX = 0;
- spObject.rotationY = 0;
- spObject.rotationZ = 0;
- var doRotate:Boolean = false;
- var prevX:Number;
- var prevY:Number;
- var facesColors:Array = [0xFFFFCC,
- 0x00FF66,
- 0x0066FF,
- 0x33FFFF,
- 0x9A7DDF,
- 0xFFCCFF];
- //Type of Gradient we will be using
- var fType:String = GradientType.LINEAR;
- //Colors of our gradient in the form of an array
- //var colors:Array = [ 0xB4E1E7, 0xEABBCB ];
- var colors:Array = [[0x00FF00, 0x004400],
- [0xFFFF00, 0xFF4400],
- [0xAAFFAA, 0xFF44AA],
- [0xCCFF00, 0xDD44AA],
- [0x00FFCC, 0x0044dd],
- [0xFF0000, 0xFF0000]];
- //Store the Alpha Values in the form of an array
- var alphas:Array = [ 1, 1 ];
- //Array of color distribution ratios.
- //The value defines percentage of the width where the color is sampled at 100%
- var ratios:Array = [ 0, 155 ];
- //Create a Matrix instance and assign the Gradient Box
- var matr:Matrix = new Matrix();
- matr.createGradientBox(205,155,1.57079632);
- //SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
- var sprMethod:String = SpreadMethod.PAD;
- function drawBack():void
- {
- shBack.graphics.beginFill(0x000000);
- shBack.graphics.drawRect(-160,-160,320,320);
- shBack.graphics.endFill();
- }
- setVertices();
- setFaces();
- rotateObj(0,0,0);
- function setVertices():void
- {
- vertsVec[0] = new Vector3D(-objRad,-objRad,-objRad);
- vertsVec[1] = new Vector3D(objRad,-objRad,-objRad);
- vertsVec[2] = new Vector3D(objRad,-objRad,objRad);
- vertsVec[3] = new Vector3D(-objRad,-objRad,objRad);
- vertsVec[4] = new Vector3D(-objRad,objRad,-objRad);
- vertsVec[5] = new Vector3D(objRad,objRad,-objRad);
- vertsVec[6] = new Vector3D(objRad,objRad,objRad);
- vertsVec[7] = new Vector3D(-objRad,objRad,objRad);
- }
- function setFaces():void
- {
- facesVec[0] = [0,4,5,1];
- facesVec[1] = [1,5,6,2];
- facesVec[2] = [2,6,7,3];
- facesVec[3] = [3,7,4,0];
- facesVec[4] = [4,5,6,7];
- facesVec[5] = [0,1,2,3];
- }
- function rotateObj(rotx:Number,roty:Number,rotz:Number):void
- {
- var i:int;
- var j:int;
- //distArray will be used for sorting faces.
- var distArray:Array = [];
- //dispVec will store vertices of the cube (or other object) projected onto xy-plane.
- var dispVec:Vector.<Point> = new Vector.<Point>();
- //Vertices in 3D, after rotx, roty, rotz rotations are applied,
- //will be stored in the next variable.
- var newVertsVec:Vector.<Vector3D> = new Vector.<Vector3D>();
- //z coordinates of midpoints of faces, zAverage, will be used for sorting faces.
- var zAverage:Number;
- var dist:Number;
- var curFace:int;
- var curFaceLen:int;
- var curObjMat:Matrix3D;
- spObject.transform.matrix3D.appendRotation(rotx,Vector3D.X_AXIS);
- spObject.transform.matrix3D.appendRotation(roty,Vector3D.Y_AXIS);
- spObject.transform.matrix3D.appendRotation(rotz,Vector3D.Z_AXIS);
- curObjMat = spObject.transform.matrix3D.clone();
- spObjImage.graphics.clear();
- for(i = 0;i<numVertices;i++)
- {
- newVertsVec[i] = curObjMat.deltaTransformVector(vertsVec[i]);
- }
- for(i = 0;i<numVertices;i++)
- {
- newVertsVec[i].w = (fLen+newVertsVec[i].z)/fLen;
- newVertsVec[i].project();
- }
- for(i = 0;i<numFaces;i++)
- {
- //In our case of a cube, the length of each face is 4
- //but for other objects it may be different.
- curFaceLen = facesVec[i].length;
- zAverage = 0;
- for(j = 0;j<curFaceLen;j++)
- {
- zAverage += newVertsVec[facesVec[i][j]].z;
- }
- zAverage /= curFaceLen;
- dist = zAverage;
- distArray[i] = [dist,i];
- }
- distArray.sort(byDist);
- for(i = 0;i<numVertices;i++)
- {
- dispVec[i] = new Point();
- dispVec[i].x = newVertsVec[i].x;
- dispVec[i].y = newVertsVec[i].y;
- }
- for(i = 0;i<numFaces;i++)
- {
- spObjImage.graphics.lineStyle(1,0xCC0000);
- curFace = distArray[i][1];
- curFaceLen = facesVec[curFace].length;
- //spObjImage.graphics.beginGradientFill( fType, colors[curFace], alphas, ratios, matr, sprMethod );
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- //comment line below and uncoment one above to get each
- //side of cube with different colors of gradient
- spObjImage.graphics.beginGradientFill( fType, colors[4], alphas, ratios, matr, sprMethod );
- spObjImage.graphics.moveTo(dispVec[facesVec[curFace][0]].x,dispVec[facesVec[curFace][0]].y);
- for(j = 1;j<curFaceLen;j++)
- {
- spObjImage.graphics.lineTo(dispVec[facesVec[curFace][j]].x,dispVec[facesVec[curFace][j]].y);
- }
- spObjImage.graphics.lineTo(dispVec[facesVec[curFace][0]].x,dispVec[facesVec[curFace][0]].y);
- spObjImage.graphics.endFill();
- }
- }
- 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 resetObj():void
- {
- spObject.transform.matrix3D = new Matrix3D();
- rotateObj(0,0,0);
- }
- //Listeners attached to spBoard are resposible for rotating with the mouse.
- spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut);
- spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
- spBoard.addEventListener(MouseEvent.MOUSE_DOWN,boardDown);
- spBoard.addEventListener(MouseEvent.MOUSE_UP,boardUp);
- function boardOut(e:MouseEvent):void
- {
- doRotate = false;
- }
- function boardDown(e:MouseEvent):void
- {
- prevX = spBoard.mouseX;
- prevY = spBoard.mouseY;
- doRotate = true;
- }
- function boardUp(e:MouseEvent):void
- {
- doRotate = false;
- }
- function boardMove(e:MouseEvent):void
- {
- var locX:Number = prevX;
- var locY:Number = prevY;
- if(doRotate)
- {
- prevX = spBoard.mouseX;
- prevY = spBoard.mouseY;
- rotateObj(prevY-locY,-(prevX-locX),0);
- e.updateAfterEvent();
- }
- }
- //The code below is responsible for sliders' functionality.
- var prevXVal:Number = 0;
- var prevYVal:Number = 0;
- var prevZVal:Number = 0;
- xSlider.addEventListener(SliderEvent.CHANGE,xSliderChange);
- function xSliderChange(e:SliderEvent):void
- {
- var curXVal:Number = e.target.value;
- rotateObj(curXVal-prevXVal,0,0);
- prevXVal = curXVal;
- }
- ySlider.addEventListener(SliderEvent.CHANGE,ySliderChange);
- function ySliderChange(e:SliderEvent):void
- {
- var curYVal:Number = e.target.value;
- rotateObj(0,curYVal-prevYVal,0);
- prevYVal = curYVal;
- }
- zSlider.addEventListener(SliderEvent.CHANGE,zSliderChange);
- function zSliderChange(e:SliderEvent):void
- {
- var curZVal:Number = e.target.value;
- rotateObj(0,0,curZVal-prevZVal);
- prevZVal = curZVal;
- }
- perspSlider.addEventListener(SliderEvent.CHANGE,perspSliderChange);
- function perspSliderChange(e:SliderEvent):void
- {
- fLen = e.target.value;
- rotateObj(0,0,0);
- }
- //RESET button resets the cube, the sliders and all the variables to their intial values.
- btnReset.addEventListener(MouseEvent.CLICK, resetAll);
- function resetAll(e:MouseEvent):void
- {
- fLen = 500;
- resetObj();
- prevXVal = 0;
- prevYVal = 0;
- prevZVal = 0;
- xSlider.value = 0;
- ySlider.value = 0;
- zSlider.value = 0;
- perspSlider.value = fLen;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement