Skip to content

Commit

Permalink
[rend2] Fix volume shadow inaccuracies
Browse files Browse the repository at this point in the history
  • Loading branch information
SomaZ committed Feb 19, 2024
1 parent e09e1cb commit 05415f8
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions shared/rd-rend2/glsl/shadowvolume.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,6 @@ layout(std140) uniform Entity

in vec3 var_Position[];

void quad(in vec3 first, in vec3 second, in vec3 L, in mat4 MVP)
{
gl_Position = MVP * vec4(first, 1.0);
EmitVertex();
gl_Position = MVP * vec4(first - L, 1.0);
EmitVertex();
gl_Position = MVP * vec4(second, 1.0);
EmitVertex();
gl_Position = MVP * vec4(second - L, 1.0);
EmitVertex();
EndPrimitive();
}

void main()
{
vec3 BmA = var_Position[1].xyz - var_Position[0].xyz;
Expand All @@ -106,27 +93,62 @@ void main()

if (dot(cross(BmA,CmA), -u_ModelLightDir.xyz) > 0.0) {
vec3 L = u_ModelLightDir.xyz*u_LocalLightOrigin.w;

vec4 positions[6] = vec4[6](
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[0], 1.0),
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[1], 1.0),
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[2], 1.0),
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[0] - L, 1.0),
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[1] - L, 1.0),
u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[2] - L, 1.0)
);

// front cap, avoids z-fighting with other shaders by NOT using the MVP, the other surfaces won't create z-fighting
gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[0].xyz, 1.0);
gl_Position = positions[0];
EmitVertex();
gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[1].xyz, 1.0);
gl_Position = positions[1];
EmitVertex();
gl_Position = u_viewProjectionMatrix * u_ModelMatrix * vec4(var_Position[2].xyz, 1.0);
gl_Position = positions[2];
EmitVertex();
EndPrimitive();

// sides
quad(var_Position[0], var_Position[1], L, MVP);
quad(var_Position[1], var_Position[2], L, MVP);
quad(var_Position[2], var_Position[0], L, MVP);
gl_Position = positions[0];
EmitVertex();
gl_Position = positions[3];
EmitVertex();
gl_Position = positions[1];
EmitVertex();
gl_Position = positions[4];
EmitVertex();
EndPrimitive();

gl_Position = positions[1];
EmitVertex();
gl_Position = positions[4];
EmitVertex();
gl_Position = positions[2];
EmitVertex();
gl_Position = positions[5];
EmitVertex();
EndPrimitive();

gl_Position = positions[2];
EmitVertex();
gl_Position = positions[5];
EmitVertex();
gl_Position = positions[0];
EmitVertex();
gl_Position = positions[3];
EmitVertex();
EndPrimitive();

// back cap
gl_Position = MVP * vec4(var_Position[2].xyz - L, 1.0);
gl_Position = positions[5];
EmitVertex();
gl_Position = MVP * vec4(var_Position[1].xyz - L, 1.0);
gl_Position = positions[4];
EmitVertex();
gl_Position = MVP * vec4(var_Position[0].xyz - L, 1.0);
gl_Position = positions[3];
EmitVertex();
EndPrimitive();
}
Expand Down

0 comments on commit 05415f8

Please sign in to comment.