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

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

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


FAVORITE BY
:
spherezsortzsortingtrianglessphere of triangles
:
Sphere from particles in 3D
:
3dZソートしてたくさん配置した例
FORKED
  1. // forked from shapevent's Sharp Sphere
  2. package {
  3.         /*
  4.         Sharp Sphere from http://www.actionsnippet.com
  5.         
  6.         */
  7.     import flash.display.*;
  8.     import flash.geom.*;
  9.     import flash.events.*;
  10.     [SWF(width = 500, height=500, backgroundColor = 0x333333)]
  11.        public class SharpSphere extends MovieClip {
  12.               
  13.            
  14.         private var polyNum:int;
  15.         private var verts:Vector.<Number>;
  16.         private var pVerts:Vector.<Number>;
  17.         private var uvts:Vector.<Number>;
  18.         private var indices:Vector.<int>; 
  19.         private var sortedIndices:Vector.<int>;
  20.         private var faces:Array;
  21.         private var m:Matrix3D;
  22.         private var poly:Vector.<Number>;
  23.         private var transPoly:Vector.<Number>;
  24.         private var i:int;
  25.         private var inc:int;
  26.         private var tex:BitmapData;
  27.         private var grad:Shape;
  28.         private var mat:Matrix;
  29.         private var render:Shape;
  30.         private var persp:PerspectiveProjection;
  31.         private var proj:Matrix3D;
  32.         private var dx:Number;
  33.         private var dy:Number;
  34.                public function SharpSphere(){
  35.                    
  36.               // don't take a capture
  37.               Wonderfl.disable_capture();
  38.               // take a capture after 10 sec
  39.               Wonderfl.capture_delay( 10 );
  40.              
  41.              
  42.                   // init
  43.             x = stage.stageWidth / 2;
  44.             y = stage.stageHeight / 2;
  45.             
  46.             
  47.             polyNum = 3000;
  48.             // standard Vectors for using drawTriangles
  49.             verts = new Vector.<Number>();
  50.             pVerts;
  51.             uvts = new Vector.<Number>();
  52.             indices = new Vector.<int>();
  53.             // needed for z-sorting
  54.             sortedIndices;
  55.             faces = [];
  56.             
  57.             // we'll use this for tranforming points
  58.             // and as the transormation matrix for our render
  59.             m = new Matrix3D();
  60.             
  61.             // plot a poly
  62.             poly;
  63.             poly = Vector.<Number>([ 000,
  64.                                     1000,
  65.                                     0,100]);
  66.             
  67.             // temp vect for any transformed polygons
  68.             transPoly = new Vector.<Number>();
  69.             
  70.             i;
  71.             inc = 0;
  72.             for (i = 0; i<polyNum; i++){
  73.                 m.identity();
  74.                 var s:Number = (int(Math.random()*50) == 1) ? 4 + Math.random()*2 : 1 + Math.random() * 2;
  75.                 m.appendScale(s, s, 1);
  76.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  77.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  78.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  79.                 m.appendTranslation(200,0,0);
  80.                                      
  81.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  82.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  83.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  84.                 
  85.                 m.transformVectors(poly, transPoly);
  86.                 
  87.                 verts = verts.concat(transPoly);
  88.                 faces.push(new Vector3D());
  89.                 indices.push(inc++, inc++, inc++);
  90.                 uvts.push(Math.random(), Math.random(), 0, Math.random(), Math.random(), 0, Math.random(), Math.random(), 0);
  91.             }
  92.             
  93.             sortedIndices = new Vector.<int>(indices.length, true);
  94.             
  95.             // create texture
  96.             tex = new BitmapData(400,400,false, 0x000000);
  97.             grad = new Shape();
  98.             mat = new Matrix();
  99.             mat.createGradientBox(400,400,0,0,0);
  100.             with (grad.graphics){
  101.                 beginGradientFill(GradientType.LINEAR, [0x000000, 0xAA0000, 0xFFFF00], [111], [20200255], mat);
  102.                 drawRect(0,0,400,400);
  103.             }
  104.             tex.draw(grad);
  105.             
  106.             // create background
  107.             mat.createGradientBox(1600,1000,0,-550, -100);
  108.             with (grad.graphics){
  109.                 beginGradientFill(GradientType.RADIAL, [0x333333, 0xffffff], [11], [0255], mat);
  110.                 drawRect(0,0,500,500);
  111.             }
  112.             grad.x = -stage.stageWidth/2
  113.             grad.y = -stage.stageHeight/2;
  114.             addChild(grad);
  115.             
  116.             // triangles will be drawn to this
  117.             render = Shape(addChild(new Shape()));
  118.             
  119.             // fix all vector lengths
  120.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  121.             pVerts = new Vector.<Number>(verts.length/3 * 2true); 
  122.             
  123.             // we need these if we want perspective
  124.             persp = new PerspectiveProjection();
  125.             persp.fieldOfView = 45;
  126.             // projection matrix
  127.             proj = persp.toMatrix3D();
  128.             
  129.             dx = 0, dy = 0;
  130.             addEventListener(Event.ENTER_FRAME, onLoop);
  131.             
  132.                }
  133.                // private methods
  134.         private function onLoop(evt:Event):void {
  135.             dx += (mouseX - dx) / 4;
  136.             dy += (mouseY - dy) / 4;
  137.             m.identity();
  138.             m.appendRotation(dy, Vector3D.X_AXIS);
  139.             m.appendRotation(dx, Vector3D.Y_AXIS);
  140.             // push everything back so its not to close
  141.             m.appendTranslation(0,0,800);
  142.             // append the projection matrix at the end
  143.             m.append(proj);
  144.             
  145.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  146.             
  147.             var face:Vector3D;
  148.             inc = 0;
  149.             for (var i:int = 0; i<indices.length; i+=3){
  150.                 face = faces[inc];
  151.                 face.x = indices[i];
  152.                 // it may look odd, but casting to an int 
  153.                 // when doing operations inside array sytnax
  154.                 // adds a big speed boost
  155.                 face.y = indices[int(i + 1)];
  156.                 face.z = indices[int(i + 2)];
  157.                 var i3:int = i * 3;
  158.                 // get the average z position (t value) and store it in the Vector3D w property
  159.                 // depending on your model, you may not need to do an average of all 3 values
  160.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  161.                 inc++;
  162.             }
  163.              
  164.             // sort on w, so far this beats all other sorting methods for speed,
  165.             // faster than Vector.sort(), faster than any custom sort method I could find
  166.             //faces.sortOn("w", Array.NUMERIC);
  167.         
  168.             // re-order indices so that faces are drawn in the correct order (back to front);
  169.             inc = 0;
  170.             for each (face in faces){
  171.                 sortedIndices[inc++] = face.x;
  172.                 sortedIndices[inc++] = face.y;
  173.                 sortedIndices[inc++] = face.z;
  174.             }
  175.             
  176.             render.graphics.clear();
  177.             render.graphics.beginBitmapFill(tex, nullfalsefalse);
  178.             render.graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  179.         }
  180.         
  181.        }
  182. }
