diff options
[WGL/P] Implemented very roughly simple ray marching, without support for anything
1 files changed, 26 insertions, 1 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/Project/shaders.js | 27 |
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);
}
`;
|
