Search Unity

Screen Coordinates not correct in Shader

Discussion in 'Windows Mixed Reality' started by nienokoue, Nov 13, 2019.

  1. nienokoue

    nienokoue

    Joined:
    Apr 19, 2017
    Posts:
    3
    My project uses Single Pass Rendering and a Windows Mixed Reality Headset. I have a shader, which is set to a new material "ppMaterial", which in turn is then used in
    Code (CSharp):
    1. void OnRenderImage(RenderTexture source, RenderTexture destination)
    2. {
    3.     Graphics.Blit(source, destination, ppMaterial);
    4. }
    The shader is just supposed to draw circles at the screen edges, so
    Code (HLSL):
    1. Shader "Custom/ScreenShader" {
    2.     Properties {
    3.     }
    4.     SubShader {
    5.         Pass {
    6.             CGPROGRAM
    7.             #pragma fragment frag
    8.             #pragma vertex vert
    9.             #include "UnityCG.cginc"
    10.  
    11.             uniform sampler2D _MainTex;
    12.             half4 _MainTex_ST;
    13.  
    14.             struct appdata
    15.             {
    16.                 float4 vertex : POSITION;
    17.                 float2 uv : TEXCOORD0;
    18.             };
    19.             struct v2f
    20.             {
    21.                 float4 vertex : SV_POSITION;
    22.                 float2 uv : TEXCOORD0;
    23.                 float4 screenPos : TEXCOORD1;
    24.             };
    25.  
    26.             float getDist(float2 isIn, float2 pos2){
    27.                 return sqrt((isIn.x - pos2.x)*(isIn.x - pos2.x) + (isIn.y - pos2.y)*(isIn.y - pos2.y));
    28.             }
    29.  
    30.             float4 NearBoundary(float2 isIn, v2f i)
    31.             {
    32.                 float2 pos2 = float2(0.5,0.5);
    33.                 float2 pos3 = float2(0,0.5);
    34.                 float2 pos4 = float2(0.5,0.0);
    35.                 float dist2 = getDist(isIn, pos2);
    36.                 float dist3 = getDist(isIn, pos3);
    37.                 float dist4 = getDist(isIn, pos4);
    38.                 if(dist2<0.1){
    39.                     return float4(0,0.5,0.5,1);
    40.                 }
    41.                 if(dist3.x<0.3){
    42.                     return float4(0.5,0,0.5,1);
    43.                 }
    44.                 if(dist4.x<0.3){
    45.                     return float4(0.5,0,0.5,1);
    46.                 }
    47.                
    48.                 return tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
    49.             }
    50.  
    51.             v2f vert (appdata v)
    52.             {
    53.                 v2f o;
    54.                 o.vertex = UnityObjectToClipPos(v.vertex);
    55.                 o.screenPos = ComputeScreenPos(o.vertex);
    56.                 o.uv = v.uv;
    57.                 return o;
    58.             }
    59.  
    60.             half4 frag(v2f i) : SV_Target {
    61.                 float2 screenPos = i.screenPos;
    62.                 return NearBoundary(screenPos, i);
    63.             }
    64.             ENDCG
    65.         }
    66.     }
    67. }
    But this does not work, the dots are not drawn at the center and the borders, respectively. How to get screen coordinates in the (0,1)-range?