/src/away3d/lights/shadowmaps/ShadowMapperBase.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 143 lines · 113 code · 21 blank · 9 comment · 12 complexity · 1ab77f6805c23e5ae1265cff2a29b672 MD5 · raw file

  1. package away3d.lights.shadowmaps
  2. {
  3. import away3d.arcane;
  4. import away3d.cameras.Camera3D;
  5. import away3d.containers.Scene3D;
  6. import away3d.core.managers.Stage3DProxy;
  7. import away3d.core.render.DepthRenderer;
  8. import away3d.core.traverse.EntityCollector;
  9. import away3d.core.traverse.ShadowCasterCollector;
  10. import away3d.errors.AbstractMethodError;
  11. import away3d.lights.LightBase;
  12. import away3d.textures.RenderTexture;
  13. import away3d.textures.TextureProxyBase;
  14. import flash.display3D.textures.TextureBase;
  15. use namespace arcane;
  16. public class ShadowMapperBase
  17. {
  18. protected var _casterCollector:ShadowCasterCollector;
  19. private var _depthMap:TextureProxyBase;
  20. protected var _depthMapSize:uint = 2048;
  21. protected var _light:LightBase;
  22. private var _explicitDepthMap:Boolean;
  23. private var _autoUpdateShadows:Boolean = true;
  24. arcane var _shadowsInvalid:Boolean;
  25. public function ShadowMapperBase()
  26. {
  27. _casterCollector = createCasterCollector();
  28. }
  29. protected function createCasterCollector():ShadowCasterCollector
  30. {
  31. return new ShadowCasterCollector();
  32. }
  33. public function get autoUpdateShadows():Boolean
  34. {
  35. return _autoUpdateShadows;
  36. }
  37. public function set autoUpdateShadows(value:Boolean):void
  38. {
  39. _autoUpdateShadows = value;
  40. }
  41. public function updateShadows():void
  42. {
  43. _shadowsInvalid = true;
  44. }
  45. /**
  46. * This is used by renderers that can support depth maps to be shared across instances
  47. * @param depthMap
  48. */
  49. arcane function setDepthMap(depthMap:TextureProxyBase):void
  50. {
  51. if (_depthMap == depthMap)
  52. return;
  53. if (_depthMap && !_explicitDepthMap)
  54. _depthMap.dispose();
  55. _depthMap = depthMap;
  56. if (_depthMap) {
  57. _explicitDepthMap = true;
  58. _depthMapSize = _depthMap.width;
  59. } else
  60. _explicitDepthMap = false;
  61. }
  62. public function get light():LightBase
  63. {
  64. return _light;
  65. }
  66. public function set light(value:LightBase):void
  67. {
  68. _light = value;
  69. }
  70. public function get depthMap():TextureProxyBase
  71. {
  72. return _depthMap ||= createDepthTexture();
  73. }
  74. public function get depthMapSize():uint
  75. {
  76. return _depthMapSize;
  77. }
  78. public function set depthMapSize(value:uint):void
  79. {
  80. if (value == _depthMapSize)
  81. return;
  82. _depthMapSize = value;
  83. if (_explicitDepthMap)
  84. throw Error("Cannot set depth map size for the current renderer.");
  85. else if (_depthMap) {
  86. _depthMap.dispose();
  87. _depthMap = null;
  88. }
  89. }
  90. public function dispose():void
  91. {
  92. _casterCollector = null;
  93. if (_depthMap && !_explicitDepthMap)
  94. _depthMap.dispose();
  95. _depthMap = null;
  96. }
  97. protected function createDepthTexture():TextureProxyBase
  98. {
  99. return new RenderTexture(_depthMapSize, _depthMapSize);
  100. }
  101. /**
  102. * Renders the depth map for this light.
  103. * @param entityCollector The EntityCollector that contains the original scene data.
  104. * @param renderer The DepthRenderer to render the depth map.
  105. */
  106. arcane function renderDepthMap(stage3DProxy:Stage3DProxy, entityCollector:EntityCollector, renderer:DepthRenderer):void
  107. {
  108. _shadowsInvalid = false;
  109. updateDepthProjection(entityCollector.camera);
  110. _depthMap ||= createDepthTexture();
  111. drawDepthMap(_depthMap.getTextureForStage3D(stage3DProxy), entityCollector.scene, renderer);
  112. }
  113. protected function updateDepthProjection(viewCamera:Camera3D):void
  114. {
  115. throw new AbstractMethodError();
  116. }
  117. protected function drawDepthMap(target:TextureBase, scene:Scene3D, renderer:DepthRenderer):void
  118. {
  119. throw new AbstractMethodError();
  120. }
  121. }
  122. }