You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

816 lines
28 KiB

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