Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Throbbing emmisive shader

Discussion in 'Shaders' started by Deleted User, May 18, 2014.

  1. Deleted User

    Deleted User

    Guest

    Does anyone know how i can get a pulsing glow/emmisive effect like what's on the glowing blue plant on this video?
     
    Last edited by a moderator: May 18, 2014
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Code (csharp):
    1.  
    2. Shader "Custom/GlowingGrass"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Base (RGBA)", 2D) = "white" {}
    7.         _FallOffTex ("FallOff (A)", 2D) = "white" {}
    8.     }
    9.     SubShader
    10.     {
    11.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    12.         LOD 200
    13.        
    14.         CGPROGRAM
    15.         #pragma surface surf Lambert alpha
    16.         #pragma only_renderers d3d9 opengl
    17.  
    18.         sampler2D _MainTex;
    19.         sampler2D _FallOffTex;
    20.  
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.             float3 worldPos;
    25.         };
    26.  
    27.         void surf (Input IN, inout SurfaceOutput o)
    28.         {
    29.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    30.             half fo = tex2D (_FallOffTex, IN.uv_MainTex).a;
    31.            
    32.             fixed illum = fo*((sin(IN.worldPos.x+_Time.g*5)+cos(IN.worldPos.z+_Time.g*5))/2+1)*3;//try use here some random value from vertex color
    33.             o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);
    34.  
    35.             o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);
    36.            
    37.             o.Alpha = c.a;
    38.         }
    39.         ENDCG
    40.     }
    41.     //FallBack "Diffuse"
    42. }
    43.  
    44.  
    "FallOff" is just gradient from the alpha channel.
    $grassAnim.gif
     
    Last edited: May 19, 2014
  3. Deleted User

    Deleted User

    Guest

    Thanks so much
     
  4. Deleted User

    Deleted User

    Guest

    Why is it transparent? what part do i change to make it opaque and not have things at the back looking like they are at the front? ive tryed making the tags "transparent" into "opaque" but then the plants look like squares, not cut-outs.

    Edit: is this because my geometry is one sided? if so, how can i make it 2 sided?

    Also, this shader is'nt very optimized; is that just how it is or could it be improved?
     
    Last edited by a moderator: May 19, 2014
  5. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Dude what kind of question is that :D first off, if you KNOW it isn't optimized, just post your "optimized" version. Secondly, the code looks totally fine to me unless you're targeting Android 2.x / iPhone 3 or lower... the pow(something, 4) will probably best be left decided to the gpu/driver on how to run this. OK instead of a sin or cos with a _Time in it, one could try rewriting this with _SinTime/_CosTime built-in uniforms. But if you don't know how to make a transparent shader opaque, then you're probably premature in worrying about "optimizing" ;)
     
  6. Deleted User

    Deleted User

    Guest

    lol, okay; guess people don't like giving freebies. Ill go forth and learn shader writing, come back in a year, and post a better version
     
  7. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    They clearly do like that, as for proof look no further than mouurusai's contribution above.

    If you don't even know what could possibly be wrong or suboptimal with the above graciously-provided shader, I really can't imagine what makes you so sure a better version is possible and necessary. But yeah, good plan, go ahead, learning something hard and staring at error messages and oddball bugs on a daily for months does make for some humility. Rock on.
     
  8. Deleted User

    Deleted User

    Guest

    Its just so laggy. I don't actually know the details of what's wrong with it, but it drops the framerate 20 times lower than a simple bumped diffuse shader
     
  9. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Interesting.. haven't written it but from the looks should be fine on current-gen (2014) PC hardware or high-end mobile hardware. Always depends on many factors, overdraw, how much geometry is using the shader etc. etc. The 3 lines you can tweak to debug whats slowing things down are 31-34. First you can do like this:

    Code (csharp):
    1.  
    2.             fixed illum = fo * _SinTime.x; // fo*((sin(IN.worldPos.x+_Time.g*5)+cos(IN.worldPos.z+_Time.g*5))/2+1)*3;//try use here some random value from vertex color
    3.             // o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);
    4.             o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);
    5.  
    Will look weird but should be basically as fast as any built-in surface shader. If not, then it's the alpha blending. If it is fast enough, move to this setup:

    Code (csharp):
    1.  
    2.             fixed illum = fo * _SinTime.x; // fo*((sin(IN.worldPos.x+_Time.g*5)+cos(IN.worldPos.z+_Time.g*5))/2+1)*3;//try use here some random value from vertex color
    3.             o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);
    4.             o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);
    5.  
    If things get noticably slower, the pow is the problem -- then you can see if doing (illum * illum) * (illum * illum) instead of pow(illum, 4) makes a difference. But if it did, the gpu driver should rewrite this on the fly itself, so I doubt it. Now if things are still fast, then it might indeed be the original line 31 giving problems. In this case might be able to rewrite it as follows:

    Code (csharp):
    1.  
    2.             fixed illum = fo*(((IN.worldPos.x+_SinTime.g*5)+(IN.worldPos.z+_CosTime.g*5))*0.5+1)*3;
    3.  
    Not tested, just a random idea how to tweak a few things. Might look weird at first but you wanna see if performance changes in any way. So this would be how you go about tweaking-and-testing, have fun!
     
  10. Deleted User

    Deleted User

    Guest

    None of that improves the performance; however, i found turning pixel light count to 0 in project quality settings makes the grass have very little impact on performance - I dont know what this means is going wrong because I don't understand most of the shader (i have next to no knowledge on shader scripting), but perhaps you can tell me why this. also i am in deferred rendering mode
    By the way, I have terrible Intel graphics 3000

    also by the way, i do beleive The shader is fine performancewise, just not right now for me - i made a similar but more simple shader in shaderforge, and it had an even bigger impact on performance - i used the same multiplying sin by time thingy
     
    Last edited by a moderator: May 20, 2014
  11. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Throwing hardware at the problem might be the cheapest optimization ;).
     
  12. Deleted User

    Deleted User

    Guest

    But ive found how to eradicate the massive performance loss - i need someone clever enough to find how it can be fixed without turning pixel light count to 0
     
  13. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Well, yes, if there are no lights to render, you're bound to get better performance.

    If you were to show interest in improving the performance of the shader yourself, I'd suggest creating a custom vertex/fragment shader, that only applies lightmapping, spherical harmonics from lightprobes, and vertex lights (no lighting calculations in the fragment shader -> no 'pixel lights').

    But you say you need someone clever enough to figure out how it can be improved, so I recommend you check out the Commercial Section of this forum.


    I think it's important for your to realize how lucky you were that mouurusai provided you with a shader so similar to what you're looking for just like that. Right now it comes across to me as if you feel like people should simply do your work for you.

    If there are already free shaders out there that achieve the effect you're looking for, great, have fun using them. But please try to understand the work involved in understanding how the rendering engine works, and making it do the things you want it to do.
     
  14. Deleted User

    Deleted User

    Guest

    I understand.. u think I'm leeching :rolleyes:

    But please, i have one last request.
    I used "cull off" to get the backfaces showing, but i still get this weird transparency
    https://d1wst0behutosd.cloudfront.net/videos/25748.mp4
    Whats that about?
     
  15. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    If by that you mean: It simply feels as if you're not prepared to put any effort in. Then yes.

    For example, this question has been asked a whole lot, and if you're prepared to put some time into it, you'll find the answer relatively easily:
    Hint: think about how lighting is calculated relative to surface normals.
     
  16. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355