2016-06-13 85 views
1

我想使用Unity和Xbox Kinect來創建遊戲/原型。解碼16位int值存儲在RGB565紋理

kinect上的深度相機返回16bit uint值的byte []。 我目前正在創建一個Unity Texture2D對象,並指定格式爲RGB565,然後使用LoadRawTextureData函數填充它。這個工作正常,並且在Unity環境中我可以在運行時看到正在製作和更新的紋理。

但是,當我在着色器中採樣紋理時,我真的不確定如何將編碼值解碼回初始深度int。我嘗試了很多東西,但不幸的是沒有運氣,我希望有人能夠指引我走向正確的方向。

 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; 
     } 

任何幫助大量讚賞。

+0

是對R16紋理格式不支持你的平臺上? – Quinchilion

+0

我相信這是 - 我確實先嚐試過,因爲我認爲它會更容易。據我瞭解,這將是紅色通道的所有16位?當我將採樣器應用到紋理時,似乎只有alpha通道中的數據 - 我一定會做出奇怪的事情,我猜想。 –

+0

不要'樣本'(或'tex2D')編碼的值,除非你正在做最近鄰採樣。插值的值是沒有意義的。 – MooseBoys

回答

0

我不知道,如果HLSL和目標平臺的支持位運算符(seems to be true),但如果有在這裏是一個可能的解決方案:

uint depth = (uint) enc.r * 31 << 11 | 
      (uint) enc.g * 63 << 5 | 
      (uint) enc.b * 31;