Skip to content
Snippets Groups Projects

fixed shader bug for ray tracing (Points, Lines) in orthographic rendering mode

Merged Aaron Kreuzberg requested to merge ak/shader_ortho into develop
Files
2
@@ -356,7 +356,18 @@ void LineRenderable::init()
vOut.LineWidth = 2 * s;
}
vec3 viewDir = normalize(pos0 - uCamPos);
vec4 pos0VS = uView * vec4(pos0, 1);
vec4 pos0CS = uProj * pos0VS;
vec3 pos0NDC = pos0CS.xyz / pos0CS.w;
// clip position to near plane in NDC and get ray origin by transforming into WS (required for orthographic rendering)
vec4 p0NearNDC = vec4(pos0NDC.xy, -1, 1);
vec4 p0NearCS = p0NearNDC * pos0CS.w;
vec4 p0NearVS = uInvProj * p0NearCS;
p0NearVS /= p0NearVS.w;
vec4 p0NearWS = uInvView * p0NearVS;
vec3 viewDir = normalize(pos0 - p0NearWS.xyz);
vec3 diff = pos1 - pos0;
vec3 right = normalize(diff);
vec3 up = normalize(cross(right, viewDir));
@@ -394,7 +405,18 @@ void LineRenderable::init()
vOut.LineWidth = 2 * s;
}
viewDir = normalize(pos1 - uCamPos);
vec4 pos1VS = uView * vec4(pos1, 1);
vec4 pos1CS = uProj * pos1VS;
vec3 pos1NDC = pos1CS.xyz / pos1CS.w;
// clip position to near plane in NDC and get ray origin by transforming into WS (required for orthographic rendering)
vec4 p1NearNDC = vec4(pos1NDC.xy, -1, 1);
vec4 p1NearCS = p1NearNDC * pos1CS.w;
vec4 p1NearVS = uInvProj * p1NearCS;
p1NearVS /= p1NearVS.w;
vec4 p1NearWS = uInvView * p1NearVS;
viewDir = normalize(pos1 - p1NearWS.xyz);
up = normalize(cross(right, viewDir));
back = normalize(cross(right, up));
r = s * right;
@@ -521,8 +543,14 @@ float distance2(vec3 a, vec3 b)
// s: the amount to go back on the ray dir to get to the intersection point
// TODO in this shader code
sb.addFragmentShaderCode(R"(
vec3 rayOrigin = uCamPos;
vec3 rayDir = normalize(vIn.fragPosWS - uCamPos);
// clip FragCoord to near plane in NDC and transform back into WS (required for orhtographic rendering)
vec3 rayOriginNDC = vec3((gl_FragCoord.x / uScreenSize.x) * 2 - 1, (gl_FragCoord.y / uScreenSize.y) * 2 - 1, uIsReverseZEnabled ? 1 : -1);
vec4 rayOriginCS = vec4(rayOriginNDC,1) / gl_FragCoord.w;
vec4 rayOriginVS = (uInvProj * rayOriginCS);
rayOriginVS /= rayOriginVS.w;
vec3 rayOrigin = (uInvView * rayOriginVS).xyz;
vec3 rayDir = normalize(vIn.fragPosWS - rayOrigin);
float cosA = dot(gLineDir, rayDir);
float sinA2 = 1 - cosA * cosA;
Loading