- Home /
 
 
               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
              
 
               
              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.
Your answer
 
             Follow this Question
Related Questions
Lighting works for iPad but not iPhone? 0 Answers
Point light too bright in single-color shader within range of light 1 Answer
Baked spotlights not showing 1 Answer
using two shaders together 1 Answer
WebCamTexture with custom VertexShader? 0 Answers