-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbal.fs
54 lines (47 loc) · 1.68 KB
/
lightbal.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
uniform sampler2D tex;
uniform vec2 _emitA, _emitB;
//uniform vec2 casterA, casterB;
in vec4 attr1;
uniform vec2 screen;
uniform float il;
const float pi = 3.141592653;
void main(void)
{
vec2 casterA = attr1.xy, casterB = attr1.zw;
vec2 uv2 = gl_FragCoord.xy/screen;
vec2 p = 2*uv2 - 1;
vec2 mid = 0.5*(_emitA + _emitB);
vec2 pmid = p-mid;
float R = length(_emitB-_emitA)/2;
vec2 n_pmid = normalize(vec2(-pmid.y, pmid.x));
vec2 emitA=mid+n_pmid*R,emitB=mid-n_pmid*R;
vec2 ab = emitB-emitA;
vec2 ap = p-emitA;
vec2 bp = p-emitB;
vec2 sp = p-casterA;
vec2 ep = p-casterB;
vec2 se = casterB-casterA;
float s = sign(ap.x*bp.y-ap.y*bp.x);
float s2 = sign(sp.x*ep.y-sp.y*ep.x);
float ino = 1./dot(ab,ab);
vec2 n_ab = vec2(-ab.y, ab.x);
vec2 n_se = vec2(-se.y, se.x);
float ts = dot(ap,n_ab)/dot(sp,n_ab);
float te = dot(ap,n_ab)/dot(ep,n_ab);
float prs = dot(ap-ts*sp, ab)*ino;
float pre = dot(ap-te*ep, ab)*ino;
prs = clamp(prs, 0, 1); pre = clamp(pre, 0, 1);
if (ts < 1) prs = 0;
if (te < 1) pre = 1;
if (dot(casterA-emitA,n_ab) < 0 && dot(casterB-emitA,n_ab) < 0) { prs = 1; pre = 0;}
if (dot(emitA-casterA,n_se) > 0 && dot(emitB-casterA,n_se) > 0) { prs = 1; pre = 0;}
if (s2 < 0) { prs = 1; pre = 0; }
if (ts < 0 && te < 0) { prs = 1; pre = 0; }
//if (s < 0) { prs = 0; pre = 1; }
float a1 = atan(emitB.y-p.y,emitB.x-p.x),
a2 = atan(emitA.y-p.y,emitA.x-p.x);
float nt = clamp(mod((a1-a2)/pi, 2),0,1);
gl_FragColor = vec4((clamp(pre-prs,0,1)),0*il*nt/length(ab),0, 1);
//gl_FragColor = vec4((s),0/*,(s2)*/,0, 1);
// gl_FragColor = vec4(1,attr1.x,attr1.y, 1);
}