Assignment for RMIT Mixed Reality in 2020
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.

770 lines
27 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // THIS FILE IS DEPRECATED AND SHOULD NOT BE USED
  4. #define SHOW_TEMPLATE_HELP_BOX
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEditor;
  9. namespace AmplifyShaderEditor
  10. {
  11. [Serializable]
  12. [NodeAttributes( "Template Master Node", "Master", "Shader Generated according to template rules", null, KeyCode.None, false, true, "Template MultiPass Master Node", typeof( TemplateMultiPassMasterNode ) )]
  13. public sealed class TemplateMasterNode : MasterNode
  14. {
  15. private const string WarningMessage = "Templates is a feature that is still heavily under development and users may experience some problems.\nPlease email support@amplify.pt if any issue occurs.";
  16. private const string CurrentTemplateLabel = "Current Template";
  17. private const string OpenTemplateStr = "Edit Template";
  18. //protected const string SnippetsFoldoutStr = " Snippets";
  19. //[SerializeField]
  20. //private bool m_snippetsFoldout = true;
  21. [NonSerialized]
  22. private TemplateData m_currentTemplate = null;
  23. private bool m_fireTemplateChange = false;
  24. private bool m_fetchMasterNodeCategory = false;
  25. private bool m_reRegisterTemplateData = false;
  26. [SerializeField]
  27. private string m_templateGUID = string.Empty;
  28. [SerializeField]
  29. private string m_templateName = string.Empty;
  30. [SerializeField]
  31. private TemplatesBlendModule m_blendOpHelper = new TemplatesBlendModule();
  32. [SerializeField]
  33. private TemplateAlphaToMaskModule m_alphaToMaskHelper = new TemplateAlphaToMaskModule();
  34. [SerializeField]
  35. private TemplateCullModeModule m_cullModeHelper = new TemplateCullModeModule();
  36. [SerializeField]
  37. private TemplateColorMaskModule m_colorMaskHelper = new TemplateColorMaskModule();
  38. [SerializeField]
  39. private TemplatesStencilBufferModule m_stencilBufferHelper = new TemplatesStencilBufferModule();
  40. [SerializeField]
  41. private TemplateDepthModule m_depthOphelper = new TemplateDepthModule();
  42. [SerializeField]
  43. private TemplateTagsModule m_tagsHelper = new TemplateTagsModule();
  44. protected override void CommonInit( int uniqueId )
  45. {
  46. base.CommonInit( uniqueId );
  47. m_masterNodeCategory = 1;// First Template
  48. m_marginPreviewLeft = 20;
  49. m_insideSize.y = 60;
  50. m_customPrecision = true;
  51. }
  52. public override void ReleaseResources()
  53. {
  54. if( m_currentTemplate != null && m_currentTemplate.AvailableShaderProperties != null )
  55. {
  56. // Unregister old template properties
  57. int oldPropertyCount = m_currentTemplate.AvailableShaderProperties.Count;
  58. for( int i = 0; i < oldPropertyCount; i++ )
  59. {
  60. UIUtils.ReleaseUniformName( UniqueId, m_currentTemplate.AvailableShaderProperties[ i ].PropertyName );
  61. }
  62. }
  63. }
  64. public override void OnEnable()
  65. {
  66. base.OnEnable();
  67. m_reRegisterTemplateData = true;
  68. }
  69. void FetchInfoFromTemplate()
  70. {
  71. if( m_currentTemplate.BlendData.DataCheck == TemplateDataCheck.Valid )
  72. m_blendOpHelper.ConfigureFromTemplateData( m_currentTemplate.BlendData );
  73. if( m_currentTemplate.AlphaToMaskData.DataCheck == TemplateDataCheck.Valid )
  74. m_alphaToMaskHelper.ConfigureFromTemplateData( m_currentTemplate.AlphaToMaskData );
  75. if( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid )
  76. m_cullModeHelper.ConfigureFromTemplateData( m_currentTemplate.CullModeData );
  77. if( m_currentTemplate.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  78. m_colorMaskHelper.ConfigureFromTemplateData( m_currentTemplate.ColorMaskData );
  79. if( m_currentTemplate.StencilData.DataCheck == TemplateDataCheck.Valid )
  80. m_stencilBufferHelper.ConfigureFromTemplateData( m_currentTemplate.StencilData );
  81. if( m_currentTemplate.DepthData.DataCheck == TemplateDataCheck.Valid )
  82. m_depthOphelper.ConfigureFromTemplateData( m_currentTemplate.DepthData );
  83. if( m_currentTemplate.TagData.DataCheck == TemplateDataCheck.Valid )
  84. m_tagsHelper.ConfigureFromTemplateData( m_currentTemplate.TagData );
  85. }
  86. void FetchCurrentTemplate()
  87. {
  88. m_currentTemplate = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplate( m_templateGUID ) as TemplateData;
  89. if( m_currentTemplate == null )
  90. {
  91. m_currentTemplate = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplate( m_templateName ) as TemplateData;
  92. }
  93. if( m_currentTemplate != null )
  94. {
  95. if( m_inputPorts.Count != m_currentTemplate.InputDataList.Count )
  96. {
  97. DeleteAllInputConnections( true );
  98. List<TemplateInputData> inputDataList = m_currentTemplate.InputDataList;
  99. int count = inputDataList.Count;
  100. for( int i = 0; i < count; i++ )
  101. {
  102. AddInputPort( inputDataList[ i ].DataType, false, inputDataList[ i ].PortName, inputDataList[ i ].OrderId, inputDataList[ i ].PortCategory, inputDataList[ i ].PortUniqueId );
  103. }
  104. FetchInfoFromTemplate();
  105. }
  106. else
  107. {
  108. List<TemplateInputData> inputDataList = m_currentTemplate.InputDataList;
  109. int count = inputDataList.Count;
  110. for( int i = 0; i < count; i++ )
  111. {
  112. m_inputPorts[ i ].ChangeProperties( inputDataList[ i ].PortName, inputDataList[ i ].DataType, false );
  113. }
  114. }
  115. }
  116. }
  117. public override void RefreshAvailableCategories()
  118. {
  119. FetchCurrentTemplate();
  120. int templateCount = m_containerGraph.ParentWindow.TemplatesManagerInstance.TemplateCount;
  121. m_availableCategories = new MasterNodeCategoriesData[ templateCount + 1 ];
  122. m_availableCategoryLabels = new GUIContent[ templateCount + 1 ];
  123. m_availableCategories[ 0 ] = new MasterNodeCategoriesData( AvailableShaderTypes.SurfaceShader, string.Empty );
  124. m_availableCategoryLabels[ 0 ] = new GUIContent( "Surface" );
  125. if( m_currentTemplate == null )
  126. {
  127. m_masterNodeCategory = -1;
  128. }
  129. for( int i = 0; i < templateCount; i++ )
  130. {
  131. int idx = i + 1;
  132. TemplateData templateData = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplate( i ) as TemplateData;
  133. if( m_currentTemplate != null && m_currentTemplate.GUID.Equals( templateData.GUID ) )
  134. m_masterNodeCategory = idx;
  135. m_availableCategories[ idx ] = new MasterNodeCategoriesData( AvailableShaderTypes.Template, templateData.GUID );
  136. m_availableCategoryLabels[ idx ] = new GUIContent( templateData.Name );
  137. }
  138. }
  139. void SetCategoryIdxFromTemplate()
  140. {
  141. int templateCount = m_containerGraph.ParentWindow.TemplatesManagerInstance.TemplateCount;
  142. for( int i = 0; i < templateCount; i++ )
  143. {
  144. int idx = i + 1;
  145. TemplateData templateData = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplate( i ) as TemplateData;
  146. if( templateData != null && m_currentTemplate != null && m_currentTemplate.GUID.Equals( templateData.GUID ) )
  147. m_masterNodeCategory = idx;
  148. }
  149. }
  150. public void SetTemplate( TemplateData newTemplate, bool writeDefaultData, bool fetchMasterNodeCategory )
  151. {
  152. ReleaseResources();
  153. if( newTemplate == null || newTemplate.InputDataList == null )
  154. return;
  155. m_fetchMasterNodeCategory = fetchMasterNodeCategory;
  156. DeleteAllInputConnections( true );
  157. m_currentTemplate = newTemplate;
  158. m_currentShaderData = newTemplate.Name;
  159. List<TemplateInputData> inputDataList = newTemplate.InputDataList;
  160. int count = inputDataList.Count;
  161. for( int i = 0; i < count; i++ )
  162. {
  163. AddInputPort( inputDataList[ i ].DataType, false, inputDataList[ i ].PortName, inputDataList[ i ].OrderId, inputDataList[ i ].PortCategory, inputDataList[ i ].PortUniqueId );
  164. }
  165. if( writeDefaultData )
  166. {
  167. ShaderName = newTemplate.DefaultShaderName;
  168. }
  169. RegisterProperties();
  170. m_fireTemplateChange = true;
  171. m_templateGUID = newTemplate.GUID;
  172. m_templateName = newTemplate.DefaultShaderName;
  173. FetchInfoFromTemplate();
  174. }
  175. void RegisterProperties()
  176. {
  177. if( m_currentTemplate != null )
  178. {
  179. m_reRegisterTemplateData = false;
  180. // Register old template properties
  181. int newPropertyCount = m_currentTemplate.AvailableShaderProperties.Count;
  182. for( int i = 0; i < newPropertyCount; i++ )
  183. {
  184. int nodeId = UIUtils.CheckUniformNameOwner( m_currentTemplate.AvailableShaderProperties[ i ].PropertyName );
  185. if( nodeId > -1 )
  186. {
  187. ParentNode node = m_containerGraph.GetNode( nodeId );
  188. if( node != null )
  189. {
  190. UIUtils.ShowMessage( string.Format( "Template requires property name {0} which is currently being used by {1}. Please rename it and reload template.", m_currentTemplate.AvailableShaderProperties[ i ].PropertyName, node.Attributes.Name ) );
  191. }
  192. else
  193. {
  194. UIUtils.ShowMessage( string.Format( "Template requires property name {0} which is currently being on your graph. Please rename it and reload template.", m_currentTemplate.AvailableShaderProperties[ i ].PropertyName ) );
  195. }
  196. }
  197. else
  198. {
  199. UIUtils.RegisterUniformName( UniqueId, m_currentTemplate.AvailableShaderProperties[ i ].PropertyName );
  200. }
  201. }
  202. }
  203. }
  204. public override void DrawProperties()
  205. {
  206. if( m_currentTemplate == null )
  207. return;
  208. base.DrawProperties();
  209. bool generalIsVisible = ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions;
  210. NodeUtils.DrawPropertyGroup( ref generalIsVisible, GeneralFoldoutStr, DrawGeneralOptions );
  211. ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions = generalIsVisible;
  212. if( m_currentTemplate.AlphaToMaskData.DataCheck == TemplateDataCheck.Valid )
  213. m_alphaToMaskHelper.Draw( this );
  214. if( m_currentTemplate.BlendData.DataCheck == TemplateDataCheck.Valid )
  215. m_blendOpHelper.Draw( this );
  216. if( m_currentTemplate.StencilData.DataCheck == TemplateDataCheck.Valid )
  217. {
  218. CullMode cullMode = ( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid ) ? m_cullModeHelper.CurrentCullMode : CullMode.Back;
  219. m_stencilBufferHelper.Draw( this, cullMode );
  220. }
  221. if( m_currentTemplate.DepthData.DataCheck == TemplateDataCheck.Valid )
  222. m_depthOphelper.Draw( this );
  223. if( m_currentTemplate.TagData.DataCheck == TemplateDataCheck.Valid )
  224. m_tagsHelper.Draw( this );
  225. DrawMaterialInputs( UIUtils.MenuItemToolbarStyle );
  226. // NodeUtils.DrawPropertyGroup( ref m_snippetsFoldout, SnippetsFoldoutStr, DrawSnippetOptions );
  227. if( GUILayout.Button( OpenTemplateStr ) && m_currentTemplate != null )
  228. {
  229. try
  230. {
  231. string pathname = AssetDatabase.GUIDToAssetPath( m_currentTemplate.GUID );
  232. if( !string.IsNullOrEmpty( pathname ) )
  233. {
  234. Shader selectedTemplate = AssetDatabase.LoadAssetAtPath<Shader>( pathname );
  235. if( selectedTemplate != null )
  236. {
  237. AssetDatabase.OpenAsset( selectedTemplate, 1 );
  238. }
  239. }
  240. }
  241. catch( Exception e )
  242. {
  243. Debug.LogException( e );
  244. }
  245. }
  246. #if SHOW_TEMPLATE_HELP_BOX
  247. EditorGUILayout.HelpBox( WarningMessage, MessageType.Warning );
  248. #endif
  249. }
  250. public void DrawGeneralOptions()
  251. {
  252. DrawShaderName();
  253. DrawCurrentShaderType();
  254. EditorGUI.BeginChangeCheck();
  255. DrawPrecisionProperty( false );
  256. if( EditorGUI.EndChangeCheck() )
  257. ContainerGraph.CurrentPrecision = m_currentPrecisionType;
  258. DrawSamplingMacros();
  259. if( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid )
  260. m_cullModeHelper.Draw( this );
  261. if( m_currentTemplate.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  262. m_colorMaskHelper.Draw( this );
  263. }
  264. //public void DrawSnippetOptions()
  265. //{
  266. // m_currentTemplate.DrawSnippetProperties( this );
  267. //}
  268. bool CreateInstructionsForList( ref List<InputPort> ports, ref string shaderBody, ref List<string> vertexInstructions, ref List<string> fragmentInstructions )
  269. {
  270. if( ports.Count == 0 )
  271. return true;
  272. bool isValid = true;
  273. UIUtils.CurrentWindow.CurrentGraph.ResetNodesLocalVariables();
  274. for( int i = 0; i < ports.Count; i++ )
  275. {
  276. TemplateInputData inputData = m_currentTemplate.InputDataFromId( ports[ i ].PortId );
  277. if( ports[ i ].IsConnected )
  278. {
  279. m_currentDataCollector.ResetInstructions();
  280. m_currentDataCollector.ResetVertexInstructions();
  281. m_currentDataCollector.PortCategory = ports[ i ].Category;
  282. string newPortInstruction = ports[ i ].GeneratePortInstructions( ref m_currentDataCollector );
  283. if( m_currentDataCollector.DirtySpecialLocalVariables )
  284. {
  285. string cleanVariables = m_currentDataCollector.SpecialLocalVariables.Replace( "\t", string.Empty );
  286. m_currentDataCollector.AddInstructions( cleanVariables, false );
  287. m_currentDataCollector.ClearSpecialLocalVariables();
  288. }
  289. if( m_currentDataCollector.DirtyVertexVariables )
  290. {
  291. string cleanVariables = m_currentDataCollector.VertexLocalVariables.Replace( "\t", string.Empty );
  292. m_currentDataCollector.AddVertexInstruction( cleanVariables, UniqueId, false );
  293. m_currentDataCollector.ClearVertexLocalVariables();
  294. }
  295. // fill functions
  296. for( int j = 0; j < m_currentDataCollector.InstructionsList.Count; j++ )
  297. {
  298. fragmentInstructions.Add( m_currentDataCollector.InstructionsList[ j ].PropertyName );
  299. }
  300. for( int j = 0; j < m_currentDataCollector.VertexDataList.Count; j++ )
  301. {
  302. vertexInstructions.Add( m_currentDataCollector.VertexDataList[ j ].PropertyName );
  303. }
  304. isValid = m_currentTemplate.FillTemplateBody( inputData.TagId, ref shaderBody, newPortInstruction ) && isValid;
  305. }
  306. else
  307. {
  308. isValid = m_currentTemplate.FillTemplateBody( inputData.TagId, ref shaderBody, inputData.DefaultValue ) && isValid;
  309. }
  310. }
  311. return isValid;
  312. }
  313. public override void Draw( DrawInfo drawInfo )
  314. {
  315. base.Draw( drawInfo );
  316. if( m_currentTemplate == null )
  317. {
  318. FetchCurrentTemplate();
  319. }
  320. if( m_reRegisterTemplateData )
  321. {
  322. RegisterProperties();
  323. }
  324. if( m_containerGraph.IsInstancedShader )
  325. {
  326. DrawInstancedIcon( drawInfo );
  327. }
  328. if( m_fetchMasterNodeCategory )
  329. {
  330. if( m_availableCategories != null )
  331. {
  332. m_fetchMasterNodeCategory = false;
  333. SetCategoryIdxFromTemplate();
  334. }
  335. }
  336. if( m_fireTemplateChange )
  337. {
  338. m_fireTemplateChange = false;
  339. m_containerGraph.FireMasterNodeReplacedEvent();
  340. }
  341. }
  342. public override void UpdateFromShader( Shader newShader )
  343. {
  344. if( m_currentMaterial != null )
  345. {
  346. m_currentMaterial.shader = newShader;
  347. }
  348. CurrentShader = newShader;
  349. }
  350. public override void UpdateMasterNodeMaterial( Material material )
  351. {
  352. m_currentMaterial = material;
  353. FireMaterialChangedEvt();
  354. }
  355. public override Shader Execute( string pathname, bool isFullPath )
  356. {
  357. if( m_currentTemplate == null )
  358. return m_currentShader;
  359. //Create data collector
  360. ForceReordering();
  361. base.Execute( pathname, isFullPath );
  362. SetupNodeCategories();
  363. m_currentDataCollector.TemplateDataCollectorInstance.BuildFromTemplateData( m_currentDataCollector, m_currentTemplate );
  364. int shaderPropertiesAmount = m_currentTemplate.AvailableShaderProperties.Count;
  365. for( int i = 0; i < shaderPropertiesAmount; i++ )
  366. {
  367. m_currentDataCollector.SoftRegisterUniform( m_currentTemplate.AvailableShaderProperties[ i ] );
  368. }
  369. m_containerGraph.CheckPropertiesAutoRegister( ref m_currentDataCollector );
  370. //Sort ports by both
  371. List<InputPort> fragmentPorts = new List<InputPort>();
  372. List<InputPort> vertexPorts = new List<InputPort>();
  373. SortInputPorts( ref vertexPorts, ref fragmentPorts );
  374. string shaderBody = m_currentTemplate.TemplateBody;
  375. List<string> vertexInstructions = new List<string>();
  376. List<string> fragmentInstructions = new List<string>();
  377. bool validBody = true;
  378. validBody = CreateInstructionsForList( ref fragmentPorts, ref shaderBody, ref vertexInstructions, ref fragmentInstructions ) && validBody;
  379. ContainerGraph.ResetNodesLocalVariablesIfNot( MasterNodePortCategory.Vertex );
  380. validBody = CreateInstructionsForList( ref vertexPorts, ref shaderBody, ref vertexInstructions, ref fragmentInstructions ) && validBody;
  381. m_currentTemplate.ResetTemplateUsageData();
  382. // Fill vertex interpolators assignment
  383. for( int i = 0; i < m_currentDataCollector.VertexInterpDeclList.Count; i++ )
  384. {
  385. vertexInstructions.Add( m_currentDataCollector.VertexInterpDeclList[ i ] );
  386. }
  387. vertexInstructions.AddRange( m_currentDataCollector.TemplateDataCollectorInstance.GetInterpUnusedChannels() );
  388. //Fill common local variables and operations
  389. validBody = m_currentTemplate.FillVertexInstructions( ref shaderBody, vertexInstructions.ToArray() ) && validBody;
  390. validBody = m_currentTemplate.FillFragmentInstructions( ref shaderBody, fragmentInstructions.ToArray() ) && validBody;
  391. // Add Instanced Properties
  392. if( m_containerGraph.IsInstancedShader )
  393. {
  394. m_currentDataCollector.TabifyInstancedVars();
  395. m_currentDataCollector.InstancedPropertiesList.Insert( 0, new PropertyDataCollector( -1, string.Format( IOUtils.InstancedPropertiesBegin, UIUtils.RemoveInvalidCharacters( m_shaderName ) ) ) );
  396. m_currentDataCollector.InstancedPropertiesList.Add( new PropertyDataCollector( -1, IOUtils.InstancedPropertiesEnd ) );
  397. m_currentDataCollector.UniformsList.AddRange( m_currentDataCollector.InstancedPropertiesList );
  398. }
  399. //Add Functions
  400. m_currentDataCollector.UniformsList.AddRange( m_currentDataCollector.FunctionsList );
  401. // Fill common tags
  402. m_currentDataCollector.IncludesList.AddRange( m_currentDataCollector.PragmasList );
  403. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.ShaderNameId, ref shaderBody, string.Format( TemplatesManager.NameFormatter, m_shaderName ) ) && validBody;
  404. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplatePassTag, ref shaderBody, m_currentDataCollector.GrabPassList ) && validBody;
  405. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplatePragmaTag, ref shaderBody, m_currentDataCollector.IncludesList ) && validBody;
  406. //validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplateTagsTag, ref shaderBody, m_currentDataCollector.TagsList ) && validBody;
  407. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplatePropertyTag, ref shaderBody, m_currentDataCollector.BuildUnformatedPropertiesStringArr() ) && validBody;
  408. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplateGlobalsTag, ref shaderBody, m_currentDataCollector.UniformsList ) && validBody;
  409. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.VertexDataId, ref shaderBody, m_currentDataCollector.VertexInputList.ToArray() ) && validBody;
  410. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.InterpDataId, ref shaderBody, m_currentDataCollector.InterpolatorList.ToArray() ) && validBody;
  411. if( m_currentTemplate.BlendData.ValidBlendMode )
  412. {
  413. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.BlendData.BlendModeId, ref shaderBody, m_blendOpHelper.CurrentBlendFactor ) && validBody;
  414. }
  415. if( m_currentTemplate.BlendData.ValidBlendOp )
  416. {
  417. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.BlendData.BlendOpId, ref shaderBody, m_blendOpHelper.CurrentBlendOp ) && validBody;
  418. }
  419. if( m_currentTemplate.AlphaToMaskData.DataCheck == TemplateDataCheck.Valid )
  420. {
  421. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.AlphaToMaskData.AlphaToMaskId, ref shaderBody, m_alphaToMaskHelper.GenerateShaderData( false ) ) && validBody;
  422. }
  423. if( m_currentTemplate.DepthData.ValidZWrite )
  424. {
  425. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.DepthData.ZWriteModeId, ref shaderBody, m_depthOphelper.CurrentZWriteMode ) && validBody;
  426. }
  427. if( m_currentTemplate.DepthData.ValidZTest )
  428. {
  429. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.DepthData.ZTestModeId, ref shaderBody, m_depthOphelper.CurrentZTestMode ) && validBody;
  430. }
  431. if( m_currentTemplate.DepthData.ValidOffset )
  432. {
  433. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.DepthData.OffsetId, ref shaderBody, m_depthOphelper.CurrentOffset ) && validBody;
  434. }
  435. if( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid )
  436. {
  437. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.CullModeData.CullModeId, ref shaderBody, m_cullModeHelper.GenerateShaderData(false) ) && validBody;
  438. }
  439. if( m_currentTemplate.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  440. {
  441. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.ColorMaskData.ColorMaskId, ref shaderBody, m_colorMaskHelper.GenerateShaderData( false ) ) && validBody;
  442. }
  443. if( m_currentTemplate.StencilData.DataCheck == TemplateDataCheck.Valid )
  444. {
  445. CullMode cullMode = ( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid ) ? m_cullModeHelper.CurrentCullMode : CullMode.Back;
  446. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.StencilData.StencilBufferId, ref shaderBody, m_stencilBufferHelper.CreateStencilOp( cullMode ) ) && validBody;
  447. }
  448. if( m_currentTemplate.TagData.DataCheck == TemplateDataCheck.Valid )
  449. {
  450. validBody = m_currentTemplate.FillTemplateBody( m_currentTemplate.TagData.TagsId, ref shaderBody, m_tagsHelper.GenerateTags() ) && validBody;
  451. }
  452. if( m_currentDataCollector.TemplateDataCollectorInstance.HasVertexInputParams )
  453. {
  454. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplateInputsVertParamsTag, ref shaderBody, m_currentDataCollector.TemplateDataCollectorInstance.VertexInputParamsStr ) && validBody;
  455. }
  456. if( m_currentDataCollector.TemplateDataCollectorInstance.HasFragmentInputParams )
  457. {
  458. validBody = m_currentTemplate.FillTemplateBody( TemplatesManager.TemplateInputsFragParamsTag, ref shaderBody, m_currentDataCollector.TemplateDataCollectorInstance.FragInputParamsStr ) && validBody;
  459. }
  460. m_currentTemplate.FillEmptyTags( ref shaderBody );
  461. //m_currentTemplate.InsertSnippets( ref shaderBody );
  462. vertexInstructions.Clear();
  463. vertexInstructions = null;
  464. fragmentInstructions.Clear();
  465. fragmentInstructions = null;
  466. if( validBody )
  467. {
  468. UpdateShaderAsset( ref pathname, ref shaderBody, isFullPath );
  469. }
  470. return m_currentShader;
  471. }
  472. public override void ReadFromString( ref string[] nodeParams )
  473. {
  474. base.ReadFromString( ref nodeParams );
  475. try
  476. {
  477. ShaderName = GetCurrentParam( ref nodeParams );
  478. if( m_shaderName.Length > 0 )
  479. ShaderName = UIUtils.RemoveShaderInvalidCharacters( ShaderName );
  480. string templateGUID = GetCurrentParam( ref nodeParams );
  481. string templateShaderName = string.Empty;
  482. if( UIUtils.CurrentShaderVersion() > 13601 )
  483. {
  484. templateShaderName = GetCurrentParam( ref nodeParams );
  485. }
  486. TemplateData template = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplate( templateGUID ) as TemplateData;
  487. if( template != null )
  488. {
  489. SetTemplate( template, false, true );
  490. }
  491. else
  492. {
  493. template = m_containerGraph.ParentWindow.TemplatesManagerInstance.GetTemplateByName( templateShaderName ) as TemplateData;
  494. if( template != null )
  495. {
  496. SetTemplate( template, false, true );
  497. }
  498. else
  499. {
  500. m_masterNodeCategory = -1;
  501. }
  502. }
  503. if( UIUtils.CurrentShaderVersion() > 13902 )
  504. {
  505. //BLEND MODULE
  506. if( m_currentTemplate.BlendData.ValidBlendMode )
  507. {
  508. m_blendOpHelper.ReadBlendModeFromString( ref m_currentReadParamIdx, ref nodeParams );
  509. }
  510. if( m_currentTemplate.BlendData.ValidBlendOp )
  511. {
  512. m_blendOpHelper.ReadBlendOpFromString( ref m_currentReadParamIdx, ref nodeParams );
  513. }
  514. //CULL MODE
  515. if( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid )
  516. {
  517. m_cullModeHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  518. }
  519. //COLOR MASK
  520. if( m_currentTemplate.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  521. {
  522. m_colorMaskHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  523. }
  524. //STENCIL BUFFER
  525. if( m_currentTemplate.StencilData.DataCheck == TemplateDataCheck.Valid )
  526. {
  527. m_stencilBufferHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  528. }
  529. }
  530. if( UIUtils.CurrentShaderVersion() > 14202 )
  531. {
  532. //DEPTH OPTIONS
  533. if( m_currentTemplate.DepthData.ValidZWrite )
  534. {
  535. m_depthOphelper.ReadZWriteFromString( ref m_currentReadParamIdx, ref nodeParams );
  536. }
  537. if( m_currentTemplate.DepthData.ValidZTest )
  538. {
  539. m_depthOphelper.ReadZTestFromString( ref m_currentReadParamIdx, ref nodeParams );
  540. }
  541. if( m_currentTemplate.DepthData.ValidOffset )
  542. {
  543. m_depthOphelper.ReadOffsetFromString( ref m_currentReadParamIdx, ref nodeParams );
  544. }
  545. }
  546. //TAGS
  547. if( UIUtils.CurrentShaderVersion() > 14301 )
  548. {
  549. if( m_currentTemplate.TagData.DataCheck == TemplateDataCheck.Valid )
  550. m_tagsHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  551. }
  552. if( UIUtils.CurrentShaderVersion() > 18302 )
  553. SamplingMacros = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  554. else
  555. SamplingMacros = false;
  556. }
  557. catch( Exception e )
  558. {
  559. Debug.LogException( e, this );
  560. }
  561. m_containerGraph.CurrentCanvasMode = NodeAvailability.TemplateShader;
  562. m_containerGraph.CurrentPrecision = m_currentPrecisionType;
  563. }
  564. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  565. {
  566. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  567. IOUtils.AddFieldValueToString( ref nodeInfo, m_shaderName );
  568. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_currentTemplate != null ) ? m_currentTemplate.GUID : string.Empty );
  569. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_currentTemplate != null ) ? m_currentTemplate.DefaultShaderName : string.Empty );
  570. //BLEND MODULE
  571. if( m_currentTemplate.BlendData.ValidBlendMode )
  572. {
  573. m_blendOpHelper.WriteBlendModeToString( ref nodeInfo );
  574. }
  575. if( m_currentTemplate.BlendData.ValidBlendOp )
  576. {
  577. m_blendOpHelper.WriteBlendOpToString( ref nodeInfo );
  578. }
  579. //CULL MODULE
  580. if( m_currentTemplate.CullModeData.DataCheck == TemplateDataCheck.Valid )
  581. {
  582. m_cullModeHelper.WriteToString( ref nodeInfo );
  583. }
  584. //COLOR MASK MODULE
  585. if( m_currentTemplate.ColorMaskData.DataCheck == TemplateDataCheck.Valid )
  586. {
  587. m_colorMaskHelper.WriteToString( ref nodeInfo );
  588. }
  589. //STENCIL BUFFER MODULE
  590. if( m_currentTemplate.StencilData.DataCheck == TemplateDataCheck.Valid )
  591. {
  592. m_stencilBufferHelper.WriteToString( ref nodeInfo );
  593. }
  594. //DEPTH MODULE
  595. if( m_currentTemplate.DepthData.ValidZWrite )
  596. {
  597. m_depthOphelper.WriteZWriteToString( ref nodeInfo );
  598. }
  599. if( m_currentTemplate.DepthData.ValidZTest )
  600. {
  601. m_depthOphelper.WriteZTestToString( ref nodeInfo );
  602. }
  603. if( m_currentTemplate.DepthData.ValidOffset )
  604. {
  605. m_depthOphelper.WriteOffsetToString( ref nodeInfo );
  606. }
  607. //TAGS
  608. if( m_currentTemplate.TagData.DataCheck == TemplateDataCheck.Valid )
  609. {
  610. m_tagsHelper.WriteToString( ref nodeInfo );
  611. }
  612. IOUtils.AddFieldValueToString( ref nodeInfo, m_samplingMacros );
  613. }
  614. public override void Destroy()
  615. {
  616. base.Destroy();
  617. m_currentTemplate = null;
  618. m_blendOpHelper = null;
  619. m_alphaToMaskHelper = null;
  620. m_cullModeHelper = null;
  621. m_colorMaskHelper.Destroy();
  622. m_colorMaskHelper = null;
  623. m_stencilBufferHelper.Destroy();
  624. m_stencilBufferHelper = null;
  625. m_tagsHelper.Destroy();
  626. m_tagsHelper = null;
  627. }
  628. public TemplateData CurrentTemplate { get { return m_currentTemplate; } }
  629. }
  630. }