Topology Map

Hey there,

I’m trying to create a topology map type overlay for my terrain, with fixed line widths. And I’m trying to accomplish it in one pass on my surface shader without extra post processing. I’ve tried several things.
Here’s a simple try with just some naive ideas:

float height = tex2D(_TerrainHeightMap, tUV).r;
float linePos = height / lineSpacing;
float lineFraction = frac(linePos);   
if (lineFraction < 0.1) {
       color = float3(0,0,0);
}

The lines are variable thickness and not desirable. So, here’s a second attempt where I’m trying to get the lines tighter using fwidth()

float height = tex2D(_TerrainHeightMap, tUV).r;
float linePos = height / lineSpacing;
float lineFraction = fwidth(frac(linePos));
   
if (lineFraction > 0.5) {
       color = float3(0,0,0);
}


This one is much closer to what I’d like, but the lines it makes are less than a pixel width and sometimes I find it a little unstable. If I don’t use a good lineFraction comparison number whole chunks will turn black.

Is there a way to use fwidth() in a way to get a nice crisp line of a fixed width in this way?
Thanks.


As an afterthought, I thought I’d also show you what it looks like when visualizing just frac(linePosition):

topologytest 3