mirror of
https://github.com/BlackMATov/unity-flash-tools.git
synced 2025-12-16 14:11:19 +07:00
compress mesh wip
This commit is contained in:
@@ -199,6 +199,7 @@ namespace FlashTools.Internal {
|
||||
List<uint> baked_uvs = new List<uint>();
|
||||
List<Color> baked_mulcolors = new List<Color>();
|
||||
List<Vector4> baked_addcolors = new List<Vector4>();
|
||||
Vector2 baked_mesh_min = new Vector2(float.MaxValue, float.MaxValue);
|
||||
List<Vector2> baked_vertices = new List<Vector2>();
|
||||
List<BakedGroup> baked_groups = new List<BakedGroup>();
|
||||
List<Material> baked_materials = new List<Material>();
|
||||
@@ -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};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,13 +22,17 @@ namespace FlashTools {
|
||||
}
|
||||
|
||||
public static List<Vector3> Vertices = new List<Vector3>();
|
||||
public static void FillVertices(List<Vector2> vertices) {
|
||||
public static void FillVertices(
|
||||
Vector2 mesh_min, float mesh_scale, List<uint> 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<SubMeshData> SubMeshes = new List<SubMeshData>();
|
||||
public List<Vector2> Vertices = new List<Vector2>();
|
||||
public Vector2 MeshMin = Vector2.zero;
|
||||
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>();
|
||||
@@ -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 ) {
|
||||
|
||||
Reference in New Issue
Block a user