Search Unity

Question Merging RealToon and a dissolve shader

Discussion in 'Shaders' started by double_lung, Jan 15, 2022.

  1. double_lung

    double_lung

    Joined:
    May 16, 2020
    Posts:
    2
    Hello, I'm trying to merge the Realtoon shader from the asset store with a dissolve shader I made with Amplify Shader Editor.
    For now I just added the dissolve shader's surface pass at the end of the toon shader, and the effect looks like this:
    upload_2022-1-15_20-29-29.png ---> upload_2022-1-15_20-29-43.png ----> upload_2022-1-15_20-30-6.png

    As you can see, the dissolve shader adds a dark layer on top of the toon shader, and it only dissolves itself.

    Here is the code I added to the end of the toon shader:
    Code (CSharp):
    1. Tags{
    2.             "RenderType" = "TransparentCutout"
    3.             "Queue" = "AlphaTest+0"
    4.             "IsEmissive" = "true"
    5.         }
    6.         Cull Off
    7.         CGPROGRAM
    8.         #include "UnityShaderVariables.cginc"
    9.         #pragma target 3.0
    10.         #pragma surface surf Standard keepalpha addshadow fullforwardshadows
    11.         struct Input
    12.         {
    13.             float3 worldPos;
    14.         };
    15.  
    16.         uniform float _NoiseScale;
    17.         uniform float _DissolveAmount;
    18.         uniform float _EdgeThickness;
    19.         uniform float4 _EdgeColor;
    20.  
    21.  
    22.         float3 mod3D289( float3 x ) { return x - floor( x / 289.0 ) * 289.0; }
    23.  
    24.         float4 mod3D289( float4 x ) { return x - floor( x / 289.0 ) * 289.0; }
    25.  
    26.         float4 permute( float4 x ) { return mod3D289( ( x * 34.0 + 1.0 ) * x ); }
    27.  
    28.         float4 taylorInvSqrt( float4 r ) { return 1.79284291400159 - r * 0.85373472095314; }
    29.  
    30.         float snoise( float3 v )
    31.         {
    32.             const float2 C = float2( 1.0 / 6.0, 1.0 / 3.0 );
    33.             float3 i = floor( v + dot( v, C.yyy ) );
    34.             float3 x0 = v - i + dot( i, C.xxx );
    35.             float3 g = step( x0.yzx, x0.xyz );
    36.             float3 l = 1.0 - g;
    37.             float3 i1 = min( g.xyz, l.zxy );
    38.             float3 i2 = max( g.xyz, l.zxy );
    39.             float3 x1 = x0 - i1 + C.xxx;
    40.             float3 x2 = x0 - i2 + C.yyy;
    41.             float3 x3 = x0 - 0.5;
    42.             i = mod3D289( i);
    43.             float4 p = permute( permute( permute( i.z + float4( 0.0, i1.z, i2.z, 1.0 ) ) + i.y + float4( 0.0, i1.y, i2.y, 1.0 ) ) + i.x + float4( 0.0, i1.x, i2.x, 1.0 ) );
    44.             float4 j = p - 49.0 * floor( p / 49.0 );  // mod(p,7*7)
    45.             float4 x_ = floor( j / 7.0 );
    46.             float4 y_ = floor( j - 7.0 * x_ );  // mod(j,N)
    47.             float4 x = ( x_ * 2.0 + 0.5 ) / 7.0 - 1.0;
    48.             float4 y = ( y_ * 2.0 + 0.5 ) / 7.0 - 1.0;
    49.             float4 h = 1.0 - abs( x ) - abs( y );
    50.             float4 b0 = float4( x.xy, y.xy );
    51.             float4 b1 = float4( x.zw, y.zw );
    52.             float4 s0 = floor( b0 ) * 2.0 + 1.0;
    53.             float4 s1 = floor( b1 ) * 2.0 + 1.0;
    54.             float4 sh = -step( h, 0.0 );
    55.             float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
    56.             float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
    57.             float3 g0 = float3( a0.xy, h.x );
    58.             float3 g1 = float3( a0.zw, h.y );
    59.             float3 g2 = float3( a1.xy, h.z );
    60.             float3 g3 = float3( a1.zw, h.w );
    61.             float4 norm = taylorInvSqrt( float4( dot( g0, g0 ), dot( g1, g1 ), dot( g2, g2 ), dot( g3, g3 ) ) );
    62.             g0 *= norm.x;
    63.             g1 *= norm.y;
    64.             g2 *= norm.z;
    65.             g3 *= norm.w;
    66.             float4 m = max( 0.6 - float4( dot( x0, x0 ), dot( x1, x1 ), dot( x2, x2 ), dot( x3, x3 ) ), 0.0 );
    67.             m = m* m;
    68.             m = m* m;
    69.             float4 px = float4( dot( x0, g0 ), dot( x1, g1 ), dot( x2, g2 ), dot( x3, g3 ) );
    70.             return 42.0 * dot( m, px);
    71.         }
    72.  
    73.  
    74.         void surf( Input i , inout SurfaceOutputStandard o )
    75.         {
    76.             float3 ase_vertex3Pos = mul( unity_WorldToObject, float4( i.worldPos , 1 ) );
    77.             float simplePerlin3D2 = snoise( ase_vertex3Pos*_NoiseScale );
    78.             simplePerlin3D2 = simplePerlin3D2*0.5 + 0.5;
    79.             o.Emission = ( step( simplePerlin3D2 , ( _DissolveAmount + ( _DissolveAmount <= 0.0 ? 0.0 : _EdgeThickness ) ) ) * _EdgeColor ).rgb;
    80.             o.Alpha = 1;
    81.             clip( simplePerlin3D2 - _DissolveAmount );
    82.         }
    83.  
    84.         ENDCG
    How should I fix this? I actually know nothing about shader.
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Yep. You added a new pass to the end of the shader, so it's just rendering that as another pass after the previous two. That's what you asked it to do, so that's what it did.

    If you need it to dissolve, you'll need to actually modify the code used in all of the passes so they individually calculate the noise and call
    clip()
    . Otherwise they're not going to disappear as nothing you do in later passes can do anything but draw over previous ones; you can't remove pixels that have already been rendered.
     
    double_lung likes this.
  3. double_lung

    double_lung

    Joined:
    May 16, 2020
    Posts:
    2
    Thanks for the hint, I will look for clipping in the previous passes.