Add lighting to vertex shader?

I have a curved vertex shader that is designed to give the players the illusion that there is a “Horizon” line in front of them at all times. The shader works flawlessly, but I have run into a problem. The textures on my gameobjects don’t darken when they are in the darkness… They just stay lit up. I don’t know if this is a shader issue, but I’m pretty sure it is…

Here is my code:

Shader "Custom/Curved" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _QOffset ("Offset", Vector) = (0,0,0,0)
        _Dist ("Distance", Float) = 100.0
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _Color ("Main Color", Color) = (1,1,1,1)
    }

 

    SubShader {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _QOffset;
			float _Dist;
			
			struct v2f {
			float4 pos : SV_POSITION;
			float4 uv : TEXCOORD0;
			};

 
            v2f vert (appdata_base v)
            {
            v2f o;
            float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
            float zOff = vPos.z/_Dist;
            vPos += _QOffset*zOff*zOff;
            o.pos = mul (UNITY_MATRIX_P, vPos);
            // o.uv = v.texcoord;
            o.uv = mul( UNITY_MATRIX_TEXTURE0, v.texcoord );
            return o;
            }
            
            half4 frag (v2f i) : COLOR
            {
            half4 col = tex2D(_MainTex, i.uv.xy);
            return col;
            }
            ENDCG
            
            }
            
           }
           SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 300

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG  
}
           
           FallBack "Diffuse"
           
        }

Okay, so I followed the example on the unity website about making a custom surface shader for lighting, and it is still not working…

Here is the new shader:

Shader "Custom/Curved" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _QOffset ("Offset", Vector) = (0,0,0,0)
        _Dist ("Distance", Float) = 100.0
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}
    }

 

    SubShader {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _QOffset;
			float _Dist;
			
			struct v2f {
			float4 pos : SV_POSITION;
			float4 uv : TEXCOORD0;
			};

 
            v2f vert (appdata_base v)
            {
            v2f o;
            float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
            float zOff = vPos.z/_Dist;
            vPos += _QOffset*zOff*zOff;
            o.pos = mul (UNITY_MATRIX_P, vPos);
            // o.uv = v.texcoord;
            o.uv = mul( UNITY_MATRIX_TEXTURE0, v.texcoord );
            return o;
            }
            
            half4 frag (v2f i) : COLOR
            {
            half4 col = tex2D(_MainTex, i.uv.xy);
            return col;
            }
            ENDCG
            
            }
            
           }
           SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 300

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG  
}
SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf SimpleLambert

      half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
          half NdotL = dot (s.Normal, lightDir);
          half4 c;
          c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten * 2);
          c.a = s.Alpha;
          return c;
      }

      struct Input {
          float2 uv_MainTex;
      };
      sampler2D _MainTex;
      void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
      }
      ENDCG
    }
           
           FallBack "Diffuse"
           
        }

Can ANYONE please help with this? I have tried everything I know how to do…

It is quite strange how you built your shader. for me it’s something like this if you want vertex function and custom lighting:

Shader "Test/Name" {
	Properties {
		// Your data
	}
	SubShader {
		Tags { "RenderType"="Opaque" }

		CGPROGRAM
		#pragma surface surf Name vertex:vert

		// Your data

		struct Input {
			// Your data
		};
		
		void vert(inout appdata_full v, out Input o)
		{ /* Code */ }
		
		half4 LightingName (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
		{ /* Code */ }

		void surf (Input IN, inout SurfaceOutput o) 
		{ /* Code */ }
		ENDCG
	} 
	FallBack "Diffuse"
}

With this you have a one pass custom shader. Can you specify what is not working ? The effect or the shader doesn’t compile ?

I actually did mostly copy and paste to build my shader as i do not have any experience with shader programing. The effect isn’t working at all. The shader compiled just fine… :confused:

In all honesty… I would literally pay someone right now to do this for me. I am completely out of ideas…

Okay, so I’ve got the lighting to work in my shader. But now, for some reason, the curvature doesn’t work…

Here is my code:

Shader "Custom/Curved" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _QOffset ("Offset", Vector) = (0,0,0,0)
        _Dist ("Distance", Float) = 100.0
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _Color ("Main Color", Color) = (1,1,1,0.5)
        _MainTex1 ("Texture 1", 2D) = "white" {}
        _Ambient("Ambient Color",Color) = (0.3,0.3,0.3,1)
        _Color ("Diffuse Material Color", Color) = (1,1,1,1) 

    }

 

    SubShader {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            
            sampler2D _MainTex;
            float4 _QOffset;
			float _Dist;
			
			struct v2f {
			float4 pos : SV_POSITION;
			float4 uv : TEXCOORD0;
			};

 
            v2f vert (appdata_base v)
            {
            v2f o;
            float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
            float zOff = vPos.z/_Dist;
            vPos += _QOffset*zOff*zOff;
            o.pos = mul (UNITY_MATRIX_P, vPos);
            // o.uv = v.texcoord;
            o.uv = mul( UNITY_MATRIX_TEXTURE0, v.texcoord );
            return o;
            }
            
            half4 frag (v2f i) : COLOR
            {
            half4 col = tex2D(_MainTex, i.uv.xy);
            return col;
            }
            ENDCG
            
            }
            Pass {    
         	Tags { "LightMode" = "ForwardBase" } 
            // make sure that all uniforms are correctly set
 
         GLSLPROGRAM
 
         uniform vec4 _Color; // shader property specified by users
 
         // The following built-in uniforms (except _LightColor0) 
         // are also defined in "UnityCG.glslinc", 
         // i.e. one could #include "UnityCG.glslinc" 
         uniform mat4 _Object2World; // model matrix
         uniform mat4 _World2Object; // inverse model matrix
         uniform vec4 _WorldSpaceLightPos0; 
            // direction to or position of light source
         uniform vec4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
 
         varying vec4 color; 
            // the diffuse lighting computed in the vertex shader
 
         #ifdef VERTEX
 
         void main()
         {                              
            mat4 modelMatrix = _Object2World;
            mat4 modelMatrixInverse = _World2Object; // unity_Scale.w
               // is unnecessary because we normalize vectors
 
            vec3 normalDirection = normalize(
               vec3(vec4(gl_Normal, 0.0) * modelMatrixInverse));
            vec3 lightDirection = normalize(
               vec3(_WorldSpaceLightPos0));
 
            vec3 diffuseReflection = vec3(_LightColor0) * vec3(_Color)
               * max(0.0, dot(normalDirection, lightDirection));
 
            color = vec4(diffuseReflection, 1.0);
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
         }
 
         #endif
 
         #ifdef FRAGMENT
 
         void main()
         {
            gl_FragColor = color;
         }
 
         #endif
 
         ENDGLSL
      }
            
           }
           SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 300

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
	float2 uv_BumpMap;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG  
}   
           FallBack "Diffuse"
           
        }

I don’t expect anyone to fix the shader for me, but a point in the right direction would be nice…