diff options
[WGL/P] Slight code reorganisation, moved buffer logic for black holes in BlackHole and exported count limits as global variables
1 files changed, 34 insertions, 30 deletions
| -rw-r--r-- | Year 2/Computer Graphics with WebGL/Project/shaders.js | 64 |
diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index 3fe1e27..88e57c8 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -1,4 +1,7 @@ -var vShader =`
+const STAR_MAX_COUNT = 5;
+const BLACKHOLE_MAX_COUNT = 5;
+
+var vShader =`
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
@@ -36,7 +39,17 @@ var fShader = ` precision highp float;
#define MORE_MAGIC 0.01
+ #define STAR_COLOR vec3(0.95,0.5,0.215)
+ varying vec3 vColor;
+ varying vec3 vXYZ;
+
+ // For ray marching
+ uniform vec4 uStars[` + STAR_MAX_COUNT + `];
+ uniform vec4 uBH[` + BLACKHOLE_MAX_COUNT + `];
+ uniform vec3 uEye;
+
+ // For light recalculation
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
@@ -46,15 +59,7 @@ var fShader = ` uniform vec3 uAmbientColor;
uniform vec3 uDiffuseColor;
- uniform vec3 uLightDir;
-
- varying vec3 vColor;
- varying vec3 vXYZ;
-
- uniform vec4 uStars[5];
- uniform vec4 uBH[5];
-
- uniform vec3 uEye;
+ uniform vec3 uLightDir;
// Thanks https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation
float linePointDistance(vec3 linePoint, vec3 lineDir, vec3 point) {
@@ -87,7 +92,7 @@ var fShader = ` return 1;
}
if (dist > BH.a * 3.0 + 1.0) { // if outside ISCO
- return 2;
+ return 0;
}
// Gravity bends light (actually spacetime, which in turn changes it's path for outside observers).
@@ -100,48 +105,47 @@ var fShader = ` step += normalize(ray - BH.xyz) * -1.5 * (h*h) * MORE_MAGIC / (dist*dist*dist*dist*dist);
ray += step;
}
- return 0; // orbits inside
+ return 1; // orbits inside
}
vec4 calculateColor(bool notBent, vec4 star, vec3 ray, vec3 step) {
if (notBent) {
- return vec4(vColor,linePointDistance(ray, step, star.xyz) / star.a + 0.1);
+ return vec4(vColor,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);
+ // This is an exact copy from the vertex shader light calculations
+ // Since the ray was bent, we don't know what exactly it will hit, so we need to recalculate
+ // This probably isn't entirely correct, but I like how it looks
+ vec3 anormal = spherePoint(ray, step, star) - star.xyz;
+ mat4 mvMatrix = uViewMatrix * uModelMatrix;
+ mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
+
+ vec3 color = uAmbientColor*STAR_COLOR;
+ vec3 light = normalize(-uLightDir);
+ vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0)));
+ color += STAR_COLOR*uDiffuseColor*max(dot(normal,light),0.0);
- return vec4(0.95,0.5,0.215,1);
+ return vec4(color, 1);
}
vec4 rayMarch(vec3 ray, vec3 step) {
- int bhStatus = 3;
+ int bhStatus = 2;
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);
}
}
- if (bhStatus == 0) return vec4(1,0,1,1);
- if (bhStatus == 1) return vec4(0,1,0,1);
- // if (bhStatus == 2) return vec4(1,1,0,1);
- if (bhStatus < 2) {
+ if (bhStatus == 1) {
return vec4(0,1,0,1);
- // return vec4(1,0,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 calculateColor(bhStatus == 2, uStars[i], ray, step);
}
}
- return vec4(1,0,0, 1);
+ return vec4(1,0,0,1);
}
void main()
|
