/// <summary> /// Bake the Substance outputs to PNG textures in the target folder /// A seperate folder /Textures folder will be created next to the material /// </summary> public void BakeSubstance(ProceduralMaterial substance, Material material, bool useCompression = false) { this.substance = substance; this.material = material; SubstanceBaker.useCompression = useCompression; //Substance .sbsar container string substancePath = AssetDatabase.GetAssetPath(substance.GetInstanceID()); si = AssetImporter.GetAtPath(substancePath) as SubstanceImporter; //Also generate shadermap si.SetGenerateAllOutputs(substance, true); //Make readable substance.isReadable = true; //Generate textures substance.RebuildTexturesImmediately(); Texture[] substanceOutputs = substance.GetGeneratedTextures(); foreach (Texture texture in substanceOutputs) { { ConvertProceduralTexture(texture); } } AssetDatabase.Refresh(); }
//Locate the StylizedWater Substance material in the project private void FindSubstance() { string[] assets = AssetDatabase.FindAssets("t:ProceduralMaterial StylizedWater"); string assetPath = AssetDatabase.GUIDToAssetPath(assets[0]); SubstanceImporter si = AssetImporter.GetAtPath(assetPath) as SubstanceImporter; //Substance .sbsar container ProceduralMaterial[] substanceContainer = si.GetMaterials(); //Look for the substance instance matching the material name we're looking for foreach (ProceduralMaterial substanceInstance in substanceContainer) { if (substanceInstance.name == "StylizedWater") { substance = substanceInstance; //Gotcha si.SetGenerateAllOutputs(substance, true); //Debug.Log("[Stylized Water] Succesfully located \"StylizedWater\" Substance in project."); } } if (!substance) { Debug.LogError("[Stylized Water] The \"StylizedWater\" Substance material could not be located in the project. Was it removed or renamed?"); } }
public static void ApplySettings(BakerProfile profile, ProceduralMaterial proceduralMaterial, SubstanceImporter substanceImporter) { int width, height, format, loadbehaviour; substanceImporter.GetPlatformTextureSettings(proceduralMaterial.name, profile.platform, out width, out height, out format, out loadbehaviour); width = profile.TargetWidth == 0 ? width : profile.TargetWidth; height = profile.TargetHeight == 0 ? height : profile.TargetHeight; format = profile.format == BakerProfile.Format.Unchanged ? format : (int)profile.format; loadbehaviour = profile.loadingBehaviour == BakerProfile.LoadingBehavior.Unchanged ? loadbehaviour : (int)profile.loadingBehaviour; Debug.Log("Applying Settings to " + proceduralMaterial.name); //Print the name of the material foreach (var property in profile.customFloat) { try { if (proceduralMaterial.HasProceduralProperty(property.Name)) { proceduralMaterial.SetProceduralFloat(property.Name, property.value); } } catch (Exception e) { Debug.LogError(e); } } substanceImporter.SetPlatformTextureSettings(proceduralMaterial, profile.platform, width, height, format, loadbehaviour); substanceImporter.SetGenerateAllOutputs(proceduralMaterial, profile.generateAllMaps); substanceImporter.SaveAndReimport(); //Reimport under the new settings AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
private void ConfigureSubstance(SubstanceImporter si) { //Set the format mode to RAW //Fails to work for an unknown reason, still looking into it! int maxTextureWidth; int maxTextureHeight; int textureFormat; int loadBehavior; si.GetPlatformTextureSettings(substance.name, "Standalone", out maxTextureWidth, out maxTextureHeight, out textureFormat, out loadBehavior); si.SetPlatformTextureSettings(substance, "Standalone", maxTextureWidth, maxTextureHeight, 1, loadBehavior); //Also generate heightmap and roughness si.SetGenerateAllOutputs(substance, true); }
void OnGUI() { if (size == 0) { size = 1; } size = EditorGUILayout.IntField("Substances", size); if (substances == null) { substances = new SubstanceArchive[size]; } if (substances.Length != size) { SubstanceArchive[] cache = substances; substances = new SubstanceArchive[size]; for (int i = 0; i < (cache.Length > size ? size : cache.Length); i++) { substances[i] = cache[i]; } } for (int i = 0; i < size; i++) { substances[i] = EditorGUILayout.ObjectField("Substance " + (i + 1), substances[i], typeof(SubstanceArchive), false) as SubstanceArchive; } if (GUILayout.Button("Extract")) { for (int subs = 0; subs < size; subs++) { SubstanceArchive substance = substances[subs]; if (substance == null) { continue; } string substancePath = AssetDatabase.GetAssetPath(substance.GetInstanceID()); SubstanceImporter substanceImporter = AssetImporter.GetAtPath(substancePath) as SubstanceImporter; int substanceMaterialCount = substanceImporter.GetMaterialCount(); ProceduralMaterial[] substanceMaterials = substanceImporter.GetMaterials(); if (substanceMaterialCount <= 0) { continue; } string basePath = substancePath.Replace("/" + substance.name + ".sbsar", ""); if (!Directory.Exists(basePath + "/" + substance.name)) { AssetDatabase.CreateFolder(basePath, substance.name); AssetDatabase.ImportAsset(basePath + "/" + substance.name); } if (!Directory.Exists("EXPORT_HERE")) { Directory.CreateDirectory("EXPORT_HERE"); } System.Type substanceImporterType = typeof(SubstanceImporter); MethodInfo exportBitmaps = substanceImporterType.GetMethod("ExportBitmaps", BindingFlags.Instance | BindingFlags.Public); if (null == exportBitmaps) { return; } foreach (ProceduralMaterial substanceMaterial in substanceMaterials) { substanceMaterial.isReadable = true;//@zpj bool generateAllOutputs = substanceImporter.GetGenerateAllOutputs(substanceMaterial); if (!Directory.Exists(basePath + "/" + substance.name + "/" + substanceMaterial.name)) { AssetDatabase.CreateFolder(basePath + "/" + substance.name, substanceMaterial.name); AssetDatabase.ImportAsset(basePath + "/" + substance.name + "/" + substanceMaterial.name); } string materialPath = basePath + "/" + substance.name + "/" + substanceMaterial.name + "/"; Material newMaterial = new Material(substanceMaterial.shader); newMaterial.CopyPropertiesFromMaterial(substanceMaterial); AssetDatabase.CreateAsset(newMaterial, materialPath + substanceMaterial.name + ".mat"); AssetDatabase.ImportAsset(materialPath + substanceMaterial.name + ".mat"); substanceImporter.SetGenerateAllOutputs(substanceMaterial, true); exportBitmaps.Invoke(substanceImporter, new object[] { substanceMaterial, @"EXPORT_HERE", false }); if (!generateAllOutputs) { substanceImporter.SetGenerateAllOutputs(substanceMaterial, false); } string[] exportedTextures = Directory.GetFiles("EXPORT_HERE"); if (exportedTextures.Length > 0) { string TmpfilePath = string.Empty; foreach (string exportedTexture in exportedTextures) { TmpfilePath = materialPath + exportedTexture.Replace("EXPORT_HERE", ""); if (File.Exists(TmpfilePath)) { File.Delete(TmpfilePath); //Debug.Log(TmpfilePath); } File.Move(exportedTexture, TmpfilePath); } } AssetDatabase.Refresh(); int propertyCount = ShaderUtil.GetPropertyCount(newMaterial.shader); Texture[] materialTextures = substanceMaterial.GetGeneratedTextures(); if ((materialTextures.Length <= 0) || (propertyCount <= 0)) { continue; } Texture newTmpTexture = new Texture(); foreach (ProceduralTexture materialTexture in materialTextures) { string newTexturePath = materialPath + materialTexture.name + ".tga";// (Clone) string astmpe = Application.dataPath + newTexturePath.Substring(6); if (!File.Exists(astmpe)) { newTexturePath = materialPath + materialTexture.name + " (Clone).tga"; astmpe = Application.dataPath + newTexturePath.Substring(6); if (!File.Exists(astmpe)) { Debug.LogError(newTexturePath + "not exist"); } } Texture newTextureAsset = (Texture)AssetDatabase.LoadAssetAtPath(newTexturePath, typeof(Texture)); if (null != newTextureAsset) { try { for (int i = 0; i < propertyCount; i++) { if (ShaderUtil.GetPropertyType(newMaterial.shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) { string propertyName = ShaderUtil.GetPropertyName(newMaterial.shader, i); newTmpTexture = newMaterial.GetTexture(propertyName); //Debug.Log(newTmpTexture.name + " and " + propertyName + " new assset " + newTextureAsset.name); if (null != newTmpTexture && (newTmpTexture.name == newTextureAsset.name || newTmpTexture.name + " (Clone)" == newTextureAsset.name)) { newMaterial.SetTexture(propertyName, newTextureAsset); } } } } catch (System.Exception ex) { Debug.Log(ex.Message); } } ProceduralOutputType outType = materialTexture.GetProceduralOutputType(); if (materialTexture.GetProceduralOutputType() == ProceduralOutputType.Normal) { TextureImporter textureImporter = AssetImporter.GetAtPath(newTexturePath) as TextureImporter; if (null != textureImporter) { textureImporter.textureType = TextureImporterType.Bump; } AssetDatabase.ImportAsset(newTexturePath); } } } if (Directory.Exists("EXPORT_HERE")) { Directory.Delete("EXPORT_HERE"); } } } }