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" 
      	_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
    	// pass for 4 vertex lights, ambient light & first pixel light
    	Tags { "RenderType"="Opaque" }
		LOD 200
      	#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
    	//Fallback "Specular"
  } // eo Shader

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.

No worries!
And by the way, I was not complaining about your shader :slight_smile:
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?