summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Nodes/HelperFuncs/ComputeGrabScreenPosHlpNode.cs
blob: 362415d33e785cf7fbdfe5c047985c795f3d18a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
namespace AmplifyShaderEditor
{
	[Serializable]
	[NodeAttributes( "Compute Grab Screen Pos", "Camera And Screen", "Computes texture coordinate for doing a screenspace-mapped texture sample. Input is clip space position" )]
	public sealed class ComputeGrabScreenPosHlpNode : HelperParentNode
	{
		private readonly string[] ComputeGrabScreenPosFunction =
		{
			"inline float4 ComputeGrabScreenPos( float4 pos )\n",
			"{\n",
			"#if UNITY_UV_STARTS_AT_TOP\n",
			"\tfloat scale = -1.0;\n",
			"#else\n",
			"\tfloat scale = 1.0;\n",
			"#endif\n",
			"\tfloat4 o = pos * 0.5f;\n",
			"\to.xy = float2( o.x, o.y*scale ) + o.w;\n",
			"#ifdef UNITY_SINGLE_PASS_STEREO\n",
			"\to.xy = TransformStereoScreenSpaceTex ( o.xy, pos.w );\n",
			"#endif\n",
			"\to.zw = pos.zw;\n",
			"\treturn o;\n",
			"}\n"
		};

		protected override void CommonInit( int uniqueId )
		{
			base.CommonInit( uniqueId );
			m_funcType = "ComputeGrabScreenPos";
			m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
			m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
			m_outputPorts[ 0 ].Name = "XYZW";
		}

		protected override void OnUniqueIDAssigned()
		{
			base.OnUniqueIDAssigned();
			m_localVarName = "computeGrabScreenPos" + OutputId;
		}

		public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
		{
			if( dataCollector.IsTemplate && dataCollector.TemplateDataCollectorInstance.CurrentSRPType == TemplateSRPType.HD )
			{
				dataCollector.AddFunction( m_funcType, ComputeGrabScreenPosFunction, false );
			}
			return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
		}
	}
}