Try to improve instancing shadows

This commit is contained in:
2023-03-12 14:06:11 +00:00
parent b88a8d5776
commit 9947979b52
9 changed files with 368 additions and 296 deletions
+74 -67
View File
@@ -1,78 +1,85 @@
#version 450 core
layout (lines_adjacency) in;
layout (invocations = 20) in;
layout (triangle_strip, max_vertices = 4) out;
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
layout (std140, binding = 1) uniform LightsBlock
{
vec4 posBool[20];
vec4 colRad[20];
} ;
vec3 lightPos1 = posBool[gl_InvocationID].xyz;
layout(lines_adjacency) in;
layout(invocations = 20) in;
layout(triangle_strip, max_vertices = 4) out;
layout(std140, binding = 0) uniform TheMat { mat4 theMat; };
layout(std140, binding = 1) uniform LightsBlock {
vec4 posBool[20];
vec4 colRad[20];
};
vec3 lightPos = posBool[gl_InvocationID].xyz;
float theBool = posBool[gl_InvocationID].w;
vec4 shift (vec4 p) { return (vec4 (p.xyz + (100000*(p.xyz-lightPos1)), 1));} ;
vec4 shiftBy (float x,vec4 p) { return (vec4 (lightPos1 + (x*normalize(p.xyz-lightPos1)), 1));} ;
// copied from lighting/cap.geom, should not be changed on its own
vec4 projNear (vec4 pos)
{
// note we project to a specific height
// this is quite brittle, not ideal
vec3 dir = pos.xyz - lightPos1 ;
float a = (140 - pos.z) / dir.z ;
vec2 xy = (pos.xyz + a * dir).xy ;
return vec4 ( xy, 140 , 1) ;
} ;
vec4 shiftNear (vec4 pos)
{ vec4 sp = shift(pos);
if (sp.z > 140)
{ return projNear(pos) ; }
else
{ return sp ; }
} ;
vec4 f (vec4 p) {return (theMat * p);} ;
void main()
{
if (theBool == 1){
// float ru2 = radiusUniform * radiusUniform ;
float ru = colRad[gl_InvocationID].w ;
float ru2 = ru * ru;
float rad = colRad[gl_InvocationID].w;
vec4 shift(vec4 p) { return (vec4(p.xyz + (100000 * (p.xyz - lightPos)), 1)); };
vec4 shiftBy(float x, vec4 p) {
return (vec4(lightPos + (x * normalize(p.xyz - lightPos)), 1));
};
vec4 shift1(vec4 p) {
return (vec4(lightPos + (rad * normalize(p.xyz - lightPos)), 1));
};
// copied from shadow/cap.geom, should not be changed on its own
vec4 projNear(vec4 pos) {
// note we project to a specific height
// this is quite brittle, not ideal
vec3 dir = pos.xyz - lightPos;
float a = (140 - pos.z) / dir.z;
vec2 xy = (pos.xyz + a * dir).xy;
return vec4(xy, 140, 1);
};
vec4 shiftNear(vec4 pos) {
vec4 sp = shift(pos);
if (sp.z > 140) {
return projNear(pos);
} else {
return sp;
}
};
void main() {
if (theBool == 1) {
vec4 p0 = gl_in[0].gl_Position;
vec4 p1 = gl_in[1].gl_Position;
vec4 mid = 0.5*(p0 + p1);
vec4 mid = 0.5 * (p0 + p1);
vec3 n0a = gl_in[2].gl_Position.xyz;
vec3 n1a = gl_in[3].gl_Position.xyz;
vec3 n0 = cross(p1.xyz - p0.xyz, n0a - p0.xyz) ;
vec3 n1 = cross(p0.xyz - p1.xyz, n1a - p1.xyz) ;
//vec3 n2 = n0 + n1; // assumes the summands are normalized
vec3 lightDir = p0.xyz - lightPos1.xyz;
vec3 lightDir2 = p1.xyz - lightPos1.xyz;
// first test if the edge is part of the silhouette
vec3 n0 = cross(p1.xyz - p0.xyz, n0a - p0.xyz);
vec3 n1 = cross(p0.xyz - p1.xyz, n1a - p1.xyz);
vec3 lightDir = p0.xyz - lightPos.xyz;
vec3 lightDir2 = p1.xyz - lightPos.xyz;
// test if the edge is part of the silhouette
// that is, if the normals of the faces connected by the edge point 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) * dot(n1, lightDir) <= 0 &&
(dot(lightDir, lightDir) < rad * rad ||
dot(lightDir2, lightDir2) < rad * rad))
// using <= rather than < seems to get rid of overlapping shadow
// artefacts
{
vec4 p2 = shiftNear(p0);
vec4 p3 = shiftNear(p1);
if ( dot(n0 , lightDir) > 0)
{
gl_Position = f(p0); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p1); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p2); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p3); gl_Layer = gl_InvocationID; EmitVertex();
}
else
{
gl_Position = f(p1); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p0); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p3); gl_Layer = gl_InvocationID; EmitVertex();
gl_Position = f(p2); gl_Layer = gl_InvocationID; EmitVertex();
}
EndPrimitive();
vec4 p2 = shiftNear(p0);
vec4 p3 = shiftNear(p1);
gl_Position = theMat * p0;
gl_Layer = gl_InvocationID;
EmitVertex();
if (dot(n0, lightDir) > 0) {
gl_Position = theMat * p1;
gl_Layer = gl_InvocationID;
EmitVertex();
gl_Position = theMat * p2;
gl_Layer = gl_InvocationID;
EmitVertex();
} else {
gl_Position = theMat * p2;
gl_Layer = gl_InvocationID;
EmitVertex();
gl_Position = theMat * p1;
gl_Layer = gl_InvocationID;
EmitVertex();
}
gl_Position = theMat * p3;
gl_Layer = gl_InvocationID;
EmitVertex();
EndPrimitive();
} else {
}
else { }
}else {}
} else {
}
}