From 8108516350ba50560129dca50507beba2305abff Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 13 Sep 2016 03:03:36 +0700 Subject: [PATCH] compress mesh wip --- .../Postprocessors/SwfAssetPostprocessor.cs | 17 +++- .../Scripts/Internal/Editor/SwfEditorUtils.cs | 52 +++++++++++- .../FlashTools/Scripts/Internal/SwfUtils.cs | 85 ++++++++++++++++--- Assets/FlashTools/Scripts/SwfClipAsset.cs | 26 +++--- 4 files changed, 148 insertions(+), 32 deletions(-) diff --git a/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs b/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs index 0b9458e..77fadb6 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs @@ -197,8 +197,8 @@ namespace FlashTools.Internal { SwfAsset asset, SwfFrameData frame) { List baked_uvs = new List(); - List baked_mulcolors = new List(); - List baked_addcolors = 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(); @@ -245,8 +245,17 @@ namespace FlashTools.Internal { baked_uvs.Add(SwfUtils.PackUV(source_rect.xMin, source_rect.yMin)); baked_uvs.Add(SwfUtils.PackUV(source_rect.xMax, source_rect.yMax)); - baked_mulcolors.Add(inst.ColorTrans.Mul); - baked_addcolors.Add(inst.ColorTrans.Add); + uint mul_u0, mul_u1; + SwfUtils.PackColorToUInts( + inst.ColorTrans.Mul, out mul_u0, out mul_u1); + baked_mulcolors.Add(mul_u0); + baked_mulcolors.Add(mul_u1); + + uint add_u0, add_u1; + SwfUtils.PackColorToUInts( + inst.ColorTrans.Add, out add_u0, out add_u1); + baked_addcolors.Add(add_u0); + baked_addcolors.Add(add_u1); if ( baked_groups.Count == 0 || baked_groups[baked_groups.Count - 1].Type != inst.Type || diff --git a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs index c923bea..7894237 100644 --- a/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs +++ b/Assets/FlashTools/Scripts/Internal/Editor/SwfEditorUtils.cs @@ -109,6 +109,50 @@ namespace FlashTools.Internal { 0.0001f); } + [Test] + public static void PackFloatColorToUShortTests() { + float v0 = -5.678f; + Assert.AreEqual( + v0, + SwfUtils.UnpackFloatColorFromUShort(SwfUtils.PackFloatColorToUShort(v0)), + 0.002f); + + float v1 = 60.678f; + Assert.AreEqual( + v1, + SwfUtils.UnpackFloatColorFromUShort(SwfUtils.PackFloatColorToUShort(v1)), + 0.002f); + + float v2 = 0.678f; + Assert.AreEqual( + v2, + SwfUtils.UnpackFloatColorFromUShort(SwfUtils.PackFloatColorToUShort(v2)), + 0.002f); + } + + [Test] + public static void PackColorToUIntsTests() { + var v0 = new Color(0.01f, 0.02f, 0.33f, 1.0f); + uint u0, u1; + SwfUtils.PackColorToUInts(v0, out u0, out u1); + Color c0; + SwfUtils.UnpackColorFromUInts(u0, u1, out c0); + Assert.AreEqual(v0.r, c0.r, SwfUtils.ColorPrecision); + Assert.AreEqual(v0.g, c0.g, SwfUtils.ColorPrecision); + Assert.AreEqual(v0.b, c0.b, SwfUtils.ColorPrecision); + Assert.AreEqual(v0.a, c0.a, SwfUtils.ColorPrecision); + + var v1 = new Vector4(0.01f, 0.02f, 0.33f, 1.0f); + uint u2, u3; + SwfUtils.PackColorToUInts(v1, out u2, out u3); + Vector4 c1; + SwfUtils.UnpackColorFromUInts(u2, u3, out c1); + Assert.AreEqual(v1.x, c1.x, SwfUtils.ColorPrecision); + Assert.AreEqual(v1.y, c1.y, SwfUtils.ColorPrecision); + Assert.AreEqual(v1.z, c1.z, SwfUtils.ColorPrecision); + Assert.AreEqual(v1.w, c1.w, SwfUtils.ColorPrecision); + } + [Test] public static void PackUShortsToUIntTests() { ushort v0 = 11, v1 = 99; @@ -132,21 +176,21 @@ namespace FlashTools.Internal { Assert.AreEqual( v0.x, SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v0)).x, - 0.05f); + SwfUtils.CoordPrecision); Assert.AreEqual( v0.y, SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v0)).y, - 0.05f); + SwfUtils.CoordPrecision); var v1 = new Vector2(1234.14f, 3200.23f); Assert.AreEqual( v1.x, SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v1)).x, - 0.05f); + SwfUtils.CoordPrecision); Assert.AreEqual( v1.y, SwfUtils.UnpackCoordsFromUInt(SwfUtils.PackCoordsToUInt(v1)).y, - 0.05f); + SwfUtils.CoordPrecision); } } } \ No newline at end of file diff --git a/Assets/FlashTools/Scripts/Internal/SwfUtils.cs b/Assets/FlashTools/Scripts/Internal/SwfUtils.cs index e76b905..d355eac 100644 --- a/Assets/FlashTools/Scripts/Internal/SwfUtils.cs +++ b/Assets/FlashTools/Scripts/Internal/SwfUtils.cs @@ -3,24 +3,82 @@ 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 const float CoordPrecision = 0.05f; + public const float ColorPrecision = 0.002f; public static uint PackUV(float u, float v) { - var uu = (uint)(Mathf.Clamp01(u) * Pow2_14); - var vv = (uint)(Mathf.Clamp01(v) * Pow2_14); + var uu = (uint)(Mathf.Clamp01(u) * ushort.MaxValue); + var vv = (uint)(Mathf.Clamp01(v) * ushort.MaxValue); return (uu << 16) + vv; } public static Vector2 UnpackUV(uint v) { var uu = ((v >> 16) & 0xFFFF); var vv = (v & 0xFFFF); - return new Vector2((float)uu / Pow2_14, (float)vv / Pow2_14); + return new Vector2( + (float)uu / ushort.MaxValue, + (float)vv / ushort.MaxValue); + } + + // + // + // + + public static ushort PackFloatColorToUShort(float v) { + return (ushort)Mathf.Clamp( + v * (1.0f / ColorPrecision), + short.MinValue, + short.MaxValue); + } + + public static float UnpackFloatColorFromUShort(ushort v) { + return (short)v / (1.0f / ColorPrecision); + } + + // + // + // + + public static void PackColorToUInts(Color v, out uint u0, out uint u1) { + PackColorToUInts(v.r, v.g, v.b, v.a, out u0, out u1); + } + + public static void PackColorToUInts(Vector4 v, out uint u0, out uint u1) { + PackColorToUInts(v.x, v.y, v.z, v.w, out u0, out u1); + } + + public static void PackColorToUInts( + float v0, float v1, float v2, float v3, + out uint u0, out uint u1) + { + var s0 = PackFloatColorToUShort(v0); + var s1 = PackFloatColorToUShort(v1); + var s2 = PackFloatColorToUShort(v2); + var s3 = PackFloatColorToUShort(v3); + u0 = PackUShortsToUInt(s0, s1); + u1 = PackUShortsToUInt(s2, s3); + } + + public static void UnpackColorFromUInts(uint u0, uint u1, out Color v) { + ushort s0, s1, s2, s3; + UnpackUShortsFromUInt(u0, out s0, out s1); + UnpackUShortsFromUInt(u1, out s2, out s3); + v = new Color( + UnpackFloatColorFromUShort(s0), + UnpackFloatColorFromUShort(s1), + UnpackFloatColorFromUShort(s2), + UnpackFloatColorFromUShort(s3)); + } + + public static void UnpackColorFromUInts(uint u0, uint u1, out Vector4 v) { + ushort s0, s1, s2, s3; + UnpackUShortsFromUInt(u0, out s0, out s1); + UnpackUShortsFromUInt(u1, out s2, out s3); + v = new Vector4( + UnpackFloatColorFromUShort(s0), + UnpackFloatColorFromUShort(s1), + UnpackFloatColorFromUShort(s2), + UnpackFloatColorFromUShort(s3)); } // @@ -45,11 +103,14 @@ namespace FlashTools.Internal { // public static ushort PackFloatCoordToUShort(float v) { - return (ushort)Mathf.Clamp(v * 20.0f, 0.0f, Pow2_16 - 1); + return (ushort)Mathf.Clamp( + v * (1.0f / CoordPrecision), + 0, + ushort.MaxValue); } public static float UnpackFloatCoordFromUShort(ushort v) { - return (float)(v / 20.0f); + return v / (1.0f / CoordPrecision); } // diff --git a/Assets/FlashTools/Scripts/SwfClipAsset.cs b/Assets/FlashTools/Scripts/SwfClipAsset.cs index c8063cd..d061b69 100644 --- a/Assets/FlashTools/Scripts/SwfClipAsset.cs +++ b/Assets/FlashTools/Scripts/SwfClipAsset.cs @@ -53,13 +53,14 @@ namespace FlashTools { } public static List AddColors = new List(); - public static void FillAddColors(List colors) { + public static void FillAddColors(List colors) { AddColors.Clear(); - if ( AddColors.Capacity < colors.Count * 4 ) { - AddColors.Capacity = colors.Count * 4 * 2; + if ( AddColors.Capacity < colors.Count * 2 ) { + AddColors.Capacity = colors.Count * 2 * 2; } - for ( int i = 0, e = colors.Count; i < e; ++i ) { - var color = colors[i]; + for ( int i = 0, e = colors.Count; i < e; i += 2 ) { + Vector4 color; + SwfUtils.UnpackColorFromUInts(colors[i+0], colors[i+1], out color); AddColors.Add(color); AddColors.Add(color); AddColors.Add(color); @@ -68,13 +69,14 @@ namespace FlashTools { } public static List MulColors = new List(); - public static void FillMulColors(List colors) { + public static void FillMulColors(List colors) { MulColors.Clear(); - if ( MulColors.Capacity < colors.Count * 4 ) { - MulColors.Capacity = colors.Count * 4 * 2; + if ( MulColors.Capacity < colors.Count * 2 ) { + MulColors.Capacity = colors.Count * 2 * 2; } - for ( int i = 0, e = colors.Count; i < e; ++i ) { - var color = colors[i]; + for ( int i = 0, e = colors.Count; i < e; i += 2 ) { + Color color; + SwfUtils.UnpackColorFromUInts(colors[i+0], colors[i+1], out color); MulColors.Add(color); MulColors.Add(color); MulColors.Add(color); @@ -97,8 +99,8 @@ namespace FlashTools { 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(); + public List AddColors = new List(); + public List MulColors = new List(); public void FillMesh(Mesh mesh) { if ( SubMeshes.Count > 0 ) {