/StageEditor/src/org/flintparticles/threeD/particles/Particle3DUtils.as

https://github.com/lvshiling/qizhon · ActionScript · 161 lines · 126 code · 4 blank · 31 comment · 16 complexity · 0b40a48d5c824941a3082b5a36c34561 MD5 · raw file

  1. /*
  2. * FLINT PARTICLE SYSTEM
  3. * .....................
  4. *
  5. * Author: Richard Lord
  6. * Copyright (c) Richard Lord 2008-2011
  7. * http://flintparticles.org/
  8. *
  9. * Licence Agreement
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. package org.flintparticles.threeD.particles
  30. {
  31. import org.flintparticles.common.particles.Particle;
  32. import org.flintparticles.common.particles.ParticleFactory;
  33. import flash.display.Bitmap;
  34. import flash.display.BitmapData;
  35. import flash.display.Sprite;
  36. import flash.geom.Point;
  37. import flash.geom.Rectangle;
  38. import flash.geom.Vector3D;
  39. /**
  40. * Utility methods for working with three-d particles.
  41. */
  42. public class Particle3DUtils
  43. {
  44. public static function createPixelParticlesFromBitmapData( bitmapData:BitmapData, factory:ParticleFactory = null, offset:Vector3D = null ):Vector.<Particle>
  45. {
  46. if( offset == null )
  47. {
  48. offset = new Vector3D( 0, 0, 0, 1 );
  49. }
  50. var particles:Vector.<Particle> = new Vector.<Particle>();
  51. var width:int = bitmapData.width;
  52. var height:int = bitmapData.height;
  53. var y:int;
  54. var x:int;
  55. var p:Particle3D;
  56. var color:uint;
  57. if( factory )
  58. {
  59. for( y = 0; y < height; ++y )
  60. {
  61. for( x = 0; x < width; ++x )
  62. {
  63. color = bitmapData.getPixel32( x, y );
  64. if( color >>> 24 > 0 )
  65. {
  66. p = Particle3D( factory.createParticle() );
  67. p.position = new Vector3D( x + offset.x, y + offset.y, offset.z, 1 );
  68. p.color = color;
  69. particles.push( p );
  70. }
  71. }
  72. }
  73. }
  74. else
  75. {
  76. for( y = 0; y < height; ++y )
  77. {
  78. for( x = 0; x < width; ++x )
  79. {
  80. color = bitmapData.getPixel32( x, y );
  81. if( color >>> 24 > 0 )
  82. {
  83. p = new Particle3D();
  84. p.position = new Vector3D( x + offset.x, y + offset.y, offset.z, 1 );
  85. p.color = color;
  86. particles.push( p );
  87. }
  88. }
  89. }
  90. }
  91. return particles;
  92. }
  93. public static function createRectangleParticlesFromBitmapData( bitmapData:BitmapData, size:uint, factory:ParticleFactory = null, offset:Vector3D = null ):Vector.<Particle>
  94. {
  95. if( offset == null )
  96. {
  97. offset = new Vector3D( 0, 0, 0, 1 );
  98. }
  99. var particles:Vector.<Particle> = new Vector.<Particle>();
  100. var width:int = bitmapData.width;
  101. var height:int = bitmapData.height;
  102. var y:int;
  103. var x:int;
  104. var halfSize:Number = size * 0.5;
  105. offset.x += halfSize;
  106. offset.y += halfSize;
  107. var p:Particle3D;
  108. var b:BitmapData;
  109. var m:Bitmap;
  110. var s:Sprite;
  111. var zero:Point = new Point( 0, 0 );
  112. if( factory )
  113. {
  114. for( y = 0; y < height; y += size )
  115. {
  116. for( x = 0; x < width; x += size )
  117. {
  118. p = Particle3D( factory.createParticle() );
  119. p.position = new Vector3D( x + offset.x, y + offset.y, offset.z, 1 );
  120. b = new BitmapData( size, size, true, 0 );
  121. b.copyPixels( bitmapData, new Rectangle( x, y, size, size ), zero );
  122. m = new Bitmap( b );
  123. m.x = -halfSize;
  124. m.y = -halfSize;
  125. s = new Sprite();
  126. s.addChild( m );
  127. p.image = s;
  128. p.collisionRadius = halfSize;
  129. particles.push( p );
  130. }
  131. }
  132. }
  133. else
  134. {
  135. for( y = 0; y < height; ++y )
  136. {
  137. for( x = 0; x < width; ++x )
  138. {
  139. p = new Particle3D();
  140. p.position = new Vector3D( x + offset.x, y + offset.y, offset.z, 1 );
  141. b = new BitmapData( size, size, true, 0 );
  142. b.copyPixels( bitmapData, new Rectangle( x, y, size, size ), zero );
  143. m = new Bitmap( b );
  144. m.x = -halfSize;
  145. m.y = -halfSize;
  146. s = new Sprite();
  147. s.addChild( m );
  148. p.image = s;
  149. p.collisionRadius = halfSize;
  150. particles.push( p );
  151. }
  152. }
  153. }
  154. return particles;
  155. }
  156. }
  157. }