转载自: http://glslsandbox.com/e#43841.1
#ifdef GL_ES
precision mediump float;
#endif
#extension GL_OES_standard_derivatives : enable
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
struct Line {
vec2 a;
vec2 b;
};
Line lightLines[3];
Line wallLines[3];
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
float lightDueTo(in vec2 pos, in float len,in float intensity) {
return (((atan((len-pos.x)/pos.y)+atan(pos.x/pos.y)))/pos.y)*intensity;
}
float light(in vec2 line1,in vec2 line2, in vec2 point,in float intensity) {
vec2 line2Translated = line2 - line1;
vec2 pointTranslated = point - line1;
float len = length(line2Translated);
float theta = atan(line2Translated.y,line2Translated.x);
vec2 pointTransformed = rotate(pointTranslated,theta);
return lightDueTo(pointTransformed,len,intensity);
}
bool get_line_intersection(vec2 p0, vec2 p1 ,
vec2 p2, vec2 p3, out vec3 i)
{
float s1_x, s1_y, s2_x, s2_y;
s1_x = p1.x - p0.x; s1_y = p1.y - p0.y;
s2_x = p3.x - p2.x; s2_y = p3.y - p2.y;
float s, t;
s = (-s1_y * (p0.x - p2.x) + s1_x * (p0.y - p2.y)) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0.y - p2.y) - s2_y * (p0.x - p2.x)) / (-s2_x * s1_y + s1_x * s2_y);
if (s >= 0. && s <= 1. && t >= 0. && t <= 1.)
{
// Collision detected
i.x = p0.x + (t * s1_x);
i.y = p0.y + (t * s1_y);
return true;
}
return false; // No collision
}
void getLinesToUse(vec2 pos, vec2 lights, vec2 walls, out Line[100] newLights, out int newLightsLength) {
}
void main( void ) {
lightLines[0] = Line(vec2(-500.,-500.),vec2(500.,500.0));
lightLines[1] = Line(vec2(500.,-500.),vec2(-500.,500.0));
lightLines[2] = Line(vec2(0.,-0),(mouse*2. -1.)*1000.);
wallLines[0] = Line(vec2(-500.,-600.),vec2(500.,400.0));
wallLines[1] = Line(vec2(500.,-600.),vec2(-500.,400.0));
wallLines[2] = Line(vec2(0.,-100),vec2(500,-100));
vec2 position = (( gl_FragCoord.xy / resolution.xy ) )* 2000.0 - vec2(1000,1000);
float l =0.;
for(int n = 0; n < 3; n++) {
l+= light(lightLines[n].a,lightLines[n].b,position,(10.*sin(time)/3.14159)+3.14159);
}
gl_FragColor = vec4( l,l,l, 1 );
}