From 6dd51a351bebd3347c1c4d9d8caf4d7b2f2a4338 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 25 Aug 2023 13:39:13 +0300 Subject: [WGL/P] Implemented very roughly simple ray marching, without support for anything --- .../Project/shaders.js | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (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 3821899..d9597df 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -13,6 +13,9 @@ attribute vec3 aXYZ; attribute vec3 aColor; attribute vec3 aNormal; + + attribute vec4 aSphere; + varying vec4 vSphere; varying vec3 vColor; varying vec3 vXYZ; @@ -29,6 +32,8 @@ 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; } `; @@ -37,9 +42,29 @@ var fShader = ` varying vec3 vColor; varying vec3 vXYZ; + varying vec4 vSphere; + + uniform vec3 uEye; + uniform vec3 uEyeForward; void main() { - gl_FragColor = vec4(vColor,1.0); + vec3 ray = uEye; + vec3 step = normalize(uEyeForward); + + 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; + } + ray += step * dist; + } + gl_FragColor = vec4(0,0,0,1); } `; -- cgit v1.3.1-26-gb76a