Skip to content

Commit

Permalink
serialize binary instead of json for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
z3y committed Jun 12, 2024
1 parent 1b83816 commit 9a98619
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions Scripts/XatlasLightmapPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using VRC.SDKBase;

namespace z3y
{
// since static batching already creates a copy of the same mesh for every instance
// there is no reason to use lightmap tiling and offset when we can just set different uv2 for each mesh renderer and pack them very efficiently
// this gets merged by static batching creating no additional cost
[ExecuteInEditMode]
public class XatlasLightmapPacker : MonoBehaviour
public class XatlasLightmapPacker : MonoBehaviour, VRC.SDKBase.IPreprocessCallbackBehaviour
{
public GameObject[] rootObjects; // the renderers here would be on the same lightmap group with no uv adjustments (original uv)
public bool autoUpdateUVs = false;
Expand Down Expand Up @@ -338,6 +339,9 @@ public void GetActiveTransformsWithRenderers(GameObject[] rootObjs, out List<Mes
public Texture2D lightmap;
[Range(1,5)]
public int radius = 2;

public int PreprocessOrder => throw new NotImplementedException();

public void GaussianPrefilter(Renderer[] renderers)
{
if ( lightmap is null)
Expand Down Expand Up @@ -520,8 +524,27 @@ public void Execute(int index)

private void WriteData(LightmapMeshData[] data)
{
string json = JsonHelper.ToJson(data);
File.WriteAllText(GetDataPath(), json);
using (MemoryStream m = new MemoryStream())
{
using (BinaryWriter w = new BinaryWriter(m))
{
w.Write(data.Length);
for (int i = 0; i < data.Length; i++)
{
var uv = data[i].lightmapUV;

w.Write(uv.Length);
for (int j = 0; j < uv.Length; j++)
{
w.Write(uv[j].x);
w.Write(uv[j].y);
}
}
}
var bytes = m.ToArray();

File.WriteAllBytes(GetDataPath(), bytes);
}
}

private void TryReadData(ref LightmapMeshData[] data)
Expand All @@ -532,8 +555,34 @@ private void TryReadData(ref LightmapMeshData[] data)
return;
}

var json = File.ReadAllText(path);
data = JsonHelper.FromJson<LightmapMeshData>(json);
var bytes = File.ReadAllBytes(path);

using (MemoryStream m = new MemoryStream(bytes))
{
using (BinaryReader reader = new BinaryReader(m))
{
var totalLength = reader.ReadInt32();

var result = new LightmapMeshData[totalLength];

for (int i = 0; i < result.Length; i++)
{
var length = reader.ReadInt32();

var uvs = new Vector2[length];

for (int j = 0; j < uvs.Length; j++)
{
uvs[j].x = reader.ReadSingle();
uvs[j].y = reader.ReadSingle();
}

result[i].lightmapUV = uvs;
}

data = result;
}
}
}

private string GetDataPath()
Expand All @@ -550,6 +599,12 @@ private string GetDataPath()

return Path.Combine(libraryPath, idString.targetObjectId + sceneGuid);
}

bool IPreprocessCallbackBehaviour.OnPreprocess()
{
//PackCharts();
return true;
}
}
public class ClearDataOnBuild : IProcessSceneWithReport
{
Expand Down Expand Up @@ -610,7 +665,7 @@ public override void OnInspectorGUI()
}
}

public static class JsonHelper
/*public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Expand All @@ -630,6 +685,6 @@ private class Wrapper<T>
{
public T[] Items;
}
}
}*/
}
#endif

0 comments on commit 9a98619

Please sign in to comment.