Search Unity

A little help with pixel position calculation

Discussion in 'Shaders' started by DVD_Rodriguez, Sep 4, 2019.

  1. DVD_Rodriguez

    DVD_Rodriguez

    Joined:
    Feb 25, 2019
    Posts:
    6
    Hello everyone, I'm building an app that uses a depth camera to send a point cloud over the network to a client that has to visualize it. Everything works fine on the server but on client all the points that represent negative coordinates respect to the pivot (0, 0, 0) a aren't visible. I use the same shader in both apps and on the server side there is no problem. This is the shader:

    Code (CSharp):
    1. Shader "PointCloud"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Size("Size", Range(0.1,2)) = 0.1
    7.     }
    8.     SubShader
    9.     {
    10.  
    11.  
    12.         Pass
    13.         {
    14.             Cull Off
    15.             CGPROGRAM
    16.             #pragma target 5.0
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.  
    21.             #include "UnityCG.cginc"
    22.          
    23.  
    24.             struct PS_INPUT
    25.             {
    26.                 float4 position : SV_POSITION;
    27.                 float4 color : COLOR;
    28.                 float3 normal : NORMAL;
    29.  
    30.             };
    31.  
    32.             sampler2D _MainTex;
    33.             float4 _MainTex_ST;
    34.          
    35.             sampler2D _XYZTex;
    36.             sampler2D _ColorTex;
    37.             float4 _XYZTex_TexelSize;
    38.             float4x4 _Position;
    39.  
    40.             float _Size;
    41.             PS_INPUT vert (appdata_full v, uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID)
    42.             {
    43.                 PS_INPUT o;
    44.                 o.normal = v.normal;
    45.  
    46.                 //Compute the UVS
    47.                 float2 uv = float2(
    48.                             clamp(fmod(instance_id, _XYZTex_TexelSize.z) * _XYZTex_TexelSize.x, _XYZTex_TexelSize.x, 1.0 - _XYZTex_TexelSize.x),
    49.                             clamp(((instance_id -fmod(instance_id, _XYZTex_TexelSize.z) * _XYZTex_TexelSize.x) / _XYZTex_TexelSize.z) * _XYZTex_TexelSize.y, _XYZTex_TexelSize.y, 1.0 - _XYZTex_TexelSize.y)
    50.                             );
    51.  
    52.              
    53.  
    54.  
    55.                 //Load the texture
    56.                 float4 XYZPos = float4(tex2Dlod(_XYZTex, float4(uv, 0.0, 0.0)).rgb ,1.0f);
    57.  
    58.                 //Set the World pos
    59.                 o.position = mul(mul(UNITY_MATRIX_VP, _Position ), XYZPos);
    60.  
    61.                 o.color =  float4(tex2Dlod(_ColorTex, float4(uv, 0.0, 0.0)).bgr ,1.0f);
    62.  
    63.                 return o;
    64.             }
    65.  
    66.             struct gs_out {
    67.                 float4 position : SV_POSITION;
    68.                 float4 color : COLOR;
    69.             };
    70.  
    71.  
    72.          
    73.             fixed4 frag (PS_INPUT i) : SV_Target
    74.             {
    75.                 return i.color;
    76.             }
    77.             ENDCG
    78.         }
    79.     }
    80. }
    Server output:

    client output:
     
    Last edited: Sep 5, 2019