noswf
  1. // forked from shapevent's Sharp Sphere
  2. package {
  3.         /*
  4.         Sharp Sphere from http://www.actionsnippet.com
  5.         
  6.         */
  7.     import flash.display.*;
  8.     import flash.geom.*;
  9.     import flash.events.*;
  10.     [SWF(width = 500, height=500, backgroundColor = 0x333333)]
  11.        public class SharpSphere extends MovieClip {
  12.               
  13.            
  14.         private var polyNum:int;
  15.         private var verts:Vector.<Number>;
  16.         private var pVerts:Vector.<Number>;
  17.         private var uvts:Vector.<Number>;
  18.         private var indices:Vector.<int>;
  19.         private var sortedIndices:Vector.<int>;
  20.         private var faces:Array;
  21.         private var m:Matrix3D;
  22.         private var poly:Vector.<Number>;
  23.         private var transPoly:Vector.<Number>;
  24.         private var i:int;
  25.         private var inc:int;
  26.         private var tex:BitmapData;
  27.         private var grad:Shape;
  28.         private var mat:Matrix;
  29.         private var render:Shape;
  30.         private var persp:PerspectiveProjection;
  31.         private var proj:Matrix3D;
  32.         private var dx:Number;
  33.         private var dy:Number;
  34.                public function SharpSphere(){
  35.                    
  36.               // don't take a capture
  37.               Wonderfl.disable_capture();
  38.               // take a capture after 10 sec
  39.               Wonderfl.capture_delay( 10 );
  40.              
  41.              
  42.                   // init
  43.             x = stage.stageWidth / 2;
  44.             y = stage.stageHeight / 2;
  45.             
  46.             
  47.             polyNum = 3000;
  48.             // standard Vectors for using drawTriangles
  49.             verts = new Vector.<Number>();
  50.             pVerts;
  51.             uvts = new Vector.<Number>();
  52.             indices = new Vector.<int>();
  53.             // needed for z-sorting
  54.             sortedIndices;
  55.             faces = [];
  56.             
  57.             // we'll use this for tranforming points
  58.             // and as the transormation matrix for our render
  59.             m = new Matrix3D();
  60.             
  61.             // plot a poly
  62.             poly;
  63.             poly = Vector.<Number>([ 000,
  64.                                     1000,
  65.                                     0,100]);
  66.             
  67.             // temp vect for any transformed polygons
  68.             transPoly = new Vector.<Number>();
  69.             
  70.             i;
  71.             inc = 0;
  72.             for (i = 0; i<polyNum; i++){
  73.                 m.identity();
  74.                 var s:Number = (int(Math.random()*50) == 1) ? 4 + Math.random()*2 : 1 + Math.random() * 2;
  75.                 m.appendScale(s, s, 1);
  76.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  77.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  78.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  79.                 m.appendTranslation(200,0,0);
  80.                                      
  81.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  82.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  83.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  84.                 
  85.                 m.transformVectors(poly, transPoly);
  86.                 
  87.                 verts = verts.concat(transPoly);
  88.                 faces.push(new Vector3D());
  89.                 indices.push(inc++, inc++, inc++);
  90.                 uvts.push(Math.random(), Math.random(), 0, Math.random(), Math.random(), 0, Math.random(), Math.random(), 0);
  91.             }
  92.             
  93.             sortedIndices = new Vector.<int>(indices.length, true);
  94.             
  95.             // create texture
  96.             tex = new BitmapData(400,400,false, 0x000000);
  97.             grad = new Shape();
  98.             mat = new Matrix();
  99.             mat.createGradientBox(400,400,0,0,0);
  100.             with (grad.graphics){
  101.                 beginGradientFill(GradientType.LINEAR, [0x000000, 0xAA0000, 0xFFFF00], [111], [20200255], mat);
  102.                 drawRect(0,0,400,400);
  103.             }
  104.             tex.draw(grad);
  105.             
  106.             // create background
  107.             mat.createGradientBox(1600,1000,0,-550, -100);
  108.             with (grad.graphics){
  109.                 beginGradientFill(GradientType.RADIAL, [0x333333, 0xffffff], [11], [0255], mat);
  110.                 drawRect(0,0,500,500);
  111.             }
  112.             grad.x = -stage.stageWidth/2
  113.             grad.y = -stage.stageHeight/2;
  114.             addChild(grad);
  115.             
  116.             // triangles will be drawn to this
  117.             render = Shape(addChild(new Shape()));
  118.             
  119.             // fix all vector lengths
  120.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  121.             pVerts = new Vector.<Number>(verts.length/3 * 2true); 
  122.             
  123.             // we need these if we want perspective
  124.             persp = new PerspectiveProjection();
  125.             persp.fieldOfView = 45;
  126.             // projection matrix
  127.             proj = persp.toMatrix3D();
  128.             
  129.             dx = 0, dy = 0;
  130.             addEventListener(Event.ENTER_FRAME, onLoop);
  131.             
  132.                }
  133.                // private methods
  134.         private function onLoop(evt:Event):void {
  135.             dx += (mouseX - dx) / 4;
  136.             dy += (mouseY - dy) / 4;
  137.             m.identity();
  138.             m.appendRotation(dy, Vector3D.X_AXIS);
  139.             m.appendRotation(dx, Vector3D.Y_AXIS);
  140.             // push everything back so its not to close
  141.             m.appendTranslation(0,0,800);
  142.             // append the projection matrix at the end
  143.             m.append(proj);
  144.             
  145.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  146.             
  147.             var face:Vector3D;
  148.             inc = 0;
  149.             for (var i:int = 0; i<indices.length; i+=3){
  150.                 face = faces[inc];
  151.                 face.x = indices[i];
  152.                 // it may look odd, but casting to an int 
  153.                 // when doing operations inside array sytnax
  154.                 // adds a big speed boost
  155.                 face.y = indices[int(i + 1)];
  156.                 face.z = indices[int(i + 2)];
  157.                 var i3:int = i * 3;
  158.                 // get the average z position (t value) and store it in the Vector3D w property
  159.                 // depending on your model, you may not need to do an average of all 3 values
  160.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  161.                 inc++;
  162.             }
  163.              
  164.             // sort on w, so far this beats all other sorting methods for speed,
  165.             // faster than Vector.sort(), faster than any custom sort method I could find
  166.             faces.sortOn("w"Array.NUMERIC);
  167.         
  168.             // re-order indices so that faces are drawn in the correct order (back to front);
  169.             inc = 0;
  170.             for each (face in faces){
  171.                 sortedIndices[inc++] = face.x;
  172.                 sortedIndices[inc++] = face.y;
  173.                 sortedIndices[inc++] = face.z;
  174.             }
  175.             
  176.             render.graphics.clear();
  177.             render.graphics.beginBitmapFill(tex, nullfalsefalse);
  178.             render.graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  179.         }
  180.         
  181.        }
  182. }
