Search Unity

Problem: shader work only in editor. Why ? (Grab Pass)

Discussion in 'Shaders' started by ticoel, Feb 18, 2018.

  1. ticoel

    ticoel

    Joined:
    Feb 18, 2018
    Posts:
    1
    Hello !

    I have got a problem with a shader work only in editor. Under camera eye, shader is transparent or distort depending camera orientation. I think that problem result of Grab Pass but I don't know resolve it. How resolve this problem ?

    Code (csharp):
    1. Shader "Unlit/EdgeDetection2"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1, 1, 1, 1)
    7.         _EdgeThreshold ("Edge Threshold", float) = 1
    8.         _Kd("Diffuse reflectivity", vector) = (1, 1, 1, 1)
    9.         _Ka("Ambient reflectivity", vector) = (1, 1, 1, 1)
    10.         _Ks("Specular reflectivity", vector) = (1, 1, 1, 1)
    11.         _Shininess("shininess", float) = 0.1
    12.     }
    13.     SubShader
    14.     {
    15.         Tags {
    16.             "RenderType"="Opaque"
    17.             "LightMode" = "ForwardBase"
    18.         }
    19.         LOD 100
    20.  
    21.         Pass
    22.         {
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #include "UnityCG.cginc"
    27.             #include "Lighting.cginc"
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 float4 vertex : SV_POSITION;
    33.                 float3 normal : NORMAL;
    34.                 fixed3 color: COLOR0;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.             fixed4 _Color;
    40.             float4 _Kd;
    41.             float4 _Ka;
    42.             float4 _Ks;
    43.             float _Shininess;
    44.  
    45.             fixed3 ads(float4 position, float3 normal) {
    46.                 float3 n = normalize(normal);
    47.                 float3 s = normalize(_WorldSpaceLightPos0);
    48.                 float3 v = normalize(float3(-position.xyz));
    49.                 float3 h = normalize(v + s);
    50.                 return _LightColor0.rgb * (
    51.                     _Ka +
    52.                     _Kd * max(dot(s, n), 0.) +
    53.                     _Ks * pow(max(dot(h, n), 0.), _Shininess)
    54.                 );
    55.             }
    56.          
    57.             v2f vert (appdata_base v) {
    58.                 v2f o;
    59.                 o.vertex = UnityObjectToClipPos(v.vertex);
    60.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    61.                 o.normal = mul(v.normal, unity_ObjectToWorld);
    62.                 o.color = ads(o.vertex, o.normal);
    63.                 return o;
    64.             }
    65.          
    66.             fixed4 frag (v2f i) : SV_Target {
    67.                 fixed4 col = tex2D(_MainTex, i.uv) * _Color;
    68.                 return fixed4(col.rgb * i.color, col.a);
    69.             }
    70.             ENDCG
    71.         }
    72.         GrabPass {
    73.             "_Inter"
    74.         }
    75.         Pass
    76.         {
    77.             CGPROGRAM
    78.             #pragma vertex vert
    79.             #pragma fragment frag
    80.             #include "UnityCG.cginc"
    81.  
    82.             struct v2f
    83.             {
    84.                 float2 grabPos : TEXCOORD1;
    85.                 float4 vertex : SV_POSITION;
    86.                 float3 normal : NORMAL;
    87.             };
    88.  
    89.             sampler2D _Inter;
    90.             float _EdgeThreshold;
    91.  
    92.             float luma(float3 color) {
    93.                 return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
    94.             }
    95.  
    96.             v2f vert(appdata_base v)
    97.             {
    98.                 v2f o;
    99.                 o.vertex = UnityObjectToClipPos(v.vertex);
    100.                 o.grabPos = ComputeGrabScreenPos(o.vertex);
    101.                 o.normal = mul(v.normal, unity_ObjectToWorld);
    102.                 return o;
    103.             }
    104.  
    105.             fixed4 frag(v2f i) : SV_Target
    106.             {
    107.                 float dx = 1. / _ScreenParams.x;
    108.                 float dy = 1. / _ScreenParams.y;
    109.  
    110.                 float s00 = luma(tex2D(_Inter, i.grabPos + float2(-dx, dy)).rgb);
    111.                 float s10 = luma(tex2D(_Inter, i.grabPos + float2(-dx, 0.)).rgb);
    112.                 float s20 = luma(tex2D(_Inter, i.grabPos + float2(-dx, -dy)).rgb);
    113.                 float s01 = luma(tex2D(_Inter, i.grabPos + float2(0., dy)).rgb);
    114.                 float s21 = luma(tex2D(_Inter, i.grabPos + float2(0., -dy)).rgb);
    115.                 float s02 = luma(tex2D(_Inter, i.grabPos + float2(dx, dy)).rgb);
    116.                 float s12 = luma(tex2D(_Inter, i.grabPos + float2(dx, 0.)).rgb);
    117.                 float s22 = luma(tex2D(_Inter, i.grabPos + float2(dx, -dy)).rgb);
    118.  
    119.                 float sx = s00 + 2 * s10 + s20 - (s02 + 2 * s12 + s22);
    120.                 float sy = s00 + 2 * s01 + s02 - (s20 + 2 * s21 + s22);
    121.                 float dist = sx * sx + sy * sy;
    122.  
    123.                 if (dist > _EdgeThreshold)
    124.                     return fixed4(0., 0., 0., 1.);
    125.                 else
    126.                     return fixed4(tex2D(_Inter, i.grabPos).rgb, 1.);
    127.             }
    128.             ENDCG
    129.         }
    130.     }
    131. }
    Sans titre.png