#version 110 // [XA] New "indexed" lightmode shader, which approximates // Doom's software lighting algorithm using the COLORMAP. // Looks very very close to the software renderer, // palette edits included, all while running on the GPU. :) // Big thanks to Jazz Mickle for pioneering this technique // and publishing an article explaining how it all works. // https://medium.com/@jmickle_/writing-a-doom-style-shader-for-unity-63fa13678634 // Also thanks to Gooberman, since I borrowed his method for // fixing rounding issues for the colorIndex variable below. // A little bit of fudgery goes a long way... :P // https://github.com/GooberMan/rum-and-raisin-doom/commit/91500a86b02243dd90958aae4427051ad5c63b0d // [XA] adjusted this value a bit from the truecolor shader, // to taste -- seems to match software pretty damn closely #define DOOMLIGHTFACTOR 200.0 uniform float lightlevel; uniform sampler2D colormap; // Returns color index based on R texel component float indexColor(float r) { return r * 0.99609375 + 0.0009765625; // equivalent to (texel.r * 255 / 256) + (1 / 1024) } // Returns light index based on depth and light level. // Based on equations from EDGE. float indexLight(float z) { float L = 63.0 * lightlevel; // Compute minimal light index (brightest) for light level float min_L = clamp(36.0 - L, 0.0, 31.0); // Compute and clamp light index return clamp(59.0 + DOOMLIGHTFACTOR - L - DOOMLIGHTFACTOR / max(0.0, z), min_L, 31.0); } // Returns RGB palette value based on color and light indices vec3 indexRGB(float ci, float li) { // - Re-clamp light index to valid range in case it was modified by caller // - Convert to texture coordinates; divide by 32 since there's an extra 32nd row in the // colormap image for the invuln pal li = clamp(li, 0.0, 31.0) / 32.0; return texture2D(colormap, vec2(ci, li)).rgb; } uniform sampler2D tex; uniform int fade_mode; void main() { // grab the texel and its color index, which is stuffed into the R channel vec4 texel = texture2D(tex, gl_TexCoord[0].st); float ci = indexColor(texel.r); float li = indexLight(gl_FragCoord.z); vec3 color; if (fade_mode == 1) { // Smooth fade mode. color = mix(indexRGB(ci, floor(li)), indexRGB(ci, floor(li + 1.0)), fract(li)); } else { // Normal (banded) fade mode. color = indexRGB(ci, li); } // Apply color and alpha gl_FragColor = vec4(gl_Color.rgb * color, gl_Color.a * texel.g); }