{"id":15867,"date":"2012-05-11T05:28:00","date_gmt":"2012-05-11T10:28:00","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15867"},"modified":"2012-05-11T05:28:00","modified_gmt":"2012-05-11T10:28:00","slug":"project-octant-part-6tiling","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=15867","title":{"rendered":"Project Octant Part 6:Tiling"},"content":{"rendered":"<p>So here we are, six entries into this series and we haven&#8217;t really done much in the way of making software. I actually find this kind of liberating. In Project Frontier I felt driven to get my core features in there. I like that this project is more an excuse for endless digressions. It&#8217;s a lot easier to navigate when you don&#8217;t care where you&#8217;re going. <\/p>\n<p>Still, I guess we&#8217;d better get started with the actual thing and stop whinging on about technology and development platforms. First, let&#8217;s look at texture mapping&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_2.jpg' class='insetimage'   alt='octant6_2.jpg' title='octant6_2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>(We don&#8217;t have a lighting system yet, so I&#8217;m just painting the sides of cubes darker than the top. That&#8217;s close enough for now.) <\/p>\n<p>I&#8217;ve slapped the default stone texture from Minecraft onto this landscape so I can illustrate the thing I want to work on.  As in Minecraft, flat stone can look kind of dull if you see too much of it at once, and I&#8217;ve deliberately designed this scene to show us way, way too much of it. <\/p>\n<p>This texture is kind of bland. I mean, it&#8217;s a scattering of grey values that barely deviate from each other.  Of course, this is by design. Notch knew what he was doing when he made his textures. Some well-meaning modders come along and try to make the textures more &#8220;interesting&#8221;, and you wind up with:<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_3.jpg' class='insetimage'   alt='octant6_3.jpg' title='octant6_3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Which sends you right into quilt-world. Sometimes people try to compensate for this by bumping up the resolution.  That can mitigate the problem, but it&#8217;s not a solution.  The issue at hand is that textures repeat exactly one per cube-face, so looking at lots of cubes means lots of repetition.  This isn&#8217;t a design <em>flaw<\/em> or anything. It&#8217;s part of Minecraft&#8217;s distinctive look. <\/p>\n<p>But what if we broke from that?  What if textures covered larger areas? Let me grab a random texture from Google image search and set up the texture system to stretch the texture over 16 blocks.  I&#8217;ll be using this texture:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_6.jpg' class='insetimage'   alt='octant6_6.jpg' title='octant6_6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And it looks like this:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_4.jpg' class='insetimage'   alt='octant6_4.jpg' title='octant6_4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You can see that spanning many blocks lets us have large patterns.  We could use this to have colored strata of rock or variations in brightness.  I know my texture here isn&#8217;t ideal, but I&#8217;m content that this would look better with more suitable art. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_5.jpg' class='insetimage'   alt='octant6_5.jpg' title='octant6_5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>However, this leads to some problems mapping textures. All this fancy detail will work against us if we can&#8217;t get it to line up.  Think of it like hanging strips of wallpaper.  We can put two strips next to each other, and their patterns will line up to create a seamless whole.  But if some deformation makes one part of the wall &#8220;longer&#8221; than the other&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_7.jpg' class='insetimage'   alt='octant6_7.jpg' title='octant6_7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>At the top of the wall the two strips line up, but then the one on the left has to fill an alcove, which covers 3 meters of wall instead of just 1.  By the time we exit the alcove, the strip on the left has run too long and there will be an ugly seam where it meets its neighbor.<\/p>\n<p>Texture mapping theory can get kind of dense. It&#8217;s basically a more convoluted version of the age-old <a href=\"http:\/\/en.wikipedia.org\/wiki\/Map_projection\">map projection problem<\/a>.  Or I suppose it&#8217;s the map problem in reverse: We have a square image and we want to project it onto a complex world with as little distortion as possible. It&#8217;s not a problem you &#8220;solve&#8221;, but a series of trade-offs you juggle. <\/p>\n<p>In <em>Half-Life 2: Episode 2<\/em>, Valve actually created a custom shader that would wrap a texture onto the complex topology of a cave.  They did this so that an artist wouldn&#8217;t have to drive themselves mad trying to get <em>rectangles<\/em> to line up on stalagmites and other topologically difficult items.  It just smears a generic rock texture all over everything. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_caves.jpg' class='insetimage'   alt='octant6_caves.jpg' title='octant6_caves.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This works because &#8220;rock walls&#8221; don&#8217;t have an expected orientation, and are very forgiving with regards to stretching. (Nobody&#8217;s going to look at the wall and think the rocks are &#8220;sideways&#8221; or &#8220;too big&#8221;. If you tried this with a brick texture it would look ridiculous. <\/p>\n<p>We are <strong>not<\/strong> going in that direction.  We&#8217;re aiming for low tech. We&#8217;re aiming for simple. We want something that will produce working results on shapeless rock and dirt, and also on non-rotatable stuff like bricks.  <\/p>\n<p>So here is my super-secret ninja technique:<\/p>\n<pre lang=\"c\">\r\nint u = x + y;\r\nint v = z;\r\n<\/pre>\n<p>I know, right? Brilliant. I&#8217;ve already filed for the patent and everything. <\/p>\n<p>This is as simple as it gets.  The texture follows the world coordinates.  If you move one unit in the world, you&#8217;ll move one texture frame.  Here, let me apply a test texture:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_8.jpg' class='insetimage'   alt='octant6_8.jpg' title='octant6_8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I suggest using a texture like this one whenever you&#8217;re dealing with texture mapping problems.  It&#8217;s orient-able. (You can tell if it&#8217;s backwards or upside-down, which doesn&#8217;t matter in our case but can be a problem if you&#8217;re working on something where textures need to face a certain way.) The letters form a clear progression from left to right, so if your texture doesn&#8217;t line up you&#8217;ll see the letters stop following natural order.  Same goes for the rainbow color-shift: If you see purple next to green, then you&#8217;ve botched something. If the textures are working right, both the lettering and color-change should progress naturally. <\/p>\n<p>Let&#8217;s apply it to our world:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_9.jpg' class='insetimage'   alt='octant6_9.jpg' title='octant6_9.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>We&#8217;re just worried about walls right now, so I&#8217;ve made the floor solid color just to avoid confusion.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_10.jpg' class='insetimage'   alt='octant6_10.jpg' title='octant6_10.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>You can see the rows I&#8217;ve highlighted here. They line up whenever they meet, even though the walls have different shapes. You can see there&#8217;s a hole in the wall on the right, where an &#8220;N&#8221; block is missing.  This means that row is longer than the other two, yet <em>even then it lines up<\/em>.  The texture can wrap around a mountain, or a continent, or a one-meter pillar, and you&#8217;ll always be able to walk all the way around it without seeing a seam.<\/p>\n<p>Here&#8217;s the backside of the hill:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_12.jpg' class='insetimage'   alt='octant6_12.jpg' title='octant6_12.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Sure, the letters are backwards, but in the case of terrain textures this generally doesn&#8217;t matter. <\/p>\n<p>The rule is simple:  When we move east or north, increase the texture by 1 section.  Corollary: If we move west or south, decrease the texture by 1. The only downside is:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_11.jpg' class='insetimage'   alt='octant6_11.jpg' title='octant6_11.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Let&#8217;s assume we&#8217;re looking at a really big pillar from above. (Note also that the vertical arrow should point UP, assuming the top of the image is north. No big deal. The principle is the same.)<\/p>\n<p>The problem areas are where you&#8217;ve got a wall running up one axis but down the other.  It moves east, goes forward one letter, then it goes south, and goes back a letter, then forward, backward.  It looks like this:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_13.jpg' class='insetimage'   alt='octant6_13.jpg' title='octant6_13.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>We we end up looking at repetition and mirroring. See what I mean about trade-offs?  We&#8217;ve left behind the drawbacks of Minecraft&#8217;s texture system and are using a new set of drawbacks.  <\/p>\n<p>Lemme put a really busy texture up.  Also, we&#8217;ll put a different texture on all of the surface blocks so we have even more contrast.  Here are some high-contrast textures at work:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_14.jpg' class='insetimage'   alt='octant6_14.jpg' title='octant6_14.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And here is the same hill from the &#8220;bad&#8221; side:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_15.jpg' class='insetimage'   alt='octant6_15.jpg' title='octant6_15.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;d say the texture itself is more ugly than the mirroring and repetition, so maybe this wasn&#8217;t an ideal test. Anyway. You get the idea. <\/p>\n<p>Next up is something I&#8217;ve wanted to do for over a decade.  Since my time at Activeworlds, I&#8217;ve always wanted to make a borderless tiling world.  A world where you can go off one edge of the map and seamlessly enter on the other, where if you travel in a straight line you&#8217;ll eventually end up back where you started.  The last time I saw that in a game was in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Magic_Carpet_(video_game)\">Magic Carpet<\/a> in the mid 90&#8217;s. <\/p>\n<p>I like borderless environments so much better than games like Fallout 3 or Oblivion where it just puts an invisible wall around your playpen and tells you &#8220;NO!&#8221; when you try to leave.  I also like it because it lets you walk around the world, which is kind of neat.  <\/p>\n<p>Let&#8217;s try it:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_18.jpg' class='insetimage'   alt='octant6_18.jpg' title='octant6_18.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I guess&#8230; I guess you can&#8217;t show this off in screenshots.  Oh! Unless I make the world really, really tiny.  Let me adjust the size down, and build some distinctive landmark:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_16.jpg' class='insetimage'   alt='octant6_16.jpg' title='octant6_16.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And if we look up:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant6_17.jpg' class='insetimage'   alt='octant6_17.jpg' title='octant6_17.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The world map is 32&#215;32 meters.  Just enough space for a suburban house with a modest yard, I think.  Our view reaches 144m in every direction. Keep in mind those question marks in the distance are the exact same question mark I have in front of me.  They&#8217;re not copies.  Those are the same polygons and blocks. If I break a block in front of me, I see it vanish from the pattern in the distance. <\/p>\n<p>All three of my kids saw this in turn, and each one asked: Will you be able to see yourself like this?<\/p>\n<p>Well, keep in mind that right now there IS no concept of a &#8220;self&#8221; model. Or any kind of model.  There are cubes.  That&#8217;s all we have, and there aren&#8217;t even any systems in place for drawing other sorts of stuff. (Items, foes, NPC&#8217;s, space marines, etc.) But if I keep with this project and if I follow the plan in my head, then looking all the way &#8220;around&#8221; the world like this would cause you to see everything <em>except<\/em> yourself. (Because your character model isn&#8217;t usually drawn in a first-person game.) <\/p>\n<p>But this is academic.  The point isn&#8217;t to make a bizzaro world where you can see things tile.  The point is to make a borderless world, and this little stunt proves that it works. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>So here we are, six entries into this series and we haven&#8217;t really done much in the way of making software. I actually find this kind of liberating. In Project Frontier I felt driven to get my core features in there. I like that this project is more an excuse for endless digressions. It&#8217;s a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[233],"class_list":["post-15867","post","type-post","status-publish","format-standard","hentry","category-programming","tag-octant"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15867"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/15867\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}