From 7526ea8de723888211101278198dc7a454024b84 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 26 Aug 2023 16:14:05 +0300 Subject: [WGL/P] Implemented crude ray rotation around black holes --- .../Project/shaders.js | 93 ++++++++++++++++------ 1 file changed, 67 insertions(+), 26 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 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); } `; -- cgit v1.3.1-26-gb76a