/src/away3d/core/base/Geometry.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 171 lines · 102 code · 23 blank · 46 comment · 11 complexity · 94a0aaa748329281f101aa9c205dbf1c MD5 · raw file

  1. package away3d.core.base
  2. {
  3. import away3d.arcane;
  4. import away3d.events.GeometryEvent;
  5. import away3d.library.assets.AssetType;
  6. import away3d.library.assets.IAsset;
  7. import away3d.library.assets.NamedAssetBase;
  8. import flash.geom.Matrix3D;
  9. use namespace arcane;
  10. /**
  11. * Geometry is a collection of SubGeometries, each of which contain the actual geometrical data such as vertices,
  12. * normals, uvs, etc. It also contains a reference to an animation class, which defines how the geometry moves.
  13. * A Geometry object is assigned to a Mesh, a scene graph occurence of the geometry, which in turn assigns
  14. * the SubGeometries to its respective SubMesh objects.
  15. *
  16. *
  17. *
  18. * @see away3d.core.base.SubGeometry
  19. * @see away3d.scenegraph.Mesh
  20. */
  21. public class Geometry extends NamedAssetBase implements IAsset
  22. {
  23. private var _subGeometries:Vector.<ISubGeometry>;
  24. public function get assetType():String
  25. {
  26. return AssetType.GEOMETRY;
  27. }
  28. /**
  29. * A collection of SubGeometry objects, each of which contain geometrical data such as vertices, normals, etc.
  30. */
  31. public function get subGeometries():Vector.<ISubGeometry>
  32. {
  33. return _subGeometries;
  34. }
  35. /**
  36. * Creates a new Geometry object.
  37. */
  38. public function Geometry()
  39. {
  40. _subGeometries = new Vector.<ISubGeometry>();
  41. }
  42. public function applyTransformation(transform:Matrix3D):void
  43. {
  44. var len:uint = _subGeometries.length;
  45. for (var i:int = 0; i < len; ++i)
  46. _subGeometries[i].applyTransformation(transform);
  47. }
  48. /**
  49. * Adds a new SubGeometry object to the list.
  50. * @param subGeometry The SubGeometry object to be added.
  51. */
  52. public function addSubGeometry(subGeometry:ISubGeometry):void
  53. {
  54. _subGeometries.push(subGeometry);
  55. subGeometry.parentGeometry = this;
  56. if (hasEventListener(GeometryEvent.SUB_GEOMETRY_ADDED))
  57. dispatchEvent(new GeometryEvent(GeometryEvent.SUB_GEOMETRY_ADDED, subGeometry));
  58. invalidateBounds(subGeometry);
  59. }
  60. /**
  61. * Removes a new SubGeometry object from the list.
  62. * @param subGeometry The SubGeometry object to be removed.
  63. */
  64. public function removeSubGeometry(subGeometry:ISubGeometry):void
  65. {
  66. _subGeometries.splice(_subGeometries.indexOf(subGeometry), 1);
  67. subGeometry.parentGeometry = null;
  68. if (hasEventListener(GeometryEvent.SUB_GEOMETRY_REMOVED))
  69. dispatchEvent(new GeometryEvent(GeometryEvent.SUB_GEOMETRY_REMOVED, subGeometry));
  70. invalidateBounds(subGeometry);
  71. }
  72. /**
  73. * Clones the geometry.
  74. * @return An exact duplicate of the current Geometry object.
  75. */
  76. public function clone():Geometry
  77. {
  78. var clone:Geometry = new Geometry();
  79. var len:uint = _subGeometries.length;
  80. for (var i:int = 0; i < len; ++i)
  81. clone.addSubGeometry(_subGeometries[i].clone());
  82. return clone;
  83. }
  84. /**
  85. * Scales the geometry.
  86. * @param scale The amount by which to scale.
  87. */
  88. public function scale(scale:Number):void
  89. {
  90. var numSubGeoms:uint = _subGeometries.length;
  91. for (var i:uint = 0; i < numSubGeoms; ++i)
  92. _subGeometries[i].scale(scale);
  93. }
  94. /**
  95. * Clears all resources used by the Geometry object, including SubGeometries.
  96. */
  97. public function dispose():void
  98. {
  99. var numSubGeoms:uint = _subGeometries.length;
  100. for (var i:uint = 0; i < numSubGeoms; ++i) {
  101. var subGeom:ISubGeometry = _subGeometries[0];
  102. removeSubGeometry(subGeom);
  103. subGeom.dispose();
  104. }
  105. }
  106. /**
  107. * Scales the uv coordinates (tiling)
  108. * @param scaleU The amount by which to scale on the u axis. Default is 1;
  109. * @param scaleV The amount by which to scale on the v axis. Default is 1;
  110. */
  111. public function scaleUV(scaleU:Number = 1, scaleV:Number = 1):void
  112. {
  113. var numSubGeoms:uint = _subGeometries.length;
  114. for (var i:uint = 0; i < numSubGeoms; ++i)
  115. _subGeometries[i].scaleUV(scaleU, scaleV);
  116. }
  117. /**
  118. * Updates the SubGeometries so all vertex data is represented in different buffers.
  119. * Use this for compatibility with Pixel Bender and PBPickingCollider
  120. */
  121. public function convertToSeparateBuffers():void
  122. {
  123. var subGeom:ISubGeometry;
  124. var numSubGeoms:int = _subGeometries.length;
  125. var _removableCompactSubGeometries:Vector.<CompactSubGeometry> = new Vector.<CompactSubGeometry>();
  126. for (var i:int = 0; i < numSubGeoms; ++i) {
  127. subGeom = _subGeometries[i];
  128. if (subGeom is SubGeometry)
  129. continue;
  130. _removableCompactSubGeometries.push(subGeom);
  131. addSubGeometry(subGeom.cloneWithSeperateBuffers());
  132. }
  133. for each (var s:CompactSubGeometry in _removableCompactSubGeometries) {
  134. removeSubGeometry(s);
  135. s.dispose();
  136. }
  137. }
  138. arcane function validate():void
  139. {
  140. // To be overridden when necessary
  141. }
  142. arcane function invalidateBounds(subGeom:ISubGeometry):void
  143. {
  144. if (hasEventListener(GeometryEvent.BOUNDS_INVALID))
  145. dispatchEvent(new GeometryEvent(GeometryEvent.BOUNDS_INVALID, subGeom));
  146. }
  147. }
  148. }