/src/away3d/primitives/PrimitiveBase.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 152 lines · 89 code · 21 blank · 42 comment · 9 complexity · d5897dcbd0d400dc0a398ee81cf94bf1 MD5 · raw file

  1. package away3d.primitives
  2. {
  3. import away3d.arcane;
  4. import away3d.core.base.CompactSubGeometry;
  5. import away3d.core.base.Geometry;
  6. import away3d.core.base.ISubGeometry;
  7. import away3d.errors.AbstractMethodError;
  8. import flash.geom.Matrix3D;
  9. use namespace arcane;
  10. /**
  11. * PrimitiveBase is an abstract base class for mesh primitives, which are prebuilt simple meshes.
  12. */
  13. public class PrimitiveBase extends Geometry
  14. {
  15. protected var _geomDirty:Boolean = true;
  16. protected var _uvDirty:Boolean = true;
  17. private var _subGeometry:CompactSubGeometry;
  18. /**
  19. * Creates a new PrimitiveBase object.
  20. * @param material The material with which to render the object
  21. */
  22. public function PrimitiveBase()
  23. {
  24. _subGeometry = new CompactSubGeometry();
  25. _subGeometry.autoGenerateDummyUVs = false;
  26. addSubGeometry(_subGeometry);
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. override public function get subGeometries():Vector.<ISubGeometry>
  32. {
  33. if (_geomDirty)
  34. updateGeometry();
  35. if (_uvDirty)
  36. updateUVs();
  37. return super.subGeometries;
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. override public function clone():Geometry
  43. {
  44. if (_geomDirty)
  45. updateGeometry();
  46. if (_uvDirty)
  47. updateUVs();
  48. return super.clone();
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. override public function scale(scale:Number):void
  54. {
  55. if (_geomDirty)
  56. updateGeometry();
  57. super.scale(scale);
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. override public function scaleUV(scaleU:Number = 1, scaleV:Number = 1):void
  63. {
  64. if (_uvDirty)
  65. updateUVs();
  66. super.scaleUV(scaleU, scaleV);
  67. }
  68. /**
  69. * @inheritDoc
  70. */
  71. override public function applyTransformation(transform:Matrix3D):void
  72. {
  73. if (_geomDirty)
  74. updateGeometry();
  75. super.applyTransformation(transform);
  76. }
  77. /**
  78. * Builds the primitive's geometry when invalid. This method should not be called directly. The calling should
  79. * be triggered by the invalidateGeometry method (and in turn by updateGeometry).
  80. */
  81. protected function buildGeometry(target:CompactSubGeometry):void
  82. {
  83. throw new AbstractMethodError();
  84. }
  85. /**
  86. * Builds the primitive's uv coordinates when invalid. This method should not be called directly. The calling
  87. * should be triggered by the invalidateUVs method (and in turn by updateUVs).
  88. */
  89. protected function buildUVs(target:CompactSubGeometry):void
  90. {
  91. throw new AbstractMethodError();
  92. }
  93. /**
  94. * Invalidates the primitive's geometry, causing it to be updated when requested.
  95. */
  96. protected function invalidateGeometry():void
  97. {
  98. _geomDirty = true;
  99. }
  100. /**
  101. * Invalidates the primitive's uv coordinates, causing them to be updated when requested.
  102. */
  103. protected function invalidateUVs():void
  104. {
  105. _uvDirty = true;
  106. }
  107. /**
  108. * Updates the geometry when invalid.
  109. */
  110. private function updateGeometry():void
  111. {
  112. buildGeometry(_subGeometry);
  113. _geomDirty = false;
  114. }
  115. /**
  116. * Updates the uv coordinates when invalid.
  117. */
  118. private function updateUVs():void
  119. {
  120. buildUVs(_subGeometry);
  121. _uvDirty = false;
  122. }
  123. override arcane function validate():void
  124. {
  125. if (_geomDirty)
  126. updateGeometry();
  127. if (_uvDirty)
  128. updateUVs();
  129. }
  130. }
  131. }