diff options
| Commit | 7526ea8de723888211101278198dc7a454024b84 (patch) | |
|---|---|---|
| Author | 2023-08-26 16:14:05 +0300 | |
| Committer | 2023-08-26 16:14:05 +0300 | |
| Files | bee44f2bbc84522979e172f824b76ecbed707401 | |
| uni-projects-7526ea8de723888211101278198dc7a454024b84.tar uni-projects-7526ea8de723888211101278198dc7a454024b84.tar.gz uni-projects-7526ea8de723888211101278198dc7a454024b84.zip | ||
| Parent | 7994fe1a000768b6ad21997039ab5960d4c530af (diff) | |
[WGL/P] Implemented crude ray rotation around black holes
4 files changed, 75 insertions, 31 deletions
diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index fc9bae8..29d26f1 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -103,9 +103,12 @@ h2 { skybox = new Skybox(10000, [0,0,0], undefined);
- star = new Star([1000,0,1000], 50)
+ stars = [
+ new Star([1000,0,1000], 50),
+ // new Star([1000,0,900], 30),
+ new Star([950,0,1100], 40),
+ ];
// new Star([1000,0,-1000], 50)
- // new Star([950,0,1100], 40)
// new Star([0,0,0], 100)
// new Star([1000, 0, -30], 20);
for (var i = 0; i < starsBuf.length; i++) {
@@ -137,7 +140,7 @@ h2 { gl.uniform3fv(uEye, pos);
skybox.draw();
- star.draw();
+ stars.forEach((s) => s.draw());
requestAnimationFrame(drawFrame);
}
diff --git a/Year 2/Computer Graphics with WebGL/Project/blackhole.js b/Year 2/Computer Graphics with WebGL/Project/blackhole.js index 5093bc4..261a541 100644 --- a/Year 2/Computer Graphics with WebGL/Project/blackhole.js +++ b/Year 2/Computer Graphics with WebGL/Project/blackhole.js @@ -1,7 +1,7 @@ blackHolesBuf = []; BlackHole = function(center, size) { - this.rs = 2/3 * size; + this.rs = size / 3; blackHolesBuf.push([center[0], center[1], center[2], this.rs]); } BlackHole.prototype.draw = function() { diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index c34ab1c..9b9cf1e 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -33,7 +33,18 @@ `;
var fShader = `
- precision mediump float;
+ precision highp float;
+
+ uniform mat4 uProjectionMatrix;
+ uniform mat4 uViewMatrix;
+ uniform mat4 uModelMatrix;
+ uniform mat4 uNormalMatrix;
+ uniform bool uUseNormalMatrix;
+
+ uniform vec3 uAmbientColor;
+ uniform vec3 uDiffuseColor;
+
+ uniform vec3 uLightDir;
varying vec3 vColor;
varying vec3 vXYZ;
@@ -48,31 +59,62 @@ var fShader = ` return length((point - linePoint) - dot(point - linePoint, lineDir) * lineDir);
}
- // vec4 rayMarchInBH(vec4 BH, vec3 ray, vec3 step) {
- // for (int i = 0; i < 100; i++) {
- // float dist = linePointDistance(ray, ray+step, BH.xyz);
- // if (dist <= BH.a) { // if past event horizon
- // return vec4(0,0,0,1);
- // }
- // if (dist > BH.a * 1.5) { // if outside photon sphere
- // return rayMarch(ray, step);
- // }
- // step += bend; // ROADBLOCK
- // ray += step;
- // }
- // return vec4(0,0,0,1);
- // }
+ // Thanks https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection#Calculation_using_vectors_in_3D
+ vec3 spherePoint(vec3 lineOrigin, vec3 lineDir, vec4 sphere) {
+ lineDir = normalize(lineDir);
+ float b = dot(lineDir, lineOrigin - sphere.xyz);
+ float Delta = b * b - (length(lineOrigin - sphere.xyz) * length(lineOrigin - sphere.xyz) - sphere.a * sphere.a);
+ return lineOrigin + min(-b + sqrt(Delta), -b - sqrt(Delta)) * lineDir;
+ }
+
+ int rayMarchInBH(vec4 BH, inout vec3 ray, inout vec3 step) {
+ ray += step * linePointDistance(ray, step, BH.xyz);
+ for (int i = 0; i < 100; i++) {
+ float dist = linePointDistance(ray, step, BH.xyz);
+ if (dist <= BH.a) { // if inside event horizon
+ return 1;
+ }
+ if (dist > BH.a * 3.0) { // if outside ISCO
+ return 2;
+ }
+ step += normalize(BH.xyz - ray) * (3.5 - dist / BH.a) * 0.05;
+ ray += step;
+ }
+ return 0; // inside photon sphere
+ }
+
+ vec4 calculateColor(bool notBent, vec4 star, vec3 ray, vec3 step) {
+ if (notBent) {
+ return vec4(vColor,linePointDistance(ray, step, star.xyz) / star.a + 0.1);
+ }
+
+ // vec3 anormal = spherePoint(ray, step, star) - star.xyz;
+ //
+ // mat4 mvMatrix = uViewMatrix * uModelMatrix;
+ // mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
+ // vec3 color = uAmbientColor*vec3(0.95,0.5,0.215);
+ // vec3 light = normalize(-uLightDir);
+ // vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0)));
+ // color += vec3(0.95,0.5,0.215)*uDiffuseColor*max(dot(normal,light),0.0);
+
+ return vec4(0.95,0.5,0.215,1);
+ }
vec4 rayMarch(vec3 ray, vec3 step) {
- // for (int i = 0; i < 5; i++) {
- // if (linePointDistance(ray, ray+step, uBH[i].xyz) <= uBH[i].a * 1.5) {
- // return rayMarchInBh(uBH, ray, step);
- // }
- // }
+ int bhStatus = 3;
+ for (int i = 0; i < 5; i++) {
+ if (linePointDistance(ray, step, uBH[i].xyz) < uBH[i].a * 3.0) {
+ bhStatus = rayMarchInBH(uBH[i], ray, step);
+ }
+ }
- for (int i = 0; i < 1; i++) {
- if (linePointDistance(uEye, ray, uStars[i].xyz) < uStars[i].a) {
- return vec4(vColor, 1);
+ if (bhStatus < 2) {
+ return vec4(0,1,0,1);
+ }
+
+ for (int i = 0; i < 5; i++) {
+ if (linePointDistance(ray, step, uStars[i].xyz) < uStars[i].a) {
+ return calculateColor(bhStatus == 3, uStars[i], ray, step);
}
}
return vec4(1,0,0, 1);
@@ -80,8 +122,7 @@ var fShader = ` void main()
{
- vec3 ray = normalize(vXYZ - uEye);
-
- gl_FragColor = rayMarch(ray, ray);
+ vec3 step = normalize(vXYZ - uEye);
+ gl_FragColor = rayMarch(uEye, step);
}
`;
diff --git a/Year 2/Computer Graphics with WebGL/Project/spaceship.js b/Year 2/Computer Graphics with WebGL/Project/spaceship.js index 075ae4d..3f88b69 100644 --- a/Year 2/Computer Graphics with WebGL/Project/spaceship.js +++ b/Year 2/Computer Graphics with WebGL/Project/spaceship.js @@ -62,7 +62,7 @@ function vecSub(a, b) { const MOVE_SPEED = 1.5; const ROTATION_SPEED = 3; -var pos = [1000, 0, 30]; +var pos = [1000, 0, 650]; var forward = new SphericalCoord(Math.PI / 2, 0); var up = new SphericalCoord(0, 0); |
