Unity Shader 中获取屏幕像素坐标
CodingShader "Unity Shaders Book/Chapter 4/Chapter4-ScreenXY" { SubShader { Pass { Tags { "RenderType"="Opaque" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" // method1: float4 vert(float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); } fixed4 frag(float4 vertex : SV_POSITION) : SV_TARGET { return fixed4(vertex.xy / _ScreenParams.xy, 0, 1.0); } /* // method2: struct v2f { float4 pos : SV_POSITION; float4 screenPos : TEXCOORD0; }; v2f vert(float4 vertex : POSITION) { v2f o = (v2f)0; o.pos = UnityObjectToClipPos(vertex); o.screenPos = ComputeScreenPos(o.pos); return o; } fixed4 frag(v2f i) : SV_TARGET { return fixed4(i.screenPos.xy / i.screenPos.w, 0, 1.0); } */ /* // method3: float4 vert(float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); } fixed4 frag(float4 sPos : VPOS) : SV_TARGET { return fixed4(sPos.xy / _ScreenParams.xy, 0, 1.0); } */ ENDCG } } FallBack "Diffuse" }Effect
Screen XY Move