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

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

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


FAVORITE BY
:
:
Zソート3ddrawTrianglesポリゴンのZソート
:
:
Zソート非常に分かりやすいZソートのやりかた(It's easy to understand!)
FORKED
  1. // forked from shapevent's Z-Sorting and drawTriangles()
  2. package {
  3.         /*
  4.         a z-sorting example using drawTriangles()
  5.         
  6.         more info here : http://actionsnippet.com/?p=2080
  7.         */
  8.     import flash.display.*;
  9.     import flash.geom.*;
  10.     import flash.events.*;
  11.     [SWF(width = 500, height=500, backgroundColor = 0x000000)]
  12.        public class Zsorting extends MovieClip {
  13.         private var verts:Vector.<Number>;
  14.         private var pVerts:Vector.<Number>;
  15.         private var uvts:Vector.<Number>;
  16.         private var indices:Vector.<int>;
  17.         private var sortedIndices:Vector.<int>;
  18.         private var faces:Array;
  19.         private var m:Matrix3D;
  20.         private var poly:Vector.<Number>;
  21.         private var transPoly:Vector.<Number>;
  22.         private var tex:BitmapData;
  23.         private var persp:PerspectiveProjection;
  24.         private var proj:Matrix3D;
  25.                public function Zsorting(){
  26.                   // init
  27.             x = stage.stageWidth / 2;
  28.             y = stage.stageHeight / 2;
  29.             // standard Vectors for using drawTriangles
  30.             verts = new Vector.<Number>();
  31.             pVerts;
  32.             uvts;
  33.             indices;
  34.             // needed for z-sorting
  35.             sortedIndices;
  36.             faces = [];
  37.             // we'll use this for tranforming points
  38.             // and as the transormation matrix for our render
  39.             m = new Matrix3D();
  40.             // plot a poly
  41.             poly;
  42.             poly = Vector.<Number>([ 000,
  43.                                     1000,
  44.                                     0,100]);
  45.             verts = verts.concat(poly);
  46.             faces.push(new Vector3D());
  47.             // add two polygons by transorming the poly Vector
  48.             // and concatenating it to the verts Vector
  49.             // Vector3D instances are added to the faces
  50.             // Array for z-sorting
  51.             // temp vect for any transformed polygons
  52.             transPoly = new Vector.<Number>();
  53.             m.appendRotation(10, Vector3D.Z_AXIS);
  54.             m.appendTranslation(0,-2,5);
  55.             m.transformVectors(poly, transPoly);
  56.             verts = verts.concat(transPoly);
  57.             faces.push(new Vector3D());
  58.             m.appendRotation(10, Vector3D.Z_AXIS);
  59.             m.appendTranslation(0,-2,5);
  60.             m.transformVectors(poly, transPoly);
  61.             verts = verts.concat(transPoly);
  62.             faces.push(new Vector3D());
  63.             // hard coded indices
  64.             indices = Vector.<int>([012345678]);
  65.             sortedIndices = new Vector.<int>(indices.length, true);
  66.             // create texture
  67.             tex = new BitmapData(100,100,false, 0x000000);
  68.             tex.fillRect(new Rectangle(0,0,50,50), 0xFF0000);
  69.             tex.fillRect(new Rectangle(50,0,50,50), 0x0000FF);
  70.             tex.fillRect(new Rectangle(0,50,50,50), 0x00FF00);
  71.             // hard coded uvts
  72.             uvts = Vector.<Number>([0,0,0, .5,0,00,.5,0,
  73.                                     .5300100, .53, .50,
  74.                                      0,.6,.50,.6,00,10]);
  75.             // fix all vector lengths
  76.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  77.             pVerts = new Vector.<Number>(verts.length/3 * 2true);
  78.             // we need these if we want perspective
  79.             persp = new PerspectiveProjection();
  80.             // projection matrix
  81.             proj = persp.toMatrix3D();
  82.             addEventListener(Event.ENTER_FRAME, onLoop);
  83.             
  84.                }
  85.                // private methods
  86.         private function onLoop(evt:Event):void {
  87.             m.identity();
  88.             m.appendRotation(mouseY * 2, Vector3D.X_AXIS);
  89.             m.appendRotation(mouseX * 2, Vector3D.Y_AXIS);
  90.             // push everything back so its not to close
  91.             m.appendTranslation(0,0,40);
  92.             // append the projection matrix at the end
  93.             m.append(proj);
  94.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  95.             var face:Vector3D;
  96.             var inc:int = 0;
  97.             for (var i:int = 0; i<indices.length; i+=3){
  98.                 face = faces[inc];
  99.                 face.x = indices[i];
  100.                 // it may look odd, but casting to an int
  101.                 // when doing operations inside array sytnax
  102.                 // adds a big speed boost
  103.                 face.y = indices[int(i + 1)];
  104.                 face.z = indices[int(i + 2)];
  105.                 var i3:int = i * 3;
  106.                 // get the average z position (t value) and store it in the Vector3D w property
  107.                 // depending on your model, you may not need to do an average of all 3 values
  108.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  109.                 inc++;
  110.             }
  111.             // sort on w, so far this beats all other sorting methods for speed,
  112.             // faster than Vector.sort(), faster than any custom sort method I could find
  113.             faces.sortOn("w"Array.NUMERIC);
  114.             // re-order indices so that faces are drawn in the correct order (back to front);
  115.             inc = 0;
  116.             for each (face in faces){
  117.                 sortedIndices[inc++] = face.x;
  118.                 sortedIndices[inc++] = face.y;
  119.                 sortedIndices[inc++] = face.z;
  120.             }
  121.             graphics.clear();
  122.             graphics.beginBitmapFill(tex, nullfalsefalse);
  123.             graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  124.         }
  125.         
  126.        }
  127. }
noswf
Get Adobe Flash Player