From c5270a01a7e6281e165b360723a25a463016bb8b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 13 Sep 2016 00:25:32 +0700 Subject: [PATCH] compress mesh wip --- .../Postprocessors/SwfAssetPostprocessor.cs | 44 ++++++++--- .../Scripts/Internal/Editor/SwfEditorUtils.cs | 79 +++++++++++++++++++ .../FlashTools/Scripts/Internal/SwfUtils.cs | 53 +++++++++++++ Assets/FlashTools/Scripts/SwfClipAsset.cs | 22 ++++-- 4 files changed, 177 insertions(+), 21 deletions(-) diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs index 2b0bb07..0b9458e 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs @@ -199,6 +199,7 @@ namespace FlashTools.Internal { List baked_uvs = new List(); List baked_mulcolors = new List(); List baked_addcolors = new List(); + Vector2 baked_mesh_min = new Vector2(float.MaxValue, float.MaxValue); List baked_vertices = new List(); List baked_groups = new List(); List baked_materials = new List(); @@ -211,22 +212,34 @@ namespace FlashTools.Internal { 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 v0 = new Vector2( 0, 0); + var v1 = new Vector2(width, 0); + var v2 = new Vector2(width, height); + var v3 = new Vector2( 0, height); var matrix = - Matrix4x4.Scale(new Vector3( - +1.0f / asset.Settings.PixelsPerUnit, - -1.0f / asset.Settings.PixelsPerUnit, - +1.0f / asset.Settings.PixelsPerUnit)) * + Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f)) * inst.Matrix.ToUnityMatrix(); - 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 p0 = matrix.MultiplyPoint3x4(v0); + var p1 = matrix.MultiplyPoint3x4(v1); + var p2 = matrix.MultiplyPoint3x4(v2); + var p3 = matrix.MultiplyPoint3x4(v3); + + baked_mesh_min.x = Mathf.Min(baked_mesh_min.x, p0.x); + baked_mesh_min.x = Mathf.Min(baked_mesh_min.x, p1.x); + baked_mesh_min.x = Mathf.Min(baked_mesh_min.x, p2.x); + baked_mesh_min.x = Mathf.Min(baked_mesh_min.x, p3.x); + + baked_mesh_min.y = Mathf.Min(baked_mesh_min.y, p0.y); + baked_mesh_min.y = Mathf.Min(baked_mesh_min.y, p1.y); + baked_mesh_min.y = Mathf.Min(baked_mesh_min.y, p2.y); + baked_mesh_min.y = Mathf.Min(baked_mesh_min.y, p3.y); + + baked_vertices.Add(p0); + baked_vertices.Add(p1); + baked_vertices.Add(p2); + baked_vertices.Add(p3); var source_rect = bitmap.SourceRect; baked_uvs.Add(SwfUtils.PackUV(source_rect.xMin, source_rect.yMin)); @@ -288,7 +301,12 @@ namespace FlashTools.Internal { StartVertex = p.StartVertex, TriangleCount = p.TriangleCount}) .ToList(), - Vertices = baked_vertices, + MeshMin = baked_mesh_min, + MeshScale = asset.Settings.PixelsPerUnit, + Vertices = baked_vertices + .Select(p => p - baked_mesh_min) + .Select(p => SwfUtils.PackCoordsToUInt(p)) + .ToList(), UVs = baked_uvs, AddColors = baked_addcolors, MulColors = baked_mulcolors}; diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs index 0577657..c923bea 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs @@ -1,6 +1,8 @@ using UnityEngine; using UnityEditor; +using NUnit.Framework; + using System.IO; using System.Collections.Generic; @@ -69,5 +71,82 @@ namespace FlashTools.Internal { var asset_path = AssetDatabase.GetAssetPath(asset); return Path.ChangeExtension(asset_path, "._Atlas_.png"); } + + // + // SwfUtils tests + // + + [Test] + public static void PackUVTests() { + float v0 = 0.99f, v1 = 0.11f; + Assert.AreEqual( + v0, + SwfUtils.UnpackUV(SwfUtils.PackUV(v0, v1)).x, + 0.0001f); + Assert.AreEqual( + v1, + SwfUtils.UnpackUV(SwfUtils.PackUV(v0, v1)).y, + 0.0001f); + + float v2 = 0.09f, v3 = 0.01f; + Assert.AreEqual( + v2, + SwfUtils.UnpackUV(SwfUtils.PackUV(v2, v3)).x, + 0.0001f); + Assert.AreEqual( + v3, + SwfUtils.UnpackUV(SwfUtils.PackUV(v2, v3)).y, + 0.0001f); + + float v4 = 1.0f, v5 = 0.0f; + Assert.AreEqual( + v4, + SwfUtils.UnpackUV(SwfUtils.PackUV(v4, v5)).x, + 0.0001f); + Assert.AreEqual( + v5, + SwfUtils.UnpackUV(SwfUtils.PackUV(v4, v5)).y, + 0.0001f); + } + + [Test] + public static void PackUShortsToUIntTests() { + ushort v0 = 11, v1 = 99; + ushort o0, o1; + SwfUtils.UnpackUShortsFromUInt( + SwfUtils.PackUShortsToUInt(v0, v1), out o0, out o1); + Assert.AreEqual(v0, o0); + Assert.AreEqual(v1, o1); + + ushort v2 = 16789, v3 = 31234; + ushort o2, o3; + SwfUtils.UnpackUShortsFromUInt( + SwfUtils.PackUShortsToUInt(v2, v3), out o2, out o3); + Assert.AreEqual(v2, o2); + Assert.AreEqual(v3, o3); + } + + [Test] + public static void PackCoordsToUIntTests() { + var v0 = new Vector2(1.14f, 2.23f); + Assert.AreEqual( + v0.x, + SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v0)).x, + 0.05f); + Assert.AreEqual( + v0.y, + SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v0)).y, + 0.05f); + + var v1 = new Vector2(1234.14f, 3200.23f); + Assert.AreEqual( + v1.x, + SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v1)).x, + 0.05f); + Assert.AreEqual( + v1.y, + SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v1)).y, + 0.05f); + } } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/SwfUtils.cs b/Assets/FlashTools/Scripts/Internal/SwfUtils.cs index 0429b4d..e76b905 100644 --- a/Assets/FlashTools/Scripts/Internal/SwfUtils.cs +++ b/Assets/FlashTools/Scripts/Internal/SwfUtils.cs @@ -4,6 +4,12 @@ namespace FlashTools.Internal { public static class SwfUtils { const uint Pow2_14 = (1 << 14); // 16 384 + const uint Pow2_15 = (1 << 15); // 32 768 + const uint Pow2_16 = (1 << 16); // 65 536 + + // + // + // public static uint PackUV(float u, float v) { var uu = (uint)(Mathf.Clamp01(u) * Pow2_14); @@ -16,5 +22,52 @@ namespace FlashTools.Internal { var vv = (v & 0xFFFF); return new Vector2((float)uu / Pow2_14, (float)vv / Pow2_14); } + + // + // + // + + public static uint PackUShortsToUInt(ushort x, ushort y) { + var xx = (uint)x; + var yy = (uint)y; + return (xx << 16) + yy; + } + + public static void UnpackUShortsFromUInt(uint v, out ushort x, out ushort y) { + var xx = ((v >> 16) & 0xFFFF); + var yy = (v & 0xFFFF); + x = (ushort)xx; + y = (ushort)yy; + } + + // + // + // + + public static ushort PackFloatCoordToUShort(float v) { + return (ushort)Mathf.Clamp(v * 20.0f, 0.0f, Pow2_16 - 1); + } + + public static float UnpackFloatCoordFromUShort(ushort v) { + return (float)(v / 20.0f); + } + + // + // + // + + public static uint PackCoordsToUInt(Vector2 v) { + return PackUShortsToUInt( + PackFloatCoordToUShort(v.x), + PackFloatCoordToUShort(v.y)); + } + + public static Vector2 UnpackCoordsFromUInt(uint v) { + ushort sx, sy; + UnpackUShortsFromUInt(v, out sx, out sy); + return new Vector2( + SwfUtils.UnpackFloatCoordFromUShort(sx), + SwfUtils.UnpackFloatCoordFromUShort(sy)); + } } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/SwfClipAsset.cs b/Assets/FlashTools/Scripts/SwfClipAsset.cs index 1ade2f7..c8063cd 100644 --- a/Assets/FlashTools/Scripts/SwfClipAsset.cs +++ b/Assets/FlashTools/Scripts/SwfClipAsset.cs @@ -22,13 +22,17 @@ namespace FlashTools { } public static List Vertices = new List(); - public static void FillVertices(List vertices) { + public static void FillVertices( + Vector2 mesh_min, float mesh_scale, List vertices) + { Vertices.Clear(); if ( Vertices.Capacity < vertices.Count ) { Vertices.Capacity = vertices.Count * 2; } - for ( var i = 0; i < vertices.Count; ++i ) { - Vertices.Add(vertices[i]); + for ( int i = 0, e = vertices.Count; i < e; ++i ) { + Vertices.Add( + (mesh_min + + SwfUtils.UnpackCoordsFromUInt(vertices[i])) / mesh_scale); } } @@ -38,7 +42,7 @@ namespace FlashTools { if ( UVs.Capacity < uvs.Count * 2 ) { UVs.Capacity = uvs.Count * 2 * 2; } - for ( var i = 0; i < uvs.Count; i += 2 ) { + for ( int i = 0, e = uvs.Count; i < e; i += 2 ) { var min = SwfUtils.UnpackUV(uvs[i+0]); var max = SwfUtils.UnpackUV(uvs[i+1]); UVs.Add(new Vector2(min.x, min.y)); @@ -54,7 +58,7 @@ namespace FlashTools { if ( AddColors.Capacity < colors.Count * 4 ) { AddColors.Capacity = colors.Count * 4 * 2; } - for ( var i = 0; i < colors.Count; ++i ) { + for ( int i = 0, e = colors.Count; i < e; ++i ) { var color = colors[i]; AddColors.Add(color); AddColors.Add(color); @@ -69,7 +73,7 @@ namespace FlashTools { if ( MulColors.Capacity < colors.Count * 4 ) { MulColors.Capacity = colors.Count * 4 * 2; } - for ( var i = 0; i < colors.Count; ++i ) { + for ( int i = 0, e = colors.Count; i < e; ++i ) { var color = colors[i]; MulColors.Add(color); MulColors.Add(color); @@ -89,7 +93,9 @@ namespace FlashTools { [System.Serializable] public class MeshData { public List SubMeshes = new List(); - public List Vertices = new List(); + public Vector2 MeshMin = Vector2.zero; + public float MeshScale = 1.0f; + public List Vertices = new List(); public List UVs = new List(); public List AddColors = new List(); public List MulColors = new List(); @@ -98,7 +104,7 @@ namespace FlashTools { if ( SubMeshes.Count > 0 ) { mesh.subMeshCount = SubMeshes.Count; - SwfClipAssetCache.FillVertices(Vertices); + SwfClipAssetCache.FillVertices(MeshMin, MeshScale, Vertices); mesh.SetVertices(SwfClipAssetCache.Vertices); for ( var i = 0; i < SubMeshes.Count; ++i ) {