noswf
  1. // forked from shapevent's Sharp Sphere
  2. package {
  3.         /*
  4.         Sharp Sphere from http://www.actionsnippet.com
  5.         
  6.         */
  7.     import flash.display.*;
  8.     import flash.geom.*;
  9.     import flash.events.*;
  10.     [SWF(width = 500, height=500, backgroundColor = 0x333333)]
  11.        public class SharpSphere extends MovieClip {
  12.               
  13.            
  14.         private var polyNum:int;
  15.         private var verts:Vector.<Number>;
  16.         private var pVerts:Vector.<Number>;
  17.         private var uvts:Vector.<Number>;
  18.         private var indices:Vector.<int>;
  19.         private var sortedIndices:Vector.<int>;
  20.         private var faces:Array;
  21.         private var m:Matrix3D;
  22.         private var poly:Vector.<Number>;
  23.         private var transPoly:Vector.<Number>;
  24.         private var i:int;
  25.         private var inc:int;
  26.         private var tex:BitmapData;
  27.         private var grad:Shape;
  28.         private var mat:Matrix;
  29.         private var render:Shape;
  30.         private var persp:PerspectiveProjection;
  31.         private var proj:Matrix3D;
  32.         private var dx:Number;
  33.         private var dy:Number;
  34.                public function SharpSphere(){
  35.                    
  36.               // don't take a capture
  37.               Wonderfl.disable_capture();
  38.               // take a capture after 10 sec
  39.               Wonderfl.capture_delay( 10 );
  40.              
  41.              
  42.                   // init
  43.             x = stage.stageWidth / 2;
  44.             y = stage.stageHeight / 2;
  45.             
  46.             
  47.             polyNum = 1024;
  48.             // standard Vectors for using drawTriangles
  49.             verts = new Vector.<Number>();
  50.             pVerts;
  51.             uvts = new Vector.<Number>();
  52.             indices = new Vector.<int>();
  53.             // needed for z-sorting
  54.             sortedIndices;
  55.             faces = [];
  56.             
  57.             // we'll use this for tranforming points
  58.             // and as the transormation matrix for our render
  59.             m = new Matrix3D();
  60.             
  61.             // plot a poly
  62.             poly;
  63.             poly = Vector.<Number>([ 000,
  64.                                     1000,
  65.                                     0,100]);
  66.             
  67.             // temp vect for any transformed polygons
  68.             transPoly = new Vector.<Number>();
  69.             
  70.             i;
  71.             inc = 0;
  72.             for (i = 0; i<polyNum; i++){
  73.                 m.identity();
  74.                 var s:Number = (int(Math.random()*512) == 1) ? 4 + Math.random()*2 : 1 + Math.random() * 2;
  75.                 m.appendScale(s*8, s*11);
  76.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  77.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  78.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  79.                 m.appendTranslation(200,0,0);
  80.                                      
  81.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  82.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  83.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  84.                 
  85.                 m.transformVectors(poly, transPoly);
  86.                 
  87.                 verts = verts.concat(transPoly);
  88.                 faces.push(new Vector3D());
  89.                 indices.push(inc++, inc++, inc++);
  90.                 uvts.push(Math.random(), Math.random(), 0, Math.random(), Math.random(), 0, Math.random(), Math.random(), 0);
  91.             }
  92.             
  93.             sortedIndices = new Vector.<int>(indices.length, true);
  94.             
  95.             // create texture
  96.             tex = new BitmapData(400,400,false, 0x000000);
  97.             grad = new Shape();
  98.             mat = new Matrix();
  99.             mat.createGradientBox(400,400,0,0,0);
  100.             with (grad.graphics){
  101.                 beginGradientFill(GradientType.LINEAR, [0x000000, 0xAA0000, 0xFFFF00], [111], [20200255], mat);
  102.                 drawRect(0,0,400,400);
  103.             }
  104.             tex.draw(grad);
  105.             
  106.             // create background
  107.             mat.createGradientBox(1600,1000,0,-550, -100);
  108.             with (grad.graphics){
  109.                 beginGradientFill(GradientType.RADIAL, [0x333333, 0xffffff], [11], [0255], mat);
  110.                 drawRect(0,0,500,500);
  111.             }
  112.             grad.x = -stage.stageWidth/2
  113.             grad.y = -stage.stageHeight/2;
  114.             addChild(grad);
  115.             
  116.             // triangles will be drawn to this
  117.             render = Shape(addChild(new Shape()));
  118.             
  119.             // fix all vector lengths
  120.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  121.             pVerts = new Vector.<Number>(verts.length/3 * 2true); 
  122.             
  123.             // we need these if we want perspective
  124.             persp = new PerspectiveProjection();
  125.             persp.fieldOfView = 100;
  126.             // projection matrix
  127.             proj = persp.toMatrix3D();
  128.             
  129.             dx = 0, dy = 0;
  130.             addEventListener(Event.ENTER_FRAME, onLoop);
  131.             
  132.                }
  133.                // private methods
  134.         private function onLoop(evt:Event):void {
  135.             dx += (mouseX - dx) / 4;
  136.             dy += (mouseY - dy) / 4;
  137.             m.identity();
  138.             m.appendRotation(dy, Vector3D.X_AXIS);
  139.             m.appendRotation(dx, Vector3D.Y_AXIS);
  140.             // push everything back so its not to close
  141.             m.appendTranslation(0,0,400);
  142.             // append the projection matrix at the end
  143.             m.append(proj);
  144.             
  145.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  146.             
  147.             var face:Vector3D;
  148.             inc = 0;
  149.             for (var i:int = 0; i<indices.length; i+=3){
  150.                 face = faces[inc];
  151.                 face.x = indices[i];
  152.                 // it may look odd, but casting to an int 
  153.                 // when doing operations inside array sytnax
  154.                 // adds a big speed boost
  155.                 face.y = indices[int(i + 1)];
  156.                 face.z = indices[int(i + 2)];
  157.                 var i3:int = i * 3;
  158.                 // get the average z position (t value) and store it in the Vector3D w property
  159.                 // depending on your model, you may not need to do an average of all 3 values
  160.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  161.                 inc++;
  162.             }
  163.              
  164.             // sort on w, so far this beats all other sorting methods for speed,
  165.             // faster than Vector.sort(), faster than any custom sort method I could find
  166.             faces.sortOn("w"Array.NUMERIC);
  167.         
  168.             // re-order indices so that faces are drawn in the correct order (back to front);
  169.             inc = 0;
  170.             for each (face in faces){
  171.                 sortedIndices[inc++] = face.x;
  172.                 sortedIndices[inc++] = face.y;
  173.                 sortedIndices[inc++] = face.z;
  174.             }
  175.             
  176.             render.graphics.clear();
  177.             render.graphics.beginBitmapFill(tex, nullfalsefalse);
  178.             render.graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  179.         }
  180.         
  181.        }
  182. }
noswf
  1. // forked from shapevent's Sharp Sphere
  2. package {
  3.         /*
  4.         Sharp Sphere from http://www.actionsnippet.com
  5.         
  6.         */
  7.     import flash.display.*;
  8.     import flash.geom.*;
  9.     import flash.events.*;
  10.     [SWF(width = 500, height=500, backgroundColor = 0x333333)]
  11.        public class SharpSphere extends MovieClip {
  12.               
  13.            
  14.         private var polyNum:int;
  15.         private var verts:Vector.<Number>;
  16.         private var pVerts:Vector.<Number>;
  17.         private var uvts:Vector.<Number>;
  18.         private var indices:Vector.<int>;
  19.         private var sortedIndices:Vector.<int>;
  20.         private var faces:Array;
  21.         private var m:Matrix3D;
  22.         private var poly:Vector.<Number>;
  23.         private var transPoly:Vector.<Number>;
  24.         private var i:int;
  25.         private var inc:int;
  26.         private var tex:BitmapData;
  27.         private var grad:Shape;
  28.         private var mat:Matrix;
  29.         private var render:Shape;
  30.         private var persp:PerspectiveProjection;
  31.         private var proj:Matrix3D;
  32.         private var dx:Number;
  33.         private var dy:Number;
  34.                public function SharpSphere(){
  35.                    
  36.               // don't take a capture
  37.               Wonderfl.disable_capture();
  38.               // take a capture after 10 sec
  39.               Wonderfl.capture_delay( 10 );
  40.              
  41.              
  42.                   // init
  43.             x = stage.stageWidth / 2;
  44.             y = stage.stageHeight / 2;
  45.             
  46.             
  47.             polyNum = 10000;
  48.             // standard Vectors for using drawTriangles
  49.             verts = new Vector.<Number>();
  50.             pVerts;
  51.             uvts = new Vector.<Number>();
  52.             indices = new Vector.<int>();
  53.             // needed for z-sorting
  54.             sortedIndices;
  55.             faces = [];
  56.             
  57.             // we'll use this for tranforming points
  58.             // and as the transormation matrix for our render
  59.             m = new Matrix3D();
  60.             
  61.             // plot a poly
  62.             poly;
  63.             poly = Vector.<Number>([ 000,
  64.                                     1000,
  65.                                     0,100]);
  66.             
  67.             // temp vect for any transformed polygons
  68.             transPoly = new Vector.<Number>();
  69.             
  70.             i;
  71.             inc = 0;
  72.             for (i = 0; i<polyNum; i++){
  73.                 m.identity();
  74.                 var s:Number = (int(Math.random()*50) == 1) ? 4 + Math.random()*2 : 1 + Math.random() * 2;
  75.                 m.appendScale(s, s, 1);
  76.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  77.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  78.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  79.                 m.appendTranslation(200,0,0);
  80.                                      
  81.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  82.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  83.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  84.                 
  85.                 m.transformVectors(poly, transPoly);
  86.                 
  87.                 verts = verts.concat(transPoly);
  88.                 faces.push(new Vector3D());
  89.                 indices.push(inc++, inc++, inc++);
  90.                 uvts.push(Math.random(), Math.random(), 0, Math.random(), Math.random(), 0, Math.random(), Math.random(), 0);
  91.             }
  92.             
  93.             sortedIndices = new Vector.<int>(indices.length, true);
  94.             
  95.             // create texture
  96.             tex = new BitmapData(400,400,false, 0x000000);
  97.             grad = new Shape();
  98.             mat = new Matrix();
  99.             mat.createGradientBox(400,400,0,0,0);
  100.             with (grad.graphics){
  101.                 beginGradientFill(GradientType.LINEAR, [0x000000, 0xAA0000, 0xFFFF00], [111], [20200255], mat);
  102.                 drawRect(0,0,400,400);
  103.             }
  104.             tex.draw(grad);
  105.             
  106.             // create background
  107.             mat.createGradientBox(1600,1000,0,-550, -100);
  108.             with (grad.graphics){
  109.                 beginGradientFill(GradientType.RADIAL, [0x333333, 0xffffff], [11], [0255], mat);
  110.                 drawRect(0,0,500,500);
  111.             }
  112.             grad.x = -stage.stageWidth/2
  113.             grad.y = -stage.stageHeight/2;
  114.             addChild(grad);
  115.             
  116.             // triangles will be drawn to this
  117.             render = Shape(addChild(new Shape()));
  118.             
  119.             // fix all vector lengths
  120.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  121.             pVerts = new Vector.<Number>(verts.length/3 * 2true); 
  122.             
  123.             // we need these if we want perspective
  124.             persp = new PerspectiveProjection();
  125.             persp.fieldOfView = 45;
  126.             // projection matrix
  127.             proj = persp.toMatrix3D();
  128.             
  129.             dx = 0, dy = 0;
  130.             addEventListener(Event.ENTER_FRAME, onLoop);
  131.             
  132.                }
  133.                // private methods
  134.         private function onLoop(evt:Event):void {
  135.             dx += (mouseX - dx) / 4;
  136.             dy += (mouseY - dy) / 4;
  137.             m.identity();
  138.             m.appendRotation(dy, Vector3D.X_AXIS);
  139.             m.appendRotation(dx, Vector3D.Y_AXIS);
  140.             // push everything back so its not to close
  141.             m.appendTranslation(0,0,800);
  142.             // append the projection matrix at the end
  143.             m.append(proj);
  144.             
  145.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  146.             
  147.             var face:Vector3D;
  148.             inc = 0;
  149.             for (var i:int = 0; i<indices.length; i+=3){
  150.                 face = faces[inc];
  151.                 face.x = indices[i];
  152.                 // it may look odd, but casting to an int 
  153.                 // when doing operations inside array sytnax
  154.                 // adds a big speed boost
  155.                 face.y = indices[int(i + 1)];
  156.                 face.z = indices[int(i + 2)];
  157.                 var i3:int = i * 3;
  158.                 // get the average z position (t value) and store it in the Vector3D w property
  159.                 // depending on your model, you may not need to do an average of all 3 values
  160.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  161.                 inc++;
  162.             }
  163.              
  164.             // sort on w, so far this beats all other sorting methods for speed,
  165.             // faster than Vector.sort(), faster than any custom sort method I could find
  166.             faces.sortOn("w"Array.NUMERIC);
  167.         
  168.             // re-order indices so that faces are drawn in the correct order (back to front);
  169.             inc = 0;
  170.             for each (face in faces){
  171.                 sortedIndices[inc++] = face.x;
  172.                 sortedIndices[inc++] = face.y;
  173.                 sortedIndices[inc++] = face.z;
  174.             }
  175.             
  176.             render.graphics.clear();
  177.             render.graphics.beginBitmapFill(tex, nullfalsefalse);
  178.             render.graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  179.         }
  180.         
  181.        }
  182. }
