Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. Shader "Unlit/Screen Space Centered"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Tags
  10. {
  11. "RenderType"="Opaque"
  12. }
  13.  
  14. Pass
  15. {
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19.  
  20. #include "UnityCG.cginc"
  21.  
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27.  
  28. struct v2f
  29. {
  30. float2 uv : TEXCOORD0;
  31. float4 vertex : SV_POSITION;
  32. float4 screenPosition : TEXCOORD1;
  33. float4 screenPosCenter : TEXCOORD2;
  34. };
  35.  
  36. sampler2D _MainTex;
  37. float4 _MainTex_ST;
  38.  
  39. v2f vert (appdata v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. float4 center = UnityObjectToClipPos(float4(0, 0, 0, 0));
  44.  
  45. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  46. // Calc the screen pos of this vertex, and the screen pos of the origin
  47. // of this object.
  48. o.screenPosition = ComputeScreenPos(o.vertex);
  49. o.screenPosCenter = ComputeScreenPos(center);
  50. return o;
  51. }
  52.  
  53. fixed4 frag (v2f i) : SV_Target
  54. {
  55. // Project the screen coordinates with the perspective divide.
  56. float2 t0 = i.screenPosition.xy / i.screenPosition.w;
  57. float2 t1 = i.screenPosCenter.xy / i.screenPosCenter.w;
  58.  
  59. // Offset the screen coord to be at the center.
  60. float2 uv = (t0 - t1);
  61.  
  62. // Correct for aspect ratio.
  63. uv.x *= _ScreenParams.x / _ScreenParams.y;
  64.  
  65. fixed4 col = tex2D(_MainTex, uv);
  66. return col;
  67. }
  68. ENDCG
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement