Created
September 22, 2017 06:25
-
-
Save Beeblerox/e2f1511403abaab7cc2f6fa4200cab8e to your computer and use it in GitHub Desktop.
Usage example for render targets in flixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flash.Lib; | |
import flixel.FlxG; | |
import flixel.FlxSprite; | |
import flixel.FlxState; | |
import flixel.effects.FlxRenderTarget; | |
import flixel.system.FlxAssets.FlxShader; | |
import flixel.util.FlxColor; | |
/** | |
* ... | |
* @author Zaphod | |
*/ | |
class RenderTargetState extends FlxState | |
{ | |
var renderTexture:FlxRenderTarget; | |
override public function create():Void | |
{ | |
// just some test sprites | |
var s1:FlxSprite = new FlxSprite(); | |
var s2:FlxSprite = new FlxSprite(200, 0); | |
s1.alpha = 0.5; | |
s2.shader = new Invert2(); | |
// we need to add our sprites to state, so they will start rendering and updating. | |
// (TODO: maybe add them to render textures instead, so render textures will be flxgroups instead of flxsprite??? probably i will change this) | |
add(s1); | |
add(s2); | |
// create render target with the size of 256 by 512 pixels | |
renderTexture = new FlxRenderTarget(256, 512); | |
// you can set clear color for render target (if you want it to have some color background) | |
// but it's optional (default color is FlxColor.TRANSPARENT). | |
// And you can even not clear render texture before each rendering, by setting `renderTexture.clearBeforeRender = false;` | |
// or you can set some semitransparent clear color, so previous drawn states will be fading away. | |
renderTexture.clearColor = FlxColor.RED; | |
// specify camera which will be used for calculation of drawable sprites positions on this render texture. | |
// basically this property will override `cameras` property for all sprites added to this render target. | |
renderTexture.renderCamera = FlxG.camera; | |
add(renderTexture); | |
// set object's renderTarget, so it will be rendered ONLY on this texture and WON'T APPEAR on any other camera. | |
// if you want object to be rendered on some camera also, then you will have to override its `draw()` method. | |
// (Tell me if you want such functionality built in flixel core). | |
s1.renderTarget = renderTexture; | |
// because render target is a sprite, you can set its position, alpha, rotation, scale, etc... | |
// (try to play with it)... | |
renderTexture.y = 100; | |
// set render pass shader (it's optional, but what's the point of this then???). | |
// (it could be cheaper to draw all sprites on texture and then apply shader effect on them, | |
// than to render each of the sprites with shader). | |
renderTexture.shader = new Invert2(); | |
// you can render one render target to another | |
// 1. let's create another render target | |
var renderTexture2:FlxRenderTarget = new FlxRenderTarget(512, 256); | |
// 2. specify camera which will be used for calculation of drawable sprites positions on this render texture. | |
// (TODO: add ability to set renderCamera in constructor). | |
renderTexture2.renderCamera = FlxG.camera; | |
add(renderTexture2); | |
// 3. set this render texture as render target for our first render target. | |
// renderTexture.renderTarget = renderTexture2; // uncomment this line to see clipping effect. | |
} | |
override public function update(elapsed:Float):Void | |
{ | |
super.update(elapsed); | |
renderTexture.x = 0.5 * FlxG.width + Math.sin(Lib.getTimer() / 1000) * FlxG.width * 0.25; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment