/src/away3d/primitives/WireframeGrid.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 109 lines · 84 code · 15 blank · 10 comment · 6 complexity · 3cab9cfbe0bcb4140feaac91a14a298c MD5 · raw file

  1. package away3d.primitives
  2. {
  3. import away3d.entities.*;
  4. import flash.geom.Vector3D;
  5. /**
  6. * Class WireframeGrid generates a grid of lines on a given plane<code>WireframeGrid</code>
  7. * @param subDivision [optional] uint . Default is 10;
  8. * @param gridSize [optional] uint . Default is 100;
  9. * @param color [optional] uint . Default is 0xFFFFFF;
  10. * @param thickness [optional] Number . Default is 1;
  11. * @param plane [optional] String . Default is PLANE_XZ;
  12. * @param worldPlanes [optional] Boolean . Default is false.
  13. * If true, class displays the 3 world planes, at 0,0,0. with subDivision, thickness and gridSize. Overrides color and plane settings.
  14. */
  15. public class WireframeGrid extends SegmentSet
  16. {
  17. public static const PLANE_ZY:String = "zy";
  18. public static const PLANE_XY:String = "xy";
  19. public static const PLANE_XZ:String = "xz";
  20. public function WireframeGrid(subDivision:uint = 10, gridSize:uint = 100, thickness:Number = 1, color:uint = 0xFFFFFF, plane:String = "xz", worldPlanes:Boolean = false ) {
  21. super();
  22. if(subDivision == 0) subDivision = 1;
  23. if(thickness <= 0) thickness = 1;
  24. if(gridSize == 0) gridSize = 1;
  25. if(worldPlanes){
  26. build(subDivision, gridSize, 0x0000FF, thickness, PLANE_XY);
  27. build(subDivision, gridSize, 0xFF0000, thickness, PLANE_ZY);
  28. build(subDivision, gridSize, 0x00FF00, thickness, PLANE_XZ);
  29. } else{
  30. build(subDivision, gridSize, color, thickness, plane);
  31. }
  32. }
  33. private function build(subDivision:uint, gridSize:uint, color:uint, thickness:Number, plane:String):void
  34. {
  35. var bound:Number = gridSize *.5;
  36. var step:Number = gridSize/subDivision;
  37. var v0 : Vector3D = new Vector3D(0, 0, 0) ;
  38. var v1 : Vector3D = new Vector3D(0, 0, 0) ;
  39. var inc:Number = -bound;
  40. while(inc<=bound){
  41. switch(plane){
  42. case PLANE_ZY:
  43. v0.x = 0;
  44. v0.y = inc;
  45. v0.z = bound;
  46. v1.x = 0;
  47. v1.y = inc;
  48. v1.z = -bound;
  49. addSegment( new LineSegment(v0, v1, color, color, thickness));
  50. v0.z = inc;
  51. v0.x = 0;
  52. v0.y = bound;
  53. v1.x = 0;
  54. v1.y = -bound;
  55. v1.z = inc;
  56. addSegment(new LineSegment(v0, v1, color, color, thickness ));
  57. break;
  58. case PLANE_XY:
  59. v0.x = bound;
  60. v0.y = inc;
  61. v0.z = 0;
  62. v1.x = -bound;
  63. v1.y = inc;
  64. v1.z = 0;
  65. addSegment( new LineSegment(v0, v1, color, color, thickness));
  66. v0.x = inc;
  67. v0.y = bound;
  68. v0.z = 0;
  69. v1.x = inc;
  70. v1.y = -bound;
  71. v1.z = 0;
  72. addSegment(new LineSegment(v0, v1, color, color, thickness ));
  73. break;
  74. default:
  75. v0.x = bound;
  76. v0.y = 0;
  77. v0.z = inc;
  78. v1.x = -bound;
  79. v1.y = 0;
  80. v1.z = inc;
  81. addSegment( new LineSegment(v0, v1, color, color, thickness));
  82. v0.x = inc;
  83. v0.y = 0;
  84. v0.z = bound;
  85. v1.x = inc;
  86. v1.y = 0;
  87. v1.z = -bound;
  88. addSegment(new LineSegment(v0, v1, color, color, thickness ));
  89. }
  90. inc += step;
  91. }
  92. }
  93. }
  94. }