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
0
Question by maxxa05 · Aug 31, 2012 at 11:24 PM · shaderlabnormalsurface shaderinterpolation

Normal vectors not interpolating in custom lighting surface shaders

I am messing around with custom lighting in surface shaders, but it seems that I can't make my specular highlights as smooth as BlinnPhong specular. If I put my shader on a basic sphere, the reflection acts on the rough geometry, while BlinnPhong seems to interpolate the normal vectors so the lighting appears smoother. I tried to use the BlinnPhong function from lighting.cginc, but the interpolation seems to come from elsewhere. I succeeded to get some interpolation by unpacking a blank normal map, but it seems to create glitches on spheres.

here's my shader:

 _MainTex("Base (RGB), Spec(A)", 2D) = "bump" {}
 _NormalMap ("Normal", 2D) = "bump" {} //Texture 2D bump par défaut, pour la normal
 _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
 _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
 _LC("Lit Center", Color)  = (1,1,1,1)
 _UC("Unlit Center", Color) = (1,1,1,1)
 _LR("Lit Rim", Color)= (1,1,1,1)
 _UR("Unlit Rim", Color) = (1,1,1,1)
 _RimPower("RimPower",Float)=0.0
     }
     
     SubShader 
     {
     Tags { "RenderType"="Opaque"}
     LOD 200
 
         CGPROGRAM
 #pragma surface surf Ramp
 #pragma target 3.0
 
 
 sampler2D _MainTex;
 half _Shininess;
 sampler2D _NormalMap;
 float4 _LC;
 float4 _UC;
 float4 _LR;
 float4 _UR;
 float _RimPower;
 
 struct Input {
     float2 uv_MainTex;
 };
 
 half4 LightingRamp (SurfaceOutput s, float3 lightDir, half3 viewDir, half atten)
             {
             half3 h = normalize (lightDir + viewDir);
             float nh = max (0, dot (s.Normal, h));
             float spec = pow (nh, s.Specular*128.0) * s.Gloss;
             float NdotL=dot(s.Normal, lightDir);
             float NdotE=pow(saturate(dot(s.Normal, viewDir)),_RimPower);
             float diff=(NdotL*0.5)+0.5;
             float4 c;
             c.rgb=(s.Albedo*_LightColor0.rgb*lerp(lerp(_UR,_UC,NdotE),lerp(_LR,_LC,NdotE),diff)+_LightColor0.rgb * _SpecColor.rgb * spec)*atten*2;
             c.a=1;
             return c;
             }
             
 
             
             
 void surf (Input IN, inout SurfaceOutput o) 
         {
             float4 c=tex2D(_MainTex,IN.uv_MainTex);
             o.Albedo=c.rgb;
             o.Alpha=c.a;
             o.Gloss=c.a;
             o.Specular = _Shininess;
             o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
         }
         ENDCG
     }
     FallBack "Specular"
 }

alt text

normal.jpg (14.8 kB)
Comment
Add comment · Show 3
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 ScroodgeM · Aug 31, 2012 at 11:27 PM 0
Share

does your model has tangents and normals?

avatar image ScroodgeM · Aug 31, 2012 at 11:33 PM 0
Share

shader without normals works O$$anonymous$$ on built-in sphere. so if you got another effect, check your model. if you still stuck - post screenshot of what you got

avatar image maxxa05 · Sep 01, 2012 at 12:09 AM 0
Share

added a photo, left one with blank normal map, right one without. The picture doesn't show the glitches on the left one.

I noticed that BlinnPhong alpha doesn't interpolate like BlinnPhong without alpha.

1 Reply

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

Answer by maxxa05 · Sep 01, 2012 at 05:56 PM

Got it, had to put

 s.Normal=normalize(s.Normal);

at the beginning of the lighting function. The normal values were interpolated, but not normalized. Now I have a perfect round specular highlight on a sphere.

Comment
Add comment · Show 2 · 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 BmB23 · Aug 31, 2014 at 09:38 AM 0
Share

Perfect! Thank you for this.

avatar image jitwtttl · Mar 12, 2015 at 11:14 AM 0
Share

Thank you! It works perfectly.

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

Not interpolating surface shader normals (by purpose) 1 Answer

Simple cartoon water shader shoreline 1 Answer

Refraction Surface Shader Upside down 1 Answer

How to use world-space normals in surface shader 0 Answers

Project UVs from arbitrary direction vector in shader 0 Answers


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