From 866b2608dfbc53dcd968bdf3261005a9e0be8b36 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 25 Aug 2023 21:26:38 +0300 Subject: [WGL/P] Added most proper shader code, now have two roadblocks --- .../Project/shaders.js | 66 ++++++++++++++-------- 1 file changed, 44 insertions(+), 22 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 d9597df..6dda637 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -14,9 +14,6 @@ attribute vec3 aColor; attribute vec3 aNormal; - attribute vec4 aSphere; - varying vec4 vSphere; - varying vec3 vColor; varying vec3 vXYZ; @@ -32,8 +29,6 @@ vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0))); vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0); vXYZ = (uModelMatrix * vec4(aXYZ,1)).xyz; - - vSphere = aSphere; } `; @@ -42,29 +37,56 @@ var fShader = ` varying vec3 vColor; varying vec3 vXYZ; - varying vec4 vSphere; + + uniform vec2 uViewPort; + uniform vec4 uStars[5]; + uniform vec4 uBH[5]; uniform vec3 uEye; uniform vec3 uEyeForward; + uniform vec3 uEyeUp; + uniform vec3 uEyeLeft; - void main() - { - vec3 ray = uEye; - vec3 step = normalize(uEyeForward); + // Thanks https://math.stackexchange.com/a/1905581 + float linePointDistance(vec3 line1v, vec3 line2v, vec3 point) { + return length(cross(point - line1v, line2v - line1v)) / length(line2v - line1v); + } + + // 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); + // } - for (int i = 0; i < 100; i++) { - float dist = distance(ray, vSphere.xyz) - vSphere.a; - if (dist < 0.001) { - if (vSphere.a == 0.0) { - gl_FragColor = vec4(vColor,1.0); - } - else { - gl_FragColor = vec4(1,0,0,1); - } - return; + 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); + // } + // } + + for (int i = 0; i < 5; i++) { + if (linePointDistance(ray, ray + step, uStars[i].xyz) <= uStars[i].a) { + return vec4(vColor, 1); } - ray += step * dist; } - gl_FragColor = vec4(0,0,0,1); + return vec4(1,0,0, 1); + } + + void main() + { + vec3 ray = uEye + uEyeForward + vXYZ.y * uEyeUp + vXYZ.x * uEyeLeft; // ROADBLOCK + vec3 step = normalize(ray - uEye); + + gl_FragColor = rayMarch(ray, step); } `; -- cgit v1.3.1-26-gb76a