Search Unity

Standard Shader: Getting Secondary textures to combine Alpha with Primary textures [SOLVED]

Discussion in 'Shaders' started by BenZed, Jun 22, 2015.

  1. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Hello Graphics forum!
    Before I begin, I'd like to admit that shader language is pretty much greek to me. I'm proficient with scripting but only have a beginners understanding of the render pipeline.

    I've made an example scene to illustrate the situation.
    I've got a crate that holds a gold treasure. It uses the standard shader in Cutout rendering mode:
    Screen Shot 2015-06-22 at 11.42.28 AM.png
    And here's the texture:
    wooden_box.png

    Fantastic, everything looks great. Now, during the course of the game, I'd like to map a damage texture to different UV coordinates that combines the alpha value from the primary texture.
    In the primary Albedo slot the damage texture would look like this: Screen Shot 2015-06-22 at 11.46.57 AM.png
    However, in the secondary slot, it looks like this:
    Screen Shot 2015-06-22 at 11.48.48 AM.png
    Settings used (it's important that I can map the Damage Texture to a different set of coordinates) Screen Shot 2015-06-22 at 11.49.35 AM.png
    Both textures have 'alpha is transparency' checked in their texture settings, but to tell you the truth, that field doesn't seem to have much of an effect on the standard shader.

    Is there anything I can do in the settings to achieve this effect? Or is there a way I can ammend the Standard Shader to add this functionality?

    Thanks very much!
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    The Bumpening
     
  4. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    I think that the last chance you can do (if no other solution is found), is to make a copy of the standard shader and do the lerp-ing() there manually.
     
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Well, I'm not familiar with shader languages or how they work, but I don't understand how anything needs to be lerped. I need to combine alphas from different textures in different uv coordinate sets into a final material.

    The Bumpening Returns
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Well, if you don't know how to fix a car, don't try to fix it. If you want a custom shader, you'll need to be able to write it. Else I would just suggest to swap textures with some premade hole positions.

    There is an option that is a little simpler than adjusting the standard shader though. You can combine the two textures into a render target and then just use this render target as the diffuse map for the object. It will allow you to add multiple damage overlays in free to choose positions, without having to adjust the standard shader. But it will still require you to write a shader that combines the two maps. (Which is extra tricky if you want to render one map using a different object UV set.)
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    If you don't know how to fix a car, I think it's perfectly acceptable to go to a support forum on the internet for how-to information about how to fix a car.

    ANYWAY, I went ahead and took the plunge, and learned a little bit about shaders. Fortunately for me, editing the Standard shader is actually pretty simple.

    Here is the shader I wrote that did the trick, after a bit of trial and error:

    Code (CSharp):
    1. Shader "Standard (Double Cutout)" {
    2.  
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGBA)", 2D) = "white" {}
    6.         _MaskTex ("Mask (RGBA)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    10.     }
    11.    
    12.     SubShader {
    13.         Tags {"Queue"="AlphaTest" "Ignore Projector" = "True" "RenderType"="Transparent"}
    14.         LOD 200
    15.  
    16.         CGPROGRAM
    17.        
    18.         #pragma surface surf Standard alphatest:_Cutoff fullforwardshadows
    19.  
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.         sampler2D _MaskTex;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.             float2 uv2_MaskTex;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         fixed4 _Color;
    33.  
    34.         void surf (Input IN, inout SurfaceOutputStandard o) {
    35.        
    36.             fixed4 main = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    37.             fixed4 mask = tex2D (_MaskTex, IN.uv2_MaskTex);
    38.  
    39.             //Multiplied by the mask.rgb in this case, because if you use a white texture as your mask,
    40.             //you can color it black to add burn marks around the damage.
    41.             o.Albedo = main.rgb * mask.rgb;
    42.  
    43.             //For most cases, you'd probably just want:
    44.             //surface.Albedo = main.rgb;
    45.             o.Metallic = _Metallic;
    46.             o.Smoothness = _Glossiness;
    47.            
    48.             o.Alpha = main.a * mask.a;
    49.         }
    50.        
    51.         ENDCG
    52.     }
    53.     FallBack "Diffuse"
    54. }
    And here it is, working:
    Screen Shot 2015-07-07 at 8.46.45 PM.png
     
    Last edited: Jul 8, 2015