as per request spotlight example (#1146)

Co-authored-by: codifies <nospam@antispam.com>
This commit is contained in:
chriscamacho 2020-03-24 13:27:14 +00:00 committed by GitHub
parent c45fe62abc
commit efe359d613
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 332 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,52 @@
#version 100
precision mediump float;
#define MAX_SPOTS 4
#define RADIUS 256.0
#define INNER 200.0
// Inputs
// array of spotlight positions
uniform vec2 spots[MAX_SPOTS];
uniform float screenWidth; // width of the screen
void main()
{
float alpha;
// 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;
for (int i = 0; i < MAX_SPOTS; i++)
{
di = distance(pos, spots[i]);
if (d > di) d = di;
}
// 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);
}
}
// 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);
}

View file

@ -0,0 +1,53 @@
#version 330
// Output fragment color
out vec4 finalColor;
#define MAX_SPOTS 4
#define RADIUS 256
#define INNER 200
// Inputs
// array of spotlight positions
uniform vec2 spots[MAX_SPOTS];
uniform float screenWidth; // width of the screen
void main()
{
float alpha;
// 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;
for (int i = 0; i < MAX_SPOTS; i++)
{
di = distance(pos, spots[i]);
if (d > di) d = di;
}
// 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);
}
}
// 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...
finalColor = vec4( 0, 0, 0, alpha);
}