diff options
| author | chriscamacho <[email protected]> | 2020-03-25 09:28:16 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-25 10:28:16 +0100 |
| commit | 2dbcef218ca5ddf4fa0c7857ac80947af0da16d5 (patch) | |
| tree | 5b4e9400cbbb464b570d04eda83542437708d359 /examples/shaders/resources | |
| parent | e07281f8bdc46934f2141c87be72eff498d5eb7e (diff) | |
| download | raylib-2dbcef218ca5ddf4fa0c7857ac80947af0da16d5.tar.gz raylib-2dbcef218ca5ddf4fa0c7857ac80947af0da16d5.zip | |
spotlight example, each spot has own radius, mouse countrol (#1148)
NB glsl100 shader needs testing on "bare metal"
Co-authored-by: codifies <[email protected]>
Diffstat (limited to 'examples/shaders/resources')
| -rw-r--r-- | examples/shaders/resources/shaders/glsl100/spotlight.fs | 63 | ||||
| -rw-r--r-- | examples/shaders/resources/shaders/glsl330/spotlight.fs | 51 |
2 files changed, 78 insertions, 36 deletions
diff --git a/examples/shaders/resources/shaders/glsl100/spotlight.fs b/examples/shaders/resources/shaders/glsl100/spotlight.fs index 204b89a7..65612753 100644 --- a/examples/shaders/resources/shaders/glsl100/spotlight.fs +++ b/examples/shaders/resources/shaders/glsl100/spotlight.fs @@ -2,50 +2,67 @@ precision mediump float; -#define MAX_SPOTS 4 -#define RADIUS 256.0 -#define INNER 200.0 +#define MAX_SPOTS 3 -// Inputs -// array of spotlight positions -uniform vec2 spots[MAX_SPOTS]; -uniform float screenWidth; // width of the screen +struct Spot { + vec2 pos; // window coords of spot + float inner; // inner fully transparent centre radius + float radius; // alpha fades out to this radius +}; + +uniform Spot spots[MAX_SPOTS]; // Spotlight positions array +uniform float screenWidth; // Width of the screen void main() { - float alpha; + float alpha = 1.0; // get the position of the current fragment (screen coordinates!) vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y); // find out which spotlight is nearest float d = 65000.0; // some high value - float di = 0.0; + int fi = -1; for (int i = 0; i < MAX_SPOTS; i++) { - di = distance(pos, spots[i]); - if (d > di) d = di; + for (int j = 0; j < MAX_SPOTS; j++) + { + float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius; + if (d > dj ) + { + d = dj; + fi = i; + } + } } // d now equals distance to nearest spot... - if (d > RADIUS) { - alpha = 1.0; - } else { - if (d < INNER) { - alpha = 0.0; - } else { - alpha = (d - INNER) / (RADIUS - INNER); + // allowing for the different radii of all spotlights + if (fi != -1) { + + if (d > spots[fi].radius) + { + alpha = 1.0; + } + else + { + if (d < spots[fi].inner) + { + alpha = 0.0; + } + else + { + alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner); + } } } - // right hand side of screen is dimly lit, could make the - // threshold value user definable. - if (pos.x > screenWidth/2.0 && alpha > 0.9) { - alpha = 0.9; - } + // Right hand side of screen is dimly lit, + // could make the threshold value user definable + if ((pos.x > screenWidth/2.0) && (alpha > 0.9)) alpha = 0.9; // could make the black out colour user definable... gl_FragColor = vec4( 0, 0, 0, alpha); diff --git a/examples/shaders/resources/shaders/glsl330/spotlight.fs b/examples/shaders/resources/shaders/glsl330/spotlight.fs index f20d92a8..fe53ad61 100644 --- a/examples/shaders/resources/shaders/glsl330/spotlight.fs +++ b/examples/shaders/resources/shaders/glsl330/spotlight.fs @@ -9,36 +9,61 @@ out vec4 finalColor; // NOTE: Add here your custom variables -#define MAX_SPOTS 2 -#define RADIUS 128 -#define INNER 96 +#define MAX_SPOTS 3 -uniform vec2 spots[MAX_SPOTS]; // Spotlight positions array +struct Spot { + vec2 pos; // window coords of spot + float inner; // inner fully transparent centre radius + float radius; // alpha fades out to this radius +}; + +uniform Spot spots[MAX_SPOTS]; // Spotlight positions array uniform float screenWidth; // Width of the screen void main() { - float alpha = 0.0; + float alpha = 1.0; // Get the position of the current fragment (screen coordinates!) + vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y); // Find out which spotlight is nearest float d = 65000; // some high value - float di = 0; + int fi = -1; // found index for (int i = 0; i < MAX_SPOTS; i++) { - di = distance(pos, spots[i]); - if (d > di) d = di; + for (int j = 0; j < MAX_SPOTS; j++) + { + float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius; + if (d > dj ) + { + d = dj; + fi = i; + } + } } // d now equals distance to nearest spot... - if (d > RADIUS) alpha = 1.0; - else - { - if (d < INNER) alpha = 0.0; - else alpha = (d - INNER)/(RADIUS - INNER); + // allowing for the different radii of all spotlights + if (fi != -1) { + + if (d > spots[fi].radius) + { + alpha = 1.0; + } + else + { + if (d < spots[fi].inner) + { + alpha = 0.0; + } + else + { + alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner); + } + } } // Right hand side of screen is dimly lit, |
