/// <summary>
        /// Packs image buffer into a single buffer. Image buffers are assumed to be 4 bytes per pixel in RGBA format
        /// </summary>
        /// <param name="buffers">Image buffers to pack</param>
        /// <param name="width">Image buffers width</param>
        /// <param name="height">Image buffers height</param>
        /// <param name="padding">Padding between each packed image</param>
        /// <param name="outPackedBuffer">Packed image buffer</param>
        /// <param name="outPackedBufferWidth">Packed image buffer's width</param>
        /// <param name="outPackedBufferHeight">Packed iamge buffer's height</param>
        /// <param name="outPackedRect">Location of each image buffers in the packed buffer</param>
        /// <param name="outUVTransform">Translation data from image original buffer to packed buffer</param>
        public static void Pack(NativeArray <Color32>[] buffers, int width, int height, int padding, out NativeArray <Color32> outPackedBuffer, out int outPackedBufferWidth, out int outPackedBufferHeight, out RectInt[] outPackedRect, out Vector2Int[] outUVTransform)
        {
            UnityEngine.Profiling.Profiler.BeginSample("Pack");
            // Determine the area that contains data in the buffer
            outPackedBuffer = default(NativeArray <Color32>);
            try
            {
                var tightRects = FindTightRectJob.Execute(buffers, width, height);
                Pack(tightRects, padding, out outPackedRect, out outPackedBufferWidth, out outPackedBufferHeight);
                outUVTransform = new Vector2Int[tightRects.Length];
                for (int i = 0; i < outUVTransform.Length; ++i)
                {
                    outUVTransform[i] = new Vector2Int(outPackedRect[i].x - tightRects[i].x, outPackedRect[i].y - tightRects[i].y);
                }
                outPackedBuffer = new NativeArray <Color32>(outPackedBufferWidth * outPackedBufferHeight, Allocator.Temp);

                Blit(outPackedBuffer, outPackedRect, outPackedBufferWidth, buffers, tightRects, width, padding);
            }
            catch (Exception ex)
            {
                if (outPackedBuffer.IsCreated)
                {
                    outPackedBuffer.Dispose();
                }
                throw ex;
            }
            finally
            {
                UnityEngine.Profiling.Profiler.EndSample();
            }
        }
Example #2
0
        public static unsafe  RectInt[] Execute(NativeArray <Color32>[] buffers, int width, int height)
        {
            var job = new FindTightRectJob();

            job.m_Buffers = new NativeArray <IntPtr>(buffers.Length, Allocator.TempJob);
            for (int i = 0; i < buffers.Length; ++i)
            {
                job.m_Buffers[i] = new IntPtr(buffers[i].GetUnsafeReadOnlyPtr());
            }
            job.m_Output = new NativeArray <RectInt>(buffers.Length, Allocator.Temp);
            job.m_Width  = width;
            job.m_Height = height;
            // Ensure all jobs are completed before we return since we don't own the buffers
            job.Schedule(buffers.Length, 1).Complete();
            var rects = job.m_Output.ToArray();

            job.m_Output.Dispose();
            return(rects);
        }