Search Unity

Cel_shader_black_artifacts+shadow question

Discussion in 'Shaders' started by Slaghton, Aug 4, 2018.

  1. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    EDIT: Well, I guess this can be ignored. I got the other cel shader working at least by doing a re-import. There was some error that was affecting that shader in vr but not in desktop view. I still like this older shader though so might look into at least fixing the black artifact thing.
    -----------------------------------------


    Found a site/pdf that lists the tutorial. Not as good as the original though.
    https://www.slideshare.net/phil_potatoe/cartoon-shader-in-unity3d-tutorial

    Hi, i've been using a cel-shader that was posted on a website years ago for learning (not sure if its still around) and it worked great but nowadays its got some black artifacts when zoomed out.
    You can see the black line/edges which show up from a far distance but go away when you get closer. I was wondering if anyone shader savvy could look at the code and see if its an easy fix?

    (Im a 3d artist/intermediate c# coder but have almost 0 knowledge in writing shaders)

    Also, if someone does look at it, how hard does it look to add shadow capability ex:It casts shadows fine but it doesn't receive shadows. I do use MK toon shader but in VR its got some strange stereoscopic errors so the images of the characters float around.

    Ill try to do some digging myself.

    Cel_shader_black_artifacts.png


    Code (CSharp):
    1. Shader "Custom/CartoonShader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _BrightColor("Bright Color", Color) = (1,0,0)
    5.         _Threshold1("Threshold Bright to Dark", range(0,1)) = 0.2
    6.         _DarkColor("Dark Color", Color) = (0,1,0)
    7.         _Threshold2("Threshold Middle to Dark", range(0,1)) = 0.9
    8.         _TransitionTexture("Transition Texture", 2D) = "white" {}
    9.         _TransitionTextureSize("Transition Texture Size", range(0.1, 50)) = 1
    10.  
    11.         _BrightTexture("Bright Color Texture", 2D) = "white"{}
    12.         _BrightTextureSize("Bright Texture Size", range(0.1,50)) = 1
    13.         _BrightTextureIntensity("Bright Texture Intensity", range(0.0,1)) = 0.5
    14.  
    15.         _DarkTexture("Dark Color Texture", 2D) = "white"{}
    16.         _DarkTextureSize("Dark Texture Size", range(0.1,50)) = 1
    17.  
    18.         _RimColor("Rim Color", Color) = (1,0,0)
    19.         _RimPower("Rim Position", range(0,4)) = 2
    20.         _RimStrength("Rim Strength", range(0,1)) = 1
    21.  
    22.     }
    23.  
    24.     SubShader {
    25.         Tags { "RenderType"="Opaque" }
    26.         LOD 200
    27.  
    28.         CGPROGRAM
    29.         #pragma surface surf Cartoon addshadow  fullforwardshadows
    30.  
    31.         sampler2D _MainTex;
    32.         sampler2D _TransitionTexture;
    33.         half4 _BrightColor;
    34.         half4 _DarkColor;
    35.         half _Threshold1;
    36.         half _Threshold2;
    37.         half _TransitionTextureSize;
    38.  
    39.         half _DarkTextureSize;
    40.         half _BrightTextureSize;
    41.         half _BrightTextureIntensity;
    42.  
    43.         sampler2D _BrightTexture;
    44.         sampler2D _DarkTexture;
    45.  
    46.         half4 _RimColor;
    47.         half _RimPower;
    48.         half _RimStrength;
    49.  
    50.         struct Input {
    51.             float2 uv_MainTex;
    52.             float3 viewDir;
    53.         };
    54.  
    55.         struct SurfaceOutputCustom    {
    56.             fixed4 Albedo;
    57.             fixed3 Normal;
    58.             fixed3 Emission;
    59.             half Specular;
    60.             fixed Gloss;
    61.             fixed Alpha;
    62.             fixed viewFallof;
    63.             half2 UV;
    64.  
    65.         };
    66.  
    67.         half4 LightingCartoon(SurfaceOutputCustom s, half3 dir, half attend){
    68.  
    69.             dir = normalize(dir);
    70.  
    71.             half NdotL = saturate( dot (s.Normal, dir));
    72.    
    73.             half4 darkColor = _DarkColor * tex2D(_DarkTexture, s.UV * _DarkTextureSize);
    74.             half4 brightColor = _BrightColor * ( tex2D(_BrightTexture, s.UV * _BrightTextureSize)  * _BrightTextureIntensity  + (1-_BrightTextureIntensity));
    75.    
    76.             half3 ShadowColor = NdotL < _Threshold1 ? darkColor : NdotL < _Threshold2 ? lerp(darkColor, brightColor, tex2D(_TransitionTexture, s.UV * _TransitionTextureSize)) : brightColor;
    77.  
    78.             half4 c;
    79.    
    80.             c.rgb = ((NdotL * 0.4f)+0.6f) * s.Albedo * _LightColor0 * ShadowColor;
    81.    
    82.             c.a = s.Alpha;
    83.             return c;
    84.  
    85.         }
    86.  
    87.         void surf (Input IN, inout SurfaceOutputCustom o) {
    88.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    89.             o.UV = IN.uv_MainTex;
    90.    
    91.             half NdotView = 1 - dot(normalize(IN.viewDir), o.Normal);
    92.    
    93.             NdotView = pow(NdotView, _RimPower);
    94.                    
    95.             o.Emission = NdotView * _RimColor * _RimStrength;
    96.    
    97.        
    98.             o.Albedo = c * ( 1 - NdotView * _RimStrength);
    99.             o.Alpha = c.a;
    100.         }
    101.         ENDCG
    102.     }
    103.     FallBack "Diffuse"
    104. }
     
    Last edited: Aug 4, 2018