Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shader with 2 decals

Discussion in 'Shaders' started by screenhogdotcom, Feb 26, 2010.

  1. screenhogdotcom

    screenhogdotcom

    Joined:
    Aug 23, 2009
    Posts:
    126
    I should start by saying that I don't know the first thing about building shaders... however, I'd like to know if what I'm about to ask is even possible.

    I would like to use a shader that has:

    - 2 decals
    - a background color that can be changed without altering the tint of either decal

    Preferably, it would be a VertexLit shader, because those do seem to render faster. Is this possible? Has it been done before?
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I sort of did it yesterday. However, if you only need a solid color as the base, and not a texture, we can do a lot better! None of the "caveats" I listed there apply to this one, except the SeparateSpecular issue. I put "SeparateSpecular On" in the first subshader below, that requires three texture units, so the specular lighting won't match between really old cards, and modern ones, but I think that's reasonable. Just get rid of that line if you want an exact match at the expense of probably looking better.

    Code (csharp):
    1. Shader "Color + 2 Decals, Vertex Lit" {
    2.  
    3. Properties
    4. {
    5.     _Color ("Main Color", Color) = (1,1,1)
    6.     _SpecColor ("Spec Color", Color) = (1,1,1)
    7.     _Emission ("Emmisive Color", Color) = (0,0,0)
    8.     _Shininess ("Shininess", Range (0.01, 1)) = 0.7
    9.     _DecalTex ("Decal A (RGBA)", 2D) = ""
    10.     _DecalTexB ("Decal B (RGBA)", 2D) = ""
    11. }
    12.  
    13. // More than two texture units
    14. SubShader {Pass
    15. {
    16.     Lighting On
    17.     SeparateSpecular On
    18.     Material
    19.     {
    20.         Diffuse (1,1,1)
    21.         Ambient (1,1,1)
    22.         Shininess [_Shininess]
    23.         Specular [_SpecColor]
    24.         Emission [_Emission]
    25.     }
    26.     SetTexture[_DecalTex]
    27.     {
    28.         ConstantColor[_Color]
    29.         Combine texture Lerp(texture) constant
    30.     }
    31.     SetTexture[_DecalTexB] {Combine texture Lerp(texture) previous}
    32.     SetTexture[_] {Combine previous * primary Double}
    33. }}
    34.  
    35. // Two texture units
    36. SubShader
    37. {
    38.     Lighting On
    39.     Material
    40.     {
    41.         Diffuse (1,1,1)
    42.         Ambient (1,1,1)
    43.         Shininess [_Shininess]
    44.         Specular [_SpecColor]
    45.         Emission [_Emission]
    46.     }
    47.     Pass
    48.     {
    49.         SetTexture[_DecalTex]
    50.         {
    51.             ConstantColor[_Color]
    52.             Combine texture Lerp(texture) constant
    53.         }
    54.         SetTexture[_DecalTexB] {Combine texture Lerp(texture) previous}
    55.     }
    56.    
    57.     // 'Double' Lighting
    58.     Pass {Blend DstColor SrcColor}
    59. }
    60.  
    61. }
     

    Attached Files:

  3. screenhogdotcom

    screenhogdotcom

    Joined:
    Aug 23, 2009
    Posts:
    126
    My apologies, I forgot to thank you for this. It works perfectly! Thanks!
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Awesome. Glad to hear it.
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    This shader doesn't appear to work on the iPhone.

    Is there any ay to make a decal shader that will work on the iPhone?
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It should work fine and perform better on the 3GS and later due to one fewer draw call. What problem are you having?
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I've attached images of your decal shader applied to a shadow texture (top) and the Unity Transparent-Diffuse shader on the same (bottom).

    Hope this helps.
     

    Attached Files:

  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    This shader isn't for a "shadow texture". This is for an entire object, an object that is colored solid and has two decals on it. Also, it looks like you're using pixel lighting; this shader can't handle that. (And neither can the iPhone for that matter.)
     
  9. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Yeah, I'm using a decal as a trick to achieve a contact shadow.

    No pixel lights here, but thanks for pointing that out. ;-)

    Anyway, I found the problem. Sort of. I was using Unity primitives when things weren't working. Once I switched to mesh-only FBX exports from 3ds Max, everything worked fine.

    Interesting, but I'm not going to argue with it. :p

    Thanks again for your help and for the nice shader!