{"id":130,"date":"2014-09-20T10:32:30","date_gmt":"2014-09-20T18:32:30","guid":{"rendered":"http:\/\/neonstruct.com\/blog\/?p=130"},"modified":"2014-09-20T10:37:57","modified_gmt":"2014-09-20T18:37:57","slug":"all-of-the-lights","status":"publish","type":"post","link":"https:\/\/neonstruct.com\/blog\/?p=130","title":{"rendered":"All of the Lights"},"content":{"rendered":"<p style=\"text-align: justify;\"><a href=\"http:\/\/imgur.com\/6eHcmaS\"><img title=\"Hosted by imgur.com\" src=\"http:\/\/i.imgur.com\/6eHcmaS.png\" alt=\"\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">A few friends have commented on or asked about the lighting in <em>NEON STRUCT<\/em>, so I&#8217;m going to provide a brief overview of how it works.<\/p>\n<p style=\"text-align: justify;\">For context, let&#8217;s first look at the lighting in <em>Eldritch<\/em> to see how the system developed.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/imgur.com\/x0SoGtz\"><img title=\"Hosted by imgur.com\" src=\"http:\/\/i.imgur.com\/x0SoGtz.png\" alt=\"\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">In <em>Eldritch<\/em>, there are few lights and usually a bright directional ambient term. (I&#8217;ve chosen a dark level here to illustrate other lighting characteristics, but levels like <a href=\"http:\/\/eldritchgame.com\/bin\/1.png\" target=\"_blank\">R&#8217;lyeh<\/a> were almost entirely lit by the ambient term.)<\/p>\n<p style=\"text-align: justify;\">Lighting is semi-static and baked into the voxel grid. Each 1m<span class=\"texhtml texhtml-big\"><sup>3<\/sup><\/span> cell acts as a low frequency light probe and stores the incoming light on each axis: X<span class=\"Unicode\">\u00b1<\/span>, Y<span class=\"Unicode\">\u00b1<\/span>, Z<span class=\"Unicode\">\u00b1<\/span>. Each light value is represented as a 4-dimensional vector of floats, and each cell contains six of these. 4 bytes * 4 * 6 = 96 bytes per cell. In <em>Eldritch<\/em>, these values were stored in a flat array, totaling about 6MB of light data per map. Lights can be added, removed, or changed at runtime, but this requires resolving all light sources influencing the local voxels and rebuilding the local voxel hull meshes. This can usually be done within the span of a 60Hz frame without causing a hitch, mostly because <em>Eldritch<\/em> has few lights, and they have relatively small radii.<\/p>\n<p style=\"text-align: justify;\">When the voxel hull meshes are constructed, <em>Eldritch<\/em> queries the singular probe on the front face of each voxel and lights the voxel by baking that light value into the mesh&#8217;s vertex color. Using only the local light value produces discontinuities between voxels, causing the &#8220;blocky&#8221; effect indicated in the image above. (The most recent update to <em>Eldritch<\/em> adds light smoothing from <em>NEON STRUCT<\/em> as described below, but the blockiness was present in all prior versions.)<\/p>\n<p style=\"text-align: justify;\">Objects are also lit using the voxel light grid, but in a different manner because the lighting cannot be baked into the vertex stream. Each tick, the renderer looks up the light probe nearest to each object&#8217;s centroid and passes its values to the pixel shader. After transformation, the mesh is lit by multiplying the components of its per-pixel normal by the corresponding values in the light probe, like so:<\/p>\n<pre style=\"text-align: justify;\">float4 LightProbe[6]; \/\/ X+\/-, Y+\/-, Z+\/-\r\nfloat4 GetLight( float3 InNormal )\r\n{\r\n\u00a0\u00a0 \u00a0float3 NormalSq = InNormal * InNormal;\r\n\u00a0\u00a0 \u00a0int3 IsNeg = InNormal &lt; 0.0f;\r\n\u00a0\u00a0 \u00a0return\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0NormalSq.x * LightProbe[IsNeg.x] +\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0NormalSq.y * LightProbe[IsNeg.y + 2] +\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0NormalSq.z * LightProbe[IsNeg.z + 4];\r\n}<\/pre>\n<p style=\"text-align: justify;\">This produces soft per-pixel lighting which roughly approximates the directions of incoming light.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/imgur.com\/TIytyGH\"><img title=\"Hosted by imgur.com\" src=\"http:\/\/i.imgur.com\/TIytyGH.png\" alt=\"\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><em>NEON STRUCT<\/em> builds upon <em>Eldritch<\/em>&#8216;s lighting system, with a few important changes. The two most apparent differences are smooth world lighting (no more blocky discontinuities between voxels) and cel shading on objects.<\/p>\n<p style=\"text-align: justify;\">Smoothed world lighting is simple in theory: just blend the light values between each voxel. In practice, it was somewhat trickier than I expected. Each corner of a voxel&#8217;s face is lit by the local light probe and the three probes adjacent to that corner, but only if the adjacent voxels are non-solid. If an adjacent voxel contains geometry, it needs to be ignored in the smoothing operation or else it will darken the voxel&#8217;s face in an undesired way. (Voxel lighting in both games already has an ambient occlusion factor to darken concave edges and corners.)<\/p>\n<p style=\"text-align: justify;\">Object lighting in <em>NEON STRUCT<\/em> is similar to that in <em>Eldritch<\/em>, but the shader is modified to produce a pseudo cel shaded effect. Unlike typical cel shading, in which the <em>color<\/em> is quantized to produce discontinuities, I chose to quantize the <em>direction<\/em> of the per-pixel normal to the cardinal axes. This produces discontinuities in color even when the colors in each direction are relatively similar, so objects in low light scenes will retain some definition and not be completely flattened out.<\/p>\n<pre style=\"text-align: justify;\">float4 LightProbe[6]; \/\/ X+\/-, Y+\/-, Z+\/-\r\nfloat4 GetCelLight( float3 InNormal )\r\n{\r\n\u00a0\u00a0 \u00a0float3 NormalSq = InNormal * InNormal;\r\n\u00a0\u00a0 \u00a0int3 IsNeg = InNormal &lt; 0.0f;\r\n\u00a0\u00a0 \u00a0int UseX = NormalSq.x &gt;= NormalSq.y &amp;&amp; NormalSq.x &gt;= NormalSq.z;\r\n\u00a0\u00a0 \u00a0int UseY = ( 1 - UseX ) * ( NormalSq.y &gt;= NormalSq.z );\r\n\u00a0\u00a0 \u00a0int UseZ = 1 - ( UseX + UseY );\r\n\r\n\u00a0\u00a0 \u00a0return\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0UseX * LightCube[IsNeg.x] +\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0UseY * LightCube[IsNeg.y + 2] +\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0UseZ * LightCube[IsNeg.z + 4];\r\n}<\/pre>\n<p style=\"text-align: justify;\">The less visible changes since <em>Eldritch<\/em> include significant optimizations for adding\/removing\/changing lights, as described in a couple of <a href=\"http:\/\/neonstruct.com\/blog\/?p=41\" target=\"_blank\">previous<\/a> <a href=\"http:\/\/neonstruct.com\/blog\/?p=83\" target=\"_blank\">posts<\/a>. Furthermore, the flat array of lighting data (which was over 6MB for <em>Eldritch<\/em>&#8216;s small levels) needed to be converted into a sparse array for <em>NEON STRUCT<\/em>&#8216;s larger levels. Now, any voxel which is lit only by the ambient term does not store a light probe.<\/p>\n<p style=\"text-align: justify;\">And while it&#8217;s not strictly part of the lighting system, the addition of bloom in post processing contributes a lot to the apparent brightness of the scene. Post process effects could be the subject of a future post, but in brief, <em>NEON STRUCT<\/em>&#8216;s post chain starts with chromatic aberration (RGB separation), adds bloom, subtracts film grain noise, and finally applies color grading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few friends have commented on or asked about the lighting in NEON STRUCT, so I&#8217;m going to provide a brief overview of how it works. For context, let&#8217;s first look at the lighting in Eldritch to see how the system developed. In Eldritch, there are few lights and usually a bright directional ambient term. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":7,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neonstruct.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}