Emissive Colour

Hi everyone! A while ago I managed to add an emissive map to the mobile bump/spec shader. I would really like the functionality to be able to change the colour of the emissive map, though; either with some kind of colour picker or hue/saturation controller. The values would then be changed with a script. I’ve tried adding some code from other shaders but haven’t managed to make it work yet.

My alternative is to create an additional emissive map for each colour that I want to use (blue, green, yellow, red, purple, white), but that seems like a really dirty way to do it!

Any help would be appreciated! Cheers :slight_smile:

// Simplified Bumped Specular shader. Differences from regular Bumped Specular one:
// - no Main Color nor Specular Color
// - specular lighting directions are approximated per vertex
// - writes zero to alpha channel
// - Normalmap uses Tiling/Offset of the Base texture
// - no Deferred Lighting support
// - no Lightmap support
// - Emissive texture and intensity control

Shader "Mobile/Bump Specular Emissive" {
Properties {
	_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
	_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_Emissive("Emissive", 2D) = "black" {}
	_EmissiveIntensity("_EmissiveIntensity", Range(0,10) ) = 0.5
}
SubShader { 
	Tags { "RenderType"="Opaque" }
	LOD 250
	
CGPROGRAM
#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap halfasview
// Add noforwardadd to the above line if there is a hardware crash

inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
	fixed diff = max (0, dot (s.Normal, lightDir));
	fixed nh = max (0, dot (s.Normal, halfDir));
	fixed spec = pow (nh, s.Specular*128) * s.Gloss;
	
	fixed4 c;
	c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2);
	c.a = 0.0;
	return c;
}

sampler2D _MainTex;
sampler2D _BumpMap;
half _Shininess;
sampler2D _Emissive;
float _EmissiveIntensity;

struct Input {
	float2 uv_MainTex;
	float2 uv_Emissive;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
	o.Albedo = tex.rgb;
	o.Gloss = tex.a;
	o.Alpha = tex.a;
	o.Specular = _Shininess;
	o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_MainTex));
	
	float4 Tex2D1=tex2D(_Emissive,(IN.uv_Emissive.xyxy).xy);
	float4 Multiply2=Tex2D1 * _EmissiveIntensity.xxxx;
	
	o.Emission = Multiply2;
}
ENDCG
}

FallBack "Mobile/VertexLit"
}

If you want to change the colour of the emission, just use a colour instead of an intensity. If you download the Unity shader source code, you can see in a lot of the shaders how they take the texture in, then multiply with a colour before assigning to Albedo or Emission.

I really know nothing about shaders, so I’m at a bit of a loss. The best I can do right now is butcher code and hope for the best.

I’ve tried adding this line from another shader to my properties:

_Color ("Main Color", Color) = (1,1,1,1)

and this somewhere at the end of my shader, but I’m getting Syntax bombed:

SubShader {Pass {
		SetTexture[_Emissive] {Combine texture * constant ConstantColor[_Color]}
}

Okay, I finally got this thing working. This would be suitable for things like glowing team colours, changing the lighting on windows, etc. Feel free to go through and rip this shader apart for me, as I’m sure it could be better! :slight_smile:

// - Simplified Bump/Specular shader with emissive texture, colour and intensity control

Shader "Mobile/Bump Specular Emissive Colour" {
Properties {
	_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
	_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_Emissive("Emissive", 2D) = "black" {}
	_EmissiveColor("_EmissiveColor", Color) = (1,1,1,1)
	_EmissiveIntensity("_EmissiveIntensity", Range(0,10) ) = 0.5
}
SubShader { 
	Tags { "RenderType"="Opaque" }
	LOD 250
	
CGPROGRAM
#pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap halfasview

inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
	fixed diff = max (0, dot (s.Normal, lightDir));
	fixed nh = max (0, dot (s.Normal, halfDir));
	fixed spec = pow (nh, s.Specular*128) * s.Gloss;
	
	fixed4 c;
	c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2);
	c.a = 0.0;
	return c;
}

sampler2D _MainTex;
sampler2D _BumpMap;
half _Shininess;
sampler2D _Emissive;
float4 _EmissiveColor;
float _EmissiveIntensity;

struct Input {
	float2 uv_MainTex;
	float2 uv_Emissive;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
	o.Albedo = tex.rgb;
	o.Gloss = tex.a;
	o.Alpha = tex.a;
	o.Specular = _Shininess;
	o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_MainTex));
	
	float4 Tex2D1=tex2D(_Emissive,(IN.uv_Emissive.xyxy).xy);
	float4 Multiply0=Tex2D1 * _EmissiveColor;
	float4 Multiply2=Multiply0 * _EmissiveIntensity.xxxx;
	
	o.Emission = Multiply2;
}
ENDCG
}

FallBack "Mobile/VertexLit"
}
1 Like

The shader is pretty much fine, but you don’t really need the intensity anymore, if you change the emissive colour to black, it’s effectively the same as having an intensity of 0. It’ll just be a little optimisation to make it faster.

Thanks MADmarine :slight_smile:

Hi Horror,

You know I have been trawling the internet for days looking for a way to control the amount of emissive light, and your shader has it.
Why there isn’t a shader like this built into unity I do not know. Also what I like about this shader is this. I can use a separate emissive map instead of using the alpha channel. Much more to my liking. I think I can work out how to add specular myself as well.

I do have a question though. Is there a way for the light to glow or to give off a bloom. Would this be easy to do? Or something like a neon light?

Any help would be appreciated. I am trying to learn this shader stuff, but a lot of it is above my head.

Asimov

Hey Asimov500,

Glad you found the shader useful! I agree, it can be quite irritating for us non-shader savvy guys when we need a particular effect not included with Unity. I don’t know why they don’t provide a solution like UDK’s material editor with support for mobile. Sooner or later we’ll get one, I’m sure :wink:

As for bloom, this is usually done with post-processing and it can really kill the performance of your game on mobile. If you want to experiment, you could try the package posting by Ole in this thread.

The Angry Bots demo also comes with a bloom effect but I couldn’t work out what files I needed to migrate to my own project to get it working properly, short of copying the whole project into mine!

I’m two years late but this is a great shader. It’s gonna help immensely. Great job!

The thing (a Shader Node Editor) most close to UDK Materials editor is ShaderForge https://www.assetstore.unity3d.com/#/content/14147 , still though it’s not optimized for mobile output, and that’s why I’m waiting before buying, because I don’t know if it will obtain the level of optimization I look for, and at the moment you cannot create vertex/frag shaders with it, just Surface Shaders.

By the whay, why are you writing uvs like this: (``IN``.``uv_Emissive``.``xyxy``)``.xy
uv_Emissive is already a two components vector, you don’t need masking any component of the vector itself.
In this specific case the compiler will optimize the result so it will be the same even if you will not correct that.
But beware, sometimes if you don’t write stuff correctly, you’ll end up wasting GPU cycles particularly on mobile platforms.

Thank you, mister Horror!!! I just don’t understand damn unity developers… why the f**k then can’t put usable shaders into default content… thanks again!

THANK YOU… YOU SAVED ME… You’ll Be credited in my game for sure and send you a message when it’s released… keep up the good work :wink: