masks wip

This commit is contained in:
2016-07-28 01:31:21 +07:00
parent bbb2e3e741
commit e24ad6b4d7
16 changed files with 193 additions and 58 deletions

View File

@@ -90,9 +90,10 @@
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfTypes\SwfShapesWithStyle.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfTypes\SwfShortHeader.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\Editor\SwfTools\SwfTypes\SwfSurfaceFilters.cs" />
<None Include="Assets\FlashTools\Shaders\FlashMasked.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMaskReset.shader" />
<None Include="Assets\FlashTools\Shaders\FlashAnim.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMask.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMasked.shader" />
<Reference Include="UnityEditor.Advertisements">
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/Advertisements/Editor/UnityEditor.Advertisements.dll</HintPath>
</Reference>

View File

@@ -49,9 +49,10 @@
<Compile Include="Assets\FlashTools\Scripts\FlashAnimAsset.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfAnimation.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfAnimationAsset.cs" />
<None Include="Assets\FlashTools\Shaders\FlashMasked.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMaskReset.shader" />
<None Include="Assets\FlashTools\Shaders\FlashAnim.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMask.shader" />
<None Include="Assets\FlashTools\Shaders\FlashMasked.shader" />
<Reference Include="UnityEngine.UI">
<HintPath>/Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
</Reference>

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 78dd883487f0e4a3281308f1e4c7b131
timeCreated: 1469337635
guid: ade26528d580c44489d6823e293f3a91
timeCreated: 1469644049
licenseType: Free
DefaultImporter:
userData:

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 68ed33ba61a1243f99660fd4aa9e4777
timeCreated: 1469371596
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -72,27 +72,49 @@ namespace FlashTools.Internal {
return AddDisplayListToFrame(
context,
context.DisplayList,
false,
0,
0,
null,
Matrix4x4.identity,
SwfAnimationColorTransform.identity,
frame);
}
static SwfAnimationFrameData AddDisplayListToFrame(
SwfContext ctx,
SwfDisplayList dl,
ushort parent_depth,
ushort parent_clip_depth,
Matrix4x4 parent_matrix,
SwfAnimationColorTransform parent_color_transform,
SwfAnimationFrameData frame)
SwfContext ctx,
SwfDisplayList dl,
bool parent_masked,
ushort parent_mask,
List<SwfAnimationInstanceData> parent_masks,
Matrix4x4 parent_matrix,
SwfAnimationColorTransform parent_color_transform,
SwfAnimationFrameData frame)
{
var masks = new List<SwfAnimationInstanceData>();
var instances = dl.Instances.Values
.Where(p => p.Visible);
foreach ( var inst in instances ) {
foreach ( var mask in masks ) {
if ( mask.ClipDepth < inst.Depth ) {
frame.Instances.Add(new SwfAnimationInstanceData{
Type = SwfAnimationInstanceType.MaskReset,
ClipDepth = 0,
Bitmap = mask.Bitmap,
Matrix = mask.Matrix,
ColorTransform = mask.ColorTransform
});
}
}
masks.RemoveAll(p => p.ClipDepth < inst.Depth);
switch ( inst.Type ) {
case SwfDisplayInstanceType.Shape:
Debug.LogFormat(
"--- Bitmap --- Depth: {0}, ClipDepth: {1}, Mask: {2}",
inst.Depth, inst.ClipDepth, inst.ClipDepth > 0);
var shape_def = ctx.Library.FindDefine<SwfLibraryShapeDefine>(inst.Id);
if ( shape_def != null ) {
for ( var i = 0; i < shape_def.Bitmaps.Length; ++i ) {
@@ -100,31 +122,36 @@ namespace FlashTools.Internal {
var bitmap_matrix = i < shape_def.Matrices.Length ? shape_def.Matrices[i] : SwfMatrix.identity;
var bitmap_def = ctx.Library.FindDefine<SwfLibraryBitmapDefine>(bitmap_id);
if ( bitmap_def != null ) {
var global_depth = (parent_depth + inst.Depth);
var local_clip_depth = (parent_clip_depth != 0 ? parent_clip_depth : inst.ClipDepth);
var global_clip_depth = (parent_clip_depth != 0 ? parent_clip_depth : (local_clip_depth != 0 ? (global_depth + local_clip_depth - 1) : 0));
Debug.LogFormat("--- Depth: {0}, ClipDepth: {1}", global_depth, global_clip_depth);
frame.Instances.Add(new SwfAnimationInstanceData{
Depth = (ushort)global_depth,
ClipDepth = (ushort)global_clip_depth,
Type = (parent_mask > 0 || inst.ClipDepth > 0) ? SwfAnimationInstanceType.Mask : (parent_masked || masks.Count > 0 ? SwfAnimationInstanceType.Masked : SwfAnimationInstanceType.Group),
ClipDepth = (ushort)(parent_mask > 0 ? parent_mask : (inst.ClipDepth > 0 ? inst.ClipDepth : (ushort)0)),
Bitmap = bitmap_id,
Matrix = parent_matrix * inst.Matrix.ToUnityMatrix() * bitmap_matrix.ToUnityMatrix(),
ColorTransform = parent_color_transform * inst.ColorTransform.ToAnimationColorTransform()});
if ( parent_mask > 0 ) {
parent_masks.Add(frame.Instances[frame.Instances.Count - 1]);
} else if ( inst.ClipDepth > 0 ) {
masks.Add(frame.Instances[frame.Instances.Count - 1]);
}
}
}
}
break;
case SwfDisplayInstanceType.Sprite:
Debug.LogFormat(
"--- Sprite --- Depth: {0}, ClipDepth: {1}, Mask: {2}",
inst.Depth, inst.ClipDepth, inst.ClipDepth > 0);
var sprite_def = ctx.Library.FindDefine<SwfLibrarySpriteDefine>(inst.Id);
if ( sprite_def != null ) {
var sprite_inst = inst as SwfDisplaySpriteInstance;
AddDisplayListToFrame(
ctx,
sprite_inst.DisplayList,
(ushort)(parent_depth + sprite_inst.Depth - 1),
(ushort)(parent_clip_depth != 0 ? parent_clip_depth : sprite_inst.ClipDepth),
parent_masked ? parent_masked : (masks.Count > 0),
(ushort)(parent_mask > 0 ? parent_mask : (inst.ClipDepth > 0 ? inst.ClipDepth : (ushort)0)),
parent_mask > 0 ? parent_masks : (inst.ClipDepth > 0 ? masks : null),
parent_matrix * sprite_inst.Matrix.ToUnityMatrix(),
parent_color_transform * sprite_inst.ColorTransform.ToAnimationColorTransform(),
frame);
@@ -135,6 +162,16 @@ namespace FlashTools.Internal {
"Unsupported SwfDisplayInstanceType: {0}", inst.Type));
}
}
foreach ( var mask in masks ) {
frame.Instances.Add(new SwfAnimationInstanceData{
Type = SwfAnimationInstanceType.MaskReset,
ClipDepth = 0,
Bitmap = mask.Bitmap,
Matrix = mask.Matrix,
ColorTransform = mask.ColorTransform
});
}
masks.Clear();
return frame;
}

View File

@@ -24,15 +24,14 @@ namespace FlashTools {
enum GroupType {
Mask,
Masked,
Group
Group,
MaskReset
}
class Group {
public GroupType Type;
public List<int> Triangles;
public int ClipDepth;
public Material Material;
public SwfAnimationInstanceType Type;
public List<int> Triangles;
public Material Material;
}
List<Group> _groups = new List<Group>();
@@ -141,6 +140,35 @@ namespace FlashTools {
_addcolors.Add(inst.ColorTransform.Add);
_addcolors.Add(inst.ColorTransform.Add);
if ( _groups.Count == 0 || _groups[_groups.Count - 1].Type != inst.Type) {
var gr = new Group();
gr.Type = inst.Type;
gr.Triangles = new List<int>();
_groups.Add(gr);
}
/*
if ( inst.MaskGroup != 0 ) {
if ( _groups.Count == 0 || _groups[_groups.Count - 1].MaskGroup != inst.MaskGroup ) {
var gr = new Group();
gr.Type = GroupType.Mask;
gr.MaskGroup = inst.MaskGroup;
gr.Triangles = new List<int>();
_groups.Add(gr);
} else {
// batching mask group
}
} else {
if ( _groups.Count == 0 ) {
var gr = new Group();
gr.Type = GroupType.Group;
gr.MaskGroup = 0;
gr.Triangles = new List<int>();
_groups.Add(gr);
}
}*/
/*
if ( inst.ClipDepth != 0 ) {
var gr = new Group();
gr.Type = GroupType.Mask;
@@ -184,7 +212,7 @@ namespace FlashTools {
// nothing
}
}
}
}*/
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2);
_groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 1);
@@ -199,20 +227,21 @@ namespace FlashTools {
for ( var i = 0; i < full_groups.Length; ++i ) {
var gr = full_groups[i];
switch ( gr.Type ) {
case GroupType.Mask:
case SwfAnimationInstanceType.Mask:
gr.Material = new Material(Shader.Find("FlashTools/FlashMask"));
gr.Material.SetTexture("_MainTex", Asset.Atlas);
gr.Material.SetInt("_StencilID", i+1);
break;
case GroupType.Masked:
gr.Material = new Material(Shader.Find("FlashTools/FlashMasked"));
gr.Material.SetTexture("_MainTex", Asset.Atlas);
gr.Material.SetInt("_StencilID", i);
break;
case GroupType.Group:
case SwfAnimationInstanceType.Group:
gr.Material = new Material(Shader.Find("FlashTools/FlashAnim"));
gr.Material.SetTexture("_MainTex", Asset.Atlas);
gr.Material.SetInt("_StencilID", 0);
break;
case SwfAnimationInstanceType.Masked:
gr.Material = new Material(Shader.Find("FlashTools/FlashMasked"));
gr.Material.SetTexture("_MainTex", Asset.Atlas);
break;
case SwfAnimationInstanceType.MaskReset:
gr.Material = new Material(Shader.Find("FlashTools/FlashMaskReset"));
gr.Material.SetTexture("_MainTex", Asset.Atlas);
break;
}
}

View File

@@ -36,9 +36,16 @@ namespace FlashTools {
}
}
public enum SwfAnimationInstanceType {
Mask,
Group,
Masked,
MaskReset
}
[System.Serializable]
public class SwfAnimationInstanceData {
public ushort Depth = 0;
public SwfAnimationInstanceType Type = SwfAnimationInstanceType.Group;
public ushort ClipDepth = 0;
public ushort Bitmap = 0;
public Matrix4x4 Matrix = Matrix4x4.identity;

View File

@@ -1,7 +1,6 @@
Shader "FlashTools/FlashMask" {
Properties {
[PerRendererData] _MainTex ("Main Texture", 2D ) = "white" {}
[PerRendererData] _StencilID ("Stencil ID" , Int) = 0
[PerRendererData] _MainTex ("Main Texture", 2D) = "white" {}
}
SubShader {
@@ -21,9 +20,9 @@ Shader "FlashTools/FlashMask" {
Pass {
Stencil {
Ref [_StencilID]
Ref 0
Comp always
Pass replace
Pass IncrSat
}
CGPROGRAM
#pragma vertex vert

View File

@@ -0,0 +1,61 @@
Shader "FlashTools/FlashMaskReset" {
Properties {
[PerRendererData] _MainTex ("Main Texture", 2D) = "white" {}
}
SubShader {
Tags {
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
ColorMask 0
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass {
Stencil {
Ref 0
Comp always
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;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target {
fixed4 c = tex2D(_MainTex, IN.uv);
if ( c.a < 0.01 ) {
discard;
}
return c;
}
ENDCG
}
}
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f1e8cd2344e824b6a9b12efcacde262c
timeCreated: 1456948894
licenseType: Free
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,7 +1,6 @@
Shader "FlashTools/FlashMasked" {
Properties {
[PerRendererData] _MainTex ("Main Texture", 2D ) = "white" {}
[PerRendererData] _StencilID ("Stencil ID" , Int) = 0
[PerRendererData] _MainTex ("Main Texture", 2D) = "white" {}
}
SubShader {
@@ -20,8 +19,8 @@ Shader "FlashTools/FlashMasked" {
Pass {
Stencil {
Ref [_StencilID]
Comp equal
Ref 0
Comp Less
}
CGPROGRAM
#pragma vertex vert

View File

@@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: f1e8cd2344e824b6a9b12efcacde262c
timeCreated: 1456948894
guid: 2fb923af3a46a49fdb944d636b6f79e8
timeCreated: 1469642514
licenseType: Free
ShaderImporter:
defaultTextures: []

View File

@@ -336,7 +336,7 @@ if (!Function.prototype.bind) {
fttim.remove_empty_layers(timeline);
if ( timeline.layers.length > 0 && timeline.frameCount > 1 ) {
timeline.selectAllFrames();
timeline.convertToKeyframes();
//timeline.convertToKeyframes();
}
ft.array_reverse_foreach(timeline.layers, function(layer, index) {
timeline.setSelectedLayers(index);