{"id":23354,"date":"2014-06-18T15:24:17","date_gmt":"2014-06-18T20:24:17","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23354"},"modified":"2014-06-18T18:46:15","modified_gmt":"2014-06-18T23:46:15","slug":"frontier-rebooted-part-7-what-have-we-learned-today","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23354","title":{"rendered":"Frontier Rebooted Part 7: What Have We Learned Today?"},"content":{"rendered":"<p>So grass is lightweight but high poly and you need lots of it <a href=\"?p=23304\">blah blah blah<\/a>. Let&#8217;s make some.<\/p>\n<p>The trick I want to try here is to just have the grass rise out of the ground as you approach. Which sounds ridiculous. Wouldn&#8217;t that just make it look like grass is magically growing all over the place? Wouldn&#8217;t all that motion attract your eye and be incredibly distracting? Wouldn&#8217;t it just be easier to have crap fade in? <\/p>\n<p>If someone had suggested this trick to me I would have called them a moron and made them stay late to defrag all the hard drives in the office as punishment. But it works. It works so well that you&#8217;ve likely seen it happen without noticing it. It works so well that I looked at it for hundreds of hours before I noticed.  Remember this little game?<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/skyrim_fade1.jpg' class='insetimage'   alt='skyrim_fade1.jpg' title='skyrim_fade1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Yeah. Skyrim. To be clear: I&#8217;ve got the draw distance turned down a bit, which makes it easier to show this off. Also, this might not be representative of the game proper, since I&#8217;m running a <a href=\"?p=22155\">ridiculous<\/a> number of mods. (My collection has grown since I compiled that list.) But this is enough to give you an idea of what I&#8217;m talking about. Let me zoom in on the center of the screen&#8230;<\/p>\n<p><!--more--><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/skyrim_fade2.jpg' class='insetimage'   alt='skyrim_fade2.jpg' title='skyrim_fade2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The little hill there doesn&#8217;t have any grass.  Then you take a few steps forward&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/skyrim_fade3.jpg' class='insetimage'   alt='skyrim_fade3.jpg' title='skyrim_fade3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That stuff doesn&#8217;t fade in. No fancy alpha-blending or object fade shenanigans. The tufts of grass just rise right out of the bare dirt. I&#8217;ve watched it happen and I still don&#8217;t believe it works. I would expect it to be this confusing sea of motion, like bugs on the edge of your vision. But instead this stuff pops right up in front of you and it&#8217;s pretty much seamless.<\/p>\n<p>Let&#8217;s try it.<\/p>\n<p>One of the key things is that we want to stagger the appearance of grass. We don&#8217;t want this wall of foliage all rising up along a clearly defined boundary. So I make three meshes. One is sparse. It covers only &frac14; of the ground, but these isolated tufts reach (say) about thirty meters into the distance. Then I&#8217;ve got another mesh that covers about &frac12; of the ground, but it only reaches (say) twenty meters. Then finally the last mesh fills in the remaining gaps, and it only reaches ten meters or so.  <\/p>\n<p>You can visualize their distribution 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\/freboot_dither.jpg' class='insetimage'   alt='freboot_dither.jpg' title='freboot_dither.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Not to scale. Red is the sparse mesh, blue is the medium one, and green is the final one to fill in the gaps. The default grass mesh is just a cluster of white polygon X&#8217;s<span class='snote' title='1'>That is, two upright panels, like a pair of intersecting billboards.<\/span>, all resting at the same height, all exactly one unit tall. I add 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\/freboot_grass.jpg' class='insetimage'   alt='freboot_grass.jpg' title='freboot_grass.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Now we make another little shader. Every frame, it grabs this mesh and shifts it so that it&#8217;s always under you. In one of my surface textures I&#8217;ve packed some data for this shader to use. One field says &#8220;draw your grass texture from this region of the texture&#8221;. Another says, &#8220;Adjust the panel height to be height N.&#8221; It also draws from the color map I made earlier in the project to color the grass, and uses the heightmap to move the grass up to match the terrain.<\/p>\n<p>The result?<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot26.jpg' class='insetimage'   alt='freboot26.jpg' title='freboot26.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Yeah.  The colors are kind of random. I was trying to make sure it was drawing the proper colors from the right parts of the texture, and the best way to do that was to make the color variations drastic.  <\/p>\n<p>Note the variations in height and texture. We&#8217;re just drawing the same three grass meshes over and over again every frame. So no sorting. No alpha-blending. No worrying about draw order. We don&#8217;t need to edit the mesh when the player moves around, or add or remove tufts of grass if they go to someplace that has more or less foliage. We&#8217;ve offloaded everything onto the GPU.  The CPU is just throwing static models at the graphics card and then taking a nap, because there&#8217;s nothing to do.<\/p>\n<p>So what happens when you&#8217;re someplace that doesn&#8217;t need grass? As it&#8217;s rendering, it looks up in the texture and sees that there&#8217;s not supposed to be any grass here. So it scales the grass down so that it&#8217;s exactly zero tall. Then it renders it anyway, because vertex shaders are stupid like that. If they were smarter, they&#8217;d be slower. I guess it&#8217;s sort of like a fly having reflexes an order of magnitude better than a human. Simplicity makes for speed. <\/p>\n<p>Rendering a zero-tall tuft of grass isn&#8217;t a big deal. It ends up drawing a couple of lines, which are usually tucked under the terrain anyway. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot27.jpg' class='insetimage'   alt='freboot27.jpg' title='freboot27.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;ve mentioned before that there are two major kinds of shaders: Vertex shaders and fragment shaders<span class='snote' title='2'>The third type is a GeoMetry Shader. I&#8217;ve been looking for an excuse to do something with them. More on this later.<\/span>. (I would love to know what they&#8217;re called &#8220;fragment&#8221; shaders in technical documents and not just &#8220;pixel shaders&#8221;. The latter is much more descriptive and easier to grasp. This is probably some bit of nomenclature that makes more sense if you&#8217;re spend the last decade fooling around on the driver and hardware level, and not on the software level where I usually tinker.) A verttex takes the corners of a polygon and puts them into place for rendering. The <del>fragment<\/del> pixel shader then fills in the pixels.<\/p>\n<p>Let&#8217;s say I&#8217;ve got a couple of shaders. The vertex shader figures out where each vertex ends up on screen. Then the pixel shader comes along and fills the space defined by that polygon. And because I&#8217;m not feeling really ambitious, let&#8217;s say all my pixel shader does is output blue pixels. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot_shaders.jpg' class='insetimage'   alt='freboot_shaders.jpg' title='freboot_shaders.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Nitpick shield: Yes, this would actually be two triangles and not a quad, but I&#8217;m trying to keep this visually simple.<\/p>\n<p>The vertex shader would run four times, once for each vertex. The pixel shader would run&#8230; I dunno. I&#8217;m not counting those blue pixels. Let&#8217;s say about plenty<sup>lots<\/sup>. Obviously the GPU spends a lot more time chewing on pixels than shuffling vertices. In the long run, processing those zero-height grass tufts just isn&#8217;t a big deal. They come down to a few vertex transforms and (essentially) a single line of pixels. It really is faster to mindlessly attempt to draw everything than to spend time avoiding drawing stuff we don&#8217;t want. <\/p>\n<p>Maybe I should add another bunch of polygons for flowers:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot28.jpg' class='insetimage'   alt='freboot28.jpg' title='freboot28.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That&#8217;s pretty.<\/p>\n<p>We&#8217;re still not taxing the hardware at all. It&#8217;s not even close. And having these flowers magically pop up out of the ground thirty meters away is fine. Not distracting at all. (Will this trick work just as well in VR, I wonder?)<\/p>\n<p>This was an educational project, but not quite as illuminating as I&#8217;d hoped. Mostly I became aware of just how far behind my knowledge is. I still don&#8217;t have a good feel for performance bottlenecks. While this project was a lot more GPU-intensive than my usual stuff, it&#8217;s not really hitting the hardware like modern games do. <\/p>\n<p>Let&#8217;s see how we&#8217;re doing for Oculus VR support:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot29.jpg' class='insetimage'   alt='freboot29.jpg' title='freboot29.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>To be clear, that&#8217;s not a real VR image. I just draw the scene twice with an arbitrary camera offset to make sure I could stay at 60FPS. I don&#8217;t think the image would work with 3D gear. <\/p>\n<p>In any case, I seem to have hit my goals without learning everything I wanted to know. I&#8217;ve got a better grip on shaders than before I started, but I&#8217;m still not pushing the hardware. I still haven&#8217;t used a geometry shader. I still have a lot of gaps in my understanding. In fact, I think this project just made me more aware of them, so I feel like I know less. <\/p>\n<p>Well, this is what I get for sticking to my comfort zone. I think I&#8217;m going to try again with something more ambitious. <\/p>\n<p>Okay, not more &#8220;ambitious&#8221;. More &#8220;not based on old-school heightmap terrain&#8221;. Which is ambitious from a certain pathetic, slacker, can-barely-be-arsed point of view. I want to mess around with some modern lighting and shadowing techniques. I think we may even do bump-mapping. However, this kind of thing is usually done in the context of a heavy-duty modern-day engine. Photorealism and such. I don&#8217;t want to have to make a Unreal Engine class editor just to make some content to work on. What I need is a way to generate a lot of content really fast.<\/p>\n<p>Ah, of course:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot30.jpg' class='insetimage'   alt='freboot30.jpg' title='freboot30.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I still have the code leftover from project <a href=\"?p=15742\">Octant<\/a>. <\/p>\n<p>So next time we&#8217;re going to take this heap of cubes and do something modern with them.<\/p>\n<p>I&#8217;ll leave you with a fun fact: My projects always have different names in the code than they have here on the blog, and are usually named with really stupid and awkward acronyms. I don&#8217;t know why. I think I hate naming projects. <\/p>\n<p>Internally, Octant was called WoD &#8211; World of Dig. This one was called &#8220;Song&#8221; &#8211; Same Old Nature Generator. This project is called &#8220;Unearth&#8221;, because I wanted a synonym for &#8220;dig&#8221; and I wasn&#8217;t in the mood to spend much time thinking about it.<\/p>\n<p>So next time we&#8217;re going to do more goofing around with shaders, only this time with blocks. (I considered using <a href=\"?p=15945\" title=\"Project Octant 10: Marching\">marching cubes<\/a>. They&#8217;re more interesting to look at, but they&#8217;re a pain to texture and I&#8217;m here to play with shaders, not texture projection theory.)<\/p>\n<p>Onward!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So grass is lightweight but high poly and you need lots of it blah blah blah. Let&#8217;s make some. The trick I want to try here is to just have the grass rise out of the ground as you approach. Which sounds ridiculous. Wouldn&#8217;t that just make it look like grass is magically growing all [&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-23354","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\/23354","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=23354"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/23354\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}