compress mesh data wip

This commit is contained in:
2016-09-12 20:29:54 +07:00
parent 876859a0e1
commit c424a4d507
6 changed files with 150 additions and 35 deletions

View File

@@ -49,6 +49,7 @@
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfAttributes.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfList.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfSettings.cs" />
<Compile Include="Assets\FlashTools\Scripts\Internal\SwfUtils.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfAsset.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfClip.cs" />
<Compile Include="Assets\FlashTools\Scripts\SwfClipAsset.cs" />

View File

@@ -6,6 +6,8 @@ using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using FlashTools.Internal;
namespace FlashTools.Internal {
public class SwfAssetPostprocessor : AssetPostprocessor {
static void OnPostprocessAllAssets(
@@ -186,17 +188,18 @@ namespace FlashTools.Internal {
class BakedGroup {
public SwfInstanceData.Types Type;
public int ClipDepth;
public List<int> Triangles;
public int StartVertex;
public int TriangleCount;
public Material Material;
}
static SwfClipAsset.Frame BakeClipFrame(
SwfAsset asset, SwfFrameData frame)
{
List<Vector2> baked_uvs = new List<Vector2>();
List<uint> baked_uvs = new List<uint>();
List<Color> baked_mulcolors = new List<Color>();
List<Vector4> baked_addcolors = new List<Vector4>();
List<Vector3> baked_vertices = new List<Vector3>();
List<Vector2> baked_vertices = new List<Vector2>();
List<BakedGroup> baked_groups = new List<BakedGroup>();
List<Material> baked_materials = new List<Material>();
@@ -226,19 +229,10 @@ namespace FlashTools.Internal {
baked_vertices.Add(matrix.MultiplyPoint3x4(v3));
var source_rect = bitmap.SourceRect;
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));
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_mulcolors.Add(inst.ColorTrans.Mul);
baked_mulcolors.Add(inst.ColorTrans.Mul);
baked_mulcolors.Add(inst.ColorTrans.Mul);
baked_addcolors.Add(inst.ColorTrans.Add);
baked_addcolors.Add(inst.ColorTrans.Add);
baked_addcolors.Add(inst.ColorTrans.Add);
baked_addcolors.Add(inst.ColorTrans.Add);
if ( baked_groups.Count == 0 ||
@@ -246,19 +240,15 @@ namespace FlashTools.Internal {
baked_groups[baked_groups.Count - 1].ClipDepth != inst.ClipDepth )
{
baked_groups.Add(new BakedGroup{
Type = inst.Type,
ClipDepth = inst.ClipDepth,
Triangles = new List<int>(),
Material = null
Type = inst.Type,
ClipDepth = inst.ClipDepth,
StartVertex = baked_vertices.Count - 4,
TriangleCount = 0,
Material = null
});
}
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);
baked_groups.Last().TriangleCount += 6;
}
}
@@ -294,7 +284,9 @@ namespace FlashTools.Internal {
var mesh_data = new SwfClipAsset.MeshData{
SubMeshes = baked_groups
.Select(p => new SwfClipAsset.SubMeshData{Triangles = p.Triangles})
.Select(p => new SwfClipAsset.SubMeshData{
StartVertex = p.StartVertex,
TriangleCount = p.TriangleCount})
.ToList(),
Vertices = baked_vertices,
UVs = baked_uvs,

View File

@@ -0,0 +1,20 @@
using UnityEngine;
namespace FlashTools.Internal {
public static class SwfUtils {
const uint Pow2_14 = (1 << 14); // 16 384
public static uint PackUV(float u, float v) {
var uu = (uint)(Mathf.Clamp01(u) * Pow2_14);
var vv = (uint)(Mathf.Clamp01(v) * Pow2_14);
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);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7403812bd90474c01b689738c58fd8dc
timeCreated: 1473684950
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -3,30 +3,118 @@ using FlashTools.Internal;
using System.Collections.Generic;
namespace FlashTools {
public static class SwfClipAssetCache {
public static List<int> Triangles = new List<int>();
public static void FillTriangles(int start_vertex, int triangle_count) {
Triangles.Clear();
if ( Triangles.Capacity < triangle_count ) {
Triangles.Capacity = triangle_count * 2;
}
for ( var i = 0; i < triangle_count; i += 6 ) {
Triangles.Add(start_vertex + 2);
Triangles.Add(start_vertex + 1);
Triangles.Add(start_vertex + 0);
Triangles.Add(start_vertex + 0);
Triangles.Add(start_vertex + 3);
Triangles.Add(start_vertex + 2);
start_vertex += 4;
}
}
public static List<Vector3> Vertices = new List<Vector3>();
public static void FillVertices(List<Vector2> 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]);
}
}
public static List<Vector2> UVs = new List<Vector2>();
public static void FillUVs(List<uint> uvs) {
UVs.Clear();
if ( UVs.Capacity < uvs.Count * 2 ) {
UVs.Capacity = uvs.Count * 2 * 2;
}
for ( var i = 0; i < uvs.Count; i += 2 ) {
var min = SwfUtils.UnpackUV(uvs[i+0]);
var max = SwfUtils.UnpackUV(uvs[i+1]);
UVs.Add(new Vector2(min.x, min.y));
UVs.Add(new Vector2(max.x, min.y));
UVs.Add(new Vector2(max.x, max.y));
UVs.Add(new Vector2(min.x, max.y));
}
}
public static List<Vector4> AddColors = new List<Vector4>();
public static void FillAddColors(List<Vector4> colors) {
AddColors.Clear();
if ( AddColors.Capacity < colors.Count * 4 ) {
AddColors.Capacity = colors.Count * 4 * 2;
}
for ( var i = 0; i < colors.Count; ++i ) {
var color = colors[i];
AddColors.Add(color);
AddColors.Add(color);
AddColors.Add(color);
AddColors.Add(color);
}
}
public static List<Color> MulColors = new List<Color>();
public static void FillMulColors(List<Color> colors) {
MulColors.Clear();
if ( MulColors.Capacity < colors.Count * 4 ) {
MulColors.Capacity = colors.Count * 4 * 2;
}
for ( var i = 0; i < colors.Count; ++i ) {
var color = colors[i];
MulColors.Add(color);
MulColors.Add(color);
MulColors.Add(color);
MulColors.Add(color);
}
}
}
public class SwfClipAsset : ScriptableObject {
[System.Serializable]
public class SubMeshData {
public List<int> Triangles = new List<int>();
public int StartVertex;
public int TriangleCount;
}
[System.Serializable]
public class MeshData {
public List<SubMeshData> SubMeshes = new List<SubMeshData>();
public List<Vector3> Vertices = new List<Vector3>();
public List<Vector2> UVs = new List<Vector2>();
public List<Vector2> Vertices = new List<Vector2>();
public List<uint> UVs = new List<uint>();
public List<Vector4> AddColors = new List<Vector4>();
public List<Color> MulColors = new List<Color>();
public void FillMesh(Mesh mesh) {
if ( SubMeshes.Count > 0 ) {
mesh.subMeshCount = SubMeshes.Count;
mesh.SetVertices(Vertices);
SwfClipAssetCache.FillVertices(Vertices);
mesh.SetVertices(SwfClipAssetCache.Vertices);
for ( var i = 0; i < SubMeshes.Count; ++i ) {
mesh.SetTriangles(SubMeshes[i].Triangles, i);
SwfClipAssetCache.FillTriangles(
SubMeshes[i].StartVertex, SubMeshes[i].TriangleCount);
mesh.SetTriangles(SwfClipAssetCache.Triangles, i);
}
mesh.SetUVs(0, UVs);
mesh.SetUVs(1, AddColors);
mesh.SetColors(MulColors);
SwfClipAssetCache.FillUVs(UVs);
mesh.SetUVs(0, SwfClipAssetCache.UVs);
SwfClipAssetCache.FillAddColors(AddColors);
mesh.SetUVs(1, SwfClipAssetCache.AddColors);
SwfClipAssetCache.FillMulColors(MulColors);
mesh.SetColors(SwfClipAssetCache.MulColors);
}
}
}

View File

@@ -1,8 +1,10 @@
<Properties StartupItem="Assembly-CSharp.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/FlashTools/Scripts/Internal/SwfUtils.cs">
<Files>
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Editors/SwfClipAssetPreview.cs" Line="1" Column="1" />
<File FileName="Assets/FlashTools/Scripts/Internal/Editor/Postprocessors/SwfAssetPostprocessor.cs" Line="232" Column="32" />
<File FileName="Assets/FlashTools/Scripts/SwfClipAsset.cs" Line="42" Column="40" />
<File FileName="Assets/FlashTools/Scripts/Internal/SwfUtils.cs" Line="13" Column="2" NotebookId="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>