2017-03-01 02:33:53 +00:00
|
|
|
|
2019-03-05 17:55:31 +00:00
|
|
|
layout(location=0) in vec2 TexCoord;
|
2018-06-12 21:52:33 +00:00
|
|
|
layout(location=0) out vec4 FragColor;
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// A node in an AABB binary tree with lines stored in the leaf nodes
|
2017-03-01 02:33:53 +00:00
|
|
|
struct GPUNode
|
|
|
|
{
|
2018-10-03 23:29:25 +00:00
|
|
|
vec2 aabb_min; // Min xy values for the axis-aligned box containing the node and its subtree
|
|
|
|
vec2 aabb_max; // Max xy values
|
|
|
|
int left; // Left subnode index
|
|
|
|
int right; // Right subnode index
|
|
|
|
int line_index; // Line index if it is a leaf node, otherwise -1
|
|
|
|
int padding; // Unused - maintains 16 byte alignment
|
2017-03-01 02:33:53 +00:00
|
|
|
};
|
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// 2D line segment, referenced by leaf nodes
|
2017-03-07 14:58:22 +00:00
|
|
|
struct GPULine
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
2018-10-03 23:29:25 +00:00
|
|
|
vec2 pos; // Line start position
|
|
|
|
vec2 delta; // Line end position - line start position
|
2017-03-01 02:33:53 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 06:54:34 +00:00
|
|
|
layout(std430, binding = 4) buffer LightNodes
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
2017-03-07 14:58:22 +00:00
|
|
|
GPUNode nodes[];
|
2017-03-01 02:33:53 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 06:54:34 +00:00
|
|
|
layout(std430, binding = 5) buffer LightLines
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
2017-03-07 14:58:22 +00:00
|
|
|
GPULine lines[];
|
2017-03-01 02:33:53 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 06:54:34 +00:00
|
|
|
layout(std430, binding = 6) buffer LightList
|
2017-03-02 17:07:47 +00:00
|
|
|
{
|
|
|
|
vec4 lights[];
|
|
|
|
};
|
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Overlap test between line segment and axis-aligned bounding box. Returns true if they overlap.
|
2017-03-07 14:58:22 +00:00
|
|
|
bool overlapRayAABB(vec2 ray_start2d, vec2 ray_end2d, vec2 aabb_min2d, vec2 aabb_max2d)
|
|
|
|
{
|
|
|
|
// To do: simplify test to use a 2D test
|
|
|
|
vec3 ray_start = vec3(ray_start2d, 0.0);
|
|
|
|
vec3 ray_end = vec3(ray_end2d, 0.0);
|
|
|
|
vec3 aabb_min = vec3(aabb_min2d, -1.0);
|
|
|
|
vec3 aabb_max = vec3(aabb_max2d, 1.0);
|
|
|
|
|
|
|
|
vec3 c = (ray_start + ray_end) * 0.5f;
|
|
|
|
vec3 w = ray_end - c;
|
|
|
|
vec3 h = (aabb_max - aabb_min) * 0.5f; // aabb.extents();
|
|
|
|
|
|
|
|
c -= (aabb_max + aabb_min) * 0.5f; // aabb.center();
|
|
|
|
|
|
|
|
vec3 v = abs(w);
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2017-03-07 14:58:22 +00:00
|
|
|
if (abs(c.x) > v.x + h.x || abs(c.y) > v.y + h.y || abs(c.z) > v.z + h.z)
|
|
|
|
return false; // disjoint;
|
|
|
|
|
|
|
|
if (abs(c.y * w.z - c.z * w.y) > h.y * v.z + h.z * v.y ||
|
|
|
|
abs(c.x * w.z - c.z * w.x) > h.x * v.z + h.z * v.x ||
|
|
|
|
abs(c.x * w.y - c.y * w.x) > h.x * v.y + h.y * v.x)
|
|
|
|
return false; // disjoint;
|
|
|
|
|
|
|
|
return true; // overlap;
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Intersection test between two line segments.
|
|
|
|
// Returns the intersection point as a value between 0-1 on the ray line segment. 1.0 if there was no hit.
|
2017-03-07 14:58:22 +00:00
|
|
|
float intersectRayLine(vec2 ray_start, vec2 ray_end, int line_index, vec2 raydelta, float rayd, float raydist2)
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
|
|
|
const float epsilon = 0.0000001;
|
2017-03-07 14:58:22 +00:00
|
|
|
GPULine line = lines[line_index];
|
|
|
|
|
|
|
|
vec2 raynormal = vec2(raydelta.y, -raydelta.x);
|
|
|
|
|
|
|
|
float den = dot(raynormal, line.delta);
|
|
|
|
if (abs(den) > epsilon)
|
|
|
|
{
|
|
|
|
float t_line = (rayd - dot(raynormal, line.pos)) / den;
|
|
|
|
if (t_line >= 0.0 && t_line <= 1.0)
|
|
|
|
{
|
|
|
|
vec2 linehitdelta = line.pos + line.delta * t_line - ray_start;
|
|
|
|
float t = dot(raydelta, linehitdelta) / raydist2;
|
|
|
|
return t > 0.0 ? t : 1.0;
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2017-03-07 14:58:22 +00:00
|
|
|
return 1.0;
|
|
|
|
}
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Returns true if an AABB tree node is a leaf node. Leaf nodes contains a line.
|
2017-03-07 14:58:22 +00:00
|
|
|
bool isLeaf(int node_index)
|
|
|
|
{
|
|
|
|
return nodes[node_index].line_index != -1;
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Perform ray intersection test between the ray line segment and all the lines in the AABB binary tree.
|
|
|
|
// Returns the intersection point as a value between 0-1 on the ray line segment. 1.0 if there was no hit.
|
2017-03-07 14:58:22 +00:00
|
|
|
float rayTest(vec2 ray_start, vec2 ray_end)
|
|
|
|
{
|
|
|
|
vec2 raydelta = ray_end - ray_start;
|
2017-03-01 02:33:53 +00:00
|
|
|
float raydist2 = dot(raydelta, raydelta);
|
|
|
|
vec2 raynormal = vec2(raydelta.y, -raydelta.x);
|
2017-03-07 14:58:22 +00:00
|
|
|
float rayd = dot(raynormal, ray_start);
|
|
|
|
if (raydist2 < 1.0)
|
|
|
|
return 1.0;
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2017-03-07 14:58:22 +00:00
|
|
|
float t = 1.0;
|
2017-03-01 02:33:53 +00:00
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Walk the AABB binary tree searching for nodes touching the ray line segment's AABB box.
|
|
|
|
// When it reaches a leaf node, use a line segment intersection test to see if we got a hit.
|
|
|
|
|
2019-01-04 14:51:59 +00:00
|
|
|
int stack[32];
|
2017-03-07 14:58:22 +00:00
|
|
|
int stack_pos = 1;
|
2019-05-24 09:42:23 +00:00
|
|
|
stack[0] = NodesCount - 1;
|
2017-03-07 14:58:22 +00:00
|
|
|
while (stack_pos > 0)
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
2017-03-07 14:58:22 +00:00
|
|
|
int node_index = stack[stack_pos - 1];
|
|
|
|
|
|
|
|
if (!overlapRayAABB(ray_start, ray_end, nodes[node_index].aabb_min, nodes[node_index].aabb_max))
|
|
|
|
{
|
|
|
|
stack_pos--;
|
|
|
|
}
|
|
|
|
else if (isLeaf(node_index))
|
|
|
|
{
|
|
|
|
t = min(intersectRayLine(ray_start, ray_end, nodes[node_index].line_index, raydelta, rayd, raydist2), t);
|
|
|
|
stack_pos--;
|
|
|
|
}
|
2019-01-04 14:51:59 +00:00
|
|
|
else if (stack_pos == 32)
|
2017-03-01 02:33:53 +00:00
|
|
|
{
|
2017-03-07 14:58:22 +00:00
|
|
|
stack_pos--; // stack overflow
|
2017-03-01 02:33:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-07 14:58:22 +00:00
|
|
|
stack[stack_pos - 1] = nodes[node_index].left;
|
|
|
|
stack[stack_pos] = nodes[node_index].right;
|
|
|
|
stack_pos++;
|
2017-03-01 02:33:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:58:22 +00:00
|
|
|
return t;
|
2017-03-01 02:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2018-10-03 23:29:25 +00:00
|
|
|
// Find the light that belongs to this texel in the shadowmap texture we output to:
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
int lightIndex = int(gl_FragCoord.y);
|
|
|
|
|
|
|
|
vec4 light = lights[lightIndex];
|
|
|
|
float radius = light.w;
|
|
|
|
vec2 lightpos = light.xy;
|
|
|
|
|
|
|
|
if (radius > 0.0)
|
2017-03-02 15:08:15 +00:00
|
|
|
{
|
2018-10-03 23:29:25 +00:00
|
|
|
// We found an active light. Calculate the ray direction for the texel.
|
|
|
|
//
|
|
|
|
// The texels are laid out so that there are four projections:
|
|
|
|
//
|
|
|
|
// * top-left to top-right
|
|
|
|
// * top-right to bottom-right
|
|
|
|
// * bottom-right to bottom-left
|
|
|
|
// * bottom-left to top-left
|
|
|
|
//
|
|
|
|
vec2 raydir;
|
2018-10-03 22:46:17 +00:00
|
|
|
float u = gl_FragCoord.x / ShadowmapQuality * 4.0;
|
|
|
|
switch (int(u))
|
2017-03-02 15:08:15 +00:00
|
|
|
{
|
2018-10-03 23:29:25 +00:00
|
|
|
case 0: raydir = vec2(u * 2.0 - 1.0, 1.0); break;
|
|
|
|
case 1: raydir = vec2(1.0, 1.0 - (u - 1.0) * 2.0); break;
|
|
|
|
case 2: raydir = vec2(1.0 - (u - 2.0) * 2.0, -1.0); break;
|
|
|
|
case 3: raydir = vec2(-1.0, (u - 3.0) * 2.0 - 1.0); break;
|
2017-03-02 15:08:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:29:25 +00:00
|
|
|
// Find the position for the ray starting at the light position and travelling until light contribution is zero:
|
|
|
|
vec2 pixelpos = lightpos + raydir * radius;
|
|
|
|
|
|
|
|
// Check if we hit any line between the light and the end position:
|
2017-03-07 14:58:22 +00:00
|
|
|
float t = rayTest(lightpos, pixelpos);
|
2018-10-03 23:29:25 +00:00
|
|
|
|
|
|
|
// Calculate the square distance for the hit, if any:
|
2017-03-07 14:58:22 +00:00
|
|
|
vec2 delta = (pixelpos - lightpos) * t;
|
2017-03-02 15:08:15 +00:00
|
|
|
float dist2 = dot(delta, delta);
|
|
|
|
|
|
|
|
FragColor = vec4(dist2, 0.0, 0.0, 1.0);
|
|
|
|
}
|
2017-03-01 16:17:33 +00:00
|
|
|
else
|
2017-03-02 15:08:15 +00:00
|
|
|
{
|
2017-03-01 16:17:33 +00:00
|
|
|
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
2017-03-02 15:08:15 +00:00
|
|
|
}
|
2017-03-01 02:33:53 +00:00
|
|
|
}
|