/src/away3d/tools/Grid.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 136 lines · 89 code · 20 blank · 27 comment · 10 complexity · 62f7ba47dd4595624e3ee540386afd97 MD5 · raw file

  1. package away3d.tools
  2. {
  3. import away3d.arcane;
  4. import away3d.containers.ObjectContainer3D;
  5. import away3d.core.base.Geometry;
  6. import away3d.core.base.SubGeometry;
  7. import away3d.entities.Mesh;
  8. use namespace arcane;
  9. /**
  10. * Class Grid snaps vertexes or meshes according to a given grid unit.<code>Grid</code>
  11. */
  12. public class Grid{
  13. private var _unit:Number;
  14. private var _objectSpace:Boolean;
  15. /**
  16. * Grid snaps vertexes according to a given grid unit
  17. * @param unit [optional] Number. The grid unit. Default is 1.
  18. * @param objectSpace [optional] Boolean. Apply only to vertexes in geometry objectspace when Object3D are considered. Default is false.
  19. */
  20. function Grid(unit:Number = 1, objectSpace:Boolean = false):void
  21. {
  22. _objectSpace = objectSpace;
  23. _unit = Math.abs(unit);
  24. }
  25. /**
  26. * Apply the grid code to a given object3D. If type ObjectContainer3D, all children Mesh vertices will be affected.
  27. * @param object3d Object3D. The Object3d to snap to grid.
  28. * @param dovert [optional]. If the vertices must be handled or not. When false only object position is snapped to grid. Default is false.
  29. */
  30. public function snapObject(object3d:ObjectContainer3D, dovert:Boolean = false):void
  31. {
  32. parse(object3d, dovert);
  33. }
  34. /**
  35. * Snaps to grid a given Vector.&lt;Number&gt; of vertices
  36. * @param vertices Vector.&lt;Number&gt;. The vertices vector
  37. */
  38. public function snapVertices(vertices:Vector.<Number>):Vector.<Number>
  39. {
  40. for (var i:uint = 0; i < vertices.length;++i)
  41. vertices[i] -= vertices[i]%_unit;
  42. return vertices;
  43. }
  44. /**
  45. * Apply the grid code to a single mesh
  46. * @param mesh Mesh. The mesh to snap to grid. Vertices are affected by default. Mesh position is snapped if grid.objectSpace is true;
  47. */
  48. public function snapMesh(mesh:Mesh):void
  49. {
  50. if(!_objectSpace){
  51. mesh.scenePosition.x -= mesh.scenePosition.x%_unit;
  52. mesh.scenePosition.y -= mesh.scenePosition.y%_unit;
  53. mesh.scenePosition.z -= mesh.scenePosition.z%_unit;
  54. }
  55. snap(mesh);
  56. }
  57. /**
  58. * Defines if the grid unit.
  59. */
  60. public function set unit(val:Number):void
  61. {
  62. _unit = Math.abs(val);
  63. _unit = (_unit ==0)? .001 : _unit;
  64. }
  65. public function get unit():Number
  66. {
  67. return _unit;
  68. }
  69. /**
  70. * Defines if the grid unit is applied in objectspace or worldspace. In worldspace, objects positions are affected.
  71. */
  72. public function set objectSpace(b:Boolean):void
  73. {
  74. _objectSpace = b;
  75. }
  76. public function get objectSpace():Boolean
  77. {
  78. return _objectSpace;
  79. }
  80. private function parse(object3d:ObjectContainer3D, dovert:Boolean = true):void
  81. {
  82. var child:ObjectContainer3D;
  83. if(!_objectSpace){
  84. object3d.scenePosition.x -= object3d.scenePosition.x%_unit;
  85. object3d.scenePosition.y -= object3d.scenePosition.y%_unit;
  86. object3d.scenePosition.z -= object3d.scenePosition.z%_unit;
  87. }
  88. if(object3d is Mesh && object3d.numChildren == 0 && dovert)
  89. snap(Mesh(object3d));
  90. for(var i:uint = 0;i<object3d.numChildren;++i){
  91. child = object3d.getChildAt(i);
  92. parse(child, dovert);
  93. }
  94. }
  95. private function snap(mesh:Mesh):void
  96. {
  97. var geometry:Geometry = mesh.geometry;
  98. var geometries:Vector.<SubGeometry> = geometry.subGeometries;
  99. var numSubGeoms:int = geometries.length;
  100. var vertices:Vector.<Number>;
  101. var j : uint;
  102. var i : uint;
  103. var vecLength : uint;
  104. var subGeom:SubGeometry;
  105. for (i = 0; i < numSubGeoms; ++i){
  106. subGeom = SubGeometry(geometries[i]);
  107. vertices = subGeom.vertexData;
  108. vecLength = vertices.length;
  109. for (j = 0; j < vecLength;++j){
  110. vertices[j] -= vertices[j]%_unit;
  111. }
  112. subGeom.updateVertexData(vertices);
  113. }
  114. }
  115. }
  116. }