private static Texture2D ExtractHoudiniImageToTexturePNGJPEG(HEU_SessionBase session, HAPI_MaterialInfo materialInfo, string imagePlanes) { Texture2D textureResult = null; HAPI_ImageInfo imageInfo = new HAPI_ImageInfo(); if (!session.GetImageInfo(materialInfo.nodeId, ref imageInfo)) { return textureResult; } // This will return null if the current imageInfo file format is supported by Unity, otherwise // returns a Unity supported file format. string desiredFileFormatName = HEU_MaterialData.GetSupportedFileFormat(session, ref imageInfo); imageInfo.gamma = HEU_PluginSettings.ImageGamma; session.SetImageInfo(materialInfo.nodeId, ref imageInfo); // Download the image into memory buffer byte[] imageData = null; if (!session.ExtractImageToMemory(materialInfo.nodeId, desiredFileFormatName, imagePlanes, out imageData)) { return textureResult; } // Upload to Unity textureResult = new Texture2D(1, 1); textureResult.LoadImage(imageData); return textureResult; }
private static Texture2D ExtractHoudiniImageToTextureRaw(HEU_SessionBase session, HAPI_MaterialInfo materialInfo, string imagePlanes) { Texture2D textureResult = null; HAPI_ImageInfo imageInfo = new HAPI_ImageInfo(); if (!session.GetImageInfo(materialInfo.nodeId, ref imageInfo)) { return textureResult; } imageInfo.dataFormat = HAPI_ImageDataFormat.HAPI_IMAGE_DATA_INT8; imageInfo.interleaved = true; imageInfo.packing = HAPI_ImagePacking.HAPI_IMAGE_PACKING_RGBA; imageInfo.gamma = HEU_PluginSettings.ImageGamma; session.SetImageInfo(materialInfo.nodeId, ref imageInfo); // Extract image to buffer byte[] imageData = null; if (!session.ExtractImageToMemory(materialInfo.nodeId, HEU_Defines.HAPI_RAW_FORMAT_NAME, imagePlanes, out imageData)) { return textureResult; } int colorDataSize = imageInfo.xRes * imageInfo.yRes; if (colorDataSize * 4 != imageData.Length) { Debug.LogErrorFormat("Extracted image size does not match expected image info size." + " Try using non-raw format for texture extraction."); return textureResult; } Color32[] colorData = new Color32[colorDataSize]; for (int i = 0; i < colorDataSize; ++i) { colorData[i].r = imageData[i * 4 + 0]; colorData[i].g = imageData[i * 4 + 1]; colorData[i].b = imageData[i * 4 + 2]; colorData[i].a = imageData[i * 4 + 3]; } textureResult = new Texture2D(imageInfo.xRes, imageInfo.yRes, TextureFormat.ARGB32, false); textureResult.SetPixels32(colorData); textureResult.Apply(); return textureResult; }
public static Texture2D ExtractHoudiniImageToTextureFile(HEU_SessionBase session, HAPI_MaterialInfo materialInfo, string imagePlanes, string assetCacheFolderPath) { Texture2D textureResult = null; // Get the Textures folder in the assetCacheFolderPath. Make sure it exists. assetCacheFolderPath = HEU_AssetDatabase.AppendTexturesPathToAssetFolder(assetCacheFolderPath); HEU_AssetDatabase.CreatePathWithFolders(assetCacheFolderPath); // Need to pass in full path to Houdini to write out the file assetCacheFolderPath = HEU_AssetDatabase.GetAssetFullPath(assetCacheFolderPath); if (assetCacheFolderPath == null) { return textureResult; } HAPI_ImageInfo imageInfo = new HAPI_ImageInfo(); if (!session.GetImageInfo(materialInfo.nodeId, ref imageInfo)) { return textureResult; } // This will return null if the current imageInfo file format is supported by Unity, otherwise // returns a Unity supported file format. string desiredFileFormatName = HEU_MaterialData.GetSupportedFileFormat(session, ref imageInfo); // Extract image to file string writtenFilePath = null; if (!session.ExtractImageToFile(materialInfo.nodeId, desiredFileFormatName, imagePlanes, assetCacheFolderPath, out writtenFilePath)) { return textureResult; } HEU_AssetDatabase.SaveAndRefreshDatabase(); // Convert full path back to relative in order to work with AssetDatabase string assetRelativePath = HEU_AssetDatabase.GetAssetRelativePath(writtenFilePath); // Re-import to refresh the project HEU_AssetDatabase.ImportAsset(assetRelativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default); textureResult = HEU_AssetDatabase.LoadAssetAtPath(assetRelativePath, typeof(Texture2D)) as Texture2D; //Debug.LogFormat("Loaded texture to file {0} with format {1}", writtenFilePath, textureResult != null ? textureResult.format.ToString() : "none"); return textureResult; }