custom color mask for shaders

This commit is contained in:
2016-09-28 02:50:14 +07:00
parent f15b386b34
commit 2d2cbdf525
10 changed files with 112 additions and 70 deletions

View File

@@ -145,7 +145,7 @@ Camera:
far clip plane: 1000
field of view: 60
orthographic: 1
orthographic size: 1.03
orthographic size: 1.3
m_Depth: 0
m_CullingMask:
serializedVersion: 2

View File

@@ -4,9 +4,10 @@ Shader "FlashTools/SwfGrabMasked" {
[PerRendererData] _Tint ("Tint" , Color) = (1,1,1,1)
_StencilID ("Stencil ID", Int) = 0
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("SrcBlend", Int) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("DstBlend", Int) = 10
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode )] _SrcBlend ("SrcBlend" , Int) = 1
[Enum(UnityEngine.Rendering.BlendMode )] _DstBlend ("DstBlend" , Int) = 10
[Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorMask ("ColorMask", Int) = 15
}
SubShader {
@@ -18,9 +19,10 @@ Shader "FlashTools/SwfGrabMasked" {
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Cull Off
Lighting Off
ZWrite Off
ColorMask [_ColorMask]
BlendOp [_BlendOp]
Blend [_SrcBlend] [_DstBlend]

View File

@@ -4,9 +4,10 @@ Shader "FlashTools/SwfMasked" {
[PerRendererData] _Tint ("Tint" , Color) = (1,1,1,1)
_StencilID ("Stencil ID", Int) = 0
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("SrcBlend", Int) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("DstBlend", Int) = 10
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode )] _SrcBlend ("SrcBlend" , Int) = 1
[Enum(UnityEngine.Rendering.BlendMode )] _DstBlend ("DstBlend" , Int) = 10
[Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorMask ("ColorMask", Int) = 15
}
SubShader {
@@ -18,9 +19,10 @@ Shader "FlashTools/SwfMasked" {
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Cull Off
Lighting Off
ZWrite Off
ColorMask [_ColorMask]
BlendOp [_BlendOp]
Blend [_SrcBlend] [_DstBlend]

View File

@@ -3,9 +3,10 @@ Shader "FlashTools/SwfGrabSimple" {
[PerRendererData] _MainTex ("Main Texture", 2D ) = "white" {}
[PerRendererData] _Tint ("Tint" , Color) = (1,1,1,1)
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("SrcBlend", Int) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("DstBlend", Int) = 10
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode )] _SrcBlend ("SrcBlend" , Int) = 1
[Enum(UnityEngine.Rendering.BlendMode )] _DstBlend ("DstBlend" , Int) = 10
[Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorMask ("ColorMask", Int) = 15
}
SubShader {
@@ -17,9 +18,10 @@ Shader "FlashTools/SwfGrabSimple" {
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Cull Off
Lighting Off
ZWrite Off
ColorMask [_ColorMask]
BlendOp [_BlendOp]
Blend [_SrcBlend] [_DstBlend]

View File

@@ -3,9 +3,10 @@ Shader "FlashTools/SwfSimple" {
[PerRendererData] _MainTex ("Main Texture", 2D ) = "white" {}
[PerRendererData] _Tint ("Tint" , Color) = (1,1,1,1)
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("SrcBlend", Int) = 1
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("DstBlend", Int) = 10
[Enum(UnityEngine.Rendering.BlendOp )] _BlendOp ("BlendOp" , Int) = 0
[Enum(UnityEngine.Rendering.BlendMode )] _SrcBlend ("SrcBlend" , Int) = 1
[Enum(UnityEngine.Rendering.BlendMode )] _DstBlend ("DstBlend" , Int) = 10
[Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorMask ("ColorMask", Int) = 15
}
SubShader {
@@ -17,9 +18,10 @@ Shader "FlashTools/SwfSimple" {
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Cull Off
Lighting Off
ZWrite Off
ColorMask [_ColorMask]
BlendOp [_BlendOp]
Blend [_SrcBlend] [_DstBlend]

View File

@@ -313,12 +313,13 @@ namespace FlashTools.Internal {
}
class BakedGroup {
public SwfInstanceData.Types Type;
public SwfBlendModeData.Types BlendType;
public int ClipDepth;
public int StartVertex;
public int TriangleCount;
public Material Material;
public SwfInstanceData.Types Type;
public SwfInstanceData.ColorModes ColorMode;
public SwfBlendModeData.Types BlendMode;
public int ClipDepth;
public int StartVertex;
public int TriangleCount;
public Material Material;
}
static SwfClipAsset.Frame BakeClipFrame(
@@ -375,12 +376,14 @@ namespace FlashTools.Internal {
if ( baked_groups.Count == 0 ||
baked_groups[baked_groups.Count - 1].Type != inst.Type ||
baked_groups[baked_groups.Count - 1].BlendType != inst.BlendMode.type ||
baked_groups[baked_groups.Count - 1].ColorMode != inst.ColorMode ||
baked_groups[baked_groups.Count - 1].BlendMode != inst.BlendMode.type ||
baked_groups[baked_groups.Count - 1].ClipDepth != inst.ClipDepth )
{
baked_groups.Add(new BakedGroup{
Type = inst.Type,
BlendType = inst.BlendMode.type,
ColorMode = inst.ColorMode,
BlendMode = inst.BlendMode.type,
ClipDepth = inst.ClipDepth,
StartVertex = baked_vertices.Count - 4,
TriangleCount = 0,
@@ -400,10 +403,12 @@ namespace FlashTools.Internal {
group.Material = settings_holder.GetIncrMaskMaterial();
break;
case SwfInstanceData.Types.Group:
group.Material = settings_holder.GetSimpleMaterial(group.BlendType);
group.Material = settings_holder.GetSimpleMaterial(
group.ColorMode, group.BlendMode);
break;
case SwfInstanceData.Types.Masked:
group.Material = settings_holder.GetMaskedMaterial(group.BlendType, group.ClipDepth);
group.Material = settings_holder.GetMaskedMaterial(
group.ColorMode, group.BlendMode, group.ClipDepth);
break;
case SwfInstanceData.Types.MaskReset:
group.Material = settings_holder.GetDecrMaskMaterial();

View File

@@ -145,6 +145,7 @@ namespace FlashTools.Internal {
: parent_masked + self_masks.Count;
frame.Instances.Add(new SwfInstanceData{
Type = frame_inst_type,
ColorMode = SwfInstanceData.ColorModes.RGBA,
ClipDepth = (ushort)frame_inst_clip_depth,
Bitmap = bitmap_id,
Matrix = SwfMatrixData.FromUMatrix(child_matrix * bitmap_matrix.ToUMatrix()),
@@ -191,6 +192,7 @@ namespace FlashTools.Internal {
if ( mask.ClipDepth < depth ) {
frame.Instances.Add(new SwfInstanceData{
Type = SwfInstanceData.Types.MaskReset,
ColorMode = SwfInstanceData.ColorModes.RGBA,
ClipDepth = 0,
Bitmap = mask.Bitmap,
Matrix = mask.Matrix,

View File

@@ -8,15 +8,15 @@ namespace FlashTools.Internal.SwfTools.SwfTypes {
Multiply, // +
Screen, // +
Lighten, // +
Darken, // + GrabPass
Difference, // + GrabPass
Darken, // +
Difference, // +
Add, // +
Subtract, // +
Invert, // + GrabPass
Invert, // +
Alpha, // -
Erase, // -
Overlay, // + GrabPass
Hardlight // + GrabPass
Overlay, // +
Hardlight // +
}
public Mode Value;

View File

@@ -99,11 +99,14 @@ namespace FlashTools.Internal {
}
void PregenerateMaterials() {
var blend_types = System.Enum.GetValues(typeof(SwfBlendModeData.Types));
foreach ( SwfBlendModeData.Types blend_type in blend_types ) {
GetSimpleMaterial(blend_type);
for ( var i = 0; i < 10; ++i ) {
GetMaskedMaterial(blend_type, i);
var color_modes = System.Enum.GetValues(typeof(SwfInstanceData.ColorModes));
var blend_modes = System.Enum.GetValues(typeof(SwfBlendModeData.Types));
foreach ( SwfInstanceData.ColorModes color_mode in color_modes ) {
foreach ( SwfBlendModeData.Types blend_mode in blend_modes ) {
GetSimpleMaterial(color_mode, blend_mode);
for ( var i = 0; i < 10; ++i ) {
GetMaskedMaterial(color_mode, blend_mode, i);
}
}
}
GetIncrMaskMaterial();
@@ -193,9 +196,21 @@ namespace FlashTools.Internal {
}
static Material FillMaterial(
Material material, SwfBlendModeData.Types blend_type, int stencil_id)
Material material, SwfInstanceData.ColorModes color_mode, SwfBlendModeData.Types blend_mode, int stencil_id)
{
switch ( blend_type ) {
switch ( color_mode ) {
case SwfInstanceData.ColorModes.RGBA:
material.SetInt("_ColorMask", (int)ColorWriteMask.All);
break;
case SwfInstanceData.ColorModes.A:
material.SetInt("_ColorMask", (int)ColorWriteMask.Alpha);
break;
default:
throw new UnityException(string.Format(
"SwfSettings. Incorrect color mode: {0}",
color_mode));
}
switch ( blend_mode ) {
case SwfBlendModeData.Types.Normal:
material.SetInt("_BlendOp" , (int)BlendOp.Add);
material.SetInt("_SrcBlend", (int)BlendMode.One);
@@ -258,8 +273,8 @@ namespace FlashTools.Internal {
break;
default:
throw new UnityException(string.Format(
"SwfSettings. Incorrect blend type: {0}",
blend_type));
"SwfSettings. Incorrect blend mode: {0}",
blend_mode));
}
material.SetInt("_StencilID", stencil_id);
return material;
@@ -271,26 +286,33 @@ namespace FlashTools.Internal {
//
// ---------------------------------------------------------------------
public Material GetSimpleMaterial(SwfBlendModeData.Types blend_type) {
public Material GetSimpleMaterial(
SwfInstanceData.ColorModes color_mode,
SwfBlendModeData.Types blend_mode)
{
return LoadOrCreateMaterial(
CheckAndGetShader(SelectShader(false, blend_type)),
(dir_path, filename) => {
return string.Format(
"{0}/{1}_{2}.mat",
dir_path, filename, blend_type);
},
material => FillMaterial(material, blend_type, 0));
}
public Material GetMaskedMaterial(SwfBlendModeData.Types blend_type, int stencil_id) {
return LoadOrCreateMaterial(
CheckAndGetShader(SelectShader(true, blend_type)),
CheckAndGetShader(SelectShader(false, blend_mode)),
(dir_path, filename) => {
return string.Format(
"{0}/{1}_{2}_{3}.mat",
dir_path, filename, blend_type, stencil_id);
dir_path, filename, color_mode, blend_mode);
},
material => FillMaterial(material, blend_type, stencil_id));
material => FillMaterial(material, color_mode, blend_mode, 0));
}
public Material GetMaskedMaterial(
SwfInstanceData.ColorModes color_mode,
SwfBlendModeData.Types blend_mode,
int stencil_id)
{
return LoadOrCreateMaterial(
CheckAndGetShader(SelectShader(true, blend_mode)),
(dir_path, filename) => {
return string.Format(
"{0}/{1}_{2}_{3}_{4}.mat",
dir_path, filename, color_mode, blend_mode, stencil_id);
},
material => FillMaterial(material, color_mode, blend_mode, stencil_id));
}
public Material GetIncrMaskMaterial() {

View File

@@ -112,13 +112,13 @@ namespace FlashTools {
Multiply,
Screen,
Lighten,
Darken,
Difference,
Darken, // GrabPass
Difference, // GrabPass
Add,
Subtract,
Invert,
Overlay,
Hardlight
Invert, // GrabPass
Overlay, // GrabPass
Hardlight // GrabPass
}
public Types type;
@@ -137,7 +137,7 @@ namespace FlashTools {
SwfBlendModeData a, SwfBlendModeData b)
{
return new SwfBlendModeData{
type = (a.type == Types.Normal ? b.type : a.type)};
type = a.type == Types.Normal ? b.type : a.type};
}
}
@@ -187,7 +187,12 @@ namespace FlashTools {
Masked,
MaskReset
}
public enum ColorModes {
RGBA,
A
}
public Types Type = Types.Group;
public ColorModes ColorMode = ColorModes.RGBA;
public ushort ClipDepth = 0;
public ushort Bitmap = 0;
public SwfMatrixData Matrix = SwfMatrixData.identity;