Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jaised · Jul 05, 2012 at 09:05 PM · shaderiosmobile

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

   
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image grojguy · May 09, 2013 at 07:31 PM 0
Share

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!

avatar image jaised · May 09, 2013 at 07:55 PM 0
Share

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.

avatar image grojguy · May 09, 2013 at 08:37 PM 0
Share

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

avatar image jaised · May 09, 2013 at 09:30 PM 0
Share

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

avatar image laurelhach · Mar 25, 2014 at 12:24 PM 0
Share

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?

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaised · Mar 25, 2014 at 08:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges