notice: Flash editor updated! Join the development! Thanks to MiniBuilder


forked from : makc3d's 3D Pythagoras tree [diff(174)]

embed

FORKED
  1. // forked from makc3d's 3D Pythagoras tree (anaglyph)
  2. // forked from 3D Pythagoras tree - anaglyph version
  3. // Three-dimensional Pythagoras tree
  4. // First implementation ever, to my best knowledge :)
  5. //
  6. // Click anywhere to regenerate.
  7. // Check http://en.wikipedia.org/wiki/Pythagoras_tree for more info on subject.
  8. package {
  9.     import flash.display.*
  10.     import flash.events.*
  11.     import flash.geom.ColorTransform;
  12.     import alternativa.engine3d.*
  13.     import alternativa.engine3d.controllers.*
  14.     import alternativa.engine3d.core.*
  15.     import alternativa.engine3d.display.*
  16.     import alternativa.engine3d.materials.*
  17.     import alternativa.engine3d.primitives.*
  18.     import alternativa.types.*
  19.     use namespace alternativa3d;
  20.     [SWF(width=465,height=465,frameRate=30,backgroundColor=0)]
  21.     public class PythagorasTree3D extends Sprite {
  22.         // tree elements (center positions, left and up vectors)
  23.         // similar to http://wonderfl.kayac.com/code/18d2f77aeee90b0a3ae76dc5509a60f8bacad276
  24.         private var atA:Point3D = new Point3D, ltA:Point3D = new Point3D, upA:Point3D = new Point3D;
  25.         private var atB:Point3D = new Point3D, ltB:Point3D = new Point3D, upB:Point3D = new Point3D;
  26.         private var atC:Point3D = new Point3D, ltC:Point3D = new Point3D, upC:Point3D = new Point3D;
  27.         private var atD:Point3D = new Point3D, ltD:Point3D = new Point3D, upD:Point3D = new Point3D;
  28.         private var ltE:Point3D = new Point3D (-100), upE:Point3D = new Point3D (0, -10);
  29.         private var matD:Matrix3D = new Matrix3D;
  30.         private var matE:Matrix3D = new Matrix3D;
  31.         private var sA:Number, sB:Number, sC:Number;
  32.         private var rA:Point3D = new Point3D, rB:Point3D = new Point3D, rC:Point3D = new Point3D;
  33.         private function calculateElements (c1:Number, c2:Number):void
  34.         {
  35.             // using c1 and c2 in 0..1 range, select such a, b, c and d that a^2 + b^2 + c^2 = d^2
  36.             // this choise is almost completely free (we only need to make sure that d != 0)
  37.             var a:Number = 0.1 + c1, c:Number = 1.1 - c1; // +
  38.             var b:Number = ((c2 > 0.5) ? (c2 - 0.49) : (c2 - 0.51)) * 2// + or -
  39.             var d:Number = Math.sqrt (a * a + b * b + c * c); // +
  40.             // define corresponding tree elements in some convenient frame
  41.             // we are constrained by Pythagoras theorem, but orientation of planes is arbitrary
  42.             // additionally, we want our tree to coincide with 2D one in boundary case of b = 0
  43.             atA.x = +a/2; atA.y = 0; atA.z = -a/2;
  44.             ltA.x = +a/2; ltA.y = 0; ltA.z =  0;
  45.             upA.x =  0;   upA.y = 0; upA.z = -a/2;
  46.             atC.x = -c/2; atC.y = b; atC.z = +c/2;
  47.             ltC.x =  0;   ltC.y = 0; ltC.z = -c/2;
  48.             upC.x = -c/2; upC.y = 0; upC.z =  0;
  49.             atD.x = +a/2; atD.y = +b/2; atD.z = +c/2;
  50.             ltD.x = +a/2; ltD.y = -b/2; ltD.z = -c/2;
  51.             upD.x = -c; upD.y = 0; upD.z = -a; upD.normalize (); upD.multiply (ltD.length);
  52.             atD.subtract (upD);
  53.             ltB.x = 0; ltB.y = -b/2; ltB.z = 0;
  54.             upB.copy (upD); upB.normalize (); upB.multiply (Math.abs (b/2));
  55.             atB.copy (upB); atB.y = +b/2;
  56.             // find transformation that aligns D element with 2x2 plane frame (E)
  57.             // this 2x2 condition is there to use getRotations () method later
  58.             var lxuD:Point3D = Point3D.cross (ltD, upD);
  59.             lxuD.normalize (); lxuD.multiply (ltD.length);
  60.             matD.a = -ltD.x; matD.e = -ltD.y; matD.i = -ltD.z;
  61.             matD.b = -upD.x; matD.f = -upD.y; matD.j = -upD.z;
  62.             matD.c = lxuD.x; matD.g = lxuD.y; matD.k = lxuD.z;
  63.             var lxuE:Point3D = Point3D.cross (ltE, upE);
  64.             matE.a = -ltE.x; matE.e = -ltE.y; matE.i = -ltE.z;
  65.             matE.b = -upE.x; matE.f = -upE.y; matE.j = -upE.z;
  66.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  67.             matD.invert (); matD.combine (matE);
  68.             // transform A, B and C elements
  69.             atA.subtract (atD); atA.transform (matD); ltA.transform (matD); upA.transform (matD);
  70.             atB.subtract (atD); atB.transform (matD); ltB.transform (matD); upB.transform (matD);
  71.             atC.subtract (atD); atC.transform (matD); ltC.transform (matD); upC.transform (matD);
  72.             // calculate scales and normalize left/up vectors
  73.             sA = ltA.length; ltA.normalize (); upA.normalize ();
  74.             sB = ltB.length; ltB.normalize (); upB.normalize ();
  75.             sC = ltC.length; ltC.normalize (); upC.normalize ();
  76.             // finally, calculate corresponding rotations (re-using matE/lxuE variables)
  77.             lxuE = Point3D.cross (ltA, upA);
  78.             matE.a = -ltA.x; matE.e = -ltA.y; matE.i = -ltA.z;
  79.             matE.b = -upA.x; matE.f = -upA.y; matE.j = -upA.z;
  80.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  81.             matE.getRotations (rA);
  82.             lxuE = Point3D.cross (ltB, upB);
  83.             matE.a = -ltB.x; matE.e = -ltB.y; matE.i = -ltB.z;
  84.             matE.b = -upB.x; matE.f = -upB.y; matE.j = -upB.z;
  85.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  86.             matE.getRotations (rB);
  87.             lxuE = Point3D.cross (ltC, upC);
  88.             matE.a = -ltC.x; matE.e = -ltC.y; matE.i = -ltC.z;
  89.             matE.b = -upC.x; matE.f = -upC.y; matE.j = -upC.z;
  90.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  91.             matE.getRotations (rC);
  92.         }
  93.         private var planes:Array = [], planesDone:Array = [];
  94.         private function step ():void {
  95.             var pD:Plane = Plane (planes.shift ());
  96.             if (pD == nullreturn;
  97.             planesDone.push (pD);
  98.             var scale:Number =
  99.                 pD.transformation.a * pD.transformation.a +
  100.                 pD.transformation.e * pD.transformation.e +
  101.                 pD.transformation.i * pD.transformation.i;
  102.             if (scale < 0.005return;
  103.             var pA:Plane = createPlane ();
  104.             pA.rotationX = rA.x;  pA.rotationY = rA.y;  pA.rotationZ = rA.z;
  105.             pA.scaleX    = sA;    pA.scaleY    = sA;    pA.scaleZ    = sA;
  106.             pA.x         = atA.x; pA.y         = atA.y; pA.z         = atA.z;
  107.             pD.addChild (pA); planes.push (pA);
  108.             var pB:Plane = createPlane ();
  109.             pB.rotationX = rB.x;  pB.rotationY = rB.y;  pB.rotationZ = rB.z;
  110.             pB.scaleX    = sB;    pB.scaleY    = sB;    pB.scaleZ    = sB;
  111.             pB.x         = atB.x; pB.y         = atB.y; pB.z         = atB.z;
  112.             pD.addChild (pB); planes.push (pB);
  113.             var pC:Plane = createPlane ();
  114.             pC.rotationX = rC.x;  pC.rotationY = rC.y;  pC.rotationZ = rC.z;
  115.             pC.scaleX    = sC;    pC.scaleY    = sC;    pC.scaleZ    = sC;
  116.             pC.x         = atC.x; pC.y         = atC.y; pC.z         = atC.z;
  117.             pD.addChild (pC); planes.push (pC);
  118.         }
  119.         private function regenerate (c1:Number, c2:Number):void {
  120.             var p:Plane;
  121.             // delete all the planes
  122.             for each (p in planesDone) destroyPlane (p);
  123.             planesDone.length = 0; planes.length = 0;
  124.             // create base plane
  125.             p = createPlane (); planes.push (p); scene.root.addChild (p);
  126.             // do the math
  127.             calculateElements (c1, c2);
  128.         }
  129.         private function destroyPlane (p:Plane):void {
  130.             // this is supposed to make alternativa trash GC-friendly
  131.             if (p.parent != null) p.parent.removeChild (p);
  132.             if (p.hasSurface ("back")) p.setMaterialToSurface (null"back");
  133.             if (p.hasSurface ("front")) p.setMaterialToSurface (null"front");
  134.             p.moveAllFacesToSurface (nulltrue);
  135.         }
  136.         private function createPlane ():Plane {
  137.             var p:Plane = new Plane (22);
  138.             p.cloneMaterialToAllSurfaces (new FillMaterial (0xFFFFFF, 0.5));
  139.             return p;
  140.         }
  141.         private function lightPlane (p:Plane):void {
  142.             var face:Face = p.faces.peek () as Face;
  143.             var dot:Number = face.globalNormal.x + face.globalNormal.y - face.globalNormal.z;
  144.             if (dot < 0) dot = 0if (dot > 1) dot = 1;
  145.             var lum:Number = 0.6 + 0.4 * dot;
  146.             var color:uint = 0x10101 * int (255 * lum);
  147.             var mat1:FillMaterial = FillMaterial (Surface (p.surfaces ["front"]).material); mat1.color = color;
  148.             var mat2:FillMaterial = FillMaterial (Surface (p.surfaces ["back"]).material); mat2.color = color;
  149.         }
  150.         private var scene:Scene3D;
  151.         private var tripod:Object3D;
  152.         private var viewL:View;
  153.         private var viewR:View;
  154.         public function PythagorasTree3D () {
  155.             scene = new Scene3D; scene.root = new Object3D;
  156.             viewL = new View; viewL.camera = new Camera3D;
  157.             viewL.camera.z = -6; viewL.camera.rotationX = -0.6; viewL.camera.y = -9;
  158.             tripod = new Object3D; scene.root.addChild (tripod); tripod.addChild (viewL.camera);
  159.             viewL.width = 500; viewL.height = 465; addChild (viewL); viewL.x = -30;
  160.             viewL.transform.colorTransform = new ColorTransform (100);
  161.             viewR = new View; viewR.camera = new Camera3D;
  162.             viewR.camera.x = 0.8; viewL.camera.addChild (viewR.camera);
  163.             viewR.width = 500; viewR.height = 465; addChild (viewR);
  164.             viewR.transform.colorTransform = new ColorTransform (00.91);
  165.             viewR.blendMode = "add";
  166.             regenerate (0.10.2);
  167.             var s:Sprite = new Sprite; s.buttonMode = s.useHandCursor = true;
  168.             s.graphics.beginFill (00); s.graphics.drawRect (00465465);
  169.             addChild (s); s.addEventListener (MouseEvent.CLICK, onClick);
  170.             addEventListener (Event.ENTER_FRAME, onEnterFrame);
  171.         }
  172.         private function onClick (e:MouseEvent):void {
  173.             regenerate (mouseX / 465.0, mouseY / 465.0);
  174.         }
  175.         private function onEnterFrame (e:Event):void {
  176.             // add 3 more planes
  177.             var grow:Boolean = (planesDone.length < 2000);
  178.             if (grow) step (); scene.calculate ();
  179.             // apply lighting after global normals were set
  180.             if (grow)
  181.             for (var i:int = Math.max (0, planes.length - 3); i < planes.length; i++)
  182.                 lightPlane (planes [i]);
  183.         }
  184.     }
  185. }
noswf
Get Adobe Flash Player