Throbbing emmisive shader

Does anyone know how i can get a pulsing glow/emmisive effect like what’s on the glowing blue plant on this video?

Shader "Custom/GlowingGrass"
{
	Properties
	{
		_MainTex ("Base (RGBA)", 2D) = "white" {}
		_FallOffTex ("FallOff (A)", 2D) = "white" {}
	}
	SubShader
	{
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert alpha
		#pragma only_renderers d3d9 opengl

		sampler2D _MainTex;
		sampler2D _FallOffTex;

		struct Input
		{
			float2 uv_MainTex;
			float3 worldPos;
		};

		void surf (Input IN, inout SurfaceOutput o)
		{
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			half fo = tex2D (_FallOffTex, IN.uv_MainTex).a;
			
			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
			o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);

			o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);
			
			o.Alpha = c.a;
		}
		ENDCG
	} 
	//FallBack "Diffuse"
}

“FallOff” is just gradient from the alpha channel.
1632268--100564--$grassAnim.gif

Thanks so much

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?

Dude what kind of question is that :smile: 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” :wink:

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

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.

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

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:

            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
            // o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);
            o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);

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:

            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
            o.Emission = fixed3(0,0,1)*(illum+pow(illum, 4)*5);
            o.Albedo = lerp(c.rgb, fixed3(0.25,0.25,1), illum);

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:

            fixed illum = fo*(((IN.worldPos.x+_SinTime.g*5)+(IN.worldPos.z+_CosTime.g*5))*0.5+1)*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!

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

Throwing hardware at the problem might be the cheapest optimization ;).

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

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.

I understand… u think I’m leeching :roll_eyes:

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?

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.

I wrote an article which explains why Cull Off is a bad idea.