Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / shaders.js
blob: 3fe1e2732260edd1adeeb207147f6c9e5e601c41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var vShader =`
	uniform mat4 uProjectionMatrix;
	uniform mat4 uViewMatrix;
	uniform mat4 uModelMatrix;
	uniform mat4 uNormalMatrix;
	uniform bool uUseNormalMatrix;
	
	uniform vec3 uAmbientColor;
	uniform vec3 uDiffuseColor;
	
	uniform vec3 uLightDir;
	
	attribute vec3 aXYZ;
	attribute vec3 aColor;
	attribute vec3 aNormal;

	varying vec3 vColor;
	varying vec3 vXYZ;
	
	void main ()
	{
		mat4 mvMatrix = uViewMatrix * uModelMatrix;
		gl_Position = uProjectionMatrix * mvMatrix * vec4(aXYZ,1);
		mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
	
		vColor = uAmbientColor*aColor;
	
		vec3 light = normalize(-uLightDir);
		vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
		vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0);
		vXYZ = (uModelMatrix * vec4(aXYZ,1)).xyz;
	}
`;
	
var fShader = `
	precision highp float;

	#define MORE_MAGIC 0.01

	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;

	uniform vec4 uStars[5];
	uniform vec4 uBH[5];

	uniform vec3 uEye;

	// Thanks https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation
	float linePointDistance(vec3 linePoint, vec3 lineDir, vec3 point) {
		return length((point - linePoint) - dot(point - linePoint, lineDir) * lineDir);
	}

	// 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;
	}

	// Thanks https://en.wikipedia.org/wiki/Dot_product#Geometric_definition
	float angleBetweenVectors(vec3 v1, vec3 v2) {
		return acos(radians(dot(v1, v2) / (length(v1) * length(v2))));
	}

	int rayMarchInBH(vec4 BH, inout vec3 ray, inout vec3 step) {
		ray = spherePoint(ray, step, vec4(BH.xyz, BH.a * 3.0));

		// h is used in the step changing calculation
		float h = length(cross(ray, step));

		for (int i = 0; i < 100; i++) {
			float dist = distance(ray, BH.xyz);

			if (dist <= BH.a) { // if inside event horizon
				return 1;
			}
			if (dist > BH.a * 3.0 + 1.0) { // if outside ISCO
				return 2;
			}

			// Gravity bends light (actually spacetime, which in turn changes it's path for outside observers).
			// This is a more or less correct (as far as I can tell) implementation of Riccardo Antonelli's formula
			// http://rantonels.github.io/starless/#MathJax-Element-29-Frame
			// Though I found it difficult to understand, so I referred to some of the code from
			// https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py
			// and
			// https://github.com/dbrant/BlackHoleRaytracer/blob/master/Equation/SchwarzschildBlackHoleEquation.cs
			step += normalize(ray - BH.xyz) * -1.5 * (h*h) * MORE_MAGIC / (dist*dist*dist*dist*dist);
			ray += step;
		}
		return 0; // 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);
		}

		// 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) {
		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);
			}
		}

		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) {
			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 vec4(1,0,0, 1);
	}

	void main()
	{
		vec3 step = normalize(vXYZ - uEye);
		gl_FragColor = rayMarch(uEye, step);
	}
`;