• 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 Design.PierreFX · Jul 05, 2013 at 12:05 AM · shaderspoint lightspot lightvertexshaderpixel light

Custom vertex/fragment shader, renders box around point light at the edge of it's range. How can I fix this?

I wrote this basic vertex/fragment shader. It's working all right except that it renders a box at the edge of any point light's range.

         //user defined vars
         uniform sampler2D _MainTex;
         uniform float4 _MainTex_ST;
         uniform float4 _Color;
         uniform float4 _SpecColor;
         uniform float _Gloss;
         //uniform float4 _RimColor;
         uniform float _Shininess;
         //uniform float _RimPower;
         
         //unity defined vars
         uniform float4 _LightColor0;
         
         //base input structs
         struct vertexInput{
             float4 vertex : POSITION;
             float3 normal : NORMAL;
             float4 texcoord : TEXCOORD0;
         };
         
         struct vertexOutput{
             float4 pos : SV_POSITION;
             float4 tex : TEXCOORD0;
             float4 posWorld : TEXCOORD1;
             float3 normalDir : TEXCOORD2;
         };
         
         //vertex function
         
         vertexOutput vert(vertexInput v){
             vertexOutput o;
             
             o.posWorld = mul(_Object2World,v.vertex);
             o.normalDir = normalize( mul( float4( v.normal, 0.0), _World2Object ).xyz );
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.tex = v.texcoord;
             
             return o;
         }
         
         //fragment function
         
         float4 frag(vertexOutput i) : COLOR {
             
             float3 normalDirection = i.normalDir;
             float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz );
             float3 lightDirection;
             float atten;
             float4 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
             
             //START NEEDS TO BE OPTIMIZED!!
             if(_WorldSpaceLightPos0.w == 0.0){ //directional
                 atten = 1.0;
                 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
             }
             else{
                 float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
                 float distance = length(fragmentToLightSource);
                 atten = 1.0/distance;
                 lightDirection = normalize(fragmentToLightSource);
             }
             //END NTBO
             
             float3 diffuseReflection = atten * _LightColor0.xyz * saturate( dot( normalDirection, lightDirection ) );
             float3 specularReflection = diffuseReflection * _SpecColor.xyz * _Gloss * pow( saturate ( dot ( reflect (-lightDirection, normalDirection ),viewDirection ) ),_Shininess );
             
             //rim lighting
                 //float rim = 1 - saturate(dot(normalize(viewDirection),normalDirection));
                 //float3 rimLighting = saturate(dot(normalDirection,lightDirection)* _RimColor.xyz * _LightColor0.xyz * pow(rim,_RimPower));
         
             float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection + specularReflection; //+ rimLighting;
             
             //Texture maps
             
             return float4 (tex.xyz * lightFinal * _Color.xyz,1.0);
         }            
         ENDCG
     }
     Pass{
         Tags{"LightMode" = "ForwardAdd"}
         Blend One One
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         
         //user defined vars
         uniform sampler2D _MainTex;
         uniform float4 _MainTex_ST;
         uniform float4 _Color;
         uniform float4 _SpecColor;
         //uniform float4 _RimColor;
         uniform float _Shininess;
         //uniform float _RimPower;
         uniform float _Gloss;
         
         //unity defined vars
         uniform float4 _LightColor0;
         
         //base input structs
         struct vertexInput{
             float4 vertex : POSITION;
             float3 normal : NORMAL;
             float4 texcoord : TEXCOORD0;
         };
         
         struct vertexOutput{
             float4 pos : SV_POSITION;
             float4 tex : TEXCOORD0;
             float4 posWorld : TEXCOORD1;
             float3 normalDir : TEXCOORD2;
         };
         
         //vertex function
         
         vertexOutput vert(vertexInput v){
             vertexOutput o;
             
             o.posWorld = mul(_Object2World,v.vertex);
             o.normalDir = normalize( mul( float4( v.normal, 0.0), _World2Object ).xyz );
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.tex = v.texcoord;
             
             return o;
         }
         
         //fragment function
         
         float4 frag(vertexOutput i) : COLOR {
             
             float3 normalDirection = i.normalDir;
             float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz );
             float3 lightDirection;
             float atten;
             float4 tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);    
             
             //START NEEDS TO BE OPTIMIZED!!
             if(_WorldSpaceLightPos0.w == 0.0){ //directional
                 atten = 1.0;
                 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
             }
             else{
                 float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
                 float distance = length(fragmentToLightSource);
                 atten = 1.0/distance;
                 lightDirection = normalize(fragmentToLightSource);
             }
             //END NTBO
             
             float3 diffuseReflection = atten * _LightColor0.xyz * saturate( dot( normalDirection, lightDirection ) );
             float3 specularReflection = diffuseReflection * _SpecColor.xyz * _Gloss * pow( saturate ( dot ( reflect (-lightDirection, normalDirection ),viewDirection ) ),_Shininess );
             
             //rim lighting
                 //float rim = 1 - saturate(dot(normalize(viewDirection),normalDirection));
                 //float3 rimLighting = saturate(dot(normalDirection,lightDirection)* _RimColor.xyz * _LightColor0.xyz * pow(rim,_RimPower));
             
             float3 lightFinal = diffuseReflection + specularReflection; //rimLighting;
                                                     
             return float4 (lightFinal,1.0);
         }            
         ENDCG
     }
     
 }

Can anyone point out where I'm going wrong?

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 R-Villani · Jul 23, 2013 at 08:01 PM 0
Share

I'm having the same problem. Anyone?

avatar image Ignorant · Jun 23, 2014 at 02:42 PM 0
Share

The same problem - if you set point light range as infinite it will give you correct result. The problem is in this, that you aren't taking into account range of this point light when counting attenuation. But how on earth you can access this value? I have no idea.

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Check if object is lit a.k.a. how to use GetPixel 0 Answers

Spot Light at ForwardBase 0 Answers

Which setting dictates how far you can be before a light turns off? 0 Answers

efficient light show 1 Answer

Does pixel shaders behave like vertex shaders when pixel light count is set to 0 from quality settings ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges