Guest User

Untitled

a guest
Apr 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.utils.setTimeout;
  4.     import flash.geom.Vector3D;
  5.     import flash.geom.Matrix3D;
  6.     import flash.text.TextField;
  7.     import flash.utils.getTimer;
  8.     import flash.display.Sprite;
  9.  
  10.     public class Main extends Sprite
  11.     {
  12.         private var q0:Vector.<ParticleContainer> = new Vector.<ParticleContainer>(256, true);
  13.         private var q1:Vector.<ParticleContainer> = new Vector.<ParticleContainer>(256, true);
  14.         public function Main()
  15.         {
  16.             setTimeout(run, 100);
  17.         }
  18.        
  19.         private function run():void {
  20.             var tf:TextField = new TextField();
  21.             addChild(tf);
  22.             // Launch your application by right clicking within this class and select: Debug As > FDT SWF Application
  23.             var partCount:uint = 50000;
  24.             var partSize:uint = (3+4);
  25.             var particles:Vector.<Number> = new Vector.<Number>(partCount*partSize);
  26.             var sortedParticles:Vector.<Number> = new Vector.<Number>(partCount*partSize);
  27.             var sortParticles:Vector.<Number> = new Vector.<Number>(partCount*3);
  28.             var k:int, i:int;
  29.             var c:ParticleContainer, p:ParticleContainer;
  30.             var pos:uint;
  31.             var d1:uint = getTimer();
  32.             var firstP:ParticleContainer = new ParticleContainer();
  33.             var curPart:ParticleContainer = firstP;
  34.             // Create particles
  35.             for(i = 0;i<partCount;i++) {
  36.                 pos = i*partSize;              
  37.                 particles[pos++] = Math.random()*1000-500;
  38.                 particles[pos++] = Math.random()*1000-500;
  39.                 particles[pos++] = Math.random()*1000-500;
  40.                 particles[pos++] = 1;
  41.                 particles[pos++] = 1;
  42.                 particles[pos++] = 1;
  43.                 particles[pos++] = 1; // Color RGBA (1,1,1,1)  
  44.                 curPart.next = new ParticleContainer();
  45.                 curPart = curPart.next;                
  46.             }
  47.             var d2:uint = getTimer();
  48.             // Rotate particles
  49.             var rotZ:Number = 0.4;
  50.             var rotX:Number = 0.2;
  51.             var rotY:Number = 0.1;
  52.             var rotMat:Matrix3D = new Matrix3D();
  53.             rotMat.appendRotation(rotX, Vector3D.X_AXIS);
  54.             rotMat.appendRotation(rotY, Vector3D.Y_AXIS);
  55.             rotMat.appendRotation(rotZ, Vector3D.Z_AXIS);
  56.            
  57.             for(i = 0;i<partCount;i++) {
  58.                 pos = i*partSize;
  59.                 // Copy particle positions for rotation
  60.                 sortParticles[i*3+0] = particles[pos];
  61.                 sortParticles[i*3+1] = particles[pos+1];
  62.                 sortParticles[i*3+2] = particles[pos+2];               
  63.             }
  64.             // Transform the vectors for z sorting
  65.             rotMat.transformVectors(sortParticles, sortParticles);                 
  66.  
  67.             var d4:uint = getTimer();
  68.             // Now we got the particles transformed to new location, time to sort the real data
  69.             var q0:Vector.<ParticleContainer> = this.q0.concat();
  70.             var q1:Vector.<ParticleContainer> = this.q1.concat();
  71.                        
  72.             curPart = firstP;
  73.             for(i = 0;i<partCount;i++) {
  74.                 pos = i*partSize;
  75.                 curPart.origIdx = i;
  76.                 curPart.z = sortParticles[i*3+2];          
  77.                 curPart.sort = q0[k = int(255 & -(curPart.z+10000))];
  78.                 q0[k] = curPart;
  79.                 curPart = curPart.next;            
  80.             }
  81.                
  82.             i = 256;
  83.             while (i--)
  84.             {
  85.                 c = q0[i];
  86.                
  87.                 while (c)
  88.                 {
  89.                     p = c.sort;
  90.                     c.sort = q1[k = int(65280 & -(c.z+10000)) >> 8];
  91.                     q1[k] = c;
  92.                     c = p;
  93.                 }
  94.             }
  95.             var idx:uint = 0;
  96.             while(i++ < 255)
  97.             {
  98.                 c = q1[i];
  99.                 while(c) {
  100.                     var oldPos:uint = c.origIdx;
  101.                     pos = oldPos*partSize;
  102.                     var newPos:uint = idx*partSize;
  103.                     sortedParticles[newPos++] = particles[pos++];
  104.                     sortedParticles[newPos++] = particles[pos++];
  105.                     sortedParticles[newPos++] = particles[pos++];
  106.                     sortedParticles[newPos++] = particles[pos++];
  107.                     sortedParticles[newPos++] = particles[pos++];
  108.                     sortedParticles[newPos++] = particles[pos++];
  109.                     sortedParticles[newPos++] = particles[pos++];
  110.                     idx++; 
  111.                     c = c.sort;        
  112.                 }
  113.             }
  114.             var d5:uint = getTimer();
  115.             // Particles are now sorted!
  116.             var d3:uint = getTimer();
  117.            
  118.             tf.text=""+(d5-d2)+":"+(d5-d4);
  119.         }
  120.     }
  121. }
Add Comment
Please, Sign In to add comment