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
// 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.
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!
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.
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.
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
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!
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!