yum-archive/Tooner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/Tooner
9b52e08
master
1using UnityEngine ; 2using UnityEditor ; 3using System . Collections . Generic ; 4using System . Linq ; 5 6public class ImageSequenceToTexture3D : EditorWindow 7{ 8private string sourcePath = "" ; 9private string textureName = "Texture3DFromSequence" ; 10private FilterMode filterMode = FilterMode . Bilinear ; 11private TextureWrapMode wrapMode = TextureWrapMode . Repeat ; 12 13[ MenuItem ( "Tools/yum_food/Image Sequence to Texture3D" )] 14public static void ShowWindow () 15{ 16GetWindow < ImageSequenceToTexture3D > ( "Image Sequence to Texture3D" ); 17} 18 19private void OnGUI () 20{ 21GUILayout . Label ( "Image Sequence to Texture3D Converter" , EditorStyles . boldLabel ); 22EditorGUILayout . HelpBox ( "Select a folder containing the image sequence (sorted by filename)" , MessageType . Info ); 23 24EditorGUILayout . BeginHorizontal (); 25sourcePath = EditorGUILayout . TextField ( "Source Folder" , sourcePath ); 26if ( GUILayout . Button ( "Browse" , GUILayout . Width ( 60 ))) 27{ 28string path = EditorUtility . OpenFolderPanel ( "Select Image Sequence Folder" , "" , "" ); 29if ( ! string . IsNullOrEmpty ( path )) 30{ 31sourcePath = path ; 32} 33} 34EditorGUILayout . EndHorizontal (); 35 36textureName = EditorGUILayout . TextField ( "Texture Name" , textureName ); 37filterMode = ( FilterMode ) EditorGUILayout . EnumPopup ( "Filter Mode" , filterMode ); 38wrapMode = ( TextureWrapMode ) EditorGUILayout . EnumPopup ( "Wrap Mode" , wrapMode ); 39 40if ( GUILayout . Button ( "Generate 3D Texture" )) 41{ 42if ( ValidateInputs ()) 43{ 44Generate3DTexture (); 45} 46} 47} 48 49private bool ValidateInputs () 50{ 51if ( string . IsNullOrEmpty ( sourcePath )) 52{ 53EditorUtility . DisplayDialog ( "Error" , "Please select a source folder." , "OK" ); 54return false ; 55} 56 57string [] files = GetImageFiles (); 58if ( files . Length == 0 ) 59{ 60EditorUtility . DisplayDialog ( "Error" , "No supported image files found in the selected folder." , "OK" ); 61return false ; 62} 63 64// Load first image to check dimensions 65Texture2D firstImage = LoadImage ( files [ 0 ]); 66int width = firstImage . width ; 67int height = firstImage . height ; 68DestroyImmediate ( firstImage ); 69 70// Verify all images have the same dimensions 71for ( int i = 1 ; i < files . Length ; i ++ ) 72{ 73Texture2D img = LoadImage ( files [ i ]); 74if ( img . width != width || img . height != height ) 75{ 76DestroyImmediate ( img ); 77EditorUtility . DisplayDialog ( "Error" , 78$"All images must have the same dimensions. Expected { width } x { height } , but image { files [ i ]} is { img . width } x { img . height } " , 79"OK" ); 80return false ; 81} 82DestroyImmediate ( img ); 83} 84 85return true ; 86} 87 88private string [] GetImageFiles () 89{ 90string [] files = System . IO . Directory . GetFiles ( sourcePath , "*.*" ) 91. Where ( file=> file . ToLower (). EndsWith ( ".png" ) || 92file . ToLower (). EndsWith ( ".jpg" ) || 93file . ToLower (). EndsWith ( ".jpeg" )) 94. OrderBy ( file=> file ) 95. ToArray (); 96return files ; 97} 98 99private Texture2D LoadImage ( string path ) 100{ 101byte [] fileData = System . IO . File . ReadAllBytes ( path ); 102Texture2D tex = new Texture2D ( 2 , 2 ); 103tex . LoadImage ( fileData ); 104return tex ; 105} 106 107private void Generate3DTexture () 108{ 109string [] files = GetImageFiles (); 110if ( files . Length == 0 ) return ; 111 112Texture2D firstImage = LoadImage ( files [ 0 ]); 113int width = firstImage . width ; 114int height = firstImage . height ; 115int depth = files . Length ; 116DestroyImmediate ( firstImage ); 117 118// Create the 3D texture 119Texture3D texture3D = new Texture3D ( width , height , depth , TextureFormat . RGBA32 , false ); 120texture3D . filterMode = filterMode ; 121texture3D . wrapMode = wrapMode ; 122 123// Prepare the color array 124Color [] colors = new Color [ width * height * depth ]; 125 126// Copy the pixel data from each source image 127for ( int z = 0 ; z < depth ; z ++ ) 128{ 129Debug . Log ( $"Processing layer { z + 1 } of { depth } : { System . IO . Path . GetFileName ( files [ z ])} " ); 130Texture2D img = LoadImage ( files [ z ]); 131Color [] imageColors = img . GetPixels (); 132System . Array . Copy ( imageColors , 0 , colors , z * width * height , width * height ); 133DestroyImmediate ( img ); 134} 135 136texture3D . SetPixels ( colors ); 137texture3D . Apply (); 138 139// Save the texture asset 140string path = $"Assets/ { textureName } .asset" ; 141AssetDatabase . CreateAsset ( texture3D , path ); 142AssetDatabase . SaveAssets (); 143AssetDatabase . Refresh (); 144 145EditorUtility . DisplayDialog ( "Success" , $"3D texture generated and saved at { path } " , "OK" ); 146} 147}