yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
7db318d
master
1#ifUNITY_EDITOR 2 3using UnityEngine ; 4using UnityEditor ; 5using System . IO ; 6 7public class GenerateMetallicGlossMap : EditorWindow 8{ 9private Texture2D metallicMap ; 10private Texture2D smoothnessMap ; 11private bool invertSmoothness = false ; 12 13[ MenuItem ( "Tools/yum_food/GenerateMetallicGlossMap" )] 14public static void ShowWindow () 15{ 16GetWindow < GenerateMetallicGlossMap > ( "Metallic Gloss Map Generator" ); 17} 18 19private void OnGUI () 20{ 21GUILayout . Label ( "Metallic Gloss Map Generator" , EditorStyles . boldLabel ); 22 23metallicMap = ( Texture2D ) EditorGUILayout . ObjectField ( 24"Metallic Map (R)" , metallicMap , typeof ( Texture2D ), false ); 25smoothnessMap = ( Texture2D ) EditorGUILayout . ObjectField ( 26"Smoothness Map (R)" , smoothnessMap , typeof ( Texture2D ), false ); 27 28invertSmoothness = EditorGUILayout . Toggle ( "Invert Smoothness" , invertSmoothness ); 29 30if ( GUILayout . Button ( "Generate Metallic Gloss Map" ) && smoothnessMap != null ) 31{ 32GenerateMap (); 33} 34} 35 36private void GenerateMap () 37{ 38// Get path and determine output location 39string directory = metallicMap != null 40? Path . GetDirectoryName ( AssetDatabase . GetAssetPath ( metallicMap )) 41: Path . GetDirectoryName ( AssetDatabase . GetAssetPath ( smoothnessMap )); 42string filename = metallicMap != null ? metallicMap . name : "black" ; 43string newPath = Path . Combine ( directory , "metallic_gloss.png" ); 44 45// Create new texture 46int width = metallicMap != null ? metallicMap . width : smoothnessMap . width ; 47int height = metallicMap != null ? metallicMap . height : smoothnessMap . height ; 48Texture2D combinedTexture = new Texture2D ( width , height , TextureFormat . RGBA32 , false ); 49 50// Make the smoothness texture readable 51TextureImporter smoothnessImporter = AssetImporter . GetAtPath ( AssetDatabase . GetAssetPath ( smoothnessMap )) as TextureImporter ; 52bool smoothnessReadable = smoothnessImporter . isReadable ; 53smoothnessImporter . isReadable = true ; 54AssetDatabase . ImportAsset ( AssetDatabase . GetAssetPath ( smoothnessMap )); 55 56// Handle metallic texture if it exists 57TextureImporter metallicImporter = null ; 58bool metallicReadable = false ; 59if ( metallicMap != null ) 60{ 61metallicImporter = AssetImporter . GetAtPath ( AssetDatabase . GetAssetPath ( metallicMap )) as TextureImporter ; 62metallicReadable = metallicImporter . isReadable ; 63metallicImporter . isReadable = true ; 64AssetDatabase . ImportAsset ( AssetDatabase . GetAssetPath ( metallicMap )); 65} 66 67// Get pixels 68Color [] smoothnessPixels = smoothnessMap . GetPixels (); 69Color [] metallicPixels = metallicMap != null ? metallicMap . GetPixels () : new Color [ smoothnessPixels . Length ]; 70Color [] newPixels = new Color [ smoothnessPixels . Length ]; 71 72// Combine channels (R from metallic or black, A from smoothness) 73for ( int i = 0 ; i < smoothnessPixels . Length ; i ++ ) 74{ 75float metallic = metallicMap != null ? metallicPixels [ i ]. r : 0f ; // Use 0 (black) if no metallic map 76float smoothness = invertSmoothness ? 1 - smoothnessPixels [ i ]. r : smoothnessPixels [ i ]. r ; 77newPixels [ i ] = new Color ( metallic , 0 , 0 , smoothness ); 78} 79 80// Apply pixels and save 81combinedTexture . SetPixels ( newPixels ); 82combinedTexture . Apply (); 83 84// Encode and save 85byte [] bytes = combinedTexture . EncodeToPNG (); 86File . WriteAllBytes ( newPath , bytes ); 87 88// Restore original import settings 89if ( metallicMap != null ) 90{ 91metallicImporter . isReadable = metallicReadable ; 92AssetDatabase . ImportAsset ( AssetDatabase . GetAssetPath ( metallicMap )); 93} 94smoothnessImporter . isReadable = smoothnessReadable ; 95AssetDatabase . ImportAsset ( AssetDatabase . GetAssetPath ( smoothnessMap )); 96AssetDatabase . ImportAsset ( newPath ); 97 98Debug . Log ( "Generated metallic gloss map at: " + newPath ); 99} 100} 101 102#endif// UNITY_EDITOR