This article on tinkering
, showcasing some code
The source code was difficult to adapt, as it seemingly required some three.js builtins (such as uv
).
While they render a tilemap, they don't render it using a single quad.
Thus, marks the goals of this post:
Without further ado, I present to you:
All the code can be found on GitHub. The code itself is the content of this article. This article is merely supplementary for search engine optimization reasons.
While showing off additional features like multiple layers, parallax scrolling, or a ton of mutations would be cool, they also distract from the main point being shown. Thus, all additional features are left as an exercise to the reader.
For this section, the overall abstract of the technique will be explained. Do note that the code provides a much more in-depth explanation of the specifics of what is being done.
The goal is to render a quad that acts as a tilemap. To render a quad, we will perform the following for every pixel:
Assume we're running some code for the pixel P in the following image:
+--------------+
| |
| P |
| |
| |
| |
+--------------+
The first step is to figure out what block the pixel should be. Let's assume we've figured that out:
+----+----+----+
|0 |1 |1 |
| | | P |
+----+----+----+
|0 |0 |1 |
| | | |
+----+----+----+
Here, the numbers in the upper left corner represent the "id" of the block in question.
Finally, the goal is to take a texture atlas, something like so (where the numbers represent both the ID and the texture):
+---+---+
|000|111|
|000|111|
+---+---+
figure out where our pixel lies in that atlas
+---+---+
|000|111|
|000|1P1|
+---+---+
and to copy over the color our pixel lies on in the texture atlas into the final image:
+----+----+----+
| | | |
| | | 1 |
+----+----+----+
| | | |
| | | |
+----+----+----+
In conclusion, check out the code. It is the entire meat of this, and it is thoroughly commented on what it does.