summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x3ner.shader7
-rw-r--r--Scripts/MetallicGloss.cs113
-rw-r--r--decal.cginc8
-rwxr-xr-xfeatures.cginc1
-rwxr-xr-xglobals.cginc4
5 files changed, 133 insertions, 0 deletions
diff --git a/3ner.shader b/3ner.shader
index 9833950..eee9d2b 100755
--- a/3ner.shader
+++ b/3ner.shader
@@ -86,6 +86,13 @@ Shader "yum_food/3ner"
[HideInInspector] m_end_Decal0_Mask("Mask", Float) = 0
//endex
+ //ifex _Decal0_Metallic_Gloss_Enabled==0
+ [HideInInspector] m_start_Decal0_Metallic_Gloss("Metallic Gloss", Float) = 0
+ [ThryToggle(_DECAL0_METALLIC_GLOSS)] _Decal0_Metallic_Gloss_Enabled("Enable", Float) = 0
+ _Decal0_Metallic_Gloss("Metallic Gloss", 2D) = "white" {}
+ [HideInInspector] m_end_Decal0_Metallic_Gloss("Metallic Gloss", Float) = 0
+ //endex
+
//ifex _Decal0_Rotation_Enabled==0
[HideInInspector] m_start_Decal0_Rotation("Rotation", Float) = 0
[ThryToggle(_DECAL0_ROTATION)] _Decal0_Rotation_Enabled("Enable", Float) = 0
diff --git a/Scripts/MetallicGloss.cs b/Scripts/MetallicGloss.cs
new file mode 100644
index 0000000..d0b8895
--- /dev/null
+++ b/Scripts/MetallicGloss.cs
@@ -0,0 +1,113 @@
+using System.IO;
+using UnityEditor;
+using UnityEngine;
+
+public class MetallicGloss : EditorWindow
+{
+ Texture2D metallicMap;
+ Texture2D smoothnessMap;
+ bool invertMetallic;
+ bool invertSmoothness;
+
+ [MenuItem("Tools/yum_food/Metallic Gloss")]
+ static void ShowWindow()
+ {
+ var window = GetWindow<MetallicGloss>("Metallic Gloss");
+ window.minSize = new Vector2(300, 180);
+ }
+
+ void OnGUI()
+ {
+ EditorGUILayout.Space(5);
+
+ metallicMap = (Texture2D)EditorGUILayout.ObjectField("Metallic Map", metallicMap, typeof(Texture2D), false);
+ invertMetallic = EditorGUILayout.Toggle("Invert", invertMetallic);
+
+ EditorGUILayout.Space(5);
+
+ smoothnessMap = (Texture2D)EditorGUILayout.ObjectField("Smoothness Map", smoothnessMap, typeof(Texture2D), false);
+ invertSmoothness = EditorGUILayout.Toggle("Invert", invertSmoothness);
+
+ if (metallicMap != null) EnforceLinear(metallicMap);
+ if (smoothnessMap != null) EnforceLinear(smoothnessMap);
+
+ EditorGUILayout.Space(10);
+
+ using (new EditorGUI.DisabledScope(metallicMap == null && smoothnessMap == null))
+ {
+ if (GUILayout.Button("Merge"))
+ Merge();
+ }
+ }
+
+ static void EnforceLinear(Texture2D tex)
+ {
+ string path = AssetDatabase.GetAssetPath(tex);
+ if (!string.IsNullOrEmpty(path)) EnforceSettings(path);
+ }
+
+ static void EnforceSettings(string path)
+ {
+ var importer = AssetImporter.GetAtPath(path) as TextureImporter;
+ if (importer == null) return;
+ bool dirty = false;
+ if (importer.sRGBTexture) { importer.sRGBTexture = false; dirty = true; }
+ if (importer.textureType != TextureImporterType.Default) { importer.textureType = TextureImporterType.Default; dirty = true; }
+ if (importer.wrapMode != TextureWrapMode.Repeat) { importer.wrapMode = TextureWrapMode.Repeat; dirty = true; }
+ if (importer.filterMode != FilterMode.Trilinear) { importer.filterMode = FilterMode.Trilinear; dirty = true; }
+ if (importer.anisoLevel != 4) { importer.anisoLevel = 4; dirty = true; }
+ if (dirty) importer.SaveAndReimport();
+ }
+
+ void Merge()
+ {
+ Texture2D reference = metallicMap != null ? metallicMap : smoothnessMap;
+ int w = reference.width;
+ int h = reference.height;
+
+ Color[] mPixels = metallicMap != null ? ReadPixels(metallicMap, w, h) : null;
+ Color[] sPixels = smoothnessMap != null ? ReadPixels(smoothnessMap, w, h) : null;
+
+ var pixels = new Color[w * h];
+ for (int i = 0; i < pixels.Length; i++)
+ {
+ float m = mPixels != null ? mPixels[i].r : 0;
+ float s = sPixels != null ? sPixels[i].r : 0;
+ if (invertMetallic) m = 1 - m;
+ if (invertSmoothness) s = 1 - s;
+ pixels[i] = new Color(m, 0, 0, s);
+ }
+
+ var output = new Texture2D(w, h, TextureFormat.RGBA32, false, true);
+ output.SetPixels(pixels);
+ output.Apply();
+
+ string refPath = AssetDatabase.GetAssetPath(reference);
+ string dir = Path.GetDirectoryName(refPath);
+ string basename = Path.GetFileNameWithoutExtension(refPath);
+ string outputPath = Path.Combine(dir, basename + "_metallic_gloss.png");
+
+ File.WriteAllBytes(outputPath, output.EncodeToPNG());
+ DestroyImmediate(output);
+ AssetDatabase.ImportAsset(outputPath);
+
+ EnforceSettings(outputPath);
+ EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Texture2D>(outputPath));
+ }
+
+ static Color[] ReadPixels(Texture2D tex, int w, int h)
+ {
+ var rt = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
+ Graphics.Blit(tex, rt);
+ var prev = RenderTexture.active;
+ RenderTexture.active = rt;
+ var tmp = new Texture2D(w, h, TextureFormat.RGBA32, false, true);
+ tmp.ReadPixels(new Rect(0, 0, w, h), 0, 0);
+ tmp.Apply();
+ RenderTexture.active = prev;
+ RenderTexture.ReleaseTemporary(rt);
+ var pixels = tmp.GetPixels();
+ DestroyImmediate(tmp);
+ return pixels;
+ }
+}
diff --git a/decal.cginc b/decal.cginc
index 7673bee..8492c74 100644
--- a/decal.cginc
+++ b/decal.cginc
@@ -68,6 +68,14 @@ void applyDecals(v2f i, inout Pbr pbr) {
pbr.albedo.rgb += lerp(0, albedo.rgb * pbr.albedo.rgb, albedo.a);
break;
}
+
+#if defined(_DECAL0_METALLIC_GLOSS)
+ float4 mg = decal_sample(_Decal0_Metallic_Gloss, uv, _Decal0_UV_Mode);
+ pbr.metallic = lerp(pbr.metallic, mg.r, albedo.a);
+ pbr.smoothness = lerp(pbr.smoothness, mg.a, albedo.a);
+ pbr.roughness_perceptual = clamp(1 - pbr.smoothness, MIN_PERCEPTUAL_ROUGHNESS, 1);
+ pbr.roughness = clamp(pbr.roughness_perceptual * pbr.roughness_perceptual, MIN_ROUGHNESS, 1);
+#endif // _DECAL0_METALLIC_GLOSS
}
#endif
}
diff --git a/features.cginc b/features.cginc
index 796dcae..265c35f 100755
--- a/features.cginc
+++ b/features.cginc
@@ -161,6 +161,7 @@
//ifex _Decal0_Enabled==0
#pragma shader_feature_local _DECAL0
#pragma shader_feature_local _DECAL0_ALBEDO_CLAMP
+#pragma shader_feature_local _DECAL0_METALLIC_GLOSS
#pragma shader_feature_local _DECAL0_ROTATION
#pragma shader_feature_local _DECAL0_MASK
#pragma shader_feature_local _DECAL0_MASK_INVERT
diff --git a/globals.cginc b/globals.cginc
index 2daab12..263c07e 100755
--- a/globals.cginc
+++ b/globals.cginc
@@ -265,6 +265,10 @@ texture2D _Decal0_Mask;
float4 _Decal0_Mask_ST;
int _Decal0_Mask_UV_Channel;
#endif // _DECAL0_MASK
+#if defined(_DECAL0_METALLIC_GLOSS)
+texture2D _Decal0_Metallic_Gloss;
+float4 _Decal0_Metallic_Gloss_ST;
+#endif // _DECAL0_METALLIC_GLOSS
#endif // _DECAL0
#define MATCAP_MODE_REPLACE 0