decoding 16 bit int value stored in RGB565 texture
<p>I am trying to use Unity and the Xbox Kinect to create a game/prototype.</p>

<p>The depth camera on the kinect returns a byte[] of 16bit uint values.
I am currently creating a Unity Texture2D object and specifying the format as RGB565 then populating it using the LoadRawTextureData function. This works fine and within the Unity environment I can see at runtime the texture being made and updating.</p>

<p>However, when I sample the texture in the shader I am really not sure of how to decode the encoded value back to the original depth int. I have tried a number of things but unfortunately no luck, I was hoping someone would be able to point me in the right direction.</p>

<pre><code>        float decodeFloatRGB565(float4 enc) {
             //... decode value ...     
        }

        // The vertex shader - handles the position of the vertex 
        v2f vert(appdata v) {
            v2f o;
            o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
            o.uv = v.uv;

            float4 depthRGBA = tex2Dlod(_DepthTex, float4(v.uv,0.0,0.0));
            float val = decodeFloatRGB565(depthRGBA);

            //...scale to value between 0 and 255...
            //...scale to value between 0 and 1...

            o.color = float4(val,val,val,0.0);

            return o;
        }

        // The fragment shader - handles the color of vertex
        fixed4 frag (v2f i) : SV_Target { 
            return i.color; 
        }
</code></pre>

<p>Any help massively appreciated.</p>