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 Professor Snake · Jun 27, 2013 at 08:12 PM · shaderlightvertexnormal

Getting vertex data from lighting function in shaders.

Hello there,
I recently started programming my own shaders, and decided to start looking into the custom lighting functions. I currently want to solve the issue of having parts of the unlit side of a mesh be lit due to their normal maps. alt text

In order to solve the issue, i figured it would be as simple as both taking the current normal's (after applying the normal map) dot product with the lighting angle, as well as the vertex's normals (without the normal map) into consideration when applying lighting.

So, i want to do the following (semi-pseudocode):

 if((dot (s.Normal, lightDir))>0.2&&dot(vertex.Normal,lightDir)>-0.1)
 c.rgb=//blah
 else
 c.rgb=float3(0,0,0);

However, i have no idea how to actually get the normal of the current vertex from the lighting function, which is what i would like help with.

Entire lighting function below:

 half4 LightingChoppedLambert (SurfaceOutput s, half3 lightDir, half atten) {
           half NdotL = dot (s.Normal, lightDir);
           half diff = (NdotL);
           half4 c;
           if(NdotL>0.2/*&&(*something*)*/)
           c.rgb = (s.Albedo) * (_LightColor0.rgb) * ((diff) * atten *2);
           else
           c.rgb=float3(0,0,0);
           c.a = _LightColor0.a ;
           return c;
       }

(Yes, the lighting model is supposed to have steep colour changes based on the lighting, "else c.rgb=float3(0,0,0);" is completely intentional.)

Comment
Add comment · Show 2
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 Jessy · Jun 27, 2013 at 08:18 PM 0
Share

This is going to look really bad at the transition. You should use self-shadowing ins$$anonymous$$d.

avatar image Professor Snake · Jun 27, 2013 at 09:47 PM 0
Share

That's pretty much how i want it to look at the transition, really.

1 Reply

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

Answer by OP_toss · Aug 22, 2013 at 09:25 PM

The current normal is the vertex normal, interpolated between vertices. By writing to the Normal, you're writing over the per-pixel normal, before lighting takes it into account.

I don't think it's possible to get non-interpolated Vertex normals, because that just doesn't make sense, since you're working with pixels.

So I assume you want interpolated vertex normals PRIOR to applying your tangent-space normal map? Apparently if you write to o.Normal, the normals and lighting is all calculated in Tangent-Space, meaning that the default vertex normal would just be (0,0,1). That's all you should need to do your calculation I think.

Sorry I don't have time to do the code and check it for you. But this should tell you enough to figure it out. Hope this helped!

Comment
Add comment · 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

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

17 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

Related Questions

Color depending on vertex normal 1 Answer

Problem of color in custom vertex shader 0 Answers

how can i catch the sky box and do a reflection in object 0 Answers

Why does my shader only shade objects based on their normals in the X axis? 1 Answer

How to make projector/light shader "alpha enabled"? 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