ComputeScreenPosHlpNode.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Compute Screen Pos", "Camera And Screen", "Computes texture coordinate for doing a screenspace-mapped texture sample. Input is clip space position" )]
  9. public sealed class ComputeScreenPosHlpNode : HelperParentNode
  10. {
  11. [SerializeField]
  12. private bool m_normalize = false;
  13. private string NormalizeStr = "Normalize";
  14. private readonly string[] NormalizeOps =
  15. { "{0} = {0} / {0}.w;",
  16. "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z* 0.5 + 0.5;"
  17. };
  18. protected override void CommonInit( int uniqueId )
  19. {
  20. base.CommonInit( uniqueId );
  21. m_funcType = "ComputeScreenPos";
  22. m_funcHDFormatOverride = "ComputeScreenPos( {0} , _ProjectionParams.x )";
  23. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  24. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  25. m_outputPorts[ 0 ].Name = "XYZW";
  26. m_autoWrapProperties = true;
  27. m_previewShaderGUID = "97bd4895d847d764eb21d2bf7aa13671";
  28. }
  29. public override void SetPreviewInputs()
  30. {
  31. base.SetPreviewInputs();
  32. m_previewMaterialPassId = m_normalize ? 1 : 0;
  33. }
  34. protected override void OnUniqueIDAssigned()
  35. {
  36. base.OnUniqueIDAssigned();
  37. m_localVarName = "computeScreenPos" + OutputId;
  38. }
  39. public override void DrawProperties()
  40. {
  41. base.DrawProperties();
  42. m_normalize = EditorGUILayoutToggle( NormalizeStr, m_normalize );
  43. }
  44. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  45. {
  46. string result = base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  47. if( m_normalize )
  48. {
  49. dataCollector.AddLocalVariable( UniqueId, string.Format( NormalizeOps[ 0 ], m_localVarName ) );
  50. dataCollector.AddLocalVariable( UniqueId, string.Format( NormalizeOps[ 1 ], m_localVarName ) );
  51. }
  52. return result;
  53. }
  54. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  55. {
  56. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  57. IOUtils.AddFieldValueToString( ref nodeInfo, m_normalize );
  58. }
  59. public override void ReadFromString( ref string[] nodeParams )
  60. {
  61. base.ReadFromString( ref nodeParams );
  62. if( UIUtils.CurrentShaderVersion() > 15404 )
  63. {
  64. m_normalize = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  65. }
  66. }
  67. }
  68. }