• 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 temptest123 · Jan 13, 2013 at 12:44 AM · lightinglightshaderlabcg

_ObjectSpaceLightPos[0] not working

I try to access _ObjectSpaceLightPos[0] in a CG shader, as it should be defined according to http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html. I know lighting should be done through surface shaders, but I want to have only very particular light calculations that only need to work with a single light source.

If I add _ObjectSpaceLightPos[0], I get "undefined variable _ObjectSpaceLightPos".

What's the catch?

 Shader "Lightingtest" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
 }

 SubShader { 
     Tags { "RenderType"="Opaque"}
     LOD 400
     
     Pass {
     
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma fragmentoption ARB_precision_hint_fastest
         #include "UnityCG.cginc"
         
         
         struct appdata_t {
             float4 vertex : POSITION;
         };
         
         struct v2f {
             float4 vertex : POSITION;
         };
         
         v2f vert (appdata_t v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             return o;
         }
         
         half4 frag( v2f i ) : COLOR
         {
             return _ObjectSpaceLightPos[0];
         }
         ENDCG
     }
 }
 }
Comment
Add comment · Show 1
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 mayorc1978 · Feb 12, 2014 at 06:12 PM 0
Share

I get same problem as yours, and it's driving me mad. I tried to use that in the vertex part of the shader, but it doesn't work. Only thing I was able to use is: unity_LightPosition[0].xyz But I've no information on what it defines and in what space it is defined and if it actually works or not.

_WorldSpaceLightPos0 works by default and UnityCG.cginc doesn't even need to be included for it to work as expected, but why can't I get to use the ObjectSpaceLight position ???

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mayorc1978 · Feb 12, 2014 at 06:58 PM

I discovered the arcane by looking at the UnityCG.cginc file.

It seems you can get it using this notation:

ObjSpaceLightDir(v.vertex);

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 KEric · Apr 23, 2014 at 07:19 AM

Add the following in the Pass: Tags { "LightMode" = "Pixel" }

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

10 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

Related Questions

ShaderLab builtin lighting properties are not correct? 0 Answers

Problem with lighting in CG Shader 1 Answer

How do I keep an object lit when a light moves past it? 0 Answers

2d volumetric lighting, light behind background 1 Answer

Changing scene messes up lights 6 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