Search Unity

Official MS Kinect.Unity package green screen shader error

Discussion in 'Shaders' started by eco_bach, Jan 27, 2016.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Getting the following error when I copy the 'GreenScreen' demo folder to my Assets folder. Unity 5.3.1 on Windows 10

    'Shader error in 'DX11/GreenScreenShader': Fragment program 'frag': sampler 'SampleType' has no matching texture and will be undefined.

    (on d3d11)


    This is the entire shader code

    Code (csharp):
    1.  
    2. Shader "DX11/GreenScreenShader" {
    3. SubShader {
    4. Pass {
    5.  
    6. CGPROGRAM
    7. #pragma target 5.0
    8.  
    9. #pragma vertex vert
    10. #pragma fragment frag
    11.  
    12. #include "UnityCG.cginc"
    13.  
    14. Texture2D _MainTex;
    15.  
    16. sampler SampleType;
    17.  
    18. struct vs_input {
    19.     float4 pos : POSITION;
    20.     float2 tex : TEXCOORD0;
    21. };
    22.  
    23. StructuredBuffer<float2> depthCoordinates;
    24. StructuredBuffer<float> bodyIndexBuffer;
    25.  
    26. struct ps_input {
    27.     float4 pos : SV_POSITION;
    28.     float2 tex : TEXCOORD0;
    29. };
    30.  
    31. ps_input vert (vs_input v)
    32. {
    33.     ps_input o;
    34.     o.pos = mul (UNITY_MATRIX_MVP, v.pos);
    35.     o.tex = v.tex;
    36.     // Flip x texture coordinate to mimic mirror.
    37.     o.tex.x = 1 - v.tex.x;
    38.     return o;
    39. }
    40.  
    41. float4 frag (ps_input i, in uint id : SV_InstanceID) : COLOR
    42. {
    43.     float4 o;
    44.  
    45.     int colorWidth = (int)(i.tex.x * (float)1920);
    46.     int colorHeight = (int)(i.tex.y * (float)1080);
    47.     int colorIndex = (int)(colorWidth + colorHeight * (float)1920);
    48.  
    49.     o = float4(0, 1, 0, 1);
    50.  
    51.     if ((!isinf(depthCoordinates[colorIndex].x) && !isnan(depthCoordinates[colorIndex].x) && depthCoordinates[colorIndex].x != 0) ||
    52.         !isinf(depthCoordinates[colorIndex].y) && !isnan(depthCoordinates[colorIndex].y) && depthCoordinates[colorIndex].y != 0)
    53.     {
    54.         // We have valid depth data coordinates from our coordinate mapper.  Find player mask from corresponding depth points.
    55.         float player = bodyIndexBuffer[(int)depthCoordinates[colorIndex].x + (int)(depthCoordinates[colorIndex].y * 512)];
    56.         if (player != 255)
    57.         {
    58.             o = _MainTex.Sample(SampleType, i.tex);
    59.         }
    60.     }
    61.  
    62.     return o;
    63. }
    64.  
    65. ENDCG
    66.  
    67. }
    68. }
    69.  
    70. Fallback Off
    71. }
    72.  
    73.  
     
  2. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Rename "SampleType" to "sampler_MainTex":
    Code (CSharp):
    1. //sampler SampleType;
    2. sampler sampler_MainTex;
    3. ...
    4. //o = _MainTex.Sample(SampleType, i.tex);
    5. o = _MainTex.Sample(sampler_MainTex, i.tex);
    Samplers are separate objects in DX11 HLSL but not in Unity, so you have to use texture name in sampler name to tell Unity which sampler belongs to which texture. It is mentioned in compute shader documentation.
     
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    thanks