Search Unity

Unlit with adjustable Alpha

Discussion in 'Shaders' started by Somian, Dec 13, 2011.

  1. Somian

    Somian

    Joined:
    Nov 22, 2011
    Posts:
    38
    Hi there,

    I don't have a clue about the unity shader language and only need a very simple shader that is basically a combination of the Alpha-Diffuse and Unlit-Alpha shader. I need it to be like the unlit shader but want to control the alpha channel via script to fade it out which works in the standard alpha-diffuse shader but not in the unlit-alpha shader.

    How do I accomplish this really quick? :)

    EDIT: oh, and it needs to run on the iPhone which the two base shaders do :)
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    This is not a mindset that is going to work around here.

    Code (csharp):
    1. Shader "Somian/Unlit/Transparent" {
    2.  
    3. Properties {
    4.     _Color ("Main Color (A=Opacity)", Color) = (1,1,1,1)
    5.     _MainTex ("Base (A=Opacity)", 2D) = ""
    6. }
    7.  
    8. Category {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True"}
    10.     ZWrite Off
    11.     Blend SrcAlpha OneMinusSrcAlpha
    12.  
    13.     SubShader {Pass {
    14.         GLSLPROGRAM
    15.         varying mediump vec2 uv;
    16.        
    17.         #ifdef VERTEX
    18.         uniform mediump vec4 _MainTex_ST;
    19.         void main() {
    20.             gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    21.             uv = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    22.         }
    23.         #endif
    24.        
    25.         #ifdef FRAGMENT
    26.         uniform lowp sampler2D _MainTex;
    27.         uniform lowp vec4 _Color;
    28.         void main() {
    29.             gl_FragColor = texture2D(_MainTex, uv) * _Color;
    30.         }
    31.         #endif     
    32.         ENDGLSL
    33.     }}
    34.    
    35.     SubShader {Pass {
    36.         SetTexture[_MainTex] {Combine texture * constant ConstantColor[_Color]}
    37.     }}
    38. }
    39.  
    40. }
     
    jlmolnar, path14, Fenikkel and 17 others like this.
  3. Somian

    Somian

    Joined:
    Nov 22, 2011
    Posts:
    38
    Thanks a lot! works perfectly. Did't know it was so complicated. I tried to puzzle something together from the source of the two original shaders but with no success since I don't know how it works (yet)^^ and don't have time to grapple with the basic concepts of the shader language :)

    Sorry for the "i want it to work and I want it now"^^
     
  4. flamy

    flamy

    Joined:
    Feb 7, 2011
    Posts:
    194
    thanks jessy the performance is better than wat i had before :)
     
  5. brycedaawg

    brycedaawg

    Joined:
    Apr 1, 2011
    Posts:
    4
    is it okay if I use this in my game too please? I would write my own shader, but seeing as yours is right there...
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Definitely. Everything I post here is free to be used by all, unless I say otherwise (which I don't recall doing).
     
  7. brycedaawg

    brycedaawg

    Joined:
    Apr 1, 2011
    Posts:
    4
    thanks very much bro!
     
  8. d3gator

    d3gator

    Joined:
    May 28, 2009
    Posts:
    23
    Hi Jessy,
    it is possible to extend this shader to use specular?
    How it is working on mobile devices?
     
  9. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Hi,

    I'd like to know the reason for the second pass in this shader. I've removed it and it seems to work just fine. Thanks
     
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    There is no second pass. Each SubShader has one pass.
     
  11. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Sorry what I meant to ask is: why is there a second sub shader? Thanks
     
  12. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    Nevermind, I get it. It's the same thing but in shaderlab language if the GLSL sub shader fails to work on the hardware running the game.
     
    Last edited: Jul 26, 2012
  13. Ross_S

    Ross_S

    Joined:
    Jul 10, 2012
    Posts:
    29
    i get
    Parse error: syntax error at line 78
    with this shader.
    Unity 4 -
    any clues?
     
  14. Lenn Dolling

    Lenn Dolling

    Joined:
    Jan 15, 2012
    Posts:
    15
    I just compiled it in Unity4 and code didn't break for me... I am using it in a particle effect material that is fed a "star" with transparent bg...I will try to compile on ios and see if the transparency is still retailed :)
     
  15. Lune

    Lune

    Joined:
    Oct 20, 2012
    Posts:
    62
    tested in OUYA and works perfect!! thank you Jessy!!! you will be in the credits of our game! (Dragon Maze)
     
  16. GarryJay

    GarryJay

    Joined:
    Nov 23, 2009
    Posts:
    42
    Thanks heaps Jessy!!! Bugged me for ages. I'll drink to you.
     
  17. Nub3h

    Nub3h

    Joined:
    Aug 23, 2012
    Posts:
    56
    Hi there,
    one question about this shader...if I want to remove the transparency from it which lines should I modify and how? I know nothing about shaders but I'm using a map plugin that is using this same exact shader to render the map planes and I want to remove the transparency because it lets me see through the map plane and I don't want that.

    thanks!
     
  18. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    I've been using Jessy's shader here for bullets and muzzle flash. However, I seem to get weird layering results. Despite the muzzleflash being positioned above the bullets, some of the bullets show through the muzzleflash gameobject depending on the angle you look at it!





    It's kind of a weird issue. Anyone know what's causing it?
     

    Attached Files:

    • $wklx.png
      $wklx.png
      File size:
      621.6 KB
      Views:
      6,628
    • $3x3.png
      $3x3.png
      File size:
      559 KB
      Views:
      6,608
    • $pdk.png
      $pdk.png
      File size:
      502.1 KB
      Views:
      6,552
  19. SpencerS

    SpencerS

    Joined:
    Jul 26, 2012
    Posts:
    3
    Jesse you are the best! Thanks man :)
     
  20. Khalanar_Dev

    Khalanar_Dev

    Joined:
    Mar 25, 2013
    Posts:
    34
    Same problem here, did you manage to fix that Essential?

     
  21. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
  22. camilosw

    camilosw

    Joined:
    Jun 9, 2013
    Posts:
    9
  23. Jaaan23

    Jaaan23

    Joined:
    Sep 29, 2014
    Posts:
    1
    thank you mate
     
  24. 3DJayC

    3DJayC

    Joined:
    Jul 29, 2014
    Posts:
    33
    Hello!

    Thanks for your shader, It works fine, but I can't animate the opacity. How can I do that?

    Thanks

    JC
     
  25. TheGabelle

    TheGabelle

    Joined:
    Aug 23, 2013
    Posts:
    242
    Hey there!

    I tested your shader on a quad as a means of rendering clouds. I change the texture offset to match my wind zone main speed multiplied by a reductive speed modifier. When I look straight up at the 3000 x 3000 quad, the shader works perfectly! As soon as I look down the offset stops changing. Also, there are moments when the clouds render strangely.

    Are these problems caused by the Unity 5 update? Do you know any workarounds for what I'm trying to accomplish?
     
  26. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    384
    Same issue here, I have a couple of fading materials which animating alpha doesn't work.

    Does someone know why this shader's Alpha can't be animated?
     
  27. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    384
    Does anyone know why I can't animate the Alpha value in this shader example? Or is this a bug with the animation tools?
     
  28. highpockets

    highpockets

    Joined:
    Jan 17, 2013
    Posts:
    71
    I just tried this shader on several 4 vertice quad objects and it's not batching for me. Any ideas?
     
  29. highpockets

    highpockets

    Joined:
    Jan 17, 2013
    Posts:
    71
    Sorry, its batching. I have another issue..
     
  30. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    Jessy, I am trying to use the shader you posted above, but I keep getting this error message

    GLSL compilation failed:
    ERROR: 0:16: 'varying' : syntax error: syntax error

    Any suggestions, anyone, how I could fix this?
     
  31. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,087
    I changed 2 lines and it started working:
    Code (CSharp):
    1. Shader "Somian/Unlit/Transparent" {
    2. Properties {
    3.     _Color ("Main Color (A=Opacity)", Color) = (1,1,1,1)
    4.     _MainTex ("Base (A=Opacity)", 2D) = ""
    5. }
    6. Category {
    7.     Tags {"Queue"="Transparent" "IgnoreProjector"="True"}
    8.     ZWrite Off
    9.     Blend SrcAlpha OneMinusSrcAlpha
    10.     SubShader {
    11.         Pass {
    12.             GLSLPROGRAM
    13.  
    14.             #ifdef VERTEX
    15.             varying mediump vec2 uv;
    16.             uniform mediump vec4 _MainTex_ST;
    17.             void main() {
    18.                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    19.                 uv = gl_MultiTexCoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    20.             }
    21.             #endif
    22.          
    23.             #ifdef FRAGMENT
    24.             varying mediump vec2 uv;
    25.             uniform lowp sampler2D _MainTex;
    26.             uniform lowp vec4 _Color;
    27.             void main() {
    28.                 gl_FragColor = texture2D(_MainTex, uv) * _Color;
    29.             }
    30.             #endif    
    31.             ENDGLSL
    32.         }
    33.     }
    34.  
    35.     SubShader {Pass {
    36.         SetTexture[_MainTex] {Combine texture * constant ConstantColor[_Color]}
    37.     }}
    38. }
    39. }
     
    Chubzdoomer and Fenikkel like this.
  32. sunwangshu

    sunwangshu

    Joined:
    Mar 2, 2017
    Posts:
    21
    Thanks! It works!

    Also it may help to drag an all white 128*128 texture as "Base" to the shadered material to make it all white. Because it's multiplying Color with Base by default so if there's no Base it seems to be around 50% transparent at max.
     
  33. FunkyBiskit

    FunkyBiskit

    Joined:
    Apr 8, 2018
    Posts:
    1
    You could also change line 4 to:
    Code (CSharp):
    1. _MainTex ("Base (A=Opacity)", 2D) = "white" {}
    This will set the texture to a blank white by default if no texture is provided in editor.
     
  34. friesam

    friesam

    Joined:
    Jan 12, 2018
    Posts:
    1
    Hi,

    I am trying to make an unlit transparent shader that will enable me to make an object opaque when an object is placed close to the camera.
    I was thinking if I could write a condition like this to set alpha to 0

    if (alpha == 1){
    alpha ==0
    }
    I do not know how I can accomplish this.

    Please does anyone have any suggestions?
     
  35. TheSwanCollective

    TheSwanCollective

    Joined:
    May 6, 2016
    Posts:
    26
    Oh my GOD! Tried to figure out forever why this shader (and also the standard diffuse shader set to fade) will be "self-transparent" even if Alpha is 0. Meaning for example, despite the object fading from invisible to full opaqueness, you could still see parts of the model that should be occluded. Like when you look at a tree you could see the hidden branches on the backside.

    So by noobishly trying out "ZWrite On" on line 9, this problem gets SOLVED! crazy I know. Thanks for the original code in any way!
     
  36. deenasty

    deenasty

    Joined:
    Nov 17, 2017
    Posts:
    3
    t
    Thanks man. You saved me a lot of work,10 years later and this is still working perfectly.
     
    Chubzdoomer, Kamyker and gterveen like this.