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; }