From c73c206450d22c9ada83b8026588c1bdf0a7e10f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 28 Aug 2023 15:12:22 +0300 Subject: [WGL/P/shaders] Moved ray hit detection to it's own function and added support for stars that are in front of a black hole --- .../Project/shaders.js | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'Year 2/Computer Graphics with WebGL/Project') diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index 86c6faf..5dcc2ae 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -150,15 +150,28 @@ var fShader = ` return vec4(color, 1); } + // Does the ray, when going in the direction step, eventually hit the surface of the sphere + bool hitsSphere(vec3 ray, vec3 step, vec4 sphere) { + float dist = linePointDistance(ray, step, sphere.xyz); + return dist >= 0.0 && dist < sphere.a; + } + // I think this slightly differs than the norm, but ray is the current position of my ray // and step is a direction vector, so moving the ray forward is as simple as ray += step vec4 rayMarch(vec3 ray, vec3 step) { // Does the ray pass through any blackholes int bhStatus = 2; for (int i = 0; i < `+BLACKHOLE_MAX_COUNT+`; i++) { - float dist = linePointDistance(ray, step, uBH[i].xyz); - if (dist >= 0.0 && dist < uBH[i].a * 3.0) { - bhStatus = rayMarchInBH(uBH[i], ray, step); + if (hitsSphere(ray, step, vec4(uBH[i].xyz, uBH[i].a * 3.0))) { + + // Try to enter black hole, only if there isn't a star that is in front + float bhDist = distance(uBH[i].xyz, ray); + for (int j = 0; j < `+STAR_MAX_COUNT+`; j++) { + if (distance(uStars[j].xyz, ray) < bhDist && hitsSphere(ray, step, uStars[j])) + bhDist = -1.0; + } + if (bhDist > -1.0) + bhStatus = rayMarchInBH(uBH[i], ray, step); } } @@ -168,11 +181,11 @@ var fShader = ` // Does the ray hit a star (even when it has escaped a black hole) for (int i = 0; i < `+STAR_MAX_COUNT+`; i++) { - float dist = linePointDistance(ray, step, uStars[i].xyz); - if (dist >= 0.0 && dist < uStars[i].a) { + if (hitsSphere(ray, step, uStars[i])) { return calculateColor(bhStatus == 2, uStars[i], ray, step); } } + return uBlack; } -- cgit v1.3.1-26-gb76a