From cf2e980560a351298e4b044c1665d745c5793d4c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 25 Aug 2016 13:52:13 +0700 Subject: [PATCH] SwfBakedAnimation to SwfAnimation --- Assembly-CSharp-Editor.csproj | 1 - Assembly-CSharp.csproj | 2 +- .../Editor/SwfAnimationAssetEditor.cs | 32 +- .../Editor/SwfAnimationEditor.cs.meta | 6 +- .../Editor/SwfBakedAnimationEditor.cs | 27 -- .../Editor/SwfBakedAnimationEditor.cs.meta | 12 - Assets/FlashTools/Scripts/SwfAnimation.cs | 361 ++++++++++-------- .../FlashTools/Scripts/SwfAnimation.cs.meta | 6 +- .../FlashTools/Scripts/SwfBakedAnimation.cs | 261 ------------- .../Scripts/SwfBakedAnimation.cs.meta | 12 - Assets/FlashTools/Scripts/SwfManager.cs | 24 +- 11 files changed, 234 insertions(+), 510 deletions(-) delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs delete mode 100644 Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs.meta delete mode 100644 Assets/FlashTools/Scripts/SwfBakedAnimation.cs delete mode 100644 Assets/FlashTools/Scripts/SwfBakedAnimation.cs.meta diff --git a/Assembly-CSharp-Editor.csproj b/Assembly-CSharp-Editor.csproj index fd9f725..e03f3d2 100644 --- a/Assembly-CSharp-Editor.csproj +++ b/Assembly-CSharp-Editor.csproj @@ -48,7 +48,6 @@ - diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index fa8cad1..0fecfe2 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -49,7 +49,7 @@ - + diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs index eeaea03..26e81c6 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetEditor.cs @@ -59,24 +59,20 @@ namespace FlashTools.Internal { } } - GameObject CreateAnimationGO(bool baked) { + GameObject CreateAnimationGO() { if ( _asset ) { var anim_go = new GameObject(_asset.name); anim_go.AddComponent(); anim_go.AddComponent(); - if ( baked ) { - anim_go.AddComponent().Asset = _asset; - anim_go.GetComponent().BakeFrameMeshes(); - } else { - anim_go.AddComponent().Asset = _asset; - } + anim_go.AddComponent().Asset = _asset; + anim_go.GetComponent().BakeFrameMeshes(); return anim_go; } return null; } - void CreateAnimationPrefab(bool baked) { - var anim_go = CreateAnimationGO(baked); + void CreateAnimationPrefab() { + var anim_go = CreateAnimationGO(); if ( anim_go ) { var prefab_path = GetPrefabPath(); if ( !string.IsNullOrEmpty(prefab_path) ) { @@ -93,8 +89,8 @@ namespace FlashTools.Internal { } } - void CreateAnimationOnScene(bool baked) { - var anim_go = CreateAnimationGO(baked); + void CreateAnimationOnScene() { + var anim_go = CreateAnimationGO(); if ( anim_go ) { Undo.RegisterCreatedObjectUndo(anim_go, "Create SwfAnimation"); } @@ -167,20 +163,10 @@ namespace FlashTools.Internal { GUILayout.BeginHorizontal(); { if ( GUILayout.Button("Create animation prefab") ) { - CreateAnimationPrefab(false); + CreateAnimationPrefab(); } if ( GUILayout.Button("Create animation on scene") ) { - CreateAnimationOnScene(false); - } - } - GUILayout.EndHorizontal(); - GUILayout.BeginHorizontal(); - { - if ( GUILayout.Button("Create baked animation prefab") ) { - CreateAnimationPrefab(true); - } - if ( GUILayout.Button("Create baked animation on scene") ) { - CreateAnimationOnScene(true); + CreateAnimationOnScene(); } } GUILayout.EndHorizontal(); diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs.meta b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs.meta index 219c7d1..39aad5d 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs.meta +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationEditor.cs.meta @@ -1,7 +1,7 @@ fileFormatVersion: 2 -guid: 40e69fd373e75451283f0389b624e18e -timeCreated: 1458054572 -licenseType: Free +guid: 7de32773fb5894d2a8e5b1f15f3b6549 +timeCreated: 1472043063 +licenseType: Pro MonoImporter: serializedVersion: 2 defaultReferences: [] diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs deleted file mode 100644 index f7de2ad..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs +++ /dev/null @@ -1,27 +0,0 @@ -using UnityEngine; -using UnityEditor; -using System.Collections.Generic; - -namespace FlashTools.Internal { - [CustomEditor(typeof(SwfBakedAnimation))] - public class SwfBakedAnimationEditor : Editor { - SwfBakedAnimation _animation = null; - - void OnEnable() { - _animation = target as SwfBakedAnimation; - } - - public override void OnInspectorGUI() { - DrawDefaultInspector(); - - if ( _animation.Asset && _animation.frameCount > 1 ) { - var new_current_frame = EditorGUILayout.IntSlider( - "Frame", _animation.currentFrame, - 0, _animation.frameCount - 1); - if ( new_current_frame != _animation.currentFrame ) { - _animation.currentFrame = new_current_frame; - } - } - } - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs.meta b/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs.meta deleted file mode 100644 index 39aad5d..0000000 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfBakedAnimationEditor.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 7de32773fb5894d2a8e5b1f15f3b6549 -timeCreated: 1472043063 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/SwfAnimation.cs b/Assets/FlashTools/Scripts/SwfAnimation.cs index 1a6572b..da1edc9 100644 --- a/Assets/FlashTools/Scripts/SwfAnimation.cs +++ b/Assets/FlashTools/Scripts/SwfAnimation.cs @@ -1,31 +1,49 @@ using UnityEngine; -using System.Linq; using System.Collections.Generic; using FlashTools.Internal; -#if UNITY_EDITOR -using UnityEditor; -#endif - namespace FlashTools { [ExecuteInEditMode] [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] public class SwfAnimation : MonoBehaviour { - public SwfAnimationAsset Asset = null; - public int GroupCount = 0; + public SwfAnimationAsset Asset = null; - public int SortingOrder = 0; [SwfSortingLayer] public string SortingLayer = "Default"; + public int SortingOrder = 0; - int _current_frame = 0; - float _frame_timer = 0.0f; - string _last_asset_path = string.Empty; + MeshFilter _meshFilter = null; + MeshRenderer _meshRenderer = null; - List _uvs = new List(); - List _mulcolors = new List(); - List _addcolors = new List(); - List _vertices = new List(); + int _current_frame = 0; + float _frame_timer = 0.0f; + + public int frameCount { + get { return Asset ? Asset.Data.Frames.Count : 0; } + } + + public int currentFrame { + get { return _current_frame; } + set { + _current_frame = Mathf.Clamp(value, 0, frameCount - 1); + FixCurrentMesh(); + } + } + + // ------------------------------------------------------------------------ + // + // Stuff + // + // ------------------------------------------------------------------------ + + MaterialPropertyBlock MatPropBlock; + + class Frame { + public Mesh Mesh; + public Material[] Materials; + } + + List _frames = new List(); class Group { public SwfAnimationInstanceType Type; @@ -34,52 +52,135 @@ namespace FlashTools { public Material Material; } - List _groups = new List(); + List _uvs = new List(); + List _mulcolors = new List(); + List _addcolors = new List(); + List _vertices = new List(); + List _groups = new List(); + List _materials = new List(); - public int frameCount { - get { return Asset ? Asset.Data.Frames.Count : 0; } + void ClearTempBakeData() { + _uvs.Clear(); + _mulcolors.Clear(); + _addcolors.Clear(); + _vertices.Clear(); + _groups.Clear(); + _materials.Clear(); } - public int currentFrame { - get { return _current_frame; } - set { _current_frame = Mathf.Clamp(value, 0, frameCount - 1); } + public void BakeFrameMeshes() { + if ( Asset && Asset.Atlas && Asset.Data != null && Asset.Data.Frames.Count > 0 ) { + MatPropBlock = new MaterialPropertyBlock(); + MatPropBlock.SetTexture("_MainTex", Asset.Atlas); + for ( var i = 0; i < Asset.Data.Frames.Count; ++i ) { + var frame = Asset.Data.Frames[i]; + BakeFrameMesh(frame); + } + } + FixCurrentMesh(); } - // ------------------------------------------------------------------------ - // - // Messages - // - // ------------------------------------------------------------------------ + void BakeFrameMesh(SwfAnimationFrameData frame) { + var swf_manager = SwfManager.Instance; + for ( var i = 0; i < frame.Instances.Count; ++i ) { + var inst = frame.Instances[i]; + var bitmap = inst != null ? FindBitmap(inst.Bitmap) : null; + if ( bitmap != null ) { + var width = bitmap.RealSize.x / 20.0f; + var height = bitmap.RealSize.y / 20.0f; - void Update() { - if ( Asset ) { - _frame_timer += Asset.Data.FrameRate * Time.deltaTime; - while ( _frame_timer > 1.0f ) { - _frame_timer -= 1.0f; - ++_current_frame; - if ( _current_frame > frameCount - 1 ) { - _current_frame = 0; + var v0 = new Vector3( 0, 0, 0); + var v1 = new Vector3(width, 0, 0); + var v2 = new Vector3(width, height, 0); + var v3 = new Vector3( 0, height, 0); + + var matrix = + Matrix4x4.Scale(new Vector3( + +1.0f / Asset.Settings.PixelsPerUnit, + -1.0f / Asset.Settings.PixelsPerUnit, + +1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix; + + _vertices.Add(matrix.MultiplyPoint3x4(v0)); + _vertices.Add(matrix.MultiplyPoint3x4(v1)); + _vertices.Add(matrix.MultiplyPoint3x4(v2)); + _vertices.Add(matrix.MultiplyPoint3x4(v3)); + + var source_rect = bitmap.SourceRect; + _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin)); + _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin)); + _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax)); + _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMax)); + + _mulcolors.Add(inst.ColorTransform.Mul); + _mulcolors.Add(inst.ColorTransform.Mul); + _mulcolors.Add(inst.ColorTransform.Mul); + _mulcolors.Add(inst.ColorTransform.Mul); + + _addcolors.Add(inst.ColorTransform.Add); + _addcolors.Add(inst.ColorTransform.Add); + _addcolors.Add(inst.ColorTransform.Add); + _addcolors.Add(inst.ColorTransform.Add); + + if ( _groups.Count == 0 || + _groups[_groups.Count - 1].Type != inst.Type || + _groups[_groups.Count - 1].ClipDepth != inst.ClipDepth ) + { + _groups.Add(new Group{ + Type = inst.Type, + ClipDepth = inst.ClipDepth, + Triangles = new List(), + Material = null + }); } - } - } else { - #if UNITY_EDITOR - OnValidate(); - #endif - } - } - #if UNITY_EDITOR - void OnValidate() { - if ( Asset ) { - _last_asset_path = AssetDatabase.GetAssetPath(Asset); - } else { - if ( !string.IsNullOrEmpty(_last_asset_path) ) { - Asset = AssetDatabase.LoadAssetAtPath(_last_asset_path); - EditorUtility.SetDirty(this); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 1); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 3); + _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); } } + + for ( var i = 0; i < _groups.Count; ++i ) { + var group = _groups[i]; + switch ( group.Type ) { + case SwfAnimationInstanceType.Mask: + group.Material = swf_manager.GetIncrMaskMaterial(); + break; + case SwfAnimationInstanceType.Group: + group.Material = swf_manager.GetSimpleMaterial(); + break; + case SwfAnimationInstanceType.Masked: + group.Material = swf_manager.GetMaskedMaterial(group.ClipDepth); + break; + case SwfAnimationInstanceType.MaskReset: + group.Material = swf_manager.GetDecrMaskMaterial(); + break; + } + } + + for ( var i = 0; i < _groups.Count; ++i ) { + var group = _groups[i]; + _materials.Add(group.Material); + } + + var mesh = new Mesh(); + mesh.subMeshCount = _groups.Count; + mesh.SetVertices(_vertices); + for ( var i = 0; i < _groups.Count; ++i ) { + mesh.SetTriangles(_groups[i].Triangles, i); + } + mesh.SetUVs(0, _uvs); + mesh.SetUVs(1, _addcolors); + mesh.SetColors(_mulcolors); + mesh.RecalculateNormals(); + + _frames.Add(new Frame{ + Mesh = mesh, + Materials = _materials.ToArray()}); + ClearTempBakeData(); } - #endif SwfAnimationBitmapData FindBitmap(int bitmap_id) { if ( Asset ) { @@ -93,117 +194,67 @@ namespace FlashTools { return null; } - void OnRenderObject() { + void UpdateFrameTimer() { if ( Asset ) { - _vertices.Clear(); - _uvs.Clear(); - _mulcolors.Clear(); - _addcolors.Clear(); - _groups.Clear(); - - var frame = Asset.Data.Frames[currentFrame]; - foreach ( var inst in frame.Instances ) { - var bitmap = FindBitmap(inst.Bitmap); - if ( bitmap != null ) { - var width = bitmap.RealSize.x / 20.0f; - var height = bitmap.RealSize.y / 20.0f; - - var v0 = new Vector3( 0, 0, 0); - var v1 = new Vector3(width, 0, 0); - var v2 = new Vector3(width, height, 0); - var v3 = new Vector3( 0, height, 0); - - var matrix = - Matrix4x4.Scale(new Vector3( - 1.0f / Asset.Settings.PixelsPerUnit, - -1.0f / Asset.Settings.PixelsPerUnit, - 1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix; - - _vertices.Add(matrix.MultiplyPoint3x4(v0)); - _vertices.Add(matrix.MultiplyPoint3x4(v1)); - _vertices.Add(matrix.MultiplyPoint3x4(v2)); - _vertices.Add(matrix.MultiplyPoint3x4(v3)); - - var source_rect = bitmap.SourceRect; - _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin)); - _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin)); - _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax)); - _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMax)); - - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - - if ( _groups.Count == 0 || _groups[_groups.Count - 1].Type != inst.Type || _groups[_groups.Count - 1].ClipDepth != inst.ClipDepth) { - var gr = new Group(); - gr.Type = inst.Type; - gr.ClipDepth = inst.ClipDepth; - gr.Triangles = new List(); - _groups.Add(gr); - } - - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 1); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 3); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); - } + if ( _frames.Count == 0 ) { + BakeFrameMeshes(); + FixCurrentMesh(); } - - var full_groups = _groups.Where(p => p.Triangles.Count > 0).ToArray(); - for ( var i = 0; i < full_groups.Length; ++i ) { - var gr = full_groups[i]; - switch ( gr.Type ) { - case SwfAnimationInstanceType.Mask: - gr.Material = new Material(Shader.Find("FlashTools/SwfIncrMask")); - gr.Material.SetTexture("_MainTex", Asset.Atlas); - break; - case SwfAnimationInstanceType.Group: - gr.Material = new Material(Shader.Find("FlashTools/SwfSimple")); - gr.Material.SetTexture("_MainTex", Asset.Atlas); - break; - case SwfAnimationInstanceType.Masked: - gr.Material = new Material(Shader.Find("FlashTools/SwfMasked")); - gr.Material.SetTexture("_MainTex", Asset.Atlas); - gr.Material.SetInt("_StencilID", gr.ClipDepth); - break; - case SwfAnimationInstanceType.MaskReset: - gr.Material = new Material(Shader.Find("FlashTools/SwfDecrMask")); - gr.Material.SetTexture("_MainTex", Asset.Atlas); - break; + _frame_timer += Asset.Data.FrameRate * Time.deltaTime; + while ( _frame_timer > 1.0f ) { + _frame_timer -= 1.0f; + ++_current_frame; + if ( _current_frame > frameCount - 1 ) { + _current_frame = 0; } + FixCurrentMesh(); } + } + } - var mesh_renderer = GetComponent(); - mesh_renderer.sharedMaterials = full_groups.Select(p => p.Material).ToArray(); - mesh_renderer.sortingOrder = SortingOrder; - mesh_renderer.sortingLayerName = SortingLayer; + void FixCurrentMesh() { + if ( _frames.Count > 0 ) { + var frame = _frames[Mathf.Clamp(currentFrame, 0, _frames.Count)]; + _meshFilter.sharedMesh = frame.Mesh; + _meshRenderer.sharedMaterials = frame.Materials; + _meshRenderer.sortingOrder = SortingOrder; + _meshRenderer.sortingLayerName = SortingLayer; + _meshRenderer.SetPropertyBlock(MatPropBlock); + } + } - var mesh_filter = GetComponent(); - if ( mesh_filter ) { - var mesh = mesh_filter.sharedMesh - ? mesh_filter.sharedMesh - : new Mesh(); - mesh.Clear(); - mesh.subMeshCount = full_groups.Length; - GroupCount = full_groups.Length; - mesh.SetVertices(_vertices); - for ( var i = 0; i < full_groups.Length; ++i ) { - mesh.SetTriangles(full_groups[i].Triangles, i); - } - mesh.SetUVs(0, _uvs); - mesh.SetUVs(1, _addcolors); - mesh.SetColors(_mulcolors); - mesh.RecalculateNormals(); - mesh_filter.sharedMesh = mesh; - } + // --------------------------------------------------------------------- + // + // Internal + // + // --------------------------------------------------------------------- + + public void InternalUpdate() { + UpdateFrameTimer(); + } + + // --------------------------------------------------------------------- + // + // Messages + // + // --------------------------------------------------------------------- + + void Awake() { + _meshFilter = GetComponent(); + _meshRenderer = GetComponent(); + } + + void OnEnable() { + var swf_manager = SwfManager.Instance; + if ( swf_manager ) { + swf_manager.AddSwfAnimation(this); + } + } + + void OnDisable() { + var swf_manager = SwfManager.Instance; + if ( swf_manager ) { + swf_manager.RemoveSwfAnimation(this); } } } diff --git a/Assets/FlashTools/Scripts/SwfAnimation.cs.meta b/Assets/FlashTools/Scripts/SwfAnimation.cs.meta index 737f75a..9b2eb6c 100644 --- a/Assets/FlashTools/Scripts/SwfAnimation.cs.meta +++ b/Assets/FlashTools/Scripts/SwfAnimation.cs.meta @@ -1,7 +1,7 @@ fileFormatVersion: 2 -guid: e2baa05cb4bda40e5ac10a5f833e104e -timeCreated: 1458054531 -licenseType: Free +guid: 950d548c7e22f4e25a47de474b49e86e +timeCreated: 1472040299 +licenseType: Pro MonoImporter: serializedVersion: 2 defaultReferences: [] diff --git a/Assets/FlashTools/Scripts/SwfBakedAnimation.cs b/Assets/FlashTools/Scripts/SwfBakedAnimation.cs deleted file mode 100644 index 07101bf..0000000 --- a/Assets/FlashTools/Scripts/SwfBakedAnimation.cs +++ /dev/null @@ -1,261 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; -using FlashTools.Internal; - -namespace FlashTools { - [ExecuteInEditMode] - [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] - public class SwfBakedAnimation : MonoBehaviour { - public SwfAnimationAsset Asset = null; - - [SwfSortingLayer] - public string SortingLayer = "Default"; - public int SortingOrder = 0; - - MeshFilter _meshFilter = null; - MeshRenderer _meshRenderer = null; - - int _current_frame = 0; - float _frame_timer = 0.0f; - - public int frameCount { - get { return Asset ? Asset.Data.Frames.Count : 0; } - } - - public int currentFrame { - get { return _current_frame; } - set { - _current_frame = Mathf.Clamp(value, 0, frameCount - 1); - FixCurrentMesh(); - } - } - - // ------------------------------------------------------------------------ - // - // Stuff - // - // ------------------------------------------------------------------------ - - MaterialPropertyBlock MatPropBlock; - - class Frame { - public Mesh Mesh; - public Material[] Materials; - } - - List _frames = new List(); - - class Group { - public SwfAnimationInstanceType Type; - public int ClipDepth; - public List Triangles; - public Material Material; - } - - List _uvs = new List(); - List _mulcolors = new List(); - List _addcolors = new List(); - List _vertices = new List(); - List _groups = new List(); - List _materials = new List(); - - void ClearTempBakeData() { - _uvs.Clear(); - _mulcolors.Clear(); - _addcolors.Clear(); - _vertices.Clear(); - _groups.Clear(); - _materials.Clear(); - } - - public void BakeFrameMeshes() { - if ( Asset && Asset.Atlas && Asset.Data != null && Asset.Data.Frames.Count > 0 ) { - MatPropBlock = new MaterialPropertyBlock(); - MatPropBlock.SetTexture("_MainTex", Asset.Atlas); - for ( var i = 0; i < Asset.Data.Frames.Count; ++i ) { - var frame = Asset.Data.Frames[i]; - BakeFrameMesh(frame); - } - } - FixCurrentMesh(); - } - - void BakeFrameMesh(SwfAnimationFrameData frame) { - var swf_manager = SwfManager.Instance; - for ( var i = 0; i < frame.Instances.Count; ++i ) { - var inst = frame.Instances[i]; - var bitmap = inst != null ? FindBitmap(inst.Bitmap) : null; - if ( bitmap != null ) { - var width = bitmap.RealSize.x / 20.0f; - var height = bitmap.RealSize.y / 20.0f; - - var v0 = new Vector3( 0, 0, 0); - var v1 = new Vector3(width, 0, 0); - var v2 = new Vector3(width, height, 0); - var v3 = new Vector3( 0, height, 0); - - var matrix = - Matrix4x4.Scale(new Vector3( - +1.0f / Asset.Settings.PixelsPerUnit, - -1.0f / Asset.Settings.PixelsPerUnit, - +1.0f / Asset.Settings.PixelsPerUnit)) * inst.Matrix; - - _vertices.Add(matrix.MultiplyPoint3x4(v0)); - _vertices.Add(matrix.MultiplyPoint3x4(v1)); - _vertices.Add(matrix.MultiplyPoint3x4(v2)); - _vertices.Add(matrix.MultiplyPoint3x4(v3)); - - var source_rect = bitmap.SourceRect; - _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin)); - _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin)); - _uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax)); - _uvs.Add(new Vector2(source_rect.xMin, source_rect.yMax)); - - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - _mulcolors.Add(inst.ColorTransform.Mul); - - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - _addcolors.Add(inst.ColorTransform.Add); - - if ( _groups.Count == 0 || - _groups[_groups.Count - 1].Type != inst.Type || - _groups[_groups.Count - 1].ClipDepth != inst.ClipDepth ) - { - _groups.Add(new Group{ - Type = inst.Type, - ClipDepth = inst.ClipDepth, - Triangles = new List(), - Material = null - }); - } - - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 1); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 0); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 3); - _groups[_groups.Count - 1].Triangles.Add(_vertices.Count - 4 + 2); - } - } - - for ( var i = 0; i < _groups.Count; ++i ) { - var group = _groups[i]; - switch ( group.Type ) { - case SwfAnimationInstanceType.Mask: - group.Material = swf_manager.GetIncrMaskMaterial(); - break; - case SwfAnimationInstanceType.Group: - group.Material = swf_manager.GetSimpleMaterial(); - break; - case SwfAnimationInstanceType.Masked: - group.Material = swf_manager.GetMaskedMaterial(group.ClipDepth); - break; - case SwfAnimationInstanceType.MaskReset: - group.Material = swf_manager.GetDecrMaskMaterial(); - break; - } - } - - for ( var i = 0; i < _groups.Count; ++i ) { - var group = _groups[i]; - _materials.Add(group.Material); - } - - var mesh = new Mesh(); - mesh.subMeshCount = _groups.Count; - mesh.SetVertices(_vertices); - for ( var i = 0; i < _groups.Count; ++i ) { - mesh.SetTriangles(_groups[i].Triangles, i); - } - mesh.SetUVs(0, _uvs); - mesh.SetUVs(1, _addcolors); - mesh.SetColors(_mulcolors); - mesh.RecalculateNormals(); - - _frames.Add(new Frame{ - Mesh = mesh, - Materials = _materials.ToArray()}); - ClearTempBakeData(); - } - - SwfAnimationBitmapData FindBitmap(int bitmap_id) { - if ( Asset ) { - for ( var i = 0; i < Asset.Data.Bitmaps.Count; ++i ) { - var bitmap = Asset.Data.Bitmaps[i]; - if ( bitmap.Id == bitmap_id ) { - return bitmap; - } - } - } - return null; - } - - void UpdateFrameTimer() { - if ( Asset ) { - if ( _frames.Count == 0 ) { - BakeFrameMeshes(); - FixCurrentMesh(); - } - _frame_timer += Asset.Data.FrameRate * Time.deltaTime; - while ( _frame_timer > 1.0f ) { - _frame_timer -= 1.0f; - ++_current_frame; - if ( _current_frame > frameCount - 1 ) { - _current_frame = 0; - } - FixCurrentMesh(); - } - } - } - - void FixCurrentMesh() { - if ( _frames.Count > 0 ) { - var frame = _frames[Mathf.Clamp(currentFrame, 0, _frames.Count)]; - _meshFilter.sharedMesh = frame.Mesh; - _meshRenderer.sharedMaterials = frame.Materials; - _meshRenderer.sortingOrder = SortingOrder; - _meshRenderer.sortingLayerName = SortingLayer; - _meshRenderer.SetPropertyBlock(MatPropBlock); - } - } - - // --------------------------------------------------------------------- - // - // Internal - // - // --------------------------------------------------------------------- - - public void InternalUpdate() { - UpdateFrameTimer(); - } - - // --------------------------------------------------------------------- - // - // Messages - // - // --------------------------------------------------------------------- - - void Awake() { - _meshFilter = GetComponent(); - _meshRenderer = GetComponent(); - } - - void OnEnable() { - var swf_manager = SwfManager.Instance; - if ( swf_manager ) { - swf_manager.AddSwfBakedAnimation(this); - } - } - - void OnDisable() { - var swf_manager = SwfManager.Instance; - if ( swf_manager ) { - swf_manager.RemoveSwfBakedAnimation(this); - } - } - } -} \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfBakedAnimation.cs.meta b/Assets/FlashTools/Scripts/SwfBakedAnimation.cs.meta deleted file mode 100644 index 9b2eb6c..0000000 --- a/Assets/FlashTools/Scripts/SwfBakedAnimation.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 950d548c7e22f4e25a47de474b49e86e -timeCreated: 1472040299 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/FlashTools/Scripts/SwfManager.cs b/Assets/FlashTools/Scripts/SwfManager.cs index 20bd77b..e8509ac 100644 --- a/Assets/FlashTools/Scripts/SwfManager.cs +++ b/Assets/FlashTools/Scripts/SwfManager.cs @@ -29,7 +29,7 @@ namespace FlashTools { [SerializeField] [HideInInspector] Material _incrMaskMaterial = null; [SerializeField] [HideInInspector] Material _decrMaskMaterial = null; - HashSet _bakedAnimations = new HashSet(); + HashSet _animations = new HashSet(); // --------------------------------------------------------------------- // @@ -53,12 +53,12 @@ namespace FlashTools { // // --------------------------------------------------------------------- - public void AddSwfBakedAnimation(SwfBakedAnimation animation) { - _bakedAnimations.Add(animation); + public void AddSwfAnimation(SwfAnimation animation) { + _animations.Add(animation); } - public void RemoveSwfBakedAnimation(SwfBakedAnimation animation) { - _bakedAnimations.Remove(animation); + public void RemoveSwfAnimation(SwfAnimation animation) { + _animations.Remove(animation); } // --------------------------------------------------------------------- @@ -142,21 +142,21 @@ namespace FlashTools { } void OnEnable() { - var all_baked_animations = FindObjectsOfType(); - for ( int i = 0, e = all_baked_animations.Length; i < e; ++i ) { - var baked_animation = all_baked_animations[i]; - if ( baked_animation.enabled ) { - _bakedAnimations.Add(baked_animation); + var all_animations = FindObjectsOfType(); + for ( int i = 0, e = all_animations.Length; i < e; ++i ) { + var animation = all_animations[i]; + if ( animation.enabled ) { + _animations.Add(animation); } } } void OnDisable() { - _bakedAnimations.Clear(); + _animations.Clear(); } void Update() { - var iter = _bakedAnimations.GetEnumerator(); + var iter = _animations.GetEnumerator(); while ( iter.MoveNext() ) { iter.Current.InternalUpdate(); }