using UnityEngine; using UnityEditor; using UnityEditor.SceneManagement; using System.Collections.Generic; using System.Runtime.InteropServices; public class xatlas { //#define UV_HINT public static List newUVBuffer; public static List newXrefBuffer; [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern System.IntPtr xatlasCreateAtlas(); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasAddMesh(System.IntPtr atlas, int vertexCount, System.IntPtr positions, System.IntPtr normals, System.IntPtr uv, int indexCount, int[] indices32); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasAddUVMesh(System.IntPtr atlas, int vertexCount, System.IntPtr uv, int indexCount, int[] indices32, bool allowRotate); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern void xatlasParametrize(System.IntPtr atlas); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern void xatlasPack(System.IntPtr atlas, int attempts, float texelsPerUnit, int resolution, int maxChartSize, int padding, bool bruteForce, bool blockAlign);//, bool allowRotate); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern void xatlasNormalize(System.IntPtr atlas, int[] atlasSizes, bool preferDensity); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasGetAtlasCount(System.IntPtr atlas); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasGetAtlasIndex(System.IntPtr atlas, int meshIndex, int chartIndex); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasGetVertexCount(System.IntPtr atlas, int meshIndex); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasGetIndexCount(System.IntPtr atlas, int meshIndex); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern void xatlasGetData(System.IntPtr atlas, int meshIndex, System.IntPtr outUV, System.IntPtr outRef, System.IntPtr outIndices); [DllImport ("xatlasLib", CallingConvention=CallingConvention.Cdecl)] public static extern int xatlasClear(System.IntPtr atlas); static T[] FillAtrribute(List xrefArray, T[] origArray) { if (origArray == null || origArray.Length == 0) return origArray; var arr = new T[xrefArray.Count]; for(int i=0; i(); //var newXref = new List(); var indexBuffers = new List(); newUVBuffer = new List(); newXrefBuffer = new List(); while(newUVBuffer.Count < m.vertexCount) { newUVBuffer.Add(new Vector2(-100, -100)); newXrefBuffer.Add(0); } xatlasNormalize(atlas, null, false); // Collect UVs/xrefs/indices for(int i=0; ifinal index mappings var xatlasIndexToNewIndex = new int[newVertCount]; for(int j=0; j= 65000;//0xFFFF; if (is32bit && origIs16bit) { Debug.LogError("Unwrap failed: original mesh (" + m.name + ") has 16 bit indices, but unwrapped requires 32 bit."); return; } // Duplicate attributes //if (newXrefBuffer.Count > m.vertexCount) // commented because can be also swapped around { m.vertices = FillAtrribute(newXrefBuffer, positions); m.normals = FillAtrribute(newXrefBuffer, normals); m.boneWeights = FillAtrribute(newXrefBuffer, m.boneWeights); m.colors32 = FillAtrribute(newXrefBuffer, m.colors32); m.tangents = FillAtrribute(newXrefBuffer, m.tangents); m.uv = FillAtrribute(newXrefBuffer, m.uv); m.uv3 = FillAtrribute(newXrefBuffer, m.uv3); m.uv4 = FillAtrribute(newXrefBuffer, m.uv4); #if UNITY_2018_2_OR_NEWER m.uv5 = FillAtrribute(newXrefBuffer, m.uv5); m.uv6 = FillAtrribute(newXrefBuffer, m.uv6); m.uv7 = FillAtrribute(newXrefBuffer, m.uv7); m.uv8 = FillAtrribute(newXrefBuffer, m.uv8); #endif } m.uv2 = newUVBuffer.ToArray(); /* // Set new UV2 var finalUV2 = new Vector2[vertCount + newUV2.Count]; for(int i=0; i