※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

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


FAVORITE BY
:
:
3d
:
pv3dでツリー
FORKED
  1. // forked from 3D Pythagoras tree - anaglyph version
  2. // Three-dimensional Pythagoras tree
  3. // First implementation ever, to my best knowledge :)
  4. //
  5. // Click anywhere to regenerate.
  6. // Check http://en.wikipedia.org/wiki/Pythagoras_tree for more info on subject.
  7. package {
  8.     import flash.display.*
  9.     import flash.events.*
  10.     import flash.geom.ColorTransform;
  11.     import alternativa.engine3d.*
  12.     import alternativa.engine3d.controllers.*
  13.     import alternativa.engine3d.core.*
  14.     import alternativa.engine3d.display.*
  15.     import alternativa.engine3d.materials.*
  16.     import alternativa.engine3d.primitives.*
  17.     import alternativa.types.*
  18.     use namespace alternativa3d;
  19.     [SWF(width=465,height=465,frameRate=30,backgroundColor=0)]
  20.     public class PythagorasTree3D extends Sprite {
  21.         // tree elements (center positions, left and up vectors)
  22.         // similar to http://wonderfl.kayac.com/code/18d2f77aeee90b0a3ae76dc5509a60f8bacad276
  23.         private var atA:Point3D = new Point3D, ltA:Point3D = new Point3D, upA:Point3D = new Point3D;
  24.         private var atB:Point3D = new Point3D, ltB:Point3D = new Point3D, upB:Point3D = new Point3D;
  25.         private var atC:Point3D = new Point3D, ltC:Point3D = new Point3D, upC:Point3D = new Point3D;
  26.         private var atD:Point3D = new Point3D, ltD:Point3D = new Point3D, upD:Point3D = new Point3D;
  27.         private var ltE:Point3D = new Point3D (-100), upE:Point3D = new Point3D (0, -10);
  28.         private var matD:Matrix3D = new Matrix3D;
  29.         private var matE:Matrix3D = new Matrix3D;
  30.         private var sA:Number, sB:Number, sC:Number;
  31.         private var rA:Point3D = new Point3D, rB:Point3D = new Point3D, rC:Point3D = new Point3D;
  32.         private function calculateElements (c1:Number, c2:Number):void
  33.         {
  34.             // using c1 and c2 in 0..1 range, select such a, b, c and d that a^2 + b^2 + c^2 = d^2
  35.             // this choise is almost completely free (we only need to make sure that d != 0)
  36.             var a:Number = 0.1 + c1, c:Number = 1.1 - c1; // +
  37.             var b:Number = ((c2 > 0.5) ? (c2 - 0.499999) : (c2 - 0.500001)) * 2// + or -
  38.             var d:Number = Math.sqrt (a * a + b * b + c * c); // +
  39.             // define corresponding tree elements in some convenient frame
  40.             // we are constrained by Pythagoras theorem, but orientation of planes is arbitrary
  41.             // additionally, we want our tree to coincide with 2D one in boundary case of b = 0
  42.             atA.x = +a/2; atA.y = 0; atA.z = -a/2;
  43.             ltA.x = +a/2; ltA.y = 0; ltA.z =  0;
  44.             upA.x =  0;   upA.y = 0; upA.z = -a/2;
  45.             atC.x = -c/2; atC.y = b; atC.z = +c/2;
  46.             ltC.x =  0;   ltC.y = 0; ltC.z = -c/2;
  47.             upC.x = -c/2; upC.y = 0; upC.z =  0;
  48.             atD.x = +a/2; atD.y = +b/2; atD.z = +c/2;
  49.             ltD.x = +a/2; ltD.y = -b/2; ltD.z = -c/2;
  50.             upD.x = -c; upD.y = 0; upD.z = -a; upD.normalize (); upD.multiply (ltD.length);
  51.             atD.subtract (upD);
  52.             ltB.x = 0; ltB.y = -b/2; ltB.z = 0;
  53.             upB.copy (upD); upB.normalize (); upB.multiply (Math.abs (b/2));
  54.             atB.copy (upB); atB.y = +b/2;
  55.             // find transformation that aligns D element with 2x2 plane frame (E)
  56.             // this 2x2 condition is there to use getRotations () method later
  57.             var lxuD:Point3D = Point3D.cross (ltD, upD);
  58.             lxuD.normalize (); lxuD.multiply (ltD.length);
  59.             matD.a = -ltD.x; matD.e = -ltD.y; matD.i = -ltD.z;
  60.             matD.b = -upD.x; matD.f = -upD.y; matD.j = -upD.z;
  61.             matD.c = lxuD.x; matD.g = lxuD.y; matD.k = lxuD.z;
  62.             var lxuE:Point3D = Point3D.cross (ltE, upE);
  63.             matE.a = -ltE.x; matE.e = -ltE.y; matE.i = -ltE.z;
  64.             matE.b = -upE.x; matE.f = -upE.y; matE.j = -upE.z;
  65.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  66.             matD.invert (); matD.combine (matE);
  67.             // transform A, B and C elements
  68.             atA.subtract (atD); atA.transform (matD); ltA.transform (matD); upA.transform (matD);
  69.             atB.subtract (atD); atB.transform (matD); ltB.transform (matD); upB.transform (matD);
  70.             atC.subtract (atD); atC.transform (matD); ltC.transform (matD); upC.transform (matD);
  71.             // calculate scales and normalize left/up vectors
  72.             sA = ltA.length; ltA.normalize (); upA.normalize ();
  73.             sB = ltB.length; ltB.normalize (); upB.normalize ();
  74.             sC = ltC.length; ltC.normalize (); upC.normalize ();
  75.             // finally, calculate corresponding rotations (re-using matE/lxuE variables)
  76.             lxuE = Point3D.cross (ltA, upA);
  77.             matE.a = -ltA.x; matE.e = -ltA.y; matE.i = -ltA.z;
  78.             matE.b = -upA.x; matE.f = -upA.y; matE.j = -upA.z;
  79.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  80.             matE.getRotations (rA);
  81.             lxuE = Point3D.cross (ltB, upB);
  82.             matE.a = -ltB.x; matE.e = -ltB.y; matE.i = -ltB.z;
  83.             matE.b = -upB.x; matE.f = -upB.y; matE.j = -upB.z;
  84.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  85.             matE.getRotations (rB);
  86.             lxuE = Point3D.cross (ltC, upC);
  87.             matE.a = -ltC.x; matE.e = -ltC.y; matE.i = -ltC.z;
  88.             matE.b = -upC.x; matE.f = -upC.y; matE.j = -upC.z;
  89.             matE.c = lxuE.x; matE.g = lxuE.y; matE.k = lxuE.z;
  90.             matE.getRotations (rC);
  91.         }
  92.         private var planes:Array = [], planesDone:Array = [];
  93.         private function step ():void {
  94.             var pD:Plane = Plane (planes.shift ());
  95.             if (pD == nullreturn;
  96.             planesDone.push (pD);
  97.             var scale:Number =
  98.                 pD.transformation.a * pD.transformation.a +
  99.                 pD.transformation.e * pD.transformation.e +
  100.                 pD.transformation.i * pD.transformation.i;
  101.             if (scale < 0.001return;
  102.             var pA:Plane = createPlane ();
  103.             pA.rotationX = rA.x;  pA.rotationY = rA.y;  pA.rotationZ = rA.z;
  104.             pA.scaleX    = sA;    pA.scaleY    = sA;    pA.scaleZ    = sA;
  105.             pA.x         = atA.x; pA.y         = atA.y; pA.z         = atA.z;
  106.             pD.addChild (pA); planes.push (pA);
  107.             var pB:Plane = createPlane ();
  108.             pB.rotationX = rB.x;  pB.rotationY = rB.y;  pB.rotationZ = rB.z;
  109.             pB.scaleX    = sB;    pB.scaleY    = sB;    pB.scaleZ    = sB;
  110.             pB.x         = atB.x; pB.y         = atB.y; pB.z         = atB.z;
  111.             pD.addChild (pB); planes.push (pB);
  112.             var pC:Plane = createPlane ();
  113.             pC.rotationX = rC.x;  pC.rotationY = rC.y;  pC.rotationZ = rC.z;
  114.             pC.scaleX    = sC;    pC.scaleY    = sC;    pC.scaleZ    = sC;
  115.             pC.x         = atC.x; pC.y         = atC.y; pC.z         = atC.z;
  116.             pD.addChild (pC); planes.push (pC);
  117.         }
  118.         private function regenerate (c1:Number, c2:Number):void {
  119.             var p:Plane;
  120.             // delete all the planes
  121.             for each (p in planesDone) destroyPlane (p);
  122.             planesDone.length = 0; planes.length = 0;
  123.             // create base plane
  124.             p = createPlane (); planes.push (p); scene.root.addChild (p);
  125.             // do the math
  126.             calculateElements (c1, c2);
  127.             // mark location
  128.             loc.x = stage.stageWidth * c1;
  129.             loc.y = stage.stageHeight * c2;
  130.         }
  131.         private function destroyPlane (p:Plane):void {
  132.             // this is supposed to make alternativa trash GC-friendly
  133.             if (p.parent != null) p.parent.removeChild (p);
  134.             if (p.hasSurface ("back")) p.setMaterialToSurface (null"back");
  135.             if (p.hasSurface ("front")) p.setMaterialToSurface (null"front");
  136.             p.moveAllFacesToSurface (nulltrue);
  137.         }
  138.         private function createPlane ():Plane {
  139.             var p:Plane = new Plane (22);
  140.             p.cloneMaterialToAllSurfaces (new FillMaterial (0x00FF));
  141.             return p;
  142.         }
  143.         private function interpolateColor (fromColor:uint, toColor:uint, progress:Number):uint {
  144.             var q:Number = 1-progress;
  145.             var fromR:uint = (fromColor >> 16) & 0xFF;
  146.             var fromG:uint = (fromColor >>  8) & 0xFF;
  147.             var fromB:uint =  fromColor        & 0xFF;
  148.             var toR:uint = (toColor >> 16) & 0xFF;
  149.             var toG:uint = (toColor >>  8) & 0xFF;
  150.             var toB:uint =  toColor        & 0xFF;
  151.             var resultR:uint = fromR*q + toR*progress;
  152.             var resultG:uint = fromG*q + toG*progress;
  153.             var resultB:uint = fromB*q + toB*progress;
  154.             var resultColor:uint = resultR << 16 | resultG << 8 | resultB;
  155.             return resultColor;
  156.         }
  157.         private function lightPlane (p:Plane):void {
  158.             var face:Face = p.faces.peek () as Face;
  159.             var dot:Number = (face.globalNormal.x + face.globalNormal.y - face.globalNormal.z) / Math.sqrt (3);
  160.             if (dot < -1) dot = -1if (dot > 1) dot = 1;
  161.             var lum:Number = 0.6 + 0.4 * Math.abs (dot);
  162.             var color1:uint = 0x40302 * int (63 * lum); // yellow-ish
  163.             var color2:uint = 0x20400 * int (63 * lum); // green-ish
  164.             var m:Matrix3D = p.transformation;
  165.             var prog:Number = Math.sqrt (m.a * m.a + m.e * m.e + m.i * m.i);
  166.             var color:uint = interpolateColor (color2, color1, prog);
  167.             var mat1:FillMaterial = FillMaterial (Surface (p.surfaces ["front"]).material); mat1.color = color;
  168.             var mat2:FillMaterial = FillMaterial (Surface (p.surfaces ["back"]).material); mat2.color = color;
  169.         }
  170.         private var scene:Scene3D;
  171.         private var tripod:Object3D;
  172.         private var viewL:View;
  173.         private var viewR:View;
  174.         private var loc:Shape;
  175.         public function PythagorasTree3D () {
  176.             stage.quality = "best";
  177.             scene = new Scene3D; scene.root = new Object3D;
  178.             viewL = new View; viewL.camera = new Camera3D;
  179.             viewL.camera.z = -6; viewL.camera.rotationX = -0.6; viewL.camera.y = -9;
  180.             tripod = new Object3D; scene.root.addChild (tripod); tripod.addChild (viewL.camera);
  181.             viewL.width = 500; viewL.height = 465; addChild (viewL); viewL.x = -26;
  182.             viewL.transform.colorTransform = new ColorTransform (100);
  183.             viewR = new View; viewR.camera = new Camera3D;
  184.             viewR.camera.x = 0.8; viewL.camera.addChild (viewR.camera);
  185.             viewR.width = 500; viewR.height = 465; addChild (viewR);
  186.             viewR.transform.colorTransform = new ColorTransform (00.91);
  187.             viewR.blendMode = "add";
  188.             var s:Sprite = new Sprite; s.buttonMode = s.useHandCursor = true;
  189.             s.graphics.beginFill (00); s.graphics.drawRect (00465465);
  190.             addChild (s); s.addEventListener (MouseEvent.CLICK, onClick);
  191.             addEventListener (Event.ENTER_FRAME, onEnterFrame);
  192.             loc = new Shape;
  193.             loc.graphics.lineStyle ();
  194.             loc.graphics.beginFill (0xFFFFFF);
  195.             loc.graphics.drawCircle (002);
  196.             addChild (loc); loc.addEventListener (MouseEvent.CLICK, onClick);
  197.             regenerate (0.50.5);
  198.             stage.addEventListener (KeyboardEvent.KEY_UP, onKeyUp);
  199.         }
  200.         private function onClick (e:MouseEvent):void {
  201.             regenerate (mouseX / stage.stageWidth, mouseY / stage.stageHeight);
  202.             if (e.shiftKey)
  203.                 trace ("\t\t\tnew Point ("
  204.                 + (mouseX / stage.stageWidth).toFixed (5)
  205.                 + ", "
  206.                 + (mouseY / stage.stageHeight).toFixed (5)
  207.                 + "),");
  208.         }
  209.         private function onEnterFrame (e:Event):void {
  210.             // add 3 more planes
  211.             var lastPlaneDone:int = planesDone.length;
  212.             var grow:Boolean = (planesDone.length < 15000);
  213.             if (grow) {
  214.                 for (var s:int = 0; s < 90; s++) step ();
  215.             }
  216.             scene.calculate ();
  217.             // apply lighting after global normals were set
  218.             if (grow || (lastPlaneDone < planesDone.length)) {
  219.                 for (var p:int = lastPlaneDone; p < planesDone.length; p++)
  220.                     lightPlane (planesDone [p]);
  221.                 for (var i:int = Math.max (0, planes.length - 9*3); i < planes.length; i++)
  222.                     lightPlane (planes [i]);
  223.             }
  224.         }
  225.         private var dae:XML =
  226.         <COLLADA version="1.4.0" xmlns="http://www.collada.org/2005/11/COLLADASchema">
  227.             <asset>
  228.                 <unit meter="0.01" name="centimeter"/>
  229.                 <up_axis>Z_UP</up_axis>
  230.             </asset>
  231.             <library_geometries>
  232.                 <geometry id="Plane-Geometry" name="Plane-Geometry">
  233.                     <mesh>
  234.                         <source id="Plane-Geometry-Position">
  235.                             <float_array count="36" id="Plane-Geometry-Position-array">1.00000 1.00000 0.00000 1.00000 -1.00000 0.00000 -1.00000 -1.00000 0.00000 -1.00000 1.00000 0.00000</float_array>
  236.                             <technique_common>
  237.                                 <accessor count="4" source="#Plane-Geometry-Position-array" stride="3">
  238.                                     <param type="float" name="X"></param>
  239.                                     <param type="float" name="Y"></param>
  240.                                     <param type="float" name="Z"></param>
  241.                                 </accessor>
  242.                             </technique_common>
  243.                         </source>
  244.                         <source id="Plane-Geometry-Normals">
  245.                             <float_array count="3" id="Plane-Geometry-Normals-array">0.00000 0.00000 1.00000</float_array>
  246.                             <technique_common>
  247.                                 <accessor count="1" source="#Plane-Geometry-Normals-array" stride="3">
  248.                                     <param type="float" name="X"></param>
  249.                                     <param type="float" name="Y"></param>
  250.                                     <param type="float" name="Z"></param>
  251.                                 </accessor>
  252.                             </technique_common>
  253.                         </source>
  254.                         <vertices id="Plane-Geometry-Vertex">
  255.                             <input semantic="POSITION" source="#Plane-Geometry-Position"/>
  256.                         </vertices>
  257.                         <polygons count="1">
  258.                             <input offset="0" semantic="VERTEX" source="#Plane-Geometry-Vertex"/>
  259.                             <input offset="1" semantic="NORMAL" source="#Plane-Geometry-Normals"/>
  260.                             <p>0 0 1 0 2 0 3 0</p>
  261.                         </polygons>
  262.                     </mesh>
  263.                 </geometry>
  264.             </library_geometries>
  265.             <library_visual_scenes>
  266.                 <visual_scene id="Scene" name="Scene" />
  267.             </library_visual_scenes>
  268.             <scene>
  269.                 <instance_visual_scene url="#Scene"/>
  270.             </scene>
  271.         </COLLADA>;
  272.         private function onKeyUp (e:KeyboardEvent):void {
  273.             // export collada 1.4
  274.             var scene:XML = <visual_scene id="Scene" name="Scene" />;
  275.             for (var i:int = 0; i < planesDone.length; i++) {
  276.                 var pi:Plane = Plane (planesDone [i]);
  277.                 var mi:Matrix3D = pi.transformation;
  278.                 var ni:XML = new XML (
  279.                 "<node layer=\"L1\" id=\"Plane" + i + "\" name=\"Plane" + i + "\">" +
  280.                     "<matrix>" +
  281.                         mi.a + " " + mi.b + " " + mi.c + " " + mi.d + " " +
  282.                         mi.e + " " + mi.f + " " + mi.g + " " + mi.h + " " +
  283.                         mi.i + " " + mi.j + " " + mi.k + " " + mi.l + " " +
  284.                         "0.0 0.0 0.0 1.0" +
  285.                     "</matrix>" +
  286.                     "<instance_geometry url=\"#Plane-Geometry\"/>" +
  287.                 "</node>"
  288.                 );
  289.                 scene.appendChild (ni);
  290.             }
  291.             // dump to flash log
  292.             dae.children() [2].setChildren (scene);
  293.             trace ("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
  294.             trace (dae.toString ());
  295.         }
  296.     }
  297. }
noswf
Get Adobe Flash Player