/src/de/nulldesign/nd2d/display/TextureRenderer.as

https://bitbucket.org/HopeSky/mars_nd2d · ActionScript · 94 lines · 45 code · 17 blank · 32 comment · 3 complexity · 92c69e3654e9bf52b44ce737aca80e89 MD5 · raw file

  1. /*
  2. * ND2D - A Flash Molehill GPU accelerated 2D engine
  3. *
  4. * Author: Lars Gerckens
  5. * Copyright (c) nulldesign 2011
  6. * Repository URL: http://github.com/nulldesign/nd2d
  7. * Getting started: https://github.com/nulldesign/nd2d/wiki
  8. *
  9. *
  10. * Licence Agreement
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. package de.nulldesign.nd2d.display {
  31. import flash.display3D.Context3D;
  32. import de.nulldesign.nd2d.materials.texture.Texture2D;
  33. import de.nulldesign.nd2d.utils.StatsObject;
  34. /**
  35. * Renders a Node2D to a texture every frame. Can be used to post process a whole scene for example.
  36. */
  37. public class TextureRenderer extends Node2D {
  38. protected var renderNode:Node2D;
  39. protected var texCamera:Camera2D = new Camera2D(1, 1);
  40. public var texture:Texture2D;
  41. private var cameraOffsetX:Number;
  42. private var cameraOffsetY:Number;
  43. public function TextureRenderer(renderNode:Node2D, texture:Texture2D, cameraOffsetX:Number = NaN, cameraOffsetY:Number = NaN) {
  44. this.texture = texture;
  45. this.renderNode = renderNode;
  46. _width = texture.bitmapWidth;
  47. _height = texture.bitmapHeight;
  48. this.cameraOffsetX = cameraOffsetX;
  49. this.cameraOffsetY = cameraOffsetY;
  50. texCamera.resizeCameraStage(width, height);
  51. }
  52. override public function handleDeviceLoss():void {
  53. super.handleDeviceLoss();
  54. texture.texture = null;
  55. }
  56. override internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean, statsObject:StatsObject):void {
  57. context.setRenderToTexture(texture.getTexture(context), false, 2, 0);
  58. context.clear(0.0, 0.0, 0.0, 0.0);
  59. if(!isNaN(cameraOffsetX) && !isNaN(cameraOffsetY)) {
  60. texCamera.x = cameraOffsetX;
  61. texCamera.y = cameraOffsetY;
  62. } else {
  63. texCamera.x = renderNode.x - (width >> 1);
  64. texCamera.y = renderNode.y - (height >> 1);
  65. }
  66. var visibleState:Boolean = renderNode.visible;
  67. renderNode.visible = true;
  68. renderNode.drawNode(context, texCamera, parentMatrixChanged, statsObject);
  69. renderNode.visible = visibleState;
  70. context.setRenderToBackBuffer();
  71. }
  72. override public function dispose():void
  73. {
  74. super.dispose();
  75. }
  76. }
  77. }