TemplateShaderPropertyNode.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum ShaderPropertyScope
  10. {
  11. Shader,
  12. SubShader,
  13. Pass
  14. }
  15. [Serializable]
  16. [NodeAttributes( "Template Parameter" , "Constants And Properties" , "Select and use one of the pre-existing properties given by the template" )]
  17. public sealed class TemplateShaderPropertyNode : TemplateNodeParent
  18. {
  19. private const string CurrentScopeStr = "Scope";
  20. private const string WarningStr = "Preview doesn't work with global variables";
  21. private const string PropertyLabelStr = "Parameter";
  22. private const string TypeLabelStr = "Type: ";
  23. private const string PropertyNameStr = "Property Name: ";
  24. private int IntPropertyId;
  25. private int FloatPropertyId;
  26. private int VectorPropertyId;
  27. private int Sampler2DPropertyId;
  28. private int Sampler3DPropertyId;
  29. private int SamplerCubePropertyId;
  30. [SerializeField]
  31. private int m_currentPropertyIdx = -1;
  32. [SerializeField]
  33. private string m_propertyName = string.Empty;
  34. [SerializeField]
  35. private int m_propertyNameId = 0;
  36. [SerializeField]
  37. private string m_typeName = string.Empty;
  38. [SerializeField]
  39. private string m_propertyNameLabel = string.Empty;
  40. private bool m_fetchPropertyId = false;
  41. private bool m_fetchScopeFromProperty = false;
  42. private List<TemplateShaderPropertyData> m_shaderProperties = null;
  43. private string[] m_propertyLabels = null;
  44. private UpperLeftWidgetHelper m_upperLeftWidgetHelper = new UpperLeftWidgetHelper();
  45. //Multi-Pass only properties
  46. [SerializeField]
  47. private ShaderPropertyScope m_currentScope = ShaderPropertyScope.Shader;
  48. [SerializeField]
  49. private bool m_advancedView = false;
  50. protected override void CommonInit( int uniqueId )
  51. {
  52. base.CommonInit( uniqueId );
  53. m_previewShaderGUID = "4feb2016be0ece148b8bf234508f6aa4";
  54. m_autoWrapProperties = true;
  55. }
  56. void FetchScope()
  57. {
  58. int shaderScopeCount = m_templateMPData.AvailableShaderProperties.Count;
  59. for( int i = 0 ; i < shaderScopeCount ; i++ )
  60. {
  61. if( m_templateMPData.AvailableShaderProperties[ i ].PropertyName.Equals( m_propertyName ) )
  62. {
  63. m_currentScope = ShaderPropertyScope.Shader;
  64. }
  65. }
  66. int subShaderScopeCount = m_templateMPData.SubShaders[ SubShaderIdx ].AvailableShaderGlobals.Count;
  67. for( int i = 0 ; i < subShaderScopeCount ; i++ )
  68. {
  69. if( m_templateMPData.SubShaders[ SubShaderIdx ].AvailableShaderGlobals[ i ].PropertyName.Equals( m_propertyName ) )
  70. {
  71. m_currentScope = ShaderPropertyScope.SubShader;
  72. }
  73. }
  74. int passScopeCount = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].AvailableShaderGlobals.Count;
  75. for( int i = 0 ; i < passScopeCount ; i++ )
  76. {
  77. if( m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].AvailableShaderGlobals[ i ].PropertyName.Equals( m_propertyName ) )
  78. {
  79. m_currentScope = ShaderPropertyScope.Pass;
  80. }
  81. }
  82. }
  83. void FetchShaderProperties()
  84. {
  85. if( m_templateMPData == null )
  86. m_templateMPData = ( m_containerGraph.CurrentMasterNode as TemplateMultiPassMasterNode ).CurrentTemplate;
  87. if( m_templateMPData != null )
  88. {
  89. if( m_advancedView )
  90. {
  91. switch( m_currentScope )
  92. {
  93. case ShaderPropertyScope.Shader:
  94. m_shaderProperties = m_templateMPData.AvailableShaderProperties;
  95. break;
  96. case ShaderPropertyScope.SubShader:
  97. m_shaderProperties = m_templateMPData.SubShaders[ SubShaderIdx ].AvailableShaderGlobals;
  98. break;
  99. case ShaderPropertyScope.Pass:
  100. m_shaderProperties = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].AvailableShaderGlobals;
  101. break;
  102. }
  103. }
  104. else
  105. {
  106. m_shaderProperties = m_templateMPData.AllShaderProperties;
  107. if( m_currentPropertyIdx < 0 && m_shaderProperties.Count > 0 )
  108. {
  109. m_currentPropertyIdx = 0;
  110. }
  111. }
  112. }
  113. }
  114. public override void OnEnable()
  115. {
  116. base.OnEnable();
  117. IntPropertyId = Shader.PropertyToID( "_IntData" );
  118. FloatPropertyId = Shader.PropertyToID( "_FloatData" );
  119. VectorPropertyId = Shader.PropertyToID( "_VectorData" );
  120. Sampler2DPropertyId = Shader.PropertyToID( "_Sampler2DData" );
  121. Sampler3DPropertyId = Shader.PropertyToID( "_Sampler3DData" );
  122. SamplerCubePropertyId = Shader.PropertyToID( "_SamplerCubeData" );
  123. }
  124. public override void DrawProperties()
  125. {
  126. base.DrawProperties();
  127. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  128. {
  129. return;
  130. }
  131. EditorGUI.BeginChangeCheck();
  132. m_advancedView = EditorGUILayoutToggle( "Advanced View" , m_advancedView );
  133. if( EditorGUI.EndChangeCheck() )
  134. {
  135. if( m_advancedView )
  136. {
  137. if( m_shaderProperties[ m_currentPropertyIdx ].PassId >= 0 )
  138. {
  139. m_currentScope = ShaderPropertyScope.Pass;
  140. PassIdx = m_shaderProperties[ m_currentPropertyIdx ].PassId;
  141. SubShaderIdx = m_shaderProperties[ m_currentPropertyIdx ].SubShaderId;
  142. }
  143. else if( m_shaderProperties[ m_currentPropertyIdx ].SubShaderId >= 0 )
  144. {
  145. m_currentScope = ShaderPropertyScope.SubShader;
  146. SubShaderIdx = m_shaderProperties[ m_currentPropertyIdx ].SubShaderId;
  147. PassIdx = 0;
  148. }
  149. else
  150. {
  151. m_currentScope = ShaderPropertyScope.Shader;
  152. SubShaderIdx = 0;
  153. PassIdx = 0;
  154. }
  155. }
  156. FetchShaderProperties();
  157. FetchPropertyId();
  158. }
  159. if( m_advancedView && m_multiPassMode )
  160. {
  161. DrawMultipassProperties();
  162. }
  163. if( m_currentPropertyIdx > -1 )
  164. {
  165. bool hasProperties = ( m_shaderProperties != null && m_shaderProperties.Count > 0 );
  166. if( hasProperties )
  167. {
  168. EditorGUI.BeginChangeCheck();
  169. m_currentPropertyIdx = EditorGUILayoutPopup( PropertyLabelStr , m_currentPropertyIdx , m_propertyLabels );
  170. if( EditorGUI.EndChangeCheck() )
  171. {
  172. UpdateFromId();
  173. }
  174. EditorGUILayout.LabelField( m_typeName );
  175. if( m_shaderProperties[ m_currentPropertyIdx ].PropertyType != PropertyType.Global )
  176. {
  177. EditorGUILayout.LabelField( m_propertyNameLabel );
  178. }
  179. }
  180. }
  181. }
  182. void DrawMultipassProperties()
  183. {
  184. EditorGUI.BeginChangeCheck();
  185. m_currentScope = (ShaderPropertyScope)EditorGUILayoutEnumPopup( CurrentScopeStr , m_currentScope );
  186. if( EditorGUI.EndChangeCheck() )
  187. {
  188. FetchShaderProperties();
  189. FetchPropertyId();
  190. }
  191. bool showSubShader = false;
  192. bool showPass = false;
  193. switch( m_currentScope )
  194. {
  195. case ShaderPropertyScope.SubShader:
  196. {
  197. showSubShader = true;
  198. }
  199. break;
  200. case ShaderPropertyScope.Pass:
  201. {
  202. showSubShader = true;
  203. showPass = true;
  204. }
  205. break;
  206. }
  207. if( showSubShader )
  208. {
  209. DrawSubShaderUI();
  210. }
  211. if( showPass )
  212. {
  213. DrawPassUI();
  214. }
  215. }
  216. protected override void OnSubShaderChange()
  217. {
  218. FetchShaderProperties();
  219. FetchPropertyId();
  220. }
  221. protected override void OnPassChange()
  222. {
  223. FetchShaderProperties();
  224. FetchPropertyId();
  225. }
  226. override protected void CheckWarningState()
  227. {
  228. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  229. {
  230. ShowTab( NodeMessageType.Error , ErrorMessageStr );
  231. }
  232. else
  233. {
  234. if( m_shaderProperties != null &&
  235. m_shaderProperties.Count > 0 &&
  236. m_shaderProperties.Count > m_currentPropertyIdx &&
  237. m_shaderProperties[ m_currentPropertyIdx ].PropertyType == PropertyType.Global &&
  238. m_showPreview )
  239. {
  240. ShowTab( NodeMessageType.Info , WarningStr );
  241. }
  242. else
  243. {
  244. m_showErrorMessage = false;
  245. }
  246. }
  247. }
  248. public override void SetPreviewInputs()
  249. {
  250. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  251. return;
  252. if( m_shaderProperties == null || m_currentPropertyIdx >= m_shaderProperties.Count || m_currentPropertyIdx < 0 )
  253. return;
  254. if( m_shaderProperties[ m_currentPropertyIdx ].PropertyType == PropertyType.Global )
  255. {
  256. m_additionalContent.text = string.Empty;
  257. PreviewMaterial.SetInt( IntPropertyId , 0 );
  258. return;
  259. }
  260. // @diogo: sacrificed material value display in order to have the node name in the title and property in subtitle
  261. //Material currMat = m_containerGraph.CurrentMaterial;
  262. //if( currMat != null && currMat.HasProperty( m_propertyNameId ) )
  263. //{
  264. // switch( m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType )
  265. // {
  266. // case WirePortDataType.INT:
  267. // {
  268. // int value = currMat.GetInt( m_propertyNameId );
  269. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , GenerateTitle( value ) ) );
  270. // PreviewMaterial.SetInt( IntPropertyId , value );
  271. // }
  272. // break;
  273. // case WirePortDataType.FLOAT:
  274. // {
  275. // float value = currMat.GetFloat( m_propertyNameId );
  276. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , GenerateTitle( value ) ) );
  277. // PreviewMaterial.SetFloat( FloatPropertyId , value );
  278. // }
  279. // break;
  280. // case WirePortDataType.FLOAT4:
  281. // {
  282. // Vector4 value = currMat.GetVector( m_propertyNameId );
  283. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , GenerateTitle( value.x , value.y , value.z , value.w ) ) );
  284. // PreviewMaterial.SetVector( VectorPropertyId , value );
  285. // }
  286. // break;
  287. // case WirePortDataType.COLOR:
  288. // {
  289. // Color value = currMat.GetColor( m_propertyNameId );
  290. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , GenerateTitle( value.r , value.g , value.b , value.a ) ) );
  291. // PreviewMaterial.SetColor( VectorPropertyId , value );
  292. // }
  293. // break;
  294. // case WirePortDataType.SAMPLER2D:
  295. // {
  296. // Texture value = currMat.GetTexture( m_propertyNameId );
  297. // if( value )
  298. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , value.name ) );
  299. // else
  300. // SetAdditonalTitleText( string.Empty );
  301. // PreviewMaterial.SetTexture( Sampler2DPropertyId , value );
  302. // }
  303. // break;
  304. // case WirePortDataType.SAMPLER3D:
  305. // {
  306. // Texture value = currMat.GetTexture( m_propertyNameId );
  307. // if( value )
  308. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , value.name ) );
  309. // else
  310. // SetAdditonalTitleText( string.Empty );
  311. // PreviewMaterial.SetTexture( Sampler3DPropertyId , value );
  312. // }
  313. // break;
  314. // case WirePortDataType.SAMPLERCUBE:
  315. // {
  316. // Texture value = currMat.GetTexture( m_propertyNameId );
  317. // if( value )
  318. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , value.name ) );
  319. // else
  320. // SetAdditonalTitleText( string.Empty );
  321. // PreviewMaterial.SetTexture( SamplerCubePropertyId , value );
  322. // }
  323. // break;
  324. // case WirePortDataType.SAMPLER2DARRAY:
  325. // {
  326. // Texture value = currMat.GetTexture( m_propertyNameId );
  327. // if( value )
  328. // SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr , value.name ) );
  329. // else
  330. // SetAdditonalTitleText( string.Empty );
  331. // PreviewMaterial.SetTexture( SamplerCubePropertyId , value );
  332. // }
  333. // break;
  334. // }
  335. //}
  336. //else
  337. //{
  338. // SetAdditonalTitleText( string.Empty );
  339. //}
  340. SetAdditonalTitleText( m_propertyName );
  341. }
  342. public override void Draw( DrawInfo drawInfo )
  343. {
  344. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  345. {
  346. if( !m_showErrorMessage || m_errorMessageTypeIsError == NodeMessageType.Info )
  347. {
  348. ShowTab( NodeMessageType.Error , ErrorMessageStr );
  349. }
  350. }
  351. else if( m_showErrorMessage )
  352. {
  353. if( m_errorMessageTypeIsError == NodeMessageType.Error )
  354. HideTab();
  355. }
  356. base.Draw( drawInfo );
  357. if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
  358. return;
  359. if( m_shaderProperties == null )
  360. {
  361. MasterNode masterNode = m_containerGraph.CurrentMasterNode;
  362. if( masterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  363. {
  364. if( SetTemplate( masterNode ) )
  365. {
  366. m_fetchPropertyId = true;
  367. }
  368. }
  369. }
  370. if( m_fetchScopeFromProperty )
  371. {
  372. m_fetchScopeFromProperty = false;
  373. FetchScope();
  374. FetchShaderProperties();
  375. }
  376. if( m_fetchPropertyId )
  377. {
  378. m_fetchPropertyId = false;
  379. FetchPropertyId();
  380. }
  381. if( m_currentPropertyIdx > -1 )
  382. {
  383. EditorGUI.BeginChangeCheck();
  384. m_currentPropertyIdx = m_upperLeftWidgetHelper.DrawWidget( this , m_currentPropertyIdx , m_propertyLabels );
  385. if( EditorGUI.EndChangeCheck() )
  386. {
  387. UpdateFromId();
  388. }
  389. }
  390. }
  391. void FetchPropertyId()
  392. {
  393. if( m_shaderProperties != null )
  394. {
  395. m_currentPropertyIdx = 0;
  396. m_propertyLabels = new string[ m_shaderProperties.Count ];
  397. for( int i = 0 ; i < m_shaderProperties.Count ; i++ )
  398. {
  399. if( m_shaderProperties[ i ].PropertyName.Equals( m_propertyName ) )
  400. {
  401. m_currentPropertyIdx = i;
  402. }
  403. m_propertyLabels[ i ] = m_shaderProperties[ i ].PropertyInspectorName;
  404. }
  405. UpdateFromId();
  406. }
  407. else
  408. {
  409. m_currentPropertyIdx = -1;
  410. }
  411. }
  412. void UpdateFromId()
  413. {
  414. if( m_shaderProperties != null )
  415. {
  416. if( m_shaderProperties.Count == 0 )
  417. {
  418. for( int i = 0 ; i < 4 ; i++ )
  419. m_containerGraph.DeleteConnection( false , UniqueId , i , false , true );
  420. m_headerColor = UIUtils.GetColorFromCategory( "Default" );
  421. SetAdditonalTitleText( "<None>" );
  422. m_additionalContent.text = string.Empty;
  423. m_previewMaterialPassId = 1;
  424. PreviewMaterial.SetFloat( FloatPropertyId , 0 );
  425. m_showPreview = false;
  426. m_drawPreviewExpander = false;
  427. m_outputPorts[ 0 ].ChangeProperties( "None" , WirePortDataType.FLOAT , false );
  428. ConfigurePorts();
  429. return;
  430. }
  431. m_drawPreviewExpander = true;
  432. bool areCompatible = TemplateHelperFunctions.CheckIfCompatibles( m_outputPorts[ 0 ].DataType , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType );
  433. switch( m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType )
  434. {
  435. case WirePortDataType.SAMPLER1D:
  436. case WirePortDataType.SAMPLER2D:
  437. case WirePortDataType.SAMPLER3D:
  438. case WirePortDataType.SAMPLERCUBE:
  439. case WirePortDataType.SAMPLER2DARRAY:
  440. m_outputPorts[ 0 ].ChangeProperties( "Tex" , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType , false );
  441. m_headerColor = UIUtils.GetColorFromCategory( "Textures" );
  442. break;
  443. case WirePortDataType.INT:
  444. case WirePortDataType.FLOAT:
  445. m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType , false );
  446. m_headerColor = UIUtils.GetColorFromCategory( "Constants And Properties" );
  447. break;
  448. case WirePortDataType.FLOAT4:
  449. m_outputPorts[ 0 ].ChangeProperties( "XYZW" , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType , false );
  450. m_headerColor = UIUtils.GetColorFromCategory( "Constants And Properties" );
  451. break;
  452. case WirePortDataType.COLOR:
  453. m_outputPorts[ 0 ].ChangeProperties( "RGBA" , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType , false );
  454. m_headerColor = UIUtils.GetColorFromCategory( "Constants And Properties" );
  455. break;
  456. default:
  457. case WirePortDataType.OBJECT:
  458. case WirePortDataType.FLOAT3x3:
  459. case WirePortDataType.FLOAT4x4:
  460. m_outputPorts[ 0 ].ChangeProperties( "Out" , m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType , false );
  461. m_headerColor = UIUtils.GetColorFromCategory( "Constants And Properties" );
  462. break;
  463. }
  464. if( !areCompatible )
  465. {
  466. for( int i = 0 ; i < 4 ; i++ )
  467. m_containerGraph.DeleteConnection( false , UniqueId , i , false , true );
  468. }
  469. ConfigurePorts();
  470. m_propertyName = m_shaderProperties[ m_currentPropertyIdx ].PropertyName;
  471. SetAdditonalTitleText( m_propertyName );
  472. m_propertyNameId = Shader.PropertyToID( m_propertyName );
  473. m_typeName = TypeLabelStr + m_shaderProperties[ m_currentPropertyIdx ].PropertyType.ToString();
  474. if( m_shaderProperties[ m_currentPropertyIdx ].PropertyType != PropertyType.Global )
  475. {
  476. m_propertyNameLabel = PropertyNameStr + m_shaderProperties[ m_currentPropertyIdx ].PropertyName;
  477. }
  478. m_sizeIsDirty = true;
  479. Material currMat = m_containerGraph.CurrentMaterial;
  480. if( currMat != null )
  481. {
  482. if( m_shaderProperties[ m_currentPropertyIdx ].PropertyType == PropertyType.Global )
  483. {
  484. m_previewMaterialPassId = 0;
  485. if( !m_showErrorMessage && m_showPreview )
  486. {
  487. ShowTab( NodeMessageType.Info , WarningStr );
  488. }
  489. }
  490. else
  491. {
  492. if( m_showErrorMessage && m_errorMessageTypeIsError != NodeMessageType.Error )
  493. {
  494. HideTab();
  495. }
  496. switch( m_shaderProperties[ m_currentPropertyIdx ].PropertyDataType )
  497. {
  498. case WirePortDataType.INT: m_previewMaterialPassId = 0; break;
  499. case WirePortDataType.FLOAT: m_previewMaterialPassId = 1; break;
  500. case WirePortDataType.FLOAT4:
  501. case WirePortDataType.COLOR: m_previewMaterialPassId = 2; break;
  502. case WirePortDataType.SAMPLER2D: m_previewMaterialPassId = 3; break;
  503. case WirePortDataType.SAMPLER3D: m_previewMaterialPassId = 4; break;
  504. case WirePortDataType.SAMPLERCUBE: m_previewMaterialPassId = 5; break;
  505. default: PreviewMaterial.SetPass( 0 ); break;
  506. }
  507. }
  508. }
  509. CheckWarningState();
  510. }
  511. }
  512. string GenerateTitle( params float[] values )
  513. {
  514. //string finalResult = "( ";
  515. string finalResult = string.Empty;
  516. if( values.Length == 1 )
  517. {
  518. finalResult += values[ 0 ].ToString( Mathf.Abs( values[ 0 ] ) > 1000 ? Constants.PropertyBigFloatFormatLabel : Constants.PropertyFloatFormatLabel );
  519. }
  520. else
  521. {
  522. for( int i = 0 ; i < values.Length ; i++ )
  523. {
  524. finalResult += values[ i ].ToString( Mathf.Abs( values[ i ] ) > 1000 ? Constants.PropertyBigVectorFormatLabel : Constants.PropertyVectorFormatLabel );
  525. if( i < ( values.Length - 1 ) )
  526. finalResult += ",";
  527. }
  528. }
  529. //finalResult += " )";
  530. return finalResult;
  531. }
  532. public override string GenerateShaderForOutput( int outputId , ref MasterNodeDataCollector dataCollector , bool ignoreLocalvar )
  533. {
  534. if( dataCollector.MasterNodeCategory != AvailableShaderTypes.Template )
  535. {
  536. UIUtils.ShowMessage( UniqueId , "Template Parameter node is only intended for templates use only" , MessageSeverity.Error );
  537. return m_outputPorts[ outputId ].ErrorValue;
  538. }
  539. if( m_shaderProperties == null || m_shaderProperties.Count == 0 )
  540. {
  541. UIUtils.ShowMessage( UniqueId , "Attempting to fetch inexistant parameter on " + m_nodeAttribs.Name + " node" , MessageSeverity.Error );
  542. return m_outputPorts[ outputId ].ErrorValue;
  543. }
  544. if( m_multiPassMode )
  545. {
  546. switch( m_currentScope )
  547. {
  548. case ShaderPropertyScope.SubShader:
  549. {
  550. if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx )
  551. {
  552. UIUtils.ShowMessage( UniqueId , string.Format( "{0} is only intended for subshader {1}" , m_propertyLabels[ m_currentPropertyIdx ] , SubShaderIdx ) );
  553. return m_outputPorts[ outputId ].ErrorValue;
  554. }
  555. }
  556. break;
  557. case ShaderPropertyScope.Pass:
  558. {
  559. if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx ||
  560. dataCollector.TemplateDataCollectorInstance.MultipassPassIdx != PassIdx
  561. )
  562. {
  563. UIUtils.ShowMessage( UniqueId , string.Format( "{0} is only intended for subshader {1} and pass {2}" , m_propertyLabels[ m_currentPropertyIdx ] , SubShaderIdx , PassIdx ) );
  564. return m_outputPorts[ outputId ].ErrorValue;
  565. }
  566. }
  567. break;
  568. }
  569. }
  570. return GetOutputVectorItem( 0 , outputId , m_propertyName );
  571. }
  572. public override void ReadFromString( ref string[] nodeParams )
  573. {
  574. base.ReadFromString( ref nodeParams );
  575. m_propertyName = GetCurrentParam( ref nodeParams );
  576. m_propertyNameId = Shader.PropertyToID( m_propertyName );
  577. if( UIUtils.CurrentShaderVersion() > TemplatesManager.MPShaderVersion )
  578. {
  579. m_currentScope = (ShaderPropertyScope)Enum.Parse( typeof( ShaderPropertyScope ) , GetCurrentParam( ref nodeParams ) );
  580. }
  581. else
  582. {
  583. m_fetchScopeFromProperty = true;
  584. }
  585. m_fetchPropertyId = true;
  586. if( UIUtils.CurrentShaderVersion() > 18502 )
  587. {
  588. m_advancedView = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  589. }
  590. }
  591. public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
  592. {
  593. base.WriteToString( ref nodeInfo , ref connectionsInfo );
  594. IOUtils.AddFieldValueToString( ref nodeInfo , m_propertyName );
  595. IOUtils.AddFieldValueToString( ref nodeInfo , m_currentScope );
  596. IOUtils.AddFieldValueToString( ref nodeInfo , m_advancedView );
  597. }
  598. public override void OnMasterNodeReplaced( MasterNode newMasterNode )
  599. {
  600. base.OnMasterNodeReplaced( newMasterNode );
  601. if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
  602. {
  603. SetTemplate( newMasterNode );
  604. if( m_fetchScopeFromProperty )
  605. {
  606. m_fetchScopeFromProperty = false;
  607. FetchScope();
  608. }
  609. FetchShaderProperties();
  610. FetchPropertyId();
  611. //m_containerGraph.DeleteConnection( false, UniqueId, 0, false, true );
  612. }
  613. }
  614. bool SetTemplate( MasterNode newMasterNode )
  615. {
  616. if( m_containerGraph.MultiPassMasterNodes.NodesList.Count > 0 )
  617. {
  618. m_multiPassMode = true;
  619. TemplateMultiPassMasterNode templateMasterNode = ( newMasterNode as TemplateMultiPassMasterNode );
  620. if( templateMasterNode != null )
  621. {
  622. m_templateMPData = templateMasterNode.CurrentTemplate;
  623. UpdateSubShaderAmount();
  624. FetchShaderProperties();
  625. return true;
  626. }
  627. }
  628. else
  629. {
  630. m_multiPassMode = false;
  631. TemplateMasterNode templateMasterNode = ( newMasterNode as TemplateMasterNode );
  632. if( templateMasterNode != null )
  633. {
  634. m_shaderProperties = templateMasterNode.CurrentTemplate.AvailableShaderProperties;
  635. return true;
  636. }
  637. }
  638. return false;
  639. }
  640. public override void RefreshExternalReferences()
  641. {
  642. base.RefreshExternalReferences();
  643. CheckWarningState();
  644. }
  645. public override void Destroy()
  646. {
  647. base.Destroy();
  648. m_propertyLabels = null;
  649. m_shaderProperties = null;
  650. m_upperLeftWidgetHelper = null;
  651. }
  652. }
  653. }