/// <summary> /// Encode the input data as per provided image format. /// </summary> /// <param name="data">An array of data to be encoded.</param> /// <param name="width">Image width.</param> /// <param name="height">Image height.</param> /// <param name="format">Graphics format used by the render texture.</param> /// <param name="imageFormat">Format for encoding the data.</param> /// <param name="additionalParam">Additional flags to be passed for the encoding.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static Array EncodeArray(Array data, int width, int height, GraphicsFormat format, ImageFormat imageFormat, int additionalParam = 0) { switch (imageFormat) { case ImageFormat.Raw: return(data); #if UNITY_2019_3_OR_NEWER case ImageFormat.Png: return(ImageConversion.EncodeArrayToPNG(data, format, (uint)width, (uint)height, 0)); case ImageFormat.Exr: return(ImageConversion.EncodeArrayToEXR(data, format, (uint)width, (uint)height, 0, /*EXRFlags*/ (Texture2D.EXRFlags)additionalParam)); case ImageFormat.Tga: return(ImageConversion.EncodeArrayToTGA(data, format, (uint)width, (uint)height, 0)); #endif case ImageFormat.Jpg: #if USIM_USE_BUILTIN_JPG_ENCODER return(ImageConversion.EncodeArrayToJPG(data, format, (uint)width, (uint)height, 0, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75)); #else return(JpegEncoder.Encode(ArrayUtilities.Cast <byte>(data), width, height, GraphicsUtilities.GetBlockSize(format), format, /*quality*/ additionalParam > 0 ? (int)additionalParam : 75)); #endif default: throw new NotSupportedException("ImageFormat is not supported"); } }
public void Execute() { byte[] data, data2; //Export depth and motion vectors as 16 bits per channel EXR to preserve precision data = ImageConversion.EncodeArrayToEXR(bytes.ToArray(), GraphicsFormat.R16G16B16A16_SFloat, (uint)dimensions.x, (uint)dimensions.y, 0U, Texture2D.EXRFlags.CompressZIP); data2 = ImageConversion.EncodeArrayToEXR(bytes2.ToArray(), GraphicsFormat.R16G16B16A16_SFloat, (uint)dimensions.x, (uint)dimensions.y, 0U, Texture2D.EXRFlags.CompressZIP); string path1, path2; path1 = path + sceneName + "\\motVec" + number.ToString("D4") + ".exr"; path2 = path + sceneName + "\\final" + number.ToString("D4") + ".exr"; if (useAutoAnimation) { System.IO.Directory.CreateDirectory(path + sceneName + "\\" + startTime + "\\"); path1 = path + sceneName + "\\" + startTime + "\\" + startTime + "motVec" + number.ToString("D4") + ".exr"; path2 = path + sceneName + "\\" + startTime + "\\" + startTime + "final" + number.ToString("D4") + ".exr"; } var writer1 = new BinaryWriter(File.Open(path1, FileMode.Create)); var writer2 = new BinaryWriter(File.Open(path2, FileMode.Create)); writer1.Write(data); writer2.Write(data2); writer1.Close(); writer2.Close(); }
// Build voxel object public override float Build(Storage voxels, Bounds bounds, Informer informer, object parameter) { processor.superSamplingCount = superSamplingCount; processor.powerOfTwo = powerOfTwo; processor.expandEdges = expandEdges; processor.backgroundColor = backgroundColor; processor.hdr = fileFormat == FileFormat.EXR; // Execute real build-up method float progress = processor.Build(voxels, bounds); // Check if processing has been finished if (progress >= 1) { // Store file, if it is specified if (fileStoring && filePath != null && filePath.Length > 0 && Texture != null) { try { // Build target path switch (fileFormat) { #if UNITY_2020_2_OR_NEWER case FileFormat.JPG: case FileFormat.PNG: case FileFormat.TGA: case FileFormat.EXR: var source = processor.Texture; if (source) { var sourceData = source.GetPixelData <byte>(0); if (sourceData != null) { var length = source.width * source.height * source.depth; //var textureWidth = Mathf.CeilToInt(Mathf.Sqrt(length)); var textureWidth = (int)Math.Pow(2, Math.Ceiling(Math.Log(Mathf.Sqrt(length)) / Math.Log(2))); var columnsCount = Mathf.CeilToInt((float)textureWidth / source.width); var rowsCount = Mathf.CeilToInt((float)source.depth / columnsCount); textureWidth = source.width * columnsCount; var textureHeight = source.height * rowsCount; var pixelSize = processor.hdr ? 8 : 4; var lineSize = source.width * pixelSize; var buffer = new byte[lineSize]; var targetData = new byte[textureWidth * textureHeight * pixelSize]; if (targetData != null) { var sourceOffset = 0; // Process depth slices for (int slice = 0; slice < source.depth; ++slice) { // Calculate column and row of the target sub image var column = slice % columnsCount; var row = rowsCount - slice / columnsCount - 1; // Process lines of the current slice for (int line = 0; line < source.height; ++line) { // Get data for line into the cache sourceData.GetSubArray(sourceOffset, lineSize).CopyTo(buffer); // Copy it to the target buffer Array.Copy(buffer, 0, targetData, (column + (line + row * source.height) * columnsCount) * lineSize, lineSize); sourceOffset += lineSize; } } byte[] data; switch (fileFormat) { case FileFormat.JPG: // Convert image data to JPEG data data = ImageConversion.EncodeArrayToJPG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, 100); break; case FileFormat.PNG: // Convert image data to PNG data data = ImageConversion.EncodeArrayToPNG(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight); break; case FileFormat.TGA: // Convert image data to TGA data data = ImageConversion.EncodeArrayToTGA(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight); break; case FileFormat.EXR: // Convert image data to EXR data data = ImageConversion.EncodeArrayToEXR(targetData, source.graphicsFormat, (uint)textureWidth, (uint)textureHeight, 0, UnityEngine.Texture2D.EXRFlags.CompressZIP); break; default: data = null; break; } if (data != null) { // Make sure the target folders exist var path = System.IO.Path.Combine(Application.dataPath, filePath); if (Helper.CreateDirectory(path, true)) { #if UNITY_EDITOR // Remove existing asset file if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } // Update database UnityEditor.AssetDatabase.Refresh(); #endif // Save data to file System.IO.File.WriteAllBytes(path, data); #if UNITY_EDITOR // Store columns and rows for the asset to be applied later voxelTexturePaths.Add(System.IO.Path.GetFullPath(path), new Vector2Int(columnsCount, rowsCount)); // Update database to enable changing import settings UnityEditor.AssetDatabase.Refresh(); #endif } } } } } break; #endif default: // Save texture as asset file Helper.StoreAsset(processor.Texture, filePath, null); break; } } catch (System.Exception exception) { Debug.Log(exception.Message); } } #if UNITY_EDITOR // Add object creation undo operation if (!Application.isPlaying) { UnityEditor.Undo.RegisterCreatedObjectUndo(processor.Texture, "\"VoxelTexture3D\" Creation"); } #endif // Execute informer callback informer?.Invoke(new UnityEngine.Object[] { processor.Texture }, parameter); } return(progress); }