From 53c352887fdb3e0345e97c2760d8f7985a1bfc48 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 28 Aug 2023 13:01:49 +0300 Subject: [WGL/P] Added a lot of comments and reworked formatting of certain code --- .../Project/shaders.js | 42 ++++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'Year 2/Computer Graphics with WebGL/Project/shaders.js') diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index a77e9b3..bd98880 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -1,5 +1,6 @@ const STAR_MAX_COUNT = 5; const BLACKHOLE_MAX_COUNT = 5; +const STAR_COLOR = [0.956,0.5,0.215]; var vShader =` uniform mat4 uProjectionMatrix; @@ -36,22 +37,28 @@ var vShader =` `; var fShader = ` - precision highp float; + precision highp float; // Needed, since we'll get uniforms which were in vertex shader + // Distance a ray should travel inside a black hole, + // changing it can alter weirdly the amount of rays that don't escape the black hole #define MAGIC_STEP 0.01 - #define STAR_COLOR vec3(0.95,0.5,0.215) varying vec3 vColor; varying vec3 vXYZ; + // This is dumb, but we can't just pass the color from vertex shader, because + // we could need it when the fragment shader is ran on the skybox (or just not on a star) + // And in those cases, we recalculate the star color, based on where our ray landed + uniform vec3 uStarColor; + // Settings uniform bool uDisableRayMarching; uniform vec4 uBlack; uniform vec4 uBHBlack; // For ray marching - uniform vec4 uStars[` + STAR_MAX_COUNT + `]; - uniform vec4 uBH[` + BLACKHOLE_MAX_COUNT + `]; + uniform vec4 uStars[`+STAR_MAX_COUNT+`]; + uniform vec4 uBH[`+BLACKHOLE_MAX_COUNT+`]; uniform vec3 uEye; // For light recalculation @@ -66,7 +73,8 @@ var fShader = ` uniform vec3 uLightDir; - // Thanks https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation + // Direction implementation of the math from + // https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation float linePointDistance(vec3 linePoint, vec3 lineDir, vec3 point) { vec3 projectionDir = dot(point - linePoint, lineDir) * lineDir; // aka projectionVector - linePoint float dist = length((point - linePoint) - projectionDir); // aka length of perpendicular vector @@ -76,7 +84,8 @@ var fShader = ` return dist; } - // Thanks https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection#Calculation_using_vectors_in_3D + // Direct implementation of the math from + // 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); @@ -84,7 +93,8 @@ var fShader = ` return lineOrigin + min(-b + sqrt(Delta), -b - sqrt(Delta)) * lineDir; } - // Thanks https://en.wikipedia.org/wiki/Dot_product#Geometric_definition + // Direct implementation of the math from + // https://en.wikipedia.org/wiki/Dot_product#Geometric_definition float angleBetweenVectors(vec3 v1, vec3 v2) { return acos(radians(dot(v1, v2) / (length(v1) * length(v2)))); } @@ -98,17 +108,19 @@ var fShader = ` for (int i = 0; i < 100; i++) { float dist = distance(ray, BH.xyz); - if (dist <= BH.a) { // if inside event horizon + // if inside event horizon + if (dist <= BH.a) { return 1; } - if (dist > BH.a * 3.0 + 1.0) { // if outside ISCO + // if outside ISCO + if (dist > BH.a * 3.0 + 1.0) { // adding 1 fixes some rays wrongfully passing directly through return 0; } // Gravity bends light (actually spacetime, which in turn changes it's path for outside observers). // This is a more or less correct (as far as I can tell) implementation of Riccardo Antonelli's formula // http://rantonels.github.io/starless/#MathJax-Element-29-Frame - // Though I found it difficult to understand, so I referred to some of the code from + // Though I found it difficult to understand, so I referred to some of it's implementations from // https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py // and // https://github.com/dbrant/BlackHoleRaytracer/blob/master/Equation/SchwarzschildBlackHoleEquation.cs @@ -130,10 +142,10 @@ var fShader = ` mat4 mvMatrix = uViewMatrix * uModelMatrix; mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix; - vec3 color = uAmbientColor*STAR_COLOR; + vec3 color = uAmbientColor*uStarColor; vec3 light = normalize(-uLightDir); vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0))); - color += STAR_COLOR*uDiffuseColor*max(dot(normal,light),0.0); + color += uStarColor*uDiffuseColor*max(dot(normal,light),0.0); return vec4(color, 1); } @@ -143,7 +155,7 @@ var fShader = ` vec4 rayMarch(vec3 ray, vec3 step) { // Does the ray pass through any blackholes int bhStatus = 2; - for (int i = 0; i < 5; i++) { + 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); @@ -154,8 +166,8 @@ var fShader = ` return uBHBlack; } - // Does the ray hit a star - for (int i = 0; i < 5; i++) { + // 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) { return calculateColor(bhStatus == 2, uStars[i], ray, step); -- cgit v1.3.1-26-gb76a