Created
January 4, 2021 21:27
-
-
Save Oppodelldog/dc35b9ada7aa9e2f03e4bf0acfcc3d2e to your computer and use it in GitHub Desktop.
Unity Rendering Pipeline Import that converts Materialize projects (.mtz) into Unity Standard Shader Mateirals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Xml; | |
using System.Xml.XPath; | |
using UnityEditor; | |
using UnityEditor.AssetImporters; | |
using UnityEngine; | |
namespace Plugins.MaterializeAsset | |
{ | |
[CustomEditor(typeof(ScriptedImportMtz))] | |
public class ScriptedImportMtzEditor : ScriptedImporterEditor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
base.ApplyRevertGUI(); | |
} | |
} | |
[ScriptedImporter(1, "mtz")] | |
public class ScriptedImportMtz : ScriptedImporter | |
{ | |
public override void OnImportAsset(AssetImportContext ctx) | |
{ | |
var assetFilepath = ctx.assetPath; | |
var targetName = Path.GetFileNameWithoutExtension(assetFilepath); | |
var assetFolder = String.Join("/", assetPath.Split('/').Reverse().Skip(1).Reverse()); | |
var material = CreateMaterial(assetFilepath, assetFolder); | |
ctx.AddObjectToAsset(targetName + ".mat", material); | |
ctx.SetMainObject(material); | |
} | |
private static Material CreateMaterial(string filename, string assetPath) | |
{ | |
var xmlDoc = new XmlDocument(); | |
xmlDoc.Load(filename); | |
var nav = xmlDoc.CreateNavigator(); | |
var diffuseMap = TextureName(nav, "/ProjectObject/diffuseMapOriginalPath", filename); | |
var heightmap = TextureName(nav, "/ProjectObject/heightMapPath", filename); | |
var normalMap = TextureName(nav, "/ProjectObject/normalMapPath", filename); | |
var metallicMap = TextureName(nav, "/ProjectObject/metallicMapPath", filename); | |
var smoothnessMap = TextureName(nav, "/ProjectObject/smoothnessMapPath", filename); | |
var aoMap = TextureName(nav, "/ProjectObject/aoMapPath", filename); | |
var settingMetallic = ParseFloat(nav, "/ProjectObject/MatS/Metallic", filename, 1); | |
var settingSmoothness = ParseFloat(nav, "/ProjectObject/MatS/Smoothness", filename, 1); | |
var settingParallax = ParseFloat(nav, "/ProjectObject/MatS/Parallax", filename, 1); | |
var settingAOPower = ParseFloat(nav, "/ProjectObject/MatS/AOPower", filename, 1); | |
var settingTexTilingX = ParseFloat(nav, "/ProjectObject/MatS/TexTilingX", filename, 1); | |
var settingTexTilingY = ParseFloat(nav, "/ProjectObject/MatS/TexTilingY", filename, 1); | |
var settingTexTilingXText = ParseFloat(nav, "/ProjectObject/MatS/TexTilingXText", filename, 1); | |
var settingTexTilingYText = ParseFloat(nav, "/ProjectObject/MatS/TexTilingYText", filename, 1); | |
var settingLightR = ParseFloat(nav, "/ProjectObject/MatS/LightR", filename, 1); | |
var settingLightG = ParseFloat(nav, "/ProjectObject/MatS/LightG", filename, 1); | |
var settingLightB = ParseFloat(nav, "/ProjectObject/MatS/LightB", filename, 1); | |
var settingLightA = ParseFloat(nav, "/ProjectObject/MatS/LightA", filename, 1); | |
var material = new Material(Shader.Find("Standard")); | |
SetTexture("_MainTex", diffuseMap, assetPath, material); | |
SetTexture("_ParallaxMap", heightmap, assetPath, material); | |
SetTexture("_BumpMap", normalMap, assetPath, material); | |
SetTexture("_OcclusionMap", aoMap, assetPath, material); | |
SetTexture("_MetallicGlossMap", metallicMap, assetPath, material); | |
material.SetInt("_SmoothnessTextureChannel", 1); | |
material.SetFloat("_Metallic", Mathf.Clamp(settingMetallic / 2, 0, 1)); | |
material.SetFloat("_GlossMapScale", Mathf.Clamp(settingSmoothness / 2, 0, 1)); | |
material.SetFloat("_OcclusionStrength", Mathf.Clamp(settingAOPower / 2, 0, 1)); | |
material.SetFloat("_Parallax", Mathf.Clamp(settingParallax / 2 * 0.08f, 0.005f, 0.08f)); | |
material.SetTextureScale("_MainTex", new Vector2(settingTexTilingX, settingTexTilingY)); | |
material.SetTextureOffset("_MainTex", new Vector2(settingTexTilingXText, settingTexTilingYText)); | |
material.SetColor("_Color", new Color(settingLightR, settingLightG, settingLightB, settingLightA)); | |
return material; | |
} | |
private static void SetTexture(string name, string diffuseMap, string assetPath, Material material) | |
{ | |
material.SetTexture(name, AssetDatabase.LoadAssetAtPath<Texture>(Path.Combine(assetPath, diffuseMap))); | |
} | |
private static string TextureName(XPathNavigator nav, string xPath, string xmlFile) | |
{ | |
var node = nav.SelectSingleNode(xPath); | |
if (node == null) | |
{ | |
Debug.LogWarning("could not read '" + xPath + "' from " + xmlFile); | |
return ""; | |
} | |
return node.Value; | |
} | |
private static float ParseFloat(XPathNavigator nav, string xPath, string xmlFile, float defaultValue) | |
{ | |
float value = 0.0f; | |
var node = nav.SelectSingleNode(xPath); | |
if (node == null) | |
{ | |
Debug.LogWarning("could not read '" + xPath + "' from " + xmlFile); | |
return defaultValue; | |
} | |
var xmlValue = node.Value; | |
if (xmlValue != "") | |
{ | |
value = float.Parse(xmlValue, CultureInfo.InvariantCulture.NumberFormat); | |
} | |
return value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment