Search Unity

Shader glitches object positions in WebGL DX11 but not DX9 GPU on DX11

Discussion in 'Shaders' started by Quakeulf, Nov 7, 2018.

  1. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    40
    webgl.gif
    This shows first in DX9, then in DX11 after the pause.

    I have used a slightly modified shader from here:
    http://www.shaderslab.com/demo-77---cel-shading-(formula).html

    With no emulation, it becomes a glitchy mess, but in WebGL1.0 emulation, it is fine. I am new to shading and I don't understand why this is happening...

    Here is the shader code:

    Code (CSharp):
    1. Shader "Toon/Lighting/CelShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Treshold ("Cel treshold", Range(1., 20.)) = 5.
    7.         _Ambient ("Ambient intensity", Range(0., 0.5)) = 0.1
    8.     }
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "RenderType"="Opaque"
    14.             //"LightMode"="ForwardBase"
    15.         }
    16.  
    17.         LOD 200
    18.         Cull Back
    19.         Pass
    20.         {
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #pragma multi_compile_instancing
    25.          
    26.             #include "UnityCG.cginc"
    27.             sampler2D _MainTex;
    28.  
    29.             UNITY_INSTANCING_BUFFER_START(Props)
    30.                 UNITY_DEFINE_INSTANCED_PROP(fixed, _Treshold)
    31.                 UNITY_DEFINE_INSTANCED_PROP(fixed, _Ambient)
    32.             UNITY_INSTANCING_BUFFER_END(Props)
    33.  
    34.             fixed4 _MainTex_ST;
    35.             fixed4 _LightColor0;
    36.  
    37.             struct v2f
    38.             {
    39.                 float4 pos : SV_POSITION;
    40.                 float2 uv : TEXCOORD0;
    41.                 float3 worldNormal : NORMAL;
    42.             };
    43.             float LightToonShading(float3 normal, float3 lightDir)
    44.             {
    45.                 fixed treshold = UNITY_ACCESS_INSTANCED_PROP(Props, _Treshold);
    46.  
    47.                 fixed NdotL = max(0.0, dot(normalize(normal), normalize(lightDir)));
    48.                 return floor(NdotL * treshold) / (treshold - 0.5);
    49.             }
    50.             v2f vert (appdata_base v)
    51.             {
    52.                 v2f o;
    53.                 o.pos = UnityObjectToClipPos(v.vertex);
    54.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    55.                 o.worldNormal = UnityObjectToWorldNormal(v.normal);
    56.                 return o;
    57.             }
    58.             fixed4 frag (v2f i) : SV_Target
    59.             {
    60.                 fixed4 col = tex2D(_MainTex, i.uv);
    61.                 col.rgb *= saturate(LightToonShading(i.worldNormal, _WorldSpaceLightPos0.xyz) + UNITY_ACCESS_INSTANCED_PROP(Props, _Ambient)) * _LightColor0.rgb;
    62.                 return col;
    63.             }
    64.             ENDCG
    65.         }
    66.     }
    67. }
    I am almost sure there is a really simple solution to this that I have overlooked. Please help me if you can.