Advertisement
Guest User

mth

a guest
Aug 23rd, 2009
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Code is a modification of code from
  2. // http://www.flashandmath.com/flashcs4/cs4simple3d/index.html
  3.  
  4. // http://stackoverflow.com/questions/1317992/how-to-rotate-gradient-along-with-sides-of-3d-cube-un-flash-actionscropt-3-0
  5.  
  6. import fl.events.SliderEvent;
  7. var numVertices:int = 8;
  8. var numFaces:int = 6;
  9. var objRad:Number = 70;
  10. var vertsVec:Vector.<Vector3D> = new Vector.<Vector3D>();
  11. var facesVec:Vector.<Array> = new Vector.<Array>();
  12. //'fLen' is reponsible for perspective distortion: the larger fLen the less distortion.
  13. var fLen:Number = 500;
  14. //We adjust the initial position of the slider that changes perspective.
  15. perspSlider.value = fLen;
  16.  
  17. var spBoard:Sprite = new Sprite();
  18. this.addChild(spBoard);
  19. spBoard.x = 180;
  20. spBoard.y = 205;
  21. spBoard.filters = [ new DropShadowFilter() ];
  22. //shBack is the black background drawn by the function 'drawBack'.
  23. var shBack:Shape = new Shape();
  24. spBoard.addChild(shBack);
  25. drawBack();
  26.  
  27. var spObject:Sprite = new Sprite();
  28. var spObjImage:Sprite = new Sprite();
  29. spBoard.addChild(spObjImage);
  30.  
  31. spObject.rotationX = 0;
  32. spObject.rotationY = 0;
  33. spObject.rotationZ = 0;
  34.  
  35. var doRotate:Boolean = false;
  36. var prevX:Number;
  37. var prevY:Number;
  38.  
  39. var facesColors:Array = [0xFFFFCC,
  40.                         0x00FF66,
  41.                         0x0066FF,
  42.                         0x33FFFF,
  43.                         0x9A7DDF,
  44.                         0xFFCCFF];
  45.  
  46. //Type of Gradient we will be using
  47. var fType:String = GradientType.LINEAR;
  48. //Colors of our gradient in the form of an array
  49. //var colors:Array = [ 0xB4E1E7, 0xEABBCB ];
  50. var colors:Array = [[0x00FF00, 0x004400],
  51.                     [0xFFFF00, 0xFF4400],
  52.                     [0xAAFFAA, 0xFF44AA],
  53.                     [0xCCFF00, 0xDD44AA],
  54.                     [0x00FFCC, 0x0044dd],
  55.                     [0xFF0000, 0xFF0000]];
  56.  
  57. //Store the Alpha Values in the form of an array
  58. var alphas:Array = [ 1, 1 ];
  59. //Array of color distribution ratios.  
  60. //The value defines percentage of the width where the color is sampled at 100%
  61. var ratios:Array = [ 0, 155 ];
  62. //Create a Matrix instance and assign the Gradient Box
  63. var matr:Matrix = new Matrix();
  64.     matr.createGradientBox(205,155,1.57079632);
  65. //SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
  66. var sprMethod:String = SpreadMethod.PAD;
  67.  
  68.  
  69. function drawBack():void
  70. {
  71.     shBack.graphics.beginFill(0x000000);
  72.     shBack.graphics.drawRect(-160,-160,320,320);
  73.     shBack.graphics.endFill();
  74. }
  75.  
  76. setVertices();
  77. setFaces();
  78. rotateObj(0,0,0);
  79.  
  80. function setVertices():void
  81. {
  82.     vertsVec[0] = new Vector3D(-objRad,-objRad,-objRad);
  83.     vertsVec[1] = new Vector3D(objRad,-objRad,-objRad);
  84.     vertsVec[2] = new Vector3D(objRad,-objRad,objRad);
  85.     vertsVec[3] = new Vector3D(-objRad,-objRad,objRad);
  86.     vertsVec[4] = new Vector3D(-objRad,objRad,-objRad);
  87.     vertsVec[5] = new Vector3D(objRad,objRad,-objRad);
  88.     vertsVec[6] = new Vector3D(objRad,objRad,objRad);
  89.     vertsVec[7] = new Vector3D(-objRad,objRad,objRad);
  90. }
  91.  
  92. function setFaces():void
  93. {
  94.     facesVec[0] = [0,4,5,1];
  95.     facesVec[1] = [1,5,6,2];
  96.     facesVec[2] = [2,6,7,3];
  97.     facesVec[3] = [3,7,4,0];
  98.     facesVec[4] = [4,5,6,7];
  99.     facesVec[5] = [0,1,2,3];
  100. }
  101.  
  102. function rotateObj(rotx:Number,roty:Number,rotz:Number):void
  103. {
  104.     var i:int;
  105.     var j:int;
  106.     //distArray will be used for sorting faces.
  107.     var distArray:Array = [];
  108.     //dispVec will store vertices of the cube (or other object) projected onto xy-plane.
  109.     var dispVec:Vector.<Point> = new Vector.<Point>();
  110.     //Vertices in 3D, after rotx, roty, rotz rotations are applied,
  111.     //will be stored in the next variable.
  112.     var newVertsVec:Vector.<Vector3D> = new Vector.<Vector3D>();
  113.     //z coordinates of midpoints of faces, zAverage, will be used for sorting faces.
  114.     var zAverage:Number;
  115.     var dist:Number;
  116.     var curFace:int;
  117.     var curFaceLen:int;
  118.     var curObjMat:Matrix3D;
  119.    
  120.     spObject.transform.matrix3D.appendRotation(rotx,Vector3D.X_AXIS);
  121.     spObject.transform.matrix3D.appendRotation(roty,Vector3D.Y_AXIS);
  122.     spObject.transform.matrix3D.appendRotation(rotz,Vector3D.Z_AXIS);
  123.    
  124.     curObjMat = spObject.transform.matrix3D.clone();
  125.     spObjImage.graphics.clear();
  126.    
  127.     for(i = 0;i<numVertices;i++)
  128.     {
  129.         newVertsVec[i] = curObjMat.deltaTransformVector(vertsVec[i]);
  130.     }
  131.    
  132.  
  133.     for(i = 0;i<numVertices;i++)
  134.     {
  135.         newVertsVec[i].w = (fLen+newVertsVec[i].z)/fLen;
  136.         newVertsVec[i].project();
  137.     }
  138.  
  139.  
  140.     for(i = 0;i<numFaces;i++)
  141.     {
  142.         //In our case of a cube, the length of each face is 4
  143.         //but for other objects it may be different.
  144.         curFaceLen = facesVec[i].length;
  145.         zAverage = 0;
  146.         for(j = 0;j<curFaceLen;j++)
  147.         {
  148.             zAverage += newVertsVec[facesVec[i][j]].z;
  149.         }
  150.         zAverage /= curFaceLen;
  151.         dist = zAverage;
  152.         distArray[i] = [dist,i];
  153.     }
  154.  
  155.     distArray.sort(byDist);
  156.  
  157.     for(i = 0;i<numVertices;i++)
  158.     {
  159.         dispVec[i] = new Point();
  160.         dispVec[i].x = newVertsVec[i].x;
  161.         dispVec[i].y = newVertsVec[i].y;
  162.     }
  163.    
  164.     for(i = 0;i<numFaces;i++)
  165.     {
  166.         spObjImage.graphics.lineStyle(1,0xCC0000);
  167.         curFace = distArray[i][1];
  168.         curFaceLen = facesVec[curFace].length;
  169.        
  170.         //spObjImage.graphics.beginGradientFill( fType, colors[curFace], alphas, ratios, matr, sprMethod );
  171.        
  172.         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  173.         //comment line below and uncoment one above to get each
  174.         //side of cube with different colors of gradient
  175.         spObjImage.graphics.beginGradientFill( fType, colors[4], alphas, ratios, matr, sprMethod );
  176.  
  177.        
  178.    
  179.         spObjImage.graphics.moveTo(dispVec[facesVec[curFace][0]].x,dispVec[facesVec[curFace][0]].y);
  180.         for(j = 1;j<curFaceLen;j++)
  181.         {
  182.             spObjImage.graphics.lineTo(dispVec[facesVec[curFace][j]].x,dispVec[facesVec[curFace][j]].y);
  183.         }
  184.         spObjImage.graphics.lineTo(dispVec[facesVec[curFace][0]].x,dispVec[facesVec[curFace][0]].y);
  185.         spObjImage.graphics.endFill();
  186.     }
  187. }
  188.  
  189.  
  190. function byDist(v:Array,w:Array):Number
  191. {
  192.     if (v[0]>w[0])
  193.     {
  194.         return -1;
  195.     }
  196.     else if (v[0]<w[0])
  197.     {
  198.         return 1;
  199.     }
  200.     else
  201.     {
  202.         return 0;
  203.     }
  204. }
  205.  
  206.  
  207. function resetObj():void
  208. {
  209.     spObject.transform.matrix3D = new Matrix3D();
  210.     rotateObj(0,0,0);
  211. }
  212.  
  213. //Listeners attached to spBoard are resposible for rotating with the mouse.
  214.  
  215. spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut);
  216. spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
  217. spBoard.addEventListener(MouseEvent.MOUSE_DOWN,boardDown);
  218. spBoard.addEventListener(MouseEvent.MOUSE_UP,boardUp);
  219.  
  220. function boardOut(e:MouseEvent):void
  221. {
  222.     doRotate = false;
  223. }
  224. function boardDown(e:MouseEvent):void
  225. {          
  226.     prevX = spBoard.mouseX;
  227.  
  228.     prevY = spBoard.mouseY;
  229.     doRotate = true;
  230. }
  231.  
  232. function boardUp(e:MouseEvent):void
  233. {
  234.     doRotate = false;
  235. }
  236.  
  237.  
  238. function boardMove(e:MouseEvent):void
  239. {
  240.     var locX:Number = prevX;
  241.     var locY:Number = prevY;
  242.     if(doRotate)
  243.     {
  244.         prevX = spBoard.mouseX;
  245.         prevY = spBoard.mouseY;
  246.         rotateObj(prevY-locY,-(prevX-locX),0);
  247.         e.updateAfterEvent();
  248.     }
  249. }
  250.  
  251.  
  252.  
  253. //The code below is responsible for sliders' functionality.
  254. var prevXVal:Number = 0;
  255. var prevYVal:Number = 0;
  256. var prevZVal:Number = 0;
  257.  
  258. xSlider.addEventListener(SliderEvent.CHANGE,xSliderChange);
  259. function xSliderChange(e:SliderEvent):void
  260. {
  261.     var curXVal:Number = e.target.value;
  262.     rotateObj(curXVal-prevXVal,0,0);
  263.     prevXVal = curXVal;
  264. }
  265.  
  266. ySlider.addEventListener(SliderEvent.CHANGE,ySliderChange);
  267. function ySliderChange(e:SliderEvent):void
  268. {
  269.     var curYVal:Number = e.target.value;
  270.     rotateObj(0,curYVal-prevYVal,0);
  271.     prevYVal = curYVal;
  272. }
  273.  
  274. zSlider.addEventListener(SliderEvent.CHANGE,zSliderChange);
  275. function zSliderChange(e:SliderEvent):void
  276. {
  277.     var curZVal:Number = e.target.value;
  278.     rotateObj(0,0,curZVal-prevZVal);
  279.     prevZVal = curZVal;
  280. }
  281.  
  282. perspSlider.addEventListener(SliderEvent.CHANGE,perspSliderChange);
  283. function perspSliderChange(e:SliderEvent):void
  284. {
  285.     fLen = e.target.value;
  286.     rotateObj(0,0,0);
  287. }
  288.  
  289. //RESET button resets the cube, the sliders and all the variables to their intial values.
  290. btnReset.addEventListener(MouseEvent.CLICK, resetAll);
  291. function resetAll(e:MouseEvent):void
  292. {
  293.     fLen = 500;
  294.     resetObj();
  295.     prevXVal = 0;
  296.     prevYVal = 0;
  297.     prevZVal = 0;
  298.     xSlider.value = 0;
  299.     ySlider.value = 0;
  300.     zSlider.value = 0;
  301.     perspSlider.value = fLen;
  302. }
  303.  
  304.  
  305.  
  306.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement