Search Unity

Confused shader n00b .... two alpha textures 'on top'

Discussion in 'Shaders' started by dacloo, Jul 14, 2007.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi!

    I am changing all kinds of shaders on this forum to try to get the desired effect, but I can't get it to work :)

    I want 2 alpha textures 'on top' of eachother.
    These should not be affected by lighting in the scene.

    Should be easy...right? :eek:
    Could anyone help me out a bit? Thank you!
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    So you want a base texture; and another texture blended on top based on it's alpha channel? Like the built-in Decal shader, just with no lighting?

    ...or something else?
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Um... yes, basicly a base texture (alpha), and another one blended on top (also alpha). And then no lighting from the scene indeed.
    8)
     
  4. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    :( Still haven't been able to find it out :(
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I'm still not sure how you want the colors or alphas of them combined, so I have several attempts:

    If you want to blend first texture and blend the second texture (note that the result will be different if you swap the textures in the material):
    Code (csharp):
    1. Shader "Alpha/TwoTextures Unlit" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans(A)", 2D) = "white" {}
    4.     _MainTex2("Base (RGB) Trans(A)", 2D) = "white" {}
    5. }
    6. SubShader {
    7.     Pass {
    8.         ColorMask RGB
    9.         Blend SrcAlpha OneMinusSrcAlpha
    10.         ZWrite Off
    11.         SetTexture[_MainTex] { combine texture }
    12.     }
    13.     Pass {
    14.         ColorMask RGB
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         ZWrite Off
    17.         SetTexture[_MainTex2] { combine texture }
    18.     }
    19. }
    20. }
    21.  
    If you want the colors and the alphas to add together, and then blend over background:
    Code (csharp):
    1. Shader "Alpha/TwoTextures Unlit" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans(A)", 2D) = "white" {}
    4.     _MainTex2("Base (RGB) Trans(A)", 2D) = "white" {}
    5. }
    6. SubShader {
    7.     Pass {
    8.         ColorMask RGB
    9.         Blend SrcAlpha OneMinusSrcAlpha
    10.         ZWrite Off
    11.         SetTexture[_MainTex] { combine texture }
    12.         SetTexture[_MainTex2] { combine texture + previous }
    13.     }
    14. }
    15. }
    16.  
    If you want the colors and alphas to multiply together, change "texture + previous" in the last example to "texture * previous". And so on :)
     
  6. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Is that all there is to writing shaders?
     
  7. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    You can obviously get way more complicated but, yes, they're just text files.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Pixel shaders are hard (at least, to me). You can download the source for all the shaders and see how they are made.

    --Eric
     
  9. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Aras,

    Thanks!
    Unfortunately they don't seem to work as they miss the _color property...
     
  10. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Well, you did not say you need the color as well... so I just went the easiest way :) How you want the color to be combined with the textures?
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't you also need

    Code (csharp):
    1. SubShader {
    2.      Tags {Queue=Transparent}
    3.  
    Otherwise 3D geometry probably covers it up?

    --Eric
     
  12. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hehe okay...thanks for helping me man!

    The idea is this:
    I have a transparent PNG (a chat 'balloon').
    I have a render-to-texture which contains text.

    The text (with transparent background) needs to go on top of the transparent balloon. Basicly the text is 'stamped' on the balloon.

    This material is then placed on a plane which faces the camera.
     
  13. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Ah, ok. So that would be like (note that I'm not actually testing this, just typing from my head :)):
    Code (csharp):
    1. Shader "Alpha/TwoTextures Unlit" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans(A)", 2D) = "white" {}
    5.     _MainTex2("Base (RGB) Trans(A)", 2D) = "white" {}
    6. }
    7. SubShader {
    8.     Tags { "Queue" = "Transparent" }
    9.     Pass {
    10.         ColorMask RGB
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         ZWrite Off
    13.         Color [_Color]
    14.         SetTexture[_MainTex] { combine texture * primary }
    15.     }
    16.     Pass {
    17.         ColorMask RGB
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.         ZWrite Off
    20.         Color [_Color]
    21.         SetTexture[_MainTex2] { combine texture * primary }
    22.     }
    23. }
    24. }
    Eric5h5: yeah, I forgot the Queue tag.
     
  14. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Aaah I had to tweak some stuff but the shader itself did a wonderful job. Thanks guys. Lemme know if I can help you out some time...