yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
8b7ae8d
master
1// !! AI ARTIFACT !! 2// This code was originally generated by Claude 3.5 Sonnet. 3using UnityEngine ; 4using UnityEditor ; 5 6public enum NoiseType 7{ 8OneDimensional , 9TwoDimensional , 10ThreeDimensional , 11FourDimensional , 12NormalizedThreeDimensional 13} 14 15public class WhiteNoiseTextureGenerator : EditorWindow 16{ 17private int textureWidth = 32 ; 18private int textureHeight = 32 ; 19private int textureDepth = 32 ; 20private string textureName = "WhiteNoiseTexture" ; 21private NoiseType noiseType = NoiseType . ThreeDimensional ; 22 23// Domain warping parameters 24private bool enableDomainWarping = false ; 25private int domainWarpingOctaves = 2 ; 26private float domainWarpingStrength = 0.1f ; 27private float domainWarpingScale = 0.5f ; 28 29// FBM parameters 30private bool enableFBM = false ; 31private int fbmOctaves = 4 ; 32private float fbmLacunarity = 2.0f ; 33private float fbmGain = 0.5f ; 34 35[ MenuItem ( "Tools/yum_food/White Noise Texture Generator" )] 36public static void ShowWindow () 37{ 38GetWindow < WhiteNoiseTextureGenerator > ( "White Noise Texture Generator" ); 39} 40 41private void OnGUI () 42{ 43GUILayout . Label ( "White Noise Texture Generator" , EditorStyles . boldLabel ); 44 45textureWidth = EditorGUILayout . IntField ( "Texture Width" , textureWidth ); 46textureHeight = EditorGUILayout . IntField ( "Texture Height" , textureHeight ); 47textureDepth = EditorGUILayout . IntField ( "Texture Depth" , textureDepth ); 48textureName = EditorGUILayout . TextField ( "Texture Name" , textureName ); 49noiseType = ( NoiseType ) EditorGUILayout . EnumPopup ( "Noise Type" , noiseType ); 50 51EditorGUILayout . Space (); 52GUILayout . Label ( "Domain Warping" , EditorStyles . boldLabel ); 53enableDomainWarping = EditorGUILayout . Toggle ( "Enable Domain Warping" , enableDomainWarping ); 54 55if ( enableDomainWarping ) 56{ 57EditorGUI . indentLevel ++ ; 58domainWarpingOctaves = EditorGUILayout . IntSlider ( "Octaves" , domainWarpingOctaves , 1 , 10 ); 59domainWarpingStrength = EditorGUILayout . Slider ( "Strength" , domainWarpingStrength , 0f , 10f ); 60domainWarpingScale = EditorGUILayout . Slider ( "Scale" , domainWarpingScale , 0.1f , 10f ); 61EditorGUI . indentLevel -- ; 62} 63 64EditorGUILayout . Space (); 65GUILayout . Label ( "FBM (Fractal Brownian Motion)" , EditorStyles . boldLabel ); 66enableFBM = EditorGUILayout . Toggle ( "Enable FBM" , enableFBM ); 67 68if ( enableFBM ) 69{ 70EditorGUI . indentLevel ++ ; 71fbmOctaves = EditorGUILayout . IntSlider ( "Octaves" , fbmOctaves , 1 , 10 ); 72fbmLacunarity = EditorGUILayout . Slider ( "Lacunarity" , fbmLacunarity , 1.0f , 4.0f ); 73fbmGain = EditorGUILayout . Slider ( "Gain" , fbmGain , 0.1f , 1.0f ); 74EditorGUI . indentLevel -- ; 75} 76 77if ( GUILayout . Button ( "Generate Texture" )) 78{ 79if ( textureWidth <= 0 || textureHeight <= 0 || textureDepth <= 0 ) 80{ 81EditorUtility . DisplayDialog ( "Error" , "Texture dimensions must be greater than zero." , "OK" ); 82return ; 83} 84 85GenerateWhiteNoiseTexture (); 86} 87} 88 89private void GenerateWhiteNoiseTexture () 90{ 91TextureFormat format = GetTextureFormat (); 92Texture3D texture = new Texture3D ( textureWidth , textureHeight , textureDepth , format , false ); 93 94if ( enableFBM ) 95{ 96GenerateWithFBM ( texture ); 97} 98else if ( enableDomainWarping ) 99{ 100GenerateWithDomainWarping ( texture ); 101} 102else 103{ 104GenerateSimpleNoise ( texture ); 105} 106 107texture . Apply (); 108 109string path = $"Assets/ { textureName } .asset" ; 110 111// Check if asset already exists 112Texture3D existingTexture = AssetDatabase . LoadAssetAtPath < Texture3D > ( path ); 113if ( existingTexture != null ) 114{ 115// Copy new texture data to existing asset to preserve GUID 116EditorUtility . CopySerialized ( texture , existingTexture ); 117EditorUtility . SetDirty ( existingTexture ); 118AssetDatabase . SaveAssets (); 119AssetDatabase . Refresh (); 120EditorUtility . DisplayDialog ( "Success" , $"White noise texture updated at { path } " , "OK" ); 121} 122else 123{ 124// Create new asset 125AssetDatabase . CreateAsset ( texture , path ); 126AssetDatabase . SaveAssets (); 127AssetDatabase . Refresh (); 128EditorUtility . DisplayDialog ( "Success" , $"White noise texture generated and saved at { path } " , "OK" ); 129} 130} 131 132private void GenerateSimpleNoise ( Texture3D texture ) 133{ 134Color [] colors = new Color [ textureWidth * textureHeight * textureDepth ]; 135 136for ( int z = 0 ; z < textureDepth ; z ++ ) 137{ 138for ( int y = 0 ; y < textureHeight ; y ++ ) 139{ 140for ( int x = 0 ; x < textureWidth ; x ++ ) 141{ 142int index = x + y * textureWidth + z * textureWidth * textureHeight ; 143colors [ index ] = GenerateColor (); 144} 145} 146} 147 148texture . SetPixels ( colors ); 149} 150 151private void GenerateWithDomainWarping ( Texture3D texture ) 152{ 153// First pass: generate base noise 154Color [] baseColors = new Color [ textureWidth * textureHeight * textureDepth ]; 155for ( int i = 0 ; i < baseColors . Length ; i ++ ) 156{ 157baseColors [ i ] = GenerateColor (); 158} 159 160// Second pass: apply domain warping 161Color [] warpedColors = new Color [ textureWidth * textureHeight * textureDepth ]; 162 163for ( int z = 0 ; z < textureDepth ; z ++ ) 164{ 165for ( int y = 0 ; y < textureHeight ; y ++ ) 166{ 167for ( int x = 0 ; x < textureWidth ; x ++ ) 168{ 169Vector3 coord = new Vector3 ( 170( float ) x / textureWidth , 171( float ) y / textureHeight , 172( float ) z / textureDepth 173); 174 175// Apply domain warping 176for ( int octave = 0 ; octave < domainWarpingOctaves ; octave ++ ) 177{ 178Vector3 sampleCoord = coord * domainWarpingScale ; 179Color warpValue = SampleTexture ( baseColors , sampleCoord ); 180 181// Convert color to offset vector 182Vector3 offset = new Vector3 ( 183warpValue . r * 2f - 1f , 184warpValue . g * 2f - 1f , 185warpValue . b * 2f - 1f 186) * domainWarpingStrength ; 187 188coord += offset ; 189} 190 191// Sample final color at warped coordinate 192int index = x + y * textureWidth + z * textureWidth * textureHeight ; 193warpedColors [ index ] = SampleTexture ( baseColors , coord ); 194} 195} 196} 197 198texture . SetPixels ( warpedColors ); 199} 200 201private Color SampleTexture ( Color [] colors , Vector3 coord ) 202{ 203// Wrap coordinates 204coord . x = Mathf . Repeat ( coord . x , 1f ); 205coord . y = Mathf . Repeat ( coord . y , 1f ); 206coord . z = Mathf . Repeat ( coord . z , 1f ); 207 208// Convert to texture space 209float fx = coord . x * ( textureWidth - 1 ); 210float fy = coord . y * ( textureHeight - 1 ); 211float fz = coord . z * ( textureDepth - 1 ); 212 213// Trilinear interpolation 214int x0 = Mathf . FloorToInt ( fx ); 215int y0 = Mathf . FloorToInt ( fy ); 216int z0 = Mathf . FloorToInt ( fz ); 217int x1 = ( x0 + 1 ) % textureWidth ; 218int y1 = ( y0 + 1 ) % textureHeight ; 219int z1 = ( z0 + 1 ) % textureDepth ; 220 221float dx = fx - x0 ; 222float dy = fy - y0 ; 223float dz = fz - z0 ; 224 225// Apply smoothstep to interpolation parameters 226float sx = Mathf . SmoothStep ( 0 , 1 , dx ); 227float sy = Mathf . SmoothStep ( 0 , 1 , dy ); 228float sz = Mathf . SmoothStep ( 0 , 1 , dz ); 229 230// Sample 8 corners 231Color c000 = colors [ x0 + y0 * textureWidth + z0 * textureWidth * textureHeight ]; 232Color c001 = colors [ x0 + y0 * textureWidth + z1 * textureWidth * textureHeight ]; 233Color c010 = colors [ x0 + y1 * textureWidth + z0 * textureWidth * textureHeight ]; 234Color c011 = colors [ x0 + y1 * textureWidth + z1 * textureWidth * textureHeight ]; 235Color c100 = colors [ x1 + y0 * textureWidth + z0 * textureWidth * textureHeight ]; 236Color c101 = colors [ x1 + y0 * textureWidth + z1 * textureWidth * textureHeight ]; 237Color c110 = colors [ x1 + y1 * textureWidth + z0 * textureWidth * textureHeight ]; 238Color c111 = colors [ x1 + y1 * textureWidth + z1 * textureWidth * textureHeight ]; 239 240// Interpolate with smoothstepped values 241Color c00 = Color . Lerp ( c000 , c001 , sz ); 242Color c01 = Color . Lerp ( c010 , c011 , sz ); 243Color c10 = Color . Lerp ( c100 , c101 , sz ); 244Color c11 = Color . Lerp ( c110 , c111 , sz ); 245 246Color c0 = Color . Lerp ( c00 , c01 , sy ); 247Color c1 = Color . Lerp ( c10 , c11 , sy ); 248 249return Color . Lerp ( c0 , c1 , sx ); 250} 251 252private Color SampleTextureCustomSize ( Color [] colors , Vector3 coord , int width , int height , int depth ) 253{ 254// Convert to texture space 255float fx = coord . x * ( width ); 256float fy = coord . y * ( height ); 257float fz = coord . z * ( depth ); 258 259// Trilinear interpolation 260int x0 = Mathf . FloorToInt ( fx ); 261int y0 = Mathf . FloorToInt ( fy ); 262int z0 = Mathf . FloorToInt ( fz ); 263int x1 = ( x0 + 1 ) % width ; 264int y1 = ( y0 + 1 ) % height ; 265int z1 = ( z0 + 1 ) % depth ; 266 267float dx = fx - x0 ; 268float dy = fy - y0 ; 269float dz = fz - z0 ; 270 271// Apply smoothstep to interpolation parameters 272float sx = Mathf . SmoothStep ( 0 , 1 , dx ); 273float sy = Mathf . SmoothStep ( 0 , 1 , dy ); 274float sz = Mathf . SmoothStep ( 0 , 1 , dz ); 275 276// Sample 8 corners 277Color c000 = colors [ x0 + y0 * width + z0 * width * height ]; 278Color c001 = colors [ x0 + y0 * width + z1 * width * height ]; 279Color c010 = colors [ x0 + y1 * width + z0 * width * height ]; 280Color c011 = colors [ x0 + y1 * width + z1 * width * height ]; 281Color c100 = colors [ x1 + y0 * width + z0 * width * height ]; 282Color c101 = colors [ x1 + y0 * width + z1 * width * height ]; 283Color c110 = colors [ x1 + y1 * width + z0 * width * height ]; 284Color c111 = colors [ x1 + y1 * width + z1 * width * height ]; 285 286// Interpolate with smoothstepped values 287Color c00 = Color . Lerp ( c000 , c001 , sz ); 288Color c01 = Color . Lerp ( c010 , c011 , sz ); 289Color c10 = Color . Lerp ( c100 , c101 , sz ); 290Color c11 = Color . Lerp ( c110 , c111 , sz ); 291 292Color c0 = Color . Lerp ( c00 , c01 , sy ); 293Color c1 = Color . Lerp ( c10 , c11 , sy ); 294 295return Color . Lerp ( c0 , c1 , sx ); 296} 297 298private Color [] ApplyDomainWarpingToColors ( Color [] baseColors , int width , int height , int depth ) 299{ 300Color [] warpedColors = new Color [ baseColors . Length ]; 301 302for ( int z = 0 ; z < depth ; z ++ ) 303{ 304for ( int y = 0 ; y < height ; y ++ ) 305{ 306for ( int x = 0 ; x < width ; x ++ ) 307{ 308Vector3 coord = new Vector3 ( 309( float ) x / width , 310( float ) y / height , 311( float ) z / depth 312); 313 314// Apply domain warping 315for ( int octave = 0 ; octave < domainWarpingOctaves ; octave ++ ) 316{ 317Vector3 sampleCoord = coord * domainWarpingScale ; 318Color warpValue = SampleTextureCustomSize ( baseColors , sampleCoord , width , height , depth ); 319 320// Convert color to offset vector 321Vector3 offset = new Vector3 ( 322warpValue . r * 2f - 1f , 323warpValue . g * 2f - 1f , 324warpValue . b * 2f - 1f 325) * domainWarpingStrength ; 326 327coord += offset ; 328} 329 330// Sample final color at warped coordinate 331int index = x + y * width + z * width * height ; 332warpedColors [ index ] = SampleTextureCustomSize ( baseColors , coord , width , height , depth ); 333} 334} 335} 336 337return warpedColors ; 338} 339 340private TextureFormat GetTextureFormat () 341{ 342switch ( noiseType ) 343{ 344case NoiseType . OneDimensional : 345return TextureFormat . R8 ; 346case NoiseType . TwoDimensional : 347return TextureFormat . RG16 ; 348case NoiseType . ThreeDimensional : 349case NoiseType . NormalizedThreeDimensional : 350return TextureFormat . RGB48 ; 351case NoiseType . FourDimensional : 352return TextureFormat . RGBA32 ; 353default : 354return TextureFormat . RGB24 ; 355} 356} 357 358private Color GenerateColor () 359{ 360switch ( noiseType ) 361{ 362case NoiseType . OneDimensional : 363return new Color ( Random . value , 0 , 0 , 1 ); 364case NoiseType . TwoDimensional : 365return new Color ( Random . value , Random . value , 0 , 1 ); 366case NoiseType . ThreeDimensional : 367return new Color ( Random . value , Random . value , Random . value , 1 ); 368case NoiseType . FourDimensional : 369return new Color ( Random . value , Random . value , Random . value , Random . value ); 370case NoiseType . NormalizedThreeDimensional : 371Vector3 normalizedColor = Random . insideUnitSphere . normalized ; 372return new Color ( normalizedColor . x * 0.5f + 0.5f , normalizedColor . y * 0.5f + 0.5f , normalizedColor . z * 0.5f + 0.5f , 1 ); 373default : 374return Color . white ; 375} 376} 377 378private void GenerateWithFBM ( Texture3D texture ) 379{ 380// Calculate starting resolution based on target size and octaves 381float scaleFactor = Mathf . Pow ( fbmLacunarity , fbmOctaves - 1 ); 382int currentWidth = Mathf . Max ( 1 , Mathf . RoundToInt ( textureWidth / scaleFactor )); 383int currentHeight = Mathf . Max ( 1 , Mathf . RoundToInt ( textureHeight / scaleFactor )); 384int currentDepth = Mathf . Max ( 1 , Mathf . RoundToInt ( textureDepth / scaleFactor )); 385 386// Track previous dimensions 387int prevWidth = currentWidth ; 388int prevHeight = currentHeight ; 389int prevDepth = currentDepth ; 390 391Color [] baseColors = null ; 392float amplitude = 1.0f ; 393float maxAmplitude = 0.0f ; 394 395for ( int octave = 0 ; octave < fbmOctaves ; octave ++ ) 396{ 397// Generate noise at current resolution 398Color [] octaveColors = new Color [ currentWidth * currentHeight * currentDepth ]; 399for ( int i = 0 ; i < octaveColors . Length ; i ++ ) { 400octaveColors [ i ] = GenerateColor (); 401} 402 403if ( baseColors == null ) { 404baseColors = octaveColors ; 405} else { 406for ( int z = 0 ; z < currentDepth ; z ++ ) 407for ( int y = 0 ; y < currentHeight ; y ++ ) 408for ( int x = 0 ; x < currentWidth ; x ++ ) 409{ 410int index = x + y * currentWidth + z * currentWidth * currentHeight ; 411Vector3 coord = new Vector3 ( 412( float ) x / currentWidth , 413( float ) y / currentHeight , 414( float ) z / currentDepth 415); 416Color prevColor = SampleTextureCustomSize ( baseColors , coord , prevWidth , prevHeight , prevDepth ); 417octaveColors [ index ] = prevColor + octaveColors [ index ] * amplitude ; 418} 419} 420 421baseColors = octaveColors ; 422 423maxAmplitude += amplitude ; 424amplitude *= fbmGain ; 425 426// Store current dimensions as previous before updating 427prevWidth = currentWidth ; 428prevHeight = currentHeight ; 429prevDepth = currentDepth ; 430 431// Increase resolution for next octave 432if ( octave < fbmOctaves - 1 ) 433{ 434currentWidth = Mathf . Min ( textureWidth , Mathf . RoundToInt ( currentWidth * fbmLacunarity )); 435currentHeight = Mathf . Min ( textureHeight , Mathf . RoundToInt ( currentHeight * fbmLacunarity )); 436currentDepth = Mathf . Min ( textureDepth , Mathf . RoundToInt ( currentDepth * fbmLacunarity )); 437} 438} 439 440// Ensure final resolution matches target 441if ( currentWidth != textureWidth || currentHeight != textureHeight || currentDepth != textureDepth ) 442{ 443Color [] finalColors = new Color [ textureWidth * textureHeight * textureDepth ]; 444for ( int z = 0 ; z < textureDepth ; z ++ ) 445{ 446for ( int y = 0 ; y < textureHeight ; y ++ ) 447{ 448for ( int x = 0 ; x < textureWidth ; x ++ ) 449{ 450int index = x + y * textureWidth + z * textureWidth * textureHeight ; 451Vector3 coord = new Vector3 ( 452( float ) x / textureWidth , 453( float ) y / textureHeight , 454( float ) z / textureDepth 455); 456finalColors [ index ] = SampleTextureCustomSize ( baseColors , coord , currentWidth , currentHeight , currentDepth ); 457} 458} 459} 460baseColors = finalColors ; 461} 462 463// Normalize 464for ( int i = 0 ; i < baseColors . Length ; i ++ ) 465{ 466baseColors [ i ] /= maxAmplitude ; 467baseColors [ i ]. a = 1.0f ; 468} 469 470texture . SetPixels ( baseColors ); 471} 472 473private Color GeneratePerlinColor ( float x , float y , float z , float scale = 1f ) 474{ 475switch ( noiseType ) 476{ 477case NoiseType . OneDimensional : 478return new Color ( Mathf . PerlinNoise ( x * scale , 0 ), 0 , 0 , 1 ); 479case NoiseType . TwoDimensional : 480return new Color ( 481Mathf . PerlinNoise ( x * scale , y * scale ), 482Mathf . PerlinNoise ( x * scale + 100 , y * scale + 100 ), 4830 , 1 ); 484case NoiseType . ThreeDimensional : 485return new Color ( 486Mathf . PerlinNoise ( x * scale , y * scale ), 487Mathf . PerlinNoise ( x * scale + 100 , z * scale ), 488Mathf . PerlinNoise ( y * scale + 200 , z * scale + 200 ), 4891 ); 490case NoiseType . FourDimensional : 491return new Color ( 492Mathf . PerlinNoise ( x * scale , y * scale ), 493Mathf . PerlinNoise ( x * scale + 100 , z * scale ), 494Mathf . PerlinNoise ( y * scale + 200 , z * scale + 200 ), 495Mathf . PerlinNoise ( x * scale + 300 , y * scale + 300 )); 496default : 497return Color . white ; 498} 499} 500}