Created
October 5, 2017 19:24
-
-
Save Beeblerox/baf43b773679ef273f865602bd5441d1 to your computer and use it in GitHub Desktop.
Drawing sprite on render texture and on camera in the same frame
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.FlxStrip; | |
import flixel.effects.FlxRenderTarget; | |
import flixel.graphics.FlxMaterial; | |
import flixel.graphics.shaders.quads.FlxTexturedShader; | |
import flixel.group.FlxGroup; | |
import flixel.system.FlxAssets.FlxShader; | |
import flixel.ui.FlxBar; | |
import flixel.ui.FlxButton; | |
import flixel.util.FlxColor; | |
import openfl.Vector; | |
/** | |
* ... | |
* @author Zaphod | |
*/ | |
class RenderTargetState extends FlxState | |
{ | |
var rt1:FlxRenderTarget; | |
var s1:SwitchMaterialSprite; | |
var first:flixel.graphics.FlxMaterial; | |
override public function create():Void | |
{ | |
s1 = new SwitchMaterialSprite(null, new InvertShader()); // first shader is the default one | |
rt1 = new FlxRenderTarget(128, 128); | |
rt1.group.active = false; // we don't want s1 sprite to update twice per frame, so we're making rt1's group inactive | |
rt1.x = 100; | |
// as you can see from the following code: | |
// 1. our sprite will be drawn on render texture first (with the first shader - default shader) | |
// 2. then our sprite will be drawn on camera with the second shader (invert shader) | |
add(rt1); | |
rt1.add(s1); | |
add(s1); | |
} | |
} | |
class SwitchMaterialSprite extends FlxSprite | |
{ | |
var first:FlxMaterial; | |
var second:FlxMaterial; | |
public function new(firstShader:FlxShader, secondShader:FlxShader) | |
{ | |
super(); | |
first = material; | |
first.shader = firstShader; | |
second = new FlxMaterial(); | |
second.shader = secondShader; | |
} | |
public function switchMaterial():Void | |
{ | |
material = (material == first) ? second : first; | |
} | |
override public function update(elapsed:Float):Void | |
{ | |
material = first; | |
super.update(elapsed); | |
} | |
override public function draw():Void | |
{ | |
switchMaterial(); // switch material before rendering on actual cameras | |
super.draw(); | |
} | |
} | |
class InvertShader extends FlxTexturedShader | |
{ | |
public function new() | |
{ | |
var fragment:String = " | |
varying vec2 vTexCoord; | |
varying vec4 vColor; | |
varying vec4 vColorOffset; | |
uniform sampler2D uImage0; | |
void main(void) | |
{ | |
vec4 color = texture2D(uImage0, vTexCoord); | |
gl_FragColor = vec4((1.0 - color.r) * color.a, (1.0 - color.g) * color.a, (1.0 - color.b) * color.a, color.a); | |
}"; | |
super(null, fragment); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment