{
  "version": 1,
  "canonical": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading",
  "title": "Xaede Game Engine - Unified Lighting & Shading",
  "description": "A 2009 explanation of unified lighting and shading in the Xaede engine, documenting the rendering approach and influences of the period.",
  "language": "en",
  "manifest": "https://xaede.net/representations/index.json",
  "contentHtml": "<article class=\"reading-page\"><header class=\"reading-header\"><nav class=\"page-breadcrumb\" aria-label=\"Breadcrumb\"><ol><li><a href=\"/\">Home</a></li><li><a href=\"/writing\">Writing</a></li><li><span aria-current=\"page\">Xaede Game Engine - Unified Lighting &amp; Shading</span></li></ol></nav><div class=\"article-label\"><span>Archive edition / Legacy Xaede</span><time datetime=\"2009-06-09\">June 9, 2009</time><span>6 MIN READ</span></div><h1>Xaede Game Engine - Unified Lighting &amp; Shading</h1><p class=\"byline\"><a href=\"/about\" rel=\"author\">Josh English</a></p><p class=\"edition-note\">From the original Xaede archive. This is historical writing, preserved in a new reading edition. Technical advice, tools and project status reflect the time of publication.</p></header><div class=\"reading-body prose\"><p>When reading about next-generation game engine development, I came across a game development project entitled Project Offset. This game engine features a concept in three-dimensional (3D) computer graphical rendering where all of the objects in a scene are both lighted and shadowed in a single pass. This is referred too as a unified lighting &amp; shading engine.</p>\n<p>I then proceeded to explore this concept by prototyping a unified lighting and shading engine using OpenGL high-level shading language GLSL in the Xaede Game Engine’s shader system. The advantage to using GLSL is that the resulting program is compiled and run directly on the video card’s graphical processing unit (GPU) and resides within the video card’s memory.</p>\n<p>The goal for this prototype was to produce a rendering pipeline that could calculate both lighting and shadowing in a single mathematical calculation while providing correct object shadows, self-shadowing, and soft-shadows – thus accurately mimicking the next-generation technology exhibited by the Project Offset engine.</p>\n\n\n<p>I began by creating a vertex shader – this is a GLSL computer program that is called whenever a new vertex is rendered to the scene – to determine where the vertex lies within the projection and model-view matrices, to retain the corresponding pixel coordinates for the vertex, to preserve the current color of the pixels associated with the vertex, and to calculate a normal vector from the vertex against the angle of the lighting source.</p>\n<p>The information calculated and retained by the vertex shader is then passed to the GLSL pixel shader. The pixel shader is the computer program applied to each pixel rendered in the scene in order to create the 2D frame composite of the 3D objects rendered in the scene in order to present the scene to the computer user. The pixel shader begins by calculating the lighting value for the pixel, then by calculating the shading value for the pixel. The lighting algorithm works in the following fashion:</p>\n<p>Note that the following values are represented as red-green-blue-alpha (RGBA) and therefore mathematically calculable as x-y-z-w vectors.</p>\n<p>Emissive Light</p>\n<p>The emissive light value is not calculated, but rather set as a constant value.</p>\n<p>v(eL) = eL</p>\n<p>Ambient Light</p>\n<p>The ambient lighting value is calculated by obtaining the ambient light amount multiplied by the ambient light’s color.</p>\n<p>v(aL) = (aL * lC)</p>\n<p>Diffuse Light</p>\n<p>The diffuse lighting value is calculated by first, obtaining the normalized result of the light location subtracted by the vertex location (obtained from the vertex shader). The result of this is then used in a dot product against the vertex normal, and is referred too as the diffuse “term”.</p>\n<p>lV = normalized(lV – vL)<br>\ndT(dL) = (vN dot lV)</p>\n<p>If the diffuse term exceeds a value of 0.0 (i.e. it has a real-number value greater than zero) the local diffuse lighting value is calculated as a value clamped between 0.0 and 1.0 resulting from the multiplication of the diffuse light intensity against the light color against the diffuse term. This also allows the specular lighting value to be calculated.</p>\n<p>v(dL) = (dL * lC * dT)</p>\n<p>Specular Light</p>\n<p>The specular lighting value is only calculated when the diffuse term exceeds a value of 0.0. The specular lighting value is calculated by first, obtaining a direction vector resulting from the normalization of the camera location subtracted by the vertex location (obtained from the vertex shader). A specular vector is then calculated by obtaining the normalized result of the light’s vector (obtained from the diffuse lighting calculation) added to the direction vector. This specular vector is used to calculate the specular “term” by obtaining the result of the vertex normal (obtained from the vertex shader) dot product against the specular vector raised to the power of the light shininess value.</p>\n<p>dV = normalized(cL – vL)<br>\nsV = normalized(lV + dV)<br>\nsT(sL) = ((vN dot sV) ^ shininess)</p>\n<p>Then the specular lighting value is finally calculated by obtaining the result (clamped between 0.0 and 1.0) of the specular light intensity multiplied by the light’s color multiplied by the specular term.</p>\n<p>v(sL) = (sL * lC * sT)</p>\n\n\n<p>Lighting Calculation</p>\n<p>The final lighting calculation is the obtained by adding the emissive value added to the ambient value added to the diffuse value added to the specular value, the result of which is multiplied by the original pixel color. This calculation results in either brightening or darkening a given pixel based upon the result of the lighting calculation.</p>\n<p>light = ((v(eL) + v(aL) + v(dL) + v(sL)) * pC)</p>\n<p>Shadow Calculation</p>\n<p>The set of coordinates defining the objects’ shadow is calculated by obtaining the projection coordinates (obtained from the vertex shader) divided by the coordinates’ quotient. If the shadow coordinates fall within the visible spectrum, the shadow color is calculated by applying the GLSL 2D shadow calculation against the shadow map (a map obtained by first rendering the scene in a shadow-map mode where the scene is rendered as a gray-scale depth-map from the camera position of the light source, then retaining the rendered frame from this calculation) generated for this scene.</p>\n<p>shadow = (sM glsl-shadow-2D sC)</p>\n<p>The result of this calculation allow for correct exact-shadows and self-shadows. The shadow calculation is then modified to create the soft-shadow effect.</p>\n<p>Soft-Shadow’s: PCF</p>\n<p>The resulting shadow color is then modified by the application of a percentage closer filtering (PCF) algorithm to create the first-pass of the soft-shadowing system. This is applied by sampling the pixels surrounding the shadowed pixel to modify the shadow color value, thus blurring the outline of the shadow. The resulting blur however is pixelated by the volume of the PCF pixel-sampling calculations. I found optimal sampling results in a 1024×768 frame with a ratio of 0.001953125 (i.e. 1.0 / 512.0) performing 8 sampling operations (one block for each surrounding pixel sampling volume).</p>\n<p>Soft-Shadow’s: Gaussian-Blur</p>\n<p>The resulting pixelization is then softened by two Gaussian-blur calculations. The first is a vertical-pass over the PCF-sampled pixel volumes followed by a horizontal pass of the PCF-sampled pixel volumes. The Gaussian-blur algorithm was applied by first calculating the versicle and horizontal pixel offsets for the PCF pixel volumes, and then applying them to the pixels contained within those volumes by adding the color of the pixel modified by the color of the offset pixel to the existing shadow pixel color.</p>\n<p>Final Calculation</p>\n<p>Once the lighting and shading calculations have completed, the resulting pixel color of the lighting color (already applied to the existing pixel color) multiplied by the shadow pixel color (if a shadow exists for this pixel, otherwise the lighting calculation alone is applied).</p>\n<p>pixel = (light * shadow)</p>\n\n</div><footer class=\"article-footer\"><span class=\"eyebrow\">PUBLICATION RECORD</span><p>Recovered article · Legacy Xaede · June 9, 2009</p><p>Originally published on Xaede. The recovered text is included in this reading edition.</p><p class=\"small\">Original authorship and publication date are preserved. This edition restores the reading layout. Recovered historical images are hosted here; unavailable images are identified where relevant.</p></footer></article>",
  "structuredData": {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "Person",
        "@id": "https://xaede.net/#josh-english",
        "name": "Josh English",
        "url": "https://xaede.net/about",
        "sameAs": [
          "https://github.com/josh-english-2k18",
          "https://medium.com/@jengas",
          "https://www.linkedin.com/in/joshua-s-english",
          "https://x.com/JoshSeriesAI"
        ],
        "jobTitle": "VP of Engineering",
        "description": "Josh English is VP of Engineering at Chronicle Media and was previously CTO of Series Entertainment. His career spans more than twenty-five years of distributed systems, game technology and engineering leadership. At Google he worked as a lead engineer on large-scale services and applied ML; at Big Run Studios he led multidisciplinary technology teams across seven time zones and four countries. At Series he directed engineering, QA, DevOps and infrastructure supporting live commercial games and AI-enabled production. He also builds personal games and developer tools to investigate how emerging capabilities work in practice, and writes about engineering judgment, organisation design and product economics.",
        "mainEntityOfPage": {
          "@id": "https://xaede.net/about#webpage"
        }
      },
      {
        "@type": "WebSite",
        "@id": "https://xaede.net/#website",
        "url": "https://xaede.net/",
        "name": "Xaede",
        "alternateName": [
          "Xaede.net",
          "Josh English — Xaede"
        ],
        "inLanguage": "en",
        "publisher": {
          "@id": "https://xaede.net/#josh-english"
        }
      },
      {
        "@type": "WebPage",
        "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#webpage",
        "url": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading",
        "name": "Xaede Game Engine - Unified Lighting & Shading",
        "description": "A 2009 explanation of unified lighting and shading in the Xaede engine, documenting the rendering approach and influences of the period.",
        "inLanguage": "en",
        "isPartOf": {
          "@id": "https://xaede.net/#website"
        },
        "about": {
          "@id": "https://xaede.net/#josh-english"
        },
        "mainEntity": {
          "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#article"
        },
        "breadcrumb": {
          "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#breadcrumbs"
        }
      },
      {
        "@type": "Article",
        "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#article",
        "name": "Xaede Game Engine - Unified Lighting & Shading",
        "headline": "Xaede Game Engine - Unified Lighting & Shading",
        "description": "A 2009 explanation of unified lighting and shading in the Xaede engine, documenting the rendering approach and influences of the period.",
        "author": {
          "@id": "https://xaede.net/#josh-english"
        },
        "inLanguage": "en",
        "mainEntityOfPage": {
          "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#webpage"
        },
        "genre": "Archive edition",
        "datePublished": "2009-06-09",
        "wordCount": 1097,
        "url": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading"
      },
      {
        "@type": "BreadcrumbList",
        "@id": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading#breadcrumbs",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "Home",
            "item": "https://xaede.net/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "Writing",
            "item": "https://xaede.net/writing"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "Xaede Game Engine - Unified Lighting & Shading",
            "item": "https://xaede.net/writing/xaede-game-engine-unified-lighting-shading"
          }
        ]
      }
    ]
  }
}
