yum/3ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum/3ner
f186fd5
master
1using System . IO ; 2using UnityEditor ; 3using UnityEngine ; 4 5public class MetallicGloss : EditorWindow 6{ 7Texture2D metallicMap ; 8Texture2D smoothnessMap ; 9bool invertMetallic ; 10bool invertSmoothness ; 11 12[ MenuItem ( "Tools/yum_food/Metallic Gloss" )] 13static void ShowWindow () 14{ 15var window = GetWindow < MetallicGloss > ( "Metallic Gloss" ); 16window . minSize = new Vector2 ( 300 , 180 ); 17} 18 19void OnGUI () 20{ 21EditorGUILayout . Space ( 5 ); 22 23metallicMap = ( Texture2D ) EditorGUILayout . ObjectField ( "Metallic Map" , metallicMap , typeof ( Texture2D ), false ); 24invertMetallic = EditorGUILayout . Toggle ( "Invert" , invertMetallic ); 25 26EditorGUILayout . Space ( 5 ); 27 28smoothnessMap = ( Texture2D ) EditorGUILayout . ObjectField ( "Smoothness Map" , smoothnessMap , typeof ( Texture2D ), false ); 29invertSmoothness = EditorGUILayout . Toggle ( "Invert" , invertSmoothness ); 30 31if ( metallicMap != null ) EnforceLinear ( metallicMap ); 32if ( smoothnessMap != null ) EnforceLinear ( smoothnessMap ); 33 34EditorGUILayout . Space ( 10 ); 35 36using ( new EditorGUI . DisabledScope ( metallicMap == null && smoothnessMap == null )) 37{ 38if ( GUILayout . Button ( "Merge" )) 39Merge (); 40} 41} 42 43static void EnforceLinear ( Texture2D tex ) 44{ 45string path = AssetDatabase . GetAssetPath ( tex ); 46if ( ! string . IsNullOrEmpty ( path )) EnforceSettings ( path ); 47} 48 49static void EnforceSettings ( string path ) 50{ 51var importer = AssetImporter . GetAtPath ( path ) as TextureImporter ; 52if ( importer == null ) return ; 53bool dirty = false ; 54if ( importer . sRGBTexture ) { importer . sRGBTexture = false ; dirty = true ; } 55if ( importer . textureType != TextureImporterType . Default ) { importer . textureType = TextureImporterType . Default ; dirty = true ; } 56if ( importer . wrapMode != TextureWrapMode . Repeat ) { importer . wrapMode = TextureWrapMode . Repeat ; dirty = true ; } 57if ( importer . filterMode != FilterMode . Trilinear ) { importer . filterMode = FilterMode . Trilinear ; dirty = true ; } 58if ( importer . anisoLevel != 4 ) { importer . anisoLevel = 4 ; dirty = true ; } 59if ( dirty ) importer . SaveAndReimport (); 60} 61 62void Merge () 63{ 64Texture2D reference = metallicMap != null ? metallicMap : smoothnessMap ; 65int w = reference . width ; 66int h = reference . height ; 67 68Color [] mPixels = metallicMap != null ? ReadPixels ( metallicMap , w , h ) : null ; 69Color [] sPixels = smoothnessMap != null ? ReadPixels ( smoothnessMap , w , h ) : null ; 70 71var pixels = new Color [ w * h ]; 72for ( int i = 0 ; i < pixels . Length ; i ++ ) 73{ 74float m = mPixels != null ? mPixels [ i ]. r : 0 ; 75float s = sPixels != null ? sPixels [ i ]. r : 0 ; 76if ( invertMetallic ) m = 1 - m ; 77if ( invertSmoothness ) s = 1 - s ; 78pixels [ i ] = new Color ( m , 0 , 0 , s ); 79} 80 81var output = new Texture2D ( w , h , TextureFormat . RGBA32 , false , true ); 82output . SetPixels ( pixels ); 83output . Apply (); 84 85string refPath = AssetDatabase . GetAssetPath ( reference ); 86string dir = Path . GetDirectoryName ( refPath ); 87string basename = Path . GetFileNameWithoutExtension ( refPath ); 88string outputPath = Path . Combine ( dir , basename + "_metallic_gloss.png" ); 89 90File . WriteAllBytes ( outputPath , output . EncodeToPNG ()); 91DestroyImmediate ( output ); 92AssetDatabase . ImportAsset ( outputPath ); 93 94EnforceSettings ( outputPath ); 95EditorGUIUtility . PingObject ( AssetDatabase . LoadAssetAtPath < Texture2D > ( outputPath )); 96} 97 98static Color [] ReadPixels ( Texture2D tex , int w , int h ) 99{ 100var rt = RenderTexture . GetTemporary ( w , h , 0 , RenderTextureFormat . ARGB32 , RenderTextureReadWrite . Linear ); 101Graphics . Blit ( tex , rt ); 102var prev = RenderTexture . active ; 103RenderTexture . active = rt ; 104var tmp = new Texture2D ( w , h , TextureFormat . RGBA32 , false , true ); 105tmp . ReadPixels ( new Rect ( 0 , 0 , w , h ), 0 , 0 ); 106tmp . Apply (); 107RenderTexture . active = prev ; 108RenderTexture . ReleaseTemporary ( rt ); 109var pixels = tmp . GetPixels (); 110DestroyImmediate ( tmp ); 111return pixels ; 112} 113}