From 917e9e0b320775634dc2e710f7deac74fd0822f0 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 22 Oct 2020 23:30:02 +0800 Subject: * amplify shader editor --- Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs | 170 ++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs (limited to 'Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs') diff --git a/Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs b/Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs new file mode 100644 index 00000000..f8957a4a --- /dev/null +++ b/Assets/MaterializeFX/Scripts/MfxMaterialUtil.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace Assets.MaterializeFX.Scripts +{ + internal static class MfxMaterialUtil + { + private const string MfxShaderName = "QFX/MFX/Uber/Standart"; + + private const string Color2PropName = "_Color2"; + private const string MainTex2PropName = "_MainTex2"; + private const string BumpMap2PropName = "_BumpMap2"; + private const string EmissionColor2PropName = "_EmissionColor2"; + private const string EmissionMap2PropName = "_EmissionMap2"; + private const string EmissionMap2ScrollPropName = "_EmissionMap2_Scroll"; + private const string EmissionSize2PropName = "_EmissionSize2"; + private const string EdgeColorPropName = "_EdgeColor"; + private const string EdgeSizePropName = "_EdgeSize"; + private const string EdgeStrengthPropName = "_EdgeStrength"; + private const string EdgeRampMap1PropName = "_EdgeRampMap1"; + private const string EdgeRampMap1ScrollPropName = "_EdgeRampMap1_Scroll"; + private const string DissolveMap1PropName = "_DissolveMap1"; + private const string DissolveMap1ScrollPropName = "_DissolveMap1_Scroll"; + private const string MaskPropName = "_Mask"; + private const string CutoffAxisPropName = "_CutoffAxis"; + private const string MaskOffsetPropName = "_MaskOffset"; + private const string MaskPositionPropName = "_MaskPosition"; + private const string DissolveSizePropName = "_DissolveSize"; + private const string DissolveEdgeColorPropName = "_DissolveEdgeColor"; + private const string DissolveEdgeSizePropName = "_DissolveEdgeSize"; + + public static void CopyMfxProperties(Material mfxTemplateMaterial, Material emptyMfxMaterial) + { + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, Color2PropName, Color2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, MainTex2PropName, MainTex2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, BumpMap2PropName, BumpMap2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EmissionColor2PropName, EmissionColor2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EmissionMap2PropName, EmissionMap2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EmissionMap2ScrollPropName, EmissionMap2ScrollPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EmissionSize2PropName, EmissionSize2PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EdgeColorPropName, EdgeColorPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EdgeSizePropName, EdgeSizePropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EdgeStrengthPropName, EdgeStrengthPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EdgeRampMap1PropName, EdgeRampMap1PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, EdgeRampMap1ScrollPropName, EdgeRampMap1ScrollPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveMap1PropName, DissolveMap1PropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveMap1ScrollPropName, DissolveMap1ScrollPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, MaskPropName, MaskPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, CutoffAxisPropName, CutoffAxisPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, MaskOffsetPropName, MaskOffsetPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, MaskPositionPropName, MaskPositionPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveSizePropName, DissolveSizePropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveSizePropName, DissolveSizePropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveEdgeColorPropName, DissolveEdgeColorPropName); + CopyPropertyToMaterial(mfxTemplateMaterial, emptyMfxMaterial, DissolveEdgeSizePropName, DissolveEdgeSizePropName); + + emptyMfxMaterial.renderQueue = mfxTemplateMaterial.renderQueue; + emptyMfxMaterial.shaderKeywords = mfxTemplateMaterial.shaderKeywords; + emptyMfxMaterial.SetOverrideTag("RenderType", mfxTemplateMaterial.GetTag("RenderType", false)); + emptyMfxMaterial.SetOverrideTag("Queue", mfxTemplateMaterial.GetTag("Queue", false)); + emptyMfxMaterial.SetOverrideTag("IsEmissive", mfxTemplateMaterial.GetTag("IsEmissive", false)); + emptyMfxMaterial.SetOverrideTag("PerformanceChecks", mfxTemplateMaterial.GetTag("PerformanceChecks", false)); + emptyMfxMaterial.SetOverrideTag("DisableBatching", mfxTemplateMaterial.GetTag("DisableBatching", false)); + } + + public static void ReplaceRenderersMaterials(Material mfxMaterial, GameObject targetObject, bool editorMode) + { + var renderers = targetObject.GetComponentsInChildren(); + + foreach (var targetRenderer in renderers) + { + var targetRendererMaterials = targetRenderer.sharedMaterials; + var newMaterials = ReplaceMaterialsToMfx(mfxMaterial, targetRendererMaterials, editorMode); + targetRenderer.sharedMaterials = newMaterials.ToArray(); + } + } + + public static List ReplaceMaterialsToMfx(Material mfxMaterial, Material[] targetRendererMaterials, bool editorMode) + { + var newMaterials = new List(); + + foreach (var targetRendererMaterial in targetRendererMaterials) + { + if (targetRendererMaterial == null) + continue; + + string newAssetPath = string.Empty; + + if (editorMode) + { + var materialPath = AssetDatabase.GetAssetPath(targetRendererMaterial); + + var extensionIdx = materialPath.LastIndexOf("/", StringComparison.Ordinal); + if (extensionIdx <= 0) + { + Debug.LogError("the path is incorrect"); + continue; + } + + var pathWithoutExtension = materialPath.Substring(0, extensionIdx); + + newAssetPath = pathWithoutExtension + "/" + targetRendererMaterial.name + "_MFX.mat"; + + var assetType = AssetDatabase.GetMainAssetTypeAtPath(newAssetPath); + if (assetType != null) + { + var existingMaterial = AssetDatabase.LoadAssetAtPath(newAssetPath); + newMaterials.Add(existingMaterial); + continue; + } + } + + var newMaterial = new Material(targetRendererMaterial) + { + shader = Shader.Find(MfxShaderName) + }; + + CopyMfxProperties(mfxMaterial, newMaterial); + + newMaterials.Add(newMaterial); + + if (editorMode) + { + AssetDatabase.CreateAsset(newMaterial, newAssetPath); + } + } + + return newMaterials; + } + + private static void CopyPropertyToMaterial(Material fromMaterial, Material toMaterial, string fromProperty, string toProperty) + { + if (!fromMaterial.HasProperty(fromProperty) || !toMaterial.HasProperty(toProperty)) + return; + + var tType = typeof(T); + + if (tType == typeof(Texture)) + { + var tex = fromMaterial.GetTexture(fromProperty); + toMaterial.SetTexture(toProperty, tex); + + var texScale = fromMaterial.GetTextureScale(fromProperty); + toMaterial.SetTextureScale(toProperty, texScale); + } + else if (tType == typeof(Color)) + { + var col = fromMaterial.GetColor(fromProperty); + toMaterial.SetColor(toProperty, col); + } + else if (tType == typeof(float)) + { + var f = fromMaterial.GetFloat(fromProperty); + toMaterial.SetFloat(toProperty, f); + } + else if (tType == typeof(int)) + { + var f = fromMaterial.GetInt(fromProperty); + toMaterial.SetInt(toProperty, f); + } + else if (tType == typeof(Vector4)) + { + var f = fromMaterial.GetVector(fromProperty); + toMaterial.SetVector(toProperty, f); + } + } + } +} -- cgit v1.1-26-g67d0