- Home /
iOS Custom Specular Shader
How's it going? So I wrote a shader (below) that is customizable in the Unity Editor. It is essentially a mobile BlingPhong Spec Shader. I made this because the Unity mobile->Specular doesn't pick up not-important lights. I am wondering how I can spruce this up to be more efficient on the iPad, mostly because I'm getting 30fps. I tried to see it's stats under the NVIDIA Shader Perf but that would require a complete re-work of the shader to conform to their standards, and frankly I just don't know how to do it. If you have an idea where it might be bottle-necking or how to make this better, I would greatly appreciate your input.
// Specular, Normal Maps with Main Texture
// Fragment based
Shader "SpecTest/SpecTest5"
{
Properties
{
_Shininess ("Shininess", Range (0, 1.5)) = 0.078125
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0, 0, 0, 0)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bump Map", 2D) = "bump" {}
_NormalStrength ("Normal Strength", Range (0, 1.5)) = 1
} // eo Properties
SubShader
{
// pass for 4 vertex lights, ambient light & first pixel light
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf MobileBlinnPhong
fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
fixed diff = saturate(dot (s.Normal, lightDir));
fixed nh = saturate(dot (s.Normal, halfDir)); //Instead of injecting the normalized light+view, we just inject view, which is provided as halfasview in the initial surface shader CG parameters
fixed spec = pow (nh, s.Specular*128) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _SpecColor.rgb * spec) * (atten*2);
c.a = 0.0;
return c;
}
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
// User-specified properties
uniform sampler2D _MainTex;
uniform sampler2D _BumpMap;
uniform float _Shininess;
uniform float _NormalStrength;
uniform fixed4 _Color;
float3 expand(float3 v) { return (v - 0.5) * 2; } // eo expand
void surf (Input IN, inout SurfaceOutput o) {
half4 tex = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = tex.rgb;
o.Gloss = tex.a;
o.Alpha = tex.a;
o.Specular = _Shininess;
// fetch and expand range-compressed normal
float3 normalTex = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
float3 normal = normalTex * _NormalStrength;
o.Normal = normal;
} // eo surf
ENDCG
}
//Fallback "Specular"
} // eo Shader
Answer by jaised · Jul 06, 2012 at 07:13 PM
Well, I fixed it. For anyone who is interested, just use a directional light with low intensity and use NOT IMPORTANT point lights to spice up the areas where more is needed. This shader offers many nice customizable features where normals in geometry is a must.
Thanks Jaised! Worked great for me. You might want to edit the code part of your post though - strangely, everything before the CGPROGRA$$anonymous$$ line is globbed together on one line and could be confusing to some not familiar with writing shaders.
I would definitely like to know out to profile this to see how it performs, in comparison to the standard Bumped Spec and $$anonymous$$obile Bumped Spec shaders. But nevertheless, excellent work and thank you for sharing!
Updated the post, sorry, I don't remember it looking like that. Thank you for your kind words. I am glad that I helped you.
Excellent! Thanks again for taking the time to post this - it was a BIG help to my project! I didn't see any responses, so I wanted to make sure you received proper props! It looks much better than the standard mobile bumped spec shader, and performance is great as far as I can tell! I am trying to learn shader writing so I can write things like this also. I suggest you post this on the Unity/UnifyCommunity Wiki Shader page HERE
Thank you and glad that the shader helped you out. It's always rewarding to hear that my work has made someone else's life easier. I applied for an account on there and we will see if it's accepted. If it is then I will most certainly post the shader there. All I can say is that I spent a while writing it and it was my first one. =P
Hi Jaised,
I know this is an old thread but I was wondering if you had any issues with this shader on ipad2. The fps is really slow for me when I use your shader ( I run at 22fps ins$$anonymous$$d of 30). If I change it for the default one from unity (mobile/diffuse-bump) then I run at 30fps. Running on Ipad Air is fine, I hit 30fps.
I really like your shader :) so I'd like to keep it.
Do you have any idea why?
Answer by laurelhach · Mar 25, 2014 at 07:58 PM
No worries! And by the way, I was not complaining about your shader :) I really like it as it offers good options. I have been looking around for quite a long time and I couldn't find something that works well for iPad2 anyway. My draw calls are almost reduced by 3 when I don't use your shader (with a default mobile one). So I think my major issue is really having a good shader. I will probably offer an iPad 2 version with low end result (but still good for playing) and another one for ipad3+.
Btw, nobody asked you, but do you mind if I use your shader in my project?
Thanks, Laurel.
Yeah, that seems to be a route that many are taking. However, it becomes a hassle to maintain 2 versions/branches of the game. I might suggest using code to swap the shaders(material) at run-time based on the device being used. This is similar with using HD and SD atlases due to resolution differences. It is your project, so feel free to do as you wish, just giving you a heads up from experience.
Secondly, I would be obliged if you used it. $$anonymous$$any people have helped me throughout the years and glad that I can give back. Thanks for your kind words and I am glad that it does what you like. I had searched for a long time as well, and I needed something somewhat specific - so I made my own. Thanks again, and good luck with your project. =D
Your answer
Follow this Question
Related Questions
Different Shader Display on 3gs vs. 4s 2 Answers
Unwanted rendering on top of all objects!? 0 Answers
Mobile Shader for Spotlight 0 Answers
Mobile controlled transparency shader 0 Answers
Shader optim ? 1 Answer