Cleanup shadows
This commit is contained in:
@@ -1,28 +1,29 @@
|
||||
#version 450 core
|
||||
struct PosColNorm { vec4 pos; vec4 dummy; };
|
||||
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
|
||||
layout (std140, binding = 1) uniform TheLight { vec4 lightPos; vec4 colrad; };
|
||||
layout (std430, binding = 3) readonly buffer Data { PosColNorm data[]; };
|
||||
layout (std430, binding = 5) readonly buffer Indices { uint indices[]; };
|
||||
layout(location=0)uniform vec3 lightPos;
|
||||
layout(location=1)uniform float radiusUniform;
|
||||
float closestPointOnLineParam3 (vec3 a, vec3 b, vec3 p) {
|
||||
return dot(p - a,b-a) / dot(b-a,b-a);
|
||||
}
|
||||
vec3 closestPointOnSeg3 (vec3 a,vec3 b, vec3 p) {
|
||||
float x = closestPointOnLineParam3(a,b,p);
|
||||
if (x < 0) {
|
||||
return a;
|
||||
} else{ return (x > 1 ? b: a + (x * (b- a))); }
|
||||
return (x < 0
|
||||
? a
|
||||
: (x > 1
|
||||
? b
|
||||
: a + (x * (b- a)))) ;
|
||||
}
|
||||
vec4 shift(vec4 p) { return (vec4(p.xyz + (10000 * (p.xyz - lightPos)), 1)); }
|
||||
vec4 shift(vec4 p) { return (vec4(p.xyz + (10000 * (p.xyz - lightPos.xyz)), 1)); }
|
||||
vec4 shiftBy(float x, vec4 p) {
|
||||
return (vec4(lightPos + (x * normalize(p.xyz - lightPos)), 1));
|
||||
return (vec4(lightPos.xyz + (x * normalize(p.xyz - lightPos.xyz)), 1));
|
||||
}
|
||||
vec4 projNear (vec4 pos)
|
||||
{
|
||||
// note we project to a specific height
|
||||
// this is quite brittle, not ideal
|
||||
vec3 dir = pos.xyz - lightPos ;
|
||||
vec3 dir = pos.xyz - lightPos.xyz ;
|
||||
float a = (100 - pos.z) / dir.z ;
|
||||
vec2 xy = (pos.xyz + a * dir).xy ;
|
||||
return vec4 ( xy, 100 , 1) ;
|
||||
@@ -31,11 +32,13 @@ vec4 shiftNear(vec4 pos)
|
||||
{
|
||||
vec4 sp = shift(pos);
|
||||
return (sp.z > 100 ? projNear(pos) : sp);
|
||||
}
|
||||
int ks[6] =
|
||||
{0,1,2 // 2--3
|
||||
,2,1,3 // | |
|
||||
}; // 0--1
|
||||
}
|
||||
// Output 6 vertices for 4 data inputs
|
||||
int ks[6] = // Inputdata: n1a Shadow:
|
||||
{0,1,2 // 2--3 / p2--p3
|
||||
,2,1,3 // | | p0---p1 / |
|
||||
}; // 0--1 / / |
|
||||
// n0a p0----p1
|
||||
void main()
|
||||
{
|
||||
int k = ks[gl_VertexID % 6];
|
||||
@@ -44,8 +47,8 @@ void main()
|
||||
vec4 p1 = data[indices[i0+1]].pos;
|
||||
vec3 n0a = data[indices[i0+2]].pos.xyz;
|
||||
vec3 n1a = data[indices[i0+3]].pos.xyz;
|
||||
vec3 closepoint = closestPointOnSeg3(p0.xyz,p1.xyz,lightPos);
|
||||
float ru2 = radiusUniform * radiusUniform;
|
||||
vec3 closepoint = closestPointOnSeg3(p0.xyz,p1.xyz,lightPos.xyz);
|
||||
float ru2 = colrad.w * colrad.w;
|
||||
vec4 mid = 0.5 * (p0 + p1);
|
||||
vec3 n0 = cross(p1.xyz - p0.xyz, n0a - p0.xyz);
|
||||
vec3 n1 = cross(p0.xyz - p1.xyz, n1a - p1.xyz);
|
||||
@@ -53,15 +56,15 @@ void main()
|
||||
vec3 lightDir2 = p1.xyz - lightPos.xyz;
|
||||
vec4 p2 = shiftNear(p0);
|
||||
vec4 p3 = shiftNear(p1);
|
||||
// first test if the edge is part of the silhouette
|
||||
// that is, if the normals of the faces connected by the edge point are in
|
||||
// test if the edge is part of the silhouette
|
||||
// that is, if the slopes of the faces connected to the edge are in
|
||||
// "different directions" wrt the light direction
|
||||
if (dot(n0, lightDir) * dot(n1, lightDir) <= 0 &&
|
||||
(dot(lightDir, lightDir) < ru2 || dot(lightDir2, lightDir2) < ru2))
|
||||
// using <= rather than < seems to get rid of overlapping shadow
|
||||
// artefacts
|
||||
{
|
||||
if (dot(n0, lightDir) > 0)
|
||||
if (dot(n0, lightDir) < 0)
|
||||
{
|
||||
vec4 sp[4] = {p1,p0,p3,p2};
|
||||
gl_Position = theMat * sp[k];}
|
||||
|
||||
Reference in New Issue
Block a user