ComputeGrabScreenPosHlpNode.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. namespace AmplifyShaderEditor
  5. {
  6. [Serializable]
  7. [NodeAttributes( "Compute Grab Screen Pos", "Camera And Screen", "Computes texture coordinate for doing a screenspace-mapped texture sample. Input is clip space position" )]
  8. public sealed class ComputeGrabScreenPosHlpNode : HelperParentNode
  9. {
  10. private readonly string[] ComputeGrabScreenPosFunction =
  11. {
  12. "inline float4 ComputeGrabScreenPos( float4 pos )\n",
  13. "{\n",
  14. "#if UNITY_UV_STARTS_AT_TOP\n",
  15. "\tfloat scale = -1.0;\n",
  16. "#else\n",
  17. "\tfloat scale = 1.0;\n",
  18. "#endif\n",
  19. "\tfloat4 o = pos * 0.5f;\n",
  20. "\to.xy = float2( o.x, o.y*scale ) + o.w;\n",
  21. "#ifdef UNITY_SINGLE_PASS_STEREO\n",
  22. "\to.xy = TransformStereoScreenSpaceTex ( o.xy, pos.w );\n",
  23. "#endif\n",
  24. "\to.zw = pos.zw;\n",
  25. "\treturn o;\n",
  26. "}\n"
  27. };
  28. protected override void CommonInit( int uniqueId )
  29. {
  30. base.CommonInit( uniqueId );
  31. m_funcType = "ComputeGrabScreenPos";
  32. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  33. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
  34. m_outputPorts[ 0 ].Name = "XYZW";
  35. }
  36. protected override void OnUniqueIDAssigned()
  37. {
  38. base.OnUniqueIDAssigned();
  39. m_localVarName = "computeGrabScreenPos" + OutputId;
  40. }
  41. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  42. {
  43. if( dataCollector.IsTemplate && dataCollector.TemplateDataCollectorInstance.CurrentSRPType == TemplateSRPType.HDRP )
  44. {
  45. dataCollector.AddFunction( m_funcType, ComputeGrabScreenPosFunction, false );
  46. }
  47. return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  48. }
  49. }
  50. }