/// <summary> /// Loads texture at path which should be under a Resources/ folder. /// </summary> /// <param name="path">Path to texture, must be relative to a Resources/ folder.</param> /// <returns>Loaded texture or null if failed.</returns> public static Texture2D LoadTexture(string path) { if(HEU_AssetDatabase.IsPathRelativeToAssets(path) || System.IO.Path.IsPathRooted(path)) { HEU_AssetDatabase.ImportAsset(path, HEU_AssetDatabase.HEU_ImportAssetOptions.Default); return HEU_AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; } else { // Try as relative path to Resources return Resources.Load<Texture2D>(path); } }
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 Material LoadUnityMaterial(string materialPath) { if(materialPath.StartsWith(HEU_Defines.DEFAULT_UNITY_BUILTIN_RESOURCES)) { return HEU_AssetDatabase.LoadUnityAssetFromUniqueAssetPath<Material>(materialPath); } else if (materialPath.StartsWith("Packages/")) { return HEU_AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; } string relativePath = materialPath; if (relativePath.StartsWith(Application.dataPath)) { // If absolute path, change to relative path relativePath = HEU_AssetDatabase.GetAssetRelativePath(materialPath); } Material material = null; string mainPath = null; string subPath = null; HEU_AssetDatabase.GetSubAssetPathFromPath(relativePath, out mainPath, out subPath); if(subPath != null) { // This is a subasset. Need to find it by first loading the main asset. Object subObject = HEU_AssetDatabase.LoadSubAssetAtPath(mainPath, subPath); if(subObject != null) { material = subObject as Material; } } else { // Try loading from Resources first material = Resources.Load<Material>(relativePath) as Material; if (material == null) { // If not in Resources, try loading from project HEU_AssetDatabase.ImportAsset(relativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default); material = HEU_AssetDatabase.LoadAssetAtPath(relativePath, typeof(Material)) as Material; } } return material; }
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; }
public static Material LoadUnityMaterial(string materialPath) { if(materialPath.StartsWith(HEU_Defines.DEFAULT_UNITY_BUILTIN_RESOURCES)) { return HEU_AssetDatabase.LoadUnityAssetFromUniqueAssetPath<Material>(materialPath); } string relativePath = materialPath; if (relativePath.StartsWith(Application.dataPath)) { // If absolute path, change to relative path relativePath = HEU_AssetDatabase.GetAssetRelativePath(materialPath); } // Try loading from Resources first Material material = Resources.Load<Material>(relativePath) as Material; if(material == null) { // If not in Resources, try loading from project HEU_AssetDatabase.ImportAsset(relativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default); material = HEU_AssetDatabase.LoadAssetAtPath(relativePath, typeof(Material)) as Material; } return material; }
private void GenerateInstancesFromAssetPaths(HEU_LoadBufferInstancer instancerBuffer, Transform instanceRootTransform) { // For single asset, this is set when its impoted GameObject singleAssetGO = null; // For multi assets, keep track of loaded objects so we only need to load once for each object Dictionary<string, GameObject> loadedUnityObjectMap = new Dictionary<string, GameObject>(); // Temporary empty gameobject in case the specified Unity asset is not found GameObject tempGO = null; if (instancerBuffer._assetPaths.Length == 1) { // Single asset path if (!string.IsNullOrEmpty(instancerBuffer._assetPaths[0])) { HEU_AssetDatabase.ImportAsset(instancerBuffer._assetPaths[0], HEU_AssetDatabase.HEU_ImportAssetOptions.Default); singleAssetGO = HEU_AssetDatabase.LoadAssetAtPath(instancerBuffer._assetPaths[0], typeof(GameObject)) as GameObject; } if (singleAssetGO == null) { Debug.LogErrorFormat("Asset at path {0} not found. Unable to create instances for {1}.", instancerBuffer._assetPaths[0], instancerBuffer._name); return; } } int numInstancesCreated = 0; int numInstances = instancerBuffer._instanceTransforms.Length; for (int i = 0; i < numInstances; ++i) { // Reset to the single asset for each instance allows which is null if using multi asset // therefore forcing the instance asset to be found GameObject unitySrcGO = singleAssetGO; if (unitySrcGO == null) { // If not using single asset, then there must be an asset path for each instance if (string.IsNullOrEmpty(instancerBuffer._assetPaths[i])) { continue; } if (!loadedUnityObjectMap.TryGetValue(instancerBuffer._assetPaths[i], out unitySrcGO)) { // Try loading it //HEU_AssetDatabase.ImportAsset(instancerBuffer._assetPaths[i], HEU_AssetDatabase.HEU_ImportAssetOptions.Default); unitySrcGO = HEU_AssetDatabase.LoadAssetAtPath(instancerBuffer._assetPaths[i], typeof(GameObject)) as GameObject; if (unitySrcGO == null) { Debug.LogErrorFormat("Unable to load asset at {0} for instancing!", instancerBuffer._assetPaths[i]); // Even though the source Unity object is not found, we should create an object instance info to track it if (tempGO == null) { tempGO = new GameObject(); } unitySrcGO = tempGO; } // Adding to map even if not found so we don't flood the log with the same error message loadedUnityObjectMap.Add(instancerBuffer._assetPaths[i], unitySrcGO); } } CreateNewInstanceFromObject(unitySrcGO, (numInstancesCreated + 1), instanceRootTransform, ref instancerBuffer._instanceTransforms[i], instancerBuffer._instancePrefixes, instancerBuffer._name); numInstancesCreated++; } if (tempGO != null) { HEU_GeneralUtility.DestroyImmediate(tempGO, bRegisterUndo: false); } }
private void GenerateInstancesFromNodeIDs(HAPI_NodeId cookNodeId, HEU_LoadBufferInstancer instancerBuffer, Dictionary <HAPI_NodeId, HEU_LoadBufferBase> idBuffersMap, Transform instanceRootTransform) { // For single collision geo override GameObject singleCollisionGO = null; // For multi collision geo overrides, keep track of loaded objects Dictionary <string, GameObject> loadedCollisionObjectMap = new Dictionary <string, GameObject>(); if (instancerBuffer._collisionAssetPaths != null && instancerBuffer._collisionAssetPaths.Length == 1) { // Single collision override if (!string.IsNullOrEmpty(instancerBuffer._collisionAssetPaths[0])) { HEU_AssetDatabase.ImportAsset(instancerBuffer._collisionAssetPaths[0], HEU_AssetDatabase.HEU_ImportAssetOptions.Default); singleCollisionGO = HEU_AssetDatabase.LoadAssetAtPath(instancerBuffer._collisionAssetPaths[0], typeof(GameObject)) as GameObject; } if (singleCollisionGO == null) { // Continue on but log error Debug.LogErrorFormat("Collision asset at path {0} not found for instance {1}.", instancerBuffer._collisionAssetPaths[0], instancerBuffer._name); } } int numInstances = instancerBuffer._instanceNodeIDs.Length; for (int i = 0; i < numInstances; ++i) { HEU_LoadBufferBase sourceBuffer = null; if (!idBuffersMap.TryGetValue(instancerBuffer._instanceNodeIDs[i], out sourceBuffer) || sourceBuffer == null) { Debug.LogErrorFormat("Part with id {0} is missing. Unable to setup instancer!", instancerBuffer._instanceNodeIDs[i]); return; } // If the part we're instancing is itself an instancer, make sure it has generated its instances if (sourceBuffer._bInstanced && sourceBuffer._generatedOutput == null) { HEU_LoadBufferInstancer sourceBufferInstancer = instancerBuffer as HEU_LoadBufferInstancer; if (sourceBufferInstancer != null) { GenerateInstancer(cookNodeId, sourceBufferInstancer, idBuffersMap); } } GameObject sourceGameObject = sourceBuffer._generatedOutput._outputData._gameObject; if (sourceGameObject == null) { Debug.LogErrorFormat("Output gameobject is null for source {0}. Unable to instance for {1}.", sourceBuffer._name, instancerBuffer._name); continue; } GameObject collisionSrcGO = null; if (singleCollisionGO != null) { // Single collision geo collisionSrcGO = singleCollisionGO; } else if (instancerBuffer._collisionAssetPaths != null && (i < instancerBuffer._collisionAssetPaths.Length) && !string.IsNullOrEmpty(instancerBuffer._collisionAssetPaths[i])) { // Mutliple collision geo (one per instance). if (!loadedCollisionObjectMap.TryGetValue(instancerBuffer._collisionAssetPaths[i], out collisionSrcGO)) { collisionSrcGO = HEU_AssetDatabase.LoadAssetAtPath(instancerBuffer._collisionAssetPaths[i], typeof(GameObject)) as GameObject; if (collisionSrcGO == null) { Debug.LogErrorFormat("Unable to load collision asset at {0} for instancing!", instancerBuffer._collisionAssetPaths[i]); } else { loadedCollisionObjectMap.Add(instancerBuffer._collisionAssetPaths[i], collisionSrcGO); } } } int numTransforms = instancerBuffer._instanceTransforms.Length; for (int j = 0; j < numTransforms; ++j) { CreateNewInstanceFromObject(sourceGameObject, (j + 1), instanceRootTransform, ref instancerBuffer._instanceTransforms[j], instancerBuffer._instancePrefixes, instancerBuffer._name, collisionSrcGO); } } }