| 1 |
#version 110 |
| 2 |
|
| 3 |
// [XA] New "indexed" lightmode shader, which approximates |
| 4 |
// Doom's software lighting algorithm using the COLORMAP. |
| 5 |
// Looks very very close to the software renderer, |
| 6 |
// palette edits included, all while running on the GPU. :) |
| 7 |
|
| 8 |
// Big thanks to Jazz Mickle for pioneering this technique |
| 9 |
// and publishing an article explaining how it all works. |
| 10 |
// https://medium.com/@jmickle_/writing-a-doom-style-shader-for-unity-63fa13678634 |
| 11 |
|
| 12 |
// Also thanks to Gooberman, since I borrowed his method for |
| 13 |
// fixing rounding issues for the colorIndex variable below. |
| 14 |
// A little bit of fudgery goes a long way... :P |
| 15 |
// https://github.com/GooberMan/rum-and-raisin-doom/commit/91500a86b02243dd90958aae4427051ad5c63b0d |
| 16 |
|
| 17 |
// [XA] adjusted this value a bit from the truecolor shader, |
| 18 |
// to taste -- seems to match software pretty damn closely |
| 19 |
#define DOOMLIGHTFACTOR 200.0 |
| 20 |
|
| 21 |
uniform float lightlevel; |
| 22 |
uniform sampler2D colormap; |
| 23 |
|
| 24 |
// Returns color index based on R texel component |
| 25 |
float indexColor(float r) |
| 26 |
{ |
| 27 |
return r * 0.99609375 + 0.0009765625; // equivalent to (texel.r * 255 / 256) + (1 / 1024) |
| 28 |
} |
| 29 |
|
| 30 |
// Returns light index based on depth and light level. |
| 31 |
// Based on equations from EDGE. |
| 32 |
float indexLight(float z) |
| 33 |
{ |
| 34 |
float L = 63.0 * lightlevel; |
| 35 |
// Compute minimal light index (brightest) for light level |
| 36 |
float min_L = clamp(36.0 - L, 0.0, 31.0); |
| 37 |
// Compute and clamp light index |
| 38 |
return clamp(59.0 + DOOMLIGHTFACTOR - L - DOOMLIGHTFACTOR / max(0.0, z), min_L, 31.0); |
| 39 |
} |
| 40 |
|
| 41 |
// Returns RGB palette value based on color and light indices |
| 42 |
vec3 indexRGB(float ci, float li) |
| 43 |
{ |
| 44 |
// - Re-clamp light index to valid range in case it was modified by caller |
| 45 |
// - Convert to texture coordinates; divide by 32 since there's an extra 32nd row in the |
| 46 |
// colormap image for the invuln pal |
| 47 |
li = clamp(li, 0.0, 31.0) / 32.0; |
| 48 |
return texture2D(colormap, vec2(ci, li)).rgb; |
| 49 |
} |
| 50 |
|
| 51 |
uniform sampler2D tex; |
| 52 |
uniform int fade_mode; |
| 53 |
|
| 54 |
void main() |
| 55 |
{ |
| 56 |
// grab the texel and its color index, which is stuffed into the R channel |
| 57 |
vec4 texel = texture2D(tex, gl_TexCoord[0].st); |
| 58 |
float ci = indexColor(texel.r); |
| 59 |
float li = indexLight(gl_FragCoord.z); |
| 60 |
vec3 color; |
| 61 |
|
| 62 |
if (fade_mode == 1) |
| 63 |
{ |
| 64 |
// Smooth fade mode. |
| 65 |
color = mix(indexRGB(ci, floor(li)), indexRGB(ci, floor(li + 1.0)), fract(li)); |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
// Normal (banded) fade mode. |
| 70 |
color = indexRGB(ci, li); |
| 71 |
} |
| 72 |
|
| 73 |
// Apply color and alpha |
| 74 |
gl_FragColor = vec4(gl_Color.rgb * color, gl_Color.a * texel.g); |
| 75 |
} |