{"id":50911,"date":"2020-10-06T06:00:15","date_gmt":"2020-10-06T10:00:15","guid":{"rendered":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50911"},"modified":"2020-10-08T11:27:59","modified_gmt":"2020-10-08T15:27:59","slug":"project-bug-hunt-5-more-about-the-atlas","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50911","title":{"rendered":"Project Bug Hunt #5: More About the Atlas"},"content":{"rendered":"<p>Last time I talked about what an atlas texture is, why we need them, and how I use them.  The important thing is that I&#8217;m not hand-crafting the atlas every time I add a new texture. I have some code that does all of this for me. It reads all the images in a particular folder, sorts them-large-to-small for efficient packing, and then generates my atlas texture. It also stores the location of each texture within the atlas. So later when I&#8217;m busy generating walls and level geometry, I can say:<\/p>\n<p>cell = LibraryAtlas.Lookup (&#8220;textureName&#8221;);<\/p>\n<p>That gives me the location of the sub-texture within the atlas. Now I can plot <b>uv<\/b> coords as if I was using a standalone texture and not worry about the atlas at all. That&#8217;s immensely important when you&#8217;re building polygons with code and you want to keep things as simple as possible.<\/p>\n<p>Last time I shared my atlas texture.\u00a0Actually, that was just one fourth of the atlas. Sort of? The point is, I don&#8217;t just have one atlas texture. I have four.<\/p>\n<p><!--more--><\/p>\n<h3>The Atlas Collection<\/h3>\n<p>So how it works is I hand the shader four different textures. These are all atlas textures with the same exact layout of sub-images, but each atlas contains different data. The shader looks at all the different atlases and does some calculations to figure out what the final color will be for this particular pixel.<\/p>\n<p>It&#8217;s important to note that a texture is a collection of four channels of data: Red, green, blue, and alpha<span class='snote' title='1'>Alpha just means transparency.<\/span>. Sure, RGB values are normally used to store color, but since I&#8217;m writing this shader myself I can interpret the data however I want. I can use a blue channel in one of the textures to determine how much to tint the base texture pink. To the shader, it&#8217;s all just numbers.<\/p>\n<p>So let&#8217;s talk about these four different textures.<\/p>\n<h3>The Diffuse Map<\/h3>\n<p><img decoding=\"async\" src=\"images\/bughunt_atlas_diffuse.png\"\/><\/p>\n<p>This is the simplest and most obvious of our four textures. It contains the traditional texture data that most people are familiar with. If this was 1997, then this would be the only texture<span class='snote' title='2'>Aside from baked shadow maps, which aren&#8217;t worth exploring right now.<\/span>. But even though I&#8217;m planning on using retro-ish assets, I&#8217;m not sticking to pure retro technologies.<\/p>\n<p>So we need a few more textures.<\/p>\n<h3>The Normal Map<\/h3>\n<p><img decoding=\"async\" src=\"images\/bughunt_atlas_normal.png\"\/><\/p>\n<p>In the really old days, objects looked very primitive. A supposedly round support pillar would have just six sides. Character models would have primitive mitten hands with the fingers painted on the surface. Everything was blocky and chunky in appearance.<\/p>\n<p>But then as technology progressed, we gained the freedom to use more polygons. That flagon of ale didn&#8217;t need to be based on a five-sided cylinder. Characters didn&#8217;t need to have flat faces with a pyramid for a nose. Instead you could model the eyes, lips, cheekbones, and other features.<\/p>\n<p>But eventually you get diminishing returns and expanding costs if you continue to add detail with polygons. It would be a nightmare to model a wall where every brick, every crack, and every surface imperfection was fully realized in 3D. All of those millions of polygons would multiply the work needed to be done by the rendering system. Drawing the wall would be slower. Lighting the wall would be slower. Casting shadows would be slower. Heck, just loading the level would take forever.<\/p>\n<p>You end up with this horrible system where making a room look 10% more detailed would make it 100x slower to draw. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Moore%27s_law\">Moore&#8217;s Law<\/a> has done a lot for us, but even Gordon Moore can&#8217;t save us from a growth curve this steep.<\/p>\n<p><strong>But!<\/strong><\/p>\n<p>We don&#8217;t need to model walls down to the individual brick. The problem we&#8217;re trying to solve is that walls look a little too flat when you shine a light on them. Shine a light on the right side of a brick wall, and you expect to see the right edges of the bricks catch the light, and the left edges go dark. If it doesn&#8217;t, then the wall doesn&#8217;t feel like a brick wall, it feels like a wall covered in brick-patterned wallpaper.<\/p>\n<p>The solution here is a normal map. Texture maps are intended to hold visual data &#8211; pictures of bricks and such. But there&#8217;s nothing saying you <b>can&#8217;t<\/b> store positional \/ directional data in them. So a normal map uses the RGB color channels to store X, Y, Z vectors. Essentially, the normal map says, &#8220;When you render this bit of the wall, pretend the wall faces this direction before you do the lighting calculations.&#8221; The bricks will be lit as if they&#8217;re really protruding from the wall, without us needing to spend hours of modeling work and millions of polygons to make it happen.<\/p>\n<p>When I make a texture map, I also make a 3D relief map to go with it. The relief map tells the atlas generator to pretend that bright pixels protrude from the surfaces and dark pixels are depressions in the surface. From there it just takes a little math to create a normal map. <\/p>\n<h3>The Specular Map<\/h3>\n<p><img decoding=\"async\" src=\"images\/bughunt_atlas_surface.png\"\/><\/p>\n<p>(In the image above, you see some images are greyscale and some are red. The grey ones are leftover from the early stages of the project before I decided how all of this was going to work. Just ignore them. We&#8217;re interested in the pink \/ red sections for now.)<\/p>\n<p>Specular lighting is when you get metallic or glossy highlights from a surface. Let&#8217;s consider a few different kinds of surface:<\/p>\n<ul>\n<li>Chalk is a fully matte surface. It&#8217;s not glossy at all. You can clean that chalk (or even the board) all day, but you&#8217;re never going to see your reflection in it.<\/li>\n<li>Brass is incredibly reflective. If you&#8217;ve got a strong light shining on polished brass or silver, then you&#8217;ll get all of these bright glints on the surface. In real life, if you get in close you&#8217;ll see those bright spots are actually mirror-like reflections of the light sources around you.<\/li>\n<li>Stainless steel is fairly shiny, but unlike Brass it doesn&#8217;t really create a mirror-like effect. The surface is a little rough, which scatters the light in a disorganized way. You still get those shiny highlights, but they&#8217;re no longer precise reflections of the local light sources and are instead smeared out.<\/li>\n<\/ul>\n<p>So when we&#8217;re dealing with specular lighting, we&#8217;re essentially dealing with two variables. One is the strength of the specular light, which dictates how bright those shiny spots are. The other is the specular roughness, which controls how much the shiny spots are scattered around.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas_brass.jpg' width=100% alt='' title=''\/><\/div><div class='mouseover-alt'><\/div><\/p>\n<p>Essentially, specular lighting is just a cheap way to make fake reflections. We&#8217;re basically reflecting just the light sources in the room. At a distance, that looks passably like a reflection. You need to get very close (or have a very large reflective surface) to see that the reflection is just spots of light and doesn&#8217;t include any of the details of the space you&#8217;re in.<\/p>\n<p>Specular lighting is one of those technologies where the first 10% of your effort gets you 90% of the way to realism, but then you need a huge additional investment of additional programming and computing power to move the needle further. You can do a bunch of extra calculations to make sure the (say) gold surface reflects the appropriate amount of light, and correctly tints the reflection. You can make even better fake reflections with a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cube_mapping#Dynamic_reflection\">static cube map<\/a>. You can get a little closer still if you&#8217;re willing to burn a bunch of processing power maintaining a dynamic cube map. Or you can go one step further and implement <a href=\"https:\/\/github.com\/Unity-Technologies\/PostProcessing\/wiki\/Screen-space-Reflections\">screen space reflections<\/a>. If that&#8217;s still not real enough for you, then you can go all the way and shoot for a <a href=\"?p=48019\">ray tracing<\/a> solution. Basic specular lighting is cheap to set up, but there&#8217;s no upper limit on how much additional power you can expend if you&#8217;re trying to simulate reality.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas_floor.jpg' width=100% alt='You can see how the specular map changes the reflective qualities of the floor. Most tiles have rough reflections like stainless steel. A few have sharp, polished reflections. And the grooves between tiles are completely dull with no reflection at all.' title='You can see how the specular map changes the reflective qualities of the floor. Most tiles have rough reflections like stainless steel. A few have sharp, polished reflections. And the grooves between tiles are completely dull with no reflection at all.'\/><\/div><div class='mouseover-alt'>You can see how the specular map changes the reflective qualities of the floor. Most tiles have rough reflections like stainless steel. A few have sharp, polished reflections. And the grooves between tiles are completely dull with no reflection at all.<\/div><\/p>\n<p>So great. Basic specular lighting is a cheap way to make fake reflections. The problem is that you don&#8217;t want everything in a scene to exhibit the <b>same<\/b> specular properties. Consider <i>Quake 4<\/i>, where <a href=\"?p=602\">all of the level geometry had the same exact specular properties<\/a>. That was understandable in 2005 when this stuff was fairly new, but you can&#8217;t get away with that now.<\/p>\n<p>As an aside: This creates a bit of a trap for artists. Specular surfaces are inherently more interesting than perfectly diffuse surfaces. Which encourages the artist (or more likely: art director) to really lean into them, even when it&#8217;s not appropriate. This probably explains how you end up with weird design choices like <i><a href=\"?p=13210\">Dead Island<\/a><\/i>, where specular abuse makes it look like people are made of dull plastic.<\/p>\n<div class=\"dmnotes\">Related: <a href=\"?p=30940\"><i>The abuse of color filters in Mass Effect 3<\/i><\/a>, or the many games that abuse \/ overuse bloom, motion blur, and depth of field. When you&#8217;re the creative director and you look at game assets for too long, it&#8217;s easy to become bored with them. This is often true even if the art is really good. At this point, heavy-handed filters feel really fun and make the scenery seem interesting again. That&#8217;s nice for the developer who&#8217;s been staring at this space station corridor for the last 6 months and who would rather gouge their eyes out than look at it again, but it tends to ruin things for the players who experience the place for the first time. For them, the filters confuse and clutter the scene by taking away the intended point of focus and draw the eye to random corners where the effect is the strongest.<\/div>\n<p>We want metal and glass to be very shiny, we want plastic to be slightly shiny, and we want bricks and dirt to have no shine at all. We want this to be true, even if those different surfaces are all part of the same 3D model with the same texture. <\/p>\n<p>So what we need is a way to control our specular lighting on a per-pixel basis. In my project, this means an extra texture. The blue channel controls how strong the specular lighting is, and the green channel controls how rough it is. Also, I used the red channel to store a copy of the relief map I discussed in the previous section. That&#8217;s not currently being used, but I could use that to add support for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Parallax_mapping\">parallax mapping<\/a> later on.<\/p>\n<h3>The Data Texture<\/h3>\n<p><img decoding=\"async\" src=\"images\/bughunt_atlas_data.png\"\/><\/p>\n<p>One last texture. For now, I&#8217;m only using two channels:<\/p>\n<p>Red: Self-lighting. If the red value is high, then the pixel will self-illuminate. This can be used to make things that glow. This might be a display screen, or some generic sci-fi control panel lights, or whatever.<\/p>\n<p>Green: Paint color. This is a bit weird and unconventional. Right now I color the polygons of each room so that no two rooms are the same color. This color is normally covered up by the texture map, which means you never see it. However, I can use the green channel to tell the shader to multiply the texture color and the polygon color together. This is enormously useful for debugging during development. Later on, I might keep this feature so I can color-code different sections the way the System Shock games colored the walls in the different areas. Something like: Medical, Engineering, Research, Crew, etc.<\/p>\n<p>Blue: Not currently used, although I have plans for it. I&#8217;ll put this channel to use later in the project.<\/p>\n<h3>Building the Atlas<\/h3>\n<p>I have a big pile of loose textures like wall1, computer3, desk5, or whatever. They&#8217;re all stored in a specific folder where the game can find them. The atlas code reads in these images and packs them into the diffuse atlas image. Then it calculates the normal map and sticks it into another image. Then it does the same for the specular and data images.<\/p>\n<p>So that&#8217;s how my atlas works. Next I think it&#8217;s time to build some furniture.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I talked about what an atlas texture is, why we need them, and how I use them. The important thing is that I&#8217;m not hand-crafting the atlas every time I add a new texture. I have some code that does all of this for me. It reads all the images in a particular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-50911","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50911","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=50911"}],"version-history":[{"count":19,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50911\/revisions"}],"predecessor-version":[{"id":50930,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50911\/revisions\/50930"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}