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.

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