diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs index ab8dab5..518a510 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfAnimationAssetPostprocessor.cs @@ -56,7 +56,6 @@ namespace FlashTools.Internal { // // ------------------------------------------------------------------------ - static void ConfigureAtlas(string asset_path, SwfAnimationAsset asset) { var atlas_importer = GetBitmapsAtlasImporter(asset_path); var atlas_importer_size = GetSizeFromTextureImporter(atlas_importer); @@ -141,7 +140,7 @@ namespace FlashTools.Internal { // // ------------------------------------------------------------------------ - class BakeGroup { + class BakedGroup { public SwfAnimationInstanceType Type; public int ClipDepth; public List Triangles; @@ -160,20 +159,24 @@ namespace FlashTools.Internal { asset.Frames = baked_frames; } - static SwfAnimationAsset.Frame BakeFrameFromAnimationFrame(SwfAnimationAsset asset, SwfAnimationFrameData frame) { - List _uvs = new List(); - List _mulcolors = new List(); - List _addcolors = new List(); - List _vertices = new List(); - List _groups = new List(); - List _materials = new List(); + static SwfAnimationAsset.Frame BakeFrameFromAnimationFrame( + SwfAnimationAsset asset, SwfAnimationFrameData frame) + { + List baked_uvs = new List(); + List baked_mulcolors = new List(); + List baked_addcolors = new List(); + List baked_vertices = new List(); + List baked_groups = new List(); + List baked_materials = new List(); for ( var i = 0; i < frame.Instances.Count; ++i ) { var inst = frame.Instances[i]; - var bitmap = inst != null ? FindBitmapFromAnimationData(asset.Data, inst.Bitmap) : null; + var bitmap = inst != null + ? FindBitmapFromAnimationData(asset.Data, inst.Bitmap) + : null; if ( bitmap != null ) { - var width = bitmap.RealSize.x / 20.0f; - var height = bitmap.RealSize.y / 20.0f; + var width = bitmap.RealSize.x / 20.0f; //TODO: twips? + var height = bitmap.RealSize.y / 20.0f; //TODO: twips? var v0 = new Vector3( 0, 0, 0); var v1 = new Vector3(width, 0, 0); @@ -186,32 +189,32 @@ namespace FlashTools.Internal { -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)); + baked_vertices.Add(matrix.MultiplyPoint3x4(v0)); + baked_vertices.Add(matrix.MultiplyPoint3x4(v1)); + baked_vertices.Add(matrix.MultiplyPoint3x4(v2)); + baked_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)); + baked_uvs.Add(new Vector2(source_rect.xMin, source_rect.yMin)); + baked_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMin)); + baked_uvs.Add(new Vector2(source_rect.xMax, source_rect.yMax)); + baked_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); + baked_mulcolors.Add(inst.ColorTransform.Mul); + baked_mulcolors.Add(inst.ColorTransform.Mul); + baked_mulcolors.Add(inst.ColorTransform.Mul); + baked_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); + baked_addcolors.Add(inst.ColorTransform.Add); + baked_addcolors.Add(inst.ColorTransform.Add); + baked_addcolors.Add(inst.ColorTransform.Add); + baked_addcolors.Add(inst.ColorTransform.Add); - if ( _groups.Count == 0 || - _groups[_groups.Count - 1].Type != inst.Type || - _groups[_groups.Count - 1].ClipDepth != inst.ClipDepth ) + if ( baked_groups.Count == 0 || + baked_groups[baked_groups.Count - 1].Type != inst.Type || + baked_groups[baked_groups.Count - 1].ClipDepth != inst.ClipDepth ) { - _groups.Add(new BakeGroup{ + baked_groups.Add(new BakedGroup{ Type = inst.Type, ClipDepth = inst.ClipDepth, Triangles = new List(), @@ -219,18 +222,18 @@ namespace FlashTools.Internal { }); } - _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); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 2); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 1); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 0); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 0); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 3); + baked_groups.Last().Triangles.Add(baked_vertices.Count - 4 + 2); } } var default_converter = SwfConverterSettings.GetDefaultConverter(); - for ( var i = 0; i < _groups.Count; ++i ) { - var group = _groups[i]; + for ( var i = 0; i < baked_groups.Count; ++i ) { + var group = baked_groups[i]; switch ( group.Type ) { case SwfAnimationInstanceType.Mask: group.Material = default_converter.GetIncrMaskMaterial(); @@ -244,30 +247,37 @@ namespace FlashTools.Internal { case SwfAnimationInstanceType.MaskReset: group.Material = default_converter.GetDecrMaskMaterial(); break; + default: + throw new UnityException(string.Format( + "SwfAnimationAssetPostprocessor. Incorrect instance type: {0}", + group.Type)); + } + if ( group.Material ) { + baked_materials.Add(group.Material); + } else { + throw new UnityException(string.Format( + "SwfAnimationAssetPostprocessor. Material for baked group ({0}) not found", + group.Type)); } } - 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.subMeshCount = baked_groups.Count; + mesh.SetVertices(baked_vertices); + for ( var i = 0; i < baked_groups.Count; ++i ) { + mesh.SetTriangles(baked_groups[i].Triangles, i); } - mesh.SetUVs(0, _uvs); - mesh.SetUVs(1, _addcolors); - mesh.SetColors(_mulcolors); + mesh.SetUVs(0, baked_uvs); + mesh.SetUVs(1, baked_addcolors); + mesh.SetColors(baked_mulcolors); mesh.RecalculateNormals(); + //TODO: add remove all old meshes AssetDatabase.AddObjectToAsset(mesh, asset); return new SwfAnimationAsset.Frame{ Mesh = mesh, - Materials = _materials.ToArray()}; + Materials = baked_materials.ToArray()}; } static SwfAnimationBitmapData FindBitmapFromAnimationData(SwfAnimationData data, int bitmap_id) {