/src/de/nulldesign/nd2d/display/TextureRenderer.as
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
31package de.nulldesign.nd2d.display {
32
33 import flash.display3D.Context3D;
34
35 import de.nulldesign.nd2d.materials.texture.Texture2D;
36 import de.nulldesign.nd2d.utils.StatsObject;
37
38 /**
39 * Renders a Node2D to a texture every frame. Can be used to post process a whole scene for example.
40 */
41 public class TextureRenderer extends Node2D {
42
43 protected var renderNode:Node2D;
44 protected var texCamera:Camera2D = new Camera2D(1, 1);
45
46 public var texture:Texture2D;
47
48 private var cameraOffsetX:Number;
49 private var cameraOffsetY:Number;
50
51 public function TextureRenderer(renderNode:Node2D, texture:Texture2D, cameraOffsetX:Number = NaN, cameraOffsetY:Number = NaN) {
52
53 this.texture = texture;
54 this.renderNode = renderNode;
55 _width = texture.bitmapWidth;
56 _height = texture.bitmapHeight;
57 this.cameraOffsetX = cameraOffsetX;
58 this.cameraOffsetY = cameraOffsetY;
59
60 texCamera.resizeCameraStage(width, height);
61 }
62
63 override public function handleDeviceLoss():void {
64 super.handleDeviceLoss();
65 texture.texture = null;
66 }
67
68 override internal function drawNode(context:Context3D, camera:Camera2D, parentMatrixChanged:Boolean, statsObject:StatsObject):void {
69
70 context.setRenderToTexture(texture.getTexture(context), false, 2, 0);
71 context.clear(0.0, 0.0, 0.0, 0.0);
72
73 if(!isNaN(cameraOffsetX) && !isNaN(cameraOffsetY)) {
74 texCamera.x = cameraOffsetX;
75 texCamera.y = cameraOffsetY;
76 } else {
77 texCamera.x = renderNode.x - (width >> 1);
78 texCamera.y = renderNode.y - (height >> 1);
79 }
80
81 var visibleState:Boolean = renderNode.visible;
82 renderNode.visible = true;
83 renderNode.drawNode(context, texCamera, parentMatrixChanged, statsObject);
84 renderNode.visible = visibleState;
85
86 context.setRenderToBackBuffer();
87 }
88
89 override public function dispose():void
90 {
91 super.dispose();
92 }
93 }
94}