mask shader to common code

This commit is contained in:
2016-09-27 21:17:05 +07:00
parent cc966f6675
commit f15b386b34
4 changed files with 72 additions and 101 deletions

View File

@@ -5,40 +5,39 @@
// blending functions
//
inline fixed4 swf_darken(fixed4 a, fixed4 b) {
fixed4 r = min(a, b);
r.a = b.a;
inline fixed4 swf_darken(fixed4 ca, fixed4 cb) {
fixed4 r = min(ca, cb);
r.a = cb.a;
return r;
}
inline fixed4 swf_difference(fixed4 a, fixed4 b) {
fixed4 r = abs(a - b);
r.a = b.a;
inline fixed4 swf_difference(fixed4 ca, fixed4 cb) {
fixed4 r = abs(ca - cb);
r.a = cb.a;
return r;
}
inline fixed4 swf_invert(fixed4 a, fixed4 b) {
fixed4 r = 1 - a;
r.a = b.a;
inline fixed4 swf_invert(fixed4 ca, fixed4 cb) {
fixed4 r = 1.0 - ca;
r.a = cb.a;
return r;
}
inline fixed4 swf_overlay(fixed4 a, fixed4 b) {
fixed4 r = a > 0.5 ? 1.0 - 2.0 * (1.0 - a) * (1.0 - b) : 2.0 * a * b;
r.a = b.a;
inline fixed4 swf_overlay(fixed4 ca, fixed4 cb) {
fixed4 r = ca > 0.5 ? 1.0 - 2.0 * (1.0 - ca) * (1.0 - cb) : 2.0 * ca * cb;
r.a = cb.a;
return r;
}
inline fixed4 swf_hardlight(fixed4 a, fixed4 b) {
fixed4 r = b > 0.5 ? 1.0 - (1.0 - a) * (1.0 - 2.0 * (b - 0.5)) : a * (2.0 * b);
r.a = b.a;
inline fixed4 swf_hardlight(fixed4 ca, fixed4 cb) {
fixed4 r = cb > 0.5 ? 1.0 - (1.0 - ca) * (1.0 - 2.0 * (cb - 0.5)) : ca * (2.0 * cb);
r.a = cb.a;
return r;
}
inline fixed4 grab_blend(sampler2D grab_tex, float4 screenpos, fixed4 c) {
float2 grab_uv = screenpos.xy / screenpos.w;
grab_uv.x = (grab_uv.x + 1.0) * .5;
grab_uv.y = (grab_uv.y + 1.0) * .5;
grab_uv = (grab_uv + 1.0) * 0.5;
#if UNITY_UV_STARTS_AT_TOP
grab_uv.y = 1.0 - grab_uv.y;
#endif
@@ -68,6 +67,18 @@ struct swf_appdata_t {
float4 addcolor : TEXCOORD1;
};
struct swf_grab_appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 mulcolor : COLOR;
float4 addcolor : TEXCOORD1;
};
struct swf_mask_appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct swf_v2f_t {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
@@ -83,6 +94,11 @@ struct swf_grab_v2f_t {
float4 screenpos : TEXCOORD2;
};
struct swf_mask_v2f_t {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
//
// vert functions
//
@@ -96,7 +112,7 @@ inline swf_v2f_t swf_vert(swf_appdata_t IN) {
return OUT;
}
inline swf_grab_v2f_t swf_grab_vert(swf_appdata_t IN) {
inline swf_grab_v2f_t swf_grab_vert(swf_grab_appdata_t IN) {
swf_grab_v2f_t OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
@@ -106,6 +122,13 @@ inline swf_grab_v2f_t swf_grab_vert(swf_appdata_t IN) {
return OUT;
}
inline swf_mask_v2f_t swf_mask_vert(swf_mask_appdata_t IN) {
swf_mask_v2f_t OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
return OUT;
}
//
// frag functions
//
@@ -123,10 +146,18 @@ inline fixed4 swf_grab_frag(swf_grab_v2f_t IN) : SV_Target {
fixed4 c = tex2D(_MainTex, IN.uv);
if ( c.a > 0.01 ) {
c = c * IN.mulcolor + IN.addcolor;
c = grab_blend(_GrabTexture, IN.screenpos, c);
}
c = grab_blend(_GrabTexture, IN.screenpos, c);
c.rgb *= c.a;
return c;
}
inline fixed4 swf_mask_frag(swf_mask_v2f_t IN) : SV_Target {
fixed4 c = tex2D(_MainTex, IN.uv);
if ( c.a < 0.01 ) {
discard;
}
return c;
}
#endif // SWF_BASE_CG_INCLUDED

View File

@@ -12,10 +12,11 @@ Shader "FlashTools/SwfDecrMask" {
"CanUseSpriteAtlas" = "True"
}
ColorMask 0
Cull Off
Lighting Off
ZWrite Off
ColorMask 0
Blend One OneMinusSrcAlpha
Pass {
@@ -25,47 +26,15 @@ Shader "FlashTools/SwfDecrMask" {
Pass DecrSat
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_t IN) {
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
return OUT;
}
fixed4 _Tint;
sampler2D _MainTex;
sampler2D _AlphaTex;
float _AlphaSplitEnabled;
sampler2D _GrabTexture;
fixed4 SampleSpriteTexture(float2 uv) {
fixed4 color = tex2D(_MainTex, uv);
#if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
if (_AlphaSplitEnabled)
color.a = tex2D(_AlphaTex, uv).r;
#endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
return color;
}
#include "UnityCG.cginc"
#include "SwfBaseCG.cginc"
fixed4 frag(v2f IN) : SV_Target {
fixed4 c = SampleSpriteTexture(IN.uv);
if ( c.a < 0.01 ) {
discard;
}
return c;
}
#pragma vertex swf_mask_vert
#pragma fragment swf_mask_frag
ENDCG
}
}

View File

@@ -12,10 +12,11 @@ Shader "FlashTools/SwfIncrMask" {
"CanUseSpriteAtlas" = "True"
}
ColorMask 0
Cull Off
Lighting Off
ZWrite Off
ColorMask 0
Blend One OneMinusSrcAlpha
Pass {
@@ -25,47 +26,15 @@ Shader "FlashTools/SwfIncrMask" {
Pass IncrSat
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_t IN) {
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
return OUT;
}
fixed4 _Tint;
sampler2D _MainTex;
sampler2D _AlphaTex;
float _AlphaSplitEnabled;
sampler2D _GrabTexture;
fixed4 SampleSpriteTexture(float2 uv) {
fixed4 color = tex2D(_MainTex, uv);
#if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
if (_AlphaSplitEnabled)
color.a = tex2D(_AlphaTex, uv).r;
#endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
return color;
}
#include "UnityCG.cginc"
#include "SwfBaseCG.cginc"
fixed4 frag(v2f IN) : SV_Target {
fixed4 c = SampleSpriteTexture(IN.uv);
if ( c.a < 0.01 ) {
discard;
}
return c;
}
#pragma vertex swf_mask_vert
#pragma fragment swf_mask_frag
ENDCG
}
}

View File

@@ -1,10 +1,12 @@
<Properties StartupItem="Assembly-CSharp.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfPostprocessor.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Resources/Materials/SwfMaskedGrabShader.shader">
<Files>
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs" Line="380" Column="7" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfPostprocessor.cs" Line="138" Column="21" />
<File FileName="Assets/FlashTools/Scripts/SwfAsset.cs" Line="121" Column="3" />
<File FileName="Assets/FlashTools/Resources/Materials/SwfBaseCG.cginc" Line="132" Column="18" />
<File FileName="Assets/FlashTools/Resources/Materials/SwfSimpleGrabShader.shader" Line="45" Column="2" />
<File FileName="Assets/FlashTools/Resources/Materials/SwfMaskedGrabShader.shader" Line="5" Column="3" />
<File FileName="Assets/FlashTools/Resources/Materials/SwfSimpleShader.shader" Line="23" Column="3" NotebookId="1" />
<File FileName="Assets/FlashTools/Resources/Materials/SwfMaskedShader.shader" Line="36" Column="27" NotebookId="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>