Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 HooksForFeet · Feb 15, 2021 at 10:54 AM · parametersdocumentationsurface shaderargumentssurfaceshader

Is the documentation for surface shader custom lighting functions wrong?

In the documentation for Custom lighting models in Surface Shaders it is stated that the functions used to define your custom lighting model should conform to one of four conventions, depending on the information you need:

  - half4 Lighting<Name> (SurfaceOutput s, UnityGI gi); 
  - half4 Lighting<Name> (SurfaceOutput s, half3 viewDir, UnityGI gi);  
  - half4 Lighting<Name>_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal);  
  - half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light);

The documentation for Surface Shader lighting examples however uses entirely different parameters:

 half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten);
 half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)

Is the documentation wrong or incomplete? It doesn't describe the attenuation or light direction parameters at all, so I have no idea how to interpret the former, nor can I be sure if the latter, for instance, is normalized.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Feb 15, 2021 at 09:01 PM

The former is more recent and how surface shaders probably should be written; the latter hasn't really changed since surface shaders were introduced nearly a decade ago, however both will still work just fine. 'lightDir' and 'viewDir' are both normalized (and in world space - might not seem like a necessary clarification but it is), 'atten' is the combined distance, shadow and cookie attenuation for any given light.

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 HooksForFeet · Feb 16, 2021 at 08:24 PM 0
Share

But if the former is the most recent version, how does one get the light direction and attenuation when using those methods?

avatar image Namey5 HooksForFeet · Feb 16, 2021 at 10:08 PM 0
Share

Those parameters are now part of the UnityGI struct that is passed instead;

 struct UnityLight
 {
     half3 color;
     half3 dir;
     half  ndotl; // Deprecated: Ndotl is now calculated on the fly and is no longer stored. Do not used it.
 };
 
 struct UnityIndirect
 {
     half3 diffuse;
     half3 specular;
 };
 
 struct UnityGI
 {
     UnityLight light;
     UnityIndirect indirect;
 };

So in this case, you would write;

 half4 LightingCustom (SurfaceOutput s, half3 viewDir, UnityGI gi)
 {
     half3 lightDir = gi.light.dir;
 
     half diff = max (0, dot (normalize (s.Normal), lightDir));
 
     //At a guess, atten seems to be baked into light.color
     half4 c;
     c.rgb = (s.Albedo.rgb * gi.light.color) * diff;
     c.a = s.Alpha;
     return c;
 }

I don't remember if Unity has an automatic GI function, so you may need to define your own.

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

113 People are following this question.

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

Related Questions

Dynamic Parameters/Arguments 1 Answer

How to add Zbuffer to Custom Surface Shader 1 Answer

why does IN.screenPos in a surface shader have different Z values on Windows and Android? 2 Answers

What happens when the alpha value is outside the 0..1 range in surface shader lighting models? 0 Answers

Get local position in surface shader 2 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