diff options
| Commit | c73c206450d22c9ada83b8026588c1bdf0a7e10f (patch) | |
|---|---|---|
| Author | 2023-08-28 15:12:22 +0300 | |
| Committer | 2023-08-28 15:12:22 +0300 | |
| Files | 5a9b3152d8077ebac089c04c59c6b46f553df4c4 | |
| uni-projects-c73c206450d22c9ada83b8026588c1bdf0a7e10f.tar uni-projects-c73c206450d22c9ada83b8026588c1bdf0a7e10f.tar.gz uni-projects-c73c206450d22c9ada83b8026588c1bdf0a7e10f.zip | ||
| Parent | 8522d61351796b1c88c41bfc2b8b4fa130c73371 (diff) | |
[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
1 files changed, 18 insertions, 5 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/Project/shaders.js | 23 |
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;
}
|
