 # Interactive Map Shader: Scrolling Effect

This is the second part of the online course dedicated to interactive maps. This is a tutorial in three parts:

In the previous lecture of this online course, we created a vertex function which extrudes the vertices of a mesh upwards. The intensity of the effect is controlled by a texture, the height map, so that brighter pixels are raised more, compared to darker ones.

What we have done so far works relatively well. Before we continue, let’s also factor the code necessary to calculate the new vertex height into its own function, called getVertex:

Now, the entire vert function becomes:

The reason why we do this is that in the next sections we will need to calculate the height of multiple points. Having this functionality in its own separate function makes the code much easier.

## Calculating UV Coordinates

This, however, opens up another issue. The getVertex function depends not only on the current vertex position ( v.vertex), but also on its UV coordinates ( v.texcoord).

When we want to calculate the height displacement of the vertex that the vert function is currently processing, both pieces of information are available in the appdata_base structure. However, what happens if we have to sample the position of a nearby point? In that case, we might know its xyz position in model space, but we have no access to its UV coordinates.

This means that the current solution is able to calculate the height displacement for the current vertex only. Such limitation would prevent us from moving forward, so we need to find a solution.

The most simple one is to find a way to calculate the UV coordinates of the 3D object, knowing its vertex position. Technically speaking, this is a very complex problem and there are several techniques that attempt to solve it (the triplanar projection being one of the most popular). In this specific case, however, we do not need to map UV to any geometry. If we assume that our shader is only ever going to be used on a flat mesh, then the problem becomes trivial.

What makes calculating UV coordinates (below, right) from vertex positions (below, left) possible is the fact that, on a flat mesh such as a plane, they are both mapped linearly.  This means that, in order to solve our problem, we need to remap the XZ components of the vertex position onto their respective UV coordinates. This is known as linear interpolation, and is a topic that has been covered extensively on this website (for instance: The Secrets Of Colour Interpolation).

Most UV values range from to ; conversely, the coordinates of each vertex are potentially unbound. Mathematically speaking, the only things that we need to perform linea remapping of XZ onto UV are their extreme values:

• , • , • , • , which can be seen below: These values changes depending on the mesh used. On a Unity plane, the UV coordinates range from to , while the vertex coordinates range from to .

The equations that remap XZ onto UV are:

(1) ❓ Show me the derivation!
If you are unfamiliar with the concept of linear interpolation, the equations above look quite scary.

Their derivation, however, is fairly simple. Let’s focus on only for this derivation. We have two ranges: one goes from to , and the other goes from to . The input is the coordinate of the vertex currently being processed, and the output will be the coordinate used to sample the texture.

The properties that we want to preserve, is the proportionality between and its interval, and and its interval. For instance, if is at 25% of its range, then will have to be at 25% its range as well.

We can see all of this in the following diagram: From which we can derive that the proportion the red segment makes with respect to the light red one, has to be the same to the one that the blue segment makes with respect to the light blue one:

(2) Now, we can rearrange the equation above to extract : which is exactly the one presented earlier, (1).

These equations can be implemented like this:

Now, we can invoke  getVertex without the need to pass v.texcoord to it:

And the entire vert function becomes:

##### ⭐ Suggested Unity Assets ⭐
Unity is free, but you can upgrade to Unity Pro or Unity Plus subscriptions plans to get more functionality and training resources to power up your projects.

### The Scrolling Effect

With the code that we have written so far, the entire map now appears on the mesh. If we want to improve this, we need to make some changes.

Let’s formalise this a bit more. First of all, we might want to zoom in on a specific part of the map, rather than seeing it in its entirety. We can define this region with two pieces of information: how large it is ( _CropSize) and where it is on the map ( _CropOffset), measured in vertex space (from _VertexMin to _VertexMax).

Once we have these two values, we can use linear interpolation once again to make sure that getVertex is not called on the actual vertex position of the 3D model, but on a rescaled, translated point. With the relative code:

If we want this to actually scroll, then we simply have to update  _CropOffset via a script. This moves the cropped area, de-facto scrolling over the landscape.

In order for this to work, is very important that all the textures used have their Wrap Mode mode set to Repeat. If not, you will not be able to loop around the texture.

For a zoom-in/zoom-out effect, you can simply chance the _CropSize.

## What’s Next…

One important aspect that we have overlooked, is the shading. The geometry appears, in fact, dull and flat. You can see how the model looks now (left) and how it should actually look (right).

We will fix this in the third, and final, part of this online course on interactive map shaders.

Become a Patron!
The full package for this tutorial is available on Patreon, and it includes all the assets necessary to reproduce the technique here presented.

##### 💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog. You will be notified when a new tutorial is relesed!

##### 📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms. Especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