1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
|
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Threading;
using UnityEditor.VersionControl;
namespace AmplifyShaderEditor
{
public enum ShaderLoadResult
{
LOADED,
TEMPLATE_LOADED,
FILE_NOT_FOUND,
ASE_INFO_NOT_FOUND,
UNITY_NATIVE_PATHS
}
public class Worker
{
public static readonly object locker = new object();
public void DoWork()
{
while ( IOUtils.ActiveThread )
{
if ( IOUtils.SaveInThreadFlag )
{
IOUtils.SaveInThreadFlag = false;
lock ( locker )
{
IOUtils.SaveInThreadShaderBody = IOUtils.ShaderCopywriteMessage + IOUtils.SaveInThreadShaderBody;
// Add checksum
string checksum = IOUtils.CreateChecksum( IOUtils.SaveInThreadShaderBody );
IOUtils.SaveInThreadShaderBody += IOUtils.CHECKSUM + IOUtils.VALUE_SEPARATOR + checksum;
// Write to disk
StreamWriter fileWriter = new StreamWriter( IOUtils.SaveInThreadPathName );
try
{
fileWriter.Write( IOUtils.SaveInThreadShaderBody );
Debug.Log( "Saving complete" );
}
catch ( Exception e )
{
Debug.LogException( e );
}
finally
{
fileWriter.Close();
}
}
}
}
Debug.Log( "Thread closed" );
}
}
public static class IOUtils
{
public delegate void OnShaderAction( Shader shader, bool isTemplate, string type );
public static OnShaderAction OnShaderSavedEvent;
public static OnShaderAction OnShaderTypeChangedEvent;
public static readonly string ShaderCopywriteMessage = "// Made with Amplify Shader Editor\n// Available at the Unity Asset Store - http://u3d.as/y3X \n";
public static readonly string GrabPassEmpty = "\t\tGrabPass{ }\n";
public static readonly string GrabPassBegin = "\t\tGrabPass{ \"";
public static readonly string GrabPassEnd = "\" }\n";
public static readonly string PropertiesBegin = "\tProperties\n\t{\n";
public static readonly string PropertiesEnd = "\t}\n";
public static readonly string PropertiesElement = "\t\t{0}\n";
public static readonly string PropertiesElementsRaw = "{0}\n";
public static readonly string PragmaTargetHeader = "\t\t#pragma target {0}\n";
public static readonly string InstancedPropertiesHeader = "multi_compile_instancing";
public static readonly string VirtualTexturePragmaHeader = "multi_compile _ _VT_SINGLE_MODE";
public static readonly string InstancedPropertiesBegin = "UNITY_INSTANCING_CBUFFER_START({0})";
public static readonly string InstancedPropertiesEnd = "UNITY_INSTANCING_CBUFFER_END";
public static readonly string InstancedPropertiesElement = "UNITY_DEFINE_INSTANCED_PROP({0}, {1})";
public static readonly string InstancedPropertiesData = "UNITY_ACCESS_INSTANCED_PROP({0})";
public static readonly string DotsInstancedPropertiesData = "\tUNITY_DOTS_INSTANCED_PROP({0}, {1})";
public static readonly string DotsInstancedDefinesData = "#define {1} UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO({0} , Metadata_{1})";
public static readonly string LWSRPInstancedPropertiesBegin = "UNITY_INSTANCING_BUFFER_START({0})";
public static readonly string LWSRPInstancedPropertiesEnd = "UNITY_INSTANCING_BUFFER_END({0})";
public static readonly string LWSRPInstancedPropertiesElement = "UNITY_DEFINE_INSTANCED_PROP({0}, {1})";
public static readonly string LWSRPInstancedPropertiesData = "UNITY_ACCESS_INSTANCED_PROP({0},{1})";
public static readonly string SRPCBufferPropertiesBegin = "CBUFFER_START( UnityPerMaterial )";//"CBUFFER_START({0})";
public static readonly string SRPCBufferPropertiesEnd = "CBUFFER_END";
public static readonly string InstancedPropertiesBeginTabs = "\t\t"+ InstancedPropertiesBegin + "\n";
public static readonly string InstancedPropertiesEndTabs = "\t\t"+ InstancedPropertiesEnd + "\n";
public static readonly string InstancedPropertiesElementTabs = "\t\t\t"+ InstancedPropertiesElement + "\n";
public static readonly string MetaBegin = "defaultTextures:";
public static readonly string MetaEnd = "userData:";
public static readonly string ShaderBodyBegin = "/*ASEBEGIN";
public static readonly string ShaderBodyEnd = "ASEEND*/";
//public static readonly float CurrentVersionFlt = 0.4f;
//public static readonly string CurrentVersionStr = "Version=" + CurrentVersionFlt;
public static readonly string CHECKSUM = "//CHKSM";
public static readonly string LAST_OPENED_OBJ_ID = "ASELASTOPENOBJID";
public static readonly string MAT_CLIPBOARD_ID = "ASEMATCLIPBRDID";
public static readonly char FIELD_SEPARATOR = ';';
public static readonly char VALUE_SEPARATOR = '=';
public static readonly char LINE_TERMINATOR = '\n';
public static readonly char VECTOR_SEPARATOR = ',';
public static readonly char FLOAT_SEPARATOR = '.';
public static readonly char CLIPBOARD_DATA_SEPARATOR = '|';
public static readonly char MATRIX_DATA_SEPARATOR = '|';
public readonly static string NO_TEXTURES = "<None>";
public static readonly string SaveShaderStr = "Please enter shader name to save";
public static readonly string FloatifyStr = ".0";
// Node parameter names
public const string NodeParam = "Node";
public const string NodePosition = "Position";
public const string NodeId = "Id";
public const string NodeType = "Type";
public const string WireConnectionParam = "WireConnection";
public static readonly uint NodeTypeId = 1;
public static readonly int InNodeId = 1;
public static readonly int InPortId = 2;
public static readonly int OutNodeId = 3;
public static readonly int OutPortId = 4;
public readonly static string DefaultASEDirtyCheckName = "__dirty";
public readonly static string DefaultASEDirtyCheckProperty = "[HideInInspector] " + DefaultASEDirtyCheckName + "( \"\", Int ) = 1";
public readonly static string DefaultASEDirtyCheckUniform = "uniform int " + DefaultASEDirtyCheckName + " = 1;";
public readonly static string MaskClipValueName = "_Cutoff";
public readonly static string MaskClipValueProperty = MaskClipValueName + "( \"{0}\", Float ) = {1}";
public readonly static string MaskClipValueUniform = "uniform float " + MaskClipValueName + " = {0};";
public readonly static string ChromaticAberrationProperty = "_ChromaticAberration";
//public static readonly string ASEFolderGUID = "daca988099666ec40aaa2cde22bb4935";
//public static string ASEResourcesPath = "/Plugins/EditorResources/";
//public static string ASEFolderPath;
//public static bool IsShaderFunctionWindow = false;
public static int DefaultASEDirtyCheckId;
// this is to be used in combination with AssetDatabase.GetAssetPath, both of these include the Assets/ path so we need to remove from one of them
public static string dataPath;
public static string EditorResourcesGUID = "0932db7ec1402c2489679c4b72eab5eb";
public static string GraphBgTextureGUID = "881c304491028ea48b5027ac6c62cf73";
public static string GraphFgTextureGUID = "8c4a7fca2884fab419769ccc0355c0c1";
public static string WireTextureGUID = "06e687f68dd96f0448c6d8217bbcf608";
public static string MasterNodeOnTextureGUID = "26c64fcee91024a49980ea2ee9d1a2fb";
public static string MasterNodeOffTextureGUID = "712aee08d999c16438e2d694f42428e8";
public static string GPUInstancedOnTextureGUID = "4b0c2926cc71c5846ae2a29652d54fb6";
public static string GPUInstancedOffTextureGUID = "486c7766baaf21b46afb63c1121ef03e";
public static string MainSkinGUID = "57482289c346f104a8162a3a79aaff9d";
public static string UpdateOutdatedGUID = "cce638be049286c41bcbd0a26c356b18";
public static string UpdateOFFGUID = "99d70ac09b4db9742b404c3f92d8564b";
public static string UpdateUpToDatedGUID = "ce30b12fbb3223746bcfef9ea82effe3";
public static string LiveOffGUID = "bb16faf366bcc6c4fbf0d7666b105354";
public static string LiveOnGUID = "6a0ae1d7892333142aeb09585572202c";
public static string LivePendingGUID = "e3182200efb67114eb5050f8955e1746";
public static string CleanupOFFGUID = "f62c0c3a5ddcd844e905fb2632fdcb15";
public static string CleanUpOnGUID = "615d853995cf2344d8641fd19cb09b5d";
public static string TakeScreenshotOFFGUID = "7587de2e3bec8bf4d973109524ccc6b1";
public static string TakeScreenshotONGUID = "7587de2e3bec8bf4d973109524ccc6b1";
public static string ShareOFFGUID = "bc5bd469748466a459badfab23915cb0";
public static string ShareONGUID = "bc5bd469748466a459badfab23915cb0";
public static string OpenSourceCodeOFFGUID = "f7e8834b42791124095a8b7f2d4daac2";
public static string OpenSourceCodeONGUID = "8b114792ff84f6546880c031eda42bc0";
public static string FocusNodeGUID = "da673e6179c67d346abb220a6935e359";
public static string FitViewGUID = "1def740f2314c6b4691529cadeee2e9c";
public static string ShowInfoWindowGUID = "77af20044e9766840a6be568806dc22e";
public static string ShowTipsWindowGUID = "066674048bbb1e64e8cdcc6c3b4abbeb";
public static string ShowConsoleWindowGUID = "9a81d7df8e62c044a9d1cada0c8a2131";
public static Dictionary<string, string> NodeTypeReplacer = new Dictionary<string, string>()
{
{"AmplifyShaderEditor.RotateAboutAxis", "AmplifyShaderEditor.RotateAboutAxisNode"},
{"GlobalArrayNode", "AmplifyShaderEditor.GlobalArrayNode"},
{"AmplifyShaderEditor.SimpleMaxOp", "AmplifyShaderEditor.SimpleMaxOpNode"},
{"AmplifyShaderEditor.SimpleMinNode", "AmplifyShaderEditor.SimpleMinOpNode"},
{"AmplifyShaderEditor.TFHCRemap", "AmplifyShaderEditor.TFHCRemapNode"},
{"AmplifyShaderEditor.TFHCPixelateUV", "AmplifyShaderEditor.TFHCPixelate"},
{"AmplifyShaderEditor.VirtualTexturePropertyNode", "AmplifyShaderEditor.VirtualTextureObject"}
};
private static readonly string AmplifyShaderEditorDefineSymbol = "AMPLIFY_SHADER_EDITOR";
/////////////////////////////////////////////////////////////////////////////
// THREAD IO UTILS
public static bool SaveInThreadFlag = false;
public static string SaveInThreadShaderBody;
public static string SaveInThreadPathName;
public static Thread SaveInThreadMainThread;
public static bool ActiveThread = true;
private static bool UseSaveThread = false;
private static bool Initialized = false;
public static bool FunctionNodeChanged = false;
public static List<AmplifyShaderEditorWindow> AllOpenedWindows = new List<AmplifyShaderEditorWindow>();
public static void StartSaveThread( string shaderBody, string pathName )
{
if( Provider.enabled && Provider.isActive )
{
Asset loadedAsset = Provider.GetAssetByPath( FileUtil.GetProjectRelativePath( pathName ) );
if( loadedAsset != null )
{
//Task statusTask = Provider.Status( loadedAsset );
//statusTask.Wait();
//if( Provider.CheckoutIsValid( statusTask.assetList[ 0 ] ) )
{
Task checkoutTask = Provider.Checkout( loadedAsset, CheckoutMode.Both );
checkoutTask.Wait();
}
}
}
if( UseSaveThread )
{
if ( !SaveInThreadFlag )
{
if ( SaveInThreadMainThread == null )
{
Worker worker = new Worker();
SaveInThreadMainThread = new Thread( worker.DoWork );
SaveInThreadMainThread.Start();
Debug.Log( "Thread created" );
}
SaveInThreadShaderBody = shaderBody;
SaveInThreadPathName = pathName;
SaveInThreadFlag = true;
}
}
else
{
SaveTextfileToDisk( shaderBody, pathName );
}
}
////////////////////////////////////////////////////////////////////////////
public static void SetAmplifyDefineSymbolOnBuildTargetGroup( BuildTargetGroup targetGroup )
{
string currData = PlayerSettings.GetScriptingDefineSymbolsForGroup( targetGroup );
if ( !currData.Contains( AmplifyShaderEditorDefineSymbol ) )
{
if ( string.IsNullOrEmpty( currData ) )
{
PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, AmplifyShaderEditorDefineSymbol );
}
else
{
if ( !currData[ currData.Length - 1 ].Equals( ';' ) )
{
currData += ';';
}
currData += AmplifyShaderEditorDefineSymbol;
PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, currData );
}
}
}
public static void RemoveAmplifyDefineSymbolOnBuildTargetGroup( BuildTargetGroup targetGroup )
{
string currData = PlayerSettings.GetScriptingDefineSymbolsForGroup( targetGroup );
if( currData.Contains( AmplifyShaderEditorDefineSymbol ) )
{
currData = currData.Replace( AmplifyShaderEditorDefineSymbol + ";", "" );
currData = currData.Replace( ";" + AmplifyShaderEditorDefineSymbol, "" );
currData = currData.Replace( AmplifyShaderEditorDefineSymbol, "" );
PlayerSettings.SetScriptingDefineSymbolsForGroup( targetGroup, currData );
}
}
public static void Init()
{
if ( !Initialized )
{
Initialized = true;
if( EditorPrefs.GetBool( Preferences.PrefDefineSymbol, true ) )
SetAmplifyDefineSymbolOnBuildTargetGroup( EditorUserBuildSettings.selectedBuildTargetGroup );
//Array BuildTargetGroupValues = Enum.GetValues( typeof( BuildTargetGroup ));
//for ( int i = 0; i < BuildTargetGroupValues.Length; i++ )
//{
// if( i != 0 && i != 15 && i != 16 )
// SetAmplifyDefineSymbolOnBuildTargetGroup( ( BuildTargetGroup ) BuildTargetGroupValues.GetValue( i ) );
//}
DefaultASEDirtyCheckId = Shader.PropertyToID( DefaultASEDirtyCheckName );
dataPath = Application.dataPath.Remove( Application.dataPath.Length - 6 );
//ASEFolderPath = AssetDatabase.GUIDToAssetPath( ASEFolderGUID );
//ASEResourcesPath = ASEFolderPath + ASEResourcesPath;
}
}
public static void DumpTemplateManagers()
{
for( int i = 0; i < AllOpenedWindows.Count; i++ )
{
if( AllOpenedWindows[ i ].TemplatesManagerInstance != null )
{
Debug.Log( AllOpenedWindows[ i ].titleContent.text + ": " + AllOpenedWindows[ i ].TemplatesManagerInstance.GetInstanceID() );
}
}
}
public static TemplatesManager FirstValidTemplatesManager
{
get
{
for( int i = 0; i < AllOpenedWindows.Count; i++ )
{
if( AllOpenedWindows[ i ].TemplatesManagerInstance != null )
{
return AllOpenedWindows[ i ].TemplatesManagerInstance;
}
}
return null;
}
}
public static void UpdateSFandRefreshWindows( AmplifyShaderFunction function )
{
for( int i = 0; i < AllOpenedWindows.Count; i++ )
{
AllOpenedWindows[ i ].LateRefreshAvailableNodes();
if( AllOpenedWindows[ i ].IsShaderFunctionWindow )
{
if( AllOpenedWindows[ i ].OpenedShaderFunction == function )
{
AllOpenedWindows[ i ].UpdateTabTitle();
}
}
}
}
public static void UpdateIO()
{
int windowCount = AllOpenedWindows.Count;
if ( windowCount == 0 )
{
EditorApplication.update -= IOUtils.UpdateIO;
return;
}
for ( int i = 0; i < AllOpenedWindows.Count; i++ )
{
if ( AllOpenedWindows[i] == EditorWindow.focusedWindow )
{
UIUtils.CurrentWindow = AllOpenedWindows[ i ];
}
if( FunctionNodeChanged )
AllOpenedWindows[ i ].CheckFunctions = true;
if ( AllOpenedWindows[ i ] == null )
{
AllOpenedWindows.RemoveAt( i );
i--;
}
}
if ( FunctionNodeChanged )
FunctionNodeChanged = false;
}
public static void Destroy()
{
ActiveThread = false;
if ( SaveInThreadMainThread != null )
{
SaveInThreadMainThread.Abort();
SaveInThreadMainThread = null;
}
}
public static void GetShaderName( out string shaderName, out string fullPathname, string defaultName, string customDatapath )
{
string currDatapath = String.IsNullOrEmpty( customDatapath ) ? Application.dataPath : customDatapath;
fullPathname = EditorUtility.SaveFilePanelInProject( "Select Shader to save", defaultName, "shader", SaveShaderStr, currDatapath );
if ( !String.IsNullOrEmpty( fullPathname ) )
{
shaderName = fullPathname.Remove( fullPathname.Length - 7 ); // -7 remove .shader extension
string[] subStr = shaderName.Split( '/' );
if ( subStr.Length > 0 )
{
shaderName = subStr[ subStr.Length - 1 ]; // Remove pathname
}
}
else
{
shaderName = string.Empty;
}
}
public static void AddTypeToString( ref string myString, string typeName )
{
myString += typeName;
}
public static void AddFieldToString( ref string myString, string fieldName, object fieldValue )
{
myString += FIELD_SEPARATOR + fieldName + VALUE_SEPARATOR + fieldValue;
}
public static void AddFieldValueToString( ref string myString, object fieldValue )
{
myString += FIELD_SEPARATOR + fieldValue.ToString();
}
public static void AddLineTerminator( ref string myString )
{
myString += LINE_TERMINATOR;
}
public static string CreateChecksum( string buffer )
{
SHA1 sha1 = SHA1.Create();
byte[] buf = System.Text.Encoding.UTF8.GetBytes( buffer );
byte[] hash = sha1.ComputeHash( buf, 0, buf.Length );
string hashstr = BitConverter.ToString( hash ).Replace( "-", "" );
return hashstr;
}
public static void SaveTextfileToDisk( string shaderBody, string pathName, bool addAdditionalInfo = true )
{
if ( addAdditionalInfo )
{
shaderBody = ShaderCopywriteMessage + shaderBody;
// Add checksum
string checksum = CreateChecksum( shaderBody );
shaderBody += CHECKSUM + VALUE_SEPARATOR + checksum;
}
// Write to disk
StreamWriter fileWriter = new StreamWriter( pathName );
try
{
fileWriter.Write( shaderBody );
}
catch ( Exception e )
{
Debug.LogException( e );
}
finally
{
fileWriter.Close();
}
}
public static string AddAdditionalInfo( string shaderBody )
{
shaderBody = ShaderCopywriteMessage + shaderBody;
string checksum = CreateChecksum( shaderBody );
shaderBody += CHECKSUM + VALUE_SEPARATOR + checksum;
return shaderBody;
}
public static string LoadTextFileFromDisk( string pathName )
{
string result = string.Empty;
if ( !string.IsNullOrEmpty( pathName ) && File.Exists( pathName ) )
{
StreamReader fileReader = null;
try
{
fileReader = new StreamReader( pathName );
result = fileReader.ReadToEnd();
}
catch ( Exception e )
{
Debug.LogException( e );
}
finally
{
if( fileReader != null)
fileReader.Close();
}
}
return result;
}
public static bool IsASEShader( Shader shader )
{
string datapath = AssetDatabase.GetAssetPath( shader );
if ( UIUtils.IsUnityNativeShader( datapath ) )
{
return false;
}
string buffer = LoadTextFileFromDisk( datapath );
if ( String.IsNullOrEmpty( buffer ) || !IOUtils.HasValidShaderBody( ref buffer ) )
{
return false;
}
return true;
}
public static bool IsShaderFunction( string functionInfo )
{
string buffer = functionInfo;
if ( String.IsNullOrEmpty( buffer ) || !IOUtils.HasValidShaderBody( ref buffer ) )
{
return false;
}
return true;
}
public static bool HasValidShaderBody( ref string shaderBody )
{
int shaderBodyBeginId = shaderBody.IndexOf( ShaderBodyBegin );
if ( shaderBodyBeginId > -1 )
{
int shaderBodyEndId = shaderBody.IndexOf( ShaderBodyEnd );
return ( shaderBodyEndId > -1 && shaderBodyEndId > shaderBodyBeginId );
}
return false;
}
public static int[] AllIndexesOf( this string str, string substr, bool ignoreCase = false )
{
if ( string.IsNullOrEmpty( str ) || string.IsNullOrEmpty( substr ) )
{
throw new ArgumentException( "String or substring is not specified." );
}
List<int> indexes = new List<int>();
int index = 0;
while ( ( index = str.IndexOf( substr, index, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal ) ) != -1 )
{
indexes.Add( index++ );
}
return indexes.ToArray();
}
public static void AddFunctionHeader( ref string function, string header )
{
function += "\t\t" + header + "\n\t\t{\n";
}
public static void AddSingleLineFunction( ref string function, string header )
{
function += "\t\t" + header;
}
public static void AddFunctionLine( ref string function, string line )
{
function += "\t\t\t" + line + "\n";
}
public static void CloseFunctionBody( ref string function )
{
function += "\t\t}\n";
}
public static string CreateFullFunction( string header, params string[] functionLines )
{
string result = string.Empty;
AddFunctionHeader( ref result, header );
for ( int i = 0; i > functionLines.Length; i++ )
{
AddFunctionLine( ref result, functionLines[ i ] );
}
CloseFunctionBody( ref result );
return result;
}
public static string CreateCodeComments( bool forceForwardSlash, params string[] comments )
{
string finalComment = string.Empty;
if ( comments.Length == 1 )
{
finalComment = "//" + comments[ 0 ];
}
else
{
if ( forceForwardSlash )
{
for ( int i = 0; i < comments.Length; i++ )
{
finalComment += "//" + comments[ i ];
if ( i < comments.Length - 1 )
{
finalComment += "\n\t\t\t";
}
}
}
else
{
finalComment = "/*";
for ( int i = 0; i < comments.Length; i++ )
{
if ( i != 0 )
finalComment += "\t\t\t";
finalComment += comments[ i ];
if ( i < comments.Length - 1 )
finalComment += "\n";
}
finalComment += "*/";
}
}
return finalComment;
}
public static string GetUVChannelDeclaration( string uvName, int channelId, int set )
{
string uvSetStr = ( set == 0 ) ? "uv" : "uv" + Constants.AvailableUVSetsStr[ set ];
return "float2 " + uvSetStr + uvName /*+ " : TEXCOORD" + channelId*/;
}
public static string GetUVChannelName( string uvName, int set )
{
string uvSetStr = ( set == 0 ) ? "uv" : "uv" + Constants.AvailableUVSetsStr[ set ];
return uvSetStr + uvName;
}
public static string GetVertexUVChannelName( int set )
{
string uvSetStr = ( set == 0 ) ? "texcoord" : ( "texcoord" + set.ToString() );
return uvSetStr;
}
public static string Floatify( float value )
{
return ( value % 1 ) != 0 ? value.ToString() : ( value.ToString() + FloatifyStr );
}
public static string Vector2ToString( Vector2 data )
{
return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString();
}
public static string Vector3ToString( Vector3 data )
{
return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString() + VECTOR_SEPARATOR + data.z.ToString();
}
public static string Vector4ToString( Vector4 data )
{
return data.x.ToString() + VECTOR_SEPARATOR + data.y.ToString() + VECTOR_SEPARATOR + data.z.ToString() + VECTOR_SEPARATOR + data.w.ToString();
}
public static string ColorToString( Color data )
{
return data.r.ToString() + VECTOR_SEPARATOR + data.g.ToString() + VECTOR_SEPARATOR + data.b.ToString() + VECTOR_SEPARATOR + data.a.ToString();
}
public static string Matrix3x3ToString( Matrix4x4 matrix )
{
return matrix[ 0, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
matrix[ 1, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
matrix[ 2, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2, 2 ].ToString();
}
public static string Matrix4x4ToString( Matrix4x4 matrix )
{
return matrix[ 0, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 0, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
matrix[ 1, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 1, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
matrix[ 2, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 2, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
matrix[ 3, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + matrix[ 3, 3 ].ToString();
}
public static Vector2 StringToVector2( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length >= 2 )
{
return new Vector2( Convert.ToSingle( parsedData[ 0 ] ),
Convert.ToSingle( parsedData[ 1 ] ) );
}
return Vector2.zero;
}
public static Vector3 StringToVector3( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length >= 3 )
{
return new Vector3( Convert.ToSingle( parsedData[ 0 ] ),
Convert.ToSingle( parsedData[ 1 ] ),
Convert.ToSingle( parsedData[ 2 ] ) );
}
return Vector3.zero;
}
public static Vector4 StringToVector4( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length >= 4 )
{
return new Vector4( Convert.ToSingle( parsedData[ 0 ] ),
Convert.ToSingle( parsedData[ 1 ] ),
Convert.ToSingle( parsedData[ 2 ] ),
Convert.ToSingle( parsedData[ 3 ] ) );
}
return Vector4.zero;
}
public static Color StringToColor( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length >= 4 )
{
return new Color( Convert.ToSingle( parsedData[ 0 ] ),
Convert.ToSingle( parsedData[ 1 ] ),
Convert.ToSingle( parsedData[ 2 ] ),
Convert.ToSingle( parsedData[ 3 ] ) );
}
return Color.white;
}
public static Matrix4x4 StringToMatrix3x3( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length == 9 )
{
Matrix4x4 matrix = new Matrix4x4();
matrix[ 0, 0 ] = Convert.ToSingle( parsedData[ 0 ] );
matrix[ 0, 1 ] = Convert.ToSingle( parsedData[ 1 ] );
matrix[ 0, 2 ] = Convert.ToSingle( parsedData[ 2 ] );
matrix[ 1, 0 ] = Convert.ToSingle( parsedData[ 3 ] );
matrix[ 1, 1 ] = Convert.ToSingle( parsedData[ 4 ] );
matrix[ 1, 2 ] = Convert.ToSingle( parsedData[ 5 ] );
matrix[ 2, 0 ] = Convert.ToSingle( parsedData[ 6 ] );
matrix[ 2, 1 ] = Convert.ToSingle( parsedData[ 7 ] );
matrix[ 2, 2 ] = Convert.ToSingle( parsedData[ 8 ] );
return matrix;
}
return Matrix4x4.identity;
}
public static Matrix4x4 StringToMatrix4x4( string data )
{
string[] parsedData = data.Split( VECTOR_SEPARATOR );
if ( parsedData.Length == 16 )
{
Matrix4x4 matrix = new Matrix4x4();
matrix[ 0, 0 ] = Convert.ToSingle( parsedData[ 0 ] );
matrix[ 0, 1 ] = Convert.ToSingle( parsedData[ 1 ] );
matrix[ 0, 2 ] = Convert.ToSingle( parsedData[ 2 ] );
matrix[ 0, 3 ] = Convert.ToSingle( parsedData[ 3 ] );
matrix[ 1, 0 ] = Convert.ToSingle( parsedData[ 4 ] );
matrix[ 1, 1 ] = Convert.ToSingle( parsedData[ 5 ] );
matrix[ 1, 2 ] = Convert.ToSingle( parsedData[ 6 ] );
matrix[ 1, 3 ] = Convert.ToSingle( parsedData[ 7 ] );
matrix[ 2, 0 ] = Convert.ToSingle( parsedData[ 8 ] );
matrix[ 2, 1 ] = Convert.ToSingle( parsedData[ 9 ] );
matrix[ 2, 2 ] = Convert.ToSingle( parsedData[ 10 ] );
matrix[ 2, 3 ] = Convert.ToSingle( parsedData[ 11 ] );
matrix[ 3, 0 ] = Convert.ToSingle( parsedData[ 12 ] );
matrix[ 3, 1 ] = Convert.ToSingle( parsedData[ 13 ] );
matrix[ 3, 2 ] = Convert.ToSingle( parsedData[ 14 ] );
matrix[ 3, 3 ] = Convert.ToSingle( parsedData[ 15 ] );
return matrix;
}
return Matrix4x4.identity;
}
public static void SaveTextureToDisk( Texture2D tex, string pathname )
{
byte[] rawData = tex.GetRawTextureData();
Texture2D newTex = new Texture2D( tex.width, tex.height, tex.format, tex.mipmapCount > 1, false );
newTex.LoadRawTextureData( rawData );
newTex.Apply();
byte[] pngData = newTex.EncodeToPNG();
File.WriteAllBytes( pathname, pngData );
}
//public static void SaveObjToList( string newObj )
//{
// Debug.Log( UIUtils.CurrentWindow.Lastpath );
// UIUtils.CurrentWindow.Lastpath = newObj;
// string lastOpenedObj = EditorPrefs.GetString( IOUtils.LAST_OPENED_OBJ_ID );
// string[] allLocations = lastOpenedObj.Split( ':' );
// string lastLocation = allLocations[ allLocations.Length - 1 ];
// string resave = string.Empty;
// for ( int i = 0; i < allLocations.Length; i++ )
// {
// if ( string.IsNullOrEmpty( allLocations[ i ] ) )
// continue;
// resave += allLocations[ i ];
// resave += ":";
// }
// resave += newObj;
// EditorPrefs.SetString( IOUtils.LAST_OPENED_OBJ_ID, resave );
//}
//public static void DeleteObjFromList( string newObj )
//{
// string lastOpenedObj = EditorPrefs.GetString( IOUtils.LAST_OPENED_OBJ_ID );
// string[] allLocations = lastOpenedObj.Split( ':' );
// string resave = string.Empty;
// for ( int i = 0; i < allLocations.Length; i++ )
// {
// if ( string.IsNullOrEmpty( allLocations[ i ] ) || newObj.Equals( allLocations[ i ] ) )
// continue;
// resave += allLocations[ i ];
// if ( i < allLocations.Length - 1 )
// resave += ":";
// }
// EditorPrefs.SetString( IOUtils.LAST_OPENED_OBJ_ID, resave );
//}
}
}
|