Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Unlit/Screen Space Centered"
- {
- Properties
- {
- _MainTex ("Texture", 2D) = "white" {}
- }
- SubShader
- {
- Tags
- {
- "RenderType"="Opaque"
- }
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- float4 vertex : SV_POSITION;
- float4 screenPosition : TEXCOORD1;
- float4 screenPosCenter : TEXCOORD2;
- };
- sampler2D _MainTex;
- float4 _MainTex_ST;
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- float4 center = UnityObjectToClipPos(float4(0, 0, 0, 0));
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- // Calc the screen pos of this vertex, and the screen pos of the origin
- // of this object.
- o.screenPosition = ComputeScreenPos(o.vertex);
- o.screenPosCenter = ComputeScreenPos(center);
- return o;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- // Project the screen coordinates with the perspective divide.
- float2 t0 = i.screenPosition.xy / i.screenPosition.w;
- float2 t1 = i.screenPosCenter.xy / i.screenPosCenter.w;
- // Offset the screen coord to be at the center.
- float2 uv = (t0 - t1);
- // Correct for aspect ratio.
- uv.x *= _ScreenParams.x / _ScreenParams.y;
- fixed4 col = tex2D(_MainTex, uv);
- return col;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement