as per request spotlight example (#1146)
Co-authored-by: codifies <nospam@antispam.com>
This commit is contained in:
parent
c45fe62abc
commit
efe359d613
5 changed files with 332 additions and 1 deletions
52
examples/shaders/resources/shaders/glsl100/spotlight.fs
Normal file
52
examples/shaders/resources/shaders/glsl100/spotlight.fs
Normal 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);
|
||||
}
|
53
examples/shaders/resources/shaders/glsl330/spotlight.fs
Normal file
53
examples/shaders/resources/shaders/glsl330/spotlight.fs
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue