void CreateCacheImage() { MegaCacheOBJ mod = (MegaCacheOBJ)target; if (mod.cacheimage) { mod.DestroyImage(); } MegaCacheImage img = CreateInstance <MegaCacheImage>(); img.maxv = 0; img.maxsm = 0; for (int i = 0; i < mod.meshes.Count; i++) { if (mod.meshes[i].vertexCount > img.maxv) { img.maxv = mod.meshes[i].vertexCount; } int sub = mod.meshes[i].subMeshCount; if (sub > img.maxsm) { img.maxsm = sub; } } img.smfc = new int[img.maxsm]; for (int i = 0; i < mod.meshes.Count; i++) { for (int s = 0; s < mod.meshes[i].subMeshCount; s++) { int len = mod.meshes[i].GetTriangles(s).Length; if (len > img.smfc[s]) { img.smfc[s] = len; } } } for (int i = 0; i < mod.meshes.Count; i++) { Mesh ms = mod.meshes[i]; MegaCacheImageFrame frame = MegaCacheImage.CreateImageFrame(ms); img.frames.Add(frame); } img.CalcMemory(); mod.cacheimage = img; }
public void CreateImageFromCacheFile() { if (br == null) { OpenCache(cachefile); } if (br != null) { if (cacheimage) { DestroyImage(); } MegaCacheImage img = (MegaCacheImage)ScriptableObject.CreateInstance <MegaCacheImage>(); img.maxv = maxv; img.maxsm = maxsm; img.smfc = new int[maxsm]; for (int i = 0; i < maxsm; i++) { img.smfc[i] = subs[i].max; } Mesh mesh = new Mesh(); for (int i = 0; i < framecount; i++) { MakeMeshFromFrame(i, mesh); MegaCacheImageFrame frame = MegaCacheImage.CreateImageFrame(mesh); img.frames.Add(frame); } cacheimage = img; ChangeSource(MegaCacheData.Image); if (Application.isEditor) { DestroyImmediate(mesh); } else { Destroy(mesh); } mesh = null; GC.Collect(); } }
public int CalcMemory() { int mem = 0; for (int i = 0; i < frames.Count; i++) { MegaCacheImageFrame fr = frames[i]; mem += fr.verts.Length; mem += fr.norms.Length; mem += fr.tangents.Length; mem += fr.uvs.Length; mem += fr.tris.Length; mem += fr.suboffs.Length * 2; mem += fr.sublen.Length * 2; } memoryuse = mem; return(mem); }
static public MegaCacheImageFrame CreateImageFrame(Mesh ms) { MegaCacheImageFrame frame = new MegaCacheImageFrame(); Vector3[] verts = ms.vertices; Vector3[] norms = ms.normals; Vector2[] uvs = ms.uv; Vector4[] tangents = ms.tangents; frame.vc = verts.Length; frame.nc = norms.Length; frame.tc = tangents.Length; frame.uvc = uvs.Length; frame.bmin = ms.bounds.min; //Vector3 bmax = ms.bounds.max; Vector3 msize = ms.bounds.size; frame.bsize = ms.bounds.size * (1.0f / 65535.0f); Bounds uvb = MegaCacheUtils.GetBounds(uvs); frame.uvmin = uvb.min; frame.uvsize = uvb.size * (1.0f / 255.0f); frame.verts = new byte[frame.vc * 6]; frame.norms = new byte[frame.nc * 3]; frame.tangents = new byte[frame.tc * 4]; frame.uvs = new byte[frame.vc * 2]; frame.tris = new byte[ms.triangles.Length * 2]; int ix = 0; byte[] by; for (int v = 0; v < verts.Length; v++) { Vector3 pos = verts[v]; short val = (short)(((pos.x - frame.bmin.x) / msize.x) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; val = (short)(((pos.y - frame.bmin.y) / msize.y) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; val = (short)(((pos.z - frame.bmin.z) / msize.z) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; } ix = 0; for (int v = 0; v < norms.Length; v++) { Vector3 pos = norms[v]; frame.norms[ix++] = (byte)((pos.x + 1.0f) * 127.0f); frame.norms[ix++] = (byte)((pos.y + 1.0f) * 127.0f); frame.norms[ix++] = (byte)((pos.z + 1.0f) * 127.0f); } ix = 0; for (int v = 0; v < tangents.Length; v++) { Vector4 pos = tangents[v]; frame.tangents[ix++] = (byte)((pos.x + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.y + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.z + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.w + 1.0f) * 127.0f); } ix = 0; for (int v = 0; v < uvs.Length; v++) { Vector2 pos = uvs[v]; frame.uvs[ix++] = (byte)(((pos.x - uvb.min.x) / uvb.size.x) * 255.0f); frame.uvs[ix++] = (byte)(((pos.y - uvb.min.y) / uvb.size.y) * 255.0f); } frame.subcount = ms.subMeshCount; frame.suboffs = new int[frame.subcount]; frame.sublen = new int[frame.subcount]; ix = 0; for (int s = 0; s < frame.subcount; s++) { int[] tris = ms.GetTriangles(s); frame.suboffs[s] = ix; frame.sublen[s] = tris.Length; for (int t = 0; t < tris.Length; t++) { short val = (short)tris[t]; by = System.BitConverter.GetBytes(val); frame.tris[ix++] = by[0]; frame.tris[ix++] = by[1]; } } return(frame); }
public static MegaCacheImageFrame CreateImageFrame(Mesh ms) { MegaCacheImageFrame frame = new MegaCacheImageFrame(); Vector3[] verts = ms.vertices; Vector3[] norms = ms.normals; Vector2[] uvs = ms.uv; Vector4[] tangents = ms.tangents; frame.vc = verts.Length; frame.nc = norms.Length; frame.tc = tangents.Length; frame.uvc = uvs.Length; frame.bmin = ms.bounds.min; //Vector3 bmax = ms.bounds.max; Vector3 msize = ms.bounds.size; frame.bsize = ms.bounds.size * (1.0f / 65535.0f); Bounds uvb = MegaCacheUtils.GetBounds(uvs); frame.uvmin = uvb.min; frame.uvsize = uvb.size * (1.0f / 255.0f); frame.verts = new byte[frame.vc * 6]; frame.norms = new byte[frame.nc * 3]; frame.tangents = new byte[frame.tc * 4]; frame.uvs = new byte[frame.vc * 2]; frame.tris = new byte[ms.triangles.Length * 2]; int ix = 0; byte[] by; for ( int v = 0; v < verts.Length; v++ ) { Vector3 pos = verts[v]; short val = (short)(((pos.x - frame.bmin.x) / msize.x) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; val = (short)(((pos.y - frame.bmin.y) / msize.y) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; val = (short)(((pos.z - frame.bmin.z) / msize.z) * 65535.0f); by = System.BitConverter.GetBytes(val); frame.verts[ix++] = by[0]; frame.verts[ix++] = by[1]; } ix = 0; for ( int v = 0; v < norms.Length; v++ ) { Vector3 pos = norms[v]; frame.norms[ix++] = (byte)((pos.x + 1.0f) * 127.0f); frame.norms[ix++] = (byte)((pos.y + 1.0f) * 127.0f); frame.norms[ix++] = (byte)((pos.z + 1.0f) * 127.0f); } ix = 0; for ( int v = 0; v < tangents.Length; v++ ) { Vector4 pos = tangents[v]; frame.tangents[ix++] = (byte)((pos.x + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.y + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.z + 1.0f) * 127.0f); frame.tangents[ix++] = (byte)((pos.w + 1.0f) * 127.0f); } ix = 0; for ( int v = 0; v < uvs.Length; v++ ) { Vector2 pos = uvs[v]; frame.uvs[ix++] = (byte)(((pos.x - uvb.min.x) / uvb.size.x) * 255.0f); frame.uvs[ix++] = (byte)(((pos.y - uvb.min.y) / uvb.size.y) * 255.0f); } frame.subcount = ms.subMeshCount; frame.suboffs = new int[frame.subcount]; frame.sublen = new int[frame.subcount]; ix = 0; for ( int s = 0; s < frame.subcount; s++ ) { int[] tris = ms.GetTriangles(s); frame.suboffs[s] = ix; frame.sublen[s] = tris.Length; for ( int t = 0; t < tris.Length; t++ ) { short val = (short)tris[t]; by = System.BitConverter.GetBytes(val); frame.tris[ix++] = by[0]; frame.tris[ix++] = by[1]; } } return frame; }