forked from Leather128/LeatherEngine
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc71c4c
commit cba1057
Showing
5 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package shaders; | ||
|
||
import flixel.system.FlxAssets.FlxShader; | ||
|
||
/** | ||
* Cool Shader by ShadowMario that changes RGB based on HSV. | ||
*/ | ||
class ColorSwapHSV { | ||
public var shader(default, null):ColorSwapHSVShader = new ColorSwapHSVShader(); | ||
public var hue(default, set):Float = 0; | ||
public var saturation(default, set):Float = 0; | ||
public var brightness(default, set):Float = 0; | ||
|
||
private function set_hue(value:Float) { | ||
hue = value; | ||
shader.uTime.value[0] = hue; | ||
return hue; | ||
} | ||
|
||
private function set_saturation(value:Float) { | ||
saturation = value; | ||
shader.uTime.value[1] = saturation; | ||
return saturation; | ||
} | ||
|
||
private function set_brightness(value:Float) { | ||
brightness = value; | ||
shader.uTime.value[2] = brightness; | ||
return brightness; | ||
} | ||
|
||
public function new() | ||
{ | ||
shader.uTime.value = [0, 0, 0]; | ||
shader.awesomeOutline.value = [false]; | ||
} | ||
} | ||
|
||
class ColorSwapHSVShader extends FlxShader { | ||
@:glFragmentSource(' | ||
varying float openfl_Alphav; | ||
varying vec4 openfl_ColorMultiplierv; | ||
varying vec4 openfl_ColorOffsetv; | ||
varying vec2 openfl_TextureCoordv; | ||
|
||
uniform bool openfl_HasColorTransform; | ||
uniform vec2 openfl_TextureSize; | ||
uniform sampler2D bitmap; | ||
|
||
uniform bool hasTransform; | ||
uniform bool hasColorTransform; | ||
|
||
vec4 flixel_texture2D(sampler2D bitmap, vec2 coord) | ||
{ | ||
vec4 color = texture2D(bitmap, coord); | ||
if (!hasTransform) | ||
{ | ||
return color; | ||
} | ||
|
||
if (color.a == 0.0) | ||
{ | ||
return vec4(0.0, 0.0, 0.0, 0.0); | ||
} | ||
|
||
if (!hasColorTransform) | ||
{ | ||
return color * openfl_Alphav; | ||
} | ||
|
||
color = vec4(color.rgb / color.a, color.a); | ||
|
||
mat4 colorMultiplier = mat4(0); | ||
colorMultiplier[0][0] = openfl_ColorMultiplierv.x; | ||
colorMultiplier[1][1] = openfl_ColorMultiplierv.y; | ||
colorMultiplier[2][2] = openfl_ColorMultiplierv.z; | ||
colorMultiplier[3][3] = openfl_ColorMultiplierv.w; | ||
|
||
color = clamp(openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0); | ||
|
||
if (color.a > 0.0) | ||
{ | ||
return vec4(color.rgb * color.a * openfl_Alphav, color.a * openfl_Alphav); | ||
} | ||
return vec4(0.0, 0.0, 0.0, 0.0); | ||
} | ||
|
||
uniform vec3 uTime; | ||
uniform bool awesomeOutline; | ||
|
||
const float offset = 1.0 / 128.0; | ||
vec3 normalizeColor(vec3 color) | ||
{ | ||
return vec3( | ||
color[0] / 255.0, | ||
color[1] / 255.0, | ||
color[2] / 255.0 | ||
); | ||
} | ||
|
||
vec3 rgb2hsv(vec3 c) | ||
{ | ||
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); | ||
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); | ||
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); | ||
|
||
float d = q.x - min(q.w, q.y); | ||
float e = 1.0e-10; | ||
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); | ||
} | ||
|
||
vec3 hsv2rgb(vec3 c) | ||
{ | ||
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | ||
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); | ||
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); | ||
} | ||
|
||
void main() | ||
{ | ||
vec4 color = flixel_texture2D(bitmap, openfl_TextureCoordv); | ||
|
||
vec4 swagColor = vec4(rgb2hsv(vec3(color[0], color[1], color[2])), color[3]); | ||
|
||
// [0] is the hue??? | ||
swagColor[0] = swagColor[0] + uTime[0]; | ||
swagColor[1] = swagColor[1] + uTime[1]; | ||
swagColor[2] = swagColor[2] * (1.0 + uTime[2]); | ||
if(swagColor[1] < 0.0) | ||
{ | ||
swagColor[1] = 0.0; | ||
} | ||
else if(swagColor[1] > 1.0) | ||
{ | ||
swagColor[1] = 1.0; | ||
} | ||
|
||
color = vec4(hsv2rgb(vec3(swagColor[0], swagColor[1], swagColor[2])), swagColor[3]); | ||
|
||
if (awesomeOutline) | ||
{ | ||
// Outline bullshit? | ||
vec2 size = vec2(3, 3); | ||
|
||
if (color.a <= 0.5) { | ||
float w = size.x / openfl_TextureSize.x; | ||
float h = size.y / openfl_TextureSize.y; | ||
if (flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x + w, openfl_TextureCoordv.y)).a != 0. | ||
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x - w, openfl_TextureCoordv.y)).a != 0. | ||
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x, openfl_TextureCoordv.y + h)).a != 0. | ||
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x, openfl_TextureCoordv.y - h)).a != 0.) | ||
color = vec4(1.0, 1.0, 1.0, 1.0); | ||
} | ||
} | ||
gl_FragColor = color; | ||
|
||
/* | ||
if (color.a > 0.5) | ||
gl_FragColor = color; | ||
else | ||
{ | ||
float a = flixel_texture2D(bitmap, vec2(openfl_TextureCoordv + offset, openfl_TextureCoordv.y)).a + | ||
flixel_texture2D(bitmap, vec2(openfl_TextureCoordv, openfl_TextureCoordv.y - offset)).a + | ||
flixel_texture2D(bitmap, vec2(openfl_TextureCoordv - offset, openfl_TextureCoordv.y)).a + | ||
flixel_texture2D(bitmap, vec2(openfl_TextureCoordv, openfl_TextureCoordv.y + offset)).a; | ||
if (color.a < 1.0 && a > 0.0) | ||
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.8); | ||
else | ||
gl_FragColor = color; | ||
} */ | ||
}') | ||
@:glVertexSource(' | ||
attribute float openfl_Alpha; | ||
attribute vec4 openfl_ColorMultiplier; | ||
attribute vec4 openfl_ColorOffset; | ||
attribute vec4 openfl_Position; | ||
attribute vec2 openfl_TextureCoord; | ||
|
||
varying float openfl_Alphav; | ||
varying vec4 openfl_ColorMultiplierv; | ||
varying vec4 openfl_ColorOffsetv; | ||
varying vec2 openfl_TextureCoordv; | ||
|
||
uniform mat4 openfl_Matrix; | ||
uniform bool openfl_HasColorTransform; | ||
uniform vec2 openfl_TextureSize; | ||
|
||
attribute float alpha; | ||
attribute vec4 colorMultiplier; | ||
attribute vec4 colorOffset; | ||
uniform bool hasColorTransform; | ||
void main(void) | ||
{ | ||
openfl_Alphav = openfl_Alpha; | ||
openfl_TextureCoordv = openfl_TextureCoord; | ||
|
||
if (openfl_HasColorTransform) { | ||
openfl_ColorMultiplierv = openfl_ColorMultiplier; | ||
openfl_ColorOffsetv = openfl_ColorOffset / 255.0; | ||
} | ||
|
||
gl_Position = openfl_Matrix * openfl_Position; | ||
|
||
openfl_Alphav = openfl_Alpha * alpha; | ||
if (hasColorTransform) | ||
{ | ||
openfl_ColorOffsetv = colorOffset / 255.0; | ||
openfl_ColorMultiplierv = colorMultiplier; | ||
} | ||
}') | ||
|
||
public function new() | ||
{ | ||
super(); | ||
} | ||
} |
This file contains 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
This file contains 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