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 Slin · Jun 26, 2010 at 12:46 AM · shaderpositionlightuniforms

How to access the lights properties from within a shader?

I am searching and trying around for about an hour now and am not able to solve it. I am just looking for a way to get the properties (in my case only position/direction and color) of the only light (directional) in my level. I found glstate.light[0].position to glstate.light[3].position and _ObjectSpaceLightPos. But glstate.light[0].position to glstate.light[3].position are all 0 as well as _ObjectSpaceLightPos0, while _ObjectSpaceLightPosN doesn is not defined as well as _ObjectSpaceLightPos[N].

I dont want to do anything else than to write a basic per pixel lighting shader (it is actually a bit more, but everything else is already working fine).

Please help me :) Thanks.

Just in case it helps, here is the whole code:

Shader "World Multi Texturing" { Properties { _MainTex0 ("Base 0 (RGB)", 2D) = "white" {} _MainTex1 ("Base 1 (RGB)", 2D) = "white" {} _MainTex2 ("Base 2 (RGB)", 2D) = "white" {} _MainTex3 ("Base 3 (RGB)", 2D) = "white" {} _MainTex4 ("Base 4 (RGB)", 2D) = "white" {}

     _BlendTex ("Blend (RGB)", 2D) = "black" {}

     _LightMap ("Light (RGB)", 2D) = "white" {}
 }

 SubShader
 {
     Pass
     {
         Lighting On

         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag

             #include "UnityCG.cginc"

             uniform sampler2D _MainTex0;
             uniform sampler2D _MainTex1;
             uniform sampler2D _MainTex2;
             uniform sampler2D _MainTex3;
             uniform sampler2D _MainTex4;
             uniform sampler2D _BlendTex;
             uniform sampler2D _LightMap;

             struct v2f
             {
                 float4 position : POSITION;
                 float3 normal : COLOR;
                 float2 texblend : TEXCOORD0;
                 float2 texcoord : TEXCOORD1;
             }; 

             v2f vert(appdata_base In)
             {
                 v2f Out;

                 Out.position = mul(glstate.matrix.mvp, In.vertex);
                 Out.normal = mul(_Object2World, float4(In.normal, 0.0f)).xyz;

                 Out.texblend = (mul(_Object2World, In.vertex).xz+4.0f)/256.0f;
                 Out.texcoord = In.texcoord.xy;

                 return Out;
             }

             half4 frag(v2f In) : COLOR
             {
                 float4 color0 = tex2D(_MainTex0, In.texcoord);
                 float4 color1 = tex2D(_MainTex1, In.texcoord);
                 float4 color2 = tex2D(_MainTex2, In.texcoord);
                 float4 color3 = tex2D(_MainTex3, In.texcoord);
                 float4 color4 = tex2D(_MainTex4, In.texcoord);

                 float4 blend = tex2D(_BlendTex, In.texblend);
                 float4 light = tex2D(_LightMap, In.texblend);

                 float4 color = lerp(color0, color1, blend.r);
                 color = lerp(color, color2, blend.g);
                 color = lerp(color, color3, blend.b);
                 color = lerp(color, color4, blend.a);

                 In.normal = normalize(In.normal);
                 float diffuse = 0.5;
                 diffuse += saturate(dot(normalize(glstate.light[0].position.xyz), In.normal));
                 diffuse += saturate(dot(normalize(glstate.light[1].position.xyz), In.normal));
                 diffuse += saturate(dot(normalize(glstate.light[2].position.xyz), In.normal));
                 diffuse += saturate(dot(normalize(glstate.light[3].position.xyz), In.normal));

                 return half4(color.rgb*light.rgb*diffuse, 1.0);
             }
         ENDCG
     }
 }

 FallBack "Diffuse"

}

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
2
Best Answer

Answer by Aras · Jun 30, 2010 at 06:09 AM

I guess I at least partially answered your question in this lengthy post on Unity forums: http://forum.unity3d.com/viewtopic.php?p=343170#343170

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 Slin · Jul 01, 2010 at 08:15 PM 0
Share

Yes, that of course answered my question. Thanks again for that post :).

avatar image allenwp · Sep 25, 2014 at 01:39 AM 0
Share

Hmm... Looks like that post has disappeared. Let us know if anyone finds its contents somewhere else...

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

1 Person is following this question.

avatar image

Related Questions

Modify Light Projector Shader to be Additive 0 Answers

I need help with 2D light effect by mask or something, please! 1 Answer

Getting vertex data from lighting function in shaders. 1 Answer

Block/Obscure light without shadows 0 Answers

bumped diffuse Shader not behaving as expected (reversed) 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