Shader height lines

Hi,

I’m trying to make a shader that shows height lines (like on a map.)
Something like this:
alt text

I can’t seem to get the world-height of my vertex right though. Here’s my shader:

    Shader "Custom/Height"
{
 Properties 
 {
 _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5)
 _Step ("Step", Float) = 50.0
 }
 SubShader
 {
 //Tags {"Queue" = "Opaque"}
 ZWrite On
        
        //GrabPass { }
 Pass
 {
 Fog { Mode Off }
 Blend SrcAlpha OneMinusSrcAlpha

 CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members position)
#pragma exclude_renderers xbox360

 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 fixed4 _Color;
 float _Step;

 struct appdata
 {
 float4 vertex : POSITION;
 };
 struct v2f
 {
 float4 pos : SV_POSITION;
 float4 uv : TEXCOORD0;
 float3 worldPos;
 };
 
 v2f vert (appdata v)
 {
 v2f o;
 o.worldPos = mul(_Object2World, v.vertex).xyz;
 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 o.uv = o.pos;
 return o;
 }
 half4 frag(v2f i) : COLOR
 {
 return _Color * fixed4(
 fixed3(
 1.0 - pow(
 (float)((int)i.worldPos.y % (int)_Step) / _Step,
 4)
 ),
 1);
 }
 ENDCG
 }
 }
}

And this show my lines, like I want it to. Though when I rotate my camera the lines change :s

Side view, here the lines are like I want them:

alt text

And a top view, as you can see the lines moved:

alt text

Anyone any idea what’s wrong? I use the worldPos to calculate the colour, so this one might be wrong? I don’t know.

Ps. I’m still very new to this shader stuff, so extra info is always welcome :wink:

Thanks a lot!

-Pablo

Ok, I seem to have fixed the problem. I’m still not entirely sure why yet though.

What I did is take a different shader as a base, and change it to fit my needs. Here’s the final shader:

Shader "Custom/Height"
{
 Properties 
 {
 _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5)
 _Step ("Step", Float) = 50.0
 }

 SubShader
 {
     Pass
     {
 CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members worldPos)
#pragma exclude_renderers xbox360
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 
 fixed4 _Color;
 float _Step;
 
 struct v2f
 {
     float4 pos : SV_POSITION;
     float3 worldPos;
 };
 
 v2f vert (appdata_base v)
 {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.worldPos = mul(_Object2World, v.vertex).xyz;
     return o;
 }
 
 half4 frag (v2f i) : COLOR
 {
     return _Color * fixed4(
 fixed3(
 1.0 - pow(
 (float)((int)i.worldPos.y % (int)_Step) / _Step,
 2)
 ),
 1);
 }
 ENDCG
     }
 }
}