Search Unity

Dissolving mesh with multiple materials attached

Discussion in 'General Graphics' started by Shii, Mar 31, 2017.

  1. Shii

    Shii

    Joined:
    Nov 23, 2014
    Posts:
    11
    Hello!
    I'm trying to figure out how to dissolve quad with multiple materials attached.
    Quad used as base for 2d background with 3 materials - first for sky texture, second for clouds texture and third for foreground texture. All 3 materials uses custom modified shader made from Unlit/Transparent shader with added Color field to control texture color and alpha.
    Code (CSharp):
    1. Shader "Unlit/TransparentColor" {
    2. Properties
    3. {
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Color("Main Color", Color) = (1,1,1,1)
    6. }
    7.  
    8. SubShader
    9. {
    10.     Tags
    11.     {
    12.         "Queue"="Transparent"
    13.         "IgnoreProjector"="True"
    14.         "RenderType"="Transparent"
    15.         "PreviewType" = "Plane"
    16.     }
    17.     LOD 100
    18.    
    19.     Cull Off
    20.     Lighting Off
    21.     ZWrite Off
    22.     Blend One OneMinusSrcAlpha
    23.    
    24.     Pass {
    25.         CGPROGRAM
    26.             #pragma vertex vert
    27.             #pragma fragment frag
    28.             #pragma target 2.0
    29.             #pragma multi_compile_fog
    30.            
    31.             #include "UnityCG.cginc"
    32.  
    33.             struct appdata_t {
    34.                 float4 vertex : POSITION;
    35.                 float4 color    : COLOR;
    36.                 float2 texcoord : TEXCOORD0;
    37.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    38.             };
    39.  
    40.             struct v2f {
    41.                 float4 vertex : SV_POSITION;
    42.                 fixed4 color : COLOR;
    43.                 float2 texcoord : TEXCOORD0;
    44.                 UNITY_FOG_COORDS(1)
    45.                 UNITY_VERTEX_OUTPUT_STEREO
    46.             };
    47.  
    48.             fixed4 _Color;
    49.             sampler2D _MainTex;
    50.             float4 _MainTex_ST;
    51.            
    52.             v2f vert (appdata_t v)
    53.             {
    54.                 v2f o;
    55.                 UNITY_SETUP_INSTANCE_ID(v);
    56.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    57.                 o.vertex = UnityObjectToClipPos(v.vertex);
    58.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    59.                 o.color = v.color * _Color;
    60.                 UNITY_TRANSFER_FOG(o,o.vertex);
    61.                 return o;
    62.             }
    63.            
    64.             fixed4 frag (v2f i) : SV_Target
    65.             {
    66.                 fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
    67.                 col.rgb *= col.a;
    68.                 UNITY_APPLY_FOG(i.fogCoord, col);
    69.                 return col;
    70.             }
    71.         ENDCG
    72.     }
    73. }
    74.  
    75. }
    Clouds animated by changing clouds material UV in simple script.

    Now the problem: I want to dissolve whole background with all materials and I'm trying to change materials alpha to achieve this with script but in process foreground became semi-transparent and I can see clouds through foreground.

    Example: http://recordit.co/01WyWl09wn

    Is there a way to dissolve this gameobject without clouds becoming visible through foreground while still animating clouds?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,328
    Yes, by using a single material & shader that can render all those layers at once.

    Basically you want to make two duplicates of all lines with "_MainTex", replacing MainTex with SkyTex and CloudTex for example. Then composite them together with lerp() functions.