Hello, dear friends. We are here today to explain you why creating sand in a videogame is kind of hard
As you know, or should already know, Immortal Redneck is set in Egypt. And even if the Nile has really green areas around and its waters allowed the egyptian civilization to prosper and conquer, we all know there’s a lot of sand in there, too. So if we were to make a game about a redneck lost in Egypt, even if the main scenarios are indoors, we needed beautiful, fine sand everywhere. And that’s exactly what we have created for Immortal Redneck. Let’s take a look.
What we wanted
While designing the rooms of Immortal Redneck, we wanted to create sand piles based on the terrain. It was a easy task, but we also wanted these piles of sand to be meshes we could bring into each particular scene very easily. Here two examples of our first attempts.
As you can imagine, we didn’t fancy how the mesh and the terrain mixed together. It seemed outdated and didn’t look like sand at all once it was on the floor, so we had to create a specific shader so we could make very detailed and soft transitions between the sand piles and the floor.
About that shader
We really wanted the ability to ‘paint’ some surfaces with sand, and to achieve this, we developed a shader that uses the vertex colors to show the correct amout of floor and/or sand. We hated the idea of using the typical blend, so the shader is configured to show the sand texture according to the floor heightmap. Obviously, the texture has a planar projection to ignore the UV mapping so it can tile correctly.
Here’s how the shader looks in shader forge. Click on it if you want.
And here’s a little demo so you can see how it works. This is how we paint sand…
And this is how we ‘erase’ it. Actually, we are just playing with opposed values: black for painting, white por erasing.
We had a little problem with the shader, though. When the terrain mesh and the painted shader mixed, the transition was a little too obvious. Like this, actually:
The problem was caused by the vertex normals, you know, those vectors that are perpendicular to a given face. The way they worked, the sand’s and the terrain’s normals were not aligned in the intersections: the first one pointed in whatever direction the geometry was pointing and the other one was just vertical, since the floor is a horizontal plane. This was an issue since we needed both meshes working together, so the solution was to create a specific tool to match the normals. This way, they would be pointing vertically in the intersection.
About the tool
Carlos, our programmer, created the tool we needed, but since he is such a great, awesome programmer – and a true Simpsons fan, too –, he ended up creating two modes in the tool: one that changed all the vertices and another that only affected the edges. The last one is the one we are using at the moment.
We first tried to change all the normals’ vertices, but even if the transition was smooth this time, the light was acting a little weird. After all, the camera ‘saw’ every surface flat because all the normals in the mesh we were pointing vertically. So now, we needed to adapt it again, and that’s why the tool can also only change the vertexes in the edges of the mesh. Since this is a bit technical, it’s easier just to see what we mean.
In this image, you have the floor normals, all pointing vertically.
Here, the mesh normals, unaffected by our tool. See those lines in the edge of the mesh? That’s what made the cut so abrupt.
Now, when we apply the tool to all vertex, the normals end up like this. See how the light is strange?
And finally, we use tool only on the edges, so the floor and the sand point in the same direction only when needed, but the rest of the mesh acts normally, unaffected.
Here you can see a little better our problem with the light. Shadows were just wrong and didn’t look natural at all.
Now that I have explained all this technical stuff, we can just show you how cool it looks when you ‘paint’ with sand.
And that’s all for now, folks. Immortal Redneck is growing so fast sometimes it’s scary. We are almost ready to show you some more guns and some more enemies, but for now, we wanted to do a little more technical devlog about the problems we are having and the solutions we are developing.