noswf
  1. // forked from shapevent's Sharp Sphere
  2. package {
  3.         /*
  4.         Sharp Sphere from http://www.actionsnippet.com
  5.         
  6.         */
  7.     import flash.display.*;
  8.     import flash.geom.*;
  9.     import flash.events.*;
  10.     [SWF(width = 500, height=500, backgroundColor = 0x333333)]
  11.        public class SharpSphere extends MovieClip {
  12.               
  13.            
  14.         private var polyNum:int;
  15.         private var verts:Vector.<Number>;
  16.         private var pVerts:Vector.<Number>;
  17.         private var uvts:Vector.<Number>;
  18.         private var indices:Vector.<int>;
  19.         private var sortedIndices:Vector.<int>;
  20.         private var faces:Array;
  21.         private var m:Matrix3D;
  22.         private var poly:Vector.<Number>;
  23.         private var transPoly:Vector.<Number>;
  24.         private var i:int;
  25.         private var inc:int;
  26.         private var tex:BitmapData;
  27.         private var grad:Shape;
  28.         private var mat:Matrix;
  29.         private var render:Shape;
  30.         private var persp:PerspectiveProjection;
  31.         private var proj:Matrix3D;
  32.         private var dx:Number;
  33.         private var dy:Number;
  34.                public function SharpSphere(){
  35.                    
  36.               // don't take a capture
  37.               Wonderfl.disable_capture();
  38.               // take a capture after 10 sec
  39.               Wonderfl.capture_delay( 10 );
  40.              
  41.              
  42.                   // init
  43.             x = stage.stageWidth / 2;
  44.             y = stage.stageHeight / 2;
  45.             
  46.             
  47.             polyNum = 3000;
  48.             // standard Vectors for using drawTriangles
  49.             verts = new Vector.<Number>();
  50.             pVerts;
  51.             uvts = new Vector.<Number>();
  52.             indices = new Vector.<int>();
  53.             // needed for z-sorting
  54.             sortedIndices;
  55.             faces = [];
  56.             
  57.             // we'll use this for tranforming points
  58.             // and as the transormation matrix for our render
  59.             m = new Matrix3D();
  60.             
  61.             // plot a poly
  62.             poly;
  63.             poly = Vector.<Number>([ 000,
  64.                                     1000,
  65.                                     0,100]);
  66.             
  67.             // temp vect for any transformed polygons
  68.             transPoly = new Vector.<Number>();
  69.             
  70.             i;
  71.             inc = 0;
  72.             for (i = 0; i<polyNum; i++){
  73.                 m.identity();
  74.                 var s:Number = (int(Math.random()*50) == 1) ? 4 + Math.random()*2 : 1 + Math.random() * 2;
  75.                 m.appendScale(s, s, 1);
  76.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  77.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  78.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  79.                 m.appendTranslation(200,0,0);
  80.                                      
  81.                 m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  82.                 m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  83.                 m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  84.                 
  85.                 m.transformVectors(poly, transPoly);
  86.                 
  87.                 verts = verts.concat(transPoly);
  88.                 faces.push(new Vector3D());
  89.                 indices.push(inc++, inc++, inc++);
  90.                 uvts.push(Math.random(), Math.random(), 0, Math.random(), Math.random(), 0, Math.random(), Math.random(), 0);
  91.             }
  92.             
  93.             sortedIndices = new Vector.<int>(indices.length, true);
  94.             
  95.             // create texture
  96.             tex = new BitmapData(400,400,false, 0x000000);
  97.             grad = new Shape();
  98.             mat = new Matrix();
  99.             mat.createGradientBox(400,400,0,0,0);
  100.             with (grad.graphics){
  101.                 beginGradientFill(GradientType.LINEAR, [0x000000, 0xAA0000, 0xFFFF00], [111], [20200255], mat);
  102.                 drawRect(0,0,400,400);
  103.             }
  104.             tex.draw(grad);
  105.             
  106.             // create background
  107.             mat.createGradientBox(1600,1000,0,-550, -100);
  108.             with (grad.graphics){
  109.                 beginGradientFill(GradientType.RADIAL, [0x333333, 0xffffff], [11], [0255], mat);
  110.                 drawRect(0,0,500,500);
  111.             }
  112.             grad.x = -stage.stageWidth/2
  113.             grad.y = -stage.stageHeight/2;
  114.             addChild(grad);
  115.             
  116.             // triangles will be drawn to this
  117.             render = Shape(addChild(new Shape()));
  118.             
  119.             // fix all vector lengths
  120.             verts.fixed = true, uvts.fixed = true, indices.fixed = true
  121.             pVerts = new Vector.<Number>(verts.length/3 * 2true); 
  122.             
  123.             // we need these if we want perspective
  124.             persp = new PerspectiveProjection();
  125.             persp.fieldOfView = 45;
  126.             // projection matrix
  127.             proj = persp.toMatrix3D();
  128.             
  129.             dx = 0, dy = 0;
  130.             addEventListener(Event.ENTER_FRAME, onLoop);
  131.             
  132.                }
  133.                // private methods
  134.         private function onLoop(evt:Event):void {
  135.             dx += (mouseX - dx) / 4;
  136.             dy += (mouseY - dy) / 4;
  137.             m.identity();
  138.             m.appendRotation(dy, Vector3D.X_AXIS);
  139.             m.appendRotation(dx, Vector3D.Y_AXIS);
  140.             // push everything back so its not to close
  141.             m.appendTranslation(0,0,800);
  142.             // append the projection matrix at the end
  143.             m.append(proj);
  144.             
  145.             Utils3D.projectVectors(m, verts, pVerts, uvts);
  146.             
  147.             var face:Vector3D;
  148.             inc = 0;
  149.             for (var i:int = 0; i<indices.length; i+=3){
  150.                 face = faces[inc];
  151.                 face.x = indices[i];
  152.                 // it may look odd, but casting to an int 
  153.                 // when doing operations inside array sytnax
  154.                 // adds a big speed boost
  155.                 face.y = indices[int(i + 1)];
  156.                 face.z = indices[int(i + 2)];
  157.                 var i3:int = i * 3;
  158.                 // get the average z position (t value) and store it in the Vector3D w property
  159.                 // depending on your model, you may not need to do an average of all 3 values
  160.                 face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
  161.                 inc++;
  162.             }
  163.              
  164.             // sort on w, so far this beats all other sorting methods for speed,
  165.             // faster than Vector.sort(), faster than any custom sort method I could find
  166.             faces.sortOn("w"Array.NUMERIC);
  167.         
  168.             // re-order indices so that faces are drawn in the correct order (back to front);
  169.             inc = 0;
  170.             for each (face in faces){
  171.                 sortedIndices[inc++] = face.x;
  172.                 sortedIndices[inc++] = face.y;
  173.                 sortedIndices[inc++] = face.z;
  174.             }
  175.             
  176.             render.graphics.clear();
  177.             render.graphics.beginBitmapFill(tex, nullfalsefalse);
  178.             render.graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
  179.         }
  180.         
  181.        }
  182. }
noswf
Get Adobe Flash Player