Search Unity

Problem with - Dissolve shader with Two texture

Discussion in 'Shaders' started by Balint3DDesign, Aug 26, 2013.

  1. Balint3DDesign

    Balint3DDesign

    Joined:
    Apr 6, 2011
    Posts:
    110
    Hi!
    I have a little problem, with a dissolve shader.
    I want to create that kinda effect when, in the shader is two texture on each other, and let the first one dissolve. (So only the second texture is visible)
    I have attached an image, something like that i want to create, but without normal map and rim.

    $205ca3a.jpg

    Here is the shader so far :
    Code (csharp):
    1.   Shader "Custom Shaders/Dissolving TextureChange" {
    2.     Properties {
    3.       _MainTex ("Texture (RGB)", 2D) = "white" {}
    4.       _MainTex2 ("Texture (RGB)", 2D) = "white" {}
    5.       _SliceGuide ("Slice Guide (RGB)", 2D) = "white" {}
    6.       _SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
    7.     }
    8.     SubShader {
    9.       Tags { "RenderType" = "Opaque" }
    10.      
    11.       CGPROGRAM
    12.       #pragma surface surf Lambert
    13.      
    14.       struct Input {
    15.           float2 uv_MainTex;
    16.           float2 uv_MainTex2;
    17.           float2 uv_SliceGuide;
    18.           float _SliceAmount;
    19.       };
    20.       sampler2D _MainTex;
    21.       sampler2D _MainTex2;
    22.       sampler2D _SliceGuide;
    23.       float _SliceAmount;
    24.       void surf (Input IN, inout SurfaceOutput o) {
    25.           clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
    26.          
    27.           fixed4 t1 = tex2D(_MainTex, IN.uv_MainTex);
    28.           fixed4 t2 = tex2D (_MainTex2, IN.uv_MainTex2);
    29.          
    30.           o.Albedo = lerp (t1, t2, _SliceAmount).rgb;
    31.       }
    32.       ENDCG
    33.     }
    34.     Fallback "Diffuse"
    35.   }
     
  2. Balint3DDesign

    Balint3DDesign

    Joined:
    Apr 6, 2011
    Posts:
    110
    bump
    Have anyone idea how can it be done?
     
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Code (csharp):
    1.  
    2. Shader "Custom/StepBlend"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _SecondTex ("SecondTex (RGB) Mask(A)", 2D) = "white" {}
    8.         _ClipPos("ClipPos", Range(0, 1)) = 0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.        
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert
    17.  
    18.         sampler2D _MainTex;
    19.         sampler2D _SecondTex;
    20.         fixed _ClipPos;
    21.  
    22.         struct Input
    23.         {
    24.             float2 uv_MainTex;
    25.             float2 uv_SecondTex;
    26.         };
    27.  
    28.         void surf (Input IN, inout SurfaceOutput o)
    29.         {
    30.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    31.             fixed4 c1 = tex2D (_SecondTex, IN.uv_SecondTex);
    32.  
    33.             fixed4 result = lerp(c, c1, step(0.0001, c1.a-_ClipPos));
    34.  
    35.             o.Albedo = result.rgb;
    36.             o.Alpha = result.a;
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Diffuse"
    41. }
    42.  
    43.  
     
  4. Balint3DDesign

    Balint3DDesign

    Joined:
    Apr 6, 2011
    Posts:
    110
    Thanks for your help!
    It works like i wanted :)
    I have modified for my needs, thanks again :)
    Oh and here is my version :
    Code (csharp):
    1. Shader "Custom/StepBlend" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (.5,.5,.5,1)
    4.         _SpecColor ("Specular Material Color (RGB)", Color) = (1,1,1,1)
    5.         _ShinPower ("Shininess", Range(0,1)) = 0.5
    6.         _GlossPower ("Gloss", Range(0.01,1)) = 0.5
    7.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    8.         _Outline ("Outline width", Range (0.0, 0.03)) = .005
    9.         _ClipPos("ClipPos", Range(0, 1)) = 0
    10.         _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
    11.         _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
    12.         _MainTex ("Base (RGB)", 2D) = "white" {}
    13.         _SecondTex ("SecondTex (RGB)", 2D) = "white" {}
    14.         _ThirdTex ("ThirdTex (RGB) Mask(A)", 2D) = "white" {}
    15.         _BumpMap ("Normal Map", 2D) = "bump" {}
    16.         _SpecularTex ("Specular Map", 2D) = "gray" {}
    17.         _Ramp ("Shading Ramp", 2D) = "gray" {}
    18.     }
    19.     SubShader {
    20.         Tags { "Queue" = "Geometry+1" "RenderType" = "Opaque" }
    21.         Pass {
    22.             Name "OUTLINE"
    23.             Tags { "LightMode" = "Always" }
    24.             Cull Off
    25.             ZWrite Off
    26.             Blend SrcAlpha OneMinusSrcAlpha
    27.  
    28.             CGPROGRAM
    29.             #pragma vertex vert
    30.             #pragma fragment frag
    31.             #include "UnityCG.cginc"
    32.  
    33.             struct appdata {
    34.                 float4 vertex : POSITION;
    35.                 float3 normal : NORMAL;
    36.             };
    37.  
    38.             struct v2f {
    39.                 float4 pos : POSITION;
    40.                 float4 color : COLOR;
    41.             };
    42.  
    43.             uniform float _Outline;
    44.             uniform float4 _OutlineColor;
    45.  
    46.             v2f vert(appdata v) {
    47.                 v2f o;
    48.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    49.                 float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    50.                 float2 offset = TransformViewToProjection(norm.xy);
    51.                 o.pos.xy += offset * o.pos.z * _Outline;
    52.                 o.color = _OutlineColor;
    53.                 return o;
    54.             }
    55.  
    56.             half4 frag(v2f i) : COLOR {
    57.                 return i.color;
    58.             }
    59.             ENDCG
    60.         }
    61.        
    62.         CGPROGRAM
    63.         #pragma surface surf Shader
    64.         #pragma only_renderers d3d9
    65.         #pragma target 3.0
    66.  
    67.         struct SurfaceOutputCustom {
    68.             fixed3 Albedo;
    69.             fixed3 Normal;
    70.             fixed3 Emission;
    71.             fixed Specular;
    72.             fixed Gloss;
    73.             float Alpha;
    74.         };
    75.  
    76.         struct Input {
    77.             float2 uv_MainTex;
    78.             float3 viewDir;
    79.         };
    80.  
    81.         sampler2D _MainTex;
    82.         sampler2D _SecondTex;
    83.         sampler2D _ThirdTex;
    84.         sampler2D _BumpMap;
    85.         sampler2D _SpecularTex;
    86.         sampler2D _Ramp;
    87.        
    88.         fixed _ClipPos;
    89.         fixed4 _Color;
    90.         float _ShinPower;
    91.         float _GlossPower;
    92.         float4 _RimColor;
    93.         float _RimPower;
    94.  
    95.         inline fixed4 LightingShader (inout SurfaceOutputCustom s, fixed3 lightDir, fixed3 viewDir, fixed atten) {
    96.             fixed3 h = normalize (lightDir + viewDir);
    97.  
    98.             fixed NdotL = dot (s.Normal, lightDir) * 0.5 + 0.5;
    99.             fixed3 ramp = tex2D(_Ramp, float2(NdotL * atten)).rgb;
    100.  
    101.             float nh = max (0, dot (s.Normal, h));
    102.             float spec = pow (nh, s.Gloss * 128.0) * s.Specular;
    103.  
    104.             fixed4 c;
    105.             c.rgb = ((s.Albedo * _Color.rgb * ramp * _LightColor0.rgb + _LightColor0.rgb * spec * _SpecColor) * (atten * 2));
    106.             c.a = 0.5;
    107.             return c;
    108.         }
    109.  
    110.         void surf (Input IN, inout SurfaceOutputCustom o)
    111.         {
    112.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
    113.             fixed4 c1 = tex2D (_SecondTex, IN.uv_MainTex);
    114.             fixed4 c2 = tex2D (_ThirdTex, IN.uv_MainTex);
    115.  
    116.             fixed4 result = lerp(c, c1, step(0.0001, c2.a - _ClipPos)) * _Color;
    117.  
    118.             o.Albedo = result.rgb;
    119.             o.Alpha = result.a;
    120.            
    121.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    122.            
    123.             fixed3 specGloss = tex2D (_SpecularTex, IN.uv_MainTex);
    124.             o.Specular = specGloss.r * _ShinPower;
    125.             o.Gloss = specGloss.g * _GlossPower;
    126.            
    127.             half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
    128.             o.Emission = _RimColor.rgb * pow (rim, _RimPower);
    129.         }
    130.         ENDCG
    131.     }
    132.     FallBack "Diffuse"
    133. }