compress mesh wip

This commit is contained in:
2016-09-13 03:03:36 +07:00
parent c5270a01a7
commit 8108516350
4 changed files with 148 additions and 32 deletions
@@ -197,8 +197,8 @@ namespace FlashTools.Internal {
SwfAsset asset, SwfFrameData frame)
{
List<uint> baked_uvs = new List<uint>();
List<Color> baked_mulcolors = new List<Color>();
List<Vector4> baked_addcolors = new List<Vector4>();
List<uint> baked_mulcolors = new List<uint>();
List<uint> baked_addcolors = new List<uint>();
Vector2 baked_mesh_min = new Vector2(float.MaxValue, float.MaxValue);
List<Vector2> baked_vertices = new List<Vector2>();
List<BakedGroup> baked_groups = new List<BakedGroup>();
@@ -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 ||
@@ -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);
}
}
}
+73 -12
View File
@@ -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);
}
//
+14 -12
View File
@@ -53,13 +53,14 @@ namespace FlashTools {
}
public static List<Vector4> AddColors = new List<Vector4>();
public static void FillAddColors(List<Vector4> colors) {
public static void FillAddColors(List<uint> 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<Color> MulColors = new List<Color>();
public static void FillMulColors(List<Color> colors) {
public static void FillMulColors(List<uint> 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<uint> Vertices = new List<uint>();
public List<uint> UVs = new List<uint>();
public List<Vector4> AddColors = new List<Vector4>();
public List<Color> MulColors = new List<Color>();
public List<uint> AddColors = new List<uint>();
public List<uint> MulColors = new List<uint>();
public void FillMesh(Mesh mesh) {
if ( SubMeshes.Count > 0 ) {