• 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 treelander · Jan 03, 2016 at 09:04 AM · shaderscg

_WorldSpaceLightPos0.xyz always 0

The following line works perfectly when using Directional Light, yet when I switch to Pointlight, the value returned is always 0.0 .

 float4 frag(vertexOutput i) : COLOR
 {
         lightDirection = normalize(_WorldSpaceLightPos0.xyz);
         return float4(lightDirection, 1.0); 
 }

Actually, I'd like to use _WorldSpaceLightPos0 to get the vector between it and the world coords (which are being calculated in the vertex function), which does not work, since _WorldSpaceLightPos0 is always zero when using Point Light. Using Tags{ "RenderType" = "ForwardBase" } . What am I missing?

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 treelander · Jan 03, 2016 at 10:21 AM 0
Share

I just stumbled upon the builtin helper functions of UnityCG.cginc, sadly with the same results.

             vertexOutput vert(vertexInput v) 
             {
                 vertexOutput o;
                 // more stuff here....
                 o.posWorld = mul(_Object2World, v.vertex);                
                 return o;
             }
 
             float4 frag(vertexOutput i) : COLOR
             {
                 // more stuff here... not important to question
                 return float4(WorldSpaceLightDir(i.posWorld), 1.0);
             }

Spits out colors, as expected, when using Directional Light, yet it remains black using Point Light.

avatar image treelander · Jan 03, 2016 at 10:31 AM 0
Share

Furthermore, I now came accross unity_LightPosition[0].xyz which simply returns (0, 0, 1), regardless of the lights position.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jan 03, 2016 at 06:14 PM

The "ForwardBase" pass is used to apply all of a scene's generic, presumably single-use lights. This generally means ambient lighting and a single, primary directional light are applied at this time, all in one pass.

Point Lights (and other non-directional lights, for that matter) are assumed not to be primary light sources. As a result, none of them will be handled in the ForwardBase pass, but instead in the ForwardAdd pass.

Because the lights aren't handled in ForwardBase, there's no information being sent to the shader about them. Therefore, the position data defaults to a zero vector.

Regarding the unity_LightPosition array, that is used for vertex lighting. The values are applied in view space, so you're seeing fallback values of (0, 0) across the screen and 1 depth.

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
avatar image
0

Answer by glupsk · Jan 20 at 09:38 PM

"RenderType" = "ForwardBase" is wrong. It should be "LightMode" = "ForwardBase". So your SubShader pass should something like this:

 Shader "Shader name here"
 {    
     SubShader
     {
         Tags { "RenderType" = "Opaque" "Queue" = "Geometry" }
         Pass
         {
             Name "FORWARD"
             Tags { "LightMode" = "ForwardBase" }
 
             // Shader code here
         }
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GrabPass twirl shader not centering correctly 0 Answers

Can this shader run in unity? 1 Answer

Access to svPosition in fragment shader 1 Answer

[Shader] Usage #ifdef DIRECTIONAL 0 Answers

Geometry Shader 3 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