public static Texture2D RenderAndExtractImageToTexture(HEU_SessionBase session, HAPI_MaterialInfo materialInfo, HAPI_ParmId textureParmID, string textureName, string assetCacheFolderPath) { //Debug.LogFormat("Rendering texture {0} with name {1} for material {2} at path {3}", textureParmID, textureName, materialInfo.nodeId, assetCacheFolderPath); Texture2D texture = null; // First we get Houdini to render the texture to an image buffer, then query the buffer over HAPI // Next we convert to PNG, and write out to file in our Assets directory // The reason for querying as a buffer is to workaround a bug with ExtractHoudiniImageToTextureFile // Note: intentionly ignoring any errors as sometimes there aren't any textures if (session.RenderTextureToImage(materialInfo.nodeId, textureParmID, false)) { texture = HEU_MaterialFactory.ExtractHoudiniImageToTextureRaw(session, materialInfo, "C A"); if(texture != null) { texture.name = textureName; // Get the Textures folder in the assetCacheFolderPath. Make sure it exists. assetCacheFolderPath = HEU_AssetDatabase.AppendTexturesPathToAssetFolder(assetCacheFolderPath); HEU_AssetDatabase.CreatePathWithFolders(assetCacheFolderPath); // We are defaulting to PNG here if no extension already set. This forces it to use PNG format below. if (!textureName.EndsWith(".png") && !textureName.EndsWith(".jpg")) { textureName = textureName + ".png"; } string textureFileName = HEU_Platform.BuildPath(assetCacheFolderPath, string.Format("{0}", textureName)); byte[] encodedBytes; if(textureName.EndsWith(".jpg")) { encodedBytes = texture.EncodeToJPG(); } else // Use PNG otherwise { encodedBytes = texture.EncodeToPNG(); } HEU_Platform.WriteBytes(textureFileName, encodedBytes); // Re-import for project to recognize the new texture file HEU_AssetDatabase.ImportAsset(textureFileName, HEU_AssetDatabase.HEU_ImportAssetOptions.Default); // Load the new texture file texture = HEU_AssetDatabase.LoadAssetAtPath(textureFileName, typeof(Texture2D)) as Texture2D; } //texture = HEU_MaterialFactory.ExtractHoudiniImageToTextureFile(session, materialInfo, "C A", assetCacheFolderPath); } return texture; }
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; }