Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Shaders sample #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added features/display/Shaders/Assets/diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added features/display/Shaders/Assets/normals.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added features/display/Shaders/Assets/openfl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions features/display/Shaders/Assets/openfl.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added features/display/Shaders/Assets/openfl_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions features/display/Shaders/Source/BitmapExample.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import openfl.events.MouseEvent;
import openfl.geom.Point;
import openfl.Lib;
import openfl.ui.Keyboard;
import shaders.BackgroundShader;

class BitmapExample extends Sprite
{

public function new()
{
super();
addEventListener(Event.ADDED_TO_STAGE, init);
}

function init(_) {

// we create a transparent bitmap
var bd = new BitmapData(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bd);
addChild(bmp);

// and initialize our shader
var bgShader = new BackgroundShader();
bgShader.uZoom = 800 * bmp.scaleX;
// As an example, we add a bitmap to show how we can use BitmapDatas in our shader
bgShader.uSampler1 = Assets.getBitmapData("assets/openfl.png");

bmp.shader = bgShader;

var movement = new Point();

var t = Lib.getTimer() / 1000;
// we update its parameters each frame
addEventListener(Event.ENTER_FRAME, function(_) {
bgShader.uGlobalTime = (Lib.getTimer() / 1000) - t;
bgShader.uMovement[0] += movement.x;
bgShader.uMovement[1] += movement.y;
});

// keep the background always there when resizing the stage
stage.addEventListener(Event.RESIZE, function(_) {
bmp.scaleX = stage.stageWidth / bmp.bitmapData.width;
bmp.scaleY = stage.stageHeight / bmp.bitmapData.height;
bgShader.uZoom = 800 * bmp.scaleX;
});

// some movement logic
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent) {
switch(e.keyCode) {
case Keyboard.RIGHT:
movement.x = -1;
case Keyboard.LEFT:
movement.x = 1;
case Keyboard.UP:
movement.y = -1;
case Keyboard.DOWN:
movement.y = 1;
}
});

stage.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent) {
switch(e.keyCode) {
case Keyboard.RIGHT:
movement.x = 0;
case Keyboard.LEFT:
movement.x = 0;
case Keyboard.UP:
movement.y = 0;
case Keyboard.DOWN:
movement.y = 0;
}
});
}

}
142 changes: 142 additions & 0 deletions features/display/Shaders/Source/FilterExample.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package;
import openfl._legacy.display.BitmapData;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.display.Shader;
import openfl.events.KeyboardEvent;
import openfl.filters.BitmapFilter;
import openfl.filters.BlurFilter;
import openfl.filters.ColorMatrixFilter;
import openfl.filters.DropShadowFilter;
import openfl.filters.GlowFilter;
import openfl.filters.ShaderFilter;
import openfl.geom.ColorTransform;
#if !flash
import shaders.DotShader;
import shaders.RGBShiftShader;
#end
import openfl.geom.Rectangle;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.ui.Keyboard;

class FilterExample extends Sprite {

public function new() {
super();
addEventListener(Event.ADDED_TO_STAGE, init);
}

function init(_) {

var bitmapData = Assets.getBitmapData("assets/openfl.png");
var left = new Sprite();
var right = new Sprite();
addChild(left);
addChild(right);
left.addChild(new Bitmap(bitmapData));
right.addChild(new Bitmap(bitmapData));
left.y = right.y = stage.stageHeight / 2 - bitmapData.height / 2;
right.x = left.x + left.width;

var text = new TextField();
text.defaultTextFormat = new TextFormat('_sans', 32, 0xFFFFFF);
text.text = "OpenFL with filters!";
text.x = 100;
text.y = 100;
text.width = 600;
text.filters = [new DropShadowFilter(), new GlowFilter(),];
addChild(text);

var shape = new Shape();
shape.graphics.beginFill(0xEE00EE);
shape.graphics.drawCircle(100, 100, 100);
shape.x = 200;
shape.y = 200;
shape.filters = [new DropShadowFilter()];
addChild(shape);

var normal:Array<Float> = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
var sepia:Array<Float> = [
0.393, 0.7689999, 0.18899999, 0, 0,
0.349, 0.6859999, 0.16799999, 0, 0,
0.272, 0.5339999, 0.13099999, 0, 0,
0,0,0,1,0
];
var invert:Array<Float> = [
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
];
var grayscale:Array<Float> = [
0.5, 0.5, 0.5, 0, 0,
0.5, 0.5, 0.5, 0, 0,
0.5, 0.5, 0.5, 0, 0,
0, 0, 0, 1, 0
];

var colors = [normal, sepia, invert, grayscale];
var randColors = function() {
return colors[Std.int(colors.length * Math.random())];
};

var blurFilter = new BlurFilter();
var colorMatrixFilter = new ColorMatrixFilter(randColors());

#if flash
var filterArray:Array<BitmapFilter> = [blurFilter, colorMatrixFilter];
#else

var rgbShader = new RGBShiftShader();
var rgbFilter = new ShaderFilter(rgbShader);
// this filter needs to extend the cached bitmap
rgbFilter.topExtension = rgbFilter.bottomExtension = 4;
rgbFilter.leftExtension = rgbFilter.rightExtension = 4;

var filterArray:Array<BitmapFilter> = [rgbFilter, blurFilter, colorMatrixFilter];
#end

this.filters = filterArray;

var removeFilters = false;
var added = true;
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent) {
switch(e.keyCode) {
case Keyboard.C:
colorMatrixFilter.matrix = randColors();
case Keyboard.S:
if(added)
removeChild(shape);
else
addChild(shape);
added = !added;
case _:
removeFilters = !removeFilters;
}

this.filters = removeFilters ? null : filterArray;
});


addEventListener(Event.ENTER_FRAME, function(_) {
#if !flash
rgbShader.angle += 0.05;

// We need to re-apply the filters to signal the cached bitmap to update.
this.filters = removeFilters ? null : filterArray;
#end
});
}

}


Loading