# elevation map

**URL:** <https://discussions.unity.com/t/elevation-map/402158>\
**Category:** Shaders & Materials\
**Tags:** Shaders\
**Created:** [November 21, 2009, 2:33pm UTC](https://discussions.unity.com/t/elevation-map/402158 "2009-11-21T14:33:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![glebski](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/glebski/32/314864_2.png) [@glebski](https://discussions.unity.com/u/glebski)\
**Post date:** [November 21, 2009, 2:33pm UTC](https://discussions.unity.com/t/elevation-map/402158/1 "2009-11-21T14:33:30Z")

</div>

Hi I’m curious whether it is possible to create an elevation map shader which would apply a gradient to a mesh depending on how high/low the parts of the mesh are. I have applied ramp shaders projected from the side in Maya to get this effect in the past. Is there a way to add “lines” every x ammount of units like in traditional cartography? I am attaching an example of the kind of effect I wish to achieve. Any help would be greatly appreciated.

Thanks in advance.

 ![228000--8202--$picture_1_384.png](https://europe1.discourse-cdn.com/unity/original/4X/4/3/a/43a065d7db04647573567ae50c855e05047f904c.png)

---

<div class="post-metadata">

**Author:** ![Kuba](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kuba/32/453283_2.png) [@Kuba](https://discussions.unity.com/u/Kuba)\
**Post date:** [November 24, 2009, 12:38pm UTC](https://discussions.unity.com/t/elevation-map/402158/2 "2009-11-24T12:38:32Z")

</div>

You can write a shader that looks at the pixel’s position along the Y axis and outputs darker pixels every N units. This solution has an inherent problem - on areas with small slope contour lines get wider than on the slopy ones.

Look at this thread:  
[http://www.gamedev.net/community/forums/topic.asp?topic\_id=529926&forum\_id=12&gforum\_id=0](http://www.gamedev.net/community/forums/topic.asp?topic_id=529926&forum_id=12&gforum_id=0)

They’re using screen-space derivatives (ddx, ddy) to make sure that the line has constant width, so it’s not dependent on the slope. Their code is in HLSL and uses [fwidth()](http://msdn.microsoft.com/en-us/library/ee418312%28VS.85%29.aspx).

---

<div class="post-metadata">

**Author:** ![glebski](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/glebski/32/314864_2.png) [@glebski](https://discussions.unity.com/u/glebski)\
**Post date:** [November 24, 2009, 1:43pm UTC](https://discussions.unity.com/t/elevation-map/402158/3 "2009-11-24T13:43:06Z")

</div>

Thanks Kuba

I had a look at the HSLS shader and it is pretty much what I need, judging by the screenshots. Unfortunately I’m a complete newb so haven’t got the slightest idea of how to implement it in Unity. I guess I would need to translate it into CG language? But again, I don’t have a clue how. More help is much needed, please.

Cheers

---

<div class="post-metadata">

**Author:** ![Kuba](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kuba/32/453283_2.png) [@Kuba](https://discussions.unity.com/u/Kuba)\
**Post date:** [November 24, 2009, 3:04pm UTC](https://discussions.unity.com/t/elevation-map/402158/4 "2009-11-24T15:04:46Z")

</div>

I might look into that later on, can’t promise. Do you have a sample terrain I should try it on? PM me.  
Of course I’ll post the results here.

---

<div class="post-metadata">

**Author:** ![bigkahuna](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bigkahuna/32/291490_2.png) [@bigkahuna](https://discussions.unity.com/u/bigkahuna)\
**Post date:** [November 24, 2009, 3:44pm UTC](https://discussions.unity.com/t/elevation-map/402158/5 "2009-11-24T15:44:14Z")

</div>

I would be very interested in seeing how you do this also. Where are you getting your original data to generate the elevation maps? DEM data?

---

<div class="post-metadata">

**Author:** ![glebski](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/glebski/32/314864_2.png) [@glebski](https://discussions.unity.com/u/glebski)\
**Post date:** [November 24, 2009, 3:49pm UTC](https://discussions.unity.com/t/elevation-map/402158/6 "2009-11-24T15:49:06Z")

</div>

Hi Kuba

Thank you so much. I realize that there are no promises, but if you can have a look, that would be fantastic. I actually want to apply this to a mesh rather than a terrain and if I can make it double-sided (see it from below and from the top) it would be even better. I guess it can be just a generic mesh with hills and dips, but I’m attaching a Maya file just in case.

Thanks a million.

[229447–8240–$nurbsmb\_730.zip](https://discussions.unity.com/uploads/short-url/j9nXDOK5ntkn1l3xmjYE9lcckol.zip) (471 KB)

---

<div class="post-metadata">

**Author:** ![glebski](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/glebski/32/314864_2.png) [@glebski](https://discussions.unity.com/u/glebski)\
**Post date:** [November 24, 2009, 3:53pm UTC](https://discussions.unity.com/t/elevation-map/402158/7 "2009-11-24T15:53:07Z")

</div>

Hi Bigkahuna

I’m taking grayscale images and then applying them as heightfields/displacement maps in Maya. But I guess you can take DEM files too.

---

<div class="post-metadata">

**Author:** ![Daniel-Brauer](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@Daniel-Brauer](https://discussions.unity.com/u/Daniel-Brauer)\
**Post date:** [November 24, 2009, 6:37pm UTC](https://discussions.unity.com/t/elevation-map/402158/8 "2009-11-24T18:37:53Z")

</div>

Hey Kuba,

If you do this, you’ll find that the dd(x|y) functions aren’t supported by OpenGL. You can make it a D3D-only shader, or try and use the technique for emulating derivatives explained in this [GPUGems article](http://http.developer.nvidia.com/GPUGems/gpugems_ch25.html).
